aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2013-09-20 19:09:31 -0400
committerDavid Howells <dhowells@redhat.com>2013-09-27 13:40:25 -0400
commit94d30ae90a00cafe686c1057be57f4885f963abf (patch)
tree2a4f927bcaf87e988f4f180d0a011856064186b5 /fs
parent8fb883f3e30065529e4f35d4b4f355193dcdb7a2 (diff)
FS-Cache: Provide the ability to enable/disable cookies
Provide 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. Signed-off-by: David Howells <dhowells@redhat.com
Diffstat (limited to 'fs')
-rw-r--r--fs/9p/cache.c6
-rw-r--r--fs/afs/cell.c2
-rw-r--r--fs/afs/inode.c2
-rw-r--r--fs/afs/vlocation.c3
-rw-r--r--fs/afs/volume.c2
-rw-r--r--fs/cachefiles/interface.c2
-rw-r--r--fs/ceph/cache.c4
-rw-r--r--fs/cifs/fscache.c8
-rw-r--r--fs/fscache/cookie.c186
-rw-r--r--fs/fscache/fsdef.c1
-rw-r--r--fs/fscache/netfs.c1
-rw-r--r--fs/fscache/object.c7
-rw-r--r--fs/fscache/page.c17
-rw-r--r--fs/nfs/fscache.c8
14 files changed, 174 insertions, 75 deletions
diff --git a/fs/9p/cache.c b/fs/9p/cache.c
index a9ea73d6dcf3..2b7a032c37bc 100644
--- a/fs/9p/cache.c
+++ b/fs/9p/cache.c
@@ -90,7 +90,7 @@ void v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses)
90 90
91 v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index, 91 v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index,
92 &v9fs_cache_session_index_def, 92 &v9fs_cache_session_index_def,
93 v9ses); 93 v9ses, true);
94 p9_debug(P9_DEBUG_FSC, "session %p get cookie %p\n", 94 p9_debug(P9_DEBUG_FSC, "session %p get cookie %p\n",
95 v9ses, v9ses->fscache); 95 v9ses, v9ses->fscache);
96} 96}
@@ -204,7 +204,7 @@ void v9fs_cache_inode_get_cookie(struct inode *inode)
204 v9ses = v9fs_inode2v9ses(inode); 204 v9ses = v9fs_inode2v9ses(inode);
205 v9inode->fscache = fscache_acquire_cookie(v9ses->fscache, 205 v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
206 &v9fs_cache_inode_index_def, 206 &v9fs_cache_inode_index_def,
207 v9inode); 207 v9inode, true);
208 208
209 p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n", 209 p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n",
210 inode, v9inode->fscache); 210 inode, v9inode->fscache);
@@ -271,7 +271,7 @@ void v9fs_cache_inode_reset_cookie(struct inode *inode)
271 v9ses = v9fs_inode2v9ses(inode); 271 v9ses = v9fs_inode2v9ses(inode);
272 v9inode->fscache = fscache_acquire_cookie(v9ses->fscache, 272 v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
273 &v9fs_cache_inode_index_def, 273 &v9fs_cache_inode_index_def,
274 v9inode); 274 v9inode, true);
275 p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n", 275 p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n",
276 inode, old, v9inode->fscache); 276 inode, old, v9inode->fscache);
277 277
diff --git a/fs/afs/cell.c b/fs/afs/cell.c
index 3c090b7555ea..ca0a3cf93791 100644
--- a/fs/afs/cell.c
+++ b/fs/afs/cell.c
@@ -179,7 +179,7 @@ struct afs_cell *afs_cell_create(const char *name, unsigned namesz,
179 /* put it up for caching (this never returns an error) */ 179 /* put it up for caching (this never returns an error) */
180 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index, 180 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
181 &afs_cell_cache_index_def, 181 &afs_cell_cache_index_def,
182 cell); 182 cell, true);
183#endif 183#endif
184 184
185 /* add to the cell lists */ 185 /* add to the cell lists */
diff --git a/fs/afs/inode.c b/fs/afs/inode.c
index 789bc253b5f6..ce25d755b7aa 100644
--- a/fs/afs/inode.c
+++ b/fs/afs/inode.c
@@ -259,7 +259,7 @@ struct inode *afs_iget(struct super_block *sb, struct key *key,
259#ifdef CONFIG_AFS_FSCACHE 259#ifdef CONFIG_AFS_FSCACHE
260 vnode->cache = fscache_acquire_cookie(vnode->volume->cache, 260 vnode->cache = fscache_acquire_cookie(vnode->volume->cache,
261 &afs_vnode_cache_index_def, 261 &afs_vnode_cache_index_def,
262 vnode); 262 vnode, true);
263#endif 263#endif
264 264
265 ret = afs_inode_map_status(vnode, key); 265 ret = afs_inode_map_status(vnode, key);
diff --git a/fs/afs/vlocation.c b/fs/afs/vlocation.c
index 57bcb1596530..b6df2e83809f 100644
--- a/fs/afs/vlocation.c
+++ b/fs/afs/vlocation.c
@@ -308,7 +308,8 @@ static int afs_vlocation_fill_in_record(struct afs_vlocation *vl,
308 /* see if we have an in-cache copy (will set vl->valid if there is) */ 308 /* see if we have an in-cache copy (will set vl->valid if there is) */
309#ifdef CONFIG_AFS_FSCACHE 309#ifdef CONFIG_AFS_FSCACHE
310 vl->cache = fscache_acquire_cookie(vl->cell->cache, 310 vl->cache = fscache_acquire_cookie(vl->cell->cache,
311 &afs_vlocation_cache_index_def, vl); 311 &afs_vlocation_cache_index_def, vl,
312 true);
312#endif 313#endif
313 314
314 if (vl->valid) { 315 if (vl->valid) {
diff --git a/fs/afs/volume.c b/fs/afs/volume.c
index 401eeb21869f..2b607257820c 100644
--- a/fs/afs/volume.c
+++ b/fs/afs/volume.c
@@ -131,7 +131,7 @@ struct afs_volume *afs_volume_lookup(struct afs_mount_params *params)
131#ifdef CONFIG_AFS_FSCACHE 131#ifdef CONFIG_AFS_FSCACHE
132 volume->cache = fscache_acquire_cookie(vlocation->cache, 132 volume->cache = fscache_acquire_cookie(vlocation->cache,
133 &afs_volume_cache_index_def, 133 &afs_volume_cache_index_def,
134 volume); 134 volume, true);
135#endif 135#endif
136 afs_get_vlocation(vlocation); 136 afs_get_vlocation(vlocation);
137 volume->vlocation = vlocation; 137 volume->vlocation = vlocation;
diff --git a/fs/cachefiles/interface.c b/fs/cachefiles/interface.c
index 43eb5592cdea..00baf1419989 100644
--- a/fs/cachefiles/interface.c
+++ b/fs/cachefiles/interface.c
@@ -270,7 +270,7 @@ static void cachefiles_drop_object(struct fscache_object *_object)
270#endif 270#endif
271 271
272 /* delete retired objects */ 272 /* delete retired objects */
273 if (test_bit(FSCACHE_COOKIE_RETIRED, &object->fscache.cookie->flags) && 273 if (test_bit(FSCACHE_OBJECT_RETIRED, &object->fscache.flags) &&
274 _object != cache->cache.fsdef 274 _object != cache->cache.fsdef
275 ) { 275 ) {
276 _debug("- retire object OBJ%x", object->fscache.debug_id); 276 _debug("- retire object OBJ%x", object->fscache.debug_id);
diff --git a/fs/ceph/cache.c b/fs/ceph/cache.c
index 6bfe65e0b038..7db2e6ca4b8f 100644
--- a/fs/ceph/cache.c
+++ b/fs/ceph/cache.c
@@ -68,7 +68,7 @@ int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
68{ 68{
69 fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index, 69 fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
70 &ceph_fscache_fsid_object_def, 70 &ceph_fscache_fsid_object_def,
71 fsc); 71 fsc, true);
72 72
73 if (fsc->fscache == NULL) { 73 if (fsc->fscache == NULL) {
74 pr_err("Unable to resgister fsid: %p fscache cookie", fsc); 74 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,
204 204
205 ci->fscache = fscache_acquire_cookie(fsc->fscache, 205 ci->fscache = fscache_acquire_cookie(fsc->fscache,
206 &ceph_fscache_inode_object_def, 206 &ceph_fscache_inode_object_def,
207 ci); 207 ci, true);
208done: 208done:
209 mutex_unlock(&inode->i_mutex); 209 mutex_unlock(&inode->i_mutex);
210 210
diff --git a/fs/cifs/fscache.c b/fs/cifs/fscache.c
index 2f4bc5a58054..fe2492d2a8fc 100644
--- a/fs/cifs/fscache.c
+++ b/fs/cifs/fscache.c
@@ -27,7 +27,7 @@ void cifs_fscache_get_client_cookie(struct TCP_Server_Info *server)
27{ 27{
28 server->fscache = 28 server->fscache =
29 fscache_acquire_cookie(cifs_fscache_netfs.primary_index, 29 fscache_acquire_cookie(cifs_fscache_netfs.primary_index,
30 &cifs_fscache_server_index_def, server); 30 &cifs_fscache_server_index_def, server, true);
31 cifs_dbg(FYI, "%s: (0x%p/0x%p)\n", 31 cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
32 __func__, server, server->fscache); 32 __func__, server, server->fscache);
33} 33}
@@ -46,7 +46,7 @@ void cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
46 46
47 tcon->fscache = 47 tcon->fscache =
48 fscache_acquire_cookie(server->fscache, 48 fscache_acquire_cookie(server->fscache,
49 &cifs_fscache_super_index_def, tcon); 49 &cifs_fscache_super_index_def, tcon, true);
50 cifs_dbg(FYI, "%s: (0x%p/0x%p)\n", 50 cifs_dbg(FYI, "%s: (0x%p/0x%p)\n",
51 __func__, server->fscache, tcon->fscache); 51 __func__, server->fscache, tcon->fscache);
52} 52}
@@ -69,7 +69,7 @@ static void cifs_fscache_enable_inode_cookie(struct inode *inode)
69 69
70 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE) { 70 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE) {
71 cifsi->fscache = fscache_acquire_cookie(tcon->fscache, 71 cifsi->fscache = fscache_acquire_cookie(tcon->fscache,
72 &cifs_fscache_inode_object_def, cifsi); 72 &cifs_fscache_inode_object_def, cifsi, true);
73 cifs_dbg(FYI, "%s: got FH cookie (0x%p/0x%p)\n", 73 cifs_dbg(FYI, "%s: got FH cookie (0x%p/0x%p)\n",
74 __func__, tcon->fscache, cifsi->fscache); 74 __func__, tcon->fscache, cifsi->fscache);
75 } 75 }
@@ -119,7 +119,7 @@ void cifs_fscache_reset_inode_cookie(struct inode *inode)
119 cifsi->fscache = fscache_acquire_cookie( 119 cifsi->fscache = fscache_acquire_cookie(
120 cifs_sb_master_tcon(cifs_sb)->fscache, 120 cifs_sb_master_tcon(cifs_sb)->fscache,
121 &cifs_fscache_inode_object_def, 121 &cifs_fscache_inode_object_def,
122 cifsi); 122 cifsi, true);
123 cifs_dbg(FYI, "%s: new cookie 0x%p oldcookie 0x%p\n", 123 cifs_dbg(FYI, "%s: new cookie 0x%p oldcookie 0x%p\n",
124 __func__, cifsi->fscache, old); 124 __func__, cifsi->fscache, old);
125 } 125 }
diff --git a/fs/fscache/cookie.c b/fs/fscache/cookie.c
index d851aa555d28..29d7feb62cf7 100644
--- a/fs/fscache/cookie.c
+++ b/fs/fscache/cookie.c
@@ -58,15 +58,16 @@ void fscache_cookie_init_once(void *_cookie)
58struct fscache_cookie *__fscache_acquire_cookie( 58struct fscache_cookie *__fscache_acquire_cookie(
59 struct fscache_cookie *parent, 59 struct fscache_cookie *parent,
60 const struct fscache_cookie_def *def, 60 const struct fscache_cookie_def *def,
61 void *netfs_data) 61 void *netfs_data,
62 bool enable)
62{ 63{
63 struct fscache_cookie *cookie; 64 struct fscache_cookie *cookie;
64 65
65 BUG_ON(!def); 66 BUG_ON(!def);
66 67
67 _enter("{%s},{%s},%p", 68 _enter("{%s},{%s},%p,%u",
68 parent ? (char *) parent->def->name : "<no-parent>", 69 parent ? (char *) parent->def->name : "<no-parent>",
69 def->name, netfs_data); 70 def->name, netfs_data, enable);
70 71
71 fscache_stat(&fscache_n_acquires); 72 fscache_stat(&fscache_n_acquires);
72 73
@@ -106,7 +107,7 @@ struct fscache_cookie *__fscache_acquire_cookie(
106 cookie->def = def; 107 cookie->def = def;
107 cookie->parent = parent; 108 cookie->parent = parent;
108 cookie->netfs_data = netfs_data; 109 cookie->netfs_data = netfs_data;
109 cookie->flags = 0; 110 cookie->flags = (1 << FSCACHE_COOKIE_NO_DATA_YET);
110 111
111 /* radix tree insertion won't use the preallocation pool unless it's 112 /* radix tree insertion won't use the preallocation pool unless it's
112 * told it may not wait */ 113 * told it may not wait */
@@ -124,16 +125,22 @@ struct fscache_cookie *__fscache_acquire_cookie(
124 break; 125 break;
125 } 126 }
126 127
127 /* if the object is an index then we need do nothing more here - we 128 if (enable) {
128 * create indices on disk when we need them as an index may exist in 129 /* if the object is an index then we need do nothing more here
129 * multiple caches */ 130 * - we create indices on disk when we need them as an index
130 if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) { 131 * may exist in multiple caches */
131 if (fscache_acquire_non_index_cookie(cookie) < 0) { 132 if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
132 atomic_dec(&parent->n_children); 133 if (fscache_acquire_non_index_cookie(cookie) == 0) {
133 __fscache_cookie_put(cookie); 134 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
134 fscache_stat(&fscache_n_acquires_nobufs); 135 } else {
135 _leave(" = NULL"); 136 atomic_dec(&parent->n_children);
136 return NULL; 137 __fscache_cookie_put(cookie);
138 fscache_stat(&fscache_n_acquires_nobufs);
139 _leave(" = NULL");
140 return NULL;
141 }
142 } else {
143 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
137 } 144 }
138 } 145 }
139 146
@@ -144,6 +151,39 @@ struct fscache_cookie *__fscache_acquire_cookie(
144EXPORT_SYMBOL(__fscache_acquire_cookie); 151EXPORT_SYMBOL(__fscache_acquire_cookie);
145 152
146/* 153/*
154 * Enable a cookie to permit it to accept new operations.
155 */
156void __fscache_enable_cookie(struct fscache_cookie *cookie,
157 bool (*can_enable)(void *data),
158 void *data)
159{
160 _enter("%p", cookie);
161
162 wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
163 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
164
165 if (test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
166 goto out_unlock;
167
168 if (can_enable && !can_enable(data)) {
169 /* The netfs decided it didn't want to enable after all */
170 } else if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
171 /* Wait for outstanding disablement to complete */
172 __fscache_wait_on_invalidate(cookie);
173
174 if (fscache_acquire_non_index_cookie(cookie) == 0)
175 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
176 } else {
177 set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
178 }
179
180out_unlock:
181 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
182 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
183}
184EXPORT_SYMBOL(__fscache_enable_cookie);
185
186/*
147 * acquire a non-index cookie 187 * acquire a non-index cookie
148 * - this must make sure the index chain is instantiated and instantiate the 188 * - this must make sure the index chain is instantiated and instantiate the
149 * object representation too 189 * object representation too
@@ -157,7 +197,7 @@ static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie)
157 197
158 _enter(""); 198 _enter("");
159 199
160 cookie->flags = 1 << FSCACHE_COOKIE_UNAVAILABLE; 200 set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
161 201
162 /* now we need to see whether the backing objects for this cookie yet 202 /* now we need to see whether the backing objects for this cookie yet
163 * exist, if not there'll be nothing to search */ 203 * exist, if not there'll be nothing to search */
@@ -180,9 +220,7 @@ static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie)
180 220
181 _debug("cache %s", cache->tag->name); 221 _debug("cache %s", cache->tag->name);
182 222
183 cookie->flags = 223 set_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
184 (1 << FSCACHE_COOKIE_LOOKING_UP) |
185 (1 << FSCACHE_COOKIE_NO_DATA_YET);
186 224
187 /* ask the cache to allocate objects for this cookie and its parent 225 /* ask the cache to allocate objects for this cookie and its parent
188 * chain */ 226 * chain */
@@ -398,7 +436,8 @@ void __fscache_invalidate(struct fscache_cookie *cookie)
398 if (!hlist_empty(&cookie->backing_objects)) { 436 if (!hlist_empty(&cookie->backing_objects)) {
399 spin_lock(&cookie->lock); 437 spin_lock(&cookie->lock);
400 438
401 if (!hlist_empty(&cookie->backing_objects) && 439 if (fscache_cookie_enabled(cookie) &&
440 !hlist_empty(&cookie->backing_objects) &&
402 !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING, 441 !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
403 &cookie->flags)) { 442 &cookie->flags)) {
404 object = hlist_entry(cookie->backing_objects.first, 443 object = hlist_entry(cookie->backing_objects.first,
@@ -452,10 +491,14 @@ void __fscache_update_cookie(struct fscache_cookie *cookie)
452 491
453 spin_lock(&cookie->lock); 492 spin_lock(&cookie->lock);
454 493
455 /* update the index entry on disk in each cache backing this cookie */ 494 if (fscache_cookie_enabled(cookie)) {
456 hlist_for_each_entry(object, 495 /* update the index entry on disk in each cache backing this
457 &cookie->backing_objects, cookie_link) { 496 * cookie.
458 fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE); 497 */
498 hlist_for_each_entry(object,
499 &cookie->backing_objects, cookie_link) {
500 fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
501 }
459 } 502 }
460 503
461 spin_unlock(&cookie->lock); 504 spin_unlock(&cookie->lock);
@@ -464,28 +507,14 @@ void __fscache_update_cookie(struct fscache_cookie *cookie)
464EXPORT_SYMBOL(__fscache_update_cookie); 507EXPORT_SYMBOL(__fscache_update_cookie);
465 508
466/* 509/*
467 * release a cookie back to the cache 510 * Disable a cookie to stop it from accepting new requests from the netfs.
468 * - the object will be marked as recyclable on disk if retire is true
469 * - all dependents of this cookie must have already been unregistered
470 * (indices/files/pages)
471 */ 511 */
472void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire) 512void __fscache_disable_cookie(struct fscache_cookie *cookie, bool invalidate)
473{ 513{
474 struct fscache_object *object; 514 struct fscache_object *object;
515 bool awaken = false;
475 516
476 fscache_stat(&fscache_n_relinquishes); 517 _enter("%p,%u", cookie, invalidate);
477 if (retire)
478 fscache_stat(&fscache_n_relinquishes_retire);
479
480 if (!cookie) {
481 fscache_stat(&fscache_n_relinquishes_null);
482 _leave(" [no cookie]");
483 return;
484 }
485
486 _enter("%p{%s,%p,%d},%d",
487 cookie, cookie->def->name, cookie->netfs_data,
488 atomic_read(&cookie->n_active), retire);
489 518
490 ASSERTCMP(atomic_read(&cookie->n_active), >, 0); 519 ASSERTCMP(atomic_read(&cookie->n_active), >, 0);
491 520
@@ -495,24 +524,82 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
495 BUG(); 524 BUG();
496 } 525 }
497 526
498 /* No further netfs-accessing operations on this cookie permitted */ 527 wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
499 set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags); 528 fscache_wait_bit, TASK_UNINTERRUPTIBLE);
500 if (retire) 529 if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
501 set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags); 530 goto out_unlock_enable;
531
532 /* If the cookie is being invalidated, wait for that to complete first
533 * so that we can reuse the flag.
534 */
535 __fscache_wait_on_invalidate(cookie);
536
537 /* Dispose of the backing objects */
538 set_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags);
502 539
503 spin_lock(&cookie->lock); 540 spin_lock(&cookie->lock);
504 hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) { 541 if (!hlist_empty(&cookie->backing_objects)) {
505 fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL); 542 hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) {
543 if (invalidate)
544 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
545 fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
546 }
547 } else {
548 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
549 awaken = true;
506 } 550 }
507 spin_unlock(&cookie->lock); 551 spin_unlock(&cookie->lock);
552 if (awaken)
553 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
508 554
509 /* Wait for cessation of activity requiring access to the netfs (when 555 /* Wait for cessation of activity requiring access to the netfs (when
510 * n_active reaches 0). 556 * n_active reaches 0). This makes sure outstanding reads and writes
557 * have completed.
511 */ 558 */
512 if (!atomic_dec_and_test(&cookie->n_active)) 559 if (!atomic_dec_and_test(&cookie->n_active))
513 wait_on_atomic_t(&cookie->n_active, fscache_wait_atomic_t, 560 wait_on_atomic_t(&cookie->n_active, fscache_wait_atomic_t,
514 TASK_UNINTERRUPTIBLE); 561 TASK_UNINTERRUPTIBLE);
515 562
563 /* Reset the cookie state if it wasn't relinquished */
564 if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) {
565 atomic_inc(&cookie->n_active);
566 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
567 }
568
569out_unlock_enable:
570 clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
571 wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
572 _leave("");
573}
574EXPORT_SYMBOL(__fscache_disable_cookie);
575
576/*
577 * release a cookie back to the cache
578 * - the object will be marked as recyclable on disk if retire is true
579 * - all dependents of this cookie must have already been unregistered
580 * (indices/files/pages)
581 */
582void __fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
583{
584 fscache_stat(&fscache_n_relinquishes);
585 if (retire)
586 fscache_stat(&fscache_n_relinquishes_retire);
587
588 if (!cookie) {
589 fscache_stat(&fscache_n_relinquishes_null);
590 _leave(" [no cookie]");
591 return;
592 }
593
594 _enter("%p{%s,%p,%d},%d",
595 cookie, cookie->def->name, cookie->netfs_data,
596 atomic_read(&cookie->n_active), retire);
597
598 /* No further netfs-accessing operations on this cookie permitted */
599 set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags);
600
601 __fscache_disable_cookie(cookie, retire);
602
516 /* Clear pointers back to the netfs */ 603 /* Clear pointers back to the netfs */
517 cookie->netfs_data = NULL; 604 cookie->netfs_data = NULL;
518 cookie->def = NULL; 605 cookie->def = NULL;
@@ -592,7 +679,8 @@ int __fscache_check_consistency(struct fscache_cookie *cookie)
592 679
593 spin_lock(&cookie->lock); 680 spin_lock(&cookie->lock);
594 681
595 if (hlist_empty(&cookie->backing_objects)) 682 if (!fscache_cookie_enabled(cookie) ||
683 hlist_empty(&cookie->backing_objects))
596 goto inconsistent; 684 goto inconsistent;
597 object = hlist_entry(cookie->backing_objects.first, 685 object = hlist_entry(cookie->backing_objects.first,
598 struct fscache_object, cookie_link); 686 struct fscache_object, cookie_link);
diff --git a/fs/fscache/fsdef.c b/fs/fscache/fsdef.c
index 10a2ade0bdf8..5a117df2a9ef 100644
--- a/fs/fscache/fsdef.c
+++ b/fs/fscache/fsdef.c
@@ -59,6 +59,7 @@ struct fscache_cookie fscache_fsdef_index = {
59 .lock = __SPIN_LOCK_UNLOCKED(fscache_fsdef_index.lock), 59 .lock = __SPIN_LOCK_UNLOCKED(fscache_fsdef_index.lock),
60 .backing_objects = HLIST_HEAD_INIT, 60 .backing_objects = HLIST_HEAD_INIT,
61 .def = &fscache_fsdef_index_def, 61 .def = &fscache_fsdef_index_def,
62 .flags = 1 << FSCACHE_COOKIE_ENABLED,
62}; 63};
63EXPORT_SYMBOL(fscache_fsdef_index); 64EXPORT_SYMBOL(fscache_fsdef_index);
64 65
diff --git a/fs/fscache/netfs.c b/fs/fscache/netfs.c
index b1bb6117473a..989f39401547 100644
--- a/fs/fscache/netfs.c
+++ b/fs/fscache/netfs.c
@@ -45,6 +45,7 @@ int __fscache_register_netfs(struct fscache_netfs *netfs)
45 netfs->primary_index->def = &fscache_fsdef_netfs_def; 45 netfs->primary_index->def = &fscache_fsdef_netfs_def;
46 netfs->primary_index->parent = &fscache_fsdef_index; 46 netfs->primary_index->parent = &fscache_fsdef_index;
47 netfs->primary_index->netfs_data = netfs; 47 netfs->primary_index->netfs_data = netfs;
48 netfs->primary_index->flags = 1 << FSCACHE_COOKIE_ENABLED;
48 49
49 atomic_inc(&netfs->primary_index->parent->usage); 50 atomic_inc(&netfs->primary_index->parent->usage);
50 atomic_inc(&netfs->primary_index->parent->n_children); 51 atomic_inc(&netfs->primary_index->parent->n_children);
diff --git a/fs/fscache/object.c b/fs/fscache/object.c
index 86d75a60b20c..dcb821617774 100644
--- a/fs/fscache/object.c
+++ b/fs/fscache/object.c
@@ -495,6 +495,7 @@ void fscache_object_lookup_negative(struct fscache_object *object)
495 * returning ENODATA. 495 * returning ENODATA.
496 */ 496 */
497 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); 497 set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
498 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
498 499
499 _debug("wake up lookup %p", &cookie->flags); 500 _debug("wake up lookup %p", &cookie->flags);
500 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags); 501 clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
@@ -527,6 +528,7 @@ void fscache_obtained_object(struct fscache_object *object)
527 528
528 /* We do (presumably) have data */ 529 /* We do (presumably) have data */
529 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); 530 clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
531 clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
530 532
531 /* Allow write requests to begin stacking up and read requests 533 /* Allow write requests to begin stacking up and read requests
532 * to begin shovelling data. 534 * to begin shovelling data.
@@ -679,7 +681,8 @@ static const struct fscache_state *fscache_drop_object(struct fscache_object *ob
679 */ 681 */
680 spin_lock(&cookie->lock); 682 spin_lock(&cookie->lock);
681 hlist_del_init(&object->cookie_link); 683 hlist_del_init(&object->cookie_link);
682 if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) 684 if (hlist_empty(&cookie->backing_objects) &&
685 test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
683 awaken = true; 686 awaken = true;
684 spin_unlock(&cookie->lock); 687 spin_unlock(&cookie->lock);
685 688
@@ -927,7 +930,7 @@ static const struct fscache_state *_fscache_invalidate_object(struct fscache_obj
927 */ 930 */
928 if (!fscache_use_cookie(object)) { 931 if (!fscache_use_cookie(object)) {
929 ASSERT(object->cookie->stores.rnode == NULL); 932 ASSERT(object->cookie->stores.rnode == NULL);
930 set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags); 933 set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
931 _leave(" [no cookie]"); 934 _leave(" [no cookie]");
932 return transit_to(KILL_OBJECT); 935 return transit_to(KILL_OBJECT);
933 } 936 }
diff --git a/fs/fscache/page.c b/fs/fscache/page.c
index 0fe42a6d0e9c..7f5c658af755 100644
--- a/fs/fscache/page.c
+++ b/fs/fscache/page.c
@@ -204,7 +204,8 @@ int __fscache_attr_changed(struct fscache_cookie *cookie)
204 204
205 spin_lock(&cookie->lock); 205 spin_lock(&cookie->lock);
206 206
207 if (hlist_empty(&cookie->backing_objects)) 207 if (!fscache_cookie_enabled(cookie) ||
208 hlist_empty(&cookie->backing_objects))
208 goto nobufs; 209 goto nobufs;
209 object = hlist_entry(cookie->backing_objects.first, 210 object = hlist_entry(cookie->backing_objects.first,
210 struct fscache_object, cookie_link); 211 struct fscache_object, cookie_link);
@@ -410,7 +411,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
410 return -ERESTARTSYS; 411 return -ERESTARTSYS;
411 412
412 op = fscache_alloc_retrieval(cookie, page->mapping, 413 op = fscache_alloc_retrieval(cookie, page->mapping,
413 end_io_func,context); 414 end_io_func, context);
414 if (!op) { 415 if (!op) {
415 _leave(" = -ENOMEM"); 416 _leave(" = -ENOMEM");
416 return -ENOMEM; 417 return -ENOMEM;
@@ -419,7 +420,8 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
419 420
420 spin_lock(&cookie->lock); 421 spin_lock(&cookie->lock);
421 422
422 if (hlist_empty(&cookie->backing_objects)) 423 if (!fscache_cookie_enabled(cookie) ||
424 hlist_empty(&cookie->backing_objects))
423 goto nobufs_unlock; 425 goto nobufs_unlock;
424 object = hlist_entry(cookie->backing_objects.first, 426 object = hlist_entry(cookie->backing_objects.first,
425 struct fscache_object, cookie_link); 427 struct fscache_object, cookie_link);
@@ -551,7 +553,8 @@ int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
551 553
552 spin_lock(&cookie->lock); 554 spin_lock(&cookie->lock);
553 555
554 if (hlist_empty(&cookie->backing_objects)) 556 if (!fscache_cookie_enabled(cookie) ||
557 hlist_empty(&cookie->backing_objects))
555 goto nobufs_unlock; 558 goto nobufs_unlock;
556 object = hlist_entry(cookie->backing_objects.first, 559 object = hlist_entry(cookie->backing_objects.first,
557 struct fscache_object, cookie_link); 560 struct fscache_object, cookie_link);
@@ -666,7 +669,8 @@ int __fscache_alloc_page(struct fscache_cookie *cookie,
666 669
667 spin_lock(&cookie->lock); 670 spin_lock(&cookie->lock);
668 671
669 if (hlist_empty(&cookie->backing_objects)) 672 if (!fscache_cookie_enabled(cookie) ||
673 hlist_empty(&cookie->backing_objects))
670 goto nobufs_unlock; 674 goto nobufs_unlock;
671 object = hlist_entry(cookie->backing_objects.first, 675 object = hlist_entry(cookie->backing_objects.first,
672 struct fscache_object, cookie_link); 676 struct fscache_object, cookie_link);
@@ -938,7 +942,8 @@ int __fscache_write_page(struct fscache_cookie *cookie,
938 ret = -ENOBUFS; 942 ret = -ENOBUFS;
939 spin_lock(&cookie->lock); 943 spin_lock(&cookie->lock);
940 944
941 if (hlist_empty(&cookie->backing_objects)) 945 if (!fscache_cookie_enabled(cookie) ||
946 hlist_empty(&cookie->backing_objects))
942 goto nobufs; 947 goto nobufs;
943 object = hlist_entry(cookie->backing_objects.first, 948 object = hlist_entry(cookie->backing_objects.first,
944 struct fscache_object, cookie_link); 949 struct fscache_object, cookie_link);
diff --git a/fs/nfs/fscache.c b/fs/nfs/fscache.c
index 24d1d1c5fcaf..cd6e7efd4305 100644
--- a/fs/nfs/fscache.c
+++ b/fs/nfs/fscache.c
@@ -39,7 +39,7 @@ void nfs_fscache_get_client_cookie(struct nfs_client *clp)
39 /* create a cache index for looking up filehandles */ 39 /* create a cache index for looking up filehandles */
40 clp->fscache = fscache_acquire_cookie(nfs_fscache_netfs.primary_index, 40 clp->fscache = fscache_acquire_cookie(nfs_fscache_netfs.primary_index,
41 &nfs_fscache_server_index_def, 41 &nfs_fscache_server_index_def,
42 clp); 42 clp, true);
43 dfprintk(FSCACHE, "NFS: get client cookie (0x%p/0x%p)\n", 43 dfprintk(FSCACHE, "NFS: get client cookie (0x%p/0x%p)\n",
44 clp, clp->fscache); 44 clp, clp->fscache);
45} 45}
@@ -139,7 +139,7 @@ void nfs_fscache_get_super_cookie(struct super_block *sb, const char *uniq, int
139 /* create a cache index for looking up filehandles */ 139 /* create a cache index for looking up filehandles */
140 nfss->fscache = fscache_acquire_cookie(nfss->nfs_client->fscache, 140 nfss->fscache = fscache_acquire_cookie(nfss->nfs_client->fscache,
141 &nfs_fscache_super_index_def, 141 &nfs_fscache_super_index_def,
142 nfss); 142 nfss, true);
143 dfprintk(FSCACHE, "NFS: get superblock cookie (0x%p/0x%p)\n", 143 dfprintk(FSCACHE, "NFS: get superblock cookie (0x%p/0x%p)\n",
144 nfss, nfss->fscache); 144 nfss, nfss->fscache);
145 return; 145 return;
@@ -200,7 +200,7 @@ static void nfs_fscache_enable_inode_cookie(struct inode *inode)
200 nfsi->fscache = fscache_acquire_cookie( 200 nfsi->fscache = fscache_acquire_cookie(
201 NFS_SB(sb)->fscache, 201 NFS_SB(sb)->fscache,
202 &nfs_fscache_inode_object_def, 202 &nfs_fscache_inode_object_def,
203 nfsi); 203 nfsi, true);
204 204
205 dfprintk(FSCACHE, "NFS: get FH cookie (0x%p/0x%p/0x%p)\n", 205 dfprintk(FSCACHE, "NFS: get FH cookie (0x%p/0x%p/0x%p)\n",
206 sb, nfsi, nfsi->fscache); 206 sb, nfsi, nfsi->fscache);
@@ -327,7 +327,7 @@ void nfs_fscache_reset_inode_cookie(struct inode *inode)
327 nfsi->fscache = fscache_acquire_cookie( 327 nfsi->fscache = fscache_acquire_cookie(
328 nfss->nfs_client->fscache, 328 nfss->nfs_client->fscache,
329 &nfs_fscache_inode_object_def, 329 &nfs_fscache_inode_object_def,
330 nfsi); 330 nfsi, true);
331 331
332 dfprintk(FSCACHE, 332 dfprintk(FSCACHE,
333 "NFS: revalidation new cookie (0x%p/0x%p/0x%p/0x%p)\n", 333 "NFS: revalidation new cookie (0x%p/0x%p/0x%p/0x%p)\n",