Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /* General filesystem caching interface
0003  *
0004  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  *
0007  * NOTE!!! See:
0008  *
0009  *  Documentation/filesystems/caching/netfs-api.rst
0010  *
0011  * for a description of the network filesystem interface declared here.
0012  */
0013 
0014 #ifndef _LINUX_FSCACHE_H
0015 #define _LINUX_FSCACHE_H
0016 
0017 #include <linux/fs.h>
0018 #include <linux/netfs.h>
0019 #include <linux/writeback.h>
0020 
0021 #if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
0022 #define __fscache_available (1)
0023 #define fscache_available() (1)
0024 #define fscache_volume_valid(volume) (volume)
0025 #define fscache_cookie_valid(cookie) (cookie)
0026 #define fscache_resources_valid(cres) ((cres)->cache_priv)
0027 #define fscache_cookie_enabled(cookie) (cookie && !test_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
0028 #else
0029 #define __fscache_available (0)
0030 #define fscache_available() (0)
0031 #define fscache_volume_valid(volume) (0)
0032 #define fscache_cookie_valid(cookie) (0)
0033 #define fscache_resources_valid(cres) (false)
0034 #define fscache_cookie_enabled(cookie) (0)
0035 #endif
0036 
0037 struct fscache_cookie;
0038 
0039 #define FSCACHE_ADV_SINGLE_CHUNK    0x01 /* The object is a single chunk of data */
0040 #define FSCACHE_ADV_WRITE_CACHE     0x00 /* Do cache if written to locally */
0041 #define FSCACHE_ADV_WRITE_NOCACHE   0x02 /* Don't cache if written to locally */
0042 #define FSCACHE_ADV_WANT_CACHE_SIZE 0x04 /* Retrieve cache size at runtime */
0043 
0044 #define FSCACHE_INVAL_DIO_WRITE     0x01 /* Invalidate due to DIO write */
0045 
0046 enum fscache_want_state {
0047     FSCACHE_WANT_PARAMS,
0048     FSCACHE_WANT_WRITE,
0049     FSCACHE_WANT_READ,
0050 };
0051 
0052 /*
0053  * Data object state.
0054  */
0055 enum fscache_cookie_state {
0056     FSCACHE_COOKIE_STATE_QUIESCENT,     /* The cookie is uncached */
0057     FSCACHE_COOKIE_STATE_LOOKING_UP,    /* The cache object is being looked up */
0058     FSCACHE_COOKIE_STATE_CREATING,      /* The cache object is being created */
0059     FSCACHE_COOKIE_STATE_ACTIVE,        /* The cache is active, readable and writable */
0060     FSCACHE_COOKIE_STATE_INVALIDATING,  /* The cache is being invalidated */
0061     FSCACHE_COOKIE_STATE_FAILED,        /* The cache failed, withdraw to clear */
0062     FSCACHE_COOKIE_STATE_LRU_DISCARDING,    /* The cookie is being discarded by the LRU */
0063     FSCACHE_COOKIE_STATE_WITHDRAWING,   /* The cookie is being withdrawn */
0064     FSCACHE_COOKIE_STATE_RELINQUISHING, /* The cookie is being relinquished */
0065     FSCACHE_COOKIE_STATE_DROPPED,       /* The cookie has been dropped */
0066 #define FSCACHE_COOKIE_STATE__NR (FSCACHE_COOKIE_STATE_DROPPED + 1)
0067 } __attribute__((mode(byte)));
0068 
0069 /*
0070  * Volume representation cookie.
0071  */
0072 struct fscache_volume {
0073     refcount_t          ref;
0074     atomic_t            n_cookies;  /* Number of data cookies in volume */
0075     atomic_t            n_accesses; /* Number of cache accesses in progress */
0076     unsigned int            debug_id;
0077     unsigned int            key_hash;   /* Hash of key string */
0078     char                *key;       /* Volume ID, eg. "afs@example.com@1234" */
0079     struct list_head        proc_link;  /* Link in /proc/fs/fscache/volumes */
0080     struct hlist_bl_node        hash_link;  /* Link in hash table */
0081     struct work_struct      work;
0082     struct fscache_cache        *cache;     /* The cache in which this resides */
0083     void                *cache_priv;    /* Cache private data */
0084     spinlock_t          lock;
0085     unsigned long           flags;
0086 #define FSCACHE_VOLUME_RELINQUISHED 0   /* Volume is being cleaned up */
0087 #define FSCACHE_VOLUME_INVALIDATE   1   /* Volume was invalidated */
0088 #define FSCACHE_VOLUME_COLLIDED_WITH    2   /* Volume was collided with */
0089 #define FSCACHE_VOLUME_ACQUIRE_PENDING  3   /* Volume is waiting to complete acquisition */
0090 #define FSCACHE_VOLUME_CREATING     4   /* Volume is being created on disk */
0091     u8              coherency_len;  /* Length of the coherency data */
0092     u8              coherency[];    /* Coherency data */
0093 };
0094 
0095 /*
0096  * Data file representation cookie.
0097  * - a file will only appear in one cache
0098  * - a request to cache a file may or may not be honoured, subject to
0099  *   constraints such as disk space
0100  * - indices are created on disk just-in-time
0101  */
0102 struct fscache_cookie {
0103     refcount_t          ref;
0104     atomic_t            n_active;   /* number of active users of cookie */
0105     atomic_t            n_accesses; /* Number of cache accesses in progress */
0106     unsigned int            debug_id;
0107     unsigned int            inval_counter;  /* Number of invalidations made */
0108     spinlock_t          lock;
0109     struct fscache_volume       *volume;    /* Parent volume of this file. */
0110     void                *cache_priv;    /* Cache-side representation */
0111     struct hlist_bl_node        hash_link;  /* Link in hash table */
0112     struct list_head        proc_link;  /* Link in proc list */
0113     struct list_head        commit_link;    /* Link in commit queue */
0114     struct work_struct      work;       /* Commit/relinq/withdraw work */
0115     loff_t              object_size;    /* Size of the netfs object */
0116     unsigned long           unused_at;  /* Time at which unused (jiffies) */
0117     unsigned long           flags;
0118 #define FSCACHE_COOKIE_RELINQUISHED 0       /* T if cookie has been relinquished */
0119 #define FSCACHE_COOKIE_RETIRED      1       /* T if this cookie has retired on relinq */
0120 #define FSCACHE_COOKIE_IS_CACHING   2       /* T if this cookie is cached */
0121 #define FSCACHE_COOKIE_NO_DATA_TO_READ  3       /* T if this cookie has nothing to read */
0122 #define FSCACHE_COOKIE_NEEDS_UPDATE 4       /* T if attrs have been updated */
0123 #define FSCACHE_COOKIE_HAS_BEEN_CACHED  5       /* T if cookie needs withdraw-on-relinq */
0124 #define FSCACHE_COOKIE_DISABLED     6       /* T if cookie has been disabled */
0125 #define FSCACHE_COOKIE_LOCAL_WRITE  7       /* T if cookie has been modified locally */
0126 #define FSCACHE_COOKIE_NO_ACCESS_WAKE   8       /* T if no wake when n_accesses goes 0 */
0127 #define FSCACHE_COOKIE_DO_RELINQUISH    9       /* T if this cookie needs relinquishment */
0128 #define FSCACHE_COOKIE_DO_WITHDRAW  10      /* T if this cookie needs withdrawing */
0129 #define FSCACHE_COOKIE_DO_LRU_DISCARD   11      /* T if this cookie needs LRU discard */
0130 #define FSCACHE_COOKIE_DO_PREP_TO_WRITE 12      /* T if cookie needs write preparation */
0131 #define FSCACHE_COOKIE_HAVE_DATA    13      /* T if this cookie has data stored */
0132 #define FSCACHE_COOKIE_IS_HASHED    14      /* T if this cookie is hashed */
0133 #define FSCACHE_COOKIE_DO_INVALIDATE    15      /* T if cookie needs invalidation */
0134 
0135     enum fscache_cookie_state   state;
0136     u8              advice;     /* FSCACHE_ADV_* */
0137     u8              key_len;    /* Length of index key */
0138     u8              aux_len;    /* Length of auxiliary data */
0139     u32             key_hash;   /* Hash of volume, key, len */
0140     union {
0141         void            *key;       /* Index key */
0142         u8          inline_key[16]; /* - If the key is short enough */
0143     };
0144     union {
0145         void            *aux;       /* Auxiliary data */
0146         u8          inline_aux[8];  /* - If the aux data is short enough */
0147     };
0148 };
0149 
0150 /*
0151  * slow-path functions for when there is actually caching available, and the
0152  * netfs does actually have a valid token
0153  * - these are not to be called directly
0154  * - these are undefined symbols when FS-Cache is not configured and the
0155  *   optimiser takes care of not using them
0156  */
0157 extern struct fscache_volume *__fscache_acquire_volume(const char *, const char *,
0158                                const void *, size_t);
0159 extern void __fscache_relinquish_volume(struct fscache_volume *, const void *, bool);
0160 
0161 extern struct fscache_cookie *__fscache_acquire_cookie(
0162     struct fscache_volume *,
0163     u8,
0164     const void *, size_t,
0165     const void *, size_t,
0166     loff_t);
0167 extern void __fscache_use_cookie(struct fscache_cookie *, bool);
0168 extern void __fscache_unuse_cookie(struct fscache_cookie *, const void *, const loff_t *);
0169 extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
0170 extern void __fscache_resize_cookie(struct fscache_cookie *, loff_t);
0171 extern void __fscache_invalidate(struct fscache_cookie *, const void *, loff_t, unsigned int);
0172 extern int __fscache_begin_read_operation(struct netfs_cache_resources *, struct fscache_cookie *);
0173 extern int __fscache_begin_write_operation(struct netfs_cache_resources *, struct fscache_cookie *);
0174 
0175 extern void __fscache_write_to_cache(struct fscache_cookie *, struct address_space *,
0176                      loff_t, size_t, loff_t, netfs_io_terminated_t, void *,
0177                      bool);
0178 extern void __fscache_clear_page_bits(struct address_space *, loff_t, size_t);
0179 
0180 /**
0181  * fscache_acquire_volume - Register a volume as desiring caching services
0182  * @volume_key: An identification string for the volume
0183  * @cache_name: The name of the cache to use (or NULL for the default)
0184  * @coherency_data: Piece of arbitrary coherency data to check (or NULL)
0185  * @coherency_len: The size of the coherency data
0186  *
0187  * Register a volume as desiring caching services if they're available.  The
0188  * caller must provide an identifier for the volume and may also indicate which
0189  * cache it should be in.  If a preexisting volume entry is found in the cache,
0190  * the coherency data must match otherwise the entry will be invalidated.
0191  *
0192  * Returns a cookie pointer on success, -ENOMEM if out of memory or -EBUSY if a
0193  * cache volume of that name is already acquired.  Note that "NULL" is a valid
0194  * cookie pointer and can be returned if caching is refused.
0195  */
0196 static inline
0197 struct fscache_volume *fscache_acquire_volume(const char *volume_key,
0198                           const char *cache_name,
0199                           const void *coherency_data,
0200                           size_t coherency_len)
0201 {
0202     if (!fscache_available())
0203         return NULL;
0204     return __fscache_acquire_volume(volume_key, cache_name,
0205                     coherency_data, coherency_len);
0206 }
0207 
0208 /**
0209  * fscache_relinquish_volume - Cease caching a volume
0210  * @volume: The volume cookie
0211  * @coherency_data: Piece of arbitrary coherency data to set (or NULL)
0212  * @invalidate: True if the volume should be invalidated
0213  *
0214  * Indicate that a filesystem no longer desires caching services for a volume.
0215  * The caller must have relinquished all file cookies prior to calling this.
0216  * The stored coherency data is updated.
0217  */
0218 static inline
0219 void fscache_relinquish_volume(struct fscache_volume *volume,
0220                    const void *coherency_data,
0221                    bool invalidate)
0222 {
0223     if (fscache_volume_valid(volume))
0224         __fscache_relinquish_volume(volume, coherency_data, invalidate);
0225 }
0226 
0227 /**
0228  * fscache_acquire_cookie - Acquire a cookie to represent a cache object
0229  * @volume: The volume in which to locate/create this cookie
0230  * @advice: Advice flags (FSCACHE_COOKIE_ADV_*)
0231  * @index_key: The index key for this cookie
0232  * @index_key_len: Size of the index key
0233  * @aux_data: The auxiliary data for the cookie (may be NULL)
0234  * @aux_data_len: Size of the auxiliary data buffer
0235  * @object_size: The initial size of object
0236  *
0237  * Acquire a cookie to represent a data file within the given cache volume.
0238  *
0239  * See Documentation/filesystems/caching/netfs-api.rst for a complete
0240  * description.
0241  */
0242 static inline
0243 struct fscache_cookie *fscache_acquire_cookie(struct fscache_volume *volume,
0244                           u8 advice,
0245                           const void *index_key,
0246                           size_t index_key_len,
0247                           const void *aux_data,
0248                           size_t aux_data_len,
0249                           loff_t object_size)
0250 {
0251     if (!fscache_volume_valid(volume))
0252         return NULL;
0253     return __fscache_acquire_cookie(volume, advice,
0254                     index_key, index_key_len,
0255                     aux_data, aux_data_len,
0256                     object_size);
0257 }
0258 
0259 /**
0260  * fscache_use_cookie - Request usage of cookie attached to an object
0261  * @cookie: The cookie representing the cache object
0262  * @will_modify: If cache is expected to be modified locally
0263  *
0264  * Request usage of the cookie attached to an object.  The caller should tell
0265  * the cache if the object's contents are about to be modified locally and then
0266  * the cache can apply the policy that has been set to handle this case.
0267  */
0268 static inline void fscache_use_cookie(struct fscache_cookie *cookie,
0269                       bool will_modify)
0270 {
0271     if (fscache_cookie_valid(cookie))
0272         __fscache_use_cookie(cookie, will_modify);
0273 }
0274 
0275 /**
0276  * fscache_unuse_cookie - Cease usage of cookie attached to an object
0277  * @cookie: The cookie representing the cache object
0278  * @aux_data: Updated auxiliary data (or NULL)
0279  * @object_size: Revised size of the object (or NULL)
0280  *
0281  * Cease usage of the cookie attached to an object.  When the users count
0282  * reaches zero then the cookie relinquishment will be permitted to proceed.
0283  */
0284 static inline void fscache_unuse_cookie(struct fscache_cookie *cookie,
0285                     const void *aux_data,
0286                     const loff_t *object_size)
0287 {
0288     if (fscache_cookie_valid(cookie))
0289         __fscache_unuse_cookie(cookie, aux_data, object_size);
0290 }
0291 
0292 /**
0293  * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
0294  * it
0295  * @cookie: The cookie being returned
0296  * @retire: True if the cache object the cookie represents is to be discarded
0297  *
0298  * This function returns a cookie to the cache, forcibly discarding the
0299  * associated cache object if retire is set to true.
0300  *
0301  * See Documentation/filesystems/caching/netfs-api.rst for a complete
0302  * description.
0303  */
0304 static inline
0305 void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
0306 {
0307     if (fscache_cookie_valid(cookie))
0308         __fscache_relinquish_cookie(cookie, retire);
0309 }
0310 
0311 /*
0312  * Find the auxiliary data on a cookie.
0313  */
0314 static inline void *fscache_get_aux(struct fscache_cookie *cookie)
0315 {
0316     if (cookie->aux_len <= sizeof(cookie->inline_aux))
0317         return cookie->inline_aux;
0318     else
0319         return cookie->aux;
0320 }
0321 
0322 /*
0323  * Update the auxiliary data on a cookie.
0324  */
0325 static inline
0326 void fscache_update_aux(struct fscache_cookie *cookie,
0327             const void *aux_data, const loff_t *object_size)
0328 {
0329     void *p = fscache_get_aux(cookie);
0330 
0331     if (aux_data && p)
0332         memcpy(p, aux_data, cookie->aux_len);
0333     if (object_size)
0334         cookie->object_size = *object_size;
0335 }
0336 
0337 #ifdef CONFIG_FSCACHE_STATS
0338 extern atomic_t fscache_n_updates;
0339 #endif
0340 
0341 static inline
0342 void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
0343                  const loff_t *object_size)
0344 {
0345 #ifdef CONFIG_FSCACHE_STATS
0346     atomic_inc(&fscache_n_updates);
0347 #endif
0348     fscache_update_aux(cookie, aux_data, object_size);
0349     smp_wmb();
0350     set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
0351 }
0352 
0353 /**
0354  * fscache_update_cookie - Request that a cache object be updated
0355  * @cookie: The cookie representing the cache object
0356  * @aux_data: The updated auxiliary data for the cookie (may be NULL)
0357  * @object_size: The current size of the object (may be NULL)
0358  *
0359  * Request an update of the index data for the cache object associated with the
0360  * cookie.  The auxiliary data on the cookie will be updated first if @aux_data
0361  * is set and the object size will be updated and the object possibly trimmed
0362  * if @object_size is set.
0363  *
0364  * See Documentation/filesystems/caching/netfs-api.rst for a complete
0365  * description.
0366  */
0367 static inline
0368 void fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
0369                const loff_t *object_size)
0370 {
0371     if (fscache_cookie_enabled(cookie))
0372         __fscache_update_cookie(cookie, aux_data, object_size);
0373 }
0374 
0375 /**
0376  * fscache_resize_cookie - Request that a cache object be resized
0377  * @cookie: The cookie representing the cache object
0378  * @new_size: The new size of the object (may be NULL)
0379  *
0380  * Request that the size of an object be changed.
0381  *
0382  * See Documentation/filesystems/caching/netfs-api.rst for a complete
0383  * description.
0384  */
0385 static inline
0386 void fscache_resize_cookie(struct fscache_cookie *cookie, loff_t new_size)
0387 {
0388     if (fscache_cookie_enabled(cookie))
0389         __fscache_resize_cookie(cookie, new_size);
0390 }
0391 
0392 /**
0393  * fscache_invalidate - Notify cache that an object needs invalidation
0394  * @cookie: The cookie representing the cache object
0395  * @aux_data: The updated auxiliary data for the cookie (may be NULL)
0396  * @size: The revised size of the object.
0397  * @flags: Invalidation flags (FSCACHE_INVAL_*)
0398  *
0399  * Notify the cache that an object is needs to be invalidated and that it
0400  * should abort any retrievals or stores it is doing on the cache.  This
0401  * increments inval_counter on the cookie which can be used by the caller to
0402  * reconsider I/O requests as they complete.
0403  *
0404  * If @flags has FSCACHE_INVAL_DIO_WRITE set, this indicates that this is due
0405  * to a direct I/O write and will cause caching to be disabled on this cookie
0406  * until it is completely unused.
0407  *
0408  * See Documentation/filesystems/caching/netfs-api.rst for a complete
0409  * description.
0410  */
0411 static inline
0412 void fscache_invalidate(struct fscache_cookie *cookie,
0413             const void *aux_data, loff_t size, unsigned int flags)
0414 {
0415     if (fscache_cookie_enabled(cookie))
0416         __fscache_invalidate(cookie, aux_data, size, flags);
0417 }
0418 
0419 /**
0420  * fscache_operation_valid - Return true if operations resources are usable
0421  * @cres: The resources to check.
0422  *
0423  * Returns a pointer to the operations table if usable or NULL if not.
0424  */
0425 static inline
0426 const struct netfs_cache_ops *fscache_operation_valid(const struct netfs_cache_resources *cres)
0427 {
0428     return fscache_resources_valid(cres) ? cres->ops : NULL;
0429 }
0430 
0431 /**
0432  * fscache_begin_read_operation - Begin a read operation for the netfs lib
0433  * @cres: The cache resources for the read being performed
0434  * @cookie: The cookie representing the cache object
0435  *
0436  * Begin a read operation on behalf of the netfs helper library.  @cres
0437  * indicates the cache resources to which the operation state should be
0438  * attached; @cookie indicates the cache object that will be accessed.
0439  *
0440  * This is intended to be called from the ->begin_cache_operation() netfs lib
0441  * operation as implemented by the network filesystem.
0442  *
0443  * @cres->inval_counter is set from @cookie->inval_counter for comparison at
0444  * the end of the operation.  This allows invalidation during the operation to
0445  * be detected by the caller.
0446  *
0447  * Returns:
0448  * * 0      - Success
0449  * * -ENOBUFS   - No caching available
0450  * * Other error code from the cache, such as -ENOMEM.
0451  */
0452 static inline
0453 int fscache_begin_read_operation(struct netfs_cache_resources *cres,
0454                  struct fscache_cookie *cookie)
0455 {
0456     if (fscache_cookie_enabled(cookie))
0457         return __fscache_begin_read_operation(cres, cookie);
0458     return -ENOBUFS;
0459 }
0460 
0461 /**
0462  * fscache_end_operation - End the read operation for the netfs lib
0463  * @cres: The cache resources for the read operation
0464  *
0465  * Clean up the resources at the end of the read request.
0466  */
0467 static inline void fscache_end_operation(struct netfs_cache_resources *cres)
0468 {
0469     const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
0470 
0471     if (ops)
0472         ops->end_operation(cres);
0473 }
0474 
0475 /**
0476  * fscache_read - Start a read from the cache.
0477  * @cres: The cache resources to use
0478  * @start_pos: The beginning file offset in the cache file
0479  * @iter: The buffer to fill - and also the length
0480  * @read_hole: How to handle a hole in the data.
0481  * @term_func: The function to call upon completion
0482  * @term_func_priv: The private data for @term_func
0483  *
0484  * Start a read from the cache.  @cres indicates the cache object to read from
0485  * and must be obtained by a call to fscache_begin_operation() beforehand.
0486  *
0487  * The data is read into the iterator, @iter, and that also indicates the size
0488  * of the operation.  @start_pos is the start position in the file, though if
0489  * @seek_data is set appropriately, the cache can use SEEK_DATA to find the
0490  * next piece of data, writing zeros for the hole into the iterator.
0491  *
0492  * Upon termination of the operation, @term_func will be called and supplied
0493  * with @term_func_priv plus the amount of data written, if successful, or the
0494  * error code otherwise.
0495  *
0496  * @read_hole indicates how a partially populated region in the cache should be
0497  * handled.  It can be one of a number of settings:
0498  *
0499  *  NETFS_READ_HOLE_IGNORE - Just try to read (may return a short read).
0500  *
0501  *  NETFS_READ_HOLE_CLEAR - Seek for data, clearing the part of the buffer
0502  *              skipped over, then do as for IGNORE.
0503  *
0504  *  NETFS_READ_HOLE_FAIL - Give ENODATA if we encounter a hole.
0505  */
0506 static inline
0507 int fscache_read(struct netfs_cache_resources *cres,
0508          loff_t start_pos,
0509          struct iov_iter *iter,
0510          enum netfs_read_from_hole read_hole,
0511          netfs_io_terminated_t term_func,
0512          void *term_func_priv)
0513 {
0514     const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
0515     return ops->read(cres, start_pos, iter, read_hole,
0516              term_func, term_func_priv);
0517 }
0518 
0519 /**
0520  * fscache_begin_write_operation - Begin a write operation for the netfs lib
0521  * @cres: The cache resources for the write being performed
0522  * @cookie: The cookie representing the cache object
0523  *
0524  * Begin a write operation on behalf of the netfs helper library.  @cres
0525  * indicates the cache resources to which the operation state should be
0526  * attached; @cookie indicates the cache object that will be accessed.
0527  *
0528  * @cres->inval_counter is set from @cookie->inval_counter for comparison at
0529  * the end of the operation.  This allows invalidation during the operation to
0530  * be detected by the caller.
0531  *
0532  * Returns:
0533  * * 0      - Success
0534  * * -ENOBUFS   - No caching available
0535  * * Other error code from the cache, such as -ENOMEM.
0536  */
0537 static inline
0538 int fscache_begin_write_operation(struct netfs_cache_resources *cres,
0539                   struct fscache_cookie *cookie)
0540 {
0541     if (fscache_cookie_enabled(cookie))
0542         return __fscache_begin_write_operation(cres, cookie);
0543     return -ENOBUFS;
0544 }
0545 
0546 /**
0547  * fscache_write - Start a write to the cache.
0548  * @cres: The cache resources to use
0549  * @start_pos: The beginning file offset in the cache file
0550  * @iter: The data to write - and also the length
0551  * @term_func: The function to call upon completion
0552  * @term_func_priv: The private data for @term_func
0553  *
0554  * Start a write to the cache.  @cres indicates the cache object to write to and
0555  * must be obtained by a call to fscache_begin_operation() beforehand.
0556  *
0557  * The data to be written is obtained from the iterator, @iter, and that also
0558  * indicates the size of the operation.  @start_pos is the start position in
0559  * the file.
0560  *
0561  * Upon termination of the operation, @term_func will be called and supplied
0562  * with @term_func_priv plus the amount of data written, if successful, or the
0563  * error code otherwise.
0564  */
0565 static inline
0566 int fscache_write(struct netfs_cache_resources *cres,
0567           loff_t start_pos,
0568           struct iov_iter *iter,
0569           netfs_io_terminated_t term_func,
0570           void *term_func_priv)
0571 {
0572     const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
0573     return ops->write(cres, start_pos, iter, term_func, term_func_priv);
0574 }
0575 
0576 /**
0577  * fscache_clear_page_bits - Clear the PG_fscache bits from a set of pages
0578  * @mapping: The netfs inode to use as the source
0579  * @start: The start position in @mapping
0580  * @len: The amount of data to unlock
0581  * @caching: If PG_fscache has been set
0582  *
0583  * Clear the PG_fscache flag from a sequence of pages and wake up anyone who's
0584  * waiting.
0585  */
0586 static inline void fscache_clear_page_bits(struct address_space *mapping,
0587                        loff_t start, size_t len,
0588                        bool caching)
0589 {
0590     if (caching)
0591         __fscache_clear_page_bits(mapping, start, len);
0592 }
0593 
0594 /**
0595  * fscache_write_to_cache - Save a write to the cache and clear PG_fscache
0596  * @cookie: The cookie representing the cache object
0597  * @mapping: The netfs inode to use as the source
0598  * @start: The start position in @mapping
0599  * @len: The amount of data to write back
0600  * @i_size: The new size of the inode
0601  * @term_func: The function to call upon completion
0602  * @term_func_priv: The private data for @term_func
0603  * @caching: If PG_fscache has been set
0604  *
0605  * Helper function for a netfs to write dirty data from an inode into the cache
0606  * object that's backing it.
0607  *
0608  * @start and @len describe the range of the data.  This does not need to be
0609  * page-aligned, but to satisfy DIO requirements, the cache may expand it up to
0610  * the page boundaries on either end.  All the pages covering the range must be
0611  * marked with PG_fscache.
0612  *
0613  * If given, @term_func will be called upon completion and supplied with
0614  * @term_func_priv.  Note that the PG_fscache flags will have been cleared by
0615  * this point, so the netfs must retain its own pin on the mapping.
0616  */
0617 static inline void fscache_write_to_cache(struct fscache_cookie *cookie,
0618                       struct address_space *mapping,
0619                       loff_t start, size_t len, loff_t i_size,
0620                       netfs_io_terminated_t term_func,
0621                       void *term_func_priv,
0622                       bool caching)
0623 {
0624     if (caching)
0625         __fscache_write_to_cache(cookie, mapping, start, len, i_size,
0626                      term_func, term_func_priv, caching);
0627     else if (term_func)
0628         term_func(term_func_priv, -ENOBUFS, false);
0629 
0630 }
0631 
0632 #if __fscache_available
0633 bool fscache_dirty_folio(struct address_space *mapping, struct folio *folio,
0634         struct fscache_cookie *cookie);
0635 #else
0636 #define fscache_dirty_folio(MAPPING, FOLIO, COOKIE) \
0637         filemap_dirty_folio(MAPPING, FOLIO)
0638 #endif
0639 
0640 /**
0641  * fscache_unpin_writeback - Unpin writeback resources
0642  * @wbc: The writeback control
0643  * @cookie: The cookie referring to the cache object
0644  *
0645  * Unpin the writeback resources pinned by fscache_dirty_folio().  This is
0646  * intended to be called by the netfs's ->write_inode() method.
0647  */
0648 static inline void fscache_unpin_writeback(struct writeback_control *wbc,
0649                        struct fscache_cookie *cookie)
0650 {
0651     if (wbc->unpinned_fscache_wb)
0652         fscache_unuse_cookie(cookie, NULL, NULL);
0653 }
0654 
0655 /**
0656  * fscache_clear_inode_writeback - Clear writeback resources pinned by an inode
0657  * @cookie: The cookie referring to the cache object
0658  * @inode: The inode to clean up
0659  * @aux: Auxiliary data to apply to the inode
0660  *
0661  * Clear any writeback resources held by an inode when the inode is evicted.
0662  * This must be called before clear_inode() is called.
0663  */
0664 static inline void fscache_clear_inode_writeback(struct fscache_cookie *cookie,
0665                          struct inode *inode,
0666                          const void *aux)
0667 {
0668     if (inode->i_state & I_PINNING_FSCACHE_WB) {
0669         loff_t i_size = i_size_read(inode);
0670         fscache_unuse_cookie(cookie, aux, &i_size);
0671     }
0672 }
0673 
0674 /**
0675  * fscache_note_page_release - Note that a netfs page got released
0676  * @cookie: The cookie corresponding to the file
0677  *
0678  * Note that a page that has been copied to the cache has been released.  This
0679  * means that future reads will need to look in the cache to see if it's there.
0680  */
0681 static inline
0682 void fscache_note_page_release(struct fscache_cookie *cookie)
0683 {
0684     /* If we've written data to the cache (HAVE_DATA) and there wasn't any
0685      * data in the cache when we started (NO_DATA_TO_READ), it may no
0686      * longer be true that we can skip reading from the cache - so clear
0687      * the flag that causes reads to be skipped.
0688      */
0689     if (cookie &&
0690         test_bit(FSCACHE_COOKIE_HAVE_DATA, &cookie->flags) &&
0691         test_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags))
0692         clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
0693 }
0694 
0695 #endif /* _LINUX_FSCACHE_H */