aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrond Myklebust <Trond.Myklebust@netapp.com>2013-10-28 19:33:36 -0400
committerTrond Myklebust <Trond.Myklebust@netapp.com>2013-10-28 19:36:46 -0400
commitc698dbf9fecae54e5dc159cee76939228c533a25 (patch)
tree1b0231679ec4d86e6f88b233de825bd5b8e687ac
parenta3f432bfd06a4ec3b812e32d3266e0d1ad75d008 (diff)
parentf1fe29b4a02d0805aa7d0ff6b73410a9f9316d69 (diff)
Merge branch 'fscache' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs into linux-next
Pull fs-cache fixes from David Howells: Can you pull these commits 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 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. Could you send these on to Linus? This likely will need fixing also in CIFS and 9P also once the FS-Cache changes are upstream. AFS and Ceph are probably safe. * 'fscache' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs: NFS: Use i_writecount to control whether to get an fscache cookie in nfs_open() FS-Cache: Provide the ability to enable/disable cookies FS-Cache: Add use/unuse/wake cookie wrappers
-rw-r--r--Documentation/filesystems/caching/netfs-api.txt73
-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.c193
-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.c59
-rw-r--r--fs/nfs/dir.c2
-rw-r--r--fs/nfs/fscache.c202
-rw-r--r--fs/nfs/fscache.h18
-rw-r--r--fs/nfs/inode.c6
-rw-r--r--fs/nfs/nfs4file.c2
-rw-r--r--include/linux/fs.h5
-rw-r--r--include/linux/fscache-cache.h50
-rw-r--r--include/linux/fscache.h113
-rw-r--r--include/linux/nfs_fs.h8
23 files changed, 464 insertions, 305 deletions
diff --git a/Documentation/filesystems/caching/netfs-api.txt b/Documentation/filesystems/caching/netfs-api.txt
index 11a0a40ce445..aed6b94160b1 100644
--- a/Documentation/filesystems/caching/netfs-api.txt
+++ b/Documentation/filesystems/caching/netfs-api.txt
@@ -29,15 +29,16 @@ This document contains the following sections:
29 (6) Index registration 29 (6) Index registration
30 (7) Data file registration 30 (7) Data file registration
31 (8) Miscellaneous object registration 31 (8) Miscellaneous object registration
32 (9) Setting the data file size 32 (9) Setting the data file size
33 (10) Page alloc/read/write 33 (10) Page alloc/read/write
34 (11) Page uncaching 34 (11) Page uncaching
35 (12) Index and data file consistency 35 (12) Index and data file consistency
36 (13) Miscellaneous cookie operations 36 (13) Cookie enablement
37 (14) Cookie unregistration 37 (14) Miscellaneous cookie operations
38 (15) Index invalidation 38 (15) Cookie unregistration
39 (16) Data file invalidation 39 (16) Index invalidation
40 (17) FS-Cache specific page flags. 40 (17) Data file invalidation
41 (18) FS-Cache specific page flags.
41 42
42 43
43============================= 44=============================
@@ -334,7 +335,8 @@ the path to the file:
334 struct fscache_cookie * 335 struct fscache_cookie *
335 fscache_acquire_cookie(struct fscache_cookie *parent, 336 fscache_acquire_cookie(struct fscache_cookie *parent,
336 const struct fscache_object_def *def, 337 const struct fscache_object_def *def,
337 void *netfs_data); 338 void *netfs_data,
339 bool enable);
338 340
339This function creates an index entry in the index represented by parent, 341This function creates an index entry in the index represented by parent,
340filling in the index entry by calling the operations pointed to by def. 342filling 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
350may be created in several different caches independently at different times. 352may be created in several different caches independently at different times.
351This is all handled transparently, and the netfs doesn't see any of it. 353This is all handled transparently, and the netfs doesn't see any of it.
352 354
355A cookie will be created in the disabled state if enabled is false. A cookie
356must be enabled to do anything with it. A disabled cookie can be enabled by
357calling fscache_enable_cookie() (see below).
358
353For example, with AFS, a cell would be added to the primary index. This index 359For example, with AFS, a cell would be added to the primary index. This index
354entry would have a dependent inode containing a volume location index for the 360entry would have a dependent inode containing a volume location index for the
355volume mappings within this cell: 361volume mappings within this cell:
@@ -357,7 +363,7 @@ volume mappings within this cell:
357 cell->cache = 363 cell->cache =
358 fscache_acquire_cookie(afs_cache_netfs.primary_index, 364 fscache_acquire_cookie(afs_cache_netfs.primary_index,
359 &afs_cell_cache_index_def, 365 &afs_cell_cache_index_def,
360 cell); 366 cell, true);
361 367
362Then when a volume location was accessed, it would be entered into the cell's 368Then when a volume location was accessed, it would be entered into the cell's
363index and an inode would be allocated that acts as a volume type and hash chain 369index and an inode would be allocated that acts as a volume type and hash chain
@@ -366,7 +372,7 @@ combination:
366 vlocation->cache = 372 vlocation->cache =
367 fscache_acquire_cookie(cell->cache, 373 fscache_acquire_cookie(cell->cache,
368 &afs_vlocation_cache_index_def, 374 &afs_vlocation_cache_index_def,
369 vlocation); 375 vlocation, true);
370 376
371And then a particular flavour of volume (R/O for example) could be added to 377And then a particular flavour of volume (R/O for example) could be added to
372that index, creating another index for vnodes (AFS inode equivalents): 378that index, creating another index for vnodes (AFS inode equivalents):
@@ -374,7 +380,7 @@ that index, creating another index for vnodes (AFS inode equivalents):
374 volume->cache = 380 volume->cache =
375 fscache_acquire_cookie(vlocation->cache, 381 fscache_acquire_cookie(vlocation->cache,
376 &afs_volume_cache_index_def, 382 &afs_volume_cache_index_def,
377 volume); 383 volume, true);
378 384
379 385
380====================== 386======================
@@ -388,7 +394,7 @@ the object definition should be something other than index type.
388 vnode->cache = 394 vnode->cache =
389 fscache_acquire_cookie(volume->cache, 395 fscache_acquire_cookie(volume->cache,
390 &afs_vnode_cache_object_def, 396 &afs_vnode_cache_object_def,
391 vnode); 397 vnode, true);
392 398
393 399
394================================= 400=================================
@@ -404,7 +410,7 @@ it would be some other type of object such as a data file.
404 xattr->cache = 410 xattr->cache =
405 fscache_acquire_cookie(vnode->cache, 411 fscache_acquire_cookie(vnode->cache,
406 &afs_xattr_cache_object_def, 412 &afs_xattr_cache_object_def,
407 xattr); 413 xattr, true);
408 414
409Miscellaneous objects might be used to store extended attributes or directory 415Miscellaneous objects might be used to store extended attributes or directory
410entries for example. 416entries for example.
@@ -733,6 +739,47 @@ Note that partial updates may happen automatically at other times, such as when
733data blocks are added to a data file object. 739data blocks are added to a data file object.
734 740
735 741
742=================
743COOKIE ENABLEMENT
744=================
745
746Cookies exist in one of two states: enabled and disabled. If a cookie is
747disabled, it ignores all attempts to acquire child cookies; check, update or
748invalidate its state; allocate, read or write backing pages - though it is
749still possible to uncache pages and relinquish the cookie.
750
751The initial enablement state is set by fscache_acquire_cookie(), but the cookie
752can be enabled or disabled later. To disable a cookie, call:
753
754 void fscache_disable_cookie(struct fscache_cookie *cookie,
755 bool invalidate);
756
757If the cookie is not already disabled, this locks the cookie against other
758enable and disable ops, marks the cookie as being disabled, discards or
759invalidates any backing objects and waits for cessation of activity on any
760associated object before unlocking the cookie.
761
762All possible failures are handled internally. The caller should consider
763calling fscache_uncache_all_inode_pages() afterwards to make sure all page
764markings are cleared up.
765
766Cookies can be enabled or reenabled with:
767
768 void fscache_enable_cookie(struct fscache_cookie *cookie,
769 bool (*can_enable)(void *data),
770 void *data)
771
772If the cookie is not already enabled, this locks the cookie against other
773enable and disable ops, invokes can_enable() and, if the cookie is not an index
774cookie, will begin the procedure of acquiring backing objects.
775
776The optional can_enable() function is passed the data argument and returns a
777ruling as to whether or not enablement should actually be permitted to begin.
778
779All possible failures are handled internally. The cookie will only be marked
780as enabled if provisional backing objects are allocated.
781
782
736=============================== 783===============================
737MISCELLANEOUS COOKIE OPERATIONS 784MISCELLANEOUS COOKIE OPERATIONS
738=============================== 785===============================
@@ -778,7 +825,7 @@ COOKIE UNREGISTRATION
778To get rid of a cookie, this function should be called. 825To get rid of a cookie, this function should be called.
779 826
780 void fscache_relinquish_cookie(struct fscache_cookie *cookie, 827 void fscache_relinquish_cookie(struct fscache_cookie *cookie,
781 int retire); 828 bool retire);
782 829
783If retire is non-zero, then the object will be marked for recycling, and all 830If retire is non-zero, then the object will be marked for recycling, and all
784copies of it will be removed from all active caches in which it is present. 831copies of it will be removed from all active caches in which it is present.
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 b2a86e324aac..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;
@@ -568,6 +655,7 @@ int __fscache_check_consistency(struct fscache_cookie *cookie)
568{ 655{
569 struct fscache_operation *op; 656 struct fscache_operation *op;
570 struct fscache_object *object; 657 struct fscache_object *object;
658 bool wake_cookie = false;
571 int ret; 659 int ret;
572 660
573 _enter("%p,", cookie); 661 _enter("%p,", cookie);
@@ -591,7 +679,8 @@ int __fscache_check_consistency(struct fscache_cookie *cookie)
591 679
592 spin_lock(&cookie->lock); 680 spin_lock(&cookie->lock);
593 681
594 if (hlist_empty(&cookie->backing_objects)) 682 if (!fscache_cookie_enabled(cookie) ||
683 hlist_empty(&cookie->backing_objects))
595 goto inconsistent; 684 goto inconsistent;
596 object = hlist_entry(cookie->backing_objects.first, 685 object = hlist_entry(cookie->backing_objects.first,
597 struct fscache_object, cookie_link); 686 struct fscache_object, cookie_link);
@@ -600,7 +689,7 @@ int __fscache_check_consistency(struct fscache_cookie *cookie)
600 689
601 op->debug_id = atomic_inc_return(&fscache_op_debug_id); 690 op->debug_id = atomic_inc_return(&fscache_op_debug_id);
602 691
603 atomic_inc(&cookie->n_active); 692 __fscache_use_cookie(cookie);
604 if (fscache_submit_op(object, op) < 0) 693 if (fscache_submit_op(object, op) < 0)
605 goto submit_failed; 694 goto submit_failed;
606 695
@@ -622,9 +711,11 @@ int __fscache_check_consistency(struct fscache_cookie *cookie)
622 return ret; 711 return ret;
623 712
624submit_failed: 713submit_failed:
625 atomic_dec(&cookie->n_active); 714 wake_cookie = __fscache_unuse_cookie(cookie);
626inconsistent: 715inconsistent:
627 spin_unlock(&cookie->lock); 716 spin_unlock(&cookie->lock);
717 if (wake_cookie)
718 __fscache_wake_unused_cookie(cookie);
628 kfree(op); 719 kfree(op);
629 _leave(" = -ESTALE"); 720 _leave(" = -ESTALE");
630 return -ESTALE; 721 return -ESTALE;
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 73899c1c3449..7f5c658af755 100644
--- a/fs/fscache/page.c
+++ b/fs/fscache/page.c
@@ -163,12 +163,10 @@ static void fscache_attr_changed_op(struct fscache_operation *op)
163 163
164 fscache_stat(&fscache_n_attr_changed_calls); 164 fscache_stat(&fscache_n_attr_changed_calls);
165 165
166 if (fscache_object_is_active(object) && 166 if (fscache_object_is_active(object)) {
167 fscache_use_cookie(object)) {
168 fscache_stat(&fscache_n_cop_attr_changed); 167 fscache_stat(&fscache_n_cop_attr_changed);
169 ret = object->cache->ops->attr_changed(object); 168 ret = object->cache->ops->attr_changed(object);
170 fscache_stat_d(&fscache_n_cop_attr_changed); 169 fscache_stat_d(&fscache_n_cop_attr_changed);
171 fscache_unuse_cookie(object);
172 if (ret < 0) 170 if (ret < 0)
173 fscache_abort_object(object); 171 fscache_abort_object(object);
174 } 172 }
@@ -184,6 +182,7 @@ int __fscache_attr_changed(struct fscache_cookie *cookie)
184{ 182{
185 struct fscache_operation *op; 183 struct fscache_operation *op;
186 struct fscache_object *object; 184 struct fscache_object *object;
185 bool wake_cookie;
187 186
188 _enter("%p", cookie); 187 _enter("%p", cookie);
189 188
@@ -199,15 +198,19 @@ int __fscache_attr_changed(struct fscache_cookie *cookie)
199 } 198 }
200 199
201 fscache_operation_init(op, fscache_attr_changed_op, NULL); 200 fscache_operation_init(op, fscache_attr_changed_op, NULL);
202 op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE); 201 op->flags = FSCACHE_OP_ASYNC |
202 (1 << FSCACHE_OP_EXCLUSIVE) |
203 (1 << FSCACHE_OP_UNUSE_COOKIE);
203 204
204 spin_lock(&cookie->lock); 205 spin_lock(&cookie->lock);
205 206
206 if (hlist_empty(&cookie->backing_objects)) 207 if (!fscache_cookie_enabled(cookie) ||
208 hlist_empty(&cookie->backing_objects))
207 goto nobufs; 209 goto nobufs;
208 object = hlist_entry(cookie->backing_objects.first, 210 object = hlist_entry(cookie->backing_objects.first,
209 struct fscache_object, cookie_link); 211 struct fscache_object, cookie_link);
210 212
213 __fscache_use_cookie(cookie);
211 if (fscache_submit_exclusive_op(object, op) < 0) 214 if (fscache_submit_exclusive_op(object, op) < 0)
212 goto nobufs; 215 goto nobufs;
213 spin_unlock(&cookie->lock); 216 spin_unlock(&cookie->lock);
@@ -217,8 +220,11 @@ int __fscache_attr_changed(struct fscache_cookie *cookie)
217 return 0; 220 return 0;
218 221
219nobufs: 222nobufs:
223 wake_cookie = __fscache_unuse_cookie(cookie);
220 spin_unlock(&cookie->lock); 224 spin_unlock(&cookie->lock);
221 kfree(op); 225 kfree(op);
226 if (wake_cookie)
227 __fscache_wake_unused_cookie(cookie);
222 fscache_stat(&fscache_n_attr_changed_nobufs); 228 fscache_stat(&fscache_n_attr_changed_nobufs);
223 _leave(" = %d", -ENOBUFS); 229 _leave(" = %d", -ENOBUFS);
224 return -ENOBUFS; 230 return -ENOBUFS;
@@ -263,7 +269,6 @@ static struct fscache_retrieval *fscache_alloc_retrieval(
263 } 269 }
264 270
265 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op); 271 fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
266 atomic_inc(&cookie->n_active);
267 op->op.flags = FSCACHE_OP_MYTHREAD | 272 op->op.flags = FSCACHE_OP_MYTHREAD |
268 (1UL << FSCACHE_OP_WAITING) | 273 (1UL << FSCACHE_OP_WAITING) |
269 (1UL << FSCACHE_OP_UNUSE_COOKIE); 274 (1UL << FSCACHE_OP_UNUSE_COOKIE);
@@ -384,6 +389,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
384{ 389{
385 struct fscache_retrieval *op; 390 struct fscache_retrieval *op;
386 struct fscache_object *object; 391 struct fscache_object *object;
392 bool wake_cookie = false;
387 int ret; 393 int ret;
388 394
389 _enter("%p,%p,,,", cookie, page); 395 _enter("%p,%p,,,", cookie, page);
@@ -405,7 +411,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
405 return -ERESTARTSYS; 411 return -ERESTARTSYS;
406 412
407 op = fscache_alloc_retrieval(cookie, page->mapping, 413 op = fscache_alloc_retrieval(cookie, page->mapping,
408 end_io_func,context); 414 end_io_func, context);
409 if (!op) { 415 if (!op) {
410 _leave(" = -ENOMEM"); 416 _leave(" = -ENOMEM");
411 return -ENOMEM; 417 return -ENOMEM;
@@ -414,13 +420,15 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
414 420
415 spin_lock(&cookie->lock); 421 spin_lock(&cookie->lock);
416 422
417 if (hlist_empty(&cookie->backing_objects)) 423 if (!fscache_cookie_enabled(cookie) ||
424 hlist_empty(&cookie->backing_objects))
418 goto nobufs_unlock; 425 goto nobufs_unlock;
419 object = hlist_entry(cookie->backing_objects.first, 426 object = hlist_entry(cookie->backing_objects.first,
420 struct fscache_object, cookie_link); 427 struct fscache_object, cookie_link);
421 428
422 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)); 429 ASSERT(test_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags));
423 430
431 __fscache_use_cookie(cookie);
424 atomic_inc(&object->n_reads); 432 atomic_inc(&object->n_reads);
425 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags); 433 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
426 434
@@ -475,9 +483,11 @@ error:
475 483
476nobufs_unlock_dec: 484nobufs_unlock_dec:
477 atomic_dec(&object->n_reads); 485 atomic_dec(&object->n_reads);
486 wake_cookie = __fscache_unuse_cookie(cookie);
478nobufs_unlock: 487nobufs_unlock:
479 spin_unlock(&cookie->lock); 488 spin_unlock(&cookie->lock);
480 atomic_dec(&cookie->n_active); 489 if (wake_cookie)
490 __fscache_wake_unused_cookie(cookie);
481 kfree(op); 491 kfree(op);
482nobufs: 492nobufs:
483 fscache_stat(&fscache_n_retrievals_nobufs); 493 fscache_stat(&fscache_n_retrievals_nobufs);
@@ -514,6 +524,7 @@ int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
514{ 524{
515 struct fscache_retrieval *op; 525 struct fscache_retrieval *op;
516 struct fscache_object *object; 526 struct fscache_object *object;
527 bool wake_cookie = false;
517 int ret; 528 int ret;
518 529
519 _enter("%p,,%d,,,", cookie, *nr_pages); 530 _enter("%p,,%d,,,", cookie, *nr_pages);
@@ -542,11 +553,13 @@ int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
542 553
543 spin_lock(&cookie->lock); 554 spin_lock(&cookie->lock);
544 555
545 if (hlist_empty(&cookie->backing_objects)) 556 if (!fscache_cookie_enabled(cookie) ||
557 hlist_empty(&cookie->backing_objects))
546 goto nobufs_unlock; 558 goto nobufs_unlock;
547 object = hlist_entry(cookie->backing_objects.first, 559 object = hlist_entry(cookie->backing_objects.first,
548 struct fscache_object, cookie_link); 560 struct fscache_object, cookie_link);
549 561
562 __fscache_use_cookie(cookie);
550 atomic_inc(&object->n_reads); 563 atomic_inc(&object->n_reads);
551 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags); 564 __set_bit(FSCACHE_OP_DEC_READ_CNT, &op->op.flags);
552 565
@@ -601,10 +614,12 @@ error:
601 614
602nobufs_unlock_dec: 615nobufs_unlock_dec:
603 atomic_dec(&object->n_reads); 616 atomic_dec(&object->n_reads);
617 wake_cookie = __fscache_unuse_cookie(cookie);
604nobufs_unlock: 618nobufs_unlock:
605 spin_unlock(&cookie->lock); 619 spin_unlock(&cookie->lock);
606 atomic_dec(&cookie->n_active);
607 kfree(op); 620 kfree(op);
621 if (wake_cookie)
622 __fscache_wake_unused_cookie(cookie);
608nobufs: 623nobufs:
609 fscache_stat(&fscache_n_retrievals_nobufs); 624 fscache_stat(&fscache_n_retrievals_nobufs);
610 _leave(" = -ENOBUFS"); 625 _leave(" = -ENOBUFS");
@@ -626,6 +641,7 @@ int __fscache_alloc_page(struct fscache_cookie *cookie,
626{ 641{
627 struct fscache_retrieval *op; 642 struct fscache_retrieval *op;
628 struct fscache_object *object; 643 struct fscache_object *object;
644 bool wake_cookie = false;
629 int ret; 645 int ret;
630 646
631 _enter("%p,%p,,,", cookie, page); 647 _enter("%p,%p,,,", cookie, page);
@@ -653,13 +669,15 @@ int __fscache_alloc_page(struct fscache_cookie *cookie,
653 669
654 spin_lock(&cookie->lock); 670 spin_lock(&cookie->lock);
655 671
656 if (hlist_empty(&cookie->backing_objects)) 672 if (!fscache_cookie_enabled(cookie) ||
673 hlist_empty(&cookie->backing_objects))
657 goto nobufs_unlock; 674 goto nobufs_unlock;
658 object = hlist_entry(cookie->backing_objects.first, 675 object = hlist_entry(cookie->backing_objects.first,
659 struct fscache_object, cookie_link); 676 struct fscache_object, cookie_link);
660 677
678 __fscache_use_cookie(cookie);
661 if (fscache_submit_op(object, &op->op) < 0) 679 if (fscache_submit_op(object, &op->op) < 0)
662 goto nobufs_unlock; 680 goto nobufs_unlock_dec;
663 spin_unlock(&cookie->lock); 681 spin_unlock(&cookie->lock);
664 682
665 fscache_stat(&fscache_n_alloc_ops); 683 fscache_stat(&fscache_n_alloc_ops);
@@ -689,10 +707,13 @@ error:
689 _leave(" = %d", ret); 707 _leave(" = %d", ret);
690 return ret; 708 return ret;
691 709
710nobufs_unlock_dec:
711 wake_cookie = __fscache_unuse_cookie(cookie);
692nobufs_unlock: 712nobufs_unlock:
693 spin_unlock(&cookie->lock); 713 spin_unlock(&cookie->lock);
694 atomic_dec(&cookie->n_active);
695 kfree(op); 714 kfree(op);
715 if (wake_cookie)
716 __fscache_wake_unused_cookie(cookie);
696nobufs: 717nobufs:
697 fscache_stat(&fscache_n_allocs_nobufs); 718 fscache_stat(&fscache_n_allocs_nobufs);
698 _leave(" = -ENOBUFS"); 719 _leave(" = -ENOBUFS");
@@ -889,6 +910,7 @@ int __fscache_write_page(struct fscache_cookie *cookie,
889{ 910{
890 struct fscache_storage *op; 911 struct fscache_storage *op;
891 struct fscache_object *object; 912 struct fscache_object *object;
913 bool wake_cookie = false;
892 int ret; 914 int ret;
893 915
894 _enter("%p,%x,", cookie, (u32) page->flags); 916 _enter("%p,%x,", cookie, (u32) page->flags);
@@ -920,7 +942,8 @@ int __fscache_write_page(struct fscache_cookie *cookie,
920 ret = -ENOBUFS; 942 ret = -ENOBUFS;
921 spin_lock(&cookie->lock); 943 spin_lock(&cookie->lock);
922 944
923 if (hlist_empty(&cookie->backing_objects)) 945 if (!fscache_cookie_enabled(cookie) ||
946 hlist_empty(&cookie->backing_objects))
924 goto nobufs; 947 goto nobufs;
925 object = hlist_entry(cookie->backing_objects.first, 948 object = hlist_entry(cookie->backing_objects.first,
926 struct fscache_object, cookie_link); 949 struct fscache_object, cookie_link);
@@ -957,7 +980,7 @@ int __fscache_write_page(struct fscache_cookie *cookie,
957 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id); 980 op->op.debug_id = atomic_inc_return(&fscache_op_debug_id);
958 op->store_limit = object->store_limit; 981 op->store_limit = object->store_limit;
959 982
960 atomic_inc(&cookie->n_active); 983 __fscache_use_cookie(cookie);
961 if (fscache_submit_op(object, &op->op) < 0) 984 if (fscache_submit_op(object, &op->op) < 0)
962 goto submit_failed; 985 goto submit_failed;
963 986
@@ -984,10 +1007,10 @@ already_pending:
984 return 0; 1007 return 0;
985 1008
986submit_failed: 1009submit_failed:
987 atomic_dec(&cookie->n_active);
988 spin_lock(&cookie->stores_lock); 1010 spin_lock(&cookie->stores_lock);
989 radix_tree_delete(&cookie->stores, page->index); 1011 radix_tree_delete(&cookie->stores, page->index);
990 spin_unlock(&cookie->stores_lock); 1012 spin_unlock(&cookie->stores_lock);
1013 wake_cookie = __fscache_unuse_cookie(cookie);
991 page_cache_release(page); 1014 page_cache_release(page);
992 ret = -ENOBUFS; 1015 ret = -ENOBUFS;
993 goto nobufs; 1016 goto nobufs;
@@ -999,6 +1022,8 @@ nobufs:
999 spin_unlock(&cookie->lock); 1022 spin_unlock(&cookie->lock);
1000 radix_tree_preload_end(); 1023 radix_tree_preload_end();
1001 kfree(op); 1024 kfree(op);
1025 if (wake_cookie)
1026 __fscache_wake_unused_cookie(cookie);
1002 fscache_stat(&fscache_n_stores_nobufs); 1027 fscache_stat(&fscache_n_stores_nobufs);
1003 _leave(" = -ENOBUFS"); 1028 _leave(" = -ENOBUFS");
1004 return -ENOBUFS; 1029 return -ENOBUFS;
diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
index 6cc51ae0638d..9a8676f33350 100644
--- a/fs/nfs/dir.c
+++ b/fs/nfs/dir.c
@@ -1387,7 +1387,7 @@ static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, i
1387 1387
1388static int do_open(struct inode *inode, struct file *filp) 1388static int do_open(struct inode *inode, struct file *filp)
1389{ 1389{
1390 nfs_fscache_set_inode_cookie(inode, filp); 1390 nfs_fscache_open_file(inode, filp);
1391 return 0; 1391 return 0;
1392} 1392}
1393 1393
diff --git a/fs/nfs/fscache.c b/fs/nfs/fscache.c
index 24d1d1c5fcaf..3ef01f0ba0bc 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;
@@ -178,163 +178,79 @@ void nfs_fscache_release_super_cookie(struct super_block *sb)
178/* 178/*
179 * Initialise the per-inode cache cookie pointer for an NFS inode. 179 * Initialise the per-inode cache cookie pointer for an NFS inode.
180 */ 180 */
181void nfs_fscache_init_inode_cookie(struct inode *inode) 181void nfs_fscache_init_inode(struct inode *inode)
182{ 182{
183 NFS_I(inode)->fscache = NULL;
184 if (S_ISREG(inode->i_mode))
185 set_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags);
186}
187
188/*
189 * Get the per-inode cache cookie for an NFS inode.
190 */
191static void nfs_fscache_enable_inode_cookie(struct inode *inode)
192{
193 struct super_block *sb = inode->i_sb;
194 struct nfs_inode *nfsi = NFS_I(inode); 183 struct nfs_inode *nfsi = NFS_I(inode);
195 184
196 if (nfsi->fscache || !NFS_FSCACHE(inode)) 185 nfsi->fscache = NULL;
186 if (!S_ISREG(inode->i_mode))
197 return; 187 return;
198 188 nfsi->fscache = fscache_acquire_cookie(NFS_SB(inode->i_sb)->fscache,
199 if ((NFS_SB(sb)->options & NFS_OPTION_FSCACHE)) { 189 &nfs_fscache_inode_object_def,
200 nfsi->fscache = fscache_acquire_cookie( 190 nfsi, false);
201 NFS_SB(sb)->fscache,
202 &nfs_fscache_inode_object_def,
203 nfsi);
204
205 dfprintk(FSCACHE, "NFS: get FH cookie (0x%p/0x%p/0x%p)\n",
206 sb, nfsi, nfsi->fscache);
207 }
208} 191}
209 192
210/* 193/*
211 * Release a per-inode cookie. 194 * Release a per-inode cookie.
212 */ 195 */
213void nfs_fscache_release_inode_cookie(struct inode *inode) 196void nfs_fscache_clear_inode(struct inode *inode)
214{ 197{
215 struct nfs_inode *nfsi = NFS_I(inode); 198 struct nfs_inode *nfsi = NFS_I(inode);
199 struct fscache_cookie *cookie = nfs_i_fscache(inode);
216 200
217 dfprintk(FSCACHE, "NFS: clear cookie (0x%p/0x%p)\n", 201 dfprintk(FSCACHE, "NFS: clear cookie (0x%p/0x%p)\n", nfsi, cookie);
218 nfsi, nfsi->fscache);
219 202
220 fscache_relinquish_cookie(nfsi->fscache, 0); 203 fscache_relinquish_cookie(cookie, false);
221 nfsi->fscache = NULL; 204 nfsi->fscache = NULL;
222} 205}
223 206
224/* 207static bool nfs_fscache_can_enable(void *data)
225 * Retire a per-inode cookie, destroying the data attached to it.
226 */
227void nfs_fscache_zap_inode_cookie(struct inode *inode)
228{ 208{
229 struct nfs_inode *nfsi = NFS_I(inode); 209 struct inode *inode = data;
230 210
231 dfprintk(FSCACHE, "NFS: zapping cookie (0x%p/0x%p)\n", 211 return !inode_is_open_for_write(inode);
232 nfsi, nfsi->fscache);
233
234 fscache_relinquish_cookie(nfsi->fscache, 1);
235 nfsi->fscache = NULL;
236} 212}
237 213
238/* 214/*
239 * Turn off the cache with regard to a per-inode cookie if opened for writing, 215 * Enable or disable caching for a file that is being opened as appropriate.
240 * invalidating all the pages in the page cache relating to the associated 216 * The cookie is allocated when the inode is initialised, but is not enabled at
241 * inode to clear the per-page caching. 217 * that time. Enablement is deferred to file-open time to avoid stat() and
242 */ 218 * access() thrashing the cache.
243static void nfs_fscache_disable_inode_cookie(struct inode *inode) 219 *
244{ 220 * For now, with NFS, only regular files that are open read-only will be able
245 clear_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags); 221 * to use the cache.
246 222 *
247 if (NFS_I(inode)->fscache) { 223 * We enable the cache for an inode if we open it read-only and it isn't
248 dfprintk(FSCACHE, 224 * currently open for writing. We disable the cache if the inode is open
249 "NFS: nfsi 0x%p turning cache off\n", NFS_I(inode)); 225 * write-only.
250 226 *
251 /* Need to uncache any pages attached to this inode that 227 * The caller uses the file struct to pin i_writecount on the inode before
252 * fscache knows about before turning off the cache. 228 * calling us when a file is opened for writing, so we can make use of that.
253 */ 229 *
254 fscache_uncache_all_inode_pages(NFS_I(inode)->fscache, inode); 230 * Note that this may be invoked multiple times in parallel by parallel
255 nfs_fscache_zap_inode_cookie(inode); 231 * nfs_open() functions.
256 }
257}
258
259/*
260 * wait_on_bit() sleep function for uninterruptible waiting
261 */
262static int nfs_fscache_wait_bit(void *flags)
263{
264 schedule();
265 return 0;
266}
267
268/*
269 * Lock against someone else trying to also acquire or relinquish a cookie
270 */
271static inline void nfs_fscache_inode_lock(struct inode *inode)
272{
273 struct nfs_inode *nfsi = NFS_I(inode);
274
275 while (test_and_set_bit(NFS_INO_FSCACHE_LOCK, &nfsi->flags))
276 wait_on_bit(&nfsi->flags, NFS_INO_FSCACHE_LOCK,
277 nfs_fscache_wait_bit, TASK_UNINTERRUPTIBLE);
278}
279
280/*
281 * Unlock cookie management lock
282 */
283static inline void nfs_fscache_inode_unlock(struct inode *inode)
284{
285 struct nfs_inode *nfsi = NFS_I(inode);
286
287 smp_mb__before_clear_bit();
288 clear_bit(NFS_INO_FSCACHE_LOCK, &nfsi->flags);
289 smp_mb__after_clear_bit();
290 wake_up_bit(&nfsi->flags, NFS_INO_FSCACHE_LOCK);
291}
292
293/*
294 * Decide if we should enable or disable local caching for this inode.
295 * - For now, with NFS, only regular files that are open read-only will be able
296 * to use the cache.
297 * - May be invoked multiple times in parallel by parallel nfs_open() functions.
298 */
299void nfs_fscache_set_inode_cookie(struct inode *inode, struct file *filp)
300{
301 if (NFS_FSCACHE(inode)) {
302 nfs_fscache_inode_lock(inode);
303 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
304 nfs_fscache_disable_inode_cookie(inode);
305 else
306 nfs_fscache_enable_inode_cookie(inode);
307 nfs_fscache_inode_unlock(inode);
308 }
309}
310EXPORT_SYMBOL_GPL(nfs_fscache_set_inode_cookie);
311
312/*
313 * Replace a per-inode cookie due to revalidation detecting a file having
314 * changed on the server.
315 */ 232 */
316void nfs_fscache_reset_inode_cookie(struct inode *inode) 233void nfs_fscache_open_file(struct inode *inode, struct file *filp)
317{ 234{
318 struct nfs_inode *nfsi = NFS_I(inode); 235 struct nfs_inode *nfsi = NFS_I(inode);
319 struct nfs_server *nfss = NFS_SERVER(inode); 236 struct fscache_cookie *cookie = nfs_i_fscache(inode);
320 NFS_IFDEBUG(struct fscache_cookie *old = nfsi->fscache);
321 237
322 nfs_fscache_inode_lock(inode); 238 if (!fscache_cookie_valid(cookie))
323 if (nfsi->fscache) { 239 return;
324 /* retire the current fscache cache and get a new one */
325 fscache_relinquish_cookie(nfsi->fscache, 1);
326
327 nfsi->fscache = fscache_acquire_cookie(
328 nfss->nfs_client->fscache,
329 &nfs_fscache_inode_object_def,
330 nfsi);
331 240
332 dfprintk(FSCACHE, 241 if (inode_is_open_for_write(inode)) {
333 "NFS: revalidation new cookie (0x%p/0x%p/0x%p/0x%p)\n", 242 dfprintk(FSCACHE, "NFS: nfsi 0x%p disabling cache\n", nfsi);
334 nfss, nfsi, old, nfsi->fscache); 243 clear_bit(NFS_INO_FSCACHE, &nfsi->flags);
244 fscache_disable_cookie(cookie, true);
245 fscache_uncache_all_inode_pages(cookie, inode);
246 } else {
247 dfprintk(FSCACHE, "NFS: nfsi 0x%p enabling cache\n", nfsi);
248 fscache_enable_cookie(cookie, nfs_fscache_can_enable, inode);
249 if (fscache_cookie_enabled(cookie))
250 set_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags);
335 } 251 }
336 nfs_fscache_inode_unlock(inode);
337} 252}
253EXPORT_SYMBOL_GPL(nfs_fscache_open_file);
338 254
339/* 255/*
340 * Release the caching state associated with a page, if the page isn't busy 256 * Release the caching state associated with a page, if the page isn't busy
@@ -344,12 +260,11 @@ void nfs_fscache_reset_inode_cookie(struct inode *inode)
344int nfs_fscache_release_page(struct page *page, gfp_t gfp) 260int nfs_fscache_release_page(struct page *page, gfp_t gfp)
345{ 261{
346 if (PageFsCache(page)) { 262 if (PageFsCache(page)) {
347 struct nfs_inode *nfsi = NFS_I(page->mapping->host); 263 struct fscache_cookie *cookie = nfs_i_fscache(page->mapping->host);
348 struct fscache_cookie *cookie = nfsi->fscache;
349 264
350 BUG_ON(!cookie); 265 BUG_ON(!cookie);
351 dfprintk(FSCACHE, "NFS: fscache releasepage (0x%p/0x%p/0x%p)\n", 266 dfprintk(FSCACHE, "NFS: fscache releasepage (0x%p/0x%p/0x%p)\n",
352 cookie, page, nfsi); 267 cookie, page, NFS_I(page->mapping->host));
353 268
354 if (!fscache_maybe_release_page(cookie, page, gfp)) 269 if (!fscache_maybe_release_page(cookie, page, gfp))
355 return 0; 270 return 0;
@@ -367,13 +282,12 @@ int nfs_fscache_release_page(struct page *page, gfp_t gfp)
367 */ 282 */
368void __nfs_fscache_invalidate_page(struct page *page, struct inode *inode) 283void __nfs_fscache_invalidate_page(struct page *page, struct inode *inode)
369{ 284{
370 struct nfs_inode *nfsi = NFS_I(inode); 285 struct fscache_cookie *cookie = nfs_i_fscache(inode);
371 struct fscache_cookie *cookie = nfsi->fscache;
372 286
373 BUG_ON(!cookie); 287 BUG_ON(!cookie);
374 288
375 dfprintk(FSCACHE, "NFS: fscache invalidatepage (0x%p/0x%p/0x%p)\n", 289 dfprintk(FSCACHE, "NFS: fscache invalidatepage (0x%p/0x%p/0x%p)\n",
376 cookie, page, nfsi); 290 cookie, page, NFS_I(inode));
377 291
378 fscache_wait_on_page_write(cookie, page); 292 fscache_wait_on_page_write(cookie, page);
379 293
@@ -417,9 +331,9 @@ int __nfs_readpage_from_fscache(struct nfs_open_context *ctx,
417 331
418 dfprintk(FSCACHE, 332 dfprintk(FSCACHE,
419 "NFS: readpage_from_fscache(fsc:%p/p:%p(i:%lx f:%lx)/0x%p)\n", 333 "NFS: readpage_from_fscache(fsc:%p/p:%p(i:%lx f:%lx)/0x%p)\n",
420 NFS_I(inode)->fscache, page, page->index, page->flags, inode); 334 nfs_i_fscache(inode), page, page->index, page->flags, inode);
421 335
422 ret = fscache_read_or_alloc_page(NFS_I(inode)->fscache, 336 ret = fscache_read_or_alloc_page(nfs_i_fscache(inode),
423 page, 337 page,
424 nfs_readpage_from_fscache_complete, 338 nfs_readpage_from_fscache_complete,
425 ctx, 339 ctx,
@@ -459,9 +373,9 @@ int __nfs_readpages_from_fscache(struct nfs_open_context *ctx,
459 int ret; 373 int ret;
460 374
461 dfprintk(FSCACHE, "NFS: nfs_getpages_from_fscache (0x%p/%u/0x%p)\n", 375 dfprintk(FSCACHE, "NFS: nfs_getpages_from_fscache (0x%p/%u/0x%p)\n",
462 NFS_I(inode)->fscache, npages, inode); 376 nfs_i_fscache(inode), npages, inode);
463 377
464 ret = fscache_read_or_alloc_pages(NFS_I(inode)->fscache, 378 ret = fscache_read_or_alloc_pages(nfs_i_fscache(inode),
465 mapping, pages, nr_pages, 379 mapping, pages, nr_pages,
466 nfs_readpage_from_fscache_complete, 380 nfs_readpage_from_fscache_complete,
467 ctx, 381 ctx,
@@ -506,15 +420,15 @@ void __nfs_readpage_to_fscache(struct inode *inode, struct page *page, int sync)
506 420
507 dfprintk(FSCACHE, 421 dfprintk(FSCACHE,
508 "NFS: readpage_to_fscache(fsc:%p/p:%p(i:%lx f:%lx)/%d)\n", 422 "NFS: readpage_to_fscache(fsc:%p/p:%p(i:%lx f:%lx)/%d)\n",
509 NFS_I(inode)->fscache, page, page->index, page->flags, sync); 423 nfs_i_fscache(inode), page, page->index, page->flags, sync);
510 424
511 ret = fscache_write_page(NFS_I(inode)->fscache, page, GFP_KERNEL); 425 ret = fscache_write_page(nfs_i_fscache(inode), page, GFP_KERNEL);
512 dfprintk(FSCACHE, 426 dfprintk(FSCACHE,
513 "NFS: readpage_to_fscache: p:%p(i:%lu f:%lx) ret %d\n", 427 "NFS: readpage_to_fscache: p:%p(i:%lu f:%lx) ret %d\n",
514 page, page->index, page->flags, ret); 428 page, page->index, page->flags, ret);
515 429
516 if (ret != 0) { 430 if (ret != 0) {
517 fscache_uncache_page(NFS_I(inode)->fscache, page); 431 fscache_uncache_page(nfs_i_fscache(inode), page);
518 nfs_add_fscache_stats(inode, 432 nfs_add_fscache_stats(inode,
519 NFSIOS_FSCACHE_PAGES_WRITTEN_FAIL, 1); 433 NFSIOS_FSCACHE_PAGES_WRITTEN_FAIL, 1);
520 nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_UNCACHED, 1); 434 nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_UNCACHED, 1);
diff --git a/fs/nfs/fscache.h b/fs/nfs/fscache.h
index 4ecb76652eba..d7fe3e799f2f 100644
--- a/fs/nfs/fscache.h
+++ b/fs/nfs/fscache.h
@@ -76,11 +76,9 @@ extern void nfs_fscache_release_client_cookie(struct nfs_client *);
76extern void nfs_fscache_get_super_cookie(struct super_block *, const char *, int); 76extern void nfs_fscache_get_super_cookie(struct super_block *, const char *, int);
77extern void nfs_fscache_release_super_cookie(struct super_block *); 77extern void nfs_fscache_release_super_cookie(struct super_block *);
78 78
79extern void nfs_fscache_init_inode_cookie(struct inode *); 79extern void nfs_fscache_init_inode(struct inode *);
80extern void nfs_fscache_release_inode_cookie(struct inode *); 80extern void nfs_fscache_clear_inode(struct inode *);
81extern void nfs_fscache_zap_inode_cookie(struct inode *); 81extern void nfs_fscache_open_file(struct inode *, struct file *);
82extern void nfs_fscache_set_inode_cookie(struct inode *, struct file *);
83extern void nfs_fscache_reset_inode_cookie(struct inode *);
84 82
85extern void __nfs_fscache_invalidate_page(struct page *, struct inode *); 83extern void __nfs_fscache_invalidate_page(struct page *, struct inode *);
86extern int nfs_fscache_release_page(struct page *, gfp_t); 84extern 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) {}
187 185
188static inline void nfs_fscache_release_super_cookie(struct super_block *sb) {} 186static inline void nfs_fscache_release_super_cookie(struct super_block *sb) {}
189 187
190static inline void nfs_fscache_init_inode_cookie(struct inode *inode) {} 188static inline void nfs_fscache_init_inode(struct inode *inode) {}
191static inline void nfs_fscache_release_inode_cookie(struct inode *inode) {} 189static inline void nfs_fscache_clear_inode(struct inode *inode) {}
192static inline void nfs_fscache_zap_inode_cookie(struct inode *inode) {} 190static inline void nfs_fscache_open_file(struct inode *inode,
193static inline void nfs_fscache_set_inode_cookie(struct inode *inode, 191 struct file *filp) {}
194 struct file *filp) {}
195static inline void nfs_fscache_reset_inode_cookie(struct inode *inode) {}
196 192
197static inline int nfs_fscache_release_page(struct page *page, gfp_t gfp) 193static inline int nfs_fscache_release_page(struct page *page, gfp_t gfp)
198{ 194{
diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c
index 4bc7538f951e..471ba59c42f9 100644
--- a/fs/nfs/inode.c
+++ b/fs/nfs/inode.c
@@ -122,7 +122,7 @@ void nfs_clear_inode(struct inode *inode)
122 WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files)); 122 WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
123 nfs_zap_acl_cache(inode); 123 nfs_zap_acl_cache(inode);
124 nfs_access_zap_cache(inode); 124 nfs_access_zap_cache(inode);
125 nfs_fscache_release_inode_cookie(inode); 125 nfs_fscache_clear_inode(inode);
126} 126}
127EXPORT_SYMBOL_GPL(nfs_clear_inode); 127EXPORT_SYMBOL_GPL(nfs_clear_inode);
128 128
@@ -459,7 +459,7 @@ nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, st
459 nfsi->attrtimeo_timestamp = now; 459 nfsi->attrtimeo_timestamp = now;
460 nfsi->access_cache = RB_ROOT; 460 nfsi->access_cache = RB_ROOT;
461 461
462 nfs_fscache_init_inode_cookie(inode); 462 nfs_fscache_init_inode(inode);
463 463
464 unlock_new_inode(inode); 464 unlock_new_inode(inode);
465 } else 465 } else
@@ -854,7 +854,7 @@ int nfs_open(struct inode *inode, struct file *filp)
854 return PTR_ERR(ctx); 854 return PTR_ERR(ctx);
855 nfs_file_set_open_context(filp, ctx); 855 nfs_file_set_open_context(filp, ctx);
856 put_nfs_open_context(ctx); 856 put_nfs_open_context(ctx);
857 nfs_fscache_set_inode_cookie(inode, filp); 857 nfs_fscache_open_file(inode, filp);
858 return 0; 858 return 0;
859} 859}
860 860
diff --git a/fs/nfs/nfs4file.c b/fs/nfs/nfs4file.c
index 77efaf15ec90..1f01b55692ee 100644
--- a/fs/nfs/nfs4file.c
+++ b/fs/nfs/nfs4file.c
@@ -75,7 +75,7 @@ nfs4_file_open(struct inode *inode, struct file *filp)
75 75
76 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 76 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
77 nfs_file_set_open_context(filp, ctx); 77 nfs_file_set_open_context(filp, ctx);
78 nfs_fscache_set_inode_cookie(inode, filp); 78 nfs_fscache_open_file(inode, filp);
79 err = 0; 79 err = 0;
80 80
81out_put_ctx: 81out_put_ctx:
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 3f40547ba191..955dff5da56a 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2292,6 +2292,11 @@ static inline void allow_write_access(struct file *file)
2292 if (file) 2292 if (file)
2293 atomic_inc(&file_inode(file)->i_writecount); 2293 atomic_inc(&file_inode(file)->i_writecount);
2294} 2294}
2295static inline bool inode_is_open_for_write(const struct inode *inode)
2296{
2297 return atomic_read(&inode->i_writecount) > 0;
2298}
2299
2295#ifdef CONFIG_IMA 2300#ifdef CONFIG_IMA
2296static inline void i_readcount_dec(struct inode *inode) 2301static inline void i_readcount_dec(struct inode *inode)
2297{ 2302{
diff --git a/include/linux/fscache-cache.h b/include/linux/fscache-cache.h
index 7823e9ef995e..771484993ca7 100644
--- a/include/linux/fscache-cache.h
+++ b/include/linux/fscache-cache.h
@@ -308,36 +308,6 @@ struct fscache_cache_ops {
308 void (*dissociate_pages)(struct fscache_cache *cache); 308 void (*dissociate_pages)(struct fscache_cache *cache);
309}; 309};
310 310
311/*
312 * data file or index object cookie
313 * - a file will only appear in one cache
314 * - a request to cache a file may or may not be honoured, subject to
315 * constraints such as disk space
316 * - indices are created on disk just-in-time
317 */
318struct fscache_cookie {
319 atomic_t usage; /* number of users of this cookie */
320 atomic_t n_children; /* number of children of this cookie */
321 atomic_t n_active; /* number of active users of netfs ptrs */
322 spinlock_t lock;
323 spinlock_t stores_lock; /* lock on page store tree */
324 struct hlist_head backing_objects; /* object(s) backing this file/index */
325 const struct fscache_cookie_def *def; /* definition */
326 struct fscache_cookie *parent; /* parent of this entry */
327 void *netfs_data; /* back pointer to netfs */
328 struct radix_tree_root stores; /* pages to be stored on this cookie */
329#define FSCACHE_COOKIE_PENDING_TAG 0 /* pages tag: pending write to cache */
330#define FSCACHE_COOKIE_STORING_TAG 1 /* pages tag: writing to cache */
331
332 unsigned long flags;
333#define FSCACHE_COOKIE_LOOKING_UP 0 /* T if non-index cookie being looked up still */
334#define FSCACHE_COOKIE_NO_DATA_YET 1 /* T if new object with no cached data yet */
335#define FSCACHE_COOKIE_UNAVAILABLE 2 /* T if cookie is unavailable (error, etc) */
336#define FSCACHE_COOKIE_INVALIDATING 3 /* T if cookie is being invalidated */
337#define FSCACHE_COOKIE_RELINQUISHED 4 /* T if cookie has been relinquished */
338#define FSCACHE_COOKIE_RETIRED 5 /* T if cookie was retired */
339};
340
341extern struct fscache_cookie fscache_fsdef_index; 311extern struct fscache_cookie fscache_fsdef_index;
342 312
343/* 313/*
@@ -400,6 +370,7 @@ struct fscache_object {
400#define FSCACHE_OBJECT_IS_LIVE 3 /* T if object is not withdrawn or relinquished */ 370#define FSCACHE_OBJECT_IS_LIVE 3 /* T if object is not withdrawn or relinquished */
401#define FSCACHE_OBJECT_IS_LOOKED_UP 4 /* T if object has been looked up */ 371#define FSCACHE_OBJECT_IS_LOOKED_UP 4 /* T if object has been looked up */
402#define FSCACHE_OBJECT_IS_AVAILABLE 5 /* T if object has become active */ 372#define FSCACHE_OBJECT_IS_AVAILABLE 5 /* T if object has become active */
373#define FSCACHE_OBJECT_RETIRED 6 /* T if object was retired on relinquishment */
403 374
404 struct list_head cache_link; /* link in cache->object_list */ 375 struct list_head cache_link; /* link in cache->object_list */
405 struct hlist_node cookie_link; /* link in cookie->backing_objects */ 376 struct hlist_node cookie_link; /* link in cookie->backing_objects */
@@ -511,6 +482,11 @@ static inline void fscache_end_io(struct fscache_retrieval *op,
511 op->end_io_func(page, op->context, error); 482 op->end_io_func(page, op->context, error);
512} 483}
513 484
485static inline void __fscache_use_cookie(struct fscache_cookie *cookie)
486{
487 atomic_inc(&cookie->n_active);
488}
489
514/** 490/**
515 * fscache_use_cookie - Request usage of cookie attached to an object 491 * fscache_use_cookie - Request usage of cookie attached to an object
516 * @object: Object description 492 * @object: Object description
@@ -524,6 +500,16 @@ static inline bool fscache_use_cookie(struct fscache_object *object)
524 return atomic_inc_not_zero(&cookie->n_active) != 0; 500 return atomic_inc_not_zero(&cookie->n_active) != 0;
525} 501}
526 502
503static inline bool __fscache_unuse_cookie(struct fscache_cookie *cookie)
504{
505 return atomic_dec_and_test(&cookie->n_active);
506}
507
508static inline void __fscache_wake_unused_cookie(struct fscache_cookie *cookie)
509{
510 wake_up_atomic_t(&cookie->n_active);
511}
512
527/** 513/**
528 * fscache_unuse_cookie - Cease usage of cookie attached to an object 514 * fscache_unuse_cookie - Cease usage of cookie attached to an object
529 * @object: Object description 515 * @object: Object description
@@ -534,8 +520,8 @@ static inline bool fscache_use_cookie(struct fscache_object *object)
534static inline void fscache_unuse_cookie(struct fscache_object *object) 520static inline void fscache_unuse_cookie(struct fscache_object *object)
535{ 521{
536 struct fscache_cookie *cookie = object->cookie; 522 struct fscache_cookie *cookie = object->cookie;
537 if (atomic_dec_and_test(&cookie->n_active)) 523 if (__fscache_unuse_cookie(cookie))
538 wake_up_atomic_t(&cookie->n_active); 524 __fscache_wake_unused_cookie(cookie);
539} 525}
540 526
541/* 527/*
diff --git a/include/linux/fscache.h b/include/linux/fscache.h
index 19b46458e4e8..115bb81912cc 100644
--- a/include/linux/fscache.h
+++ b/include/linux/fscache.h
@@ -167,6 +167,42 @@ struct fscache_netfs {
167}; 167};
168 168
169/* 169/*
170 * data file or index object cookie
171 * - a file will only appear in one cache
172 * - a request to cache a file may or may not be honoured, subject to
173 * constraints such as disk space
174 * - indices are created on disk just-in-time
175 */
176struct fscache_cookie {
177 atomic_t usage; /* number of users of this cookie */
178 atomic_t n_children; /* number of children of this cookie */
179 atomic_t n_active; /* number of active users of netfs ptrs */
180 spinlock_t lock;
181 spinlock_t stores_lock; /* lock on page store tree */
182 struct hlist_head backing_objects; /* object(s) backing this file/index */
183 const struct fscache_cookie_def *def; /* definition */
184 struct fscache_cookie *parent; /* parent of this entry */
185 void *netfs_data; /* back pointer to netfs */
186 struct radix_tree_root stores; /* pages to be stored on this cookie */
187#define FSCACHE_COOKIE_PENDING_TAG 0 /* pages tag: pending write to cache */
188#define FSCACHE_COOKIE_STORING_TAG 1 /* pages tag: writing to cache */
189
190 unsigned long flags;
191#define FSCACHE_COOKIE_LOOKING_UP 0 /* T if non-index cookie being looked up still */
192#define FSCACHE_COOKIE_NO_DATA_YET 1 /* T if new object with no cached data yet */
193#define FSCACHE_COOKIE_UNAVAILABLE 2 /* T if cookie is unavailable (error, etc) */
194#define FSCACHE_COOKIE_INVALIDATING 3 /* T if cookie is being invalidated */
195#define FSCACHE_COOKIE_RELINQUISHED 4 /* T if cookie has been relinquished */
196#define FSCACHE_COOKIE_ENABLED 5 /* T if cookie is enabled */
197#define FSCACHE_COOKIE_ENABLEMENT_LOCK 6 /* T if cookie is being en/disabled */
198};
199
200static inline bool fscache_cookie_enabled(struct fscache_cookie *cookie)
201{
202 return test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
203}
204
205/*
170 * slow-path functions for when there is actually caching available, and the 206 * slow-path functions for when there is actually caching available, and the
171 * netfs does actually have a valid token 207 * netfs does actually have a valid token
172 * - these are not to be called directly 208 * - these are not to be called directly
@@ -181,8 +217,8 @@ extern void __fscache_release_cache_tag(struct fscache_cache_tag *);
181extern struct fscache_cookie *__fscache_acquire_cookie( 217extern struct fscache_cookie *__fscache_acquire_cookie(
182 struct fscache_cookie *, 218 struct fscache_cookie *,
183 const struct fscache_cookie_def *, 219 const struct fscache_cookie_def *,
184 void *); 220 void *, bool);
185extern void __fscache_relinquish_cookie(struct fscache_cookie *, int); 221extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
186extern int __fscache_check_consistency(struct fscache_cookie *); 222extern int __fscache_check_consistency(struct fscache_cookie *);
187extern void __fscache_update_cookie(struct fscache_cookie *); 223extern void __fscache_update_cookie(struct fscache_cookie *);
188extern int __fscache_attr_changed(struct fscache_cookie *); 224extern int __fscache_attr_changed(struct fscache_cookie *);
@@ -211,6 +247,9 @@ extern void __fscache_uncache_all_inode_pages(struct fscache_cookie *,
211 struct inode *); 247 struct inode *);
212extern void __fscache_readpages_cancel(struct fscache_cookie *cookie, 248extern void __fscache_readpages_cancel(struct fscache_cookie *cookie,
213 struct list_head *pages); 249 struct list_head *pages);
250extern void __fscache_disable_cookie(struct fscache_cookie *, bool);
251extern void __fscache_enable_cookie(struct fscache_cookie *,
252 bool (*)(void *), void *);
214 253
215/** 254/**
216 * fscache_register_netfs - Register a filesystem as desiring caching services 255 * fscache_register_netfs - Register a filesystem as desiring caching services
@@ -289,6 +328,7 @@ void fscache_release_cache_tag(struct fscache_cache_tag *tag)
289 * @def: A description of the cache object, including callback operations 328 * @def: A description of the cache object, including callback operations
290 * @netfs_data: An arbitrary piece of data to be kept in the cookie to 329 * @netfs_data: An arbitrary piece of data to be kept in the cookie to
291 * represent the cache object to the netfs 330 * represent the cache object to the netfs
331 * @enable: Whether or not to enable a data cookie immediately
292 * 332 *
293 * This function is used to inform FS-Cache about part of an index hierarchy 333 * This function is used to inform FS-Cache about part of an index hierarchy
294 * that can be used to locate files. This is done by requesting a cookie for 334 * that can be used to locate files. This is done by requesting a cookie for
@@ -301,10 +341,12 @@ static inline
301struct fscache_cookie *fscache_acquire_cookie( 341struct fscache_cookie *fscache_acquire_cookie(
302 struct fscache_cookie *parent, 342 struct fscache_cookie *parent,
303 const struct fscache_cookie_def *def, 343 const struct fscache_cookie_def *def,
304 void *netfs_data) 344 void *netfs_data,
345 bool enable)
305{ 346{
306 if (fscache_cookie_valid(parent)) 347 if (fscache_cookie_valid(parent) && fscache_cookie_enabled(parent))
307 return __fscache_acquire_cookie(parent, def, netfs_data); 348 return __fscache_acquire_cookie(parent, def, netfs_data,
349 enable);
308 else 350 else
309 return NULL; 351 return NULL;
310} 352}
@@ -322,7 +364,7 @@ struct fscache_cookie *fscache_acquire_cookie(
322 * description. 364 * description.
323 */ 365 */
324static inline 366static inline
325void fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire) 367void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
326{ 368{
327 if (fscache_cookie_valid(cookie)) 369 if (fscache_cookie_valid(cookie))
328 __fscache_relinquish_cookie(cookie, retire); 370 __fscache_relinquish_cookie(cookie, retire);
@@ -341,7 +383,7 @@ void fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
341static inline 383static inline
342int fscache_check_consistency(struct fscache_cookie *cookie) 384int fscache_check_consistency(struct fscache_cookie *cookie)
343{ 385{
344 if (fscache_cookie_valid(cookie)) 386 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
345 return __fscache_check_consistency(cookie); 387 return __fscache_check_consistency(cookie);
346 else 388 else
347 return 0; 389 return 0;
@@ -360,7 +402,7 @@ int fscache_check_consistency(struct fscache_cookie *cookie)
360static inline 402static inline
361void fscache_update_cookie(struct fscache_cookie *cookie) 403void fscache_update_cookie(struct fscache_cookie *cookie)
362{ 404{
363 if (fscache_cookie_valid(cookie)) 405 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
364 __fscache_update_cookie(cookie); 406 __fscache_update_cookie(cookie);
365} 407}
366 408
@@ -407,7 +449,7 @@ void fscache_unpin_cookie(struct fscache_cookie *cookie)
407static inline 449static inline
408int fscache_attr_changed(struct fscache_cookie *cookie) 450int fscache_attr_changed(struct fscache_cookie *cookie)
409{ 451{
410 if (fscache_cookie_valid(cookie)) 452 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
411 return __fscache_attr_changed(cookie); 453 return __fscache_attr_changed(cookie);
412 else 454 else
413 return -ENOBUFS; 455 return -ENOBUFS;
@@ -429,7 +471,7 @@ int fscache_attr_changed(struct fscache_cookie *cookie)
429static inline 471static inline
430void fscache_invalidate(struct fscache_cookie *cookie) 472void fscache_invalidate(struct fscache_cookie *cookie)
431{ 473{
432 if (fscache_cookie_valid(cookie)) 474 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
433 __fscache_invalidate(cookie); 475 __fscache_invalidate(cookie);
434} 476}
435 477
@@ -503,7 +545,7 @@ int fscache_read_or_alloc_page(struct fscache_cookie *cookie,
503 void *context, 545 void *context,
504 gfp_t gfp) 546 gfp_t gfp)
505{ 547{
506 if (fscache_cookie_valid(cookie)) 548 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
507 return __fscache_read_or_alloc_page(cookie, page, end_io_func, 549 return __fscache_read_or_alloc_page(cookie, page, end_io_func,
508 context, gfp); 550 context, gfp);
509 else 551 else
@@ -554,7 +596,7 @@ int fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
554 void *context, 596 void *context,
555 gfp_t gfp) 597 gfp_t gfp)
556{ 598{
557 if (fscache_cookie_valid(cookie)) 599 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
558 return __fscache_read_or_alloc_pages(cookie, mapping, pages, 600 return __fscache_read_or_alloc_pages(cookie, mapping, pages,
559 nr_pages, end_io_func, 601 nr_pages, end_io_func,
560 context, gfp); 602 context, gfp);
@@ -585,7 +627,7 @@ int fscache_alloc_page(struct fscache_cookie *cookie,
585 struct page *page, 627 struct page *page,
586 gfp_t gfp) 628 gfp_t gfp)
587{ 629{
588 if (fscache_cookie_valid(cookie)) 630 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
589 return __fscache_alloc_page(cookie, page, gfp); 631 return __fscache_alloc_page(cookie, page, gfp);
590 else 632 else
591 return -ENOBUFS; 633 return -ENOBUFS;
@@ -634,7 +676,7 @@ int fscache_write_page(struct fscache_cookie *cookie,
634 struct page *page, 676 struct page *page,
635 gfp_t gfp) 677 gfp_t gfp)
636{ 678{
637 if (fscache_cookie_valid(cookie)) 679 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
638 return __fscache_write_page(cookie, page, gfp); 680 return __fscache_write_page(cookie, page, gfp);
639 else 681 else
640 return -ENOBUFS; 682 return -ENOBUFS;
@@ -744,4 +786,47 @@ void fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
744 __fscache_uncache_all_inode_pages(cookie, inode); 786 __fscache_uncache_all_inode_pages(cookie, inode);
745} 787}
746 788
789/**
790 * fscache_disable_cookie - Disable a cookie
791 * @cookie: The cookie representing the cache object
792 * @invalidate: Invalidate the backing object
793 *
794 * Disable a cookie from accepting further alloc, read, write, invalidate,
795 * update or acquire operations. Outstanding operations can still be waited
796 * upon and pages can still be uncached and the cookie relinquished.
797 *
798 * This will not return until all outstanding operations have completed.
799 *
800 * If @invalidate is set, then the backing object will be invalidated and
801 * detached, otherwise it will just be detached.
802 */
803static inline
804void fscache_disable_cookie(struct fscache_cookie *cookie, bool invalidate)
805{
806 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
807 __fscache_disable_cookie(cookie, invalidate);
808}
809
810/**
811 * fscache_enable_cookie - Reenable a cookie
812 * @cookie: The cookie representing the cache object
813 * @can_enable: A function to permit enablement once lock is held
814 * @data: Data for can_enable()
815 *
816 * Reenable a previously disabled cookie, allowing it to accept further alloc,
817 * read, write, invalidate, update or acquire operations. An attempt will be
818 * made to immediately reattach the cookie to a backing object.
819 *
820 * The can_enable() function is called (if not NULL) once the enablement lock
821 * is held to rule on whether enablement is still permitted to go ahead.
822 */
823static inline
824void fscache_enable_cookie(struct fscache_cookie *cookie,
825 bool (*can_enable)(void *data),
826 void *data)
827{
828 if (fscache_cookie_valid(cookie) && !fscache_cookie_enabled(cookie))
829 __fscache_enable_cookie(cookie, can_enable, data);
830}
831
747#endif /* _LINUX_FSCACHE_H */ 832#endif /* _LINUX_FSCACHE_H */
diff --git a/include/linux/nfs_fs.h b/include/linux/nfs_fs.h
index 3ea4cde8701c..14a48207a304 100644
--- a/include/linux/nfs_fs.h
+++ b/include/linux/nfs_fs.h
@@ -269,9 +269,13 @@ static inline int NFS_STALE(const struct inode *inode)
269 return test_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 269 return test_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
270} 270}
271 271
272static inline int NFS_FSCACHE(const struct inode *inode) 272static inline struct fscache_cookie *nfs_i_fscache(struct inode *inode)
273{ 273{
274 return test_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags); 274#ifdef CONFIG_NFS_FSCACHE
275 return NFS_I(inode)->fscache;
276#else
277 return NULL;
278#endif
275} 279}
276 280
277static inline __u64 NFS_FILEID(const struct inode *inode) 281static inline __u64 NFS_FILEID(const struct inode *inode)