fscache.h 29.5 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0-or-later */
2 3 4 5 6 7 8
/* General filesystem caching interface
 *
 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * NOTE!!! See:
 *
9
 *	Documentation/filesystems/caching/netfs-api.rst
10 11 12 13 14 15 16 17 18 19 20
 *
 * for a description of the network filesystem interface declared here.
 */

#ifndef _LINUX_FSCACHE_H
#define _LINUX_FSCACHE_H

#include <linux/fs.h>
#include <linux/list.h>
#include <linux/pagemap.h>
#include <linux/pagevec.h>
21
#include <linux/list_bl.h>
22
#include <linux/netfs.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

#if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
#define fscache_available() (1)
#define fscache_cookie_valid(cookie) (cookie)
#else
#define fscache_available() (0)
#define fscache_cookie_valid(cookie) (0)
#endif


/* pattern used to fill dead space in an index entry */
#define FSCACHE_INDEX_DEADFILL_PATTERN 0x79

struct pagevec;
struct fscache_cache_tag;
struct fscache_cookie;
struct fscache_netfs;
40
struct netfs_read_request;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

typedef void (*fscache_rw_complete_t)(struct page *page,
				      void *context,
				      int error);

/* result of index entry consultation */
enum fscache_checkaux {
	FSCACHE_CHECKAUX_OKAY,		/* entry okay as is */
	FSCACHE_CHECKAUX_NEEDS_UPDATE,	/* entry requires update */
	FSCACHE_CHECKAUX_OBSOLETE,	/* entry requires deletion */
};

/*
 * fscache cookie definition
 */
struct fscache_cookie_def {
	/* name of cookie type */
	char name[16];

	/* cookie type */
	uint8_t type;
#define FSCACHE_COOKIE_TYPE_INDEX	0
#define FSCACHE_COOKIE_TYPE_DATAFILE	1

	/* select the cache into which to insert an entry in this index
	 * - optional
	 * - should return a cache identifier or NULL to cause the cache to be
	 *   inherited from the parent if possible or the first cache picked
	 *   for a non-index file if not
	 */
	struct fscache_cache_tag *(*select_cache)(
		const void *parent_netfs_data,
		const void *cookie_netfs_data);

	/* consult the netfs about the state of an object
	 * - this function can be absent if the index carries no state data
	 * - the netfs data from the cookie being used as the target is
78
	 *   presented, as is the auxiliary data and the object size
79 80 81
	 */
	enum fscache_checkaux (*check_aux)(void *cookie_netfs_data,
					   const void *data,
82 83
					   uint16_t datalen,
					   loff_t object_size);
84 85 86 87 88 89 90 91 92 93 94 95 96

	/* get an extra reference on a read context
	 * - this function can be absent if the completion function doesn't
	 *   require a context
	 */
	void (*get_context)(void *cookie_netfs_data, void *context);

	/* release an extra reference on a read context
	 * - this function can be absent if the completion function doesn't
	 *   require a context
	 */
	void (*put_context)(void *cookie_netfs_data, void *context);

97 98 99
	/* indicate page that now have cache metadata retained
	 * - this function should mark the specified page as now being cached
	 * - the page will have been marked with PG_fscache before this is
100 101
	 *   called, so this is optional
	 */
102 103 104
	void (*mark_page_cached)(void *cookie_netfs_data,
				 struct address_space *mapping,
				 struct page *page);
105 106 107 108 109 110 111 112 113 114 115 116 117
};

/*
 * fscache cached network filesystem type
 * - name, version and ops must be filled in before registration
 * - all other fields will be set during registration
 */
struct fscache_netfs {
	uint32_t			version;	/* indexing version */
	const char			*name;		/* filesystem name */
	struct fscache_cookie		*primary_index;
};

118 119 120 121 122 123 124 125 126 127 128
/*
 * data file or index object cookie
 * - a file will only appear in one cache
 * - a request to cache a file may or may not be honoured, subject to
 *   constraints such as disk space
 * - indices are created on disk just-in-time
 */
