Merge tag 'for-4.15-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux
[sfrench/cifs-2.6.git] / include / linux / fscache.h
1 /* General filesystem caching interface
2  *
3  * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  *
11  * NOTE!!! See:
12  *
13  *      Documentation/filesystems/caching/netfs-api.txt
14  *
15  * for a description of the network filesystem interface declared here.
16  */
17
18 #ifndef _LINUX_FSCACHE_H
19 #define _LINUX_FSCACHE_H
20
21 #include <linux/fs.h>
22 #include <linux/list.h>
23 #include <linux/pagemap.h>
24 #include <linux/pagevec.h>
25
26 #if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
27 #define fscache_available() (1)
28 #define fscache_cookie_valid(cookie) (cookie)
29 #else
30 #define fscache_available() (0)
31 #define fscache_cookie_valid(cookie) (0)
32 #endif
33
34
35 /*
36  * overload PG_private_2 to give us PG_fscache - this is used to indicate that
37  * a page is currently backed by a local disk cache
38  */
39 #define PageFsCache(page)               PagePrivate2((page))
40 #define SetPageFsCache(page)            SetPagePrivate2((page))
41 #define ClearPageFsCache(page)          ClearPagePrivate2((page))
42 #define TestSetPageFsCache(page)        TestSetPagePrivate2((page))
43 #define TestClearPageFsCache(page)      TestClearPagePrivate2((page))
44
45 /* pattern used to fill dead space in an index entry */
46 #define FSCACHE_INDEX_DEADFILL_PATTERN 0x79
47
48 struct pagevec;
49 struct fscache_cache_tag;
50 struct fscache_cookie;
51 struct fscache_netfs;
52
53 typedef void (*fscache_rw_complete_t)(struct page *page,
54                                       void *context,
55                                       int error);
56
57 /* result of index entry consultation */
58 enum fscache_checkaux {
59         FSCACHE_CHECKAUX_OKAY,          /* entry okay as is */
60         FSCACHE_CHECKAUX_NEEDS_UPDATE,  /* entry requires update */
61         FSCACHE_CHECKAUX_OBSOLETE,      /* entry requires deletion */
62 };
63
64 /*
65  * fscache cookie definition
66  */
67 struct fscache_cookie_def {
68         /* name of cookie type */
69         char name[16];
70
71         /* cookie type */
72         uint8_t type;
73 #define FSCACHE_COOKIE_TYPE_INDEX       0
74 #define FSCACHE_COOKIE_TYPE_DATAFILE    1
75
76         /* select the cache into which to insert an entry in this index
77          * - optional
78          * - should return a cache identifier or NULL to cause the cache to be
79          *   inherited from the parent if possible or the first cache picked
80          *   for a non-index file if not
81          */
82         struct fscache_cache_tag *(*select_cache)(
83                 const void *parent_netfs_data,
84                 const void *cookie_netfs_data);
85
86         /* get an index key
87          * - should store the key data in the buffer
88          * - should return the amount of data stored
89          * - not permitted to return an error
90          * - the netfs data from the cookie being used as the source is
91          *   presented
92          */
93         uint16_t (*get_key)(const void *cookie_netfs_data,
94                             void *buffer,
95                             uint16_t bufmax);
96
97         /* get certain file attributes from the netfs data
98          * - this function can be absent for an index
99          * - not permitted to return an error
100          * - the netfs data from the cookie being used as the source is
101          *   presented
102          */
103         void (*get_attr)(const void *cookie_netfs_data, uint64_t *size);
104
105         /* get the auxiliary data from netfs data
106          * - this function can be absent if the index carries no state data
107          * - should store the auxiliary data in the buffer
108          * - should return the amount of amount stored
109          * - not permitted to return an error
110          * - the netfs data from the cookie being used as the source is
111          *   presented
112          */
113         uint16_t (*get_aux)(const void *cookie_netfs_data,
114                             void *buffer,
115                             uint16_t bufmax);
116
117         /* consult the netfs about the state of an object
118          * - this function can be absent if the index carries no state data
119          * - the netfs data from the cookie being used as the target is
120          *   presented, as is the auxiliary data
121          */
122         enum fscache_checkaux (*check_aux)(void *cookie_netfs_data,
123                                            const void *data,
124                                            uint16_t datalen);
125
126         /* get an extra reference on a read context
127          * - this function can be absent if the completion function doesn't
128          *   require a context
129          */
130         void (*get_context)(void *cookie_netfs_data, void *context);
131
132         /* release an extra reference on a read context
133          * - this function can be absent if the completion function doesn't
134          *   require a context
135          */
136         void (*put_context)(void *cookie_netfs_data, void *context);
137
138         /* indicate page that now have cache metadata retained
139          * - this function should mark the specified page as now being cached
140          * - the page will have been marked with PG_fscache before this is
141          *   called, so this is optional
142          */
143         void (*mark_page_cached)(void *cookie_netfs_data,
144                                  struct address_space *mapping,
145                                  struct page *page);
146 };
147
148 /*
149  * fscache cached network filesystem type
150  * - name, version and ops must be filled in before registration
151  * - all other fields will be set during registration
152  */
153 struct fscache_netfs {
154         uint32_t                        version;        /* indexing version */
155         const char                      *name;          /* filesystem name */
156         struct fscache_cookie           *primary_index;
157         struct list_head                link;           /* internal link */
158 };
159
160 /*
161  * data file or index object cookie
162  * - a file will only appear in one cache
163  * - a request to cache a file may or may not be honoured, subject to
164  *   constraints such as disk space
165  * - indices are created on disk just-in-time
166  */
167 struct fscache_cookie {
168         atomic_t                        usage;          /* number of users of this cookie */
169         atomic_t                        n_children;     /* number of children of this cookie */
170         atomic_t                        n_active;       /* number of active users of netfs ptrs */
171         spinlock_t                      lock;
172         spinlock_t                      stores_lock;    /* lock on page store tree */
173         struct hlist_head               backing_objects; /* object(s) backing this file/index */
174         const struct fscache_cookie_def *def;           /* definition */
175         struct fscache_cookie           *parent;        /* parent of this entry */
176         void                            *netfs_data;    /* back pointer to netfs */
177         struct radix_tree_root          stores;         /* pages to be stored on this cookie */
178 #define FSCACHE_COOKIE_PENDING_TAG      0               /* pages tag: pending write to cache */
179 #define FSCACHE_COOKIE_STORING_TAG      1               /* pages tag: writing to cache */
180
181         unsigned long                   flags;
182 #define FSCACHE_COOKIE_LOOKING_UP       0       /* T if non-index cookie being looked up still */
183 #define FSCACHE_COOKIE_NO_DATA_YET      1       /* T if new object with no cached data yet */
184 #define FSCACHE_COOKIE_UNAVAILABLE      2       /* T if cookie is unavailable (error, etc) */
185 #define FSCACHE_COOKIE_INVALIDATING     3       /* T if cookie is being invalidated */
186 #define FSCACHE_COOKIE_RELINQUISHED     4       /* T if cookie has been relinquished */
187 #define FSCACHE_COOKIE_ENABLED          5       /* T if cookie is enabled */
188 #define FSCACHE_COOKIE_ENABLEMENT_LOCK  6       /* T if cookie is being en/disabled */
189 };
190
191 static inline bool fscache_cookie_enabled(struct fscache_cookie *cookie)
192 {
193         return test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
194 }
195
196 /*
197  * slow-path functions for when there is actually caching available, and the
198  * netfs does actually have a valid token
199  * - these are not to be called directly
200  * - these are undefined symbols when FS-Cache is not configured and the
201  *   optimiser takes care of not using them
202  */
203 extern int __fscache_register_netfs(struct fscache_netfs *);
204 extern void __fscache_unregister_netfs(struct fscache_netfs *);
205 extern struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *);
206 extern void __fscache_release_cache_tag(struct fscache_cache_tag *);
207
208 extern struct fscache_cookie *__fscache_acquire_cookie(
209         struct fscache_cookie *,
210         const struct fscache_cookie_def *,
211         void *, bool);
212 extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
213 extern int __fscache_check_consistency(struct fscache_cookie *);
214 extern void __fscache_update_cookie(struct fscache_cookie *);
215 extern int __fscache_attr_changed(struct fscache_cookie *);
216 extern void __fscache_invalidate(struct fscache_cookie *);
217 extern void __fscache_wait_on_invalidate(struct fscache_cookie *);
218 extern int __fscache_read_or_alloc_page(struct fscache_cookie *,
219                                         struct page *,
220                                         fscache_rw_complete_t,
221                                         void *,
222                                         gfp_t);
223 extern int __fscache_read_or_alloc_pages(struct fscache_cookie *,
224                                          struct address_space *,
225                                          struct list_head *,
226                                          unsigned *,
227                                          fscache_rw_complete_t,
228                                          void *,
229                                          gfp_t);
230 extern int __fscache_alloc_page(struct fscache_cookie *, struct page *, gfp_t);
231 extern int __fscache_write_page(struct fscache_cookie *, struct page *, gfp_t);
232 extern void __fscache_uncache_page(struct fscache_cookie *, struct page *);
233 extern bool __fscache_check_page_write(struct fscache_cookie *, struct page *);
234 extern void __fscache_wait_on_page_write(struct fscache_cookie *, struct page *);
235 extern bool __fscache_maybe_release_page(struct fscache_cookie *, struct page *,
236                                          gfp_t);
237 extern void __fscache_uncache_all_inode_pages(struct fscache_cookie *,
238                                               struct inode *);
239 extern void __fscache_readpages_cancel(struct fscache_cookie *cookie,
240                                        struct list_head *pages);
241 extern void __fscache_disable_cookie(struct fscache_cookie *, bool);
242 extern void __fscache_enable_cookie(struct fscache_cookie *,
243                                     bool (*)(void *), void *);
244
245 /**
246  * fscache_register_netfs - Register a filesystem as desiring caching services
247  * @netfs: The description of the filesystem
248  *
249  * Register a filesystem as desiring caching services if they're available.
250  *
251  * See Documentation/filesystems/caching/netfs-api.txt for a complete
252  * description.
253  */
254 static inline
255 int fscache_register_netfs(struct fscache_netfs *netfs)
256 {
257         if (fscache_available())
258                 return __fscache_register_netfs(netfs);
259         else
260                 return 0;
261 }
262
263 /**
264  * fscache_unregister_netfs - Indicate that a filesystem no longer desires
265  * caching services
266  * @netfs: The description of the filesystem
267  *
268  * Indicate that a filesystem no longer desires caching services for the
269  * moment.
270  *
271  * See Documentation/filesystems/caching/netfs-api.txt for a complete
272  * description.
273  */
274 static inline
275 void fscache_unregister_netfs(struct fscache_netfs *netfs)
276 {
277         if (fscache_available())
278                 __fscache_unregister_netfs(netfs);
279 }
280
281 /**
282  * fscache_lookup_cache_tag - Look up a cache tag
283  * @name: The name of the tag to search for
284  *
285  * Acquire a specific cache referral tag that can be used to select a specific
286  * cache in which to cache an index.
287  *
288  * See Documentation/filesystems/caching/netfs-api.txt for a complete
289  * description.
290  */
291 static inline
292 struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name)
293 {
294         if (fscache_available())
295                 return __fscache_lookup_cache_tag(name);
296         else
297                 return NULL;
298 }
299
300 /**
301  * fscache_release_cache_tag - Release a cache tag
302  * @tag: The tag to release
303  *
304  * Release a reference to a cache referral tag previously looked up.
305  *
306  * See Documentation/filesystems/caching/netfs-api.txt for a complete
307  * description.
308  */
309 static inline
310 void fscache_release_cache_tag(struct fscache_cache_tag *tag)
311 {
312         if (fscache_available())
313                 __fscache_release_cache_tag(tag);
314 }
315
316 /**
317  * fscache_acquire_cookie - Acquire a cookie to represent a cache object
318  * @parent: The cookie that's to be the parent of this one
319  * @def: A description of the cache object, including callback operations
320  * @netfs_data: An arbitrary piece of data to be kept in the cookie to
321  * represent the cache object to the netfs
322  * @enable: Whether or not to enable a data cookie immediately
323  *
324  * This function is used to inform FS-Cache about part of an index hierarchy
325  * that can be used to locate files.  This is done by requesting a cookie for
326  * each index in the path to the file.
327  *
328  * See Documentation/filesystems/caching/netfs-api.txt for a complete
329  * description.
330  */
331 static inline
332 struct fscache_cookie *fscache_acquire_cookie(
333         struct fscache_cookie *parent,
334         const struct fscache_cookie_def *def,
335         void *netfs_data,
336         bool enable)
337 {
338         if (fscache_cookie_valid(parent) && fscache_cookie_enabled(parent))
339                 return __fscache_acquire_cookie(parent, def, netfs_data,
340                                                 enable);
341         else
342                 return NULL;
343 }
344
345 /**
346  * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
347  * it
348  * @cookie: The cookie being returned
349  * @retire: True if the cache object the cookie represents is to be discarded
350  *
351  * This function returns a cookie to the cache, forcibly discarding the
352  * associated cache object if retire is set to true.
353  *
354  * See Documentation/filesystems/caching/netfs-api.txt for a complete
355  * description.
356  */
357 static inline
358 void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
359 {
360         if (fscache_cookie_valid(cookie))
361                 __fscache_relinquish_cookie(cookie, retire);
362 }
363
364 /**
365  * fscache_check_consistency - Request that if the cache is updated
366  * @cookie: The cookie representing the cache object
367  *
368  * Request an consistency check from fscache, which passes the request
369  * to the backing cache.
370  *
371  * Returns 0 if consistent and -ESTALE if inconsistent.  May also
372  * return -ENOMEM and -ERESTARTSYS.
373  */
374 static inline
375 int fscache_check_consistency(struct fscache_cookie *cookie)
376 {
377         if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
378                 return __fscache_check_consistency(cookie);
379         else
380                 return 0;
381 }
382
383 /**
384  * fscache_update_cookie - Request that a cache object be updated
385  * @cookie: The cookie representing the cache object
386  *
387  * Request an update of the index data for the cache object associated with the
388  * cookie.
389  *
390  * See Documentation/filesystems/caching/netfs-api.txt for a complete
391  * description.
392  */
393 static inline
394 void fscache_update_cookie(struct fscache_cookie *cookie)
395 {
396         if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
397                 __fscache_update_cookie(cookie);
398 }
399
400 /**
401  * fscache_pin_cookie - Pin a data-storage cache object in its cache
402  * @cookie: The cookie representing the cache object
403  *
404  * Permit data-storage cache objects to be pinned in the cache.
405  *
406  * See Documentation/filesystems/caching/netfs-api.txt for a complete
407  * description.
408  */
409 static inline
410 int fscache_pin_cookie(struct fscache_cookie *cookie)
411 {
412         return -ENOBUFS;
413 }
414
415 /**
416  * fscache_pin_cookie - Unpin a data-storage cache object in its cache
417  * @cookie: The cookie representing the cache object
418  *
419  * Permit data-storage cache objects to be unpinned from the cache.
420  *
421  * See Documentation/filesystems/caching/netfs-api.txt for a complete
422  * description.
423  */
424 static inline
425 void fscache_unpin_cookie(struct fscache_cookie *cookie)
426 {
427 }
428
429 /**
430  * fscache_attr_changed - Notify cache that an object's attributes changed
431  * @cookie: The cookie representing the cache object
432  *
433  * Send a notification to the cache indicating that an object's attributes have
434  * changed.  This includes the data size.  These attributes will be obtained
435  * through the get_attr() cookie definition op.
436  *
437  * See Documentation/filesystems/caching/netfs-api.txt for a complete
438  * description.
439  */
440 static inline
441 int fscache_attr_changed(struct fscache_cookie *cookie)
442 {
443         if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
444                 return __fscache_attr_changed(cookie);
445         else
446                 return -ENOBUFS;
447 }
448
449 /**
450  * fscache_invalidate - Notify cache that an object needs invalidation
451  * @cookie: The cookie representing the cache object
452  *
453  * Notify the cache that an object is needs to be invalidated and that it
454  * should abort any retrievals or stores it is doing on the cache.  The object
455  * is then marked non-caching until such time as the invalidation is complete.
456  *
457  * This can be called with spinlocks held.
458  *
459  * See Documentation/filesystems/caching/netfs-api.txt for a complete
460  * description.
461  */
462 static inline
463 void fscache_invalidate(struct fscache_cookie *cookie)
464 {
465         if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
466                 __fscache_invalidate(cookie);
467 }
468
469 /**
470  * fscache_wait_on_invalidate - Wait for invalidation to complete
471  * @cookie: The cookie representing the cache object
472  *
473  * Wait for the invalidation of an object to complete.
474  *
475  * See Documentation/filesystems/caching/netfs-api.txt for a complete
476  * description.
477  */
478 static inline
479 void fscache_wait_on_invalidate(struct fscache_cookie *cookie)
480 {
481         if (fscache_cookie_valid(cookie))
482                 __fscache_wait_on_invalidate(cookie);
483 }
484
485 /**
486  * fscache_reserve_space - Reserve data space for a cached object
487  * @cookie: The cookie representing the cache object
488  * @i_size: The amount of space to be reserved
489  *
490  * Reserve an amount of space in the cache for the cache object attached to a
491  * cookie so that a write to that object within the space can always be
492  * honoured.
493  *
494  * See Documentation/filesystems/caching/netfs-api.txt for a complete
495  * description.
496  */
497 static inline
498 int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size)
499 {
500         return -ENOBUFS;
501 }
502
503 /**
504  * fscache_read_or_alloc_page - Read a page from the cache or allocate a block
505  * in which to store it
506  * @cookie: The cookie representing the cache object
507  * @page: The netfs page to fill if possible
508  * @end_io_func: The callback to invoke when and if the page is filled
509  * @context: An arbitrary piece of data to pass on to end_io_func()
510  * @gfp: The conditions under which memory allocation should be made
511  *
512  * Read a page from the cache, or if that's not possible make a potential
513  * one-block reservation in the cache into which the page may be stored once
514  * fetched from the server.
515  *
516  * If the page is not backed by the cache object, or if it there's some reason
517  * it can't be, -ENOBUFS will be returned and nothing more will be done for
518  * that page.
519  *
520  * Else, if that page is backed by the cache, a read will be initiated directly
521  * to the netfs's page and 0 will be returned by this function.  The
522  * end_io_func() callback will be invoked when the operation terminates on a
523  * completion or failure.  Note that the callback may be invoked before the
524  * return.
525  *
526  * Else, if the page is unbacked, -ENODATA is returned and a block may have
527  * been allocated in the cache.
528  *
529  * See Documentation/filesystems/caching/netfs-api.txt for a complete
530  * description.
531  */
532 static inline
533 int fscache_read_or_alloc_page(struct fscache_cookie *cookie,
534                                struct page *page,
535                                fscache_rw_complete_t end_io_func,
536                                void *context,
537                                gfp_t gfp)
538 {
539         if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
540                 return __fscache_read_or_alloc_page(cookie, page, end_io_func,
541                                                     context, gfp);
542         else
543                 return -ENOBUFS;
544 }
545
546 /**
547  * fscache_read_or_alloc_pages - Read pages from the cache and/or allocate
548  * blocks in which to store them
549  * @cookie: The cookie representing the cache object
550  * @mapping: The netfs inode mapping to which the pages will be attached
551  * @pages: A list of potential netfs pages to be filled
552  * @nr_pages: Number of pages to be read and/or allocated
553  * @end_io_func: The callback to invoke when and if each page is filled
554  * @context: An arbitrary piece of data to pass on to end_io_func()
555  * @gfp: The conditions under which memory allocation should be made
556  *
557  * Read a set of pages from the cache, or if that's not possible, attempt to
558  * make a potential one-block reservation for each page in the cache into which
559  * that page may be stored once fetched from the server.
560  *
561  * If some pages are not backed by the cache object, or if it there's some
562  * reason they can't be, -ENOBUFS will be returned and nothing more will be
563  * done for that pages.
564  *
565  * Else, if some of the pages are backed by the cache, a read will be initiated
566  * directly to the netfs's page and 0 will be returned by this function.  The
567  * end_io_func() callback will be invoked when the operation terminates on a
568  * completion or failure.  Note that the callback may be invoked before the
569  * return.
570  *
571  * Else, if a page is unbacked, -ENODATA is returned and a block may have
572  * been allocated in the cache.
573  *
574  * Because the function may want to return all of -ENOBUFS, -ENODATA and 0 in
575  * regard to different pages, the return values are prioritised in that order.
576  * Any pages submitted for reading are removed from the pages list.
577  *
578  * See Documentation/filesystems/caching/netfs-api.txt for a complete
579  * description.
580  */
581 static inline
582 int fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
583                                 struct address_space *mapping,
584                                 struct list_head *pages,
585                                 unsigned *nr_pages,
586                                 fscache_rw_complete_t end_io_func,
587                                 void *context,
588                                 gfp_t gfp)
589 {
590         if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
591                 return __fscache_read_or_alloc_pages(cookie, mapping, pages,
592                                                      nr_pages, end_io_func,
593                                                      context, gfp);
594         else
595                 return -ENOBUFS;
596 }
597
598 /**
599  * fscache_alloc_page - Allocate a block in which to store a page
600  * @cookie: The cookie representing the cache object
601  * @page: The netfs page to allocate a page for
602  * @gfp: The conditions under which memory allocation should be made
603  *
604  * Request Allocation a block in the cache in which to store a netfs page
605  * without retrieving any contents from the cache.
606  *
607  * If the page is not backed by a file then -ENOBUFS will be returned and
608  * nothing more will be done, and no reservation will be made.
609  *
610  * Else, a block will be allocated if one wasn't already, and 0 will be
611  * returned
612  *
613  * See Documentation/filesystems/caching/netfs-api.txt for a complete
614  * description.
615  */
616 static inline
617 int fscache_alloc_page(struct fscache_cookie *cookie,
618                        struct page *page,
619                        gfp_t gfp)
620 {
621         if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
622                 return __fscache_alloc_page(cookie, page, gfp);
623         else
624                 return -ENOBUFS;
625 }
626
627 /**
628  * fscache_readpages_cancel - Cancel read/alloc on pages
629  * @cookie: The cookie representing the inode's cache object.
630  * @pages: The netfs pages that we canceled write on in readpages()
631  *
632  * Uncache/unreserve the pages reserved earlier in readpages() via
633  * fscache_readpages_or_alloc() and similar.  In most successful caches in
634  * readpages() this doesn't do anything.  In cases when the underlying netfs's
635  * readahead failed we need to clean up the pagelist (unmark and uncache).
636  *
637  * This function may sleep as it may have to clean up disk state.
638  */
639 static inline
640 void fscache_readpages_cancel(struct fscache_cookie *cookie,
641                               struct list_head *pages)
642 {
643         if (fscache_cookie_valid(cookie))
644                 __fscache_readpages_cancel(cookie, pages);
645 }
646
647 /**
648  * fscache_write_page - Request storage of a page in the cache
649  * @cookie: The cookie representing the cache object
650  * @page: The netfs page to store
651  * @gfp: The conditions under which memory allocation should be made
652  *
653  * Request the contents of the netfs page be written into the cache.  This
654  * request may be ignored if no cache block is currently allocated, in which
655  * case it will return -ENOBUFS.
656  *
657  * If a cache block was already allocated, a write will be initiated and 0 will
658  * be returned.  The PG_fscache_write page bit is set immediately and will then
659  * be cleared at the completion of the write to indicate the success or failure
660  * of the operation.  Note that the completion may happen before the return.
661  *
662  * See Documentation/filesystems/caching/netfs-api.txt for a complete
663  * description.
664  */
665 static inline
666 int fscache_write_page(struct fscache_cookie *cookie,
667                        struct page *page,
668                        gfp_t gfp)
669 {
670         if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
671                 return __fscache_write_page(cookie, page, gfp);
672         else
673                 return -ENOBUFS;
674 }
675
676 /**
677  * fscache_uncache_page - Indicate that caching is no longer required on a page
678  * @cookie: The cookie representing the cache object
679  * @page: The netfs page that was being cached.
680  *
681  * Tell the cache that we no longer want a page to be cached and that it should
682  * remove any knowledge of the netfs page it may have.
683  *
684  * Note that this cannot cancel any outstanding I/O operations between this
685  * page and the cache.
686  *
687  * See Documentation/filesystems/caching/netfs-api.txt for a complete
688  * description.
689  */
690 static inline
691 void fscache_uncache_page(struct fscache_cookie *cookie,
692                           struct page *page)
693 {
694         if (fscache_cookie_valid(cookie))
695                 __fscache_uncache_page(cookie, page);
696 }
697
698 /**
699  * fscache_check_page_write - Ask if a page is being writing to the cache
700  * @cookie: The cookie representing the cache object
701  * @page: The netfs page that is being cached.
702  *
703  * Ask the cache if a page is being written to the cache.
704  *
705  * See Documentation/filesystems/caching/netfs-api.txt for a complete
706  * description.
707  */
708 static inline
709 bool fscache_check_page_write(struct fscache_cookie *cookie,
710                               struct page *page)
711 {
712         if (fscache_cookie_valid(cookie))
713                 return __fscache_check_page_write(cookie, page);
714         return false;
715 }
716
717 /**
718  * fscache_wait_on_page_write - Wait for a page to complete writing to the cache
719  * @cookie: The cookie representing the cache object
720  * @page: The netfs page that is being cached.
721  *
722  * Ask the cache to wake us up when a page is no longer being written to the
723  * cache.
724  *
725  * See Documentation/filesystems/caching/netfs-api.txt for a complete
726  * description.
727  */
728 static inline
729 void fscache_wait_on_page_write(struct fscache_cookie *cookie,
730                                 struct page *page)
731 {
732         if (fscache_cookie_valid(cookie))
733                 __fscache_wait_on_page_write(cookie, page);
734 }
735
736 /**
737  * fscache_maybe_release_page - Consider releasing a page, cancelling a store
738  * @cookie: The cookie representing the cache object
739  * @page: The netfs page that is being cached.
740  * @gfp: The gfp flags passed to releasepage()
741  *
742  * Consider releasing a page for the vmscan algorithm, on behalf of the netfs's
743  * releasepage() call.  A storage request on the page may cancelled if it is
744  * not currently being processed.
745  *
746  * The function returns true if the page no longer has a storage request on it,
747  * and false if a storage request is left in place.  If true is returned, the
748  * page will have been passed to fscache_uncache_page().  If false is returned
749  * the page cannot be freed yet.
750  */
751 static inline
752 bool fscache_maybe_release_page(struct fscache_cookie *cookie,
753                                 struct page *page,
754                                 gfp_t gfp)
755 {
756         if (fscache_cookie_valid(cookie) && PageFsCache(page))
757                 return __fscache_maybe_release_page(cookie, page, gfp);
758         return true;
759 }
760
761 /**
762  * fscache_uncache_all_inode_pages - Uncache all an inode's pages
763  * @cookie: The cookie representing the inode's cache object.
764  * @inode: The inode to uncache pages from.
765  *
766  * Uncache all the pages in an inode that are marked PG_fscache, assuming them
767  * to be associated with the given cookie.
768  *
769  * This function may sleep.  It will wait for pages that are being written out
770  * and will wait whilst the PG_fscache mark is removed by the cache.
771  */
772 static inline
773 void fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
774                                      struct inode *inode)
775 {
776         if (fscache_cookie_valid(cookie))
777                 __fscache_uncache_all_inode_pages(cookie, inode);
778 }
779
780 /**
781  * fscache_disable_cookie - Disable a cookie
782  * @cookie: The cookie representing the cache object
783  * @invalidate: Invalidate the backing object
784  *
785  * Disable a cookie from accepting further alloc, read, write, invalidate,
786  * update or acquire operations.  Outstanding operations can still be waited
787  * upon and pages can still be uncached and the cookie relinquished.
788  *
789  * This will not return until all outstanding operations have completed.
790  *
791  * If @invalidate is set, then the backing object will be invalidated and
792  * detached, otherwise it will just be detached.
793  */
794 static inline
795 void fscache_disable_cookie(struct fscache_cookie *cookie, bool invalidate)
796 {
797         if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
798                 __fscache_disable_cookie(cookie, invalidate);
799 }
800
801 /**
802  * fscache_enable_cookie - Reenable a cookie
803  * @cookie: The cookie representing the cache object
804  * @can_enable: A function to permit enablement once lock is held
805  * @data: Data for can_enable()
806  *
807  * Reenable a previously disabled cookie, allowing it to accept further alloc,
808  * read, write, invalidate, update or acquire operations.  An attempt will be
809  * made to immediately reattach the cookie to a backing object.
810  *
811  * The can_enable() function is called (if not NULL) once the enablement lock
812  * is held to rule on whether enablement is still permitted to go ahead.
813  */
814 static inline
815 void fscache_enable_cookie(struct fscache_cookie *cookie,
816                            bool (*can_enable)(void *data),
817                            void *data)
818 {
819         if (fscache_cookie_valid(cookie) && !fscache_cookie_enabled(cookie))
820                 __fscache_enable_cookie(cookie, can_enable, data);
821 }
822
823 #endif /* _LINUX_FSCACHE_H */