Merge tag 'devprop-5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-block.git] / include / linux / fscache.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
2d6fff63
DH
2/* General filesystem caching interface
3 *
1e1236b8 4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
2d6fff63
DH
5 * Written by David Howells (dhowells@redhat.com)
6 *
2d6fff63
DH
7 * NOTE!!! See:
8 *
efc930fa 9 * Documentation/filesystems/caching/netfs-api.rst
2d6fff63
DH
10 *
11 * for a description of the network filesystem interface declared here.
12 */
13
14#ifndef _LINUX_FSCACHE_H
15#define _LINUX_FSCACHE_H
16
17#include <linux/fs.h>
b533a83f 18#include <linux/netfs.h>
08276bda 19#include <linux/writeback.h>
2d6fff63
DH
20
21#if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
1e1236b8
DH
22#define __fscache_available (1)
23#define fscache_available() (1)
62ab6335 24#define fscache_volume_valid(volume) (volume)
2d6fff63 25#define fscache_cookie_valid(cookie) (cookie)
12bb21a2
DH
26#define fscache_resources_valid(cres) ((cres)->cache_priv)
27#define fscache_cookie_enabled(cookie) (cookie && !test_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
2d6fff63 28#else
1e1236b8
DH
29#define __fscache_available (0)
30#define fscache_available() (0)
62ab6335 31#define fscache_volume_valid(volume) (0)
2d6fff63 32#define fscache_cookie_valid(cookie) (0)
12bb21a2 33#define fscache_resources_valid(cres) (false)
2cee6fbb 34#define fscache_cookie_enabled(cookie) (0)
2d6fff63
DH
35#endif
36
7f3283ab
DH
37struct fscache_cookie;
38
39#define FSCACHE_ADV_SINGLE_CHUNK 0x01 /* The object is a single chunk of data */
40#define FSCACHE_ADV_WRITE_CACHE 0x00 /* Do cache if written to locally */
41#define FSCACHE_ADV_WRITE_NOCACHE 0x02 /* Don't cache if written to locally */
42
d24af13e
DH
43#define FSCACHE_INVAL_DIO_WRITE 0x01 /* Invalidate due to DIO write */
44
d64f4554
DH
45enum fscache_want_state {
46 FSCACHE_WANT_PARAMS,
47 FSCACHE_WANT_WRITE,
48 FSCACHE_WANT_READ,
49};
50
7f3283ab
DH
51/*
52 * Data object state.
53 */
54enum fscache_cookie_state {
55 FSCACHE_COOKIE_STATE_QUIESCENT, /* The cookie is uncached */
56 FSCACHE_COOKIE_STATE_LOOKING_UP, /* The cache object is being looked up */
57 FSCACHE_COOKIE_STATE_CREATING, /* The cache object is being created */
58 FSCACHE_COOKIE_STATE_ACTIVE, /* The cache is active, readable and writable */
d24af13e 59 FSCACHE_COOKIE_STATE_INVALIDATING, /* The cache is being invalidated */
7f3283ab 60 FSCACHE_COOKIE_STATE_FAILED, /* The cache failed, withdraw to clear */
12bb21a2 61 FSCACHE_COOKIE_STATE_LRU_DISCARDING, /* The cookie is being discarded by the LRU */
7f3283ab
DH
62 FSCACHE_COOKIE_STATE_WITHDRAWING, /* The cookie is being withdrawn */
63 FSCACHE_COOKIE_STATE_RELINQUISHING, /* The cookie is being relinquished */
64 FSCACHE_COOKIE_STATE_DROPPED, /* The cookie has been dropped */
65#define FSCACHE_COOKIE_STATE__NR (FSCACHE_COOKIE_STATE_DROPPED + 1)
66} __attribute__((mode(byte)));
67
62ab6335
DH
68/*
69 * Volume representation cookie.
70 */
71struct fscache_volume {
72 refcount_t ref;
73 atomic_t n_cookies; /* Number of data cookies in volume */
74 atomic_t n_accesses; /* Number of cache accesses in progress */
75 unsigned int debug_id;
76 unsigned int key_hash; /* Hash of key string */
77 char *key; /* Volume ID, eg. "afs@example.com@1234" */
78 struct list_head proc_link; /* Link in /proc/fs/fscache/volumes */
79 struct hlist_bl_node hash_link; /* Link in hash table */
80 struct work_struct work;
81 struct fscache_cache *cache; /* The cache in which this resides */
82 void *cache_priv; /* Cache private data */
83 spinlock_t lock;
84 unsigned long flags;
85#define FSCACHE_VOLUME_RELINQUISHED 0 /* Volume is being cleaned up */
86#define FSCACHE_VOLUME_INVALIDATE 1 /* Volume was invalidated */
87#define FSCACHE_VOLUME_COLLIDED_WITH 2 /* Volume was collided with */
88#define FSCACHE_VOLUME_ACQUIRE_PENDING 3 /* Volume is waiting to complete acquisition */
89#define FSCACHE_VOLUME_CREATING 4 /* Volume is being created on disk */
32e15003
DH
90 u8 coherency_len; /* Length of the coherency data */
91 u8 coherency[]; /* Coherency data */
62ab6335
DH
92};
93
7f3283ab
DH
94/*
95 * Data file representation cookie.
96 * - a file will only appear in one cache
97 * - a request to cache a file may or may not be honoured, subject to
98 * constraints such as disk space
99 * - indices are created on disk just-in-time
100 */
101struct fscache_cookie {
102 refcount_t ref;
103 atomic_t n_active; /* number of active users of cookie */
104 atomic_t n_accesses; /* Number of cache accesses in progress */
105 unsigned int debug_id;
106 unsigned int inval_counter; /* Number of invalidations made */
107 spinlock_t lock;
108 struct fscache_volume *volume; /* Parent volume of this file. */
109 void *cache_priv; /* Cache-side representation */
110 struct hlist_bl_node hash_link; /* Link in hash table */
111 struct list_head proc_link; /* Link in proc list */
112 struct list_head commit_link; /* Link in commit queue */
113 struct work_struct work; /* Commit/relinq/withdraw work */
114 loff_t object_size; /* Size of the netfs object */
115 unsigned long unused_at; /* Time at which unused (jiffies) */
116 unsigned long flags;
117#define FSCACHE_COOKIE_RELINQUISHED 0 /* T if cookie has been relinquished */
118#define FSCACHE_COOKIE_RETIRED 1 /* T if this cookie has retired on relinq */
119#define FSCACHE_COOKIE_IS_CACHING 2 /* T if this cookie is cached */
120#define FSCACHE_COOKIE_NO_DATA_TO_READ 3 /* T if this cookie has nothing to read */
121#define FSCACHE_COOKIE_NEEDS_UPDATE 4 /* T if attrs have been updated */
122#define FSCACHE_COOKIE_HAS_BEEN_CACHED 5 /* T if cookie needs withdraw-on-relinq */
123#define FSCACHE_COOKIE_DISABLED 6 /* T if cookie has been disabled */
124#define FSCACHE_COOKIE_LOCAL_WRITE 7 /* T if cookie has been modified locally */
125#define FSCACHE_COOKIE_NO_ACCESS_WAKE 8 /* T if no wake when n_accesses goes 0 */
126#define FSCACHE_COOKIE_DO_RELINQUISH 9 /* T if this cookie needs relinquishment */
127#define FSCACHE_COOKIE_DO_WITHDRAW 10 /* T if this cookie needs withdrawing */
128#define FSCACHE_COOKIE_DO_LRU_DISCARD 11 /* T if this cookie needs LRU discard */
129#define FSCACHE_COOKIE_DO_PREP_TO_WRITE 12 /* T if cookie needs write preparation */
130#define FSCACHE_COOKIE_HAVE_DATA 13 /* T if this cookie has data stored */
131#define FSCACHE_COOKIE_IS_HASHED 14 /* T if this cookie is hashed */
132
133 enum fscache_cookie_state state;
134 u8 advice; /* FSCACHE_ADV_* */
135 u8 key_len; /* Length of index key */
136 u8 aux_len; /* Length of auxiliary data */
137 u32 key_hash; /* Hash of volume, key, len */
138 union {
139 void *key; /* Index key */
140 u8 inline_key[16]; /* - If the key is short enough */
141 };
142 union {
143 void *aux; /* Auxiliary data */
144 u8 inline_aux[8]; /* - If the aux data is short enough */
145 };
146};
147
62ab6335
DH
148/*
149 * slow-path functions for when there is actually caching available, and the
150 * netfs does actually have a valid token
151 * - these are not to be called directly
152 * - these are undefined symbols when FS-Cache is not configured and the
153 * optimiser takes care of not using them
154 */
155extern struct fscache_volume *__fscache_acquire_volume(const char *, const char *,
156 const void *, size_t);
157extern void __fscache_relinquish_volume(struct fscache_volume *, const void *, bool);
158
7f3283ab
DH
159extern struct fscache_cookie *__fscache_acquire_cookie(
160 struct fscache_volume *,
161 u8,
162 const void *, size_t,
163 const void *, size_t,
164 loff_t);
12bb21a2
DH
165extern void __fscache_use_cookie(struct fscache_cookie *, bool);
166extern void __fscache_unuse_cookie(struct fscache_cookie *, const void *, const loff_t *);
7f3283ab 167extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
16a96bdf 168extern void __fscache_resize_cookie(struct fscache_cookie *, loff_t);
d24af13e 169extern void __fscache_invalidate(struct fscache_cookie *, const void *, loff_t, unsigned int);
d64f4554 170extern int __fscache_begin_read_operation(struct netfs_cache_resources *, struct fscache_cookie *);
16f2f4e6 171extern int __fscache_begin_write_operation(struct netfs_cache_resources *, struct fscache_cookie *);
7f3283ab 172
b6e16652
DH
173extern void __fscache_write_to_cache(struct fscache_cookie *, struct address_space *,
174 loff_t, size_t, loff_t, netfs_io_terminated_t, void *,
175 bool);
176extern void __fscache_clear_page_bits(struct address_space *, loff_t, size_t);
177
62ab6335
DH
178/**
179 * fscache_acquire_volume - Register a volume as desiring caching services
180 * @volume_key: An identification string for the volume
181 * @cache_name: The name of the cache to use (or NULL for the default)
182 * @coherency_data: Piece of arbitrary coherency data to check (or NULL)
183 * @coherency_len: The size of the coherency data
184 *
185 * Register a volume as desiring caching services if they're available. The
186 * caller must provide an identifier for the volume and may also indicate which
187 * cache it should be in. If a preexisting volume entry is found in the cache,
188 * the coherency data must match otherwise the entry will be invalidated.
189 *
190 * Returns a cookie pointer on success, -ENOMEM if out of memory or -EBUSY if a
191 * cache volume of that name is already acquired. Note that "NULL" is a valid
192 * cookie pointer and can be returned if caching is refused.
193 */
194static inline
195struct fscache_volume *fscache_acquire_volume(const char *volume_key,
196 const char *cache_name,
197 const void *coherency_data,
198 size_t coherency_len)
199{
200 if (!fscache_available())
201 return NULL;
202 return __fscache_acquire_volume(volume_key, cache_name,
203 coherency_data, coherency_len);
204}
205
206/**
207 * fscache_relinquish_volume - Cease caching a volume
208 * @volume: The volume cookie
209 * @coherency_data: Piece of arbitrary coherency data to set (or NULL)
210 * @invalidate: True if the volume should be invalidated
211 *
212 * Indicate that a filesystem no longer desires caching services for a volume.
213 * The caller must have relinquished all file cookies prior to calling this.
214 * The stored coherency data is updated.
215 */
216static inline
217void fscache_relinquish_volume(struct fscache_volume *volume,
218 const void *coherency_data,
219 bool invalidate)
220{
221 if (fscache_volume_valid(volume))
222 __fscache_relinquish_volume(volume, coherency_data, invalidate);
223}
224
7f3283ab
DH
225/**
226 * fscache_acquire_cookie - Acquire a cookie to represent a cache object
227 * @volume: The volume in which to locate/create this cookie
228 * @advice: Advice flags (FSCACHE_COOKIE_ADV_*)
229 * @index_key: The index key for this cookie
230 * @index_key_len: Size of the index key
231 * @aux_data: The auxiliary data for the cookie (may be NULL)
232 * @aux_data_len: Size of the auxiliary data buffer
233 * @object_size: The initial size of object
234 *
235 * Acquire a cookie to represent a data file within the given cache volume.
236 *
237 * See Documentation/filesystems/caching/netfs-api.rst for a complete
238 * description.
239 */
240static inline
241struct fscache_cookie *fscache_acquire_cookie(struct fscache_volume *volume,
242 u8 advice,
243 const void *index_key,
244 size_t index_key_len,
245 const void *aux_data,
246 size_t aux_data_len,
247 loff_t object_size)
248{
249 if (!fscache_volume_valid(volume))
250 return NULL;
251 return __fscache_acquire_cookie(volume, advice,
252 index_key, index_key_len,
253 aux_data, aux_data_len,
254 object_size);
255}
256
12bb21a2
DH
257/**
258 * fscache_use_cookie - Request usage of cookie attached to an object
259 * @object: Object description
260 * @will_modify: If cache is expected to be modified locally
261 *
262 * Request usage of the cookie attached to an object. The caller should tell
263 * the cache if the object's contents are about to be modified locally and then
264 * the cache can apply the policy that has been set to handle this case.
265 */
266static inline void fscache_use_cookie(struct fscache_cookie *cookie,
267 bool will_modify)
268{
269 if (fscache_cookie_valid(cookie))
270 __fscache_use_cookie(cookie, will_modify);
271}
272
273/**
274 * fscache_unuse_cookie - Cease usage of cookie attached to an object
275 * @object: Object description
276 * @aux_data: Updated auxiliary data (or NULL)
277 * @object_size: Revised size of the object (or NULL)
278 *
279 * Cease usage of the cookie attached to an object. When the users count
280 * reaches zero then the cookie relinquishment will be permitted to proceed.
281 */
282static inline void fscache_unuse_cookie(struct fscache_cookie *cookie,
283 const void *aux_data,
284 const loff_t *object_size)
285{
286 if (fscache_cookie_valid(cookie))
287 __fscache_unuse_cookie(cookie, aux_data, object_size);
288}
289
7f3283ab
DH
290/**
291 * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
292 * it
293 * @cookie: The cookie being returned
294 * @retire: True if the cache object the cookie represents is to be discarded
295 *
296 * This function returns a cookie to the cache, forcibly discarding the
297 * associated cache object if retire is set to true.
298 *
299 * See Documentation/filesystems/caching/netfs-api.rst for a complete
300 * description.
301 */
302static inline
303void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
304{
305 if (fscache_cookie_valid(cookie))
306 __fscache_relinquish_cookie(cookie, retire);
307}
308
12bb21a2
DH
309/*
310 * Find the auxiliary data on a cookie.
311 */
312static inline void *fscache_get_aux(struct fscache_cookie *cookie)
313{
314 if (cookie->aux_len <= sizeof(cookie->inline_aux))
315 return cookie->inline_aux;
316 else
317 return cookie->aux;
318}
319
320/*
321 * Update the auxiliary data on a cookie.
322 */
323static inline
324void fscache_update_aux(struct fscache_cookie *cookie,
325 const void *aux_data, const loff_t *object_size)
326{
327 void *p = fscache_get_aux(cookie);
328
329 if (aux_data && p)
330 memcpy(p, aux_data, cookie->aux_len);
331 if (object_size)
332 cookie->object_size = *object_size;
333}
334
335#ifdef CONFIG_FSCACHE_STATS
336extern atomic_t fscache_n_updates;
337#endif
338
339static inline
340void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
341 const loff_t *object_size)
342{
343#ifdef CONFIG_FSCACHE_STATS
344 atomic_inc(&fscache_n_updates);
345#endif
346 fscache_update_aux(cookie, aux_data, object_size);
347 smp_wmb();
348 set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
349}
350
ed1235eb
DH
351/**
352 * fscache_update_cookie - Request that a cache object be updated
353 * @cookie: The cookie representing the cache object
354 * @aux_data: The updated auxiliary data for the cookie (may be NULL)
355 * @object_size: The current size of the object (may be NULL)
356 *
357 * Request an update of the index data for the cache object associated with the
358 * cookie. The auxiliary data on the cookie will be updated first if @aux_data
359 * is set and the object size will be updated and the object possibly trimmed
360 * if @object_size is set.
361 *
362 * See Documentation/filesystems/caching/netfs-api.rst for a complete
363 * description.
364 */
365static inline
366void fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
367 const loff_t *object_size)
368{
369 if (fscache_cookie_enabled(cookie))
370 __fscache_update_cookie(cookie, aux_data, object_size);
371}
372
16a96bdf
DH
373/**
374 * fscache_resize_cookie - Request that a cache object be resized
375 * @cookie: The cookie representing the cache object
376 * @new_size: The new size of the object (may be NULL)
377 *
378 * Request that the size of an object be changed.
379 *
380 * See Documentation/filesystems/caching/netfs-api.txt for a complete
381 * description.
382 */
383static inline
384void fscache_resize_cookie(struct fscache_cookie *cookie, loff_t new_size)
385{
386 if (fscache_cookie_enabled(cookie))
387 __fscache_resize_cookie(cookie, new_size);
388}
389
d24af13e
DH
390/**
391 * fscache_invalidate - Notify cache that an object needs invalidation
392 * @cookie: The cookie representing the cache object
393 * @aux_data: The updated auxiliary data for the cookie (may be NULL)
394 * @size: The revised size of the object.
395 * @flags: Invalidation flags (FSCACHE_INVAL_*)
396 *
397 * Notify the cache that an object is needs to be invalidated and that it
398 * should abort any retrievals or stores it is doing on the cache. This
399 * increments inval_counter on the cookie which can be used by the caller to
400 * reconsider I/O requests as they complete.
401 *
402 * If @flags has FSCACHE_INVAL_DIO_WRITE set, this indicates that this is due
403 * to a direct I/O write and will cause caching to be disabled on this cookie
404 * until it is completely unused.
405 *
406 * See Documentation/filesystems/caching/netfs-api.rst for a complete
407 * description.
408 */
409static inline
410void fscache_invalidate(struct fscache_cookie *cookie,
411 const void *aux_data, loff_t size, unsigned int flags)
412{
413 if (fscache_cookie_enabled(cookie))
414 __fscache_invalidate(cookie, aux_data, size, flags);
415}
416
d64f4554
DH
417/**
418 * fscache_operation_valid - Return true if operations resources are usable
419 * @cres: The resources to check.
420 *
421 * Returns a pointer to the operations table if usable or NULL if not.
422 */
423static inline
424const struct netfs_cache_ops *fscache_operation_valid(const struct netfs_cache_resources *cres)
425{
426 return fscache_resources_valid(cres) ? cres->ops : NULL;
427}
428
429/**
430 * fscache_begin_read_operation - Begin a read operation for the netfs lib
431 * @cres: The cache resources for the read being performed
432 * @cookie: The cookie representing the cache object
433 *
434 * Begin a read operation on behalf of the netfs helper library. @cres
435 * indicates the cache resources to which the operation state should be
436 * attached; @cookie indicates the cache object that will be accessed.
437 *
438 * This is intended to be called from the ->begin_cache_operation() netfs lib
439 * operation as implemented by the network filesystem.
440 *
441 * @cres->inval_counter is set from @cookie->inval_counter for comparison at
442 * the end of the operation. This allows invalidation during the operation to
443 * be detected by the caller.
444 *
445 * Returns:
446 * * 0 - Success
447 * * -ENOBUFS - No caching available
448 * * Other error code from the cache, such as -ENOMEM.
449 */
450static inline
451int fscache_begin_read_operation(struct netfs_cache_resources *cres,
452 struct fscache_cookie *cookie)
453{
454 if (fscache_cookie_enabled(cookie))
455 return __fscache_begin_read_operation(cres, cookie);
456 return -ENOBUFS;
457}
458
e9b57aaa
JX
459/**
460 * fscache_end_operation - End the read operation for the netfs lib
461 * @cres: The cache resources for the read operation
462 *
463 * Clean up the resources at the end of the read request.
464 */
465static inline void fscache_end_operation(struct netfs_cache_resources *cres)
466{
467 const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
468
469 if (ops)
470 ops->end_operation(cres);
471}
472
9af1c6c3
DH
473/**
474 * fscache_read - Start a read from the cache.
475 * @cres: The cache resources to use
476 * @start_pos: The beginning file offset in the cache file
477 * @iter: The buffer to fill - and also the length
478 * @read_hole: How to handle a hole in the data.
479 * @term_func: The function to call upon completion
480 * @term_func_priv: The private data for @term_func
481 *
482 * Start a read from the cache. @cres indicates the cache object to read from
483 * and must be obtained by a call to fscache_begin_operation() beforehand.
484 *
485 * The data is read into the iterator, @iter, and that also indicates the size
486 * of the operation. @start_pos is the start position in the file, though if
487 * @seek_data is set appropriately, the cache can use SEEK_DATA to find the
488 * next piece of data, writing zeros for the hole into the iterator.
489 *
490 * Upon termination of the operation, @term_func will be called and supplied
491 * with @term_func_priv plus the amount of data written, if successful, or the
492 * error code otherwise.
493 *
494 * @read_hole indicates how a partially populated region in the cache should be
495 * handled. It can be one of a number of settings:
496 *
497 * NETFS_READ_HOLE_IGNORE - Just try to read (may return a short read).
498 *
499 * NETFS_READ_HOLE_CLEAR - Seek for data, clearing the part of the buffer
500 * skipped over, then do as for IGNORE.
501 *
502 * NETFS_READ_HOLE_FAIL - Give ENODATA if we encounter a hole.
503 */
504static inline
505int fscache_read(struct netfs_cache_resources *cres,
506 loff_t start_pos,
507 struct iov_iter *iter,
508 enum netfs_read_from_hole read_hole,
509 netfs_io_terminated_t term_func,
510 void *term_func_priv)
511{
512 const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
513 return ops->read(cres, start_pos, iter, read_hole,
514 term_func, term_func_priv);
515}
516
16f2f4e6
DH
517/**
518 * fscache_begin_write_operation - Begin a write operation for the netfs lib
519 * @cres: The cache resources for the write being performed
520 * @cookie: The cookie representing the cache object
521 *
522 * Begin a write operation on behalf of the netfs helper library. @cres
523 * indicates the cache resources to which the operation state should be
524 * attached; @cookie indicates the cache object that will be accessed.
525 *
526 * @cres->inval_counter is set from @cookie->inval_counter for comparison at
527 * the end of the operation. This allows invalidation during the operation to
528 * be detected by the caller.
529 *
530 * Returns:
531 * * 0 - Success
532 * * -ENOBUFS - No caching available
533 * * Other error code from the cache, such as -ENOMEM.
534 */
535static inline
536int fscache_begin_write_operation(struct netfs_cache_resources *cres,
537 struct fscache_cookie *cookie)
538{
539 if (fscache_cookie_enabled(cookie))
540 return __fscache_begin_write_operation(cres, cookie);
541 return -ENOBUFS;
542}
543
9af1c6c3
DH
544/**
545 * fscache_write - Start a write to the cache.
546 * @cres: The cache resources to use
547 * @start_pos: The beginning file offset in the cache file
548 * @iter: The data to write - and also the length
549 * @term_func: The function to call upon completion
550 * @term_func_priv: The private data for @term_func
551 *
552 * Start a write to the cache. @cres indicates the cache object to write to and
553 * must be obtained by a call to fscache_begin_operation() beforehand.
554 *
555 * The data to be written is obtained from the iterator, @iter, and that also
556 * indicates the size of the operation. @start_pos is the start position in
557 * the file.
558 *
559 * Upon termination of the operation, @term_func will be called and supplied
560 * with @term_func_priv plus the amount of data written, if successful, or the
561 * error code otherwise.
562 */
563static inline
564int fscache_write(struct netfs_cache_resources *cres,
565 loff_t start_pos,
566 struct iov_iter *iter,
567 netfs_io_terminated_t term_func,
568 void *term_func_priv)
569{
570 const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
571 return ops->write(cres, start_pos, iter, term_func, term_func_priv);
572}
573
b6e16652
DH
574/**
575 * fscache_clear_page_bits - Clear the PG_fscache bits from a set of pages
b6e16652
DH
576 * @mapping: The netfs inode to use as the source
577 * @start: The start position in @mapping
578 * @len: The amount of data to unlock
579 * @caching: If PG_fscache has been set
580 *
581 * Clear the PG_fscache flag from a sequence of pages and wake up anyone who's
582 * waiting.
583 */
2c547f29 584static inline void fscache_clear_page_bits(struct address_space *mapping,
b6e16652
DH
585 loff_t start, size_t len,
586 bool caching)
587{
588 if (caching)
589 __fscache_clear_page_bits(mapping, start, len);
590}
591
592/**
593 * fscache_write_to_cache - Save a write to the cache and clear PG_fscache
594 * @cookie: The cookie representing the cache object
595 * @mapping: The netfs inode to use as the source
596 * @start: The start position in @mapping
597 * @len: The amount of data to write back
598 * @i_size: The new size of the inode
599 * @term_func: The function to call upon completion
600 * @term_func_priv: The private data for @term_func
601 * @caching: If PG_fscache has been set
602 *
603 * Helper function for a netfs to write dirty data from an inode into the cache
604 * object that's backing it.
605 *
606 * @start and @len describe the range of the data. This does not need to be
607 * page-aligned, but to satisfy DIO requirements, the cache may expand it up to
608 * the page boundaries on either end. All the pages covering the range must be
609 * marked with PG_fscache.
610 *
611 * If given, @term_func will be called upon completion and supplied with
612 * @term_func_priv. Note that the PG_fscache flags will have been cleared by
613 * this point, so the netfs must retain its own pin on the mapping.
614 */
615static inline void fscache_write_to_cache(struct fscache_cookie *cookie,
616 struct address_space *mapping,
617 loff_t start, size_t len, loff_t i_size,
618 netfs_io_terminated_t term_func,
619 void *term_func_priv,
620 bool caching)
621{
622 if (caching)
623 __fscache_write_to_cache(cookie, mapping, start, len, i_size,
624 term_func, term_func_priv, caching);
625 else if (term_func)
626 term_func(term_func_priv, -ENOBUFS, false);
627
628}
629
08276bda 630#if __fscache_available
8fb72b4a
MWO
631bool fscache_dirty_folio(struct address_space *mapping, struct folio *folio,
632 struct fscache_cookie *cookie);
08276bda 633#else
8fb72b4a
MWO
634#define fscache_dirty_folio(MAPPING, FOLIO, COOKIE) \
635 filemap_dirty_folio(MAPPING, FOLIO)
08276bda
DH
636#endif
637
638/**
639 * fscache_unpin_writeback - Unpin writeback resources
640 * @wbc: The writeback control
641 * @cookie: The cookie referring to the cache object
642 *
8fb72b4a 643 * Unpin the writeback resources pinned by fscache_dirty_folio(). This is
08276bda
DH
644 * intended to be called by the netfs's ->write_inode() method.
645 */
646static inline void fscache_unpin_writeback(struct writeback_control *wbc,
647 struct fscache_cookie *cookie)
648{
649 if (wbc->unpinned_fscache_wb)
650 fscache_unuse_cookie(cookie, NULL, NULL);
651}
652
653/**
654 * fscache_clear_inode_writeback - Clear writeback resources pinned by an inode
655 * @cookie: The cookie referring to the cache object
656 * @inode: The inode to clean up
657 * @aux: Auxiliary data to apply to the inode
658 *
659 * Clear any writeback resources held by an inode when the inode is evicted.
660 * This must be called before clear_inode() is called.
661 */
662static inline void fscache_clear_inode_writeback(struct fscache_cookie *cookie,
663 struct inode *inode,
664 const void *aux)
665{
666 if (inode->i_state & I_PINNING_FSCACHE_WB) {
667 loff_t i_size = i_size_read(inode);
668 fscache_unuse_cookie(cookie, aux, &i_size);
669 }
670}
671
1f67e6d0
DH
672/**
673 * fscache_note_page_release - Note that a netfs page got released
674 * @cookie: The cookie corresponding to the file
675 *
676 * Note that a page that has been copied to the cache has been released. This
677 * means that future reads will need to look in the cache to see if it's there.
678 */
679static inline
680void fscache_note_page_release(struct fscache_cookie *cookie)
681{
c522e3ad
DH
682 /* If we've written data to the cache (HAVE_DATA) and there wasn't any
683 * data in the cache when we started (NO_DATA_TO_READ), it may no
684 * longer be true that we can skip reading from the cache - so clear
685 * the flag that causes reads to be skipped.
686 */
1f67e6d0
DH
687 if (cookie &&
688 test_bit(FSCACHE_COOKIE_HAVE_DATA, &cookie->flags) &&
689 test_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags))
690 clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
691}
692
2d6fff63 693#endif /* _LINUX_FSCACHE_H */