struct fscache_cookie {
	atomic_t			usage;		/* number of users of this cookie */
	atomic_t			n_children;	/* number of children of this cookie */
	atomic_t			n_active;	/* number of active users of netfs ptrs */
129
	unsigned int			debug_id;
130 131 132 133 134
	spinlock_t			lock;
	spinlock_t			stores_lock;	/* lock on page store tree */
	struct hlist_head		backing_objects; /* object(s) backing this file/index */
	const struct fscache_cookie_def	*def;		/* definition */
	struct fscache_cookie		*parent;	/* parent of this entry */
135
	struct hlist_bl_node		hash_link;	/* Link in hash table */
136 137 138 139 140 141 142 143 144 145 146 147 148
	void				*netfs_data;	/* back pointer to netfs */
	struct radix_tree_root		stores;		/* pages to be stored on this cookie */
#define FSCACHE_COOKIE_PENDING_TAG	0		/* pages tag: pending write to cache */
#define FSCACHE_COOKIE_STORING_TAG	1		/* pages tag: writing to cache */

	unsigned long			flags;
#define FSCACHE_COOKIE_LOOKING_UP	0	/* T if non-index cookie being looked up still */
#define FSCACHE_COOKIE_NO_DATA_YET	1	/* T if new object with no cached data yet */
#define FSCACHE_COOKIE_UNAVAILABLE	2	/* T if cookie is unavailable (error, etc) */
#define FSCACHE_COOKIE_INVALIDATING	3	/* T if cookie is being invalidated */
#define FSCACHE_COOKIE_RELINQUISHED	4	/* T if cookie has been relinquished */
#define FSCACHE_COOKIE_ENABLED		5	/* T if cookie is enabled */
#define FSCACHE_COOKIE_ENABLEMENT_LOCK	6	/* T if cookie is being en/disabled */
149 150 151
#define FSCACHE_COOKIE_AUX_UPDATED	8	/* T if the auxiliary data was updated */
#define FSCACHE_COOKIE_ACQUIRED		9	/* T if cookie is in use */
#define FSCACHE_COOKIE_RELINQUISHING	10	/* T if cookie is being relinquished */
152 153 154 155

	u8				type;		/* Type of object */
	u8				key_len;	/* Length of index key */
	u8				aux_len;	/* Length of auxiliary data */
156
	u32				key_hash;	/* Hash of parent, type, key, len */
157 158 159 160 161 162 163 164
	union {
		void			*key;		/* Index key */
		u8			inline_key[16];	/* - If the key is short enough */
	};
	union {
		void			*aux;		/* Auxiliary data */
		u8			inline_aux[8];	/* - If the aux data is short enough */
	};
165 166 167 168 169 170 171
};

static inline bool fscache_cookie_enabled(struct fscache_cookie *cookie)
{
	return test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
}

172 173 174 175 176 177 178
/*
 * slow-path functions for when there is actually caching available, and the
 * netfs does actually have a valid token
 * - these are not to be called directly
 * - these are undefined symbols when FS-Cache is not configured and the
 *   optimiser takes care of not using them
 */
D
David Howells 已提交
179 180
extern int __fscache_register_netfs(struct fscache_netfs *);
extern void __fscache_unregister_netfs(struct fscache_netfs *);
D
David Howells 已提交
181 182
extern struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *);
extern void __fscache_release_cache_tag(struct fscache_cache_tag *);
183

184 185 186
extern struct fscache_cookie *__fscache_acquire_cookie(
	struct fscache_cookie *,
	const struct fscache_cookie_def *,
187 188
	const void *, size_t,
	const void *, size_t,
189
	void *, loff_t, bool);
190 191 192
extern void __fscache_relinquish_cookie(struct fscache_cookie *, const void *, bool);
extern int __fscache_check_consistency(struct fscache_cookie *, const void *);
extern void __fscache_update_cookie(struct fscache_cookie *, const void *);
193
extern int __fscache_attr_changed(struct fscache_cookie *);
194 195
extern void __fscache_invalidate(struct fscache_cookie *);
extern void __fscache_wait_on_invalidate(struct fscache_cookie *);
196 197 198 199

