blob: 595ce49288b7807f0f42c40a6c2dcde9ed354d36 [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
88 * - should return the amount of amount 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 auxilliary data from netfs data
106 * - this function can be absent if the index carries no state data
107 * - should store the auxilliary 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 auxilliary 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 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
142 */
143 void (*mark_pages_cached)(void *cookie_netfs_data,
144 struct address_space *mapping,
145 struct pagevec *cached_pvec);
146
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/*
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
175 */
David Howells726dd7f2009-04-03 16:42:38 +0100176extern int __fscache_register_netfs(struct fscache_netfs *);
177extern void __fscache_unregister_netfs(struct fscache_netfs *);
David Howells0e04d4c2009-04-03 16:42:37 +0100178extern struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *);
179extern void __fscache_release_cache_tag(struct fscache_cache_tag *);
David Howells2d6fff62009-04-03 16:42:36 +0100180
David Howellsccc4fc32009-04-03 16:42:38 +0100181extern struct fscache_cookie *__fscache_acquire_cookie(
182 struct fscache_cookie *,
183 const struct fscache_cookie_def *,
184 void *);
185extern void __fscache_relinquish_cookie(struct fscache_cookie *, int);
186extern void __fscache_update_cookie(struct fscache_cookie *);
David Howellsb5108822009-04-03 16:42:39 +0100187extern int __fscache_attr_changed(struct fscache_cookie *);
188extern int __fscache_read_or_alloc_page(struct fscache_cookie *,
189 struct page *,
190 fscache_rw_complete_t,
191 void *,
192 gfp_t);
193extern int __fscache_read_or_alloc_pages(struct fscache_cookie *,
194 struct address_space *,
195 struct list_head *,
196 unsigned *,
197 fscache_rw_complete_t,
198 void *,
199 gfp_t);
200extern int __fscache_alloc_page(struct fscache_cookie *, struct page *, gfp_t);
201extern int __fscache_write_page(struct fscache_cookie *, struct page *, gfp_t);
202extern void __fscache_uncache_page(struct fscache_cookie *, struct page *);
203extern bool __fscache_check_page_write(struct fscache_cookie *, struct page *);
204extern void __fscache_wait_on_page_write(struct fscache_cookie *, struct page *);
David Howells201a1542009-11-19 18:11:35 +0000205extern bool __fscache_maybe_release_page(struct fscache_cookie *, struct page *,
206 gfp_t);
David Howellsccc4fc32009-04-03 16:42:38 +0100207
David Howells2d6fff62009-04-03 16:42:36 +0100208/**
209 * fscache_register_netfs - Register a filesystem as desiring caching services
210 * @netfs: The description of the filesystem
211 *
212 * Register a filesystem as desiring caching services if they're available.
213 *
214 * See Documentation/filesystems/caching/netfs-api.txt for a complete
215 * description.
216 */
217static inline
218int fscache_register_netfs(struct fscache_netfs *netfs)
219{
David Howells726dd7f2009-04-03 16:42:38 +0100220 if (fscache_available())
221 return __fscache_register_netfs(netfs);
222 else
223 return 0;
David Howells2d6fff62009-04-03 16:42:36 +0100224}
225
226/**
227 * fscache_unregister_netfs - Indicate that a filesystem no longer desires
228 * caching services
229 * @netfs: The description of the filesystem
230 *
231 * Indicate that a filesystem no longer desires caching services for the
232 * moment.
233 *
234 * See Documentation/filesystems/caching/netfs-api.txt for a complete
235 * description.
236 */
237static inline
238void fscache_unregister_netfs(struct fscache_netfs *netfs)
239{
David Howells726dd7f2009-04-03 16:42:38 +0100240 if (fscache_available())
241 __fscache_unregister_netfs(netfs);
David Howells2d6fff62009-04-03 16:42:36 +0100242}
243
244/**
245 * fscache_lookup_cache_tag - Look up a cache tag
246 * @name: The name of the tag to search for
247 *
248 * Acquire a specific cache referral tag that can be used to select a specific
249 * cache in which to cache an index.
250 *
251 * See Documentation/filesystems/caching/netfs-api.txt for a complete
252 * description.
253 */
254static inline
255struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name)
256{
David Howells0e04d4c2009-04-03 16:42:37 +0100257 if (fscache_available())
258 return __fscache_lookup_cache_tag(name);
259 else
260 return NULL;
David Howells2d6fff62009-04-03 16:42:36 +0100261}
262
263/**
264 * fscache_release_cache_tag - Release a cache tag
265 * @tag: The tag to release
266 *
267 * Release a reference to a cache referral tag previously looked up.
268 *
269 * See Documentation/filesystems/caching/netfs-api.txt for a complete
270 * description.
271 */
272static inline
273void fscache_release_cache_tag(struct fscache_cache_tag *tag)
274{
David Howells0e04d4c2009-04-03 16:42:37 +0100275 if (fscache_available())
276 __fscache_release_cache_tag(tag);
David Howells2d6fff62009-04-03 16:42:36 +0100277}
278
279/**
280 * fscache_acquire_cookie - Acquire a cookie to represent a cache object
281 * @parent: The cookie that's to be the parent of this one
282 * @def: A description of the cache object, including callback operations
283 * @netfs_data: An arbitrary piece of data to be kept in the cookie to
284 * represent the cache object to the netfs
285 *
286 * This function is used to inform FS-Cache about part of an index hierarchy
287 * that can be used to locate files. This is done by requesting a cookie for
288 * each index in the path to the file.
289 *
290 * See Documentation/filesystems/caching/netfs-api.txt for a complete
291 * description.
292 */
293static inline
294struct fscache_cookie *fscache_acquire_cookie(
295 struct fscache_cookie *parent,
296 const struct fscache_cookie_def *def,
297 void *netfs_data)
298{
David Howellsccc4fc32009-04-03 16:42:38 +0100299 if (fscache_cookie_valid(parent))
300 return __fscache_acquire_cookie(parent, def, netfs_data);
301 else
302 return NULL;
David Howells2d6fff62009-04-03 16:42:36 +0100303}
304
305/**
306 * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
307 * it
308 * @cookie: The cookie being returned
309 * @retire: True if the cache object the cookie represents is to be discarded
310 *
311 * This function returns a cookie to the cache, forcibly discarding the
312 * associated cache object if retire is set to true.
313 *
314 * See Documentation/filesystems/caching/netfs-api.txt for a complete
315 * description.
316 */
317static inline
318void fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
319{
David Howellsccc4fc32009-04-03 16:42:38 +0100320 if (fscache_cookie_valid(cookie))
321 __fscache_relinquish_cookie(cookie, retire);
David Howells2d6fff62009-04-03 16:42:36 +0100322}
323
324/**
325 * fscache_update_cookie - Request that a cache object be updated
326 * @cookie: The cookie representing the cache object
327 *
328 * Request an update of the index data for the cache object associated with the
329 * cookie.
330 *
331 * See Documentation/filesystems/caching/netfs-api.txt for a complete
332 * description.
333 */
334static inline
335void fscache_update_cookie(struct fscache_cookie *cookie)
336{
David Howellsccc4fc32009-04-03 16:42:38 +0100337 if (fscache_cookie_valid(cookie))
338 __fscache_update_cookie(cookie);
David Howells2d6fff62009-04-03 16:42:36 +0100339}
340
341/**
342 * fscache_pin_cookie - Pin a data-storage cache object in its cache
343 * @cookie: The cookie representing the cache object
344 *
345 * Permit data-storage cache objects to be pinned in the cache.
346 *
347 * See Documentation/filesystems/caching/netfs-api.txt for a complete
348 * description.
349 */
350static inline
351int fscache_pin_cookie(struct fscache_cookie *cookie)
352{
353 return -ENOBUFS;
354}
355
356/**
357 * fscache_pin_cookie - Unpin a data-storage cache object in its cache
358 * @cookie: The cookie representing the cache object
359 *
360 * Permit data-storage cache objects to be unpinned from the cache.
361 *
362 * See Documentation/filesystems/caching/netfs-api.txt for a complete
363 * description.
364 */
365static inline
366void fscache_unpin_cookie(struct fscache_cookie *cookie)
367{
368}
369
370/**
371 * fscache_attr_changed - Notify cache that an object's attributes changed
372 * @cookie: The cookie representing the cache object
373 *
374 * Send a notification to the cache indicating that an object's attributes have
375 * changed. This includes the data size. These attributes will be obtained
376 * through the get_attr() cookie definition op.
377 *
378 * See Documentation/filesystems/caching/netfs-api.txt for a complete
379 * description.
380 */
381static inline
382int fscache_attr_changed(struct fscache_cookie *cookie)
383{
David Howellsb5108822009-04-03 16:42:39 +0100384 if (fscache_cookie_valid(cookie))
385 return __fscache_attr_changed(cookie);
386 else
387 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100388}
389
390/**
391 * fscache_reserve_space - Reserve data space for a cached object
392 * @cookie: The cookie representing the cache object
393 * @i_size: The amount of space to be reserved
394 *
395 * Reserve an amount of space in the cache for the cache object attached to a
396 * cookie so that a write to that object within the space can always be
397 * honoured.
398 *
399 * See Documentation/filesystems/caching/netfs-api.txt for a complete
400 * description.
401 */
402static inline
403int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size)
404{
405 return -ENOBUFS;
406}
407
408/**
409 * fscache_read_or_alloc_page - Read a page from the cache or allocate a block
410 * in which to store it
411 * @cookie: The cookie representing the cache object
412 * @page: The netfs page to fill if possible
413 * @end_io_func: The callback to invoke when and if the page is filled
414 * @context: An arbitrary piece of data to pass on to end_io_func()
415 * @gfp: The conditions under which memory allocation should be made
416 *
417 * Read a page from the cache, or if that's not possible make a potential
418 * one-block reservation in the cache into which the page may be stored once
419 * fetched from the server.
420 *
421 * If the page is not backed by the cache object, or if it there's some reason
422 * it can't be, -ENOBUFS will be returned and nothing more will be done for
423 * that page.
424 *
425 * Else, if that page is backed by the cache, a read will be initiated directly
426 * to the netfs's page and 0 will be returned by this function. The
427 * end_io_func() callback will be invoked when the operation terminates on a
428 * completion or failure. Note that the callback may be invoked before the
429 * return.
430 *
431 * Else, if the page is unbacked, -ENODATA is returned and a block may have
432 * been allocated in the cache.
433 *
434 * See Documentation/filesystems/caching/netfs-api.txt for a complete
435 * description.
436 */
437static inline
438int fscache_read_or_alloc_page(struct fscache_cookie *cookie,
439 struct page *page,
440 fscache_rw_complete_t end_io_func,
441 void *context,
442 gfp_t gfp)
443{
David Howellsb5108822009-04-03 16:42:39 +0100444 if (fscache_cookie_valid(cookie))
445 return __fscache_read_or_alloc_page(cookie, page, end_io_func,
446 context, gfp);
447 else
448 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100449}
450
451/**
452 * fscache_read_or_alloc_pages - Read pages from the cache and/or allocate
453 * blocks in which to store them
454 * @cookie: The cookie representing the cache object
455 * @mapping: The netfs inode mapping to which the pages will be attached
456 * @pages: A list of potential netfs pages to be filled
457 * @end_io_func: The callback to invoke when and if each page is filled
458 * @context: An arbitrary piece of data to pass on to end_io_func()
459 * @gfp: The conditions under which memory allocation should be made
460 *
461 * Read a set of pages from the cache, or if that's not possible, attempt to
462 * make a potential one-block reservation for each page in the cache into which
463 * that page may be stored once fetched from the server.
464 *
465 * If some pages are not backed by the cache object, or if it there's some
466 * reason they can't be, -ENOBUFS will be returned and nothing more will be
467 * done for that pages.
468 *
469 * Else, if some of the pages are backed by the cache, a read will be initiated
470 * directly to the netfs's page and 0 will be returned by this function. The
471 * end_io_func() callback will be invoked when the operation terminates on a
472 * completion or failure. Note that the callback may be invoked before the
473 * return.
474 *
475 * Else, if a page is unbacked, -ENODATA is returned and a block may have
476 * been allocated in the cache.
477 *
478 * Because the function may want to return all of -ENOBUFS, -ENODATA and 0 in
479 * regard to different pages, the return values are prioritised in that order.
480 * Any pages submitted for reading are removed from the pages list.
481 *
482 * See Documentation/filesystems/caching/netfs-api.txt for a complete
483 * description.
484 */
485static inline
486int fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
487 struct address_space *mapping,
488 struct list_head *pages,
489 unsigned *nr_pages,
490 fscache_rw_complete_t end_io_func,
491 void *context,
492 gfp_t gfp)
493{
David Howellsb5108822009-04-03 16:42:39 +0100494 if (fscache_cookie_valid(cookie))
495 return __fscache_read_or_alloc_pages(cookie, mapping, pages,
496 nr_pages, end_io_func,
497 context, gfp);
498 else
499 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100500}
501
502/**
503 * fscache_alloc_page - Allocate a block in which to store a page
504 * @cookie: The cookie representing the cache object
505 * @page: The netfs page to allocate a page for
506 * @gfp: The conditions under which memory allocation should be made
507 *
508 * Request Allocation a block in the cache in which to store a netfs page
509 * without retrieving any contents from the cache.
510 *
511 * If the page is not backed by a file then -ENOBUFS will be returned and
512 * nothing more will be done, and no reservation will be made.
513 *
514 * Else, a block will be allocated if one wasn't already, and 0 will be
515 * returned
516 *
517 * See Documentation/filesystems/caching/netfs-api.txt for a complete
518 * description.
519 */
520static inline
521int fscache_alloc_page(struct fscache_cookie *cookie,
522 struct page *page,
523 gfp_t gfp)
524{
David Howellsb5108822009-04-03 16:42:39 +0100525 if (fscache_cookie_valid(cookie))
526 return __fscache_alloc_page(cookie, page, gfp);
527 else
528 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100529}
530
531/**
532 * fscache_write_page - Request storage of a page in the cache
533 * @cookie: The cookie representing the cache object
534 * @page: The netfs page to store
535 * @gfp: The conditions under which memory allocation should be made
536 *
537 * Request the contents of the netfs page be written into the cache. This
538 * request may be ignored if no cache block is currently allocated, in which
539 * case it will return -ENOBUFS.
540 *
541 * If a cache block was already allocated, a write will be initiated and 0 will
542 * be returned. The PG_fscache_write page bit is set immediately and will then
543 * be cleared at the completion of the write to indicate the success or failure
544 * of the operation. Note that the completion may happen before the return.
545 *
546 * See Documentation/filesystems/caching/netfs-api.txt for a complete
547 * description.
548 */
549static inline
550int fscache_write_page(struct fscache_cookie *cookie,
551 struct page *page,
552 gfp_t gfp)
553{
David Howellsb5108822009-04-03 16:42:39 +0100554 if (fscache_cookie_valid(cookie))
555 return __fscache_write_page(cookie, page, gfp);
556 else
557 return -ENOBUFS;
David Howells2d6fff62009-04-03 16:42:36 +0100558}
559
560/**
561 * fscache_uncache_page - Indicate that caching is no longer required on a page
562 * @cookie: The cookie representing the cache object
563 * @page: The netfs page that was being cached.
564 *
565 * Tell the cache that we no longer want a page to be cached and that it should
566 * remove any knowledge of the netfs page it may have.
567 *
568 * Note that this cannot cancel any outstanding I/O operations between this
569 * page and the cache.
570 *
571 * See Documentation/filesystems/caching/netfs-api.txt for a complete
572 * description.
573 */
574static inline
575void fscache_uncache_page(struct fscache_cookie *cookie,
576 struct page *page)
577{
David Howellsb5108822009-04-03 16:42:39 +0100578 if (fscache_cookie_valid(cookie))
579 __fscache_uncache_page(cookie, page);
David Howells2d6fff62009-04-03 16:42:36 +0100580}
581
582/**
583 * fscache_check_page_write - Ask if a page is being writing to the cache
584 * @cookie: The cookie representing the cache object
585 * @page: The netfs page that is being cached.
586 *
587 * Ask the cache if a page is being written to the cache.
588 *
589 * See Documentation/filesystems/caching/netfs-api.txt for a complete
590 * description.
591 */
592static inline
593bool fscache_check_page_write(struct fscache_cookie *cookie,
594 struct page *page)
595{
David Howellsb5108822009-04-03 16:42:39 +0100596 if (fscache_cookie_valid(cookie))
597 return __fscache_check_page_write(cookie, page);
David Howells2d6fff62009-04-03 16:42:36 +0100598 return false;
599}
600
601/**
602 * fscache_wait_on_page_write - Wait for a page to complete writing to the cache
603 * @cookie: The cookie representing the cache object
604 * @page: The netfs page that is being cached.
605 *
606 * Ask the cache to wake us up when a page is no longer being written to the
607 * cache.
608 *
609 * See Documentation/filesystems/caching/netfs-api.txt for a complete
610 * description.
611 */
612static inline
613void fscache_wait_on_page_write(struct fscache_cookie *cookie,
614 struct page *page)
615{
David Howellsb5108822009-04-03 16:42:39 +0100616 if (fscache_cookie_valid(cookie))
617 __fscache_wait_on_page_write(cookie, page);
David Howells2d6fff62009-04-03 16:42:36 +0100618}
619
David Howells201a1542009-11-19 18:11:35 +0000620/**
621 * fscache_maybe_release_page - Consider releasing a page, cancelling a store
622 * @cookie: The cookie representing the cache object
623 * @page: The netfs page that is being cached.
624 * @gfp: The gfp flags passed to releasepage()
625 *
626 * Consider releasing a page for the vmscan algorithm, on behalf of the netfs's
627 * releasepage() call. A storage request on the page may cancelled if it is
628 * not currently being processed.
629 *
630 * The function returns true if the page no longer has a storage request on it,
631 * and false if a storage request is left in place. If true is returned, the
632 * page will have been passed to fscache_uncache_page(). If false is returned
633 * the page cannot be freed yet.
634 */
635static inline
636bool fscache_maybe_release_page(struct fscache_cookie *cookie,
637 struct page *page,
638 gfp_t gfp)
639{
640 if (fscache_cookie_valid(cookie) && PageFsCache(page))
641 return __fscache_maybe_release_page(cookie, page, gfp);
642 return false;
643}
644
David Howells2d6fff62009-04-03 16:42:36 +0100645#endif /* _LINUX_FSCACHE_H */