xref: /linux/include/linux/fscache.h (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
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 	loff_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 /*
151  * slow-path functions for when there is actually caching available, and the
152  * netfs does actually have a valid token
153  * - these are not to be called directly
154  * - these are undefined symbols when FS-Cache is not configured and the
155  *   optimiser takes care of not using them
156  */
157 extern struct fscache_volume *__fscache_acquire_volume(const char *, const char *,
158 						       const void *, size_t);
159 extern void __fscache_relinquish_volume(struct fscache_volume *, const void *, bool);
160 
161 extern struct fscache_cookie *__fscache_acquire_cookie(
162 	struct fscache_volume *,
163 	u8,
164 	const void *, size_t,
165 	const void *, size_t,
166 	loff_t);
167 extern void __fscache_use_cookie(struct fscache_cookie *, bool);
168 extern void __fscache_unuse_cookie(struct fscache_cookie *, const void *, const loff_t *);
169 extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
170 extern void __fscache_resize_cookie(struct fscache_cookie *, loff_t);
171 extern void __fscache_invalidate(struct fscache_cookie *, const void *, loff_t, unsigned int);
172 extern int __fscache_begin_read_operation(struct netfs_cache_resources *, struct fscache_cookie *);
173 extern int __fscache_begin_write_operation(struct netfs_cache_resources *, struct fscache_cookie *);
174 
175 void __fscache_write_to_cache(struct fscache_cookie *cookie,
176 			      struct address_space *mapping,
177 			      loff_t start, size_t len, loff_t i_size,
178 			      netfs_io_terminated_t term_func,
179 			      void *term_func_priv,
180 			      bool using_pgpriv2, bool cond);
181 extern void __fscache_clear_page_bits(struct address_space *, loff_t, size_t);
182 
183 /**
184  * fscache_acquire_volume - Register a volume as desiring caching services
185  * @volume_key: An identification string for the volume
186  * @cache_name: The name of the cache to use (or NULL for the default)
187  * @coherency_data: Piece of arbitrary coherency data to check (or NULL)
188  * @coherency_len: The size of the coherency data
189  *
190  * Register a volume as desiring caching services if they're available.  The
191  * caller must provide an identifier for the volume and may also indicate which
192  * cache it should be in.  If a preexisting volume entry is found in the cache,
193  * the coherency data must match otherwise the entry will be invalidated.
194  *
195  * Returns a cookie pointer on success, -ENOMEM if out of memory or -EBUSY if a
196  * cache volume of that name is already acquired.  Note that "NULL" is a valid
197  * cookie pointer and can be returned if caching is refused.
198  */
199 static inline
fscache_acquire_volume(const char * volume_key,const char * cache_name,const void * coherency_data,size_t coherency_len)200 struct fscache_volume *fscache_acquire_volume(const char *volume_key,
201 					      const char *cache_name,
202 					      const void *coherency_data,
203 					      size_t coherency_len)
204 {
205 	if (!fscache_available())
206 		return NULL;
207 	return __fscache_acquire_volume(volume_key, cache_name,
208 					coherency_data, coherency_len);
209 }
210 
211 /**
212  * fscache_relinquish_volume - Cease caching a volume
213  * @volume: The volume cookie
214  * @coherency_data: Piece of arbitrary coherency data to set (or NULL)
215  * @invalidate: True if the volume should be invalidated
216  *
217  * Indicate that a filesystem no longer desires caching services for a volume.
218  * The caller must have relinquished all file cookies prior to calling this.
219  * The stored coherency data is updated.
220  */
221 static inline
fscache_relinquish_volume(struct fscache_volume * volume,const void * coherency_data,bool invalidate)222 void fscache_relinquish_volume(struct fscache_volume *volume,
223 			       const void *coherency_data,
224 			       bool invalidate)
225 {
226 	if (fscache_volume_valid(volume))
227 		__fscache_relinquish_volume(volume, coherency_data, invalidate);
228 }
229 
230 /**
231  * fscache_acquire_cookie - Acquire a cookie to represent a cache object
232  * @volume: The volume in which to locate/create this cookie
233  * @advice: Advice flags (FSCACHE_COOKIE_ADV_*)
234  * @index_key: The index key for this cookie
235  * @index_key_len: Size of the index key
236  * @aux_data: The auxiliary data for the cookie (may be NULL)
237  * @aux_data_len: Size of the auxiliary data buffer
238  * @object_size: The initial size of object
239  *
240  * Acquire a cookie to represent a data file within the given cache volume.
241  *
242  * See Documentation/filesystems/caching/netfs-api.rst for a complete
243  * description.
244  */
245 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)246 struct fscache_cookie *fscache_acquire_cookie(struct fscache_volume *volume,
247 					      u8 advice,
248 					      const void *index_key,
249 					      size_t index_key_len,
250 					      const void *aux_data,
251 					      size_t aux_data_len,
252 					      loff_t object_size)
253 {
254 	if (!fscache_volume_valid(volume))
255 		return NULL;
256 	return __fscache_acquire_cookie(volume, advice,
257 					index_key, index_key_len,
258 					aux_data, aux_data_len,
259 					object_size);
260 }
261 
262 /**
263  * fscache_use_cookie - Request usage of cookie attached to an object
264  * @cookie: The cookie representing the cache object
265  * @will_modify: If cache is expected to be modified locally
266  *
267  * Request usage of the cookie attached to an object.  The caller should tell
268  * the cache if the object's contents are about to be modified locally and then
269  * the cache can apply the policy that has been set to handle this case.
270  */
fscache_use_cookie(struct fscache_cookie * cookie,bool will_modify)271 static inline void fscache_use_cookie(struct fscache_cookie *cookie,
272 				      bool will_modify)
273 {
274 	if (fscache_cookie_valid(cookie))
275 		__fscache_use_cookie(cookie, will_modify);
276 }
277 
278 /**
279  * fscache_unuse_cookie - Cease usage of cookie attached to an object
280  * @cookie: The cookie representing the cache object
281  * @aux_data: Updated auxiliary data (or NULL)
282  * @object_size: Revised size of the object (or NULL)
283  *
284  * Cease usage of the cookie attached to an object.  When the users count
285  * reaches zero then the cookie relinquishment will be permitted to proceed.
286  */
fscache_unuse_cookie(struct fscache_cookie * cookie,const void * aux_data,const loff_t * object_size)287 static inline void fscache_unuse_cookie(struct fscache_cookie *cookie,
288 					const void *aux_data,
289 					const loff_t *object_size)
290 {
291 	if (fscache_cookie_valid(cookie))
292 		__fscache_unuse_cookie(cookie, aux_data, object_size);
293 }
294 
295 /**
296  * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
297  * it
298  * @cookie: The cookie being returned
299  * @retire: True if the cache object the cookie represents is to be discarded
300  *
301  * This function returns a cookie to the cache, forcibly discarding the
302  * associated cache object if retire is set to true.
303  *
304  * See Documentation/filesystems/caching/netfs-api.rst for a complete
305  * description.
306  */
307 static inline
fscache_relinquish_cookie(struct fscache_cookie * cookie,bool retire)308 void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
309 {
310 	if (fscache_cookie_valid(cookie))
311 		__fscache_relinquish_cookie(cookie, retire);
312 }
313 
314 /*
315  * Find the auxiliary data on a cookie.
316  */
fscache_get_aux(struct fscache_cookie * cookie)317 static inline void *fscache_get_aux(struct fscache_cookie *cookie)
318 {
319 	if (cookie->aux_len <= sizeof(cookie->inline_aux))
320 		return cookie->inline_aux;
321 	else
322 		return cookie->aux;
323 }
324 
325 /*
326  * Update the auxiliary data on a cookie.
327  */
328 static inline
fscache_update_aux(struct fscache_cookie * cookie,const void * aux_data,const loff_t * object_size)329 void fscache_update_aux(struct fscache_cookie *cookie,
330 			const void *aux_data, const loff_t *object_size)
331 {
332 	void *p = fscache_get_aux(cookie);
333 
334 	if (aux_data && p)
335 		memcpy(p, aux_data, cookie->aux_len);
336 	if (object_size)
337 		cookie->object_size = *object_size;
338 }
339 
340 #ifdef CONFIG_FSCACHE_STATS
341 extern atomic_t fscache_n_updates;
342 #endif
343 
344 static inline
__fscache_update_cookie(struct fscache_cookie * cookie,const void * aux_data,const loff_t * object_size)345 void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
346 			     const loff_t *object_size)
347 {
348 #ifdef CONFIG_FSCACHE_STATS
349 	atomic_inc(&fscache_n_updates);
350 #endif
351 	fscache_update_aux(cookie, aux_data, object_size);
352 	smp_wmb();
353 	set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
354 }
355 
356 /**
357  * fscache_update_cookie - Request that a cache object be updated
358  * @cookie: The cookie representing the cache object
359  * @aux_data: The updated auxiliary data for the cookie (may be NULL)
360  * @object_size: The current size of the object (may be NULL)
361  *
362  * Request an update of the index data for the cache object associated with the
363  * cookie.  The auxiliary data on the cookie will be updated first if @aux_data
364  * is set and the object size will be updated and the object possibly trimmed
365  * if @object_size is set.
366  *
367  * See Documentation/filesystems/caching/netfs-api.rst for a complete
368  * description.
369  */
370 static inline
fscache_update_cookie(struct fscache_cookie * cookie,const void * aux_data,const loff_t * object_size)371 void fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
372 			   const loff_t *object_size)
373 {
374 	if (fscache_cookie_enabled(cookie))
375 		__fscache_update_cookie(cookie, aux_data, object_size);
376 }
377 
378 /**
379  * fscache_resize_cookie - Request that a cache object be resized
380  * @cookie: The cookie representing the cache object
381  * @new_size: The new size of the object (may be NULL)
382  *
383  * Request that the size of an object be changed.
384  *
385  * See Documentation/filesystems/caching/netfs-api.rst for a complete
386  * description.
387  */
388 static inline
fscache_resize_cookie(struct fscache_cookie * cookie,loff_t new_size)389 void fscache_resize_cookie(struct fscache_cookie *cookie, loff_t new_size)
390 {
391 	if (fscache_cookie_enabled(cookie))
392 		__fscache_resize_cookie(cookie, new_size);
393 }
394 
395 /**
396  * fscache_invalidate - Notify cache that an object needs invalidation
397  * @cookie: The cookie representing the cache object
398  * @aux_data: The updated auxiliary data for the cookie (may be NULL)
399  * @size: The revised size of the object.
400  * @flags: Invalidation flags (FSCACHE_INVAL_*)
401  *
402  * Notify the cache that an object is needs to be invalidated and that it
403  * should abort any retrievals or stores it is doing on the cache.  This
404  * increments inval_counter on the cookie which can be used by the caller to
405  * reconsider I/O requests as they complete.
406  *
407  * If @flags has FSCACHE_INVAL_DIO_WRITE set, this indicates that this is due
408  * to a direct I/O write and will cause caching to be disabled on this cookie
409  * until it is completely unused.
410  *
411  * See Documentation/filesystems/caching/netfs-api.rst for a complete
412  * description.
413  */
414 static inline
fscache_invalidate(struct fscache_cookie * cookie,const void * aux_data,loff_t size,unsigned int flags)415 void fscache_invalidate(struct fscache_cookie *cookie,
416 			const void *aux_data, loff_t size, unsigned int flags)
417 {
418 	if (fscache_cookie_enabled(cookie))
419 		__fscache_invalidate(cookie, aux_data, size, flags);
420 }
421 
422 /**
423  * fscache_operation_valid - Return true if operations resources are usable
424  * @cres: The resources to check.
425  *
426  * Returns a pointer to the operations table if usable or NULL if not.
427  */
428 static inline
fscache_operation_valid(const struct netfs_cache_resources * cres)429 const struct netfs_cache_ops *fscache_operation_valid(const struct netfs_cache_resources *cres)
430 {
431 	return fscache_resources_valid(cres) ? cres->ops : NULL;
432 }
433 
434 /**
435  * fscache_begin_read_operation - Begin a read operation for the netfs lib
436  * @cres: The cache resources for the read being performed
437  * @cookie: The cookie representing the cache object
438  *
439  * Begin a read operation on behalf of the netfs helper library.  @cres
440  * indicates the cache resources to which the operation state should be
441  * attached; @cookie indicates the cache object that will be accessed.
442  *
443  * @cres->inval_counter is set from @cookie->inval_counter for comparison at
444  * the end of the operation.  This allows invalidation during the operation to
445  * be detected by the caller.
446  *
447  * Returns:
448  * * 0		- Success
449  * * -ENOBUFS	- No caching available
450  * * Other error code from the cache, such as -ENOMEM.
451  */
452 static inline
fscache_begin_read_operation(struct netfs_cache_resources * cres,struct fscache_cookie * cookie)453 int fscache_begin_read_operation(struct netfs_cache_resources *cres,
454 				 struct fscache_cookie *cookie)
455 {
456 	if (fscache_cookie_enabled(cookie))
457 		return __fscache_begin_read_operation(cres, cookie);
458 	return -ENOBUFS;
459 }
460 
461 /**
462  * fscache_end_operation - End the read operation for the netfs lib
463  * @cres: The cache resources for the read operation
464  *
465  * Clean up the resources at the end of the read request.
466  */
fscache_end_operation(struct netfs_cache_resources * cres)467 static inline void fscache_end_operation(struct netfs_cache_resources *cres)
468 {
469 	const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
470 
471 	if (ops)
472 		ops->end_operation(cres);
473 }
474 
475 /**
476  * fscache_read - Start a read from the cache.
477  * @cres: The cache resources to use
478  * @start_pos: The beginning file offset in the cache file
479  * @iter: The buffer to fill - and also the length
480  * @read_hole: How to handle a hole in the data.
481  * @term_func: The function to call upon completion
482  * @term_func_priv: The private data for @term_func
483  *
484  * Start a read from the cache.  @cres indicates the cache object to read from
485  * and must be obtained by a call to fscache_begin_operation() beforehand.
486  *
487  * The data is read into the iterator, @iter, and that also indicates the size
488  * of the operation.  @start_pos is the start position in the file, though if
489  * @seek_data is set appropriately, the cache can use SEEK_DATA to find the
490  * next piece of data, writing zeros for the hole into the iterator.
491  *
492  * Upon termination of the operation, @term_func will be called and supplied
493  * with @term_func_priv plus the amount of data written, if successful, or the
494  * error code otherwise.
495  *
496  * @read_hole indicates how a partially populated region in the cache should be
497  * handled.  It can be one of a number of settings:
498  *
499  *	NETFS_READ_HOLE_IGNORE - Just try to read (may return a short read).
500  *
501  *	NETFS_READ_HOLE_CLEAR - Seek for data, clearing the part of the buffer
502  *				skipped over, then do as for IGNORE.
503  *
504  *	NETFS_READ_HOLE_FAIL - Give ENODATA if we encounter a hole.
505  */
506 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)507 int fscache_read(struct netfs_cache_resources *cres,
508 		 loff_t start_pos,
509 		 struct iov_iter *iter,
510 		 enum netfs_read_from_hole read_hole,
511 		 netfs_io_terminated_t term_func,
512 		 void *term_func_priv)
513 {
514 	const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
515 	return ops->read(cres, start_pos, iter, read_hole,
516 			 term_func, term_func_priv);
517 }
518 
519 /**
520  * fscache_begin_write_operation - Begin a write operation for the netfs lib
521  * @cres: The cache resources for the write being performed
522  * @cookie: The cookie representing the cache object
523  *
524  * Begin a write operation on behalf of the netfs helper library.  @cres
525  * indicates the cache resources to which the operation state should be
526  * attached; @cookie indicates the cache object that will be accessed.
527  *
528  * @cres->inval_counter is set from @cookie->inval_counter for comparison at
529  * the end of the operation.  This allows invalidation during the operation to
530  * be detected by the caller.
531  *
532  * Returns:
533  * * 0		- Success
534  * * -ENOBUFS	- No caching available
535  * * Other error code from the cache, such as -ENOMEM.
536  */
537 static inline
fscache_begin_write_operation(struct netfs_cache_resources * cres,struct fscache_cookie * cookie)538 int fscache_begin_write_operation(struct netfs_cache_resources *cres,
539 				  struct fscache_cookie *cookie)
540 {
541 	if (fscache_cookie_enabled(cookie))
542 		return __fscache_begin_write_operation(cres, cookie);
543 	return -ENOBUFS;
544 }
545 
546 /**
547  * fscache_write - Start a write to the cache.
548  * @cres: The cache resources to use
549  * @start_pos: The beginning file offset in the cache file
550  * @iter: The data to write - and also the length
551  * @term_func: The function to call upon completion
552  * @term_func_priv: The private data for @term_func
553  *
554  * Start a write to the cache.  @cres indicates the cache object to write to and
555  * must be obtained by a call to fscache_begin_operation() beforehand.
556  *
557  * The data to be written is obtained from the iterator, @iter, and that also
558  * indicates the size of the operation.  @start_pos is the start position in
559  * the file.
560  *
561  * Upon termination of the operation, @term_func will be called and supplied
562  * with @term_func_priv plus the amount of data written, if successful, or the
563  * error code otherwise.
564  */
565 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)566 int fscache_write(struct netfs_cache_resources *cres,
567 		  loff_t start_pos,
568 		  struct iov_iter *iter,
569 		  netfs_io_terminated_t term_func,
570 		  void *term_func_priv)
571 {
572 	const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
573 	return ops->write(cres, start_pos, iter, term_func, term_func_priv);
574 }
575 
576 /**
577  * fscache_clear_page_bits - Clear the PG_fscache bits from a set of pages
578  * @mapping: The netfs inode to use as the source
579  * @start: The start position in @mapping
580  * @len: The amount of data to unlock
581  * @caching: If PG_fscache has been set
582  *
583  * Clear the PG_fscache flag from a sequence of pages and wake up anyone who's
584  * waiting.
585  */
fscache_clear_page_bits(struct address_space * mapping,loff_t start,size_t len,bool caching)586 static inline void fscache_clear_page_bits(struct address_space *mapping,
587 					   loff_t start, size_t len,
588 					   bool caching)
589 {
590 	if (caching)
591 		__fscache_clear_page_bits(mapping, start, len);
592 }
593 
594 /**
595  * fscache_write_to_cache - Save a write to the cache and clear PG_fscache
596  * @cookie: The cookie representing the cache object
597  * @mapping: The netfs inode to use as the source
598  * @start: The start position in @mapping
599  * @len: The amount of data to write back
600  * @i_size: The new size of the inode
601  * @term_func: The function to call upon completion
602  * @term_func_priv: The private data for @term_func
603  * @using_pgpriv2: If we're using PG_private_2 to mark in-progress write
604  * @caching: If we actually want to do the caching
605  *
606  * Helper function for a netfs to write dirty data from an inode into the cache
607  * object that's backing it.
608  *
609  * @start and @len describe the range of the data.  This does not need to be
610  * page-aligned, but to satisfy DIO requirements, the cache may expand it up to
611  * the page boundaries on either end.  All the pages covering the range must be
612  * marked with PG_fscache.
613  *
614  * If given, @term_func will be called upon completion and supplied with
615  * @term_func_priv.  Note that if @using_pgpriv2 is set, the PG_private_2 flags
616  * will have been cleared by this point, so the netfs must retain its own pin
617  * on the mapping.
618  */
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)619 static inline void fscache_write_to_cache(struct fscache_cookie *cookie,
620 					  struct address_space *mapping,
621 					  loff_t start, size_t len, loff_t i_size,
622 					  netfs_io_terminated_t term_func,
623 					  void *term_func_priv,
624 					  bool using_pgpriv2, bool caching)
625 {
626 	if (caching)
627 		__fscache_write_to_cache(cookie, mapping, start, len, i_size,
628 					 term_func, term_func_priv,
629 					 using_pgpriv2, caching);
630 	else if (term_func)
631 		term_func(term_func_priv, -ENOBUFS, false);
632 
633 }
634 
635 /**
636  * fscache_note_page_release - Note that a netfs page got released
637  * @cookie: The cookie corresponding to the file
638  *
639  * Note that a page that has been copied to the cache has been released.  This
640  * means that future reads will need to look in the cache to see if it's there.
641  */
642 static inline
fscache_note_page_release(struct fscache_cookie * cookie)643 void fscache_note_page_release(struct fscache_cookie *cookie)
644 {
645 	/* If we've written data to the cache (HAVE_DATA) and there wasn't any
646 	 * data in the cache when we started (NO_DATA_TO_READ), it may no
647 	 * longer be true that we can skip reading from the cache - so clear
648 	 * the flag that causes reads to be skipped.
649 	 */
650 	if (cookie &&
651 	    test_bit(FSCACHE_COOKIE_HAVE_DATA, &cookie->flags) &&
652 	    test_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags))
653 		clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
654 }
655 
656 #endif /* _LINUX_FSCACHE_H */
657