#ifdef FSCACHE_USE_NEW_IO_API
extern int __fscache_begin_read_operation(struct netfs_read_request *, struct fscache_cookie *);
#else
200 201 202 203 204 205 206 207 208 209 210 211 212
extern int __fscache_read_or_alloc_page(struct fscache_cookie *,
					struct page *,
					fscache_rw_complete_t,
					void *,
					gfp_t);
extern int __fscache_read_or_alloc_pages(struct fscache_cookie *,
					 struct address_space *,
					 struct list_head *,
					 unsigned *,
					 fscache_rw_complete_t,
					 void *,
					 gfp_t);
extern int __fscache_alloc_page(struct fscache_cookie *, struct page *, gfp_t);
213
extern int __fscache_write_page(struct fscache_cookie *, struct page *, loff_t, gfp_t);
214 215 216
extern void __fscache_uncache_page(struct fscache_cookie *, struct page *);
extern bool __fscache_check_page_write(struct fscache_cookie *, struct page *);
extern void __fscache_wait_on_page_write(struct fscache_cookie *, struct page *);
217 218
extern bool __fscache_maybe_release_page(struct fscache_cookie *, struct page *,
					 gfp_t);
219 220
extern void __fscache_uncache_all_inode_pages(struct fscache_cookie *,
					      struct inode *);
221 222
extern void __fscache_readpages_cancel(struct fscache_cookie *cookie,
				       struct list_head *pages);
223 224
#endif /* FSCACHE_USE_NEW_IO_API */

225
extern void __fscache_disable_cookie(struct fscache_cookie *, const void *, bool);
226
extern void __fscache_enable_cookie(struct fscache_cookie *, const void *, loff_t,
227
				    bool (*)(void *), void *);
228

229 230 231 232 233 234
/**
 * fscache_register_netfs - Register a filesystem as desiring caching services
 * @netfs: The description of the filesystem
 *
 * Register a filesystem as desiring caching services if they're available.
 *
235
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
236 237 238 239 240
 * description.
 */
static inline
int fscache_register_netfs(struct fscache_netfs *netfs)
{
D
David Howells 已提交
241 242 243 244
	if (fscache_available())
		return __fscache_register_netfs(netfs);
	else
		return 0;
245 246 247 248 249 250 251 252 253 254
}

/**
 * fscache_unregister_netfs - Indicate that a filesystem no longer desires
 * caching services
 * @netfs: The description of the filesystem
 *
 * Indicate that a filesystem no longer desires caching services for the
 * moment.
 *
255
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
256 257 258 259 260
 * description.
 */
static inline
void fscache_unregister_netfs(struct fscache_netfs *netfs)
{
D
David Howells 已提交
261 262
	if (fscache_available())
		__fscache_unregister_netfs(netfs);
263 264 265 266 267 268 269 270 271
}

/**
 * fscache_lookup_cache_tag - Look up a cache tag
 * @name: The name of the tag to search for
 *
 * Acquire a specific cache referral tag that can be used to select a specific
 * cache in which to cache an index.
 *
272
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
273 274 275 276 277
 * description.
 */
static inline
struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name)
{
D
David Howells 已提交
278 279 280 281
	if (fscache_available())
		return __fscache_lookup_cache_tag(name);
	else
		return NULL;
282 283 284 285 286 287 288 289
}

/**
 * fscache_release_cache_tag - Release a cache tag
 * @tag: The tag to release
 *
 * Release a reference to a cache referral tag previously looked up.
 *
290
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
291 292 293 294 295
 * description.
 */
static inline
void fscache_release_cache_tag(struct fscache_cache_tag *tag)
{
D
David Howells 已提交
296 297
	if (fscache_available())
		__fscache_release_cache_tag(tag);
298 299 300 301 302 303
}

