0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 =================
0004 Cache Backend API
0005 =================
0006
0007 The FS-Cache system provides an API by which actual caches can be supplied to
0008 FS-Cache for it to then serve out to network filesystems and other interested
0009 parties. This API is used by::
0010
0011 #include <linux/fscache-cache.h>.
0012
0013
0014 Overview
0015 ========
0016
0017 Interaction with the API is handled on three levels: cache, volume and data
0018 storage, and each level has its own type of cookie object:
0019
0020 ======================= =======================
0021 COOKIE C TYPE
0022 ======================= =======================
0023 Cache cookie struct fscache_cache
0024 Volume cookie struct fscache_volume
0025 Data storage cookie struct fscache_cookie
0026 ======================= =======================
0027
0028 Cookies are used to provide some filesystem data to the cache, manage state and
0029 pin the cache during access in addition to acting as reference points for the
0030 API functions. Each cookie has a debugging ID that is included in trace points
0031 to make it easier to correlate traces. Note, though, that debugging IDs are
0032 simply allocated from incrementing counters and will eventually wrap.
0033
0034 The cache backend and the network filesystem can both ask for cache cookies -
0035 and if they ask for one of the same name, they'll get the same cookie. Volume
0036 and data cookies, however, are created at the behest of the filesystem only.
0037
0038
0039 Cache Cookies
0040 =============
0041
0042 Caches are represented in the API by cache cookies. These are objects of
0043 type::
0044
0045 struct fscache_cache {
0046 void *cache_priv;
0047 unsigned int debug_id;
0048 char *name;
0049 ...
0050 };
0051
0052 There are a few fields that the cache backend might be interested in. The
0053 ``debug_id`` can be used in tracing to match lines referring to the same cache
0054 and ``name`` is the name the cache was registered with. The ``cache_priv``
0055 member is private data provided by the cache when it is brought online. The
0056 other fields are for internal use.
0057
0058
0059 Registering a Cache
0060 ===================
0061
0062 When a cache backend wants to bring a cache online, it should first register
0063 the cache name and that will get it a cache cookie. This is done with::
0064
0065 struct fscache_cache *fscache_acquire_cache(const char *name);
0066
0067 This will look up and potentially create a cache cookie. The cache cookie may
0068 have already been created by a network filesystem looking for it, in which case
0069 that cache cookie will be used. If the cache cookie is not in use by another
0070 cache, it will be moved into the preparing state, otherwise it will return
0071 busy.
0072
0073 If successful, the cache backend can then start setting up the cache. In the
0074 event that the initialisation fails, the cache backend should call::
0075
0076 void fscache_relinquish_cache(struct fscache_cache *cache);
0077
0078 to reset and discard the cookie.
0079
0080
0081 Bringing a Cache Online
0082 =======================
0083
0084 Once the cache is set up, it can be brought online by calling::
0085
0086 int fscache_add_cache(struct fscache_cache *cache,
0087 const struct fscache_cache_ops *ops,
0088 void *cache_priv);
0089
0090 This stores the cache operations table pointer and cache private data into the
0091 cache cookie and moves the cache to the active state, thereby allowing accesses
0092 to take place.
0093
0094
0095 Withdrawing a Cache From Service
0096 ================================
0097
0098 The cache backend can withdraw a cache from service by calling this function::
0099
0100 void fscache_withdraw_cache(struct fscache_cache *cache);
0101
0102 This moves the cache to the withdrawn state to prevent new cache- and
0103 volume-level accesses from starting and then waits for outstanding cache-level
0104 accesses to complete.
0105
0106 The cache must then go through the data storage objects it has and tell fscache
0107 to withdraw them, calling::
0108
0109 void fscache_withdraw_cookie(struct fscache_cookie *cookie);
0110
0111 on the cookie that each object belongs to. This schedules the specified cookie
0112 for withdrawal. This gets offloaded to a workqueue. The cache backend can
0113 wait for completion by calling::
0114
0115 void fscache_wait_for_objects(struct fscache_cache *cache);
0116
0117 Once all the cookies are withdrawn, a cache backend can withdraw all the
0118 volumes, calling::
0119
0120 void fscache_withdraw_volume(struct fscache_volume *volume);
0121
0122 to tell fscache that a volume has been withdrawn. This waits for all
0123 outstanding accesses on the volume to complete before returning.
0124
0125 When the the cache is completely withdrawn, fscache should be notified by
0126 calling::
0127
0128 void fscache_relinquish_cache(struct fscache_cache *cache);
0129
0130 to clear fields in the cookie and discard the caller's ref on it.
0131
0132
0133 Volume Cookies
0134 ==============
0135
0136 Within a cache, the data storage objects are organised into logical volumes.
0137 These are represented in the API as objects of type::
0138
0139 struct fscache_volume {
0140 struct fscache_cache *cache;
0141 void *cache_priv;
0142 unsigned int debug_id;
0143 char *key;
0144 unsigned int key_hash;
0145 ...
0146 u8 coherency_len;
0147 u8 coherency[];
0148 };
0149
0150 There are a number of fields here that are of interest to the caching backend:
0151
0152 * ``cache`` - The parent cache cookie.
0153
0154 * ``cache_priv`` - A place for the cache to stash private data.
0155
0156 * ``debug_id`` - A debugging ID for logging in tracepoints.
0157
0158 * ``key`` - A printable string with no '/' characters in it that represents
0159 the index key for the volume. The key is NUL-terminated and padded out to
0160 a multiple of 4 bytes.
0161
0162 * ``key_hash`` - A hash of the index key. This should work out the same, no
0163 matter the cpu arch and endianness.
0164
0165 * ``coherency`` - A piece of coherency data that should be checked when the
0166 volume is bound to in the cache.
0167
0168 * ``coherency_len`` - The amount of data in the coherency buffer.
0169
0170
0171 Data Storage Cookies
0172 ====================
0173
0174 A volume is a logical group of data storage objects, each of which is
0175 represented to the network filesystem by a cookie. Cookies are represented in
0176 the API as objects of type::
0177
0178 struct fscache_cookie {
0179 struct fscache_volume *volume;
0180 void *cache_priv;
0181 unsigned long flags;
0182 unsigned int debug_id;
0183 unsigned int inval_counter;
0184 loff_t object_size;
0185 u8 advice;
0186 u32 key_hash;
0187 u8 key_len;
0188 u8 aux_len;
0189 ...
0190 };
0191
0192 The fields in the cookie that are of interest to the cache backend are:
0193
0194 * ``volume`` - The parent volume cookie.
0195
0196 * ``cache_priv`` - A place for the cache to stash private data.
0197
0198 * ``flags`` - A collection of bit flags, including:
0199
0200 * FSCACHE_COOKIE_NO_DATA_TO_READ - There is no data available in the
0201 cache to be read as the cookie has been created or invalidated.
0202
0203 * FSCACHE_COOKIE_NEEDS_UPDATE - The coherency data and/or object size has
0204 been changed and needs committing.
0205
0206 * FSCACHE_COOKIE_LOCAL_WRITE - The netfs's data has been modified
0207 locally, so the cache object may be in an incoherent state with respect
0208 to the server.
0209
0210 * FSCACHE_COOKIE_HAVE_DATA - The backend should set this if it
0211 successfully stores data into the cache.
0212
0213 * FSCACHE_COOKIE_RETIRED - The cookie was invalidated when it was
0214 relinquished and the cached data should be discarded.
0215
0216 * ``debug_id`` - A debugging ID for logging in tracepoints.
0217
0218 * ``inval_counter`` - The number of invalidations done on the cookie.
0219
0220 * ``advice`` - Information about how the cookie is to be used.
0221
0222 * ``key_hash`` - A hash of the index key. This should work out the same, no
0223 matter the cpu arch and endianness.
0224
0225 * ``key_len`` - The length of the index key.
0226
0227 * ``aux_len`` - The length of the coherency data buffer.
0228
0229 Each cookie has an index key, which may be stored inline to the cookie or
0230 elsewhere. A pointer to this can be obtained by calling::
0231
0232 void *fscache_get_key(struct fscache_cookie *cookie);
0233
0234 The index key is a binary blob, the storage for which is padded out to a
0235 multiple of 4 bytes.
0236
0237 Each cookie also has a buffer for coherency data. This may also be inline or
0238 detached from the cookie and a pointer is obtained by calling::
0239
0240 void *fscache_get_aux(struct fscache_cookie *cookie);
0241
0242
0243
0244 Cookie Accounting
0245 =================
0246
0247 Data storage cookies are counted and this is used to block cache withdrawal
0248 completion until all objects have been destroyed. The following functions are
0249 provided to the cache to deal with that::
0250
0251 void fscache_count_object(struct fscache_cache *cache);
0252 void fscache_uncount_object(struct fscache_cache *cache);
0253 void fscache_wait_for_objects(struct fscache_cache *cache);
0254
0255 The count function records the allocation of an object in a cache and the
0256 uncount function records its destruction. Warning: by the time the uncount
0257 function returns, the cache may have been destroyed.
0258
0259 The wait function can be used during the withdrawal procedure to wait for
0260 fscache to finish withdrawing all the objects in the cache. When it completes,
0261 there will be no remaining objects referring to the cache object or any volume
0262 objects.
0263
0264
0265 Cache Management API
0266 ====================
0267
0268 The cache backend implements the cache management API by providing a table of
0269 operations that fscache can use to manage various aspects of the cache. These
0270 are held in a structure of type::
0271
0272 struct fscache_cache_ops {
0273 const char *name;
0274 ...
0275 };
0276
0277 This contains a printable name for the cache backend driver plus a number of
0278 pointers to methods to allow fscache to request management of the cache:
0279
0280 * Set up a volume cookie [optional]::
0281
0282 void (*acquire_volume)(struct fscache_volume *volume);
0283
0284 This method is called when a volume cookie is being created. The caller
0285 holds a cache-level access pin to prevent the cache from going away for
0286 the duration. This method should set up the resources to access a volume
0287 in the cache and should not return until it has done so.
0288
0289 If successful, it can set ``cache_priv`` to its own data.
0290
0291
0292 * Clean up volume cookie [optional]::
0293
0294 void (*free_volume)(struct fscache_volume *volume);
0295
0296 This method is called when a volume cookie is being released if
0297 ``cache_priv`` is set.
0298
0299
0300 * Look up a cookie in the cache [mandatory]::
0301
0302 bool (*lookup_cookie)(struct fscache_cookie *cookie);
0303
0304 This method is called to look up/create the resources needed to access the
0305 data storage for a cookie. It is called from a worker thread with a
0306 volume-level access pin in the cache to prevent it from being withdrawn.
0307
0308 True should be returned if successful and false otherwise. If false is
0309 returned, the withdraw_cookie op (see below) will be called.
0310
0311 If lookup fails, but the object could still be created (e.g. it hasn't
0312 been cached before), then::
0313
0314 void fscache_cookie_lookup_negative(
0315 struct fscache_cookie *cookie);
0316
0317 can be called to let the network filesystem proceed and start downloading
0318 stuff whilst the cache backend gets on with the job of creating things.
0319
0320 If successful, ``cookie->cache_priv`` can be set.
0321
0322
0323 * Withdraw an object without any cookie access counts held [mandatory]::
0324
0325 void (*withdraw_cookie)(struct fscache_cookie *cookie);
0326
0327 This method is called to withdraw a cookie from service. It will be
0328 called when the cookie is relinquished by the netfs, withdrawn or culled
0329 by the cache backend or closed after a period of non-use by fscache.
0330
0331 The caller doesn't hold any access pins, but it is called from a
0332 non-reentrant work item to manage races between the various ways
0333 withdrawal can occur.
0334
0335 The cookie will have the ``FSCACHE_COOKIE_RETIRED`` flag set on it if the
0336 associated data is to be removed from the cache.
0337
0338
0339 * Change the size of a data storage object [mandatory]::
0340
0341 void (*resize_cookie)(struct netfs_cache_resources *cres,
0342 loff_t new_size);
0343
0344 This method is called to inform the cache backend of a change in size of
0345 the netfs file due to local truncation. The cache backend should make all
0346 of the changes it needs to make before returning as this is done under the
0347 netfs inode mutex.
0348
0349 The caller holds a cookie-level access pin to prevent a race with
0350 withdrawal and the netfs must have the cookie marked in-use to prevent
0351 garbage collection or culling from removing any resources.
0352
0353
0354 * Invalidate a data storage object [mandatory]::
0355
0356 bool (*invalidate_cookie)(struct fscache_cookie *cookie);
0357
0358 This is called when the network filesystem detects a third-party
0359 modification or when an O_DIRECT write is made locally. This requests
0360 that the cache backend should throw away all the data in the cache for
0361 this object and start afresh. It should return true if successful and
0362 false otherwise.
0363
0364 On entry, new I O/operations are blocked. Once the cache is in a position
0365 to accept I/O again, the backend should release the block by calling::
0366
0367 void fscache_resume_after_invalidation(struct fscache_cookie *cookie);
0368
0369 If the method returns false, caching will be withdrawn for this cookie.
0370
0371
0372 * Prepare to make local modifications to the cache [mandatory]::
0373
0374 void (*prepare_to_write)(struct fscache_cookie *cookie);
0375
0376 This method is called when the network filesystem finds that it is going
0377 to need to modify the contents of the cache due to local writes or
0378 truncations. This gives the cache a chance to note that a cache object
0379 may be incoherent with respect to the server and may need writing back
0380 later. This may also cause the cached data to be scrapped on later
0381 rebinding if not properly committed.
0382
0383
0384 * Begin an operation for the netfs lib [mandatory]::
0385
0386 bool (*begin_operation)(struct netfs_cache_resources *cres,
0387 enum fscache_want_state want_state);
0388
0389 This method is called when an I/O operation is being set up (read, write
0390 or resize). The caller holds an access pin on the cookie and must have
0391 marked the cookie as in-use.
0392
0393 If it can, the backend should attach any resources it needs to keep around
0394 to the netfs_cache_resources object and return true.
0395
0396 If it can't complete the setup, it should return false.
0397
0398 The want_state parameter indicates the state the caller needs the cache
0399 object to be in and what it wants to do during the operation:
0400
0401 * ``FSCACHE_WANT_PARAMS`` - The caller just wants to access cache
0402 object parameters; it doesn't need to do data I/O yet.
0403
0404 * ``FSCACHE_WANT_READ`` - The caller wants to read data.
0405
0406 * ``FSCACHE_WANT_WRITE`` - The caller wants to write to or resize the
0407 cache object.
0408
0409 Note that there won't necessarily be anything attached to the cookie's
0410 cache_priv yet if the cookie is still being created.
0411
0412
0413 Data I/O API
0414 ============
0415
0416 A cache backend provides a data I/O API by through the netfs library's ``struct
0417 netfs_cache_ops`` attached to a ``struct netfs_cache_resources`` by the
0418 ``begin_operation`` method described above.
0419
0420 See the Documentation/filesystems/netfs_library.rst for a description.
0421
0422
0423 Miscellaneous Functions
0424 =======================
0425
0426 FS-Cache provides some utilities that a cache backend may make use of:
0427
0428 * Note occurrence of an I/O error in a cache::
0429
0430 void fscache_io_error(struct fscache_cache *cache);
0431
0432 This tells FS-Cache that an I/O error occurred in the cache. This
0433 prevents any new I/O from being started on the cache.
0434
0435 This does not actually withdraw the cache. That must be done separately.
0436
0437 * Note cessation of caching on a cookie due to failure::
0438
0439 void fscache_caching_failed(struct fscache_cookie *cookie);
0440
0441 This notes that a the caching that was being done on a cookie failed in
0442 some way, for instance the backing storage failed to be created or
0443 invalidation failed and that no further I/O operations should take place
0444 on it until the cache is reset.
0445
0446 * Count I/O requests::
0447
0448 void fscache_count_read(void);
0449 void fscache_count_write(void);
0450
0451 These record reads and writes from/to the cache. The numbers are
0452 displayed in /proc/fs/fscache/stats.
0453
0454 * Count out-of-space errors::
0455
0456 void fscache_count_no_write_space(void);
0457 void fscache_count_no_create_space(void);
0458
0459 These record ENOSPC errors in the cache, divided into failures of data
0460 writes and failures of filesystem object creations (e.g. mkdir).
0461
0462 * Count objects culled::
0463
0464 void fscache_count_culled(void);
0465
0466 This records the culling of an object.
0467
0468 * Get the cookie from a set of cache resources::
0469
0470 struct fscache_cookie *fscache_cres_cookie(struct netfs_cache_resources *cres)
0471
0472 Pull a pointer to the cookie from the cache resources. This may return a
0473 NULL cookie if no cookie was set.
0474
0475
0476 API Function Reference
0477 ======================
0478
0479 .. kernel-doc:: include/linux/fscache-cache.h