12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
22d6fff63SDavid Howells /* General filesystem caching interface
32d6fff63SDavid Howells *
41e1236b8SDavid Howells * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
52d6fff63SDavid Howells * Written by David Howells ([email protected])
62d6fff63SDavid Howells *
72d6fff63SDavid Howells * NOTE!!! See:
82d6fff63SDavid Howells *
9efc930faSMauro Carvalho Chehab * Documentation/filesystems/caching/netfs-api.rst
102d6fff63SDavid Howells *
112d6fff63SDavid Howells * for a description of the network filesystem interface declared here.
122d6fff63SDavid Howells */
132d6fff63SDavid Howells
142d6fff63SDavid Howells #ifndef _LINUX_FSCACHE_H
152d6fff63SDavid Howells #define _LINUX_FSCACHE_H
162d6fff63SDavid Howells
172d6fff63SDavid Howells #include <linux/fs.h>
18b533a83fSDavid Howells #include <linux/netfs.h>
1908276bdaSDavid Howells #include <linux/writeback.h>
202d6fff63SDavid Howells
212d6fff63SDavid Howells #if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
221e1236b8SDavid Howells #define __fscache_available (1)
231e1236b8SDavid Howells #define fscache_available() (1)
2462ab6335SDavid Howells #define fscache_volume_valid(volume) (volume)
252d6fff63SDavid Howells #define fscache_cookie_valid(cookie) (cookie)
2612bb21a2SDavid Howells #define fscache_resources_valid(cres) ((cres)->cache_priv)
2712bb21a2SDavid Howells #define fscache_cookie_enabled(cookie) (cookie && !test_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
282d6fff63SDavid Howells #else
291e1236b8SDavid Howells #define __fscache_available (0)
301e1236b8SDavid Howells #define fscache_available() (0)
3162ab6335SDavid Howells #define fscache_volume_valid(volume) (0)
322d6fff63SDavid Howells #define fscache_cookie_valid(cookie) (0)
3312bb21a2SDavid Howells #define fscache_resources_valid(cres) (false)
342cee6fbbSDavid Howells #define fscache_cookie_enabled(cookie) (0)
352d6fff63SDavid Howells #endif
362d6fff63SDavid Howells
377f3283abSDavid Howells struct fscache_cookie;
387f3283abSDavid Howells
397f3283abSDavid Howells #define FSCACHE_ADV_SINGLE_CHUNK 0x01 /* The object is a single chunk of data */
407f3283abSDavid Howells #define FSCACHE_ADV_WRITE_CACHE 0x00 /* Do cache if written to locally */
417f3283abSDavid Howells #define FSCACHE_ADV_WRITE_NOCACHE 0x02 /* Don't cache if written to locally */
42c8383054SJeffle Xu #define FSCACHE_ADV_WANT_CACHE_SIZE 0x04 /* Retrieve cache size at runtime */
437f3283abSDavid Howells
44d24af13eSDavid Howells #define FSCACHE_INVAL_DIO_WRITE 0x01 /* Invalidate due to DIO write */
45d24af13eSDavid Howells
46d64f4554SDavid Howells enum fscache_want_state {
47d64f4554SDavid Howells FSCACHE_WANT_PARAMS,
48d64f4554SDavid Howells FSCACHE_WANT_WRITE,
49d64f4554SDavid Howells FSCACHE_WANT_READ,
50d64f4554SDavid Howells };
51d64f4554SDavid Howells
527f3283abSDavid Howells /*
537f3283abSDavid Howells * Data object state.
547f3283abSDavid Howells */
557f3283abSDavid Howells enum fscache_cookie_state {
567f3283abSDavid Howells FSCACHE_COOKIE_STATE_QUIESCENT, /* The cookie is uncached */
577f3283abSDavid Howells FSCACHE_COOKIE_STATE_LOOKING_UP, /* The cache object is being looked up */
587f3283abSDavid Howells FSCACHE_COOKIE_STATE_CREATING, /* The cache object is being created */
597f3283abSDavid Howells FSCACHE_COOKIE_STATE_ACTIVE, /* The cache is active, readable and writable */
60d24af13eSDavid Howells FSCACHE_COOKIE_STATE_INVALIDATING, /* The cache is being invalidated */
617f3283abSDavid Howells FSCACHE_COOKIE_STATE_FAILED, /* The cache failed, withdraw to clear */
6212bb21a2SDavid Howells FSCACHE_COOKIE_STATE_LRU_DISCARDING, /* The cookie is being discarded by the LRU */
637f3283abSDavid Howells FSCACHE_COOKIE_STATE_WITHDRAWING, /* The cookie is being withdrawn */
647f3283abSDavid Howells FSCACHE_COOKIE_STATE_RELINQUISHING, /* The cookie is being relinquished */
657f3283abSDavid Howells FSCACHE_COOKIE_STATE_DROPPED, /* The cookie has been dropped */
667f3283abSDavid Howells #define FSCACHE_COOKIE_STATE__NR (FSCACHE_COOKIE_STATE_DROPPED + 1)
677f3283abSDavid Howells } __attribute__((mode(byte)));
687f3283abSDavid Howells
6962ab6335SDavid Howells /*
7062ab6335SDavid Howells * Volume representation cookie.
7162ab6335SDavid Howells */
7262ab6335SDavid Howells struct fscache_volume {
7362ab6335SDavid Howells refcount_t ref;
7462ab6335SDavid Howells atomic_t n_cookies; /* Number of data cookies in volume */
7562ab6335SDavid Howells atomic_t n_accesses; /* Number of cache accesses in progress */
7662ab6335SDavid Howells unsigned int debug_id;
7762ab6335SDavid Howells unsigned int key_hash; /* Hash of key string */
789f0933acSDavid Howells u8 *key; /* Volume ID, eg. "[email protected]@1234" */
7962ab6335SDavid Howells struct list_head proc_link; /* Link in /proc/fs/fscache/volumes */
8062ab6335SDavid Howells struct hlist_bl_node hash_link; /* Link in hash table */
8162ab6335SDavid Howells struct work_struct work;
8262ab6335SDavid Howells struct fscache_cache *cache; /* The cache in which this resides */
8362ab6335SDavid Howells void *cache_priv; /* Cache private data */
8462ab6335SDavid Howells spinlock_t lock;
8562ab6335SDavid Howells unsigned long flags;
8662ab6335SDavid Howells #define FSCACHE_VOLUME_RELINQUISHED 0 /* Volume is being cleaned up */
8762ab6335SDavid Howells #define FSCACHE_VOLUME_INVALIDATE 1 /* Volume was invalidated */
8862ab6335SDavid Howells #define FSCACHE_VOLUME_COLLIDED_WITH 2 /* Volume was collided with */
8962ab6335SDavid Howells #define FSCACHE_VOLUME_ACQUIRE_PENDING 3 /* Volume is waiting to complete acquisition */
9062ab6335SDavid Howells #define FSCACHE_VOLUME_CREATING 4 /* Volume is being created on disk */
9132e15003SDavid Howells u8 coherency_len; /* Length of the coherency data */
9232e15003SDavid Howells u8 coherency[]; /* Coherency data */
9362ab6335SDavid Howells };
9462ab6335SDavid Howells
9562ab6335SDavid Howells /*
967f3283abSDavid Howells * Data file representation cookie.
977f3283abSDavid Howells * - a file will only appear in one cache
987f3283abSDavid Howells * - a request to cache a file may or may not be honoured, subject to
997f3283abSDavid Howells * constraints such as disk space
1007f3283abSDavid Howells * - indices are created on disk just-in-time
1017f3283abSDavid Howells */
1027f3283abSDavid Howells struct fscache_cookie {
1037f3283abSDavid Howells refcount_t ref;
1047f3283abSDavid Howells atomic_t n_active; /* number of active users of cookie */
1057f3283abSDavid Howells atomic_t n_accesses; /* Number of cache accesses in progress */
1067f3283abSDavid Howells unsigned int debug_id;
1077f3283abSDavid Howells unsigned int inval_counter; /* Number of invalidations made */
1087f3283abSDavid Howells spinlock_t lock;
1097f3283abSDavid Howells struct fscache_volume *volume; /* Parent volume of this file. */
1107f3283abSDavid Howells void *cache_priv; /* Cache-side representation */
1117f3283abSDavid Howells struct hlist_bl_node hash_link; /* Link in hash table */
1127f3283abSDavid Howells struct list_head proc_link; /* Link in proc list */
1137f3283abSDavid Howells struct list_head commit_link; /* Link in commit queue */
1147f3283abSDavid Howells struct work_struct work; /* Commit/relinq/withdraw work */
1157f3283abSDavid Howells loff_t object_size; /* Size of the netfs object */
1167f3283abSDavid Howells unsigned long unused_at; /* Time at which unused (jiffies) */
1177f3283abSDavid Howells unsigned long flags;
1187f3283abSDavid Howells #define FSCACHE_COOKIE_RELINQUISHED 0 /* T if cookie has been relinquished */
1197f3283abSDavid Howells #define FSCACHE_COOKIE_RETIRED 1 /* T if this cookie has retired on relinq */
1207f3283abSDavid Howells #define FSCACHE_COOKIE_IS_CACHING 2 /* T if this cookie is cached */
1217f3283abSDavid Howells #define FSCACHE_COOKIE_NO_DATA_TO_READ 3 /* T if this cookie has nothing to read */
1227f3283abSDavid Howells #define FSCACHE_COOKIE_NEEDS_UPDATE 4 /* T if attrs have been updated */
1237f3283abSDavid Howells #define FSCACHE_COOKIE_HAS_BEEN_CACHED 5 /* T if cookie needs withdraw-on-relinq */
1247f3283abSDavid Howells #define FSCACHE_COOKIE_DISABLED 6 /* T if cookie has been disabled */
1257f3283abSDavid Howells #define FSCACHE_COOKIE_LOCAL_WRITE 7 /* T if cookie has been modified locally */
1267f3283abSDavid Howells #define FSCACHE_COOKIE_NO_ACCESS_WAKE 8 /* T if no wake when n_accesses goes 0 */
1277f3283abSDavid Howells #define FSCACHE_COOKIE_DO_RELINQUISH 9 /* T if this cookie needs relinquishment */
1287f3283abSDavid Howells #define FSCACHE_COOKIE_DO_WITHDRAW 10 /* T if this cookie needs withdrawing */
1297f3283abSDavid Howells #define FSCACHE_COOKIE_DO_LRU_DISCARD 11 /* T if this cookie needs LRU discard */
1307f3283abSDavid Howells #define FSCACHE_COOKIE_DO_PREP_TO_WRITE 12 /* T if cookie needs write preparation */
1317f3283abSDavid Howells #define FSCACHE_COOKIE_HAVE_DATA 13 /* T if this cookie has data stored */
1327f3283abSDavid Howells #define FSCACHE_COOKIE_IS_HASHED 14 /* T if this cookie is hashed */
13385e4ea10SDavid Howells #define FSCACHE_COOKIE_DO_INVALIDATE 15 /* T if cookie needs invalidation */
1347f3283abSDavid Howells
1357f3283abSDavid Howells enum fscache_cookie_state state;
1367f3283abSDavid Howells u8 advice; /* FSCACHE_ADV_* */
1377f3283abSDavid Howells u8 key_len; /* Length of index key */
1387f3283abSDavid Howells u8 aux_len; /* Length of auxiliary data */
1397f3283abSDavid Howells u32 key_hash; /* Hash of volume, key, len */
1407f3283abSDavid Howells union {
1417f3283abSDavid Howells void *key; /* Index key */
1427f3283abSDavid Howells u8 inline_key[16]; /* - If the key is short enough */
1437f3283abSDavid Howells };
1447f3283abSDavid Howells union {
1457f3283abSDavid Howells void *aux; /* Auxiliary data */
1467f3283abSDavid Howells u8 inline_aux[8]; /* - If the aux data is short enough */
1477f3283abSDavid Howells };
1487f3283abSDavid Howells };
1497f3283abSDavid Howells
1507f3283abSDavid Howells /*
15162ab6335SDavid Howells * slow-path functions for when there is actually caching available, and the
15262ab6335SDavid Howells * netfs does actually have a valid token
15362ab6335SDavid Howells * - these are not to be called directly
15462ab6335SDavid Howells * - these are undefined symbols when FS-Cache is not configured and the
15562ab6335SDavid Howells * optimiser takes care of not using them
15662ab6335SDavid Howells */
15762ab6335SDavid Howells extern struct fscache_volume *__fscache_acquire_volume(const char *, const char *,
15862ab6335SDavid Howells const void *, size_t);
15962ab6335SDavid Howells extern void __fscache_relinquish_volume(struct fscache_volume *, const void *, bool);
16062ab6335SDavid Howells
1617f3283abSDavid Howells extern struct fscache_cookie *__fscache_acquire_cookie(
1627f3283abSDavid Howells struct fscache_volume *,
1637f3283abSDavid Howells u8,
1647f3283abSDavid Howells const void *, size_t,
1657f3283abSDavid Howells const void *, size_t,
1667f3283abSDavid Howells loff_t);
16712bb21a2SDavid Howells extern void __fscache_use_cookie(struct fscache_cookie *, bool);
16812bb21a2SDavid Howells extern void __fscache_unuse_cookie(struct fscache_cookie *, const void *, const loff_t *);
1697f3283abSDavid Howells extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
17016a96bdfSDavid Howells extern void __fscache_resize_cookie(struct fscache_cookie *, loff_t);
171d24af13eSDavid Howells extern void __fscache_invalidate(struct fscache_cookie *, const void *, loff_t, unsigned int);
172d64f4554SDavid Howells extern int __fscache_begin_read_operation(struct netfs_cache_resources *, struct fscache_cookie *);
17316f2f4e6SDavid Howells extern int __fscache_begin_write_operation(struct netfs_cache_resources *, struct fscache_cookie *);
1747f3283abSDavid Howells
175*2ff1e975SDavid Howells void __fscache_write_to_cache(struct fscache_cookie *cookie,
176*2ff1e975SDavid Howells struct address_space *mapping,
177*2ff1e975SDavid Howells loff_t start, size_t len, loff_t i_size,
178*2ff1e975SDavid Howells netfs_io_terminated_t term_func,
179*2ff1e975SDavid Howells void *term_func_priv,
180*2ff1e975SDavid Howells bool using_pgpriv2, bool cond);
181b6e16652SDavid Howells extern void __fscache_clear_page_bits(struct address_space *, loff_t, size_t);
182b6e16652SDavid Howells
18362ab6335SDavid Howells /**
18462ab6335SDavid Howells * fscache_acquire_volume - Register a volume as desiring caching services
18562ab6335SDavid Howells * @volume_key: An identification string for the volume
18662ab6335SDavid Howells * @cache_name: The name of the cache to use (or NULL for the default)
18762ab6335SDavid Howells * @coherency_data: Piece of arbitrary coherency data to check (or NULL)
18862ab6335SDavid Howells * @coherency_len: The size of the coherency data
18962ab6335SDavid Howells *
19062ab6335SDavid Howells * Register a volume as desiring caching services if they're available. The
19162ab6335SDavid Howells * caller must provide an identifier for the volume and may also indicate which
19262ab6335SDavid Howells * cache it should be in. If a preexisting volume entry is found in the cache,
19362ab6335SDavid Howells * the coherency data must match otherwise the entry will be invalidated.
19462ab6335SDavid Howells *
19562ab6335SDavid Howells * Returns a cookie pointer on success, -ENOMEM if out of memory or -EBUSY if a
19662ab6335SDavid Howells * cache volume of that name is already acquired. Note that "NULL" is a valid
19762ab6335SDavid Howells * cookie pointer and can be returned if caching is refused.
19862ab6335SDavid Howells */
19962ab6335SDavid Howells static inline
fscache_acquire_volume(const char * volume_key,const char * cache_name,const void * coherency_data,size_t coherency_len)20062ab6335SDavid Howells struct fscache_volume *fscache_acquire_volume(const char *volume_key,
20162ab6335SDavid Howells const char *cache_name,
20262ab6335SDavid Howells const void *coherency_data,
20362ab6335SDavid Howells size_t coherency_len)
20462ab6335SDavid Howells {
20562ab6335SDavid Howells if (!fscache_available())
20662ab6335SDavid Howells return NULL;
20762ab6335SDavid Howells return __fscache_acquire_volume(volume_key, cache_name,
20862ab6335SDavid Howells coherency_data, coherency_len);
20962ab6335SDavid Howells }
21062ab6335SDavid Howells
21162ab6335SDavid Howells /**
21262ab6335SDavid Howells * fscache_relinquish_volume - Cease caching a volume
21362ab6335SDavid Howells * @volume: The volume cookie
21462ab6335SDavid Howells * @coherency_data: Piece of arbitrary coherency data to set (or NULL)
21562ab6335SDavid Howells * @invalidate: True if the volume should be invalidated
21662ab6335SDavid Howells *
21762ab6335SDavid Howells * Indicate that a filesystem no longer desires caching services for a volume.
21862ab6335SDavid Howells * The caller must have relinquished all file cookies prior to calling this.
21962ab6335SDavid Howells * The stored coherency data is updated.
22062ab6335SDavid Howells */
22162ab6335SDavid Howells static inline
fscache_relinquish_volume(struct fscache_volume * volume,const void * coherency_data,bool invalidate)22262ab6335SDavid Howells void fscache_relinquish_volume(struct fscache_volume *volume,
22362ab6335SDavid Howells const void *coherency_data,
22462ab6335SDavid Howells bool invalidate)
22562ab6335SDavid Howells {
22662ab6335SDavid Howells if (fscache_volume_valid(volume))
22762ab6335SDavid Howells __fscache_relinquish_volume(volume, coherency_data, invalidate);
22862ab6335SDavid Howells }
22962ab6335SDavid Howells
2307f3283abSDavid Howells /**
2317f3283abSDavid Howells * fscache_acquire_cookie - Acquire a cookie to represent a cache object
2327f3283abSDavid Howells * @volume: The volume in which to locate/create this cookie
2337f3283abSDavid Howells * @advice: Advice flags (FSCACHE_COOKIE_ADV_*)
2347f3283abSDavid Howells * @index_key: The index key for this cookie
2357f3283abSDavid Howells * @index_key_len: Size of the index key
2367f3283abSDavid Howells * @aux_data: The auxiliary data for the cookie (may be NULL)
2377f3283abSDavid Howells * @aux_data_len: Size of the auxiliary data buffer
2387f3283abSDavid Howells * @object_size: The initial size of object
2397f3283abSDavid Howells *
2407f3283abSDavid Howells * Acquire a cookie to represent a data file within the given cache volume.
2417f3283abSDavid Howells *
2427f3283abSDavid Howells * See Documentation/filesystems/caching/netfs-api.rst for a complete
2437f3283abSDavid Howells * description.
2447f3283abSDavid Howells */
2457f3283abSDavid Howells static inline
fscache_acquire_cookie(struct fscache_volume * volume,u8 advice,const void * index_key,size_t index_key_len,const void * aux_data,size_t aux_data_len,loff_t object_size)2467f3283abSDavid Howells struct fscache_cookie *fscache_acquire_cookie(struct fscache_volume *volume,
2477f3283abSDavid Howells u8 advice,
2487f3283abSDavid Howells const void *index_key,
2497f3283abSDavid Howells size_t index_key_len,
2507f3283abSDavid Howells const void *aux_data,
2517f3283abSDavid Howells size_t aux_data_len,
2527f3283abSDavid Howells loff_t object_size)
2537f3283abSDavid Howells {
2547f3283abSDavid Howells if (!fscache_volume_valid(volume))
2557f3283abSDavid Howells return NULL;
2567f3283abSDavid Howells return __fscache_acquire_cookie(volume, advice,
2577f3283abSDavid Howells index_key, index_key_len,
2587f3283abSDavid Howells aux_data, aux_data_len,
2597f3283abSDavid Howells object_size);
2607f3283abSDavid Howells }
2617f3283abSDavid Howells
2627f3283abSDavid Howells /**
26312bb21a2SDavid Howells * fscache_use_cookie - Request usage of cookie attached to an object
264ec1bd371SKhalid Masum * @cookie: The cookie representing the cache object
26512bb21a2SDavid Howells * @will_modify: If cache is expected to be modified locally
26612bb21a2SDavid Howells *
26712bb21a2SDavid Howells * Request usage of the cookie attached to an object. The caller should tell
26812bb21a2SDavid Howells * the cache if the object's contents are about to be modified locally and then
26912bb21a2SDavid Howells * the cache can apply the policy that has been set to handle this case.
27012bb21a2SDavid Howells */
fscache_use_cookie(struct fscache_cookie * cookie,bool will_modify)27112bb21a2SDavid Howells static inline void fscache_use_cookie(struct fscache_cookie *cookie,
27212bb21a2SDavid Howells bool will_modify)
27312bb21a2SDavid Howells {
27412bb21a2SDavid Howells if (fscache_cookie_valid(cookie))
27512bb21a2SDavid Howells __fscache_use_cookie(cookie, will_modify);
27612bb21a2SDavid Howells }
27712bb21a2SDavid Howells
27812bb21a2SDavid Howells /**
27912bb21a2SDavid Howells * fscache_unuse_cookie - Cease usage of cookie attached to an object
280ec1bd371SKhalid Masum * @cookie: The cookie representing the cache object
28112bb21a2SDavid Howells * @aux_data: Updated auxiliary data (or NULL)
28212bb21a2SDavid Howells * @object_size: Revised size of the object (or NULL)
28312bb21a2SDavid Howells *
28412bb21a2SDavid Howells * Cease usage of the cookie attached to an object. When the users count
28512bb21a2SDavid Howells * reaches zero then the cookie relinquishment will be permitted to proceed.
28612bb21a2SDavid Howells */
fscache_unuse_cookie(struct fscache_cookie * cookie,const void * aux_data,const loff_t * object_size)28712bb21a2SDavid Howells static inline void fscache_unuse_cookie(struct fscache_cookie *cookie,
28812bb21a2SDavid Howells const void *aux_data,
28912bb21a2SDavid Howells const loff_t *object_size)
29012bb21a2SDavid Howells {
29112bb21a2SDavid Howells if (fscache_cookie_valid(cookie))
29212bb21a2SDavid Howells __fscache_unuse_cookie(cookie, aux_data, object_size);
29312bb21a2SDavid Howells }
29412bb21a2SDavid Howells
29512bb21a2SDavid Howells /**
2967f3283abSDavid Howells * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
2977f3283abSDavid Howells * it
2987f3283abSDavid Howells * @cookie: The cookie being returned
2997f3283abSDavid Howells * @retire: True if the cache object the cookie represents is to be discarded
3007f3283abSDavid Howells *
3017f3283abSDavid Howells * This function returns a cookie to the cache, forcibly discarding the
3027f3283abSDavid Howells * associated cache object if retire is set to true.
3037f3283abSDavid Howells *
3047f3283abSDavid Howells * See Documentation/filesystems/caching/netfs-api.rst for a complete
3057f3283abSDavid Howells * description.
3067f3283abSDavid Howells */
3077f3283abSDavid Howells static inline
fscache_relinquish_cookie(struct fscache_cookie * cookie,bool retire)3087f3283abSDavid Howells void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
3097f3283abSDavid Howells {
3107f3283abSDavid Howells if (fscache_cookie_valid(cookie))
3117f3283abSDavid Howells __fscache_relinquish_cookie(cookie, retire);
3127f3283abSDavid Howells }
3137f3283abSDavid Howells
31412bb21a2SDavid Howells /*
31512bb21a2SDavid Howells * Find the auxiliary data on a cookie.
31612bb21a2SDavid Howells */
fscache_get_aux(struct fscache_cookie * cookie)31712bb21a2SDavid Howells static inline void *fscache_get_aux(struct fscache_cookie *cookie)
31812bb21a2SDavid Howells {
31912bb21a2SDavid Howells if (cookie->aux_len <= sizeof(cookie->inline_aux))
32012bb21a2SDavid Howells return cookie->inline_aux;
32112bb21a2SDavid Howells else
32212bb21a2SDavid Howells return cookie->aux;
32312bb21a2SDavid Howells }
32412bb21a2SDavid Howells
32512bb21a2SDavid Howells /*
32612bb21a2SDavid Howells * Update the auxiliary data on a cookie.
32712bb21a2SDavid Howells */
32812bb21a2SDavid Howells static inline
fscache_update_aux(struct fscache_cookie * cookie,const void * aux_data,const loff_t * object_size)32912bb21a2SDavid Howells void fscache_update_aux(struct fscache_cookie *cookie,
33012bb21a2SDavid Howells const void *aux_data, const loff_t *object_size)
33112bb21a2SDavid Howells {
33212bb21a2SDavid Howells void *p = fscache_get_aux(cookie);
33312bb21a2SDavid Howells
33412bb21a2SDavid Howells if (aux_data && p)
33512bb21a2SDavid Howells memcpy(p, aux_data, cookie->aux_len);
33612bb21a2SDavid Howells if (object_size)
33712bb21a2SDavid Howells cookie->object_size = *object_size;
33812bb21a2SDavid Howells }
33912bb21a2SDavid Howells
34012bb21a2SDavid Howells #ifdef CONFIG_FSCACHE_STATS
34112bb21a2SDavid Howells extern atomic_t fscache_n_updates;
34212bb21a2SDavid Howells #endif
34312bb21a2SDavid Howells
34412bb21a2SDavid Howells static inline
__fscache_update_cookie(struct fscache_cookie * cookie,const void * aux_data,const loff_t * object_size)34512bb21a2SDavid Howells void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
34612bb21a2SDavid Howells const loff_t *object_size)
34712bb21a2SDavid Howells {
34812bb21a2SDavid Howells #ifdef CONFIG_FSCACHE_STATS
34912bb21a2SDavid Howells atomic_inc(&fscache_n_updates);
35012bb21a2SDavid Howells #endif
35112bb21a2SDavid Howells fscache_update_aux(cookie, aux_data, object_size);
35212bb21a2SDavid Howells smp_wmb();
35312bb21a2SDavid Howells set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
35412bb21a2SDavid Howells }
35512bb21a2SDavid Howells
356d24af13eSDavid Howells /**
357ed1235ebSDavid Howells * fscache_update_cookie - Request that a cache object be updated
358ed1235ebSDavid Howells * @cookie: The cookie representing the cache object
359ed1235ebSDavid Howells * @aux_data: The updated auxiliary data for the cookie (may be NULL)
360ed1235ebSDavid Howells * @object_size: The current size of the object (may be NULL)
361ed1235ebSDavid Howells *
362ed1235ebSDavid Howells * Request an update of the index data for the cache object associated with the
363ed1235ebSDavid Howells * cookie. The auxiliary data on the cookie will be updated first if @aux_data
364ed1235ebSDavid Howells * is set and the object size will be updated and the object possibly trimmed
365ed1235ebSDavid Howells * if @object_size is set.
366ed1235ebSDavid Howells *
367ed1235ebSDavid Howells * See Documentation/filesystems/caching/netfs-api.rst for a complete
368ed1235ebSDavid Howells * description.
369ed1235ebSDavid Howells */
370ed1235ebSDavid Howells static inline
fscache_update_cookie(struct fscache_cookie * cookie,const void * aux_data,const loff_t * object_size)371ed1235ebSDavid Howells void fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
372ed1235ebSDavid Howells const loff_t *object_size)
373ed1235ebSDavid Howells {
374ed1235ebSDavid Howells if (fscache_cookie_enabled(cookie))
375ed1235ebSDavid Howells __fscache_update_cookie(cookie, aux_data, object_size);
376ed1235ebSDavid Howells }
377ed1235ebSDavid Howells
378ed1235ebSDavid Howells /**
37916a96bdfSDavid Howells * fscache_resize_cookie - Request that a cache object be resized
38016a96bdfSDavid Howells * @cookie: The cookie representing the cache object
38116a96bdfSDavid Howells * @new_size: The new size of the object (may be NULL)
38216a96bdfSDavid Howells *
38316a96bdfSDavid Howells * Request that the size of an object be changed.
38416a96bdfSDavid Howells *
385752f5963SMauro Carvalho Chehab * See Documentation/filesystems/caching/netfs-api.rst for a complete
38616a96bdfSDavid Howells * description.
38716a96bdfSDavid Howells */
38816a96bdfSDavid Howells static inline
fscache_resize_cookie(struct fscache_cookie * cookie,loff_t new_size)38916a96bdfSDavid Howells void fscache_resize_cookie(struct fscache_cookie *cookie, loff_t new_size)
39016a96bdfSDavid Howells {
39116a96bdfSDavid Howells if (fscache_cookie_enabled(cookie))
39216a96bdfSDavid Howells __fscache_resize_cookie(cookie, new_size);
39316a96bdfSDavid Howells }
39416a96bdfSDavid Howells
39516a96bdfSDavid Howells /**
396d24af13eSDavid Howells * fscache_invalidate - Notify cache that an object needs invalidation
397d24af13eSDavid Howells * @cookie: The cookie representing the cache object
398d24af13eSDavid Howells * @aux_data: The updated auxiliary data for the cookie (may be NULL)
399d24af13eSDavid Howells * @size: The revised size of the object.
400d24af13eSDavid Howells * @flags: Invalidation flags (FSCACHE_INVAL_*)
401d24af13eSDavid Howells *
402d24af13eSDavid Howells * Notify the cache that an object is needs to be invalidated and that it
403d24af13eSDavid Howells * should abort any retrievals or stores it is doing on the cache. This
404d24af13eSDavid Howells * increments inval_counter on the cookie which can be used by the caller to
405d24af13eSDavid Howells * reconsider I/O requests as they complete.
406d24af13eSDavid Howells *
407d24af13eSDavid Howells * If @flags has FSCACHE_INVAL_DIO_WRITE set, this indicates that this is due
408d24af13eSDavid Howells * to a direct I/O write and will cause caching to be disabled on this cookie
409d24af13eSDavid Howells * until it is completely unused.
410d24af13eSDavid Howells *
411d24af13eSDavid Howells * See Documentation/filesystems/caching/netfs-api.rst for a complete
412d24af13eSDavid Howells * description.
413d24af13eSDavid Howells */
414d24af13eSDavid Howells static inline
fscache_invalidate(struct fscache_cookie * cookie,const void * aux_data,loff_t size,unsigned int flags)415d24af13eSDavid Howells void fscache_invalidate(struct fscache_cookie *cookie,
416d24af13eSDavid Howells const void *aux_data, loff_t size, unsigned int flags)
417d24af13eSDavid Howells {
418d24af13eSDavid Howells if (fscache_cookie_enabled(cookie))
419d24af13eSDavid Howells __fscache_invalidate(cookie, aux_data, size, flags);
420d24af13eSDavid Howells }
421d24af13eSDavid Howells
422d64f4554SDavid Howells /**
423d64f4554SDavid Howells * fscache_operation_valid - Return true if operations resources are usable
424d64f4554SDavid Howells * @cres: The resources to check.
425d64f4554SDavid Howells *
426d64f4554SDavid Howells * Returns a pointer to the operations table if usable or NULL if not.
427d64f4554SDavid Howells */
428d64f4554SDavid Howells static inline
fscache_operation_valid(const struct netfs_cache_resources * cres)429d64f4554SDavid Howells const struct netfs_cache_ops *fscache_operation_valid(const struct netfs_cache_resources *cres)
430d64f4554SDavid Howells {
431d64f4554SDavid Howells return fscache_resources_valid(cres) ? cres->ops : NULL;
432d64f4554SDavid Howells }
433d64f4554SDavid Howells
434d64f4554SDavid Howells /**
435d64f4554SDavid Howells * fscache_begin_read_operation - Begin a read operation for the netfs lib
436d64f4554SDavid Howells * @cres: The cache resources for the read being performed
437d64f4554SDavid Howells * @cookie: The cookie representing the cache object
438d64f4554SDavid Howells *
439d64f4554SDavid Howells * Begin a read operation on behalf of the netfs helper library. @cres
440d64f4554SDavid Howells * indicates the cache resources to which the operation state should be
441d64f4554SDavid Howells * attached; @cookie indicates the cache object that will be accessed.
442d64f4554SDavid Howells *
443d64f4554SDavid Howells * @cres->inval_counter is set from @cookie->inval_counter for comparison at
444d64f4554SDavid Howells * the end of the operation. This allows invalidation during the operation to
445d64f4554SDavid Howells * be detected by the caller.
446d64f4554SDavid Howells *
447d64f4554SDavid Howells * Returns:
448d64f4554SDavid Howells * * 0 - Success
449d64f4554SDavid Howells * * -ENOBUFS - No caching available
450d64f4554SDavid Howells * * Other error code from the cache, such as -ENOMEM.
451d64f4554SDavid Howells */
452d64f4554SDavid Howells static inline
fscache_begin_read_operation(struct netfs_cache_resources * cres,struct fscache_cookie * cookie)453d64f4554SDavid Howells int fscache_begin_read_operation(struct netfs_cache_resources *cres,
454d64f4554SDavid Howells struct fscache_cookie *cookie)
455d64f4554SDavid Howells {
456d64f4554SDavid Howells if (fscache_cookie_enabled(cookie))
457d64f4554SDavid Howells return __fscache_begin_read_operation(cres, cookie);
458d64f4554SDavid Howells return -ENOBUFS;
459d64f4554SDavid Howells }
460d64f4554SDavid Howells
4619af1c6c3SDavid Howells /**
462e9b57aaaSJeffle Xu * fscache_end_operation - End the read operation for the netfs lib
463e9b57aaaSJeffle Xu * @cres: The cache resources for the read operation
464e9b57aaaSJeffle Xu *
465e9b57aaaSJeffle Xu * Clean up the resources at the end of the read request.
466e9b57aaaSJeffle Xu */
fscache_end_operation(struct netfs_cache_resources * cres)467e9b57aaaSJeffle Xu static inline void fscache_end_operation(struct netfs_cache_resources *cres)
468e9b57aaaSJeffle Xu {
469e9b57aaaSJeffle Xu const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
470e9b57aaaSJeffle Xu
471e9b57aaaSJeffle Xu if (ops)
472e9b57aaaSJeffle Xu ops->end_operation(cres);
473e9b57aaaSJeffle Xu }
474e9b57aaaSJeffle Xu
475e9b57aaaSJeffle Xu /**
4769af1c6c3SDavid Howells * fscache_read - Start a read from the cache.
4779af1c6c3SDavid Howells * @cres: The cache resources to use
4789af1c6c3SDavid Howells * @start_pos: The beginning file offset in the cache file
4799af1c6c3SDavid Howells * @iter: The buffer to fill - and also the length
4809af1c6c3SDavid Howells * @read_hole: How to handle a hole in the data.
4819af1c6c3SDavid Howells * @term_func: The function to call upon completion
4829af1c6c3SDavid Howells * @term_func_priv: The private data for @term_func
4839af1c6c3SDavid Howells *
4849af1c6c3SDavid Howells * Start a read from the cache. @cres indicates the cache object to read from
4859af1c6c3SDavid Howells * and must be obtained by a call to fscache_begin_operation() beforehand.
4869af1c6c3SDavid Howells *
4879af1c6c3SDavid Howells * The data is read into the iterator, @iter, and that also indicates the size
4889af1c6c3SDavid Howells * of the operation. @start_pos is the start position in the file, though if
4899af1c6c3SDavid Howells * @seek_data is set appropriately, the cache can use SEEK_DATA to find the
4909af1c6c3SDavid Howells * next piece of data, writing zeros for the hole into the iterator.
4919af1c6c3SDavid Howells *
4929af1c6c3SDavid Howells * Upon termination of the operation, @term_func will be called and supplied
4939af1c6c3SDavid Howells * with @term_func_priv plus the amount of data written, if successful, or the
4949af1c6c3SDavid Howells * error code otherwise.
4959af1c6c3SDavid Howells *
4969af1c6c3SDavid Howells * @read_hole indicates how a partially populated region in the cache should be
4979af1c6c3SDavid Howells * handled. It can be one of a number of settings:
4989af1c6c3SDavid Howells *
4999af1c6c3SDavid Howells * NETFS_READ_HOLE_IGNORE - Just try to read (may return a short read).
5009af1c6c3SDavid Howells *
5019af1c6c3SDavid Howells * NETFS_READ_HOLE_CLEAR - Seek for data, clearing the part of the buffer
5029af1c6c3SDavid Howells * skipped over, then do as for IGNORE.
5039af1c6c3SDavid Howells *
5049af1c6c3SDavid Howells * NETFS_READ_HOLE_FAIL - Give ENODATA if we encounter a hole.
5059af1c6c3SDavid Howells */
5069af1c6c3SDavid Howells static inline
fscache_read(struct netfs_cache_resources * cres,loff_t start_pos,struct iov_iter * iter,enum netfs_read_from_hole read_hole,netfs_io_terminated_t term_func,void * term_func_priv)5079af1c6c3SDavid Howells int fscache_read(struct netfs_cache_resources *cres,
5089af1c6c3SDavid Howells loff_t start_pos,
5099af1c6c3SDavid Howells struct iov_iter *iter,
5109af1c6c3SDavid Howells enum netfs_read_from_hole read_hole,
5119af1c6c3SDavid Howells netfs_io_terminated_t term_func,
5129af1c6c3SDavid Howells void *term_func_priv)
5139af1c6c3SDavid Howells {
5149af1c6c3SDavid Howells const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
5159af1c6c3SDavid Howells return ops->read(cres, start_pos, iter, read_hole,
5169af1c6c3SDavid Howells term_func, term_func_priv);
5179af1c6c3SDavid Howells }
5189af1c6c3SDavid Howells
5199af1c6c3SDavid Howells /**
52016f2f4e6SDavid Howells * fscache_begin_write_operation - Begin a write operation for the netfs lib
52116f2f4e6SDavid Howells * @cres: The cache resources for the write being performed
52216f2f4e6SDavid Howells * @cookie: The cookie representing the cache object
52316f2f4e6SDavid Howells *
52416f2f4e6SDavid Howells * Begin a write operation on behalf of the netfs helper library. @cres
52516f2f4e6SDavid Howells * indicates the cache resources to which the operation state should be
52616f2f4e6SDavid Howells * attached; @cookie indicates the cache object that will be accessed.
52716f2f4e6SDavid Howells *
52816f2f4e6SDavid Howells * @cres->inval_counter is set from @cookie->inval_counter for comparison at
52916f2f4e6SDavid Howells * the end of the operation. This allows invalidation during the operation to
53016f2f4e6SDavid Howells * be detected by the caller.
53116f2f4e6SDavid Howells *
53216f2f4e6SDavid Howells * Returns:
53316f2f4e6SDavid Howells * * 0 - Success
53416f2f4e6SDavid Howells * * -ENOBUFS - No caching available
53516f2f4e6SDavid Howells * * Other error code from the cache, such as -ENOMEM.
53616f2f4e6SDavid Howells */
53716f2f4e6SDavid Howells static inline
fscache_begin_write_operation(struct netfs_cache_resources * cres,struct fscache_cookie * cookie)53816f2f4e6SDavid Howells int fscache_begin_write_operation(struct netfs_cache_resources *cres,
53916f2f4e6SDavid Howells struct fscache_cookie *cookie)
54016f2f4e6SDavid Howells {
54116f2f4e6SDavid Howells if (fscache_cookie_enabled(cookie))
54216f2f4e6SDavid Howells return __fscache_begin_write_operation(cres, cookie);
54316f2f4e6SDavid Howells return -ENOBUFS;
54416f2f4e6SDavid Howells }
54516f2f4e6SDavid Howells
54616f2f4e6SDavid Howells /**
5479af1c6c3SDavid Howells * fscache_write - Start a write to the cache.
5489af1c6c3SDavid Howells * @cres: The cache resources to use
5499af1c6c3SDavid Howells * @start_pos: The beginning file offset in the cache file
5509af1c6c3SDavid Howells * @iter: The data to write - and also the length
5519af1c6c3SDavid Howells * @term_func: The function to call upon completion
5529af1c6c3SDavid Howells * @term_func_priv: The private data for @term_func
5539af1c6c3SDavid Howells *
5549af1c6c3SDavid Howells * Start a write to the cache. @cres indicates the cache object to write to and
5559af1c6c3SDavid Howells * must be obtained by a call to fscache_begin_operation() beforehand.
5569af1c6c3SDavid Howells *
5579af1c6c3SDavid Howells * The data to be written is obtained from the iterator, @iter, and that also
5589af1c6c3SDavid Howells * indicates the size of the operation. @start_pos is the start position in
5599af1c6c3SDavid Howells * the file.
5609af1c6c3SDavid Howells *
5619af1c6c3SDavid Howells * Upon termination of the operation, @term_func will be called and supplied
5629af1c6c3SDavid Howells * with @term_func_priv plus the amount of data written, if successful, or the
5639af1c6c3SDavid Howells * error code otherwise.
5649af1c6c3SDavid Howells */
5659af1c6c3SDavid Howells static inline
fscache_write(struct netfs_cache_resources * cres,loff_t start_pos,struct iov_iter * iter,netfs_io_terminated_t term_func,void * term_func_priv)5669af1c6c3SDavid Howells int fscache_write(struct netfs_cache_resources *cres,
5679af1c6c3SDavid Howells loff_t start_pos,
5689af1c6c3SDavid Howells struct iov_iter *iter,
5699af1c6c3SDavid Howells netfs_io_terminated_t term_func,
5709af1c6c3SDavid Howells void *term_func_priv)
5719af1c6c3SDavid Howells {
5729af1c6c3SDavid Howells const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
5739af1c6c3SDavid Howells return ops->write(cres, start_pos, iter, term_func, term_func_priv);
5749af1c6c3SDavid Howells }
5759af1c6c3SDavid Howells
576b6e16652SDavid Howells /**
577b6e16652SDavid Howells * fscache_clear_page_bits - Clear the PG_fscache bits from a set of pages
578b6e16652SDavid Howells * @mapping: The netfs inode to use as the source
579b6e16652SDavid Howells * @start: The start position in @mapping
580b6e16652SDavid Howells * @len: The amount of data to unlock
581b6e16652SDavid Howells * @caching: If PG_fscache has been set
582b6e16652SDavid Howells *
583b6e16652SDavid Howells * Clear the PG_fscache flag from a sequence of pages and wake up anyone who's
584b6e16652SDavid Howells * waiting.
585b6e16652SDavid Howells */
fscache_clear_page_bits(struct address_space * mapping,loff_t start,size_t len,bool caching)5862c547f29SYue Hu static inline void fscache_clear_page_bits(struct address_space *mapping,
587b6e16652SDavid Howells loff_t start, size_t len,
588b6e16652SDavid Howells bool caching)
589b6e16652SDavid Howells {
590b6e16652SDavid Howells if (caching)
591b6e16652SDavid Howells __fscache_clear_page_bits(mapping, start, len);
592b6e16652SDavid Howells }
593b6e16652SDavid Howells
594b6e16652SDavid Howells /**
595b6e16652SDavid Howells * fscache_write_to_cache - Save a write to the cache and clear PG_fscache
596b6e16652SDavid Howells * @cookie: The cookie representing the cache object
597b6e16652SDavid Howells * @mapping: The netfs inode to use as the source
598b6e16652SDavid Howells * @start: The start position in @mapping
599b6e16652SDavid Howells * @len: The amount of data to write back
600b6e16652SDavid Howells * @i_size: The new size of the inode
601b6e16652SDavid Howells * @term_func: The function to call upon completion
602b6e16652SDavid Howells * @term_func_priv: The private data for @term_func
603*2ff1e975SDavid Howells * @using_pgpriv2: If we're using PG_private_2 to mark in-progress write
604*2ff1e975SDavid Howells * @caching: If we actually want to do the caching
605b6e16652SDavid Howells *
606b6e16652SDavid Howells * Helper function for a netfs to write dirty data from an inode into the cache
607b6e16652SDavid Howells * object that's backing it.
608b6e16652SDavid Howells *
609b6e16652SDavid Howells * @start and @len describe the range of the data. This does not need to be
610b6e16652SDavid Howells * page-aligned, but to satisfy DIO requirements, the cache may expand it up to
611b6e16652SDavid Howells * the page boundaries on either end. All the pages covering the range must be
612b6e16652SDavid Howells * marked with PG_fscache.
613b6e16652SDavid Howells *
614b6e16652SDavid Howells * If given, @term_func will be called upon completion and supplied with
615*2ff1e975SDavid Howells * @term_func_priv. Note that if @using_pgpriv2 is set, the PG_private_2 flags
616*2ff1e975SDavid Howells * will have been cleared by this point, so the netfs must retain its own pin
617*2ff1e975SDavid Howells * on the mapping.
618b6e16652SDavid Howells */
fscache_write_to_cache(struct fscache_cookie * cookie,struct address_space * mapping,loff_t start,size_t len,loff_t i_size,netfs_io_terminated_t term_func,void * term_func_priv,bool using_pgpriv2,bool caching)619b6e16652SDavid Howells static inline void fscache_write_to_cache(struct fscache_cookie *cookie,
620b6e16652SDavid Howells struct address_space *mapping,
621b6e16652SDavid Howells loff_t start, size_t len, loff_t i_size,
622b6e16652SDavid Howells netfs_io_terminated_t term_func,
623b6e16652SDavid Howells void *term_func_priv,
624*2ff1e975SDavid Howells bool using_pgpriv2, bool caching)
625b6e16652SDavid Howells {
626b6e16652SDavid Howells if (caching)
627b6e16652SDavid Howells __fscache_write_to_cache(cookie, mapping, start, len, i_size,
628*2ff1e975SDavid Howells term_func, term_func_priv,
629*2ff1e975SDavid Howells using_pgpriv2, caching);
630b6e16652SDavid Howells else if (term_func)
631b6e16652SDavid Howells term_func(term_func_priv, -ENOBUFS, false);
632b6e16652SDavid Howells
633b6e16652SDavid Howells }
634b6e16652SDavid Howells
6351f67e6d0SDavid Howells /**
6361f67e6d0SDavid Howells * fscache_note_page_release - Note that a netfs page got released
6371f67e6d0SDavid Howells * @cookie: The cookie corresponding to the file
6381f67e6d0SDavid Howells *
6391f67e6d0SDavid Howells * Note that a page that has been copied to the cache has been released. This
6401f67e6d0SDavid Howells * means that future reads will need to look in the cache to see if it's there.
6411f67e6d0SDavid Howells */
6421f67e6d0SDavid Howells static inline
fscache_note_page_release(struct fscache_cookie * cookie)6431f67e6d0SDavid Howells void fscache_note_page_release(struct fscache_cookie *cookie)
6441f67e6d0SDavid Howells {
645c522e3adSDavid Howells /* If we've written data to the cache (HAVE_DATA) and there wasn't any
646c522e3adSDavid Howells * data in the cache when we started (NO_DATA_TO_READ), it may no
647c522e3adSDavid Howells * longer be true that we can skip reading from the cache - so clear
648c522e3adSDavid Howells * the flag that causes reads to be skipped.
649c522e3adSDavid Howells */
6501f67e6d0SDavid Howells if (cookie &&
6511f67e6d0SDavid Howells test_bit(FSCACHE_COOKIE_HAVE_DATA, &cookie->flags) &&
6521f67e6d0SDavid Howells test_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags))
6531f67e6d0SDavid Howells clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
6541f67e6d0SDavid Howells }
6551f67e6d0SDavid Howells
6562d6fff63SDavid Howells #endif /* _LINUX_FSCACHE_H */
657