/**
 * fscache_acquire_cookie - Acquire a cookie to represent a cache object
 * @parent: The cookie that's to be the parent of this one
 * @def: A description of the cache object, including callback operations
304 305 306 307
 * @index_key: The index key for this cookie
 * @index_key_len: Size of the index key
 * @aux_data: The auxiliary data for the cookie (may be NULL)
 * @aux_data_len: Size of the auxiliary data buffer
308 309
 * @netfs_data: An arbitrary piece of data to be kept in the cookie to
 * represent the cache object to the netfs
310
 * @object_size: The initial size of object
311
 * @enable: Whether or not to enable a data cookie immediately
312 313 314 315 316
 *
 * This function is used to inform FS-Cache about part of an index hierarchy
 * that can be used to locate files.  This is done by requesting a cookie for
 * each index in the path to the file.
 *
317
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
318 319 320 321 322 323
 * description.
 */
static inline
struct fscache_cookie *fscache_acquire_cookie(
	struct fscache_cookie *parent,
	const struct fscache_cookie_def *def,
324 325 326 327
	const void *index_key,
	size_t index_key_len,
	const void *aux_data,
	size_t aux_data_len,
328
	void *netfs_data,
329
	loff_t object_size,
330
	bool enable)
331
{
332
	if (fscache_cookie_valid(parent) && fscache_cookie_enabled(parent))
333 334 335
		return __fscache_acquire_cookie(parent, def,
						index_key, index_key_len,
						aux_data, aux_data_len,
336
						netfs_data, object_size, enable);
337 338
	else
		return NULL;
339 340 341 342 343 344
}

/**
 * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
 * it
 * @cookie: The cookie being returned
345
 * @aux_data: The updated auxiliary data for the cookie (may be NULL)
346 347 348
 * @retire: True if the cache object the cookie represents is to be discarded
 *
 * This function returns a cookie to the cache, forcibly discarding the
349 350 351
 * associated cache object if retire is set to true.  The opportunity is
 * provided to update the auxiliary data in the cache before the object is
 * disconnected.
352
 *
353
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
354 355 356
 * description.
 */
static inline
357 358 359
void fscache_relinquish_cookie(struct fscache_cookie *cookie,
			       const void *aux_data,
			       bool retire)
360
{
361
	if (fscache_cookie_valid(cookie))
362
		__fscache_relinquish_cookie(cookie, aux_data, retire);
363 364
}

365
/**
366
 * fscache_check_consistency - Request validation of a cache's auxiliary data
367
 * @cookie: The cookie representing the cache object
368
 * @aux_data: The updated auxiliary data for the cookie (may be NULL)
369
 *
370 371 372
 * Request an consistency check from fscache, which passes the request to the
 * backing cache.  The auxiliary data on the cookie will be updated first if
 * @aux_data is set.
373 374 375 376 377
 *
 * Returns 0 if consistent and -ESTALE if inconsistent.  May also
 * return -ENOMEM and -ERESTARTSYS.
 */
static inline
378 379
int fscache_check_consistency(struct fscache_cookie *cookie,
			      const void *aux_data)
380
{
381
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
382
		return __fscache_check_consistency(cookie, aux_data);
383 384 385 386
	else
		return 0;
}

387 388 389
/**
 * fscache_update_cookie - Request that a cache object be updated
 * @cookie: The cookie representing the cache object
390
 * @aux_data: The updated auxiliary data for the cookie (may be NULL)
391 392
 *
 * Request an update of the index data for the cache object associated with the
393 394
 * cookie.  The auxiliary data on the cookie will be updated first if @aux_data
 * is set.
395
 *
396
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
397 398 399
 * description.
 */
static inline
400
void fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data)
401
{
402
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
403
		__fscache_update_cookie(cookie, aux_data);
404 405 406 407 408 409 410 411
}

