FS-Cache: NFS(/CIFS/Ceph/9P): Fix start-caching whilst writing race
Subsystems (11)
David Howells
maintainercover FS-Cache: NFS(/CIFS/Ceph/9P): Fix start-caching whilst writing race
Following this mail is a series of patches to fix an issue with NFS whereby caching can be enabled on a file that is open for writing by subsequently opening it for reading. This can be made to crash by opening it for writing again if you're quick enough.
The patches mailed here may not apply directly. They are based on top of three fix patches as can be seen in the GIT branch here:
http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=fscache
The gist of the patchset is that the cookie should be acquired at inode creation only and subsequently enabled and disabled as appropriate (which dispenses with the backing objects when they're not needed).
The extra synchronisation that NFS does can then be dispensed with as it is thenceforth managed by FS-Cache.
This likely will need applying to CIFS, Ceph and 9P also once this is okayed for NFS. AFS is probably safe.
David
---
David Howells (3):
FS-Cache: Add use/unuse/wake cookie wrappers
FS-Cache: Provide the ability to enable/disable cookies
NFS: Use i_writecount to control whether to get an fscache cookie in nfs_open()
Documentation/filesystems/caching/netfs-api.txt | 73 +++++++-- fs/9p/cache.c | 6 - fs/afs/cell.c | 2 fs/afs/inode.c | 2 fs/afs/vlocation.c | 3 fs/afs/volume.c | 2 fs/cachefiles/interface.c | 2 fs/ceph/cache.c | 4 fs/cifs/fscache.c | 8 - fs/fscache/cookie.c | 193 +++++++++++++++++------ fs/fscache/fsdef.c | 1 fs/fscache/netfs.c | 1 fs/fscache/object.c | 7 + fs/fscache/page.c | 59 +++++-- fs/nfs/dir.c | 2 fs/nfs/fscache.c | 196 +++++++---------------- fs/nfs/fscache.h | 18 +- fs/nfs/inode.c | 6 - fs/nfs/nfs4file.c | 2 include/linux/fs.h | 5 + include/linux/fscache-cache.h | 50 ++---- include/linux/fscache.h | 113 ++++++++++++- include/linux/nfs_fs.h | 8 + 23 files changed, 462 insertions(+), 301 deletions(-)
David Howells
maintainerPATCH 1/3 FS-Cache: Add use/unuse/wake cookie wrappers
Add wrapper functions for dealing with cookie->n_active:
(*) __fscache_use_cookie() to increment it.
(*) __fscache_unuse_cookie() to decrement and test against zero.
(*) __fscache_wake_unused_cookie() to wake up anyone waiting for it to reach
zero.
The second and third are split so that the third can be done after cookie->lock has been released in case the waiter wakes up whilst we're still holding it and tries to get it.
We will need to wake-on-zero once the cookie disablement patch is applied because it will then be possible to see n_active become zero without the cookie being relinquished.
Also move the cookie usement out of fscache_attr_changed_op() and into fscache_attr_changed() and the operation struct so that cookie disablement will be able to track it.
Whilst we're at it, only increment n_active if we're about to do fscache_submit_op() so that we don't have to deal with undoing it if anything earlier fails. Possibly this should be moved into fscache_submit_op() which could look at FSCACHE_OP_UNUSE_COOKIE.
---
fs/fscache/cookie.c | 7 +++++-- fs/fscache/page.c | 42 ++++++++++++++++++++++++++++++----------- include/linux/fscache-cache.h | 19 +++++++++++++++++-- 3 files changed, 53 insertions(+), 15 deletions(-)
fs/fscache/cookie.c +5 -2
@@ -568,6 +568,7 @@ int __fscache_check_consistency(struct fscache_cookie *cookie) {struct fscache_operation *op;struct fscache_object *object; +bool wake_cookie = false;int ret;_enter ("%p," , cookie);
@@ -600,7 +601,7 @@ int __fscache_check_consistency(struct fscache_cookie *cookie) op->debug_id =atomic_inc_return (&fscache_op_debug_id); -atomic_inc (&cookie->n_active); +__fscache_use_cookie (cookie);if (fscache_submit_op (object, op) <0 ) goto submit_failed;
@@ -622,9 +623,11 @@ int __fscache_check_consistency(struct fscache_cookie *cookie)return ret; submit_failed: -atomic_dec (&cookie->n_active); + wake_cookie =__fscache_unuse_cookie (cookie); inconsistent:spin_unlock (&cookie->lock); +if (wake_cookie) +__fscache_wake_unused_cookie (cookie);kfree (op);_leave (" = -ESTALE" );return -ESTALE ;
fs/fscache/page.c +31 -11
173 lines not on this page · raw message
include/linux/fscache-cache.h +17 -2
@@ -511,6 +511,11 @@ static inline void fscache_end_io(struct fscache_retrieval *op, op->end_io_func (page, op->context, error); } +static inline void __fscache_use_cookie (struct fscache_cookie *cookie) +{ +atomic_inc (&cookie->n_active); +} +/** * fscache_use_cookie - Request usage of cookie attached to an object * @object: Object description
@@ -524,6 +529,16 @@ static inline bool fscache_use_cookie(struct fscache_object *object)return atomic_inc_not_zero (&cookie->n_active) !=0 ; } +static inline bool __fscache_unuse_cookie (struct fscache_cookie *cookie) +{ +return atomic_dec_and_test (&cookie->n_active); +} + +static inline void __fscache_wake_unused_cookie (struct fscache_cookie *cookie) +{ +wake_up_atomic_t (&cookie->n_active); +} +/** * fscache_unuse_cookie - Cease usage of cookie attached to an object * @object: Object description
@@ -534,8 +549,8 @@ static inline bool fscache_use_cookie(struct fscache_object *object)static inline void fscache_unuse_cookie (struct fscache_object *object) {struct fscache_cookie *cookie = object->cookie; -if (atomic_dec_and_test (&cookie->n_active)) -wake_up_atomic_t (&cookie->n_active); +if (__fscache_unuse_cookie (cookie)) +__fscache_wake_unused_cookie (cookie); }/*
David Howells
maintainerPATCH 2/3 FS-Cache: Provide the ability to enable/disable cookies
In reply to David HowellsProvide the ability to enable and disable fscache cookies. A disabled cookie will reject or ignore further requests to:
Acquire a child cookie
Invalidate and update backing objects
Check the consistency of a backing object
Allocate storage for backing page
Read backing pages
Write to backing pages
but still allows:
Checks/waits on the completion of already in-progress objects Uncaching of pages
Relinquishment of cookies
Two new operations are provided:
(1) Disable a cookie:
void fscache_disable_cookie(struct fscache_cookie *cookie,
bool invalidate);
If the cookie is not already disabled, this locks the cookie against other dis/enablement ops, marks the cookie as being disabled, discards or invalidates any backing objects and waits for cessation of activity on any associated object.
This is a wrapper around a chunk split out of fscache_relinquish_cookie(), but it reinitialises the cookie such that it can be reenabled.
All possible failures are handled internally. The caller should consider calling fscache_uncache_all_inode_pages() afterwards to make sure all page markings are cleared up.
(2) Enable a cookie:
void fscache_enable_cookie(struct fscache_cookie *cookie,
bool (*can_enable)(void *data), void *data)
If the cookie is not already enabled, this locks the cookie against other dis/enablement ops, invokes can_enable() and, if the cookie is not an index cookie, will begin the procedure of acquiring backing objects.
The optional can_enable() function is passed the data argument and returns a ruling as to whether or not enablement should actually be permitted to begin.
All possible failures are handled internally. The cookie will only be marked as enabled if provisional backing objects are allocated.
A later patch will introduce these to NFS. Cookie enablement during nfs_open() is then contingent on i_writecount <= 0. can_enable() checks for a race between open(O_RDONLY) and open(O_WRONLY/O_RDWR). This simplifies NFS's cookie handling and allows us to get rid of open(O_RDONLY) accidentally introducing caching to an inode that's open for writing already.
One operation has its API modified:
(3) Acquire a cookie.
struct fscache_cookie *fscache_acquire_cookie(
struct fscache_cookie *parent, const struct fscache_cookie_def *def, void *netfs_data,
bool enable);
This now has an additional argument that indicates whether the requested cookie should be enabled by default. It doesn't need the can_enable() function because the caller must prevent multiple calls for the same netfs object and it doesn't need to take the enablement lock because no one else can get at the cookie before this returns.
---
Documentation/filesystems/caching/netfs-api.txt | 73 +++++++-- fs/9p/cache.c | 6 - fs/afs/cell.c | 2 fs/afs/inode.c | 2 fs/afs/vlocation.c | 3 fs/afs/volume.c | 2 fs/cachefiles/interface.c | 2 fs/ceph/cache.c | 4 fs/cifs/fscache.c | 8 - fs/fscache/cookie.c | 186 +++++++++++++++++------ fs/fscache/fsdef.c | 1 fs/fscache/netfs.c | 1 fs/fscache/object.c | 7 + fs/fscache/page.c | 17 +- fs/nfs/fscache.c | 8 - include/linux/fscache-cache.h | 31 ---- include/linux/fscache.h | 113 ++++++++++++-- 17 files changed, 334 insertions(+), 132 deletions(-)
Documentation/filesystems/caching/netfs-api.txt +60 -13
@@ -29,15 +29,16 @@ This document contains the following sections: (6) Index registration (7) Data file registration (8) Miscellaneous object registration - (9) Setting the data file size + (9) Setting the data file size (10) Page alloc/read/write (11) Page uncaching (12) Index and data file consistency - (13) Miscellaneous cookie operations - (14) Cookie unregistration - (15) Index invalidation - (16) Data file invalidation - (17) FS-Cache specific page flags. + (13) Cookie enablement + (14) Miscellaneous cookie operations + (15) Cookie unregistration + (16) Index invalidation + (17) Data file invalidation + (18) FS-Cache specific page flags. =============================
@@ -334,7 +335,8 @@ the path to the file: struct fscache_cookie * fscache_acquire_cookie(struct fscache_cookie *parent, const struct fscache_object_def *def, - void *netfs_data); + void *netfs_data, + bool enable); This function creates an index entry in the index represented by parent, filling in the index entry by calling the operations pointed to by def.
@@ -350,6 +352,10 @@ object needs to be created somewhere down the hierarchy. Furthermore, an index may be created in several different caches independently at different times. This is all handled transparently, and the netfs doesn't see any of it. +A cookie will be created in the disabled state if enabled is false. A cookie +must be enabled to do anything with it. A disabled cookie can be enabled by +calling fscache_enable_cookie() (see below). + For example, with AFS, a cell would be added to the primary index. This index entry would have a dependent inode containing a volume location index for the volume mappings within this cell:
@@ -357,7 +363,7 @@ volume mappings within this cell: cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index, &afs_cell_cache_index_def, - cell); + cell, true); Then when a volume location was accessed, it would be entered into the cell's index and an inode would be allocated that acts as a volume type and hash chain
@@ -366,7 +372,7 @@ combination: vlocation->cache = fscache_acquire_cookie(cell->cache, &afs_vlocation_cache_index_def, - vlocation); + vlocation, true); And then a particular flavour of volume (R/O for example) could be added to that index, creating another index for vnodes (AFS inode equivalents):
@@ -374,7 +380,7 @@ that index, creating another index for vnodes (AFS inode equivalents): volume->cache = fscache_acquire_cookie(vlocation->cache, &afs_volume_cache_index_def, - volume); + volume, true); ======================
@@ -388,7 +394,7 @@ the object definition should be something other than index type. vnode->cache = fscache_acquire_cookie(volume->cache, &afs_vnode_cache_object_def, - vnode); + vnode, true); =================================
@@ -404,7 +410,7 @@ it would be some other type of object such as a data file. xattr->cache = fscache_acquire_cookie(vnode->cache, &afs_xattr_cache_object_def, - xattr); + xattr, true); Miscellaneous objects might be used to store extended attributes or directory entries for example.
@@ -733,6 +739,47 @@ Note that partial updates may happen automatically at other times, such as when data blocks are added to a data file object. +================= +COOKIE ENABLEMENT +================= + +Cookies exist in one of two states: enabled and disabled. If a cookie is +disabled, it ignores all attempts to acquire child cookies; check, update or +invalidate its state; allocate, read or write backing pages - though it is +still possible to uncache pages and relinquish the cookie. + +The initial enablement state is set by fscache_acquire_cookie(), but the cookie +can be enabled or disabled later. To disable a cookie, call: + + void fscache_disable_cookie(struct fscache_cookie *cookie, + bool invalidate); + +If the cookie is not already disabled, this locks the cookie against other +enable and disable ops, marks the cookie as being disabled, discards or +invalidates any backing objects and waits for cessation of activity on any +associated object before unlocking the cookie. + +All possible failures are handled internally. The caller should consider +calling fscache_uncache_all_inode_pages() afterwards to make sure all page +markings are cleared up. + +Cookies can be enabled or reenabled with: + + void fscache_enable_cookie(struct fscache_cookie *cookie, + bool (*can_enable)(void *data), + void *data) + +If the cookie is not already enabled, this locks the cookie against other +enable and disable ops, invokes can_enable() and, if the cookie is not an index +cookie, will begin the procedure of acquiring backing objects. + +The optional can_enable() function is passed the data argument and returns a +ruling as to whether or not enablement should actually be permitted to begin. + +All possible failures are handled internally. The cookie will only be marked +as enabled if provisional backing objects are allocated. + + =============================== MISCELLANEOUS COOKIE OPERATIONS ===============================
@@ -778,7 +825,7 @@ COOKIE UNREGISTRATION To get rid of a cookie, this function should be called. void fscache_relinquish_cookie(struct fscache_cookie *cookie, - int retire); + bool retire); If retire is non-zero, then the object will be marked for recycling, and all copies of it will be removed from all active caches in which it is present.
fs/9p/cache.c +3 -3
@@ -90,7 +90,7 @@ void v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses) v9ses->fscache =fscache_acquire_cookie (v9fs_cache_netfs.primary_index, &v9fs_cache_session_index_def, - v9ses); + v9ses, true);p9_debug (P9_DEBUG_FSC ,"session %p get cookie %p\n" , v9ses, v9ses->fscache); }
@@ -204,7 +204,7 @@ void v9fs_cache_inode_get_cookie(struct inode *inode) v9ses =v9fs_inode2v9ses (inode); v9inode->fscache =fscache_acquire_cookie (v9ses->fscache, &v9fs_cache_inode_index_def, - v9inode); + v9inode, true);p9_debug (P9_DEBUG_FSC ,"inode %p get cookie %p\n" , inode, v9inode->fscache);
@@ -271,7 +271,7 @@ void v9fs_cache_inode_reset_cookie(struct inode *inode) v9ses =v9fs_inode2v9ses (inode); v9inode->fscache =fscache_acquire_cookie (v9ses->fscache, &v9fs_cache_inode_index_def, - v9inode); + v9inode, true);p9_debug (P9_DEBUG_FSC ,"inode %p revalidating cookie old %p new %p\n" , inode, old, v9inode->fscache);
fs/afs/cell.c +1 -1
@@ -179,7 +179,7 @@ struct afs_cell *afs_cell_create(const char *name, unsigned namesz,/* put it up for caching (this never returns an error) */ cell->cache =fscache_acquire_cookie (afs_cache_netfs.primary_index, &afs_cell_cache_index_def, - cell); + cell, true);#endif /* add to the cell lists */
fs/afs/inode.c +1 -1
@@ -259,7 +259,7 @@ struct inode *afs_iget(struct super_block *sb, struct key *key,#ifdef CONFIG_AFS_FSCACHE vnode->cache =fscache_acquire_cookie (vnode->volume->cache, &afs_vnode_cache_index_def, - vnode); + vnode, true);#endif ret =afs_inode_map_status (vnode, key);
fs/afs/vlocation.c +2 -1
@@ -308,7 +308,8 @@ static int afs_vlocation_fill_in_record(struct afs_vlocation *vl,/* see if we have an in-cache copy (will set vl->valid if there is) */ #ifdef CONFIG_AFS_FSCACHE vl->cache =fscache_acquire_cookie (vl->cell->cache, - &afs_vlocation_cache_index_def, vl); + &afs_vlocation_cache_index_def, vl, + true);#endif if (vl->valid) {
fs/afs/volume.c +1 -1
@@ -131,7 +131,7 @@ struct afs_volume *afs_volume_lookup(struct afs_mount_params *params)#ifdef CONFIG_AFS_FSCACHE volume->cache =fscache_acquire_cookie (vlocation->cache, &afs_volume_cache_index_def, - volume); + volume, true);#endif afs_get_vlocation (vlocation); volume->vlocation = vlocation;
fs/cachefiles/interface.c +1 -1
@@ -270,7 +270,7 @@ static void cachefiles_drop_object(struct fscache_object *_object)#endif /* delete retired objects */ -if (test_bit (FSCACHE_COOKIE_RETIRED , &object->fscache.cookie->flags) && +if (test_bit (FSCACHE_OBJECT_RETIRED , &object->fscache.flags) && _object != cache->cache.fsdef ) {_debug ("- retire object OBJ%x" , object->fscache.debug_id);
fs/ceph/cache.c +2 -2
@@ -68,7 +68,7 @@ int ceph_fscache_register_fs(struct ceph_fs_client* fsc) { fsc->fscache =fscache_acquire_cookie (ceph_cache_netfs.primary_index, &ceph_fscache_fsid_object_def, - fsc); + fsc, true);if (fsc->fscache ==NULL ) {pr_err ("Unable to resgister fsid: %p fscache cookie" , fsc);
@@ -204,7 +204,7 @@ void ceph_fscache_register_inode_cookie(struct ceph_fs_client* fsc, ci->fscache =fscache_acquire_cookie (fsc->fscache, &ceph_fscache_inode_object_def, - ci); + ci, true); done:mutex_unlock (&inode->i_mutex);
fs/cifs/fscache.c +4 -4
@@ -27,7 +27,7 @@ void cifs_fscache_get_client_cookie(struct TCP_Server_Info *server) { server->fscache =fscache_acquire_cookie (cifs_fscache_netfs.primary_index, - &cifs_fscache_server_index_def, server); + &cifs_fscache_server_index_def, server, true);cifs_dbg (FYI ,"%s: (0x%p/0x%p)\n" , __func__, server, server->fscache); }
@@ -46,7 +46,7 @@ void cifs_fscache_get_super_cookie(struct cifs_tcon *tcon) tcon->fscache =fscache_acquire_cookie (server->fscache, - &cifs_fscache_super_index_def, tcon); + &cifs_fscache_super_index_def, tcon, true);cifs_dbg (FYI ,"%s: (0x%p/0x%p)\n" , __func__, server->fscache, tcon->fscache); }
@@ -69,7 +69,7 @@ static void cifs_fscache_enable_inode_cookie(struct inode *inode)if (cifs_sb->mnt_cifs_flags &CIFS_MOUNT_FSCACHE ) { cifsi->fscache =fscache_acquire_cookie (tcon->fscache, - &cifs_fscache_inode_object_def, cifsi); + &cifs_fscache_inode_object_def, cifsi, true);cifs_dbg (FYI ,"%s: got FH cookie (0x%p/0x%p)\n" , __func__, tcon->fscache, cifsi->fscache); }
@@ -119,7 +119,7 @@ void cifs_fscache_reset_inode_cookie(struct inode *inode) cifsi->fscache =fscache_acquire_cookie (cifs_sb_master_tcon (cifs_sb)->fscache, &cifs_fscache_inode_object_def, - cifsi); + cifsi, true);cifs_dbg (FYI ,"%s: new cookie 0x%p oldcookie 0x%p\n" , __func__, cifsi->fscache, old); }
fs/fscache/cookie.c +137 -49
273 lines not on this page · raw message
fs/fscache/fsdef.c +1 -0
@@ -59,6 +59,7 @@ struct fscache_cookie fscache_fsdef_index = { .lock = __SPIN_LOCK_UNLOCKED(fscache_fsdef_index.lock), .backing_objects = HLIST_HEAD_INIT, .def = &fscache_fsdef_index_def, + .flags = 1 << FSCACHE_COOKIE_ENABLED, };EXPORT_SYMBOL (fscache_fsdef_index);
fs/fscache/netfs.c +1 -0
@@ -45,6 +45,7 @@ int __fscache_register_netfs(struct fscache_netfs *netfs) netfs->primary_index->def = &fscache_fsdef_netfs_def; netfs->primary_index->parent = &fscache_fsdef_index; netfs->primary_index->netfs_data = netfs; + netfs->primary_index->flags =1 <<FSCACHE_COOKIE_ENABLED ;atomic_inc (&netfs->primary_index->parent->usage);atomic_inc (&netfs->primary_index->parent->n_children);
fs/fscache/object.c +5 -2
@@ -495,6 +495,7 @@ void fscache_object_lookup_negative(struct fscache_object *object)* returning ENODATA. */ set_bit (FSCACHE_COOKIE_NO_DATA_YET , &cookie->flags); +clear_bit (FSCACHE_COOKIE_UNAVAILABLE , &cookie->flags);_debug ("wake up lookup %p" , &cookie->flags);clear_bit_unlock (FSCACHE_COOKIE_LOOKING_UP , &cookie->flags);
@@ -527,6 +528,7 @@ void fscache_obtained_object(struct fscache_object *object)/* We do (presumably) have data */ clear_bit_unlock (FSCACHE_COOKIE_NO_DATA_YET , &cookie->flags); +clear_bit (FSCACHE_COOKIE_UNAVAILABLE , &cookie->flags);/* Allow write requests to begin stacking up and read requests * to begin shovelling data.
@@ -679,7 +681,8 @@ static const struct fscache_state *fscache_drop_object(struct fscache_object *ob*/ spin_lock (&cookie->lock);hlist_del_init (&object->cookie_link); -if (test_and_clear_bit (FSCACHE_COOKIE_INVALIDATING , &cookie->flags)) +if (hlist_empty (&cookie->backing_objects) && +test_and_clear_bit (FSCACHE_COOKIE_INVALIDATING , &cookie->flags)) awaken = true;spin_unlock (&cookie->lock);
@@ -927,7 +930,7 @@ static const struct fscache_state *_fscache_invalidate_object(struct fscache_obj*/ if (!fscache_use_cookie (object)) {ASSERT (object->cookie->stores.rnode ==NULL ); -set_bit (FSCACHE_COOKIE_RETIRED , &cookie->flags); +set_bit (FSCACHE_OBJECT_RETIRED , &object->flags);_leave (" [no cookie]" );return transit_to (KILL_OBJECT ); }
fs/fscache/page.c +11 -6
@@ -204,7 +204,8 @@ int __fscache_attr_changed(struct fscache_cookie *cookie)spin_lock (&cookie->lock); -if (hlist_empty (&cookie->backing_objects)) +if (!fscache_cookie_enabled (cookie) || +hlist_empty (&cookie->backing_objects)) goto nobufs; object =hlist_entry (cookie->backing_objects.first, struct fscache_object, cookie_link);
@@ -410,7 +411,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,return -ERESTARTSYS ; op =fscache_alloc_retrieval (cookie, page->mapping, - end_io_func,context); + end_io_func, context);if (!op) {_leave (" = -ENOMEM" );return -ENOMEM ;
@@ -419,7 +420,8 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,spin_lock (&cookie->lock); -if (hlist_empty (&cookie->backing_objects)) +if (!fscache_cookie_enabled (cookie) || +hlist_empty (&cookie->backing_objects)) goto nobufs_unlock; object =hlist_entry (cookie->backing_objects.first, struct fscache_object, cookie_link);
@@ -551,7 +553,8 @@ int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,spin_lock (&cookie->lock); -if (hlist_empty (&cookie->backing_objects)) +if (!fscache_cookie_enabled (cookie) || +hlist_empty (&cookie->backing_objects)) goto nobufs_unlock; object =hlist_entry (cookie->backing_objects.first, struct fscache_object, cookie_link);
@@ -666,7 +669,8 @@ int __fscache_alloc_page(struct fscache_cookie *cookie,spin_lock (&cookie->lock); -if (hlist_empty (&cookie->backing_objects)) +if (!fscache_cookie_enabled (cookie) || +hlist_empty (&cookie->backing_objects)) goto nobufs_unlock; object =hlist_entry (cookie->backing_objects.first, struct fscache_object, cookie_link);
@@ -938,7 +942,8 @@ int __fscache_write_page(struct fscache_cookie *cookie, ret = -ENOBUFS ;spin_lock (&cookie->lock); -if (hlist_empty (&cookie->backing_objects)) +if (!fscache_cookie_enabled (cookie) || +hlist_empty (&cookie->backing_objects)) goto nobufs; object =hlist_entry (cookie->backing_objects.first, struct fscache_object, cookie_link);
fs/nfs/fscache.c +4 -4
@@ -39,7 +39,7 @@ void nfs_fscache_get_client_cookie(struct nfs_client *clp)/* create a cache index for looking up filehandles */ clp->fscache =fscache_acquire_cookie (nfs_fscache_netfs.primary_index, &nfs_fscache_server_index_def, - clp); + clp, true);dfprintk (FSCACHE ,"NFS: get client cookie (0x%p/0x%p)\n" , clp, clp->fscache); }
@@ -139,7 +139,7 @@ void nfs_fscache_get_super_cookie(struct super_block *sb, const char *uniq, int/* create a cache index for looking up filehandles */ nfss->fscache =fscache_acquire_cookie (nfss->nfs_client->fscache, &nfs_fscache_super_index_def, - nfss); + nfss, true);dfprintk (FSCACHE ,"NFS: get superblock cookie (0x%p/0x%p)\n" , nfss, nfss->fscache);return ;
@@ -200,7 +200,7 @@ static void nfs_fscache_enable_inode_cookie(struct inode *inode) nfsi->fscache =fscache_acquire_cookie (NFS_SB (sb)->fscache, &nfs_fscache_inode_object_def, - nfsi); + nfsi, true);dfprintk (FSCACHE ,"NFS: get FH cookie (0x%p/0x%p/0x%p)\n" , sb, nfsi, nfsi->fscache);
@@ -327,7 +327,7 @@ void nfs_fscache_reset_inode_cookie(struct inode *inode) nfsi->fscache =fscache_acquire_cookie ( nfss->nfs_client->fscache, &nfs_fscache_inode_object_def, - nfsi); + nfsi, true); dfprintk(FSCACHE, "NFS: revalidation new cookie (0x%p/0x%p/0x%p/0x%p)\n",
include/linux/fscache-cache.h +1 -30
@@ -308,36 +308,6 @@ struct fscache_cache_ops {void (*dissociate_pages)(struct fscache_cache *cache); }; -/* -* data file or index object cookie -* - a file will only appear in one cache -* - a request to cache a file may or may not be honoured, subject to -* constraints such as disk space -* - indices are created on disk just-in-time -*/ -struct fscache_cookie { -atomic_t usage;/* number of users of this cookie */ -atomic_t n_children;/* number of children of this cookie */ -atomic_t n_active;/* number of active users of netfs ptrs */ -spinlock_t lock; -spinlock_t stores_lock;/* lock on page store tree */ -struct hlist_head backing_objects;/* object(s) backing this file/index */ -const struct fscache_cookie_def *def;/* definition */ -struct fscache_cookie *parent;/* parent of this entry */ -void *netfs_data;/* back pointer to netfs */ -struct radix_tree_root stores;/* pages to be stored on this cookie */ -#define FSCACHE_COOKIE_PENDING_TAG 0/* pages tag: pending write to cache */ -#define FSCACHE_COOKIE_STORING_TAG 1/* pages tag: writing to cache */ - -unsigned long flags; -#define FSCACHE_COOKIE_LOOKING_UP 0/* T if non-index cookie being looked up still */ -#define FSCACHE_COOKIE_NO_DATA_YET 1/* T if new object with no cached data yet */ -#define FSCACHE_COOKIE_UNAVAILABLE 2/* T if cookie is unavailable (error, etc) */ -#define FSCACHE_COOKIE_INVALIDATING 3/* T if cookie is being invalidated */ -#define FSCACHE_COOKIE_RELINQUISHED 4/* T if cookie has been relinquished */ -#define FSCACHE_COOKIE_RETIRED 5/* T if cookie was retired */ -}; -extern struct fscache_cookie fscache_fsdef_index;/*
@@ -400,6 +370,7 @@ struct fscache_object {#define FSCACHE_OBJECT_IS_LIVE 3/* T if object is not withdrawn or relinquished */ #define FSCACHE_OBJECT_IS_LOOKED_UP 4/* T if object has been looked up */ #define FSCACHE_OBJECT_IS_AVAILABLE 5/* T if object has become active */ +#define FSCACHE_OBJECT_RETIRED 6/* T if object was retired on relinquishment */ struct list_head cache_link;/* link in cache->object_list */ struct hlist_node cookie_link;/* link in cookie->backing_objects */
include/linux/fscache.h +99 -14
202 lines not on this page · raw message
David Howells
maintainerPATCH 3/3 NFS: Use i_writecount to control whether to get an fscache cookie in nfs_open()
In reply to David HowellsUse i_writecount to control whether to get an fscache cookie in nfs_open() as NFS does not do write caching yet. I *think* this is the cause of a problem encountered by Mark Moseley whereby __fscache_uncache_page() gets a NULL pointer dereference because cookie->def is NULL:
BUG: unable to handle kernel NULL pointer dereference at 0000000000000010 IP: [<ffffffff812a1903>] __fscache_uncache_page+0x23/0x160 PGD 0 Thread overran stack, or stack corrupted Oops: 0000 [#1] SMP Modules linked in: ... CPU: 7 PID: 18993 Comm: php Not tainted 3.11.1 #1 Hardware name: Dell Inc. PowerEdge R420/072XWF, BIOS 1.3.5 08/21/2012 task: ffff8804203460c0 ti: ffff880420346640 RIP: 0010:[<ffffffff812a1903>] __fscache_uncache_page+0x23/0x160 RSP: 0018:ffff8801053af878 EFLAGS: 00210286 RAX: 0000000000000000 RBX: ffff8800be2f8780 RCX: ffff88022ffae5e8 RDX: 0000000000004c66 RSI: ffffea00055ff440 RDI: ffff8800be2f8780 RBP: ffff8801053af898 R08: 0000000000000001 R09: 0000000000000003 R10: 0000000000000000 R11: 0000000000000000 R12: ffffea00055ff440 R13: 0000000000001000 R14: ffff8800c50be538 R15: 0000000000000000 FS: 0000000000000000(0000) GS:ffff88042fc60000(0063) knlGS:00000000e439c700 CS: 0010 DS: 002b ES: 002b CR0: 0000000080050033 CR2: 0000000000000010 CR3: 0000000001d8f000 CR4: 00000000000607f0 Stack: ... Call Trace: [<ffffffff81365a72>] __nfs_fscache_invalidate_page+0x42/0x70 [<ffffffff813553d5>] nfs_invalidate_page+0x75/0x90 [<ffffffff811b8f5e>] truncate_inode_page+0x8e/0x90 [<ffffffff811b90ad>] truncate_inode_pages_range.part.12+0x14d/0x620 [<ffffffff81d6387d>] ? __mutex_lock_slowpath+0x1fd/0x2e0 [<ffffffff811b95d3>] truncate_inode_pages_range+0x53/0x70 [<ffffffff811b969d>] truncate_inode_pages+0x2d/0x40 [<ffffffff811b96ff>] truncate_pagecache+0x4f/0x70 [<ffffffff81356840>] nfs_setattr_update_inode+0xa0/0x120 [<ffffffff81368de4>] nfs3_proc_setattr+0xc4/0xe0 [<ffffffff81357f78>] nfs_setattr+0xc8/0x150 [<ffffffff8122d95b>] notify_change+0x1cb/0x390 [<ffffffff8120a55b>] do_truncate+0x7b/0xc0 [<ffffffff8121f96c>] do_last+0xa4c/0xfd0 [<ffffffff8121ffbc>] path_openat+0xcc/0x670 [<ffffffff81220a0e>] do_filp_open+0x4e/0xb0 [<ffffffff8120ba1f>] do_sys_open+0x13f/0x2b0 [<ffffffff8126aaf6>] compat_SyS_open+0x36/0x50 [<ffffffff81d7204c>] sysenter_dispatch+0x7/0x24
The code at the instruction pointer was disassembled:
Quoted 6 lines
(gdb) disas
__fscache_uncache_page
Dump of assembler code for function__fscache_uncache_page:
...0xffffffff812a18ff<+31>: mov 0x48(%rbx),%rax0xffffffff812a1903<+35>: cmpb $0x0,0x10(%rax)0xffffffff812a1907<+39>: je0xffffffff812a19cd<__fscache_uncache_page+237>
These instructions make up:
ASSERTCMP(cookie->def->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
That cmpb is the faulting instruction (%rax is 0). So cookie->def is NULL - which presumably means that the cookie has already been at least partway through __fscache_relinquish_cookie().
What I think may be happening is something like a three-way race on the same file:
PROCESS 1 PROCESS 2 PROCESS 3 =============== =============== =============== open(O_TRUNC|O_WRONLY) open(O_RDONLY) open(O_WRONLY) -->nfs_open() -->nfs_fscache_set_inode_cookie() nfs_fscache_inode_lock() nfs_fscache_disable_inode_cookie() __fscache_relinquish_cookie() nfs_inode->fscache = NULL <--nfs_fscache_set_inode_cookie() -->nfs_open() -->nfs_fscache_set_inode_cookie() nfs_fscache_inode_lock() nfs_fscache_enable_inode_cookie() __fscache_acquire_cookie() nfs_inode->fscache = cookie <--nfs_fscache_set_inode_cookie() <--nfs_open() -->nfs_setattr() ... ... -->nfs_invalidate_page() -->__nfs_fscache_invalidate_page() cookie = nfsi->fscache -->nfs_open() -->nfs_fscache_set_inode_cookie() nfs_fscache_inode_lock() nfs_fscache_disable_inode_cookie() -->__fscache_relinquish_cookie() -->__fscache_uncache_page(cookie) <crash> <--__fscache_relinquish_cookie() nfs_inode->fscache = NULL <--nfs_fscache_set_inode_cookie()
What is needed is something to prevent process #2 from reacquiring the cookie
- and I think checking i_writecount should do the trick.
It's also possible to have a two-way race on this if the file is opened O_TRUNC|O_RDONLY instead.
---
fs/nfs/dir.c | 2 - fs/nfs/fscache.c | 192 ++++++++++++++---------------------------------- fs/nfs/fscache.h | 18 ++--- fs/nfs/inode.c | 6 +- fs/nfs/nfs4file.c | 2 - include/linux/fs.h | 5 + include/linux/nfs_fs.h | 8 ++ 7 files changed, 77 insertions(+), 156 deletions(-)
fs/nfs/dir.c +1 -1
@@ -1381,7 +1381,7 @@ static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, istatic int do_open (struct inode *inode,struct file *filp) { -nfs_fscache_set_inode_cookie (inode, filp); +nfs_fscache_open_file (inode, filp);return 0 ; }
fs/nfs/fscache.c +54 -138
264 lines not on this page · raw message
fs/nfs/fscache.h +7 -11
@@ -76,11 +76,9 @@ extern void nfs_fscache_release_client_cookie(struct nfs_client *);extern void nfs_fscache_get_super_cookie (struct super_block *,const char *,int );extern void nfs_fscache_release_super_cookie (struct super_block *); -extern void nfs_fscache_init_inode_cookie (struct inode *); -extern void nfs_fscache_release_inode_cookie (struct inode *); -extern void nfs_fscache_zap_inode_cookie (struct inode *); -extern void nfs_fscache_set_inode_cookie (struct inode *,struct file *); -extern void nfs_fscache_reset_inode_cookie (struct inode *); +extern void nfs_fscache_init_inode (struct inode *); +extern void nfs_fscache_clear_inode (struct inode *); +extern void nfs_fscache_open_file (struct inode *,struct file *);extern void __nfs_fscache_invalidate_page (struct page *,struct inode *);extern int nfs_fscache_release_page (struct page *,gfp_t );
@@ -187,12 +185,10 @@ static inline void nfs_fscache_release_client_cookie(struct nfs_client *clp) {}static inline void nfs_fscache_release_super_cookie (struct super_block *sb) {} -static inline void nfs_fscache_init_inode_cookie (struct inode *inode) {} -static inline void nfs_fscache_release_inode_cookie (struct inode *inode) {} -static inline void nfs_fscache_zap_inode_cookie (struct inode *inode) {} -static inline void nfs_fscache_set_inode_cookie (struct inode *inode, -struct file *filp) {} -static inline void nfs_fscache_reset_inode_cookie (struct inode *inode) {} +static inline void nfs_fscache_init_inode (struct inode *inode) {} +static inline void nfs_fscache_clear_inode (struct inode *inode) {} +static inline void nfs_fscache_open_file (struct inode *inode, +struct file *filp) {}static inline int nfs_fscache_release_page (struct page *page,gfp_t gfp) {
fs/nfs/inode.c +3 -3
@@ -122,7 +122,7 @@ void nfs_clear_inode(struct inode *inode)WARN_ON_ONCE (!list_empty (&NFS_I (inode)->open_files));nfs_zap_acl_cache (inode);nfs_access_zap_cache (inode); -nfs_fscache_release_inode_cookie (inode); +nfs_fscache_clear_inode (inode); }EXPORT_SYMBOL_GPL (nfs_clear_inode);
@@ -459,7 +459,7 @@ nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, st nfsi->attrtimeo_timestamp = now; nfsi->access_cache =RB_ROOT ; -nfs_fscache_init_inode_cookie (inode); +nfs_fscache_init_inode (inode);unlock_new_inode (inode); }else
@@ -854,7 +854,7 @@ int nfs_open(struct inode *inode, struct file *filp)return PTR_ERR (ctx);nfs_file_set_open_context (filp, ctx);put_nfs_open_context (ctx); -nfs_fscache_set_inode_cookie (inode, filp); +nfs_fscache_open_file (inode, filp);return 0 ; }
fs/nfs/nfs4file.c +1 -1
@@ -74,7 +74,7 @@ nfs4_file_open(struct inode *inode, struct file *filp)nfs_set_verifier (dentry,nfs_save_change_attribute (dir));nfs_file_set_open_context (filp, ctx); -nfs_fscache_set_inode_cookie (inode, filp); +nfs_fscache_open_file (inode, filp); err =0 ; out_put_ctx:
include/linux/fs.h +5 -0
@@ -2292,6 +2292,11 @@ static inline void allow_write_access(struct file *file)if (file)atomic_inc (&file_inode (file)->i_writecount); } +static inline bool inode_is_open_for_write (const struct inode *inode) +{ +return atomic_read (&inode->i_writecount) >0 ; +} +#ifdef CONFIG_IMA static inline void i_readcount_dec (struct inode *inode) {
include/linux/nfs_fs.h +6 -2
@@ -269,9 +269,13 @@ static inline int NFS_STALE(const struct inode *inode)return test_bit (NFS_INO_STALE , &NFS_I (inode)->flags); } -static inline int NFS_FSCACHE (const struct inode *inode) +static inline struct fscache_cookie *nfs_i_fscache (struct inode *inode) { -return test_bit (NFS_INO_FSCACHE , &NFS_I (inode)->flags); +#ifdef CONFIG_NFS_FSCACHE +return NFS_I (inode)->fscache; +#else +return NULL ; +#endif }static inline __u64 NFS_FILEID (const struct inode *inode)
PATCH 1/1 NFS: Don't use debug-printk-only local variables
Due to commit 996a2493ed5d3a8d5db03b560fcbbf1ca3f10897 (NFS:
Use i_writecount to control whether to get an fscache cookie in nfs_open(), in two instances a local variable is only used in debug print statements. Said statements might be compiled out, which results in unused variable warnings:
fs/nfs/fscache.c: In function 'nfs_fscache_release_page':fs/nfs/fscache.c:263:21: warning: unused variable 'nfsi' [-Wunused-variable]
fs/nfs/fscache.c: In function '__nfs_fscache_invalidate_page':fs/nfs/fscache.c:286:20: warning: unused variable 'nfsi' [-Wunused-variable]
Just skip the local variable in these cases.
---
David,
This showed up in today's linux-next, seems to have come in through the fscache tree.
Either amend and roll in the fix (after review -- I'm guessing this is the right thing to do), or apply on top please. :)
Thanks!
-Olof
fs/nfs/fscache.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
fs/nfs/fscache.c +2 -4
@@ -260,12 +260,11 @@ EXPORT_SYMBOL_GPL(nfs_fscache_open_file);int nfs_fscache_release_page (struct page *page,gfp_t gfp) {if (PageFsCache (page)) { -struct nfs_inode *nfsi =NFS_I (page->mapping->host);struct fscache_cookie *cookie =nfs_i_fscache (page->mapping->host);BUG_ON (!cookie);dfprintk (FSCACHE ,"NFS: fscache releasepage (0x%p/0x%p/0x%p)\n" , - cookie, page, nfsi); + cookie, page,NFS_I (page->mapping->host));if (!fscache_maybe_release_page (cookie, page, gfp))return 0 ;
@@ -283,13 +282,12 @@ int nfs_fscache_release_page(struct page *page, gfp_t gfp)*/ void __nfs_fscache_invalidate_page (struct page *page,struct inode *inode) { -struct nfs_inode *nfsi =NFS_I (inode);struct fscache_cookie *cookie =nfs_i_fscache (inode);BUG_ON (!cookie);dfprintk (FSCACHE ,"NFS: fscache invalidatepage (0x%p/0x%p/0x%p)\n" , - cookie, page, nfsi); + cookie, page,NFS_I (inode));fscache_wait_on_page_write (cookie, page);
Signature · 1 line
1.7.10.4
David Howells
maintainerRe: [PATCH] NFS: Don't use debug-printk-only local variables
Quoted 5 lines · Olof Johansson
Due to commit
996a2493ed5d3a8d5db03b560fcbbf1ca3f10897(NFS:
Usei_writecountto control whether to get an fscache cookie innfs_open(), in two instances a local variable is only used in debug print statements. Said statements might be compiled out, which results in unused variable warnings:
Yeah, Fengguang's magic compile farm has also complained about that.
David
David Howells
maintainerRe: [PATCH] NFS: Don't use debug-printk-only local variables
In reply to Olof Johanssonfs/nfs/fscache.c EXPORT_SYMBOL_GPL(nfs_fscache_open_file); · line 267
Quoted 7 lines · Olof Johansson
fs/nfs/fscache.c +1
-2-struct nfs_inode *nfsi =NFS_I (page->mapping->host);struct fscache_cookie *cookie =nfs_i_fscache (page->mapping->host);BUG_ON (!cookie);dfprintk (FSCACHE ,"NFS: fscache releasepage (0x%p/0x%p/0x%p)\n" , - cookie, page, nfsi); + cookie, page,NFS_I (page->mapping->host));
This is really the wrong fix. The null dfprintk() should use no_printk().
David
This should not be a problem with Ceph as the MDS (the metadata server) hands out cache capability bit for an inode. In this schema there's no need to worry about the count of of local file pointers open for write (or even across the cluster).
As an aside, the Ceph code checks if we can perform the requested VMA operation by checking the valid_fscache generation (compared to the current cache generation). This way we only create the cookie the first time the file is open. This has greatly simplified the code and got rid of a lot of races.
Best,
- Milosz
Quoted 56 lines · David Howells
Following this mail is a series of patches to fix an issue with NFS whereby caching can be enabled on a file that is open for writing by subsequently opening it for reading. This can be made to crash by opening it for writing again if you're quick enough.
The patches mailed here may not apply directly. They are based on top of three fix patches as can be seen in the GIT branch here:
http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=fscache
The gist of the patchset is that the cookie should be acquired at inode creation only and subsequently enabled and disabled as appropriate (which dispenses with the backing objects when they're not needed).
The extra synchronisation that NFS does can then be dispensed with as it is thenceforth managed by FS-Cache.
This likely will need applying to CIFS, Ceph and 9P also once this is okayed for NFS. AFS is probably safe.
David
---
David Howells (3):
FS-Cache: Add use/unuse/wake cookie wrappers
FS-Cache: Provide the ability to enable/disable cookies
NFS: Usei_writecountto control whether to get an fscache cookie innfs_open()Documentation/filesystems/caching/netfs-api.txt | 73 +++++++-- fs/9p/cache.c | 6 - fs/afs/cell.c | 2 fs/afs/inode.c | 2 fs/afs/vlocation.c | 3 fs/afs/volume.c | 2 fs/cachefiles/interface.c | 2 fs/ceph/cache.c | 4 fs/cifs/fscache.c | 8 - fs/fscache/cookie.c | 193 +++++++++++++++++------ fs/fscache/fsdef.c | 1 fs/fscache/netfs.c | 1 fs/fscache/object.c | 7 + fs/fscache/page.c | 59 +++++-- fs/nfs/dir.c | 2 fs/nfs/fscache.c | 196 +++++++---------------- fs/nfs/fscache.h | 18 +- fs/nfs/inode.c | 6 - fs/nfs/nfs4file.c | 2 include/linux/fs.h | 5 + include/linux/fscache-cache.h | 50 ++---- include/linux/fscache.h | 113 ++++++++++++- include/linux/nfs_fs.h | 8 + 23 files changed, 462 insertions(+), 301 deletions(-)
Quoted 4 lines
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html