blob: 94a8aae8f9e2be702c15e7b081f9d1b4197421e1 [file] [log] [blame]
David Howells2d6fff62009-04-03 16:42:36 +01001/* 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
48struct pagevec;
49struct fscache_cache_tag;
50struct fscache_cookie;
51struct fscache_netfs;
52
53typedef void (*fscache_rw_complete_t)(struct page *page,
54 void *context,
55 int error);
56
57/* result of index entry consultation */
58enum 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 */
67struct 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
Suresh Jayaramanab0cfb92010-07-06 18:12:39 +053088 * - should return the amount of data stored
David Howells2d6fff62009-04-03 16:42:36 +010089 * - 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300105 /* get the auxiliary data from netfs data
David Howells2d6fff62009-04-03 16:42:36 +0100106 * - this function can be absent if the index carries no state data
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300107 * - should store the auxiliary data in the buffer
David Howells2d6fff62009-04-03 16:42:36 +0100108 * - 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300120 * presented, as is the auxiliary data
David Howells2d6fff62009-04-03 16:42:36 +0100121 */
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
David Howellsc4d6d8d2012-12-20 21:52:32 +0000138 /* 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
David Howells2d6fff62009-04-03 16:42:36 +0100141 * called, so this is optional
142 */
David Howellsc4d6d8d2012-12-20 21:52:32 +0000143 void (*mark_page_cached)(void *cookie_netfs_data,
144 struct address_space *mapping,
145 struct page *page);
David Howells2d6fff62009-04-03 16:42:36 +0100146
147 /* indicate the cookie is no longer cached
148 * - this function is called when the backing store currently caching
149 * a cookie is removed
150 * - the netfs should use this to clean up any markers indicating
151 * cached pages
152 * - this is mandatory for any object that may have data
153 */
154 void (*now_uncached)(void *cookie_netfs_data);
155};
156
157/*
158 * fscache cached network filesystem type
159 * - name, version and ops must be filled in before registration
160 * - all other fields will be set during registration
161 */
162struct fscache_netfs {
163 uint32_t version; /* indexing version */
164 const char *name; /* filesystem name */
165 struct fscache_cookie *primary_index;
166 struct list_head link; /* internal link */
167};
168
169/*
David Howells94d30ae2013-09-21 00:09:31 +0100170 * 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/*
David Howells2d6fff62009-04-03 16:42:36 +0100206 * slow-path functions for when there is actually caching available, and the
207 * netfs does actually have a valid token
208 * - these are not to be called directly
209 * - these are undefined symbols when FS-Cache is not configured and the
210 * optimiser takes care of not using them
211 */
David Howells726dd7f2009-04-03 16:42:38 +0100212extern int __fscache_register_netfs(struct fscache_netfs *);
213extern void __fscache_unregister_netfs(struct fscache_netfs *);
David Howells0e04d4c2009-04-03 16:42:37 +0100214extern struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *);
215extern void __fscache_release_cache_tag(struct fscache_cache_tag *);
David Howells2d6fff62009-04-03 16:42:36 +0100216
David Howellsccc4fc32009-04-03 16:42:38 +0100217extern struct fscache_cookie *__fscache_acquire_cookie(
218 struct fscache_cookie *,
219 const struct fscache_cookie_def *,
David Howells94d30ae2013-09-21 00:09:31 +0100220 void *, bool);
221extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
David Howellsda9803b2013-08-21 17:29:38 -0400222extern int __fscache_check_consistency(struct fscache_cookie *);
David Howellsccc4fc32009-04-03 16:42:38 +0100223extern void __fscache_update_cookie(struct fscache_cookie *);
David Howellsb5108822009-04-03 16:42:39 +0100224extern int __fscache_attr_changed(struct fscache_cookie *);
David Howellsef778e72012-12-20 21:52:36 +0000225extern void __fscache_invalidate(struct fscache_cookie *);
226extern void __fscache_wait_on_invalidate(struct fscache_cookie *);
David Howellsb5108822009-04-03 16:42:39 +0100227extern int __fscache_read_or_alloc_page(struct fscache_cookie *,
228 struct page *,
229 fscache_rw_complete_t,
230 void *,
231 gfp_t);
232extern int __fscache_read_or_alloc_pages(struct fscache_cookie *,
233 struct address_space *,
234 struct list_head *,
235 unsigned *,
236 fscache_rw_complete_t,
237 void *,
238 gfp_t);
239extern int __fscache_alloc_page(struct fscache_cookie *, struct page *, gfp_t);
240extern int __fscache_write_page(struct fscache_cookie *, struct page *, gfp_t);
241extern void __fscache_uncache_page(struct fscache_cookie *, struct page *);
242extern bool __fscache_check_page_write(struct fscache_cookie *, struct page *);
243extern void __fscache_wait_on_page_write(struct fscache_cookie *, struct page *);
David Howells201a1542009-11-19 18:11:35 +0000244extern bool __fscache_maybe_release_page(struct fscache_cookie *, struct page *,
245 gfp_t);
David Howellsc902ce12011-07-07 12:19:48 +0100246extern void __fscache_uncache_all_inode_pages(struct fscache_cookie *,
247 struct inode *);
Milosz Tanski5a6f2822013-08-21 17:30:11 -0400248extern void __fscache_readpages_cancel(struct fscache_cookie *cookie,
249 struct list_head *pages);
David Howells94d30ae2013-09-21 00:09:31 +0100250extern void __fscache_disable_cookie(struct fscache_cookie *, bool);
251extern void __fscache_enable_cookie(struct fscache_cookie *,
252 bool (*)(void *), void *);
David Howellsccc4fc32009-04-03 16:42:38 +0100253
David Howells2d6fff62009-04-03 16:42:36 +0100254/**
255 * fscache_register_netfs - Register a filesystem as desiring caching services
256 * @netfs: The description of the filesystem
257 *
258 * Register a filesystem as desiring caching services if they're available.
259 *
260 * See Documentation/filesystems/caching/netfs-api.txt for a complete
261 * description.
262 */
263static inline
264int fscache_register_netfs(struct fscache_netfs *netfs)
265{
David Howells726dd7f2009-04-03 16:42:38 +0100266 if (fscache_available())
267 return __fscache_register_netfs(netfs);
268 else
269 return 0;
David Howells2d6fff62009-04-03 16:42:36 +0100270}
271
272/**
273 * fscache_unregister_netfs - Indicate that a filesystem no longer desires
274 * caching services
275 * @netfs: The description of the filesystem
276 *
277 * Indicate that a filesystem no longer desires caching services for the
278 * moment.
279 *
280 * See Documentation/filesystems/caching/netfs-api.txt for a complete
281 * description.
282 */
283static inline
284void fscache_unregister_netfs(struct fscache_netfs *netfs)
285{
David Howells726dd7f2009-04-03 16:42:38 +0100286 if (fscache_available())
287 __fscache_unregister_netfs(netfs);
David Howells2d6fff62009-04-03 16:42:36 +0100288}
289
290/**
291 * fscache_lookup_cache_tag - Look up a cache tag
292 * @name: The name of the tag to search for
293 *
294 * Acquire a specific cache referral tag that can be used to select a specific
295 * cache in which to cache an index.
296 *
297 * See Documentation/filesystems/caching/netfs-api.txt for a complete
298 * description.
299 */
300static inline
301struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name)
302{
David Howells0e04d4c2009-04-03 16:42:37 +0100303 if (fscache_available())
304 return __fscache_lookup_cache_tag(name);
305 else
306 return NULL;
David Howells2d6fff62009-04-03 16:42:36 +0100307}
308
309/**
310 * fscache_release_cache_tag - Release a cache tag
311 * @tag: The tag to release
312 *
313 * Release a reference to a cache referral tag previously looked up.
314 *
315 * See Documentation/filesystems/caching/netfs-api.txt for a complete
316 * description.
317 */
318static inline
319void fscache_release_cache_tag(struct fscache_cache_tag *tag)
320{
David Howells0e04d4c2009-04-03 16:42:37 +0100321 if (fscache_available())
322 __fscache_release_cache_tag(tag);
David Howells2d6fff62009-04-03 16:42:36 +0100323}
324
325/**
326 * fscache_acquire_cookie - Acquire a cookie to represent a cache object
327 * @parent: The cookie that's to be the parent of this one
328 * @def: A description of the cache object, including callback operations
329 * @netfs_data: An arbitrary piece of data to be kept in the cookie to
330 * represent the cache object to the netfs
David Howells94d30ae2013-09-21 00:09:31 +0100331 * @enable: Whether or not to enable a data cookie immediately
David Howells2d6fff62009-04-03 16:42:36 +0100332 *
333 * This function is used to inform FS-Cache about part of an index hierarchy
334 * that can be used to locate files. This is done by requesting a cookie for
335 * each index in the path to the file.
336 *
337 * See Documentation/filesystems/caching/netfs-api.txt for a complete
338 * description.
339 */
340static inline
341struct fscache_cookie *fscache_acquire_cookie(
342 struct fscache_cookie *parent,
343 const struct fscache_cookie_def *def,
David Howells94d30ae2013-09-21 00:09:31 +0100344 void *netfs_data,
345 bool enable)
David Howells2d6fff62009-04-03 16:42:36 +0100346{
David Howells94d30ae2013-09-21 00:09:31 +0100347 if (fscache_cookie_valid(parent) && fscache_cookie_enabled(parent))
348 return __fscache_acquire_cookie(parent, def, netfs_data,
349 enable);
David Howellsccc4fc32009-04-03 16:42:38 +0100350 else
351 return NULL;
David Howells2d6fff62009-04-03 16:42:36 +0100352}
353
354/**
355 * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
356 * it
357 * @cookie: The cookie being returned
358 * @retire: True if the cache object the cookie represents is to be discarded
359 *
360 * This function returns a cookie to the cache, forcibly discarding the
361 * associated cache object if retire is set to true.
362 *
363 * See Documentation/filesystems/caching/netfs-api.txt for a complete
364 * description.
365 */
366static inline
David Howells94d30ae2013-09-21 00:09:31 +0100367void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
David Howells2d6fff62009-04-03 16:42:36 +0100368{
David Howellsccc4fc32009-04-03 16:42:38 +0100369 if (fscache_cookie_valid(cookie))
370 __fscache_relinquish_cookie(cookie, retire);
David Howells2d6fff62009-04-03 16:42:36 +0100371}
372
373/**
David Howellsda9803b2013-08-21 17:29:38 -0400374 * fscache_check_consistency - Request that if the cache is updated
375 * @cookie: The cookie representing the cache object
376 *
377 * Request an consistency check from fscache, which passes the request
378 * to the backing cache.
379 *
380 * Returns 0 if consistent and -ESTALE if inconsistent. May also
381 * return -ENOMEM and -ERESTARTSYS.
382 */
383static inline
384int fscache_check_consistency(struct fscache_cookie *cookie)
385{
David Howells94d30ae2013-09-21 00:09:31 +0100386 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
David Howellsda9803b2013-08-21 17:29:38 -0400387 return __fscache_check_consistency(cookie);
388 else
389 return 0;
390}
391
392/**
David Howells2d6fff62009-04-03 16:42:36 +0100393 * fscache_update_cookie - Request that a cache object be updated
394 * @cookie: The cookie representing the cache object
395 *
396 * Request an update of the index data for the cache object associated with the
397 * cookie.
398 *
399 * See Documentation/filesystems/caching/netfs-api.txt for a complete
400 * description.
401 */
402static inline
403void fscache_update_cookie(struct fscache_cookie *cookie)
404{
David Howells94d30ae2013-09-21 00:09:31 +0100405 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
David Howellsccc4fc32009-04-03 16:42:38 +0100406 __fscache_update_cookie(cookie);
David Howells2d6fff62009-04-03 16:42:36 +0100407}
408
409/**
410 * fscache_pin_cookie - Pin a data-storage cache object in its cache
411 * @cookie: The cookie representing the cache object
412 *
413 * Permit data-storage cache objects to be pinned in the cache.
414 *
415 * See Documentation/filesystems/caching/netfs-api.txt for a complete
416 * description.
417 */
418static inline
419int fscache_pin_cookie(struct fscache_cookie *cookie)
420{
421 return -ENOBUFS;
422}
423
424/**
425 * fscache_pin_cookie - Unpin a data-storage cache object in its cache
426 * @cookie: The cookie representing the cache object
427 *
428 * Permit data-storage cache objects to be unpinned from the cache.
429 *
430 * See Documentation/filesystems/caching/netfs-api.txt for a complete
431 * description.
432 */
433static inline
434void fscache_unpin_cookie(struct fscache_cookie *cookie)
435{
436}
437
438/**
439 * fscache_attr_changed - Notify cache that an object's attributes changed
440 * @cookie: The cookie representing the cache object
441 *
442 * Send a notification to the cache indicating that an object's attributes have
443 * changed. This includes the data size. These attributes will be obtained
444 * through the get_attr() cookie definition op.
445 *
446 * See Documentation/filesystems/caching/netfs-api.txt for a complete
447 * description.
448 */
449static inline
450int fscache_attr_changed(struct fscache_cookie *cookie)
451{
David Howells94d30ae2013-09-21 00:09:31 +0100452 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
David Howellsb5108822009-04-03 16:42:39 +0100453 return __fscache_attr_changed(cookie);
454 else
455 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100456}
457
458/**
David Howellsef778e72012-12-20 21:52:36 +0000459 * fscache_invalidate - Notify cache that an object needs invalidation
460 * @cookie: The cookie representing the cache object
461 *
462 * Notify the cache that an object is needs to be invalidated and that it
463 * should abort any retrievals or stores it is doing on the cache. The object
464 * is then marked non-caching until such time as the invalidation is complete.
465 *
466 * This can be called with spinlocks held.
467 *
468 * See Documentation/filesystems/caching/netfs-api.txt for a complete
469 * description.
470 */
471static inline
472void fscache_invalidate(struct fscache_cookie *cookie)
473{
David Howells94d30ae2013-09-21 00:09:31 +0100474 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
David Howellsef778e72012-12-20 21:52:36 +0000475 __fscache_invalidate(cookie);
476}
477
478/**
479 * fscache_wait_on_invalidate - Wait for invalidation to complete
480 * @cookie: The cookie representing the cache object
481 *
482 * Wait for the invalidation of an object to complete.
483 *
484 * See Documentation/filesystems/caching/netfs-api.txt for a complete
485 * description.
486 */
487static inline
488void fscache_wait_on_invalidate(struct fscache_cookie *cookie)
489{
490 if (fscache_cookie_valid(cookie))
491 __fscache_wait_on_invalidate(cookie);
492}
493
494/**
David Howells2d6fff62009-04-03 16:42:36 +0100495 * fscache_reserve_space - Reserve data space for a cached object
496 * @cookie: The cookie representing the cache object
497 * @i_size: The amount of space to be reserved
498 *
499 * Reserve an amount of space in the cache for the cache object attached to a
500 * cookie so that a write to that object within the space can always be
501 * honoured.
502 *
503 * See Documentation/filesystems/caching/netfs-api.txt for a complete
504 * description.
505 */
506static inline
507int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size)
508{
509 return -ENOBUFS;
510}
511
512/**
513 * fscache_read_or_alloc_page - Read a page from the cache or allocate a block
514 * in which to store it
515 * @cookie: The cookie representing the cache object
516 * @page: The netfs page to fill if possible
517 * @end_io_func: The callback to invoke when and if the page is filled
518 * @context: An arbitrary piece of data to pass on to end_io_func()
519 * @gfp: The conditions under which memory allocation should be made
520 *
521 * Read a page from the cache, or if that's not possible make a potential
522 * one-block reservation in the cache into which the page may be stored once
523 * fetched from the server.
524 *
525 * If the page is not backed by the cache object, or if it there's some reason
526 * it can't be, -ENOBUFS will be returned and nothing more will be done for
527 * that page.
528 *
529 * Else, if that page is backed by the cache, a read will be initiated directly
530 * to the netfs's page and 0 will be returned by this function. The
531 * end_io_func() callback will be invoked when the operation terminates on a
532 * completion or failure. Note that the callback may be invoked before the
533 * return.
534 *
535 * Else, if the page is unbacked, -ENODATA is returned and a block may have
536 * been allocated in the cache.
537 *
538 * See Documentation/filesystems/caching/netfs-api.txt for a complete
539 * description.
540 */
541static inline
542int fscache_read_or_alloc_page(struct fscache_cookie *cookie,
543 struct page *page,
544 fscache_rw_complete_t end_io_func,
545 void *context,
546 gfp_t gfp)
547{
David Howells94d30ae2013-09-21 00:09:31 +0100548 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
David Howellsb5108822009-04-03 16:42:39 +0100549 return __fscache_read_or_alloc_page(cookie, page, end_io_func,
550 context, gfp);
551 else
552 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100553}
554
555/**
556 * fscache_read_or_alloc_pages - Read pages from the cache and/or allocate
557 * blocks in which to store them
558 * @cookie: The cookie representing the cache object
559 * @mapping: The netfs inode mapping to which the pages will be attached
560 * @pages: A list of potential netfs pages to be filled
Suresh Jayaraman49a3df82010-07-06 18:29:45 +0530561 * @nr_pages: Number of pages to be read and/or allocated
David Howells2d6fff62009-04-03 16:42:36 +0100562 * @end_io_func: The callback to invoke when and if each page is filled
563 * @context: An arbitrary piece of data to pass on to end_io_func()
564 * @gfp: The conditions under which memory allocation should be made
565 *
566 * Read a set of pages from the cache, or if that's not possible, attempt to
567 * make a potential one-block reservation for each page in the cache into which
568 * that page may be stored once fetched from the server.
569 *
570 * If some pages are not backed by the cache object, or if it there's some
571 * reason they can't be, -ENOBUFS will be returned and nothing more will be
572 * done for that pages.
573 *
574 * Else, if some of the pages are backed by the cache, a read will be initiated
575 * directly to the netfs's page and 0 will be returned by this function. The
576 * end_io_func() callback will be invoked when the operation terminates on a
577 * completion or failure. Note that the callback may be invoked before the
578 * return.
579 *
580 * Else, if a page is unbacked, -ENODATA is returned and a block may have
581 * been allocated in the cache.
582 *
583 * Because the function may want to return all of -ENOBUFS, -ENODATA and 0 in
584 * regard to different pages, the return values are prioritised in that order.
585 * Any pages submitted for reading are removed from the pages list.
586 *
587 * See Documentation/filesystems/caching/netfs-api.txt for a complete
588 * description.
589 */
590static inline
591int fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
592 struct address_space *mapping,
593 struct list_head *pages,
594 unsigned *nr_pages,
595 fscache_rw_complete_t end_io_func,
596 void *context,
597 gfp_t gfp)
598{
David Howells94d30ae2013-09-21 00:09:31 +0100599 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
David Howellsb5108822009-04-03 16:42:39 +0100600 return __fscache_read_or_alloc_pages(cookie, mapping, pages,
601 nr_pages, end_io_func,
602 context, gfp);
603 else
604 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100605}
606
607/**
608 * fscache_alloc_page - Allocate a block in which to store a page
609 * @cookie: The cookie representing the cache object
610 * @page: The netfs page to allocate a page for
611 * @gfp: The conditions under which memory allocation should be made
612 *
613 * Request Allocation a block in the cache in which to store a netfs page
614 * without retrieving any contents from the cache.
615 *
616 * If the page is not backed by a file then -ENOBUFS will be returned and
617 * nothing more will be done, and no reservation will be made.
618 *
619 * Else, a block will be allocated if one wasn't already, and 0 will be
620 * returned
621 *
622 * See Documentation/filesystems/caching/netfs-api.txt for a complete
623 * description.
624 */
625static inline
626int fscache_alloc_page(struct fscache_cookie *cookie,
627 struct page *page,
628 gfp_t gfp)
629{
David Howells94d30ae2013-09-21 00:09:31 +0100630 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
David Howellsb5108822009-04-03 16:42:39 +0100631 return __fscache_alloc_page(cookie, page, gfp);
632 else
633 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100634}
635
636/**
Milosz Tanski5a6f2822013-08-21 17:30:11 -0400637 * fscache_readpages_cancel - Cancel read/alloc on pages
638 * @cookie: The cookie representing the inode's cache object.
639 * @pages: The netfs pages that we canceled write on in readpages()
640 *
641 * Uncache/unreserve the pages reserved earlier in readpages() via
642 * fscache_readpages_or_alloc() and similar. In most successful caches in
643 * readpages() this doesn't do anything. In cases when the underlying netfs's
644 * readahead failed we need to clean up the pagelist (unmark and uncache).
645 *
646 * This function may sleep as it may have to clean up disk state.
647 */
648static inline
649void fscache_readpages_cancel(struct fscache_cookie *cookie,
650 struct list_head *pages)
651{
652 if (fscache_cookie_valid(cookie))
653 __fscache_readpages_cancel(cookie, pages);
654}
655
656/**
David Howells2d6fff62009-04-03 16:42:36 +0100657 * fscache_write_page - Request storage of a page in the cache
658 * @cookie: The cookie representing the cache object
659 * @page: The netfs page to store
660 * @gfp: The conditions under which memory allocation should be made
661 *
662 * Request the contents of the netfs page be written into the cache. This
663 * request may be ignored if no cache block is currently allocated, in which
664 * case it will return -ENOBUFS.
665 *
666 * If a cache block was already allocated, a write will be initiated and 0 will
667 * be returned. The PG_fscache_write page bit is set immediately and will then
668 * be cleared at the completion of the write to indicate the success or failure
669 * of the operation. Note that the completion may happen before the return.
670 *
671 * See Documentation/filesystems/caching/netfs-api.txt for a complete
672 * description.
673 */
674static inline
675int fscache_write_page(struct fscache_cookie *cookie,
676 struct page *page,
677 gfp_t gfp)
678{
David Howells94d30ae2013-09-21 00:09:31 +0100679 if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
David Howellsb5108822009-04-03 16:42:39 +0100680 return __fscache_write_page(cookie, page, gfp);
681 else
682 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100683}
684
685/**
686 * fscache_uncache_page - Indicate that caching is no longer required on a page
687 * @cookie: The cookie representing the cache object
688 * @page: The netfs page that was being cached.
689 *
690 * Tell the cache that we no longer want a page to be cached and that it should
691 * remove any knowledge of the netfs page it may have.
692 *
693 * Note that this cannot cancel any outstanding I/O operations between this
694 * page and the cache.
695 *
696 * See Documentation/filesystems/caching/netfs-api.txt for a complete
697 * description.
698 */
699static inline
700void fscache_uncache_page(struct fscache_cookie *cookie,
701 struct page *page)
702{
David Howellsb5108822009-04-03 16:42:39 +0100703 if (fscache_cookie_valid(cookie))
704 __fscache_uncache_page(cookie, page);
David Howells2d6fff62009-04-03 16:42:36 +0100705}
706
707/**
708 * fscache_check_page_write - Ask if a page is being writing to the cache
709 * @cookie: The cookie representing the cache object
710 * @page: The netfs page that is being cached.
711 *
712 * Ask the cache if a page is being written to the cache.
713 *
714 * See Documentation/filesystems/caching/netfs-api.txt for a complete
715 * description.
716 */
717static inline
718bool fscache_check_page_write(struct fscache_cookie *cookie,
719 struct page *page)
720{
David Howellsb5108822009-04-03 16:42:39 +0100721 if (fscache_cookie_valid(cookie))
722 return __fscache_check_page_write(cookie, page);
David Howells2d6fff62009-04-03 16:42:36 +0100723 return false;
724}
725
726/**
727 * fscache_wait_on_page_write - Wait for a page to complete writing to the cache
728 * @cookie: The cookie representing the cache object
729 * @page: The netfs page that is being cached.
730 *
731 * Ask the cache to wake us up when a page is no longer being written to the
732 * cache.
733 *
734 * See Documentation/filesystems/caching/netfs-api.txt for a complete
735 * description.
736 */
737static inline
738void fscache_wait_on_page_write(struct fscache_cookie *cookie,
739 struct page *page)
740{
David Howellsb5108822009-04-03 16:42:39 +0100741 if (fscache_cookie_valid(cookie))
742 __fscache_wait_on_page_write(cookie, page);
David Howells2d6fff62009-04-03 16:42:36 +0100743}
744
David Howells201a1542009-11-19 18:11:35 +0000745/**
746 * fscache_maybe_release_page - Consider releasing a page, cancelling a store
747 * @cookie: The cookie representing the cache object
748 * @page: The netfs page that is being cached.
749 * @gfp: The gfp flags passed to releasepage()
750 *
751 * Consider releasing a page for the vmscan algorithm, on behalf of the netfs's
752 * releasepage() call. A storage request on the page may cancelled if it is
753 * not currently being processed.
754 *
755 * The function returns true if the page no longer has a storage request on it,
756 * and false if a storage request is left in place. If true is returned, the
757 * page will have been passed to fscache_uncache_page(). If false is returned
758 * the page cannot be freed yet.
759 */
760static inline
761bool fscache_maybe_release_page(struct fscache_cookie *cookie,
762 struct page *page,
763 gfp_t gfp)
764{
765 if (fscache_cookie_valid(cookie) && PageFsCache(page))
766 return __fscache_maybe_release_page(cookie, page, gfp);
David Howells2b9b2002018-01-02 10:02:19 +0000767 return true;
David Howells201a1542009-11-19 18:11:35 +0000768}
769
David Howellsc902ce12011-07-07 12:19:48 +0100770/**
771 * fscache_uncache_all_inode_pages - Uncache all an inode's pages
772 * @cookie: The cookie representing the inode's cache object.
773 * @inode: The inode to uncache pages from.
774 *
775 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
776 * to be associated with the given cookie.
777 *
778 * This function may sleep. It will wait for pages that are being written out
779 * and will wait whilst the PG_fscache mark is removed by the cache.
780 */
781static inline
782void fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
783 struct inode *inode)
784{
785 if (fscache_cookie_valid(cookie))
786 __fscache_uncache_all_inode_pages(cookie, inode);
787}
788
David Howells94d30ae2013-09-21 00:09:31 +0100789/**
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
David Howells2d6fff62009-04-03 16:42:36 +0100832#endif /* _LINUX_FSCACHE_H */