/**
 * fscache_pin_cookie - Pin a data-storage cache object in its cache
 * @cookie: The cookie representing the cache object
 *
 * Permit data-storage cache objects to be pinned in the cache.
 *
412
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
413 414 415 416 417 418 419 420 421 422 423 424 425 426
 * description.
 */
static inline
int fscache_pin_cookie(struct fscache_cookie *cookie)
{
	return -ENOBUFS;
}

/**
 * fscache_pin_cookie - Unpin a data-storage cache object in its cache
 * @cookie: The cookie representing the cache object
 *
 * Permit data-storage cache objects to be unpinned from the cache.
 *
427
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
 * description.
 */
static inline
void fscache_unpin_cookie(struct fscache_cookie *cookie)
{
}

/**
 * fscache_attr_changed - Notify cache that an object's attributes changed
 * @cookie: The cookie representing the cache object
 *
 * Send a notification to the cache indicating that an object's attributes have
 * changed.  This includes the data size.  These attributes will be obtained
 * through the get_attr() cookie definition op.
 *
443
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
444 445 446 447 448
 * description.
 */
static inline
int fscache_attr_changed(struct fscache_cookie *cookie)
{
449
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
450 451 452
		return __fscache_attr_changed(cookie);
	else
		return -ENOBUFS;
453 454
}

455 456 457 458 459 460 461 462 463 464
/**
 * fscache_invalidate - Notify cache that an object needs invalidation
 * @cookie: The cookie representing the cache object
 *
 * Notify the cache that an object is needs to be invalidated and that it
 * should abort any retrievals or stores it is doing on the cache.  The object
 * is then marked non-caching until such time as the invalidation is complete.
 *
 * This can be called with spinlocks held.
 *
465
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
466 467 468 469 470
 * description.
 */
static inline
void fscache_invalidate(struct fscache_cookie *cookie)
{
471
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
472 473 474 475 476 477 478 479 480
		__fscache_invalidate(cookie);
}

/**
 * fscache_wait_on_invalidate - Wait for invalidation to complete
 * @cookie: The cookie representing the cache object
 *
 * Wait for the invalidation of an object to complete.
 *
481
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
482 483 484 485 486 487 488 489 490
 * description.
 */
static inline
void fscache_wait_on_invalidate(struct fscache_cookie *cookie)
{
	if (fscache_cookie_valid(cookie))
		__fscache_wait_on_invalidate(cookie);
}

491 492 493 494 495 496 497 498 499
/**
 * fscache_reserve_space - Reserve data space for a cached object
 * @cookie: The cookie representing the cache object
 * @i_size: The amount of space to be reserved
 *
 * Reserve an amount of space in the cache for the cache object attached to a
 * cookie so that a write to that object within the space can always be
 * honoured.
 *
500
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
501 502 503 504 505 506 507 508
 * description.
 */
static inline
int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size)
{
	return -ENOBUFS;
}

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
#ifdef FSCACHE_USE_NEW_IO_API

/**
 * fscache_begin_read_operation - Begin a read operation for the netfs lib
 * @rreq: The read request being undertaken
 * @cookie: The cookie representing the cache object
 *
 * Begin a read operation on behalf of the netfs helper library.  @rreq
 * indicates the read request to which the operation state should be attached;
 * @cookie indicates the cache object that will be accessed.
 *
 * This is intended to be called from the ->begin_cache_operation() netfs lib
 * operation as implemented by the network filesystem.
 *
 * Returns:
 * * 0		- Success
 * * -ENOBUFS	- No caching available
 * * Other error code from the cache, such as -ENOMEM.
 */
static inline
int fscache_begin_read_operation(struct netfs_read_request *rreq,
				 struct fscache_cookie *cookie)
{
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
		return __fscache_begin_read_operation(rreq, cookie);
	return -ENOBUFS;
}

