1 /* General filesystem caching interface
3 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
13 * Documentation/filesystems/caching/netfs-api.txt
15 * for a description of the network filesystem interface declared here.
18 #ifndef _LINUX_FSCACHE_H
19 #define _LINUX_FSCACHE_H
22 #include <linux/list.h>
23 #include <linux/pagemap.h>
24 #include <linux/pagevec.h>
26 #if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
27 #define fscache_available() (1)
28 #define fscache_cookie_valid(cookie) (cookie)
30 #define fscache_available() (0)
31 #define fscache_cookie_valid(cookie) (0)
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
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))
45 /* pattern used to fill dead space in an index entry */
46 #define FSCACHE_INDEX_DEADFILL_PATTERN 0x79
49 struct fscache_cache_tag
;
50 struct fscache_cookie
;
53 typedef void (*fscache_rw_complete_t
)(struct page
*page
,
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 */
65 * fscache cookie definition
67 struct fscache_cookie_def
{
68 /* name of cookie type */
73 #define FSCACHE_COOKIE_TYPE_INDEX 0
74 #define FSCACHE_COOKIE_TYPE_DATAFILE 1
76 /* select the cache into which to insert an entry in this index
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
82 struct fscache_cache_tag
*(*select_cache
)(
83 const void *parent_netfs_data
,
84 const void *cookie_netfs_data
);
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
93 uint16_t (*get_key
)(const void *cookie_netfs_data
,
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
103 void (*get_attr
)(const void *cookie_netfs_data
, uint64_t *size
);
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
113 uint16_t (*get_aux
)(const void *cookie_netfs_data
,
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
122 enum fscache_checkaux (*check_aux
)(void *cookie_netfs_data
,
126 /* get an extra reference on a read context
127 * - this function can be absent if the completion function doesn't
130 void (*get_context
)(void *cookie_netfs_data
, void *context
);
132 /* release an extra reference on a read context
133 * - this function can be absent if the completion function doesn't
136 void (*put_context
)(void *cookie_netfs_data
, void *context
);
138 /* indicate pages that now have cache metadata retained
139 * - this function should mark the specified pages as now being cached
140 * - the pages will have been marked with PG_fscache before this is
141 * called, so this is optional
143 void (*mark_pages_cached
)(void *cookie_netfs_data
,
144 struct address_space
*mapping
,
145 struct pagevec
*cached_pvec
);
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
152 * - this is mandatory for any object that may have data
154 void (*now_uncached
)(void *cookie_netfs_data
);
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
162 struct 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 */
170 * slow-path functions for when there is actually caching available, and the
171 * netfs does actually have a valid token
172 * - these are not to be called directly
173 * - these are undefined symbols when FS-Cache is not configured and the
174 * optimiser takes care of not using them
176 extern int __fscache_register_netfs(struct fscache_netfs
*);
177 extern void __fscache_unregister_netfs(struct fscache_netfs
*);
178 extern struct fscache_cache_tag
*__fscache_lookup_cache_tag(const char *);
179 extern void __fscache_release_cache_tag(struct fscache_cache_tag
*);
181 extern struct fscache_cookie
*__fscache_acquire_cookie(
182 struct fscache_cookie
*,
183 const struct fscache_cookie_def
*,
185 extern void __fscache_relinquish_cookie(struct fscache_cookie
*, int);
186 extern void __fscache_update_cookie(struct fscache_cookie
*);
187 extern int __fscache_attr_changed(struct fscache_cookie
*);
188 extern int __fscache_read_or_alloc_page(struct fscache_cookie
*,
190 fscache_rw_complete_t
,
193 extern int __fscache_read_or_alloc_pages(struct fscache_cookie
*,
194 struct address_space
*,
197 fscache_rw_complete_t
,
200 extern int __fscache_alloc_page(struct fscache_cookie
*, struct page
*, gfp_t
);
201 extern int __fscache_write_page(struct fscache_cookie
*, struct page
*, gfp_t
);
202 extern void __fscache_uncache_page(struct fscache_cookie
*, struct page
*);
203 extern bool __fscache_check_page_write(struct fscache_cookie
*, struct page
*);
204 extern void __fscache_wait_on_page_write(struct fscache_cookie
*, struct page
*);
205 extern bool __fscache_maybe_release_page(struct fscache_cookie
*, struct page
*,
207 extern void __fscache_uncache_all_inode_pages(struct fscache_cookie
*,
211 * fscache_register_netfs - Register a filesystem as desiring caching services
212 * @netfs: The description of the filesystem
214 * Register a filesystem as desiring caching services if they're available.
216 * See Documentation/filesystems/caching/netfs-api.txt for a complete
220 int fscache_register_netfs(struct fscache_netfs
*netfs
)
222 if (fscache_available())
223 return __fscache_register_netfs(netfs
);
229 * fscache_unregister_netfs - Indicate that a filesystem no longer desires
231 * @netfs: The description of the filesystem
233 * Indicate that a filesystem no longer desires caching services for the
236 * See Documentation/filesystems/caching/netfs-api.txt for a complete
240 void fscache_unregister_netfs(struct fscache_netfs
*netfs
)
242 if (fscache_available())
243 __fscache_unregister_netfs(netfs
);
247 * fscache_lookup_cache_tag - Look up a cache tag
248 * @name: The name of the tag to search for
250 * Acquire a specific cache referral tag that can be used to select a specific
251 * cache in which to cache an index.
253 * See Documentation/filesystems/caching/netfs-api.txt for a complete
257 struct fscache_cache_tag
*fscache_lookup_cache_tag(const char *name
)
259 if (fscache_available())
260 return __fscache_lookup_cache_tag(name
);
266 * fscache_release_cache_tag - Release a cache tag
267 * @tag: The tag to release
269 * Release a reference to a cache referral tag previously looked up.
271 * See Documentation/filesystems/caching/netfs-api.txt for a complete
275 void fscache_release_cache_tag(struct fscache_cache_tag
*tag
)
277 if (fscache_available())
278 __fscache_release_cache_tag(tag
);
282 * fscache_acquire_cookie - Acquire a cookie to represent a cache object
283 * @parent: The cookie that's to be the parent of this one
284 * @def: A description of the cache object, including callback operations
285 * @netfs_data: An arbitrary piece of data to be kept in the cookie to
286 * represent the cache object to the netfs
288 * This function is used to inform FS-Cache about part of an index hierarchy
289 * that can be used to locate files. This is done by requesting a cookie for
290 * each index in the path to the file.
292 * See Documentation/filesystems/caching/netfs-api.txt for a complete
296 struct fscache_cookie
*fscache_acquire_cookie(
297 struct fscache_cookie
*parent
,
298 const struct fscache_cookie_def
*def
,
301 if (fscache_cookie_valid(parent
))
302 return __fscache_acquire_cookie(parent
, def
, netfs_data
);
308 * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
310 * @cookie: The cookie being returned
311 * @retire: True if the cache object the cookie represents is to be discarded
313 * This function returns a cookie to the cache, forcibly discarding the
314 * associated cache object if retire is set to true.
316 * See Documentation/filesystems/caching/netfs-api.txt for a complete
320 void fscache_relinquish_cookie(struct fscache_cookie
*cookie
, int retire
)
322 if (fscache_cookie_valid(cookie
))
323 __fscache_relinquish_cookie(cookie
, retire
);
327 * fscache_update_cookie - Request that a cache object be updated
328 * @cookie: The cookie representing the cache object
330 * Request an update of the index data for the cache object associated with the
333 * See Documentation/filesystems/caching/netfs-api.txt for a complete
337 void fscache_update_cookie(struct fscache_cookie
*cookie
)
339 if (fscache_cookie_valid(cookie
))
340 __fscache_update_cookie(cookie
);
344 * fscache_pin_cookie - Pin a data-storage cache object in its cache
345 * @cookie: The cookie representing the cache object
347 * Permit data-storage cache objects to be pinned in the cache.
349 * See Documentation/filesystems/caching/netfs-api.txt for a complete
353 int fscache_pin_cookie(struct fscache_cookie
*cookie
)
359 * fscache_pin_cookie - Unpin a data-storage cache object in its cache
360 * @cookie: The cookie representing the cache object
362 * Permit data-storage cache objects to be unpinned from the cache.
364 * See Documentation/filesystems/caching/netfs-api.txt for a complete
368 void fscache_unpin_cookie(struct fscache_cookie
*cookie
)
373 * fscache_attr_changed - Notify cache that an object's attributes changed
374 * @cookie: The cookie representing the cache object
376 * Send a notification to the cache indicating that an object's attributes have
377 * changed. This includes the data size. These attributes will be obtained
378 * through the get_attr() cookie definition op.
380 * See Documentation/filesystems/caching/netfs-api.txt for a complete
384 int fscache_attr_changed(struct fscache_cookie
*cookie
)
386 if (fscache_cookie_valid(cookie
))
387 return __fscache_attr_changed(cookie
);
393 * fscache_reserve_space - Reserve data space for a cached object
394 * @cookie: The cookie representing the cache object
395 * @i_size: The amount of space to be reserved
397 * Reserve an amount of space in the cache for the cache object attached to a
398 * cookie so that a write to that object within the space can always be
401 * See Documentation/filesystems/caching/netfs-api.txt for a complete
405 int fscache_reserve_space(struct fscache_cookie
*cookie
, loff_t size
)
411 * fscache_read_or_alloc_page - Read a page from the cache or allocate a block
412 * in which to store it
413 * @cookie: The cookie representing the cache object
414 * @page: The netfs page to fill if possible
415 * @end_io_func: The callback to invoke when and if the page is filled
416 * @context: An arbitrary piece of data to pass on to end_io_func()
417 * @gfp: The conditions under which memory allocation should be made
419 * Read a page from the cache, or if that's not possible make a potential
420 * one-block reservation in the cache into which the page may be stored once
421 * fetched from the server.
423 * If the page is not backed by the cache object, or if it there's some reason
424 * it can't be, -ENOBUFS will be returned and nothing more will be done for
427 * Else, if that page is backed by the cache, a read will be initiated directly
428 * to the netfs's page and 0 will be returned by this function. The
429 * end_io_func() callback will be invoked when the operation terminates on a
430 * completion or failure. Note that the callback may be invoked before the
433 * Else, if the page is unbacked, -ENODATA is returned and a block may have
434 * been allocated in the cache.
436 * See Documentation/filesystems/caching/netfs-api.txt for a complete
440 int fscache_read_or_alloc_page(struct fscache_cookie
*cookie
,
442 fscache_rw_complete_t end_io_func
,
446 if (fscache_cookie_valid(cookie
))
447 return __fscache_read_or_alloc_page(cookie
, page
, end_io_func
,
454 * fscache_read_or_alloc_pages - Read pages from the cache and/or allocate
455 * blocks in which to store them
456 * @cookie: The cookie representing the cache object
457 * @mapping: The netfs inode mapping to which the pages will be attached
458 * @pages: A list of potential netfs pages to be filled
459 * @nr_pages: Number of pages to be read and/or allocated
460 * @end_io_func: The callback to invoke when and if each page is filled
461 * @context: An arbitrary piece of data to pass on to end_io_func()
462 * @gfp: The conditions under which memory allocation should be made
464 * Read a set of pages from the cache, or if that's not possible, attempt to
465 * make a potential one-block reservation for each page in the cache into which
466 * that page may be stored once fetched from the server.
468 * If some pages are not backed by the cache object, or if it there's some
469 * reason they can't be, -ENOBUFS will be returned and nothing more will be
470 * done for that pages.
472 * Else, if some of the pages are backed by the cache, a read will be initiated
473 * directly to the netfs's page and 0 will be returned by this function. The
474 * end_io_func() callback will be invoked when the operation terminates on a
475 * completion or failure. Note that the callback may be invoked before the
478 * Else, if a page is unbacked, -ENODATA is returned and a block may have
479 * been allocated in the cache.
481 * Because the function may want to return all of -ENOBUFS, -ENODATA and 0 in
482 * regard to different pages, the return values are prioritised in that order.
483 * Any pages submitted for reading are removed from the pages list.
485 * See Documentation/filesystems/caching/netfs-api.txt for a complete
489 int fscache_read_or_alloc_pages(struct fscache_cookie
*cookie
,
490 struct address_space
*mapping
,
491 struct list_head
*pages
,
493 fscache_rw_complete_t end_io_func
,
497 if (fscache_cookie_valid(cookie
))
498 return __fscache_read_or_alloc_pages(cookie
, mapping
, pages
,
499 nr_pages
, end_io_func
,
506 * fscache_alloc_page - Allocate a block in which to store a page
507 * @cookie: The cookie representing the cache object
508 * @page: The netfs page to allocate a page for
509 * @gfp: The conditions under which memory allocation should be made
511 * Request Allocation a block in the cache in which to store a netfs page
512 * without retrieving any contents from the cache.
514 * If the page is not backed by a file then -ENOBUFS will be returned and
515 * nothing more will be done, and no reservation will be made.
517 * Else, a block will be allocated if one wasn't already, and 0 will be
520 * See Documentation/filesystems/caching/netfs-api.txt for a complete
524 int fscache_alloc_page(struct fscache_cookie
*cookie
,
528 if (fscache_cookie_valid(cookie
))
529 return __fscache_alloc_page(cookie
, page
, gfp
);
535 * fscache_write_page - Request storage of a page in the cache
536 * @cookie: The cookie representing the cache object
537 * @page: The netfs page to store
538 * @gfp: The conditions under which memory allocation should be made
540 * Request the contents of the netfs page be written into the cache. This
541 * request may be ignored if no cache block is currently allocated, in which
542 * case it will return -ENOBUFS.
544 * If a cache block was already allocated, a write will be initiated and 0 will
545 * be returned. The PG_fscache_write page bit is set immediately and will then
546 * be cleared at the completion of the write to indicate the success or failure
547 * of the operation. Note that the completion may happen before the return.
549 * See Documentation/filesystems/caching/netfs-api.txt for a complete
553 int fscache_write_page(struct fscache_cookie
*cookie
,
557 if (fscache_cookie_valid(cookie
))
558 return __fscache_write_page(cookie
, page
, gfp
);
564 * fscache_uncache_page - Indicate that caching is no longer required on a page
565 * @cookie: The cookie representing the cache object
566 * @page: The netfs page that was being cached.
568 * Tell the cache that we no longer want a page to be cached and that it should
569 * remove any knowledge of the netfs page it may have.
571 * Note that this cannot cancel any outstanding I/O operations between this
572 * page and the cache.
574 * See Documentation/filesystems/caching/netfs-api.txt for a complete
578 void fscache_uncache_page(struct fscache_cookie
*cookie
,
581 if (fscache_cookie_valid(cookie
))
582 __fscache_uncache_page(cookie
, page
);
586 * fscache_check_page_write - Ask if a page is being writing to the cache
587 * @cookie: The cookie representing the cache object
588 * @page: The netfs page that is being cached.
590 * Ask the cache if a page is being written to the cache.
592 * See Documentation/filesystems/caching/netfs-api.txt for a complete
596 bool fscache_check_page_write(struct fscache_cookie
*cookie
,
599 if (fscache_cookie_valid(cookie
))
600 return __fscache_check_page_write(cookie
, page
);
605 * fscache_wait_on_page_write - Wait for a page to complete writing to the cache
606 * @cookie: The cookie representing the cache object
607 * @page: The netfs page that is being cached.
609 * Ask the cache to wake us up when a page is no longer being written to the
612 * See Documentation/filesystems/caching/netfs-api.txt for a complete
616 void fscache_wait_on_page_write(struct fscache_cookie
*cookie
,
619 if (fscache_cookie_valid(cookie
))
620 __fscache_wait_on_page_write(cookie
, page
);
624 * fscache_maybe_release_page - Consider releasing a page, cancelling a store
625 * @cookie: The cookie representing the cache object
626 * @page: The netfs page that is being cached.
627 * @gfp: The gfp flags passed to releasepage()
629 * Consider releasing a page for the vmscan algorithm, on behalf of the netfs's
630 * releasepage() call. A storage request on the page may cancelled if it is
631 * not currently being processed.
633 * The function returns true if the page no longer has a storage request on it,
634 * and false if a storage request is left in place. If true is returned, the
635 * page will have been passed to fscache_uncache_page(). If false is returned
636 * the page cannot be freed yet.
639 bool fscache_maybe_release_page(struct fscache_cookie
*cookie
,
643 if (fscache_cookie_valid(cookie
) && PageFsCache(page
))
644 return __fscache_maybe_release_page(cookie
, page
, gfp
);
649 * fscache_uncache_all_inode_pages - Uncache all an inode's pages
650 * @cookie: The cookie representing the inode's cache object.
651 * @inode: The inode to uncache pages from.
653 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
654 * to be associated with the given cookie.
656 * This function may sleep. It will wait for pages that are being written out
657 * and will wait whilst the PG_fscache mark is removed by the cache.
660 void fscache_uncache_all_inode_pages(struct fscache_cookie
*cookie
,
663 if (fscache_cookie_valid(cookie
))
664 __fscache_uncache_all_inode_pages(cookie
, inode
);
667 #endif /* _LINUX_FSCACHE_H */