at v3.8 23 kB view raw
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 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 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 /* 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 */ 176extern int __fscache_register_netfs(struct fscache_netfs *); 177extern void __fscache_unregister_netfs(struct fscache_netfs *); 178extern struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *); 179extern void __fscache_release_cache_tag(struct fscache_cache_tag *); 180 181extern 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 *); 187extern int __fscache_attr_changed(struct fscache_cookie *); 188extern void __fscache_invalidate(struct fscache_cookie *); 189extern void __fscache_wait_on_invalidate(struct fscache_cookie *); 190extern int __fscache_read_or_alloc_page(struct fscache_cookie *, 191 struct page *, 192 fscache_rw_complete_t, 193 void *, 194 gfp_t); 195extern int __fscache_read_or_alloc_pages(struct fscache_cookie *, 196 struct address_space *, 197 struct list_head *, 198 unsigned *, 199 fscache_rw_complete_t, 200 void *, 201 gfp_t); 202extern int __fscache_alloc_page(struct fscache_cookie *, struct page *, gfp_t); 203extern int __fscache_write_page(struct fscache_cookie *, struct page *, gfp_t); 204extern void __fscache_uncache_page(struct fscache_cookie *, struct page *); 205extern bool __fscache_check_page_write(struct fscache_cookie *, struct page *); 206extern void __fscache_wait_on_page_write(struct fscache_cookie *, struct page *); 207extern bool __fscache_maybe_release_page(struct fscache_cookie *, struct page *, 208 gfp_t); 209extern void __fscache_uncache_all_inode_pages(struct fscache_cookie *, 210 struct inode *); 211 212/** 213 * fscache_register_netfs - Register a filesystem as desiring caching services 214 * @netfs: The description of the filesystem 215 * 216 * Register a filesystem as desiring caching services if they're available. 217 * 218 * See Documentation/filesystems/caching/netfs-api.txt for a complete 219 * description. 220 */ 221static inline 222int fscache_register_netfs(struct fscache_netfs *netfs) 223{ 224 if (fscache_available()) 225 return __fscache_register_netfs(netfs); 226 else 227 return 0; 228} 229 230/** 231 * fscache_unregister_netfs - Indicate that a filesystem no longer desires 232 * caching services 233 * @netfs: The description of the filesystem 234 * 235 * Indicate that a filesystem no longer desires caching services for the 236 * moment. 237 * 238 * See Documentation/filesystems/caching/netfs-api.txt for a complete 239 * description. 240 */ 241static inline 242void fscache_unregister_netfs(struct fscache_netfs *netfs) 243{ 244 if (fscache_available()) 245 __fscache_unregister_netfs(netfs); 246} 247 248/** 249 * fscache_lookup_cache_tag - Look up a cache tag 250 * @name: The name of the tag to search for 251 * 252 * Acquire a specific cache referral tag that can be used to select a specific 253 * cache in which to cache an index. 254 * 255 * See Documentation/filesystems/caching/netfs-api.txt for a complete 256 * description. 257 */ 258static inline 259struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name) 260{ 261 if (fscache_available()) 262 return __fscache_lookup_cache_tag(name); 263 else 264 return NULL; 265} 266 267/** 268 * fscache_release_cache_tag - Release a cache tag 269 * @tag: The tag to release 270 * 271 * Release a reference to a cache referral tag previously looked up. 272 * 273 * See Documentation/filesystems/caching/netfs-api.txt for a complete 274 * description. 275 */ 276static inline 277void fscache_release_cache_tag(struct fscache_cache_tag *tag) 278{ 279 if (fscache_available()) 280 __fscache_release_cache_tag(tag); 281} 282 283/** 284 * fscache_acquire_cookie - Acquire a cookie to represent a cache object 285 * @parent: The cookie that's to be the parent of this one 286 * @def: A description of the cache object, including callback operations 287 * @netfs_data: An arbitrary piece of data to be kept in the cookie to 288 * represent the cache object to the netfs 289 * 290 * This function is used to inform FS-Cache about part of an index hierarchy 291 * that can be used to locate files. This is done by requesting a cookie for 292 * each index in the path to the file. 293 * 294 * See Documentation/filesystems/caching/netfs-api.txt for a complete 295 * description. 296 */ 297static inline 298struct fscache_cookie *fscache_acquire_cookie( 299 struct fscache_cookie *parent, 300 const struct fscache_cookie_def *def, 301 void *netfs_data) 302{ 303 if (fscache_cookie_valid(parent)) 304 return __fscache_acquire_cookie(parent, def, netfs_data); 305 else 306 return NULL; 307} 308 309/** 310 * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding 311 * it 312 * @cookie: The cookie being returned 313 * @retire: True if the cache object the cookie represents is to be discarded 314 * 315 * This function returns a cookie to the cache, forcibly discarding the 316 * associated cache object if retire is set to true. 317 * 318 * See Documentation/filesystems/caching/netfs-api.txt for a complete 319 * description. 320 */ 321static inline 322void fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire) 323{ 324 if (fscache_cookie_valid(cookie)) 325 __fscache_relinquish_cookie(cookie, retire); 326} 327 328/** 329 * fscache_update_cookie - Request that a cache object be updated 330 * @cookie: The cookie representing the cache object 331 * 332 * Request an update of the index data for the cache object associated with the 333 * cookie. 334 * 335 * See Documentation/filesystems/caching/netfs-api.txt for a complete 336 * description. 337 */ 338static inline 339void fscache_update_cookie(struct fscache_cookie *cookie) 340{ 341 if (fscache_cookie_valid(cookie)) 342 __fscache_update_cookie(cookie); 343} 344 345/** 346 * fscache_pin_cookie - Pin a data-storage cache object in its cache 347 * @cookie: The cookie representing the cache object 348 * 349 * Permit data-storage cache objects to be pinned in the cache. 350 * 351 * See Documentation/filesystems/caching/netfs-api.txt for a complete 352 * description. 353 */ 354static inline 355int fscache_pin_cookie(struct fscache_cookie *cookie) 356{ 357 return -ENOBUFS; 358} 359 360/** 361 * fscache_pin_cookie - Unpin a data-storage cache object in its cache 362 * @cookie: The cookie representing the cache object 363 * 364 * Permit data-storage cache objects to be unpinned from the cache. 365 * 366 * See Documentation/filesystems/caching/netfs-api.txt for a complete 367 * description. 368 */ 369static inline 370void fscache_unpin_cookie(struct fscache_cookie *cookie) 371{ 372} 373 374/** 375 * fscache_attr_changed - Notify cache that an object's attributes changed 376 * @cookie: The cookie representing the cache object 377 * 378 * Send a notification to the cache indicating that an object's attributes have 379 * changed. This includes the data size. These attributes will be obtained 380 * through the get_attr() cookie definition op. 381 * 382 * See Documentation/filesystems/caching/netfs-api.txt for a complete 383 * description. 384 */ 385static inline 386int fscache_attr_changed(struct fscache_cookie *cookie) 387{ 388 if (fscache_cookie_valid(cookie)) 389 return __fscache_attr_changed(cookie); 390 else 391 return -ENOBUFS; 392} 393 394/** 395 * fscache_invalidate - Notify cache that an object needs invalidation 396 * @cookie: The cookie representing the cache object 397 * 398 * Notify the cache that an object is needs to be invalidated and that it 399 * should abort any retrievals or stores it is doing on the cache. The object 400 * is then marked non-caching until such time as the invalidation is complete. 401 * 402 * This can be called with spinlocks held. 403 * 404 * See Documentation/filesystems/caching/netfs-api.txt for a complete 405 * description. 406 */ 407static inline 408void fscache_invalidate(struct fscache_cookie *cookie) 409{ 410 if (fscache_cookie_valid(cookie)) 411 __fscache_invalidate(cookie); 412} 413 414/** 415 * fscache_wait_on_invalidate - Wait for invalidation to complete 416 * @cookie: The cookie representing the cache object 417 * 418 * Wait for the invalidation of an object to complete. 419 * 420 * See Documentation/filesystems/caching/netfs-api.txt for a complete 421 * description. 422 */ 423static inline 424void fscache_wait_on_invalidate(struct fscache_cookie *cookie) 425{ 426 if (fscache_cookie_valid(cookie)) 427 __fscache_wait_on_invalidate(cookie); 428} 429 430/** 431 * fscache_reserve_space - Reserve data space for a cached object 432 * @cookie: The cookie representing the cache object 433 * @i_size: The amount of space to be reserved 434 * 435 * Reserve an amount of space in the cache for the cache object attached to a 436 * cookie so that a write to that object within the space can always be 437 * honoured. 438 * 439 * See Documentation/filesystems/caching/netfs-api.txt for a complete 440 * description. 441 */ 442static inline 443int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size) 444{ 445 return -ENOBUFS; 446} 447 448/** 449 * fscache_read_or_alloc_page - Read a page from the cache or allocate a block 450 * in which to store it 451 * @cookie: The cookie representing the cache object 452 * @page: The netfs page to fill if possible 453 * @end_io_func: The callback to invoke when and if the page is filled 454 * @context: An arbitrary piece of data to pass on to end_io_func() 455 * @gfp: The conditions under which memory allocation should be made 456 * 457 * Read a page from the cache, or if that's not possible make a potential 458 * one-block reservation in the cache into which the page may be stored once 459 * fetched from the server. 460 * 461 * If the page is not backed by the cache object, or if it there's some reason 462 * it can't be, -ENOBUFS will be returned and nothing more will be done for 463 * that page. 464 * 465 * Else, if that page is backed by the cache, a read will be initiated directly 466 * to the netfs's page and 0 will be returned by this function. The 467 * end_io_func() callback will be invoked when the operation terminates on a 468 * completion or failure. Note that the callback may be invoked before the 469 * return. 470 * 471 * Else, if the page is unbacked, -ENODATA is returned and a block may have 472 * been allocated in the cache. 473 * 474 * See Documentation/filesystems/caching/netfs-api.txt for a complete 475 * description. 476 */ 477static inline 478int fscache_read_or_alloc_page(struct fscache_cookie *cookie, 479 struct page *page, 480 fscache_rw_complete_t end_io_func, 481 void *context, 482 gfp_t gfp) 483{ 484 if (fscache_cookie_valid(cookie)) 485 return __fscache_read_or_alloc_page(cookie, page, end_io_func, 486 context, gfp); 487 else 488 return -ENOBUFS; 489} 490 491/** 492 * fscache_read_or_alloc_pages - Read pages from the cache and/or allocate 493 * blocks in which to store them 494 * @cookie: The cookie representing the cache object 495 * @mapping: The netfs inode mapping to which the pages will be attached 496 * @pages: A list of potential netfs pages to be filled 497 * @nr_pages: Number of pages to be read and/or allocated 498 * @end_io_func: The callback to invoke when and if each page is filled 499 * @context: An arbitrary piece of data to pass on to end_io_func() 500 * @gfp: The conditions under which memory allocation should be made 501 * 502 * Read a set of pages from the cache, or if that's not possible, attempt to 503 * make a potential one-block reservation for each page in the cache into which 504 * that page may be stored once fetched from the server. 505 * 506 * If some pages are not backed by the cache object, or if it there's some 507 * reason they can't be, -ENOBUFS will be returned and nothing more will be 508 * done for that pages. 509 * 510 * Else, if some of the pages are backed by the cache, a read will be initiated 511 * directly to the netfs's page and 0 will be returned by this function. The 512 * end_io_func() callback will be invoked when the operation terminates on a 513 * completion or failure. Note that the callback may be invoked before the 514 * return. 515 * 516 * Else, if a page is unbacked, -ENODATA is returned and a block may have 517 * been allocated in the cache. 518 * 519 * Because the function may want to return all of -ENOBUFS, -ENODATA and 0 in 520 * regard to different pages, the return values are prioritised in that order. 521 * Any pages submitted for reading are removed from the pages list. 522 * 523 * See Documentation/filesystems/caching/netfs-api.txt for a complete 524 * description. 525 */ 526static inline 527int fscache_read_or_alloc_pages(struct fscache_cookie *cookie, 528 struct address_space *mapping, 529 struct list_head *pages, 530 unsigned *nr_pages, 531 fscache_rw_complete_t end_io_func, 532 void *context, 533 gfp_t gfp) 534{ 535 if (fscache_cookie_valid(cookie)) 536 return __fscache_read_or_alloc_pages(cookie, mapping, pages, 537 nr_pages, end_io_func, 538 context, gfp); 539 else 540 return -ENOBUFS; 541} 542 543/** 544 * fscache_alloc_page - Allocate a block in which to store a page 545 * @cookie: The cookie representing the cache object 546 * @page: The netfs page to allocate a page for 547 * @gfp: The conditions under which memory allocation should be made 548 * 549 * Request Allocation a block in the cache in which to store a netfs page 550 * without retrieving any contents from the cache. 551 * 552 * If the page is not backed by a file then -ENOBUFS will be returned and 553 * nothing more will be done, and no reservation will be made. 554 * 555 * Else, a block will be allocated if one wasn't already, and 0 will be 556 * returned 557 * 558 * See Documentation/filesystems/caching/netfs-api.txt for a complete 559 * description. 560 */ 561static inline 562int fscache_alloc_page(struct fscache_cookie *cookie, 563 struct page *page, 564 gfp_t gfp) 565{ 566 if (fscache_cookie_valid(cookie)) 567 return __fscache_alloc_page(cookie, page, gfp); 568 else 569 return -ENOBUFS; 570} 571 572/** 573 * fscache_write_page - Request storage of a page in the cache 574 * @cookie: The cookie representing the cache object 575 * @page: The netfs page to store 576 * @gfp: The conditions under which memory allocation should be made 577 * 578 * Request the contents of the netfs page be written into the cache. This 579 * request may be ignored if no cache block is currently allocated, in which 580 * case it will return -ENOBUFS. 581 * 582 * If a cache block was already allocated, a write will be initiated and 0 will 583 * be returned. The PG_fscache_write page bit is set immediately and will then 584 * be cleared at the completion of the write to indicate the success or failure 585 * of the operation. Note that the completion may happen before the return. 586 * 587 * See Documentation/filesystems/caching/netfs-api.txt for a complete 588 * description. 589 */ 590static inline 591int fscache_write_page(struct fscache_cookie *cookie, 592 struct page *page, 593 gfp_t gfp) 594{ 595 if (fscache_cookie_valid(cookie)) 596 return __fscache_write_page(cookie, page, gfp); 597 else 598 return -ENOBUFS; 599} 600 601/** 602 * fscache_uncache_page - Indicate that caching is no longer required on a page 603 * @cookie: The cookie representing the cache object 604 * @page: The netfs page that was being cached. 605 * 606 * Tell the cache that we no longer want a page to be cached and that it should 607 * remove any knowledge of the netfs page it may have. 608 * 609 * Note that this cannot cancel any outstanding I/O operations between this 610 * page and the cache. 611 * 612 * See Documentation/filesystems/caching/netfs-api.txt for a complete 613 * description. 614 */ 615static inline 616void fscache_uncache_page(struct fscache_cookie *cookie, 617 struct page *page) 618{ 619 if (fscache_cookie_valid(cookie)) 620 __fscache_uncache_page(cookie, page); 621} 622 623/** 624 * fscache_check_page_write - Ask if a page is being writing to the cache 625 * @cookie: The cookie representing the cache object 626 * @page: The netfs page that is being cached. 627 * 628 * Ask the cache if a page is being written to the cache. 629 * 630 * See Documentation/filesystems/caching/netfs-api.txt for a complete 631 * description. 632 */ 633static inline 634bool fscache_check_page_write(struct fscache_cookie *cookie, 635 struct page *page) 636{ 637 if (fscache_cookie_valid(cookie)) 638 return __fscache_check_page_write(cookie, page); 639 return false; 640} 641 642/** 643 * fscache_wait_on_page_write - Wait for a page to complete writing to the cache 644 * @cookie: The cookie representing the cache object 645 * @page: The netfs page that is being cached. 646 * 647 * Ask the cache to wake us up when a page is no longer being written to the 648 * cache. 649 * 650 * See Documentation/filesystems/caching/netfs-api.txt for a complete 651 * description. 652 */ 653static inline 654void fscache_wait_on_page_write(struct fscache_cookie *cookie, 655 struct page *page) 656{ 657 if (fscache_cookie_valid(cookie)) 658 __fscache_wait_on_page_write(cookie, page); 659} 660 661/** 662 * fscache_maybe_release_page - Consider releasing a page, cancelling a store 663 * @cookie: The cookie representing the cache object 664 * @page: The netfs page that is being cached. 665 * @gfp: The gfp flags passed to releasepage() 666 * 667 * Consider releasing a page for the vmscan algorithm, on behalf of the netfs's 668 * releasepage() call. A storage request on the page may cancelled if it is 669 * not currently being processed. 670 * 671 * The function returns true if the page no longer has a storage request on it, 672 * and false if a storage request is left in place. If true is returned, the 673 * page will have been passed to fscache_uncache_page(). If false is returned 674 * the page cannot be freed yet. 675 */ 676static inline 677bool fscache_maybe_release_page(struct fscache_cookie *cookie, 678 struct page *page, 679 gfp_t gfp) 680{ 681 if (fscache_cookie_valid(cookie) && PageFsCache(page)) 682 return __fscache_maybe_release_page(cookie, page, gfp); 683 return false; 684} 685 686/** 687 * fscache_uncache_all_inode_pages - Uncache all an inode's pages 688 * @cookie: The cookie representing the inode's cache object. 689 * @inode: The inode to uncache pages from. 690 * 691 * Uncache all the pages in an inode that are marked PG_fscache, assuming them 692 * to be associated with the given cookie. 693 * 694 * This function may sleep. It will wait for pages that are being written out 695 * and will wait whilst the PG_fscache mark is removed by the cache. 696 */ 697static inline 698void fscache_uncache_all_inode_pages(struct fscache_cookie *cookie, 699 struct inode *inode) 700{ 701 if (fscache_cookie_valid(cookie)) 702 __fscache_uncache_all_inode_pages(cookie, inode); 703} 704 705#endif /* _LINUX_FSCACHE_H */