#else /* FSCACHE_USE_NEW_IO_API */

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
/**
 * fscache_read_or_alloc_page - Read a page from the cache or allocate a block
 * in which to store it
 * @cookie: The cookie representing the cache object
 * @page: The netfs page to fill if possible
 * @end_io_func: The callback to invoke when and if the page is filled
 * @context: An arbitrary piece of data to pass on to end_io_func()
 * @gfp: The conditions under which memory allocation should be made
 *
 * Read a page from the cache, or if that's not possible make a potential
 * one-block reservation in the cache into which the page may be stored once
 * fetched from the server.
 *
 * If the page is not backed by the cache object, or if it there's some reason
 * it can't be, -ENOBUFS will be returned and nothing more will be done for
 * that page.
 *
 * Else, if that page is backed by the cache, a read will be initiated directly
 * to the netfs's page and 0 will be returned by this function.  The
 * end_io_func() callback will be invoked when the operation terminates on a
 * completion or failure.  Note that the callback may be invoked before the
 * return.
 *
 * Else, if the page is unbacked, -ENODATA is returned and a block may have
 * been allocated in the cache.
 *
565
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
566 567 568 569 570 571 572 573 574
 * description.
 */
static inline
int fscache_read_or_alloc_page(struct fscache_cookie *cookie,
			       struct page *page,
			       fscache_rw_complete_t end_io_func,
			       void *context,
			       gfp_t gfp)
{
575
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
576 577 578 579
		return __fscache_read_or_alloc_page(cookie, page, end_io_func,
						    context, gfp);
	else
		return -ENOBUFS;
580 581 582 583 584 585 586 587
}

/**
 * fscache_read_or_alloc_pages - Read pages from the cache and/or allocate
 * blocks in which to store them
 * @cookie: The cookie representing the cache object
 * @mapping: The netfs inode mapping to which the pages will be attached
 * @pages: A list of potential netfs pages to be filled
588
 * @nr_pages: Number of pages to be read and/or allocated
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
 * @end_io_func: The callback to invoke when and if each page is filled
 * @context: An arbitrary piece of data to pass on to end_io_func()
 * @gfp: The conditions under which memory allocation should be made
 *
 * Read a set of pages from the cache, or if that's not possible, attempt to
 * make a potential one-block reservation for each page in the cache into which
 * that page may be stored once fetched from the server.
 *
 * If some pages are not backed by the cache object, or if it there's some
 * reason they can't be, -ENOBUFS will be returned and nothing more will be
 * done for that pages.
 *
 * Else, if some of the pages are backed by the cache, a read will be initiated
 * directly to the netfs's page and 0 will be returned by this function.  The
 * end_io_func() callback will be invoked when the operation terminates on a
 * completion or failure.  Note that the callback may be invoked before the
 * return.
 *
 * Else, if a page is unbacked, -ENODATA is returned and a block may have
 * been allocated in the cache.
 *
 * Because the function may want to return all of -ENOBUFS, -ENODATA and 0 in
 * regard to different pages, the return values are prioritised in that order.
 * Any pages submitted for reading are removed from the pages list.
 *
614
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
615 616 617 618 619 620 621 622 623 624 625
 * description.
 */
static inline
int 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)
{
626
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
627 628 629 630 631
		return __fscache_read_or_alloc_pages(cookie, mapping, pages,
						     nr_pages, end_io_func,
						     context, gfp);
	else
		return -ENOBUFS;
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
}

/**
 * fscache_alloc_page - Allocate a block in which to store a page
 * @cookie: The cookie representing the cache object
 * @page: The netfs page to allocate a page for
 * @gfp: The conditions under which memory allocation should be made
 *
 * Request Allocation a block in the cache in which to store a netfs page
 * without retrieving any contents from the cache.
 *
 * If the page is not backed by a file then -ENOBUFS will be returned and
 * nothing more will be done, and no reservation will be made.
 *
 * Else, a block will be allocated if one wasn't already, and 0 will be
 * returned
 *
649
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
650 651 652 653 654 655 656
 * description.
 */
