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