1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun=============================== 4*4882a593SmuzhiyunFS-Cache Network Filesystem API 5*4882a593Smuzhiyun=============================== 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunThere's an API by which a network filesystem can make use of the FS-Cache 8*4882a593Smuzhiyunfacilities. This is based around a number of principles: 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun (1) Caches can store a number of different object types. There are two main 11*4882a593Smuzhiyun object types: indices and files. The first is a special type used by 12*4882a593Smuzhiyun FS-Cache to make finding objects faster and to make retiring of groups of 13*4882a593Smuzhiyun objects easier. 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun (2) Every index, file or other object is represented by a cookie. This cookie 16*4882a593Smuzhiyun may or may not have anything associated with it, but the netfs doesn't 17*4882a593Smuzhiyun need to care. 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun (3) Barring the top-level index (one entry per cached netfs), the index 20*4882a593Smuzhiyun hierarchy for each netfs is structured according the whim of the netfs. 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunThis API is declared in <linux/fscache.h>. 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun.. This document contains the following sections: 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun (1) Network filesystem definition 27*4882a593Smuzhiyun (2) Index definition 28*4882a593Smuzhiyun (3) Object definition 29*4882a593Smuzhiyun (4) Network filesystem (un)registration 30*4882a593Smuzhiyun (5) Cache tag lookup 31*4882a593Smuzhiyun (6) Index registration 32*4882a593Smuzhiyun (7) Data file registration 33*4882a593Smuzhiyun (8) Miscellaneous object registration 34*4882a593Smuzhiyun (9) Setting the data file size 35*4882a593Smuzhiyun (10) Page alloc/read/write 36*4882a593Smuzhiyun (11) Page uncaching 37*4882a593Smuzhiyun (12) Index and data file consistency 38*4882a593Smuzhiyun (13) Cookie enablement 39*4882a593Smuzhiyun (14) Miscellaneous cookie operations 40*4882a593Smuzhiyun (15) Cookie unregistration 41*4882a593Smuzhiyun (16) Index invalidation 42*4882a593Smuzhiyun (17) Data file invalidation 43*4882a593Smuzhiyun (18) FS-Cache specific page flags. 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun 46*4882a593SmuzhiyunNetwork Filesystem Definition 47*4882a593Smuzhiyun============================= 48*4882a593Smuzhiyun 49*4882a593SmuzhiyunFS-Cache needs a description of the network filesystem. This is specified 50*4882a593Smuzhiyunusing a record of the following structure:: 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun struct fscache_netfs { 53*4882a593Smuzhiyun uint32_t version; 54*4882a593Smuzhiyun const char *name; 55*4882a593Smuzhiyun struct fscache_cookie *primary_index; 56*4882a593Smuzhiyun ... 57*4882a593Smuzhiyun }; 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunThis first two fields should be filled in before registration, and the third 60*4882a593Smuzhiyunwill be filled in by the registration function; any other fields should just be 61*4882a593Smuzhiyunignored and are for internal use only. 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunThe fields are: 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun (1) The name of the netfs (used as the key in the toplevel index). 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun (2) The version of the netfs (if the name matches but the version doesn't, the 68*4882a593Smuzhiyun entire in-cache hierarchy for this netfs will be scrapped and begun 69*4882a593Smuzhiyun afresh). 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun (3) The cookie representing the primary index will be allocated according to 72*4882a593Smuzhiyun another parameter passed into the registration function. 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunFor example, kAFS (linux/fs/afs/) uses the following definitions to describe 75*4882a593Smuzhiyunitself:: 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun struct fscache_netfs afs_cache_netfs = { 78*4882a593Smuzhiyun .version = 0, 79*4882a593Smuzhiyun .name = "afs", 80*4882a593Smuzhiyun }; 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun 83*4882a593SmuzhiyunIndex Definition 84*4882a593Smuzhiyun================ 85*4882a593Smuzhiyun 86*4882a593SmuzhiyunIndices are used for two purposes: 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun (1) To aid the finding of a file based on a series of keys (such as AFS's 89*4882a593Smuzhiyun "cell", "volume ID", "vnode ID"). 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun (2) To make it easier to discard a subset of all the files cached based around 92*4882a593Smuzhiyun a particular key - for instance to mirror the removal of an AFS volume. 93*4882a593Smuzhiyun 94*4882a593SmuzhiyunHowever, since it's unlikely that any two netfs's are going to want to define 95*4882a593Smuzhiyuntheir index hierarchies in quite the same way, FS-Cache tries to impose as few 96*4882a593Smuzhiyunrestraints as possible on how an index is structured and where it is placed in 97*4882a593Smuzhiyunthe tree. The netfs can even mix indices and data files at the same level, but 98*4882a593Smuzhiyunit's not recommended. 99*4882a593Smuzhiyun 100*4882a593SmuzhiyunEach index entry consists of a key of indeterminate length plus some auxiliary 101*4882a593Smuzhiyundata, also of indeterminate length. 102*4882a593Smuzhiyun 103*4882a593SmuzhiyunThere are some limits on indices: 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun (1) Any index containing non-index objects should be restricted to a single 106*4882a593Smuzhiyun cache. Any such objects created within an index will be created in the 107*4882a593Smuzhiyun first cache only. The cache in which an index is created can be 108*4882a593Smuzhiyun controlled by cache tags (see below). 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun (2) The entry data must be atomically journallable, so it is limited to about 111*4882a593Smuzhiyun 400 bytes at present. At least 400 bytes will be available. 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun (3) The depth of the index tree should be judged with care as the search 114*4882a593Smuzhiyun function is recursive. Too many layers will run the kernel out of stack. 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunObject Definition 118*4882a593Smuzhiyun================= 119*4882a593Smuzhiyun 120*4882a593SmuzhiyunTo define an object, a structure of the following type should be filled out:: 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun struct fscache_cookie_def 123*4882a593Smuzhiyun { 124*4882a593Smuzhiyun uint8_t name[16]; 125*4882a593Smuzhiyun uint8_t type; 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun struct fscache_cache_tag *(*select_cache)( 128*4882a593Smuzhiyun const void *parent_netfs_data, 129*4882a593Smuzhiyun const void *cookie_netfs_data); 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun enum fscache_checkaux (*check_aux)(void *cookie_netfs_data, 132*4882a593Smuzhiyun const void *data, 133*4882a593Smuzhiyun uint16_t datalen, 134*4882a593Smuzhiyun loff_t object_size); 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun void (*get_context)(void *cookie_netfs_data, void *context); 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun void (*put_context)(void *cookie_netfs_data, void *context); 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun void (*mark_pages_cached)(void *cookie_netfs_data, 141*4882a593Smuzhiyun struct address_space *mapping, 142*4882a593Smuzhiyun struct pagevec *cached_pvec); 143*4882a593Smuzhiyun }; 144*4882a593Smuzhiyun 145*4882a593SmuzhiyunThis has the following fields: 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun (1) The type of the object [mandatory]. 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun This is one of the following values: 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun FSCACHE_COOKIE_TYPE_INDEX 152*4882a593Smuzhiyun This defines an index, which is a special FS-Cache type. 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun FSCACHE_COOKIE_TYPE_DATAFILE 155*4882a593Smuzhiyun This defines an ordinary data file. 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun Any other value between 2 and 255 158*4882a593Smuzhiyun This defines an extraordinary object such as an XATTR. 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun (2) The name of the object type (NUL terminated unless all 16 chars are used) 161*4882a593Smuzhiyun [optional]. 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun (3) A function to select the cache in which to store an index [optional]. 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun This function is invoked when an index needs to be instantiated in a cache 166*4882a593Smuzhiyun during the instantiation of a non-index object. Only the immediate index 167*4882a593Smuzhiyun parent for the non-index object will be queried. Any indices above that 168*4882a593Smuzhiyun in the hierarchy may be stored in multiple caches. This function does not 169*4882a593Smuzhiyun need to be supplied for any non-index object or any index that will only 170*4882a593Smuzhiyun have index children. 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun If this function is not supplied or if it returns NULL then the first 173*4882a593Smuzhiyun cache in the parent's list will be chosen, or failing that, the first 174*4882a593Smuzhiyun cache in the master list. 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun (4) A function to check the auxiliary data [optional]. 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun This function will be called to check that a match found in the cache for 179*4882a593Smuzhiyun this object is valid. For instance with AFS it could check the auxiliary 180*4882a593Smuzhiyun data against the data version number returned by the server to determine 181*4882a593Smuzhiyun whether the index entry in a cache is still valid. 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun If this function is absent, it will be assumed that matching objects in a 184*4882a593Smuzhiyun cache are always valid. 185*4882a593Smuzhiyun 186*4882a593Smuzhiyun The function is also passed the cache's idea of the object size and may 187*4882a593Smuzhiyun use this to manage coherency also. 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun If present, the function should return one of the following values: 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun FSCACHE_CHECKAUX_OKAY 192*4882a593Smuzhiyun - the entry is okay as is 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun FSCACHE_CHECKAUX_NEEDS_UPDATE 195*4882a593Smuzhiyun - the entry requires update 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun FSCACHE_CHECKAUX_OBSOLETE 198*4882a593Smuzhiyun - the entry should be deleted 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun This function can also be used to extract data from the auxiliary data in 201*4882a593Smuzhiyun the cache and copy it into the netfs's structures. 202*4882a593Smuzhiyun 203*4882a593Smuzhiyun (5) A pair of functions to manage contexts for the completion callback 204*4882a593Smuzhiyun [optional]. 205*4882a593Smuzhiyun 206*4882a593Smuzhiyun The cache read/write functions are passed a context which is then passed 207*4882a593Smuzhiyun to the I/O completion callback function. To ensure this context remains 208*4882a593Smuzhiyun valid until after the I/O completion is called, two functions may be 209*4882a593Smuzhiyun provided: one to get an extra reference on the context, and one to drop a 210*4882a593Smuzhiyun reference to it. 211*4882a593Smuzhiyun 212*4882a593Smuzhiyun If the context is not used or is a type of object that won't go out of 213*4882a593Smuzhiyun scope, then these functions are not required. These functions are not 214*4882a593Smuzhiyun required for indices as indices may not contain data. These functions may 215*4882a593Smuzhiyun be called in interrupt context and so may not sleep. 216*4882a593Smuzhiyun 217*4882a593Smuzhiyun (6) A function to mark a page as retaining cache metadata [optional]. 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun This is called by the cache to indicate that it is retaining in-memory 220*4882a593Smuzhiyun information for this page and that the netfs should uncache the page when 221*4882a593Smuzhiyun it has finished. This does not indicate whether there's data on the disk 222*4882a593Smuzhiyun or not. Note that several pages at once may be presented for marking. 223*4882a593Smuzhiyun 224*4882a593Smuzhiyun The PG_fscache bit is set on the pages before this function would be 225*4882a593Smuzhiyun called, so the function need not be provided if this is sufficient. 226*4882a593Smuzhiyun 227*4882a593Smuzhiyun This function is not required for indices as they're not permitted data. 228*4882a593Smuzhiyun 229*4882a593Smuzhiyun (7) A function to unmark all the pages retaining cache metadata [mandatory]. 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun This is called by FS-Cache to indicate that a backing store is being 232*4882a593Smuzhiyun unbound from a cookie and that all the marks on the pages should be 233*4882a593Smuzhiyun cleared to prevent confusion. Note that the cache will have torn down all 234*4882a593Smuzhiyun its tracking information so that the pages don't need to be explicitly 235*4882a593Smuzhiyun uncached. 236*4882a593Smuzhiyun 237*4882a593Smuzhiyun This function is not required for indices as they're not permitted data. 238*4882a593Smuzhiyun 239*4882a593Smuzhiyun 240*4882a593SmuzhiyunNetwork Filesystem (Un)registration 241*4882a593Smuzhiyun=================================== 242*4882a593Smuzhiyun 243*4882a593SmuzhiyunThe first step is to declare the network filesystem to the cache. This also 244*4882a593Smuzhiyuninvolves specifying the layout of the primary index (for AFS, this would be the 245*4882a593Smuzhiyun"cell" level). 246*4882a593Smuzhiyun 247*4882a593SmuzhiyunThe registration function is:: 248*4882a593Smuzhiyun 249*4882a593Smuzhiyun int fscache_register_netfs(struct fscache_netfs *netfs); 250*4882a593Smuzhiyun 251*4882a593SmuzhiyunIt just takes a pointer to the netfs definition. It returns 0 or an error as 252*4882a593Smuzhiyunappropriate. 253*4882a593Smuzhiyun 254*4882a593SmuzhiyunFor kAFS, registration is done as follows:: 255*4882a593Smuzhiyun 256*4882a593Smuzhiyun ret = fscache_register_netfs(&afs_cache_netfs); 257*4882a593Smuzhiyun 258*4882a593SmuzhiyunThe last step is, of course, unregistration:: 259*4882a593Smuzhiyun 260*4882a593Smuzhiyun void fscache_unregister_netfs(struct fscache_netfs *netfs); 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun 263*4882a593SmuzhiyunCache Tag Lookup 264*4882a593Smuzhiyun================ 265*4882a593Smuzhiyun 266*4882a593SmuzhiyunFS-Cache permits the use of more than one cache. To permit particular index 267*4882a593Smuzhiyunsubtrees to be bound to particular caches, the second step is to look up cache 268*4882a593Smuzhiyunrepresentation tags. This step is optional; it can be left entirely up to 269*4882a593SmuzhiyunFS-Cache as to which cache should be used. The problem with doing that is that 270*4882a593SmuzhiyunFS-Cache will always pick the first cache that was registered. 271*4882a593Smuzhiyun 272*4882a593SmuzhiyunTo get the representation for a named tag:: 273*4882a593Smuzhiyun 274*4882a593Smuzhiyun struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name); 275*4882a593Smuzhiyun 276*4882a593SmuzhiyunThis takes a text string as the name and returns a representation of a tag. It 277*4882a593Smuzhiyunwill never return an error. It may return a dummy tag, however, if it runs out 278*4882a593Smuzhiyunof memory; this will inhibit caching with this tag. 279*4882a593Smuzhiyun 280*4882a593SmuzhiyunAny representation so obtained must be released by passing it to this function:: 281*4882a593Smuzhiyun 282*4882a593Smuzhiyun void fscache_release_cache_tag(struct fscache_cache_tag *tag); 283*4882a593Smuzhiyun 284*4882a593SmuzhiyunThe tag will be retrieved by FS-Cache when it calls the object definition 285*4882a593Smuzhiyunoperation select_cache(). 286*4882a593Smuzhiyun 287*4882a593Smuzhiyun 288*4882a593SmuzhiyunIndex Registration 289*4882a593Smuzhiyun================== 290*4882a593Smuzhiyun 291*4882a593SmuzhiyunThe third step is to inform FS-Cache about part of an index hierarchy that can 292*4882a593Smuzhiyunbe used to locate files. This is done by requesting a cookie for each index in 293*4882a593Smuzhiyunthe path to the file:: 294*4882a593Smuzhiyun 295*4882a593Smuzhiyun struct fscache_cookie * 296*4882a593Smuzhiyun fscache_acquire_cookie(struct fscache_cookie *parent, 297*4882a593Smuzhiyun const struct fscache_object_def *def, 298*4882a593Smuzhiyun const void *index_key, 299*4882a593Smuzhiyun size_t index_key_len, 300*4882a593Smuzhiyun const void *aux_data, 301*4882a593Smuzhiyun size_t aux_data_len, 302*4882a593Smuzhiyun void *netfs_data, 303*4882a593Smuzhiyun loff_t object_size, 304*4882a593Smuzhiyun bool enable); 305*4882a593Smuzhiyun 306*4882a593SmuzhiyunThis function creates an index entry in the index represented by parent, 307*4882a593Smuzhiyunfilling in the index entry by calling the operations pointed to by def. 308*4882a593Smuzhiyun 309*4882a593SmuzhiyunA unique key that represents the object within the parent must be pointed to by 310*4882a593Smuzhiyunindex_key and is of length index_key_len. 311*4882a593Smuzhiyun 312*4882a593SmuzhiyunAn optional blob of auxiliary data that is to be stored within the cache can be 313*4882a593Smuzhiyunpointed to with aux_data and should be of length aux_data_len. This would 314*4882a593Smuzhiyuntypically be used for storing coherency data. 315*4882a593Smuzhiyun 316*4882a593SmuzhiyunThe netfs may pass an arbitrary value in netfs_data and this will be presented 317*4882a593Smuzhiyunto it in the event of any calling back. This may also be used in tracing or 318*4882a593Smuzhiyunlogging of messages. 319*4882a593Smuzhiyun 320*4882a593SmuzhiyunThe cache tracks the size of the data attached to an object and this set to be 321*4882a593Smuzhiyunobject_size. For indices, this should be 0. This value will be passed to the 322*4882a593Smuzhiyun->check_aux() callback. 323*4882a593Smuzhiyun 324*4882a593SmuzhiyunNote that this function never returns an error - all errors are handled 325*4882a593Smuzhiyuninternally. It may, however, return NULL to indicate no cookie. It is quite 326*4882a593Smuzhiyunacceptable to pass this token back to this function as the parent to another 327*4882a593Smuzhiyunacquisition (or even to the relinquish cookie, read page and write page 328*4882a593Smuzhiyunfunctions - see below). 329*4882a593Smuzhiyun 330*4882a593SmuzhiyunNote also that no indices are actually created in a cache until a non-index 331*4882a593Smuzhiyunobject needs to be created somewhere down the hierarchy. Furthermore, an index 332*4882a593Smuzhiyunmay be created in several different caches independently at different times. 333*4882a593SmuzhiyunThis is all handled transparently, and the netfs doesn't see any of it. 334*4882a593Smuzhiyun 335*4882a593SmuzhiyunA cookie will be created in the disabled state if enabled is false. A cookie 336*4882a593Smuzhiyunmust be enabled to do anything with it. A disabled cookie can be enabled by 337*4882a593Smuzhiyuncalling fscache_enable_cookie() (see below). 338*4882a593Smuzhiyun 339*4882a593SmuzhiyunFor example, with AFS, a cell would be added to the primary index. This index 340*4882a593Smuzhiyunentry would have a dependent inode containing volume mappings within this cell:: 341*4882a593Smuzhiyun 342*4882a593Smuzhiyun cell->cache = 343*4882a593Smuzhiyun fscache_acquire_cookie(afs_cache_netfs.primary_index, 344*4882a593Smuzhiyun &afs_cell_cache_index_def, 345*4882a593Smuzhiyun cell->name, strlen(cell->name), 346*4882a593Smuzhiyun NULL, 0, 347*4882a593Smuzhiyun cell, 0, true); 348*4882a593Smuzhiyun 349*4882a593SmuzhiyunAnd then a particular volume could be added to that index by ID, creating 350*4882a593Smuzhiyunanother index for vnodes (AFS inode equivalents):: 351*4882a593Smuzhiyun 352*4882a593Smuzhiyun volume->cache = 353*4882a593Smuzhiyun fscache_acquire_cookie(volume->cell->cache, 354*4882a593Smuzhiyun &afs_volume_cache_index_def, 355*4882a593Smuzhiyun &volume->vid, sizeof(volume->vid), 356*4882a593Smuzhiyun NULL, 0, 357*4882a593Smuzhiyun volume, 0, true); 358*4882a593Smuzhiyun 359*4882a593Smuzhiyun 360*4882a593SmuzhiyunData File Registration 361*4882a593Smuzhiyun====================== 362*4882a593Smuzhiyun 363*4882a593SmuzhiyunThe fourth step is to request a data file be created in the cache. This is 364*4882a593Smuzhiyunidentical to index cookie acquisition. The only difference is that the type in 365*4882a593Smuzhiyunthe object definition should be something other than index type:: 366*4882a593Smuzhiyun 367*4882a593Smuzhiyun vnode->cache = 368*4882a593Smuzhiyun fscache_acquire_cookie(volume->cache, 369*4882a593Smuzhiyun &afs_vnode_cache_object_def, 370*4882a593Smuzhiyun &key, sizeof(key), 371*4882a593Smuzhiyun &aux, sizeof(aux), 372*4882a593Smuzhiyun vnode, vnode->status.size, true); 373*4882a593Smuzhiyun 374*4882a593Smuzhiyun 375*4882a593SmuzhiyunMiscellaneous Object Registration 376*4882a593Smuzhiyun================================= 377*4882a593Smuzhiyun 378*4882a593SmuzhiyunAn optional step is to request an object of miscellaneous type be created in 379*4882a593Smuzhiyunthe cache. This is almost identical to index cookie acquisition. The only 380*4882a593Smuzhiyundifference is that the type in the object definition should be something other 381*4882a593Smuzhiyunthan index type. While the parent object could be an index, it's more likely 382*4882a593Smuzhiyunit would be some other type of object such as a data file:: 383*4882a593Smuzhiyun 384*4882a593Smuzhiyun xattr->cache = 385*4882a593Smuzhiyun fscache_acquire_cookie(vnode->cache, 386*4882a593Smuzhiyun &afs_xattr_cache_object_def, 387*4882a593Smuzhiyun &xattr->name, strlen(xattr->name), 388*4882a593Smuzhiyun NULL, 0, 389*4882a593Smuzhiyun xattr, strlen(xattr->val), true); 390*4882a593Smuzhiyun 391*4882a593SmuzhiyunMiscellaneous objects might be used to store extended attributes or directory 392*4882a593Smuzhiyunentries for example. 393*4882a593Smuzhiyun 394*4882a593Smuzhiyun 395*4882a593SmuzhiyunSetting the Data File Size 396*4882a593Smuzhiyun========================== 397*4882a593Smuzhiyun 398*4882a593SmuzhiyunThe fifth step is to set the physical attributes of the file, such as its size. 399*4882a593SmuzhiyunThis doesn't automatically reserve any space in the cache, but permits the 400*4882a593Smuzhiyuncache to adjust its metadata for data tracking appropriately:: 401*4882a593Smuzhiyun 402*4882a593Smuzhiyun int fscache_attr_changed(struct fscache_cookie *cookie); 403*4882a593Smuzhiyun 404*4882a593SmuzhiyunThe cache will return -ENOBUFS if there is no backing cache or if there is no 405*4882a593Smuzhiyunspace to allocate any extra metadata required in the cache. 406*4882a593Smuzhiyun 407*4882a593SmuzhiyunNote that attempts to read or write data pages in the cache over this size may 408*4882a593Smuzhiyunbe rebuffed with -ENOBUFS. 409*4882a593Smuzhiyun 410*4882a593SmuzhiyunThis operation schedules an attribute adjustment to happen asynchronously at 411*4882a593Smuzhiyunsome point in the future, and as such, it may happen after the function returns 412*4882a593Smuzhiyunto the caller. The attribute adjustment excludes read and write operations. 413*4882a593Smuzhiyun 414*4882a593Smuzhiyun 415*4882a593SmuzhiyunPage alloc/read/write 416*4882a593Smuzhiyun===================== 417*4882a593Smuzhiyun 418*4882a593SmuzhiyunAnd the sixth step is to store and retrieve pages in the cache. There are 419*4882a593Smuzhiyunthree functions that are used to do this. 420*4882a593Smuzhiyun 421*4882a593SmuzhiyunNote: 422*4882a593Smuzhiyun 423*4882a593Smuzhiyun (1) A page should not be re-read or re-allocated without uncaching it first. 424*4882a593Smuzhiyun 425*4882a593Smuzhiyun (2) A read or allocated page must be uncached when the netfs page is released 426*4882a593Smuzhiyun from the pagecache. 427*4882a593Smuzhiyun 428*4882a593Smuzhiyun (3) A page should only be written to the cache if previous read or allocated. 429*4882a593Smuzhiyun 430*4882a593SmuzhiyunThis permits the cache to maintain its page tracking in proper order. 431*4882a593Smuzhiyun 432*4882a593Smuzhiyun 433*4882a593SmuzhiyunPAGE READ 434*4882a593Smuzhiyun--------- 435*4882a593Smuzhiyun 436*4882a593SmuzhiyunFirstly, the netfs should ask FS-Cache to examine the caches and read the 437*4882a593Smuzhiyuncontents cached for a particular page of a particular file if present, or else 438*4882a593Smuzhiyunallocate space to store the contents if not:: 439*4882a593Smuzhiyun 440*4882a593Smuzhiyun typedef 441*4882a593Smuzhiyun void (*fscache_rw_complete_t)(struct page *page, 442*4882a593Smuzhiyun void *context, 443*4882a593Smuzhiyun int error); 444*4882a593Smuzhiyun 445*4882a593Smuzhiyun int fscache_read_or_alloc_page(struct fscache_cookie *cookie, 446*4882a593Smuzhiyun struct page *page, 447*4882a593Smuzhiyun fscache_rw_complete_t end_io_func, 448*4882a593Smuzhiyun void *context, 449*4882a593Smuzhiyun gfp_t gfp); 450*4882a593Smuzhiyun 451*4882a593SmuzhiyunThe cookie argument must specify a cookie for an object that isn't an index, 452*4882a593Smuzhiyunthe page specified will have the data loaded into it (and is also used to 453*4882a593Smuzhiyunspecify the page number), and the gfp argument is used to control how any 454*4882a593Smuzhiyunmemory allocations made are satisfied. 455*4882a593Smuzhiyun 456*4882a593SmuzhiyunIf the cookie indicates the inode is not cached: 457*4882a593Smuzhiyun 458*4882a593Smuzhiyun (1) The function will return -ENOBUFS. 459*4882a593Smuzhiyun 460*4882a593SmuzhiyunElse if there's a copy of the page resident in the cache: 461*4882a593Smuzhiyun 462*4882a593Smuzhiyun (1) The mark_pages_cached() cookie operation will be called on that page. 463*4882a593Smuzhiyun 464*4882a593Smuzhiyun (2) The function will submit a request to read the data from the cache's 465*4882a593Smuzhiyun backing device directly into the page specified. 466*4882a593Smuzhiyun 467*4882a593Smuzhiyun (3) The function will return 0. 468*4882a593Smuzhiyun 469*4882a593Smuzhiyun (4) When the read is complete, end_io_func() will be invoked with: 470*4882a593Smuzhiyun 471*4882a593Smuzhiyun * The netfs data supplied when the cookie was created. 472*4882a593Smuzhiyun 473*4882a593Smuzhiyun * The page descriptor. 474*4882a593Smuzhiyun 475*4882a593Smuzhiyun * The context argument passed to the above function. This will be 476*4882a593Smuzhiyun maintained with the get_context/put_context functions mentioned above. 477*4882a593Smuzhiyun 478*4882a593Smuzhiyun * An argument that's 0 on success or negative for an error code. 479*4882a593Smuzhiyun 480*4882a593Smuzhiyun If an error occurs, it should be assumed that the page contains no usable 481*4882a593Smuzhiyun data. fscache_readpages_cancel() may need to be called. 482*4882a593Smuzhiyun 483*4882a593Smuzhiyun end_io_func() will be called in process context if the read is results in 484*4882a593Smuzhiyun an error, but it might be called in interrupt context if the read is 485*4882a593Smuzhiyun successful. 486*4882a593Smuzhiyun 487*4882a593SmuzhiyunOtherwise, if there's not a copy available in cache, but the cache may be able 488*4882a593Smuzhiyunto store the page: 489*4882a593Smuzhiyun 490*4882a593Smuzhiyun (1) The mark_pages_cached() cookie operation will be called on that page. 491*4882a593Smuzhiyun 492*4882a593Smuzhiyun (2) A block may be reserved in the cache and attached to the object at the 493*4882a593Smuzhiyun appropriate place. 494*4882a593Smuzhiyun 495*4882a593Smuzhiyun (3) The function will return -ENODATA. 496*4882a593Smuzhiyun 497*4882a593SmuzhiyunThis function may also return -ENOMEM or -EINTR, in which case it won't have 498*4882a593Smuzhiyunread any data from the cache. 499*4882a593Smuzhiyun 500*4882a593Smuzhiyun 501*4882a593SmuzhiyunPage Allocate 502*4882a593Smuzhiyun------------- 503*4882a593Smuzhiyun 504*4882a593SmuzhiyunAlternatively, if there's not expected to be any data in the cache for a page 505*4882a593Smuzhiyunbecause the file has been extended, a block can simply be allocated instead:: 506*4882a593Smuzhiyun 507*4882a593Smuzhiyun int fscache_alloc_page(struct fscache_cookie *cookie, 508*4882a593Smuzhiyun struct page *page, 509*4882a593Smuzhiyun gfp_t gfp); 510*4882a593Smuzhiyun 511*4882a593SmuzhiyunThis is similar to the fscache_read_or_alloc_page() function, except that it 512*4882a593Smuzhiyunnever reads from the cache. It will return 0 if a block has been allocated, 513*4882a593Smuzhiyunrather than -ENODATA as the other would. One or the other must be performed 514*4882a593Smuzhiyunbefore writing to the cache. 515*4882a593Smuzhiyun 516*4882a593SmuzhiyunThe mark_pages_cached() cookie operation will be called on the page if 517*4882a593Smuzhiyunsuccessful. 518*4882a593Smuzhiyun 519*4882a593Smuzhiyun 520*4882a593SmuzhiyunPage Write 521*4882a593Smuzhiyun---------- 522*4882a593Smuzhiyun 523*4882a593SmuzhiyunSecondly, if the netfs changes the contents of the page (either due to an 524*4882a593Smuzhiyuninitial download or if a user performs a write), then the page should be 525*4882a593Smuzhiyunwritten back to the cache:: 526*4882a593Smuzhiyun 527*4882a593Smuzhiyun int fscache_write_page(struct fscache_cookie *cookie, 528*4882a593Smuzhiyun struct page *page, 529*4882a593Smuzhiyun loff_t object_size, 530*4882a593Smuzhiyun gfp_t gfp); 531*4882a593Smuzhiyun 532*4882a593SmuzhiyunThe cookie argument must specify a data file cookie, the page specified should 533*4882a593Smuzhiyuncontain the data to be written (and is also used to specify the page number), 534*4882a593Smuzhiyunobject_size is the revised size of the object and the gfp argument is used to 535*4882a593Smuzhiyuncontrol how any memory allocations made are satisfied. 536*4882a593Smuzhiyun 537*4882a593SmuzhiyunThe page must have first been read or allocated successfully and must not have 538*4882a593Smuzhiyunbeen uncached before writing is performed. 539*4882a593Smuzhiyun 540*4882a593SmuzhiyunIf the cookie indicates the inode is not cached then: 541*4882a593Smuzhiyun 542*4882a593Smuzhiyun (1) The function will return -ENOBUFS. 543*4882a593Smuzhiyun 544*4882a593SmuzhiyunElse if space can be allocated in the cache to hold this page: 545*4882a593Smuzhiyun 546*4882a593Smuzhiyun (1) PG_fscache_write will be set on the page. 547*4882a593Smuzhiyun 548*4882a593Smuzhiyun (2) The function will submit a request to write the data to cache's backing 549*4882a593Smuzhiyun device directly from the page specified. 550*4882a593Smuzhiyun 551*4882a593Smuzhiyun (3) The function will return 0. 552*4882a593Smuzhiyun 553*4882a593Smuzhiyun (4) When the write is complete PG_fscache_write is cleared on the page and 554*4882a593Smuzhiyun anyone waiting for that bit will be woken up. 555*4882a593Smuzhiyun 556*4882a593SmuzhiyunElse if there's no space available in the cache, -ENOBUFS will be returned. It 557*4882a593Smuzhiyunis also possible for the PG_fscache_write bit to be cleared when no write took 558*4882a593Smuzhiyunplace if unforeseen circumstances arose (such as a disk error). 559*4882a593Smuzhiyun 560*4882a593SmuzhiyunWriting takes place asynchronously. 561*4882a593Smuzhiyun 562*4882a593Smuzhiyun 563*4882a593SmuzhiyunMultiple Page Read 564*4882a593Smuzhiyun------------------ 565*4882a593Smuzhiyun 566*4882a593SmuzhiyunA facility is provided to read several pages at once, as requested by the 567*4882a593Smuzhiyunreadpages() address space operation:: 568*4882a593Smuzhiyun 569*4882a593Smuzhiyun int fscache_read_or_alloc_pages(struct fscache_cookie *cookie, 570*4882a593Smuzhiyun struct address_space *mapping, 571*4882a593Smuzhiyun struct list_head *pages, 572*4882a593Smuzhiyun int *nr_pages, 573*4882a593Smuzhiyun fscache_rw_complete_t end_io_func, 574*4882a593Smuzhiyun void *context, 575*4882a593Smuzhiyun gfp_t gfp); 576*4882a593Smuzhiyun 577*4882a593SmuzhiyunThis works in a similar way to fscache_read_or_alloc_page(), except: 578*4882a593Smuzhiyun 579*4882a593Smuzhiyun (1) Any page it can retrieve data for is removed from pages and nr_pages and 580*4882a593Smuzhiyun dispatched for reading to the disk. Reads of adjacent pages on disk may 581*4882a593Smuzhiyun be merged for greater efficiency. 582*4882a593Smuzhiyun 583*4882a593Smuzhiyun (2) The mark_pages_cached() cookie operation will be called on several pages 584*4882a593Smuzhiyun at once if they're being read or allocated. 585*4882a593Smuzhiyun 586*4882a593Smuzhiyun (3) If there was an general error, then that error will be returned. 587*4882a593Smuzhiyun 588*4882a593Smuzhiyun Else if some pages couldn't be allocated or read, then -ENOBUFS will be 589*4882a593Smuzhiyun returned. 590*4882a593Smuzhiyun 591*4882a593Smuzhiyun Else if some pages couldn't be read but were allocated, then -ENODATA will 592*4882a593Smuzhiyun be returned. 593*4882a593Smuzhiyun 594*4882a593Smuzhiyun Otherwise, if all pages had reads dispatched, then 0 will be returned, the 595*4882a593Smuzhiyun list will be empty and ``*nr_pages`` will be 0. 596*4882a593Smuzhiyun 597*4882a593Smuzhiyun (4) end_io_func will be called once for each page being read as the reads 598*4882a593Smuzhiyun complete. It will be called in process context if error != 0, but it may 599*4882a593Smuzhiyun be called in interrupt context if there is no error. 600*4882a593Smuzhiyun 601*4882a593SmuzhiyunNote that a return of -ENODATA, -ENOBUFS or any other error does not preclude 602*4882a593Smuzhiyunsome of the pages being read and some being allocated. Those pages will have 603*4882a593Smuzhiyunbeen marked appropriately and will need uncaching. 604*4882a593Smuzhiyun 605*4882a593Smuzhiyun 606*4882a593SmuzhiyunCancellation of Unread Pages 607*4882a593Smuzhiyun---------------------------- 608*4882a593Smuzhiyun 609*4882a593SmuzhiyunIf one or more pages are passed to fscache_read_or_alloc_pages() but not then 610*4882a593Smuzhiyunread from the cache and also not read from the underlying filesystem then 611*4882a593Smuzhiyunthose pages will need to have any marks and reservations removed. This can be 612*4882a593Smuzhiyundone by calling:: 613*4882a593Smuzhiyun 614*4882a593Smuzhiyun void fscache_readpages_cancel(struct fscache_cookie *cookie, 615*4882a593Smuzhiyun struct list_head *pages); 616*4882a593Smuzhiyun 617*4882a593Smuzhiyunprior to returning to the caller. The cookie argument should be as passed to 618*4882a593Smuzhiyunfscache_read_or_alloc_pages(). Every page in the pages list will be examined 619*4882a593Smuzhiyunand any that have PG_fscache set will be uncached. 620*4882a593Smuzhiyun 621*4882a593Smuzhiyun 622*4882a593SmuzhiyunPage Uncaching 623*4882a593Smuzhiyun============== 624*4882a593Smuzhiyun 625*4882a593SmuzhiyunTo uncache a page, this function should be called:: 626*4882a593Smuzhiyun 627*4882a593Smuzhiyun void fscache_uncache_page(struct fscache_cookie *cookie, 628*4882a593Smuzhiyun struct page *page); 629*4882a593Smuzhiyun 630*4882a593SmuzhiyunThis function permits the cache to release any in-memory representation it 631*4882a593Smuzhiyunmight be holding for this netfs page. This function must be called once for 632*4882a593Smuzhiyuneach page on which the read or write page functions above have been called to 633*4882a593Smuzhiyunmake sure the cache's in-memory tracking information gets torn down. 634*4882a593Smuzhiyun 635*4882a593SmuzhiyunNote that pages can't be explicitly deleted from the a data file. The whole 636*4882a593Smuzhiyundata file must be retired (see the relinquish cookie function below). 637*4882a593Smuzhiyun 638*4882a593SmuzhiyunFurthermore, note that this does not cancel the asynchronous read or write 639*4882a593Smuzhiyunoperation started by the read/alloc and write functions, so the page 640*4882a593Smuzhiyuninvalidation functions must use:: 641*4882a593Smuzhiyun 642*4882a593Smuzhiyun bool fscache_check_page_write(struct fscache_cookie *cookie, 643*4882a593Smuzhiyun struct page *page); 644*4882a593Smuzhiyun 645*4882a593Smuzhiyunto see if a page is being written to the cache, and:: 646*4882a593Smuzhiyun 647*4882a593Smuzhiyun void fscache_wait_on_page_write(struct fscache_cookie *cookie, 648*4882a593Smuzhiyun struct page *page); 649*4882a593Smuzhiyun 650*4882a593Smuzhiyunto wait for it to finish if it is. 651*4882a593Smuzhiyun 652*4882a593Smuzhiyun 653*4882a593SmuzhiyunWhen releasepage() is being implemented, a special FS-Cache function exists to 654*4882a593Smuzhiyunmanage the heuristics of coping with vmscan trying to eject pages, which may 655*4882a593Smuzhiyunconflict with the cache trying to write pages to the cache (which may itself 656*4882a593Smuzhiyunneed to allocate memory):: 657*4882a593Smuzhiyun 658*4882a593Smuzhiyun bool fscache_maybe_release_page(struct fscache_cookie *cookie, 659*4882a593Smuzhiyun struct page *page, 660*4882a593Smuzhiyun gfp_t gfp); 661*4882a593Smuzhiyun 662*4882a593SmuzhiyunThis takes the netfs cookie, and the page and gfp arguments as supplied to 663*4882a593Smuzhiyunreleasepage(). It will return false if the page cannot be released yet for 664*4882a593Smuzhiyunsome reason and if it returns true, the page has been uncached and can now be 665*4882a593Smuzhiyunreleased. 666*4882a593Smuzhiyun 667*4882a593SmuzhiyunTo make a page available for release, this function may wait for an outstanding 668*4882a593Smuzhiyunstorage request to complete, or it may attempt to cancel the storage request - 669*4882a593Smuzhiyunin which case the page will not be stored in the cache this time. 670*4882a593Smuzhiyun 671*4882a593Smuzhiyun 672*4882a593SmuzhiyunBulk Image Page Uncache 673*4882a593Smuzhiyun----------------------- 674*4882a593Smuzhiyun 675*4882a593SmuzhiyunA convenience routine is provided to perform an uncache on all the pages 676*4882a593Smuzhiyunattached to an inode. This assumes that the pages on the inode correspond on a 677*4882a593Smuzhiyun1:1 basis with the pages in the cache:: 678*4882a593Smuzhiyun 679*4882a593Smuzhiyun void fscache_uncache_all_inode_pages(struct fscache_cookie *cookie, 680*4882a593Smuzhiyun struct inode *inode); 681*4882a593Smuzhiyun 682*4882a593SmuzhiyunThis takes the netfs cookie that the pages were cached with and the inode that 683*4882a593Smuzhiyunthe pages are attached to. This function will wait for pages to finish being 684*4882a593Smuzhiyunwritten to the cache and for the cache to finish with the page generally. No 685*4882a593Smuzhiyunerror is returned. 686*4882a593Smuzhiyun 687*4882a593Smuzhiyun 688*4882a593SmuzhiyunIndex and Data File consistency 689*4882a593Smuzhiyun=============================== 690*4882a593Smuzhiyun 691*4882a593SmuzhiyunTo find out whether auxiliary data for an object is up to data within the 692*4882a593Smuzhiyuncache, the following function can be called:: 693*4882a593Smuzhiyun 694*4882a593Smuzhiyun int fscache_check_consistency(struct fscache_cookie *cookie, 695*4882a593Smuzhiyun const void *aux_data); 696*4882a593Smuzhiyun 697*4882a593SmuzhiyunThis will call back to the netfs to check whether the auxiliary data associated 698*4882a593Smuzhiyunwith a cookie is correct; if aux_data is non-NULL, it will update the auxiliary 699*4882a593Smuzhiyundata buffer first. It returns 0 if it is and -ESTALE if it isn't; it may also 700*4882a593Smuzhiyunreturn -ENOMEM and -ERESTARTSYS. 701*4882a593Smuzhiyun 702*4882a593SmuzhiyunTo request an update of the index data for an index or other object, the 703*4882a593Smuzhiyunfollowing function should be called:: 704*4882a593Smuzhiyun 705*4882a593Smuzhiyun void fscache_update_cookie(struct fscache_cookie *cookie, 706*4882a593Smuzhiyun const void *aux_data); 707*4882a593Smuzhiyun 708*4882a593SmuzhiyunThis function will update the cookie's auxiliary data buffer from aux_data if 709*4882a593Smuzhiyunthat is non-NULL and then schedule this to be stored on disk. The update 710*4882a593Smuzhiyunmethod in the parent index definition will be called to transfer the data. 711*4882a593Smuzhiyun 712*4882a593SmuzhiyunNote that partial updates may happen automatically at other times, such as when 713*4882a593Smuzhiyundata blocks are added to a data file object. 714*4882a593Smuzhiyun 715*4882a593Smuzhiyun 716*4882a593SmuzhiyunCookie Enablement 717*4882a593Smuzhiyun================= 718*4882a593Smuzhiyun 719*4882a593SmuzhiyunCookies exist in one of two states: enabled and disabled. If a cookie is 720*4882a593Smuzhiyundisabled, it ignores all attempts to acquire child cookies; check, update or 721*4882a593Smuzhiyuninvalidate its state; allocate, read or write backing pages - though it is 722*4882a593Smuzhiyunstill possible to uncache pages and relinquish the cookie. 723*4882a593Smuzhiyun 724*4882a593SmuzhiyunThe initial enablement state is set by fscache_acquire_cookie(), but the cookie 725*4882a593Smuzhiyuncan be enabled or disabled later. To disable a cookie, call:: 726*4882a593Smuzhiyun 727*4882a593Smuzhiyun void fscache_disable_cookie(struct fscache_cookie *cookie, 728*4882a593Smuzhiyun const void *aux_data, 729*4882a593Smuzhiyun bool invalidate); 730*4882a593Smuzhiyun 731*4882a593SmuzhiyunIf the cookie is not already disabled, this locks the cookie against other 732*4882a593Smuzhiyunenable and disable ops, marks the cookie as being disabled, discards or 733*4882a593Smuzhiyuninvalidates any backing objects and waits for cessation of activity on any 734*4882a593Smuzhiyunassociated object before unlocking the cookie. 735*4882a593Smuzhiyun 736*4882a593SmuzhiyunAll possible failures are handled internally. The caller should consider 737*4882a593Smuzhiyuncalling fscache_uncache_all_inode_pages() afterwards to make sure all page 738*4882a593Smuzhiyunmarkings are cleared up. 739*4882a593Smuzhiyun 740*4882a593SmuzhiyunCookies can be enabled or reenabled with:: 741*4882a593Smuzhiyun 742*4882a593Smuzhiyun void fscache_enable_cookie(struct fscache_cookie *cookie, 743*4882a593Smuzhiyun const void *aux_data, 744*4882a593Smuzhiyun loff_t object_size, 745*4882a593Smuzhiyun bool (*can_enable)(void *data), 746*4882a593Smuzhiyun void *data) 747*4882a593Smuzhiyun 748*4882a593SmuzhiyunIf the cookie is not already enabled, this locks the cookie against other 749*4882a593Smuzhiyunenable and disable ops, invokes can_enable() and, if the cookie is not an index 750*4882a593Smuzhiyuncookie, will begin the procedure of acquiring backing objects. 751*4882a593Smuzhiyun 752*4882a593SmuzhiyunThe optional can_enable() function is passed the data argument and returns a 753*4882a593Smuzhiyunruling as to whether or not enablement should actually be permitted to begin. 754*4882a593Smuzhiyun 755*4882a593SmuzhiyunAll possible failures are handled internally. The cookie will only be marked 756*4882a593Smuzhiyunas enabled if provisional backing objects are allocated. 757*4882a593Smuzhiyun 758*4882a593SmuzhiyunThe object's data size is updated from object_size and is passed to the 759*4882a593Smuzhiyun->check_aux() function. 760*4882a593Smuzhiyun 761*4882a593SmuzhiyunIn both cases, the cookie's auxiliary data buffer is updated from aux_data if 762*4882a593Smuzhiyunthat is non-NULL inside the enablement lock before proceeding. 763*4882a593Smuzhiyun 764*4882a593Smuzhiyun 765*4882a593SmuzhiyunMiscellaneous Cookie operations 766*4882a593Smuzhiyun=============================== 767*4882a593Smuzhiyun 768*4882a593SmuzhiyunThere are a number of operations that can be used to control cookies: 769*4882a593Smuzhiyun 770*4882a593Smuzhiyun * Cookie pinning:: 771*4882a593Smuzhiyun 772*4882a593Smuzhiyun int fscache_pin_cookie(struct fscache_cookie *cookie); 773*4882a593Smuzhiyun void fscache_unpin_cookie(struct fscache_cookie *cookie); 774*4882a593Smuzhiyun 775*4882a593Smuzhiyun These operations permit data cookies to be pinned into the cache and to 776*4882a593Smuzhiyun have the pinning removed. They are not permitted on index cookies. 777*4882a593Smuzhiyun 778*4882a593Smuzhiyun The pinning function will return 0 if successful, -ENOBUFS in the cookie 779*4882a593Smuzhiyun isn't backed by a cache, -EOPNOTSUPP if the cache doesn't support pinning, 780*4882a593Smuzhiyun -ENOSPC if there isn't enough space to honour the operation, -ENOMEM or 781*4882a593Smuzhiyun -EIO if there's any other problem. 782*4882a593Smuzhiyun 783*4882a593Smuzhiyun * Data space reservation:: 784*4882a593Smuzhiyun 785*4882a593Smuzhiyun int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size); 786*4882a593Smuzhiyun 787*4882a593Smuzhiyun This permits a netfs to request cache space be reserved to store up to the 788*4882a593Smuzhiyun given amount of a file. It is permitted to ask for more than the current 789*4882a593Smuzhiyun size of the file to allow for future file expansion. 790*4882a593Smuzhiyun 791*4882a593Smuzhiyun If size is given as zero then the reservation will be cancelled. 792*4882a593Smuzhiyun 793*4882a593Smuzhiyun The function will return 0 if successful, -ENOBUFS in the cookie isn't 794*4882a593Smuzhiyun backed by a cache, -EOPNOTSUPP if the cache doesn't support reservations, 795*4882a593Smuzhiyun -ENOSPC if there isn't enough space to honour the operation, -ENOMEM or 796*4882a593Smuzhiyun -EIO if there's any other problem. 797*4882a593Smuzhiyun 798*4882a593Smuzhiyun Note that this doesn't pin an object in a cache; it can still be culled to 799*4882a593Smuzhiyun make space if it's not in use. 800*4882a593Smuzhiyun 801*4882a593Smuzhiyun 802*4882a593SmuzhiyunCookie Unregistration 803*4882a593Smuzhiyun===================== 804*4882a593Smuzhiyun 805*4882a593SmuzhiyunTo get rid of a cookie, this function should be called:: 806*4882a593Smuzhiyun 807*4882a593Smuzhiyun void fscache_relinquish_cookie(struct fscache_cookie *cookie, 808*4882a593Smuzhiyun const void *aux_data, 809*4882a593Smuzhiyun bool retire); 810*4882a593Smuzhiyun 811*4882a593SmuzhiyunIf retire is non-zero, then the object will be marked for recycling, and all 812*4882a593Smuzhiyuncopies of it will be removed from all active caches in which it is present. 813*4882a593SmuzhiyunNot only that but all child objects will also be retired. 814*4882a593Smuzhiyun 815*4882a593SmuzhiyunIf retire is zero, then the object may be available again when next the 816*4882a593Smuzhiyunacquisition function is called. Retirement here will overrule the pinning on a 817*4882a593Smuzhiyuncookie. 818*4882a593Smuzhiyun 819*4882a593SmuzhiyunThe cookie's auxiliary data will be updated from aux_data if that is non-NULL 820*4882a593Smuzhiyunso that the cache can lazily update it on disk. 821*4882a593Smuzhiyun 822*4882a593SmuzhiyunOne very important note - relinquish must NOT be called for a cookie unless all 823*4882a593Smuzhiyunthe cookies for "child" indices, objects and pages have been relinquished 824*4882a593Smuzhiyunfirst. 825*4882a593Smuzhiyun 826*4882a593Smuzhiyun 827*4882a593SmuzhiyunIndex Invalidation 828*4882a593Smuzhiyun================== 829*4882a593Smuzhiyun 830*4882a593SmuzhiyunThere is no direct way to invalidate an index subtree. To do this, the caller 831*4882a593Smuzhiyunshould relinquish and retire the cookie they have, and then acquire a new one. 832*4882a593Smuzhiyun 833*4882a593Smuzhiyun 834*4882a593SmuzhiyunData File Invalidation 835*4882a593Smuzhiyun====================== 836*4882a593Smuzhiyun 837*4882a593SmuzhiyunSometimes it will be necessary to invalidate an object that contains data. 838*4882a593SmuzhiyunTypically this will be necessary when the server tells the netfs of a foreign 839*4882a593Smuzhiyunchange - at which point the netfs has to throw away all the state it had for an 840*4882a593Smuzhiyuninode and reload from the server. 841*4882a593Smuzhiyun 842*4882a593SmuzhiyunTo indicate that a cache object should be invalidated, the following function 843*4882a593Smuzhiyuncan be called:: 844*4882a593Smuzhiyun 845*4882a593Smuzhiyun void fscache_invalidate(struct fscache_cookie *cookie); 846*4882a593Smuzhiyun 847*4882a593SmuzhiyunThis can be called with spinlocks held as it defers the work to a thread pool. 848*4882a593SmuzhiyunAll extant storage, retrieval and attribute change ops at this point are 849*4882a593Smuzhiyuncancelled and discarded. Some future operations will be rejected until the 850*4882a593Smuzhiyuncache has had a chance to insert a barrier in the operations queue. After 851*4882a593Smuzhiyunthat, operations will be queued again behind the invalidation operation. 852*4882a593Smuzhiyun 853*4882a593SmuzhiyunThe invalidation operation will perform an attribute change operation and an 854*4882a593Smuzhiyunauxiliary data update operation as it is very likely these will have changed. 855*4882a593Smuzhiyun 856*4882a593SmuzhiyunUsing the following function, the netfs can wait for the invalidation operation 857*4882a593Smuzhiyunto have reached a point at which it can start submitting ordinary operations 858*4882a593Smuzhiyunonce again:: 859*4882a593Smuzhiyun 860*4882a593Smuzhiyun void fscache_wait_on_invalidate(struct fscache_cookie *cookie); 861*4882a593Smuzhiyun 862*4882a593Smuzhiyun 863*4882a593SmuzhiyunFS-cache Specific Page Flag 864*4882a593Smuzhiyun=========================== 865*4882a593Smuzhiyun 866*4882a593SmuzhiyunFS-Cache makes use of a page flag, PG_private_2, for its own purpose. This is 867*4882a593Smuzhiyungiven the alternative name PG_fscache. 868*4882a593Smuzhiyun 869*4882a593SmuzhiyunPG_fscache is used to indicate that the page is known by the cache, and that 870*4882a593Smuzhiyunthe cache must be informed if the page is going to go away. It's an indication 871*4882a593Smuzhiyunto the netfs that the cache has an interest in this page, where an interest may 872*4882a593Smuzhiyunbe a pointer to it, resources allocated or reserved for it, or I/O in progress 873*4882a593Smuzhiyunupon it. 874*4882a593Smuzhiyun 875*4882a593SmuzhiyunThe netfs can use this information in methods such as releasepage() to 876*4882a593Smuzhiyundetermine whether it needs to uncache a page or update it. 877*4882a593Smuzhiyun 878*4882a593SmuzhiyunFurthermore, if this bit is set, releasepage() and invalidatepage() operations 879*4882a593Smuzhiyunwill be called on a page to get rid of it, even if PG_private is not set. This 880*4882a593Smuzhiyunallows caching to attempted on a page before read_cache_pages() to be called 881*4882a593Smuzhiyunafter fscache_read_or_alloc_pages() as the former will try and release pages it 882*4882a593Smuzhiyunwas given under certain circumstances. 883*4882a593Smuzhiyun 884*4882a593SmuzhiyunThis bit does not overlap with such as PG_private. This means that FS-Cache 885*4882a593Smuzhiyuncan be used with a filesystem that uses the block buffering code. 886*4882a593Smuzhiyun 887*4882a593SmuzhiyunThere are a number of operations defined on this flag:: 888*4882a593Smuzhiyun 889*4882a593Smuzhiyun int PageFsCache(struct page *page); 890*4882a593Smuzhiyun void SetPageFsCache(struct page *page) 891*4882a593Smuzhiyun void ClearPageFsCache(struct page *page) 892*4882a593Smuzhiyun int TestSetPageFsCache(struct page *page) 893*4882a593Smuzhiyun int TestClearPageFsCache(struct page *page) 894*4882a593Smuzhiyun 895*4882a593SmuzhiyunThese functions are bit test, bit set, bit clear, bit test and set and bit 896*4882a593Smuzhiyuntest and clear operations on PG_fscache. 897