static inline
int fscache_alloc_page(struct fscache_cookie *cookie,
		       struct page *page,
		       gfp_t gfp)
{
657
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
658 659 660
		return __fscache_alloc_page(cookie, page, gfp);
	else
		return -ENOBUFS;
661 662
}

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
/**
 * fscache_readpages_cancel - Cancel read/alloc on pages
 * @cookie: The cookie representing the inode's cache object.
 * @pages: The netfs pages that we canceled write on in readpages()
 *
 * Uncache/unreserve the pages reserved earlier in readpages() via
 * fscache_readpages_or_alloc() and similar.  In most successful caches in
 * readpages() this doesn't do anything.  In cases when the underlying netfs's
 * readahead failed we need to clean up the pagelist (unmark and uncache).
 *
 * This function may sleep as it may have to clean up disk state.
 */
static inline
void fscache_readpages_cancel(struct fscache_cookie *cookie,
			      struct list_head *pages)
{
	if (fscache_cookie_valid(cookie))
		__fscache_readpages_cancel(cookie, pages);
}

683 684 685 686
/**
 * fscache_write_page - Request storage of a page in the cache
 * @cookie: The cookie representing the cache object
 * @page: The netfs page to store
687
 * @object_size: Updated size of object
688 689 690 691 692 693 694 695 696 697 698
 * @gfp: The conditions under which memory allocation should be made
 *
 * Request the contents of the netfs page be written into the cache.  This
 * request may be ignored if no cache block is currently allocated, in which
 * case it will return -ENOBUFS.
 *
 * If a cache block was already allocated, a write will be initiated and 0 will
 * be returned.  The PG_fscache_write page bit is set immediately and will then
 * be cleared at the completion of the write to indicate the success or failure
 * of the operation.  Note that the completion may happen before the return.
 *
699
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
700 701 702 703 704
 * description.
 */
static inline
int fscache_write_page(struct fscache_cookie *cookie,
		       struct page *page,
705
		       loff_t object_size,
706 707
		       gfp_t gfp)
{
708
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
709
		return __fscache_write_page(cookie, page, object_size, gfp);
710 711
	else
		return -ENOBUFS;
712 713 714 715 716 717 718 719 720 721 722 723 724
}

/**
 * fscache_uncache_page - Indicate that caching is no longer required on a page
 * @cookie: The cookie representing the cache object
 * @page: The netfs page that was being cached.
 *
 * Tell the cache that we no longer want a page to be cached and that it should
 * remove any knowledge of the netfs page it may have.
 *
 * Note that this cannot cancel any outstanding I/O operations between this
 * page and the cache.
 *
725
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
726 727 728 729 730 731
 * description.
 */
static inline
void fscache_uncache_page(struct fscache_cookie *cookie,
			  struct page *page)
{
732 733
	if (fscache_cookie_valid(cookie))
		__fscache_uncache_page(cookie, page);
734 735 736 737 738 739 740 741 742
}

/**
 * fscache_check_page_write - Ask if a page is being writing to the cache
 * @cookie: The cookie representing the cache object
 * @page: The netfs page that is being cached.
 *
 * Ask the cache if a page is being written to the cache.
 *
743
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
744 745 746 747 748 749
 * description.
 */
static inline
bool fscache_check_page_write(struct fscache_cookie *cookie,
			      struct page *page)
{
750 751
	if (fscache_cookie_valid(cookie))
		return __fscache_check_page_write(cookie, page);
752 753 754 755 756 757 758 759 760 761 762
	return false;
}

/**
 * fscache_wait_on_page_write - Wait for a page to complete writing to the cache
 * @cookie: The cookie representing the cache object
 * @page: The netfs page that is being cached.
 *
 * Ask the cache to wake us up when a page is no longer being written to the
 * cache.
 *
763
 * See Documentation/filesystems/caching/netfs-api.rst for a complete
764 765 766 767 768 769
 * description.
 */
