xref: /OK3568_Linux_fs/kernel/include/linux/fscache.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /* General filesystem caching interface
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun  * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * NOTE!!! See:
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *	Documentation/filesystems/caching/netfs-api.rst
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * for a description of the network filesystem interface declared here.
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #ifndef _LINUX_FSCACHE_H
15*4882a593Smuzhiyun #define _LINUX_FSCACHE_H
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/fs.h>
18*4882a593Smuzhiyun #include <linux/list.h>
19*4882a593Smuzhiyun #include <linux/pagemap.h>
20*4882a593Smuzhiyun #include <linux/pagevec.h>
21*4882a593Smuzhiyun #include <linux/list_bl.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
24*4882a593Smuzhiyun #define fscache_available() (1)
25*4882a593Smuzhiyun #define fscache_cookie_valid(cookie) (cookie)
26*4882a593Smuzhiyun #else
27*4882a593Smuzhiyun #define fscache_available() (0)
28*4882a593Smuzhiyun #define fscache_cookie_valid(cookie) (0)
29*4882a593Smuzhiyun #endif
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun  * overload PG_private_2 to give us PG_fscache - this is used to indicate that
34*4882a593Smuzhiyun  * a page is currently backed by a local disk cache
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun #define PageFsCache(page)		PagePrivate2((page))
37*4882a593Smuzhiyun #define SetPageFsCache(page)		SetPagePrivate2((page))
38*4882a593Smuzhiyun #define ClearPageFsCache(page)		ClearPagePrivate2((page))
39*4882a593Smuzhiyun #define TestSetPageFsCache(page)	TestSetPagePrivate2((page))
40*4882a593Smuzhiyun #define TestClearPageFsCache(page)	TestClearPagePrivate2((page))
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* pattern used to fill dead space in an index entry */
43*4882a593Smuzhiyun #define FSCACHE_INDEX_DEADFILL_PATTERN 0x79
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun struct pagevec;
46*4882a593Smuzhiyun struct fscache_cache_tag;
47*4882a593Smuzhiyun struct fscache_cookie;
48*4882a593Smuzhiyun struct fscache_netfs;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun typedef void (*fscache_rw_complete_t)(struct page *page,
51*4882a593Smuzhiyun 				      void *context,
52*4882a593Smuzhiyun 				      int error);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /* result of index entry consultation */
55*4882a593Smuzhiyun enum fscache_checkaux {
56*4882a593Smuzhiyun 	FSCACHE_CHECKAUX_OKAY,		/* entry okay as is */
57*4882a593Smuzhiyun 	FSCACHE_CHECKAUX_NEEDS_UPDATE,	/* entry requires update */
58*4882a593Smuzhiyun 	FSCACHE_CHECKAUX_OBSOLETE,	/* entry requires deletion */
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * fscache cookie definition
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun struct fscache_cookie_def {
65*4882a593Smuzhiyun 	/* name of cookie type */
66*4882a593Smuzhiyun 	char name[16];
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	/* cookie type */
69*4882a593Smuzhiyun 	uint8_t type;
70*4882a593Smuzhiyun #define FSCACHE_COOKIE_TYPE_INDEX	0
71*4882a593Smuzhiyun #define FSCACHE_COOKIE_TYPE_DATAFILE	1
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	/* select the cache into which to insert an entry in this index
74*4882a593Smuzhiyun 	 * - optional
75*4882a593Smuzhiyun 	 * - should return a cache identifier or NULL to cause the cache to be
76*4882a593Smuzhiyun 	 *   inherited from the parent if possible or the first cache picked
77*4882a593Smuzhiyun 	 *   for a non-index file if not
78*4882a593Smuzhiyun 	 */
79*4882a593Smuzhiyun 	struct fscache_cache_tag *(*select_cache)(
80*4882a593Smuzhiyun 		const void *parent_netfs_data,
81*4882a593Smuzhiyun 		const void *cookie_netfs_data);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	/* consult the netfs about the state of an object
84*4882a593Smuzhiyun 	 * - this function can be absent if the index carries no state data
85*4882a593Smuzhiyun 	 * - the netfs data from the cookie being used as the target is
86*4882a593Smuzhiyun 	 *   presented, as is the auxiliary data and the object size
87*4882a593Smuzhiyun 	 */
88*4882a593Smuzhiyun 	enum fscache_checkaux (*check_aux)(void *cookie_netfs_data,
89*4882a593Smuzhiyun 					   const void *data,
90*4882a593Smuzhiyun 					   uint16_t datalen,
91*4882a593Smuzhiyun 					   loff_t object_size);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	/* get an extra reference on a read context
94*4882a593Smuzhiyun 	 * - this function can be absent if the completion function doesn't
95*4882a593Smuzhiyun 	 *   require a context
96*4882a593Smuzhiyun 	 */
97*4882a593Smuzhiyun 	void (*get_context)(void *cookie_netfs_data, void *context);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* release an extra reference on a read context
100*4882a593Smuzhiyun 	 * - this function can be absent if the completion function doesn't
101*4882a593Smuzhiyun 	 *   require a context
102*4882a593Smuzhiyun 	 */
103*4882a593Smuzhiyun 	void (*put_context)(void *cookie_netfs_data, void *context);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* indicate page that now have cache metadata retained
106*4882a593Smuzhiyun 	 * - this function should mark the specified page as now being cached
107*4882a593Smuzhiyun 	 * - the page will have been marked with PG_fscache before this is
108*4882a593Smuzhiyun 	 *   called, so this is optional
109*4882a593Smuzhiyun 	 */
110*4882a593Smuzhiyun 	void (*mark_page_cached)(void *cookie_netfs_data,
111*4882a593Smuzhiyun 				 struct address_space *mapping,
112*4882a593Smuzhiyun 				 struct page *page);
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun  * fscache cached network filesystem type
117*4882a593Smuzhiyun  * - name, version and ops must be filled in before registration
118*4882a593Smuzhiyun  * - all other fields will be set during registration
119*4882a593Smuzhiyun  */
120*4882a593Smuzhiyun struct fscache_netfs {
121*4882a593Smuzhiyun 	uint32_t			version;	/* indexing version */
122*4882a593Smuzhiyun 	const char			*name;		/* filesystem name */
123*4882a593Smuzhiyun 	struct fscache_cookie		*primary_index;
124*4882a593Smuzhiyun };
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun  * data file or index object cookie
128*4882a593Smuzhiyun  * - a file will only appear in one cache
129*4882a593Smuzhiyun  * - a request to cache a file may or may not be honoured, subject to
130*4882a593Smuzhiyun  *   constraints such as disk space
131*4882a593Smuzhiyun  * - indices are created on disk just-in-time
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun struct fscache_cookie {
134*4882a593Smuzhiyun 	atomic_t			usage;		/* number of users of this cookie */
135*4882a593Smuzhiyun 	atomic_t			n_children;	/* number of children of this cookie */
136*4882a593Smuzhiyun 	atomic_t			n_active;	/* number of active users of netfs ptrs */
137*4882a593Smuzhiyun 	spinlock_t			lock;
138*4882a593Smuzhiyun 	spinlock_t			stores_lock;	/* lock on page store tree */
139*4882a593Smuzhiyun 	struct hlist_head		backing_objects; /* object(s) backing this file/index */
140*4882a593Smuzhiyun 	const struct fscache_cookie_def	*def;		/* definition */
141*4882a593Smuzhiyun 	struct fscache_cookie		*parent;	/* parent of this entry */
142*4882a593Smuzhiyun 	struct hlist_bl_node		hash_link;	/* Link in hash table */
143*4882a593Smuzhiyun 	void				*netfs_data;	/* back pointer to netfs */
144*4882a593Smuzhiyun 	struct radix_tree_root		stores;		/* pages to be stored on this cookie */
145*4882a593Smuzhiyun #define FSCACHE_COOKIE_PENDING_TAG	0		/* pages tag: pending write to cache */
146*4882a593Smuzhiyun #define FSCACHE_COOKIE_STORING_TAG	1		/* pages tag: writing to cache */
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	unsigned long			flags;
149*4882a593Smuzhiyun #define FSCACHE_COOKIE_LOOKING_UP	0	/* T if non-index cookie being looked up still */
150*4882a593Smuzhiyun #define FSCACHE_COOKIE_NO_DATA_YET	1	/* T if new object with no cached data yet */
151*4882a593Smuzhiyun #define FSCACHE_COOKIE_UNAVAILABLE	2	/* T if cookie is unavailable (error, etc) */
152*4882a593Smuzhiyun #define FSCACHE_COOKIE_INVALIDATING	3	/* T if cookie is being invalidated */
153*4882a593Smuzhiyun #define FSCACHE_COOKIE_RELINQUISHED	4	/* T if cookie has been relinquished */
154*4882a593Smuzhiyun #define FSCACHE_COOKIE_ENABLED		5	/* T if cookie is enabled */
155*4882a593Smuzhiyun #define FSCACHE_COOKIE_ENABLEMENT_LOCK	6	/* T if cookie is being en/disabled */
156*4882a593Smuzhiyun #define FSCACHE_COOKIE_AUX_UPDATED	8	/* T if the auxiliary data was updated */
157*4882a593Smuzhiyun #define FSCACHE_COOKIE_ACQUIRED		9	/* T if cookie is in use */
158*4882a593Smuzhiyun #define FSCACHE_COOKIE_RELINQUISHING	10	/* T if cookie is being relinquished */
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	u8				type;		/* Type of object */
161*4882a593Smuzhiyun 	u8				key_len;	/* Length of index key */
162*4882a593Smuzhiyun 	u8				aux_len;	/* Length of auxiliary data */
163*4882a593Smuzhiyun 	u32				key_hash;	/* Hash of parent, type, key, len */
164*4882a593Smuzhiyun 	union {
165*4882a593Smuzhiyun 		void			*key;		/* Index key */
166*4882a593Smuzhiyun 		u8			inline_key[16];	/* - If the key is short enough */
167*4882a593Smuzhiyun 	};
168*4882a593Smuzhiyun 	union {
169*4882a593Smuzhiyun 		void			*aux;		/* Auxiliary data */
170*4882a593Smuzhiyun 		u8			inline_aux[8];	/* - If the aux data is short enough */
171*4882a593Smuzhiyun 	};
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun 
fscache_cookie_enabled(struct fscache_cookie * cookie)174*4882a593Smuzhiyun static inline bool fscache_cookie_enabled(struct fscache_cookie *cookie)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	return test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun  * slow-path functions for when there is actually caching available, and the
181*4882a593Smuzhiyun  * netfs does actually have a valid token
182*4882a593Smuzhiyun  * - these are not to be called directly
183*4882a593Smuzhiyun  * - these are undefined symbols when FS-Cache is not configured and the
184*4882a593Smuzhiyun  *   optimiser takes care of not using them
185*4882a593Smuzhiyun  */
186*4882a593Smuzhiyun extern int __fscache_register_netfs(struct fscache_netfs *);
187*4882a593Smuzhiyun extern void __fscache_unregister_netfs(struct fscache_netfs *);
188*4882a593Smuzhiyun extern struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *);
189*4882a593Smuzhiyun extern void __fscache_release_cache_tag(struct fscache_cache_tag *);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun extern struct fscache_cookie *__fscache_acquire_cookie(
192*4882a593Smuzhiyun 	struct fscache_cookie *,
193*4882a593Smuzhiyun 	const struct fscache_cookie_def *,
194*4882a593Smuzhiyun 	const void *, size_t,
195*4882a593Smuzhiyun 	const void *, size_t,
196*4882a593Smuzhiyun 	void *, loff_t, bool);
197*4882a593Smuzhiyun extern void __fscache_relinquish_cookie(struct fscache_cookie *, const void *, bool);
198*4882a593Smuzhiyun extern int __fscache_check_consistency(struct fscache_cookie *, const void *);
199*4882a593Smuzhiyun extern void __fscache_update_cookie(struct fscache_cookie *, const void *);
200*4882a593Smuzhiyun extern int __fscache_attr_changed(struct fscache_cookie *);
201*4882a593Smuzhiyun extern void __fscache_invalidate(struct fscache_cookie *);
202*4882a593Smuzhiyun extern void __fscache_wait_on_invalidate(struct fscache_cookie *);
203*4882a593Smuzhiyun extern int __fscache_read_or_alloc_page(struct fscache_cookie *,
204*4882a593Smuzhiyun 					struct page *,
205*4882a593Smuzhiyun 					fscache_rw_complete_t,
206*4882a593Smuzhiyun 					void *,
207*4882a593Smuzhiyun 					gfp_t);
208*4882a593Smuzhiyun extern int __fscache_read_or_alloc_pages(struct fscache_cookie *,
209*4882a593Smuzhiyun 					 struct address_space *,
210*4882a593Smuzhiyun 					 struct list_head *,
211*4882a593Smuzhiyun 					 unsigned *,
212*4882a593Smuzhiyun 					 fscache_rw_complete_t,
213*4882a593Smuzhiyun 					 void *,
214*4882a593Smuzhiyun 					 gfp_t);
215*4882a593Smuzhiyun extern int __fscache_alloc_page(struct fscache_cookie *, struct page *, gfp_t);
216*4882a593Smuzhiyun extern int __fscache_write_page(struct fscache_cookie *, struct page *, loff_t, gfp_t);
217*4882a593Smuzhiyun extern void __fscache_uncache_page(struct fscache_cookie *, struct page *);
218*4882a593Smuzhiyun extern bool __fscache_check_page_write(struct fscache_cookie *, struct page *);
219*4882a593Smuzhiyun extern void __fscache_wait_on_page_write(struct fscache_cookie *, struct page *);
220*4882a593Smuzhiyun extern bool __fscache_maybe_release_page(struct fscache_cookie *, struct page *,
221*4882a593Smuzhiyun 					 gfp_t);
222*4882a593Smuzhiyun extern void __fscache_uncache_all_inode_pages(struct fscache_cookie *,
223*4882a593Smuzhiyun 					      struct inode *);
224*4882a593Smuzhiyun extern void __fscache_readpages_cancel(struct fscache_cookie *cookie,
225*4882a593Smuzhiyun 				       struct list_head *pages);
226*4882a593Smuzhiyun extern void __fscache_disable_cookie(struct fscache_cookie *, const void *, bool);
227*4882a593Smuzhiyun extern void __fscache_enable_cookie(struct fscache_cookie *, const void *, loff_t,
228*4882a593Smuzhiyun 				    bool (*)(void *), void *);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun /**
231*4882a593Smuzhiyun  * fscache_register_netfs - Register a filesystem as desiring caching services
232*4882a593Smuzhiyun  * @netfs: The description of the filesystem
233*4882a593Smuzhiyun  *
234*4882a593Smuzhiyun  * Register a filesystem as desiring caching services if they're available.
235*4882a593Smuzhiyun  *
236*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
237*4882a593Smuzhiyun  * description.
238*4882a593Smuzhiyun  */
239*4882a593Smuzhiyun static inline
fscache_register_netfs(struct fscache_netfs * netfs)240*4882a593Smuzhiyun int fscache_register_netfs(struct fscache_netfs *netfs)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	if (fscache_available())
243*4882a593Smuzhiyun 		return __fscache_register_netfs(netfs);
244*4882a593Smuzhiyun 	else
245*4882a593Smuzhiyun 		return 0;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun /**
249*4882a593Smuzhiyun  * fscache_unregister_netfs - Indicate that a filesystem no longer desires
250*4882a593Smuzhiyun  * caching services
251*4882a593Smuzhiyun  * @netfs: The description of the filesystem
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * Indicate that a filesystem no longer desires caching services for the
254*4882a593Smuzhiyun  * moment.
255*4882a593Smuzhiyun  *
256*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
257*4882a593Smuzhiyun  * description.
258*4882a593Smuzhiyun  */
259*4882a593Smuzhiyun static inline
fscache_unregister_netfs(struct fscache_netfs * netfs)260*4882a593Smuzhiyun void fscache_unregister_netfs(struct fscache_netfs *netfs)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	if (fscache_available())
263*4882a593Smuzhiyun 		__fscache_unregister_netfs(netfs);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun /**
267*4882a593Smuzhiyun  * fscache_lookup_cache_tag - Look up a cache tag
268*4882a593Smuzhiyun  * @name: The name of the tag to search for
269*4882a593Smuzhiyun  *
270*4882a593Smuzhiyun  * Acquire a specific cache referral tag that can be used to select a specific
271*4882a593Smuzhiyun  * cache in which to cache an index.
272*4882a593Smuzhiyun  *
273*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
274*4882a593Smuzhiyun  * description.
275*4882a593Smuzhiyun  */
276*4882a593Smuzhiyun static inline
fscache_lookup_cache_tag(const char * name)277*4882a593Smuzhiyun struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	if (fscache_available())
280*4882a593Smuzhiyun 		return __fscache_lookup_cache_tag(name);
281*4882a593Smuzhiyun 	else
282*4882a593Smuzhiyun 		return NULL;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun /**
286*4882a593Smuzhiyun  * fscache_release_cache_tag - Release a cache tag
287*4882a593Smuzhiyun  * @tag: The tag to release
288*4882a593Smuzhiyun  *
289*4882a593Smuzhiyun  * Release a reference to a cache referral tag previously looked up.
290*4882a593Smuzhiyun  *
291*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
292*4882a593Smuzhiyun  * description.
293*4882a593Smuzhiyun  */
294*4882a593Smuzhiyun static inline
fscache_release_cache_tag(struct fscache_cache_tag * tag)295*4882a593Smuzhiyun void fscache_release_cache_tag(struct fscache_cache_tag *tag)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun 	if (fscache_available())
298*4882a593Smuzhiyun 		__fscache_release_cache_tag(tag);
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun /**
302*4882a593Smuzhiyun  * fscache_acquire_cookie - Acquire a cookie to represent a cache object
303*4882a593Smuzhiyun  * @parent: The cookie that's to be the parent of this one
304*4882a593Smuzhiyun  * @def: A description of the cache object, including callback operations
305*4882a593Smuzhiyun  * @index_key: The index key for this cookie
306*4882a593Smuzhiyun  * @index_key_len: Size of the index key
307*4882a593Smuzhiyun  * @aux_data: The auxiliary data for the cookie (may be NULL)
308*4882a593Smuzhiyun  * @aux_data_len: Size of the auxiliary data buffer
309*4882a593Smuzhiyun  * @netfs_data: An arbitrary piece of data to be kept in the cookie to
310*4882a593Smuzhiyun  * represent the cache object to the netfs
311*4882a593Smuzhiyun  * @object_size: The initial size of object
312*4882a593Smuzhiyun  * @enable: Whether or not to enable a data cookie immediately
313*4882a593Smuzhiyun  *
314*4882a593Smuzhiyun  * This function is used to inform FS-Cache about part of an index hierarchy
315*4882a593Smuzhiyun  * that can be used to locate files.  This is done by requesting a cookie for
316*4882a593Smuzhiyun  * each index in the path to the file.
317*4882a593Smuzhiyun  *
318*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
319*4882a593Smuzhiyun  * description.
320*4882a593Smuzhiyun  */
321*4882a593Smuzhiyun static inline
fscache_acquire_cookie(struct fscache_cookie * parent,const struct fscache_cookie_def * def,const void * index_key,size_t index_key_len,const void * aux_data,size_t aux_data_len,void * netfs_data,loff_t object_size,bool enable)322*4882a593Smuzhiyun struct fscache_cookie *fscache_acquire_cookie(
323*4882a593Smuzhiyun 	struct fscache_cookie *parent,
324*4882a593Smuzhiyun 	const struct fscache_cookie_def *def,
325*4882a593Smuzhiyun 	const void *index_key,
326*4882a593Smuzhiyun 	size_t index_key_len,
327*4882a593Smuzhiyun 	const void *aux_data,
328*4882a593Smuzhiyun 	size_t aux_data_len,
329*4882a593Smuzhiyun 	void *netfs_data,
330*4882a593Smuzhiyun 	loff_t object_size,
331*4882a593Smuzhiyun 	bool enable)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	if (fscache_cookie_valid(parent) && fscache_cookie_enabled(parent))
334*4882a593Smuzhiyun 		return __fscache_acquire_cookie(parent, def,
335*4882a593Smuzhiyun 						index_key, index_key_len,
336*4882a593Smuzhiyun 						aux_data, aux_data_len,
337*4882a593Smuzhiyun 						netfs_data, object_size, enable);
338*4882a593Smuzhiyun 	else
339*4882a593Smuzhiyun 		return NULL;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun /**
343*4882a593Smuzhiyun  * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
344*4882a593Smuzhiyun  * it
345*4882a593Smuzhiyun  * @cookie: The cookie being returned
346*4882a593Smuzhiyun  * @aux_data: The updated auxiliary data for the cookie (may be NULL)
347*4882a593Smuzhiyun  * @retire: True if the cache object the cookie represents is to be discarded
348*4882a593Smuzhiyun  *
349*4882a593Smuzhiyun  * This function returns a cookie to the cache, forcibly discarding the
350*4882a593Smuzhiyun  * associated cache object if retire is set to true.  The opportunity is
351*4882a593Smuzhiyun  * provided to update the auxiliary data in the cache before the object is
352*4882a593Smuzhiyun  * disconnected.
353*4882a593Smuzhiyun  *
354*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
355*4882a593Smuzhiyun  * description.
356*4882a593Smuzhiyun  */
357*4882a593Smuzhiyun static inline
fscache_relinquish_cookie(struct fscache_cookie * cookie,const void * aux_data,bool retire)358*4882a593Smuzhiyun void fscache_relinquish_cookie(struct fscache_cookie *cookie,
359*4882a593Smuzhiyun 			       const void *aux_data,
360*4882a593Smuzhiyun 			       bool retire)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie))
363*4882a593Smuzhiyun 		__fscache_relinquish_cookie(cookie, aux_data, retire);
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun /**
367*4882a593Smuzhiyun  * fscache_check_consistency - Request validation of a cache's auxiliary data
368*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
369*4882a593Smuzhiyun  * @aux_data: The updated auxiliary data for the cookie (may be NULL)
370*4882a593Smuzhiyun  *
371*4882a593Smuzhiyun  * Request an consistency check from fscache, which passes the request to the
372*4882a593Smuzhiyun  * backing cache.  The auxiliary data on the cookie will be updated first if
373*4882a593Smuzhiyun  * @aux_data is set.
374*4882a593Smuzhiyun  *
375*4882a593Smuzhiyun  * Returns 0 if consistent and -ESTALE if inconsistent.  May also
376*4882a593Smuzhiyun  * return -ENOMEM and -ERESTARTSYS.
377*4882a593Smuzhiyun  */
378*4882a593Smuzhiyun static inline
fscache_check_consistency(struct fscache_cookie * cookie,const void * aux_data)379*4882a593Smuzhiyun int fscache_check_consistency(struct fscache_cookie *cookie,
380*4882a593Smuzhiyun 			      const void *aux_data)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
383*4882a593Smuzhiyun 		return __fscache_check_consistency(cookie, aux_data);
384*4882a593Smuzhiyun 	else
385*4882a593Smuzhiyun 		return 0;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun /**
389*4882a593Smuzhiyun  * fscache_update_cookie - Request that a cache object be updated
390*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
391*4882a593Smuzhiyun  * @aux_data: The updated auxiliary data for the cookie (may be NULL)
392*4882a593Smuzhiyun  *
393*4882a593Smuzhiyun  * Request an update of the index data for the cache object associated with the
394*4882a593Smuzhiyun  * cookie.  The auxiliary data on the cookie will be updated first if @aux_data
395*4882a593Smuzhiyun  * is set.
396*4882a593Smuzhiyun  *
397*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
398*4882a593Smuzhiyun  * description.
399*4882a593Smuzhiyun  */
400*4882a593Smuzhiyun static inline
fscache_update_cookie(struct fscache_cookie * cookie,const void * aux_data)401*4882a593Smuzhiyun void fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
404*4882a593Smuzhiyun 		__fscache_update_cookie(cookie, aux_data);
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun /**
408*4882a593Smuzhiyun  * fscache_pin_cookie - Pin a data-storage cache object in its cache
409*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
410*4882a593Smuzhiyun  *
411*4882a593Smuzhiyun  * Permit data-storage cache objects to be pinned in the cache.
412*4882a593Smuzhiyun  *
413*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
414*4882a593Smuzhiyun  * description.
415*4882a593Smuzhiyun  */
416*4882a593Smuzhiyun static inline
fscache_pin_cookie(struct fscache_cookie * cookie)417*4882a593Smuzhiyun int fscache_pin_cookie(struct fscache_cookie *cookie)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	return -ENOBUFS;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun /**
423*4882a593Smuzhiyun  * fscache_pin_cookie - Unpin a data-storage cache object in its cache
424*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
425*4882a593Smuzhiyun  *
426*4882a593Smuzhiyun  * Permit data-storage cache objects to be unpinned from the cache.
427*4882a593Smuzhiyun  *
428*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
429*4882a593Smuzhiyun  * description.
430*4882a593Smuzhiyun  */
431*4882a593Smuzhiyun static inline
fscache_unpin_cookie(struct fscache_cookie * cookie)432*4882a593Smuzhiyun void fscache_unpin_cookie(struct fscache_cookie *cookie)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun /**
437*4882a593Smuzhiyun  * fscache_attr_changed - Notify cache that an object's attributes changed
438*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
439*4882a593Smuzhiyun  *
440*4882a593Smuzhiyun  * Send a notification to the cache indicating that an object's attributes have
441*4882a593Smuzhiyun  * changed.  This includes the data size.  These attributes will be obtained
442*4882a593Smuzhiyun  * through the get_attr() cookie definition op.
443*4882a593Smuzhiyun  *
444*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
445*4882a593Smuzhiyun  * description.
446*4882a593Smuzhiyun  */
447*4882a593Smuzhiyun static inline
fscache_attr_changed(struct fscache_cookie * cookie)448*4882a593Smuzhiyun int fscache_attr_changed(struct fscache_cookie *cookie)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
451*4882a593Smuzhiyun 		return __fscache_attr_changed(cookie);
452*4882a593Smuzhiyun 	else
453*4882a593Smuzhiyun 		return -ENOBUFS;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun /**
457*4882a593Smuzhiyun  * fscache_invalidate - Notify cache that an object needs invalidation
458*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
459*4882a593Smuzhiyun  *
460*4882a593Smuzhiyun  * Notify the cache that an object is needs to be invalidated and that it
461*4882a593Smuzhiyun  * should abort any retrievals or stores it is doing on the cache.  The object
462*4882a593Smuzhiyun  * is then marked non-caching until such time as the invalidation is complete.
463*4882a593Smuzhiyun  *
464*4882a593Smuzhiyun  * This can be called with spinlocks held.
465*4882a593Smuzhiyun  *
466*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
467*4882a593Smuzhiyun  * description.
468*4882a593Smuzhiyun  */
469*4882a593Smuzhiyun static inline
fscache_invalidate(struct fscache_cookie * cookie)470*4882a593Smuzhiyun void fscache_invalidate(struct fscache_cookie *cookie)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
473*4882a593Smuzhiyun 		__fscache_invalidate(cookie);
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun /**
477*4882a593Smuzhiyun  * fscache_wait_on_invalidate - Wait for invalidation to complete
478*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
479*4882a593Smuzhiyun  *
480*4882a593Smuzhiyun  * Wait for the invalidation of an object to complete.
481*4882a593Smuzhiyun  *
482*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
483*4882a593Smuzhiyun  * description.
484*4882a593Smuzhiyun  */
485*4882a593Smuzhiyun static inline
fscache_wait_on_invalidate(struct fscache_cookie * cookie)486*4882a593Smuzhiyun void fscache_wait_on_invalidate(struct fscache_cookie *cookie)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie))
489*4882a593Smuzhiyun 		__fscache_wait_on_invalidate(cookie);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun /**
493*4882a593Smuzhiyun  * fscache_reserve_space - Reserve data space for a cached object
494*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
495*4882a593Smuzhiyun  * @i_size: The amount of space to be reserved
496*4882a593Smuzhiyun  *
497*4882a593Smuzhiyun  * Reserve an amount of space in the cache for the cache object attached to a
498*4882a593Smuzhiyun  * cookie so that a write to that object within the space can always be
499*4882a593Smuzhiyun  * honoured.
500*4882a593Smuzhiyun  *
501*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
502*4882a593Smuzhiyun  * description.
503*4882a593Smuzhiyun  */
504*4882a593Smuzhiyun static inline
fscache_reserve_space(struct fscache_cookie * cookie,loff_t size)505*4882a593Smuzhiyun int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun 	return -ENOBUFS;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun /**
511*4882a593Smuzhiyun  * fscache_read_or_alloc_page - Read a page from the cache or allocate a block
512*4882a593Smuzhiyun  * in which to store it
513*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
514*4882a593Smuzhiyun  * @page: The netfs page to fill if possible
515*4882a593Smuzhiyun  * @end_io_func: The callback to invoke when and if the page is filled
516*4882a593Smuzhiyun  * @context: An arbitrary piece of data to pass on to end_io_func()
517*4882a593Smuzhiyun  * @gfp: The conditions under which memory allocation should be made
518*4882a593Smuzhiyun  *
519*4882a593Smuzhiyun  * Read a page from the cache, or if that's not possible make a potential
520*4882a593Smuzhiyun  * one-block reservation in the cache into which the page may be stored once
521*4882a593Smuzhiyun  * fetched from the server.
522*4882a593Smuzhiyun  *
523*4882a593Smuzhiyun  * If the page is not backed by the cache object, or if it there's some reason
524*4882a593Smuzhiyun  * it can't be, -ENOBUFS will be returned and nothing more will be done for
525*4882a593Smuzhiyun  * that page.
526*4882a593Smuzhiyun  *
527*4882a593Smuzhiyun  * Else, if that page is backed by the cache, a read will be initiated directly
528*4882a593Smuzhiyun  * to the netfs's page and 0 will be returned by this function.  The
529*4882a593Smuzhiyun  * end_io_func() callback will be invoked when the operation terminates on a
530*4882a593Smuzhiyun  * completion or failure.  Note that the callback may be invoked before the
531*4882a593Smuzhiyun  * return.
532*4882a593Smuzhiyun  *
533*4882a593Smuzhiyun  * Else, if the page is unbacked, -ENODATA is returned and a block may have
534*4882a593Smuzhiyun  * been allocated in the cache.
535*4882a593Smuzhiyun  *
536*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
537*4882a593Smuzhiyun  * description.
538*4882a593Smuzhiyun  */
539*4882a593Smuzhiyun static inline
fscache_read_or_alloc_page(struct fscache_cookie * cookie,struct page * page,fscache_rw_complete_t end_io_func,void * context,gfp_t gfp)540*4882a593Smuzhiyun int fscache_read_or_alloc_page(struct fscache_cookie *cookie,
541*4882a593Smuzhiyun 			       struct page *page,
542*4882a593Smuzhiyun 			       fscache_rw_complete_t end_io_func,
543*4882a593Smuzhiyun 			       void *context,
544*4882a593Smuzhiyun 			       gfp_t gfp)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
547*4882a593Smuzhiyun 		return __fscache_read_or_alloc_page(cookie, page, end_io_func,
548*4882a593Smuzhiyun 						    context, gfp);
549*4882a593Smuzhiyun 	else
550*4882a593Smuzhiyun 		return -ENOBUFS;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun /**
554*4882a593Smuzhiyun  * fscache_read_or_alloc_pages - Read pages from the cache and/or allocate
555*4882a593Smuzhiyun  * blocks in which to store them
556*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
557*4882a593Smuzhiyun  * @mapping: The netfs inode mapping to which the pages will be attached
558*4882a593Smuzhiyun  * @pages: A list of potential netfs pages to be filled
559*4882a593Smuzhiyun  * @nr_pages: Number of pages to be read and/or allocated
560*4882a593Smuzhiyun  * @end_io_func: The callback to invoke when and if each page is filled
561*4882a593Smuzhiyun  * @context: An arbitrary piece of data to pass on to end_io_func()
562*4882a593Smuzhiyun  * @gfp: The conditions under which memory allocation should be made
563*4882a593Smuzhiyun  *
564*4882a593Smuzhiyun  * Read a set of pages from the cache, or if that's not possible, attempt to
565*4882a593Smuzhiyun  * make a potential one-block reservation for each page in the cache into which
566*4882a593Smuzhiyun  * that page may be stored once fetched from the server.
567*4882a593Smuzhiyun  *
568*4882a593Smuzhiyun  * If some pages are not backed by the cache object, or if it there's some
569*4882a593Smuzhiyun  * reason they can't be, -ENOBUFS will be returned and nothing more will be
570*4882a593Smuzhiyun  * done for that pages.
571*4882a593Smuzhiyun  *
572*4882a593Smuzhiyun  * Else, if some of the pages are backed by the cache, a read will be initiated
573*4882a593Smuzhiyun  * directly to the netfs's page and 0 will be returned by this function.  The
574*4882a593Smuzhiyun  * end_io_func() callback will be invoked when the operation terminates on a
575*4882a593Smuzhiyun  * completion or failure.  Note that the callback may be invoked before the
576*4882a593Smuzhiyun  * return.
577*4882a593Smuzhiyun  *
578*4882a593Smuzhiyun  * Else, if a page is unbacked, -ENODATA is returned and a block may have
579*4882a593Smuzhiyun  * been allocated in the cache.
580*4882a593Smuzhiyun  *
581*4882a593Smuzhiyun  * Because the function may want to return all of -ENOBUFS, -ENODATA and 0 in
582*4882a593Smuzhiyun  * regard to different pages, the return values are prioritised in that order.
583*4882a593Smuzhiyun  * Any pages submitted for reading are removed from the pages list.
584*4882a593Smuzhiyun  *
585*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
586*4882a593Smuzhiyun  * description.
587*4882a593Smuzhiyun  */
588*4882a593Smuzhiyun static inline
fscache_read_or_alloc_pages(struct fscache_cookie * cookie,struct address_space * mapping,struct list_head * pages,unsigned * nr_pages,fscache_rw_complete_t end_io_func,void * context,gfp_t gfp)589*4882a593Smuzhiyun int fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
590*4882a593Smuzhiyun 				struct address_space *mapping,
591*4882a593Smuzhiyun 				struct list_head *pages,
592*4882a593Smuzhiyun 				unsigned *nr_pages,
593*4882a593Smuzhiyun 				fscache_rw_complete_t end_io_func,
594*4882a593Smuzhiyun 				void *context,
595*4882a593Smuzhiyun 				gfp_t gfp)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
598*4882a593Smuzhiyun 		return __fscache_read_or_alloc_pages(cookie, mapping, pages,
599*4882a593Smuzhiyun 						     nr_pages, end_io_func,
600*4882a593Smuzhiyun 						     context, gfp);
601*4882a593Smuzhiyun 	else
602*4882a593Smuzhiyun 		return -ENOBUFS;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun /**
606*4882a593Smuzhiyun  * fscache_alloc_page - Allocate a block in which to store a page
607*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
608*4882a593Smuzhiyun  * @page: The netfs page to allocate a page for
609*4882a593Smuzhiyun  * @gfp: The conditions under which memory allocation should be made
610*4882a593Smuzhiyun  *
611*4882a593Smuzhiyun  * Request Allocation a block in the cache in which to store a netfs page
612*4882a593Smuzhiyun  * without retrieving any contents from the cache.
613*4882a593Smuzhiyun  *
614*4882a593Smuzhiyun  * If the page is not backed by a file then -ENOBUFS will be returned and
615*4882a593Smuzhiyun  * nothing more will be done, and no reservation will be made.
616*4882a593Smuzhiyun  *
617*4882a593Smuzhiyun  * Else, a block will be allocated if one wasn't already, and 0 will be
618*4882a593Smuzhiyun  * returned
619*4882a593Smuzhiyun  *
620*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
621*4882a593Smuzhiyun  * description.
622*4882a593Smuzhiyun  */
623*4882a593Smuzhiyun static inline
fscache_alloc_page(struct fscache_cookie * cookie,struct page * page,gfp_t gfp)624*4882a593Smuzhiyun int fscache_alloc_page(struct fscache_cookie *cookie,
625*4882a593Smuzhiyun 		       struct page *page,
626*4882a593Smuzhiyun 		       gfp_t gfp)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
629*4882a593Smuzhiyun 		return __fscache_alloc_page(cookie, page, gfp);
630*4882a593Smuzhiyun 	else
631*4882a593Smuzhiyun 		return -ENOBUFS;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun /**
635*4882a593Smuzhiyun  * fscache_readpages_cancel - Cancel read/alloc on pages
636*4882a593Smuzhiyun  * @cookie: The cookie representing the inode's cache object.
637*4882a593Smuzhiyun  * @pages: The netfs pages that we canceled write on in readpages()
638*4882a593Smuzhiyun  *
639*4882a593Smuzhiyun  * Uncache/unreserve the pages reserved earlier in readpages() via
640*4882a593Smuzhiyun  * fscache_readpages_or_alloc() and similar.  In most successful caches in
641*4882a593Smuzhiyun  * readpages() this doesn't do anything.  In cases when the underlying netfs's
642*4882a593Smuzhiyun  * readahead failed we need to clean up the pagelist (unmark and uncache).
643*4882a593Smuzhiyun  *
644*4882a593Smuzhiyun  * This function may sleep as it may have to clean up disk state.
645*4882a593Smuzhiyun  */
646*4882a593Smuzhiyun static inline
fscache_readpages_cancel(struct fscache_cookie * cookie,struct list_head * pages)647*4882a593Smuzhiyun void fscache_readpages_cancel(struct fscache_cookie *cookie,
648*4882a593Smuzhiyun 			      struct list_head *pages)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie))
651*4882a593Smuzhiyun 		__fscache_readpages_cancel(cookie, pages);
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun /**
655*4882a593Smuzhiyun  * fscache_write_page - Request storage of a page in the cache
656*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
657*4882a593Smuzhiyun  * @page: The netfs page to store
658*4882a593Smuzhiyun  * @object_size: Updated size of object
659*4882a593Smuzhiyun  * @gfp: The conditions under which memory allocation should be made
660*4882a593Smuzhiyun  *
661*4882a593Smuzhiyun  * Request the contents of the netfs page be written into the cache.  This
662*4882a593Smuzhiyun  * request may be ignored if no cache block is currently allocated, in which
663*4882a593Smuzhiyun  * case it will return -ENOBUFS.
664*4882a593Smuzhiyun  *
665*4882a593Smuzhiyun  * If a cache block was already allocated, a write will be initiated and 0 will
666*4882a593Smuzhiyun  * be returned.  The PG_fscache_write page bit is set immediately and will then
667*4882a593Smuzhiyun  * be cleared at the completion of the write to indicate the success or failure
668*4882a593Smuzhiyun  * of the operation.  Note that the completion may happen before the return.
669*4882a593Smuzhiyun  *
670*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
671*4882a593Smuzhiyun  * description.
672*4882a593Smuzhiyun  */
673*4882a593Smuzhiyun static inline
fscache_write_page(struct fscache_cookie * cookie,struct page * page,loff_t object_size,gfp_t gfp)674*4882a593Smuzhiyun int fscache_write_page(struct fscache_cookie *cookie,
675*4882a593Smuzhiyun 		       struct page *page,
676*4882a593Smuzhiyun 		       loff_t object_size,
677*4882a593Smuzhiyun 		       gfp_t gfp)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
680*4882a593Smuzhiyun 		return __fscache_write_page(cookie, page, object_size, gfp);
681*4882a593Smuzhiyun 	else
682*4882a593Smuzhiyun 		return -ENOBUFS;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun /**
686*4882a593Smuzhiyun  * fscache_uncache_page - Indicate that caching is no longer required on a page
687*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
688*4882a593Smuzhiyun  * @page: The netfs page that was being cached.
689*4882a593Smuzhiyun  *
690*4882a593Smuzhiyun  * Tell the cache that we no longer want a page to be cached and that it should
691*4882a593Smuzhiyun  * remove any knowledge of the netfs page it may have.
692*4882a593Smuzhiyun  *
693*4882a593Smuzhiyun  * Note that this cannot cancel any outstanding I/O operations between this
694*4882a593Smuzhiyun  * page and the cache.
695*4882a593Smuzhiyun  *
696*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
697*4882a593Smuzhiyun  * description.
698*4882a593Smuzhiyun  */
699*4882a593Smuzhiyun static inline
fscache_uncache_page(struct fscache_cookie * cookie,struct page * page)700*4882a593Smuzhiyun void fscache_uncache_page(struct fscache_cookie *cookie,
701*4882a593Smuzhiyun 			  struct page *page)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie))
704*4882a593Smuzhiyun 		__fscache_uncache_page(cookie, page);
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun /**
708*4882a593Smuzhiyun  * fscache_check_page_write - Ask if a page is being writing to the cache
709*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
710*4882a593Smuzhiyun  * @page: The netfs page that is being cached.
711*4882a593Smuzhiyun  *
712*4882a593Smuzhiyun  * Ask the cache if a page is being written to the cache.
713*4882a593Smuzhiyun  *
714*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
715*4882a593Smuzhiyun  * description.
716*4882a593Smuzhiyun  */
717*4882a593Smuzhiyun static inline
fscache_check_page_write(struct fscache_cookie * cookie,struct page * page)718*4882a593Smuzhiyun bool fscache_check_page_write(struct fscache_cookie *cookie,
719*4882a593Smuzhiyun 			      struct page *page)
720*4882a593Smuzhiyun {
721*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie))
722*4882a593Smuzhiyun 		return __fscache_check_page_write(cookie, page);
723*4882a593Smuzhiyun 	return false;
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun /**
727*4882a593Smuzhiyun  * fscache_wait_on_page_write - Wait for a page to complete writing to the cache
728*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
729*4882a593Smuzhiyun  * @page: The netfs page that is being cached.
730*4882a593Smuzhiyun  *
731*4882a593Smuzhiyun  * Ask the cache to wake us up when a page is no longer being written to the
732*4882a593Smuzhiyun  * cache.
733*4882a593Smuzhiyun  *
734*4882a593Smuzhiyun  * See Documentation/filesystems/caching/netfs-api.rst for a complete
735*4882a593Smuzhiyun  * description.
736*4882a593Smuzhiyun  */
737*4882a593Smuzhiyun static inline
fscache_wait_on_page_write(struct fscache_cookie * cookie,struct page * page)738*4882a593Smuzhiyun void fscache_wait_on_page_write(struct fscache_cookie *cookie,
739*4882a593Smuzhiyun 				struct page *page)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie))
742*4882a593Smuzhiyun 		__fscache_wait_on_page_write(cookie, page);
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun /**
746*4882a593Smuzhiyun  * fscache_maybe_release_page - Consider releasing a page, cancelling a store
747*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
748*4882a593Smuzhiyun  * @page: The netfs page that is being cached.
749*4882a593Smuzhiyun  * @gfp: The gfp flags passed to releasepage()
750*4882a593Smuzhiyun  *
751*4882a593Smuzhiyun  * Consider releasing a page for the vmscan algorithm, on behalf of the netfs's
752*4882a593Smuzhiyun  * releasepage() call.  A storage request on the page may cancelled if it is
753*4882a593Smuzhiyun  * not currently being processed.
754*4882a593Smuzhiyun  *
755*4882a593Smuzhiyun  * The function returns true if the page no longer has a storage request on it,
756*4882a593Smuzhiyun  * and false if a storage request is left in place.  If true is returned, the
757*4882a593Smuzhiyun  * page will have been passed to fscache_uncache_page().  If false is returned
758*4882a593Smuzhiyun  * the page cannot be freed yet.
759*4882a593Smuzhiyun  */
760*4882a593Smuzhiyun static inline
fscache_maybe_release_page(struct fscache_cookie * cookie,struct page * page,gfp_t gfp)761*4882a593Smuzhiyun bool fscache_maybe_release_page(struct fscache_cookie *cookie,
762*4882a593Smuzhiyun 				struct page *page,
763*4882a593Smuzhiyun 				gfp_t gfp)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && PageFsCache(page))
766*4882a593Smuzhiyun 		return __fscache_maybe_release_page(cookie, page, gfp);
767*4882a593Smuzhiyun 	return true;
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun /**
771*4882a593Smuzhiyun  * fscache_uncache_all_inode_pages - Uncache all an inode's pages
772*4882a593Smuzhiyun  * @cookie: The cookie representing the inode's cache object.
773*4882a593Smuzhiyun  * @inode: The inode to uncache pages from.
774*4882a593Smuzhiyun  *
775*4882a593Smuzhiyun  * Uncache all the pages in an inode that are marked PG_fscache, assuming them
776*4882a593Smuzhiyun  * to be associated with the given cookie.
777*4882a593Smuzhiyun  *
778*4882a593Smuzhiyun  * This function may sleep.  It will wait for pages that are being written out
779*4882a593Smuzhiyun  * and will wait whilst the PG_fscache mark is removed by the cache.
780*4882a593Smuzhiyun  */
781*4882a593Smuzhiyun static inline
fscache_uncache_all_inode_pages(struct fscache_cookie * cookie,struct inode * inode)782*4882a593Smuzhiyun void fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
783*4882a593Smuzhiyun 				     struct inode *inode)
784*4882a593Smuzhiyun {
785*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie))
786*4882a593Smuzhiyun 		__fscache_uncache_all_inode_pages(cookie, inode);
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun /**
790*4882a593Smuzhiyun  * fscache_disable_cookie - Disable a cookie
791*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
792*4882a593Smuzhiyun  * @aux_data: The updated auxiliary data for the cookie (may be NULL)
793*4882a593Smuzhiyun  * @invalidate: Invalidate the backing object
794*4882a593Smuzhiyun  *
795*4882a593Smuzhiyun  * Disable a cookie from accepting further alloc, read, write, invalidate,
796*4882a593Smuzhiyun  * update or acquire operations.  Outstanding operations can still be waited
797*4882a593Smuzhiyun  * upon and pages can still be uncached and the cookie relinquished.
798*4882a593Smuzhiyun  *
799*4882a593Smuzhiyun  * This will not return until all outstanding operations have completed.
800*4882a593Smuzhiyun  *
801*4882a593Smuzhiyun  * If @invalidate is set, then the backing object will be invalidated and
802*4882a593Smuzhiyun  * detached, otherwise it will just be detached.
803*4882a593Smuzhiyun  *
804*4882a593Smuzhiyun  * If @aux_data is set, then auxiliary data will be updated from that.
805*4882a593Smuzhiyun  */
806*4882a593Smuzhiyun static inline
fscache_disable_cookie(struct fscache_cookie * cookie,const void * aux_data,bool invalidate)807*4882a593Smuzhiyun void fscache_disable_cookie(struct fscache_cookie *cookie,
808*4882a593Smuzhiyun 			    const void *aux_data,
809*4882a593Smuzhiyun 			    bool invalidate)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
812*4882a593Smuzhiyun 		__fscache_disable_cookie(cookie, aux_data, invalidate);
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun /**
816*4882a593Smuzhiyun  * fscache_enable_cookie - Reenable a cookie
817*4882a593Smuzhiyun  * @cookie: The cookie representing the cache object
818*4882a593Smuzhiyun  * @aux_data: The updated auxiliary data for the cookie (may be NULL)
819*4882a593Smuzhiyun  * @object_size: Current size of object
820*4882a593Smuzhiyun  * @can_enable: A function to permit enablement once lock is held
821*4882a593Smuzhiyun  * @data: Data for can_enable()
822*4882a593Smuzhiyun  *
823*4882a593Smuzhiyun  * Reenable a previously disabled cookie, allowing it to accept further alloc,
824*4882a593Smuzhiyun  * read, write, invalidate, update or acquire operations.  An attempt will be
825*4882a593Smuzhiyun  * made to immediately reattach the cookie to a backing object.  If @aux_data
826*4882a593Smuzhiyun  * is set, the auxiliary data attached to the cookie will be updated.
827*4882a593Smuzhiyun  *
828*4882a593Smuzhiyun  * The can_enable() function is called (if not NULL) once the enablement lock
829*4882a593Smuzhiyun  * is held to rule on whether enablement is still permitted to go ahead.
830*4882a593Smuzhiyun  */
831*4882a593Smuzhiyun static inline
fscache_enable_cookie(struct fscache_cookie * cookie,const void * aux_data,loff_t object_size,bool (* can_enable)(void * data),void * data)832*4882a593Smuzhiyun void fscache_enable_cookie(struct fscache_cookie *cookie,
833*4882a593Smuzhiyun 			   const void *aux_data,
834*4882a593Smuzhiyun 			   loff_t object_size,
835*4882a593Smuzhiyun 			   bool (*can_enable)(void *data),
836*4882a593Smuzhiyun 			   void *data)
837*4882a593Smuzhiyun {
838*4882a593Smuzhiyun 	if (fscache_cookie_valid(cookie) && !fscache_cookie_enabled(cookie))
839*4882a593Smuzhiyun 		__fscache_enable_cookie(cookie, aux_data, object_size,
840*4882a593Smuzhiyun 					can_enable, data);
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun #endif /* _LINUX_FSCACHE_H */
844