static inline
void fscache_wait_on_page_write(struct fscache_cookie *cookie,
				struct page *page)
{
770 771
	if (fscache_cookie_valid(cookie))
		__fscache_wait_on_page_write(cookie, page);
772 773
}

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
/**
 * fscache_maybe_release_page - Consider releasing a page, cancelling a store
 * @cookie: The cookie representing the cache object
 * @page: The netfs page that is being cached.
 * @gfp: The gfp flags passed to releasepage()
 *
 * Consider releasing a page for the vmscan algorithm, on behalf of the netfs's
 * releasepage() call.  A storage request on the page may cancelled if it is
 * not currently being processed.
 *
 * The function returns true if the page no longer has a storage request on it,
 * and false if a storage request is left in place.  If true is returned, the
 * page will have been passed to fscache_uncache_page().  If false is returned
 * the page cannot be freed yet.
 */
static inline
bool fscache_maybe_release_page(struct fscache_cookie *cookie,
				struct page *page,
				gfp_t gfp)
{
	if (fscache_cookie_valid(cookie) && PageFsCache(page))
		return __fscache_maybe_release_page(cookie, page, gfp);
796
	return true;
797 798
}

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
/**
 * fscache_uncache_all_inode_pages - Uncache all an inode's pages
 * @cookie: The cookie representing the inode's cache object.
 * @inode: The inode to uncache pages from.
 *
 * Uncache all the pages in an inode that are marked PG_fscache, assuming them
 * to be associated with the given cookie.
 *
 * This function may sleep.  It will wait for pages that are being written out
 * and will wait whilst the PG_fscache mark is removed by the cache.
 */
static inline
void fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
				     struct inode *inode)
{
	if (fscache_cookie_valid(cookie))
		__fscache_uncache_all_inode_pages(cookie, inode);
}

818 819
#endif /* FSCACHE_USE_NEW_IO_API */

820 821 822
/**
 * fscache_disable_cookie - Disable a cookie
 * @cookie: The cookie representing the cache object
823
 * @aux_data: The updated auxiliary data for the cookie (may be NULL)
824 825 826 827 828 829 830 831 832 833
 * @invalidate: Invalidate the backing object
 *
 * Disable a cookie from accepting further alloc, read, write, invalidate,
 * update or acquire operations.  Outstanding operations can still be waited
 * upon and pages can still be uncached and the cookie relinquished.
 *
 * This will not return until all outstanding operations have completed.
 *
 * If @invalidate is set, then the backing object will be invalidated and
 * detached, otherwise it will just be detached.
834 835
 *
 * If @aux_data is set, then auxiliary data will be updated from that.
836 837
 */
static inline
838 839 840
void fscache_disable_cookie(struct fscache_cookie *cookie,
			    const void *aux_data,
			    bool invalidate)
841 842
{
	if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
843
		__fscache_disable_cookie(cookie, aux_data, invalidate);
844 845 846 847 848
}

/**
 * fscache_enable_cookie - Reenable a cookie
 * @cookie: The cookie representing the cache object
849
 * @aux_data: The updated auxiliary data for the cookie (may be NULL)
850
 * @object_size: Current size of object
851 852 853 854 855
 * @can_enable: A function to permit enablement once lock is held
 * @data: Data for can_enable()
 *
 * Reenable a previously disabled cookie, allowing it to accept further alloc,
 * read, write, invalidate, update or acquire operations.  An attempt will be
856 857
 * made to immediately reattach the cookie to a backing object.  If @aux_data
 * is set, the auxiliary data attached to the cookie will be updated.
858 859 860 861 862 863
 *
 * The can_enable() function is called (if not NULL) once the enablement lock
 * is held to rule on whether enablement is still permitted to go ahead.
 */
static inline
void fscache_enable_cookie(struct fscache_cookie *cookie,
864
			   const void *aux_data,
865
			   loff_t object_size,
866 867 868 869
			   bool (*can_enable)(void *data),
			   void *data)
{
	if (fscache_cookie_valid(cookie) && !fscache_cookie_enabled(cookie))
870 871
		__fscache_enable_cookie(cookie, aux_data, object_size,
					can_enable, data);
872 873
}

874
#endif /* _LINUX_FSCACHE_H */