1b533a83fSDavid Howells /* SPDX-License-Identifier: GPL-2.0-or-later */
2b533a83fSDavid Howells /* Network filesystem support services.
3b533a83fSDavid Howells *
4b533a83fSDavid Howells * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5b533a83fSDavid Howells * Written by David Howells ([email protected])
6b533a83fSDavid Howells *
7b533a83fSDavid Howells * See:
8b533a83fSDavid Howells *
9b533a83fSDavid Howells * Documentation/filesystems/netfs_library.rst
10b533a83fSDavid Howells *
11b533a83fSDavid Howells * for a description of the network filesystem interface declared here.
12b533a83fSDavid Howells */
13b533a83fSDavid Howells
14b533a83fSDavid Howells #ifndef _LINUX_NETFS_H
15b533a83fSDavid Howells #define _LINUX_NETFS_H
16b533a83fSDavid Howells
173d3c9504SDavid Howells #include <linux/workqueue.h>
183d3c9504SDavid Howells #include <linux/fs.h>
19b533a83fSDavid Howells #include <linux/pagemap.h>
2085dd2c8fSDavid Howells #include <linux/uio.h>
2106fa229cSDavid Howells #include <linux/rolling_buffer.h>
22b533a83fSDavid Howells
236cd3d6fdSDavid Howells enum netfs_sreq_ref_trace;
24d9f85a04SDavid Howells typedef struct mempool_s mempool_t;
25eb118159SDavid Howells struct folio_queue;
266cd3d6fdSDavid Howells
2799bff93cSDavid Howells /**
282e9d7e4bSDavid Howells * folio_start_private_2 - Start an fscache write on a folio. [DEPRECATED]
296abbaa5bSMatthew Wilcox (Oracle) * @folio: The folio.
3099bff93cSDavid Howells *
316abbaa5bSMatthew Wilcox (Oracle) * Call this function before writing a folio to a local cache. Starting a
326abbaa5bSMatthew Wilcox (Oracle) * second write before the first one finishes is not allowed.
332e9d7e4bSDavid Howells *
342e9d7e4bSDavid Howells * Note that this should no longer be used.
3599bff93cSDavid Howells */
folio_start_private_2(struct folio * folio)362e9d7e4bSDavid Howells static inline void folio_start_private_2(struct folio *folio)
3799bff93cSDavid Howells {
386abbaa5bSMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio);
396abbaa5bSMatthew Wilcox (Oracle) folio_get(folio);
406abbaa5bSMatthew Wilcox (Oracle) folio_set_private_2(folio);
4199bff93cSDavid Howells }
4299bff93cSDavid Howells
436a19114bSDavid Howells enum netfs_io_source {
4451e9a86aSDavid Howells NETFS_SOURCE_UNKNOWN,
453d3c9504SDavid Howells NETFS_FILL_WITH_ZEROES,
463d3c9504SDavid Howells NETFS_DOWNLOAD_FROM_SERVER,
473d3c9504SDavid Howells NETFS_READ_FROM_CACHE,
483d3c9504SDavid Howells NETFS_INVALID_READ,
4916af134cSDavid Howells NETFS_UPLOAD_TO_SERVER,
5016af134cSDavid Howells NETFS_WRITE_TO_CACHE,
5116af134cSDavid Howells NETFS_INVALID_WRITE,
523d3c9504SDavid Howells } __mode(byte);
533d3c9504SDavid Howells
54726218fdSDavid Howells typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error,
55726218fdSDavid Howells bool was_async);
56726218fdSDavid Howells
57726218fdSDavid Howells /*
58874c8ca1SDavid Howells * Per-inode context. This wraps the VFS inode.
59bc899ee1SDavid Howells */
60874c8ca1SDavid Howells struct netfs_inode {
61874c8ca1SDavid Howells struct inode inode; /* The VFS inode */
62bc899ee1SDavid Howells const struct netfs_request_ops *ops;
63bc899ee1SDavid Howells #if IS_ENABLED(CONFIG_FSCACHE)
64bc899ee1SDavid Howells struct fscache_cookie *cache;
65bc899ee1SDavid Howells #endif
66288ace2fSDavid Howells struct mutex wb_lock; /* Writeback serialisation */
674058f742SDavid Howells loff_t remote_i_size; /* Size of the remote file */
68100ccd18SDavid Howells loff_t zero_point; /* Size after which we assume there's no data
69100ccd18SDavid Howells * on the server */
70f89ea63fSDavid Howells atomic_t io_count; /* Number of outstanding reqs */
7146ed60dcSDavid Howells unsigned long flags;
7246ed60dcSDavid Howells #define NETFS_ICTX_ODIRECT 0 /* The file has DIO in progress */
73153a9961SDavid Howells #define NETFS_ICTX_UNBUFFERED 1 /* I/O should not use the pagecache */
7441d8e767SDavid Howells #define NETFS_ICTX_WRITETHROUGH 2 /* Write-through caching */
7573425800SDavid Howells #define NETFS_ICTX_MODIFIED_ATTR 3 /* Indicate change in mtime/ctime */
7649866ce7SDavid Howells #define NETFS_ICTX_SINGLE_NO_UPLOAD 4 /* Monolithic payload, cache but no upload */
77bc899ee1SDavid Howells };
78bc899ee1SDavid Howells
79bc899ee1SDavid Howells /*
809ebff83eSDavid Howells * A netfs group - for instance a ceph snap. This is marked on dirty pages and
819ebff83eSDavid Howells * pages marked with a group must be flushed before they can be written under
829ebff83eSDavid Howells * the domain of another group.
839ebff83eSDavid Howells */
849ebff83eSDavid Howells struct netfs_group {
859ebff83eSDavid Howells refcount_t ref;
869ebff83eSDavid Howells void (*free)(struct netfs_group *netfs_group);
879ebff83eSDavid Howells };
889ebff83eSDavid Howells
899ebff83eSDavid Howells /*
909ebff83eSDavid Howells * Information about a dirty page (attached only if necessary).
919ebff83eSDavid Howells * folio->private
929ebff83eSDavid Howells */
939ebff83eSDavid Howells struct netfs_folio {
949ebff83eSDavid Howells struct netfs_group *netfs_group; /* Filesystem's grouping marker (or NULL). */
959ebff83eSDavid Howells unsigned int dirty_offset; /* Write-streaming dirty data offset */
969ebff83eSDavid Howells unsigned int dirty_len; /* Write-streaming dirty data length */
979ebff83eSDavid Howells };
989ebff83eSDavid Howells #define NETFS_FOLIO_INFO 0x1UL /* OR'd with folio->private. */
992ff1e975SDavid Howells #define NETFS_FOLIO_COPY_TO_CACHE ((struct netfs_group *)0x356UL) /* Write to the cache only */
1002ff1e975SDavid Howells
netfs_is_folio_info(const void * priv)1012ff1e975SDavid Howells static inline bool netfs_is_folio_info(const void *priv)
1022ff1e975SDavid Howells {
1032ff1e975SDavid Howells return (unsigned long)priv & NETFS_FOLIO_INFO;
1042ff1e975SDavid Howells }
1052ff1e975SDavid Howells
__netfs_folio_info(const void * priv)1062ff1e975SDavid Howells static inline struct netfs_folio *__netfs_folio_info(const void *priv)
1072ff1e975SDavid Howells {
1082ff1e975SDavid Howells if (netfs_is_folio_info(priv))
1092ff1e975SDavid Howells return (struct netfs_folio *)((unsigned long)priv & ~NETFS_FOLIO_INFO);
1102ff1e975SDavid Howells return NULL;
1112ff1e975SDavid Howells }
1129ebff83eSDavid Howells
netfs_folio_info(struct folio * folio)1139ebff83eSDavid Howells static inline struct netfs_folio *netfs_folio_info(struct folio *folio)
1149ebff83eSDavid Howells {
1152ff1e975SDavid Howells return __netfs_folio_info(folio_get_private(folio));
1169ebff83eSDavid Howells }
1179ebff83eSDavid Howells
netfs_folio_group(struct folio * folio)1189ebff83eSDavid Howells static inline struct netfs_group *netfs_folio_group(struct folio *folio)
1199ebff83eSDavid Howells {
1209ebff83eSDavid Howells struct netfs_folio *finfo;
1219ebff83eSDavid Howells void *priv = folio_get_private(folio);
1229ebff83eSDavid Howells
1239ebff83eSDavid Howells finfo = netfs_folio_info(folio);
1249ebff83eSDavid Howells if (finfo)
1259ebff83eSDavid Howells return finfo->netfs_group;
1269ebff83eSDavid Howells return priv;
1279ebff83eSDavid Howells }
1289ebff83eSDavid Howells
1299ebff83eSDavid Howells /*
130288ace2fSDavid Howells * Stream of I/O subrequests going to a particular destination, such as the
131288ace2fSDavid Howells * server or the local cache. This is mainly intended for writing where we may
132288ace2fSDavid Howells * have to write to multiple destinations concurrently.
133288ace2fSDavid Howells */
134288ace2fSDavid Howells struct netfs_io_stream {
135288ace2fSDavid Howells /* Submission tracking */
136288ace2fSDavid Howells struct netfs_io_subrequest *construct; /* Op being constructed */
13752d55922SDavid Howells size_t sreq_max_len; /* Maximum size of a subrequest */
13852d55922SDavid Howells unsigned int sreq_max_segs; /* 0 or max number of segments in an iterator */
139288ace2fSDavid Howells unsigned int submit_off; /* Folio offset we're submitting from */
140288ace2fSDavid Howells unsigned int submit_len; /* Amount of data left to submit */
141c4f1450eSDavid Howells unsigned int submit_extendable_to; /* Amount I/O can be rounded up to */
142288ace2fSDavid Howells void (*prepare_write)(struct netfs_io_subrequest *subreq);
143288ace2fSDavid Howells void (*issue_write)(struct netfs_io_subrequest *subreq);
144288ace2fSDavid Howells /* Collection tracking */
145288ace2fSDavid Howells struct list_head subrequests; /* Contributory I/O operations */
146288ace2fSDavid Howells struct netfs_io_subrequest *front; /* Op being collected */
147288ace2fSDavid Howells unsigned long long collected_to; /* Position we've collected results to */
148288ace2fSDavid Howells size_t transferred; /* The amount transferred from this stream */
149288ace2fSDavid Howells enum netfs_io_source source; /* Where to read from/write to */
150288ace2fSDavid Howells unsigned short error; /* Aggregate error for the stream */
151288ace2fSDavid Howells unsigned char stream_nr; /* Index of stream in parent table */
152288ace2fSDavid Howells bool avail; /* T if stream is available */
153288ace2fSDavid Howells bool active; /* T if stream is active */
154288ace2fSDavid Howells bool need_retry; /* T if this stream needs retrying */
155288ace2fSDavid Howells bool failed; /* T if this stream failed */
156288ace2fSDavid Howells };
157288ace2fSDavid Howells
158288ace2fSDavid Howells /*
159726218fdSDavid Howells * Resources required to do operations on a cache.
160726218fdSDavid Howells */
161726218fdSDavid Howells struct netfs_cache_resources {
162726218fdSDavid Howells const struct netfs_cache_ops *ops;
163726218fdSDavid Howells void *cache_priv;
164726218fdSDavid Howells void *cache_priv2;
165a7e20e31SDavid Howells unsigned int debug_id; /* Cookie debug ID */
166d24af13eSDavid Howells unsigned int inval_counter; /* object->inval_counter at begin_op */
167726218fdSDavid Howells };
168726218fdSDavid Howells
1693d3c9504SDavid Howells /*
17016af134cSDavid Howells * Descriptor for a single component subrequest. Each operation represents an
17116af134cSDavid Howells * individual read/write from/to a server, a cache, a journal, etc..
17216af134cSDavid Howells *
17316af134cSDavid Howells * The buffer iterator is persistent for the life of the subrequest struct and
17416af134cSDavid Howells * the pages it points to can be relied on to exist for the duration.
1753d3c9504SDavid Howells */
1766a19114bSDavid Howells struct netfs_io_subrequest {
177f18a3785SDavid Howells struct netfs_io_request *rreq; /* Supervising I/O request */
178040a82beSDavid Howells struct work_struct work;
1793d3c9504SDavid Howells struct list_head rreq_link; /* Link in rreq->subrequests */
18092b6cc5dSDavid Howells struct iov_iter io_iter; /* Iterator for this subrequest */
1817ba167c4SDavid Howells unsigned long long start; /* Where to start the I/O */
1823d3c9504SDavid Howells size_t len; /* Size of the I/O */
1833d3c9504SDavid Howells size_t transferred; /* Amount of data transferred */
1846cd3d6fdSDavid Howells refcount_t ref;
1853d3c9504SDavid Howells short error; /* 0 or error that occurred */
1863d3c9504SDavid Howells unsigned short debug_index; /* Index in list (for debugging output) */
187288ace2fSDavid Howells unsigned int nr_segs; /* Number of segs in io_iter */
1884acb665cSDavid Howells u8 retry_count; /* The number of retries (0 on initial pass) */
189f18a3785SDavid Howells enum netfs_io_source source; /* Where to read from/write to */
190288ace2fSDavid Howells unsigned char stream_nr; /* I/O stream this belongs to */
1913d3c9504SDavid Howells unsigned long flags;
192f18a3785SDavid Howells #define NETFS_SREQ_COPY_TO_CACHE 0 /* Set if should copy the data to the cache */
1933d3c9504SDavid Howells #define NETFS_SREQ_CLEAR_TAIL 1 /* Set if the rest of the read should be cleared */
1943d3c9504SDavid Howells #define NETFS_SREQ_SEEK_DATA_READ 3 /* Set if ->read() should SEEK_DATA first */
1954acb665cSDavid Howells #define NETFS_SREQ_MADE_PROGRESS 4 /* Set if we transferred at least some data */
1969032b6e8SJeffle Xu #define NETFS_SREQ_ONDEMAND 5 /* Set if it's from on-demand read mode */
197288ace2fSDavid Howells #define NETFS_SREQ_BOUNDARY 6 /* Set if ends on hard boundary (eg. ceph object) */
198ee4cdf7bSDavid Howells #define NETFS_SREQ_HIT_EOF 7 /* Set if short due to EOF */
199288ace2fSDavid Howells #define NETFS_SREQ_IN_PROGRESS 8 /* Unlocked when the subrequest completes */
200288ace2fSDavid Howells #define NETFS_SREQ_NEED_RETRY 9 /* Set if the filesystem requests a retry */
2014acb665cSDavid Howells #define NETFS_SREQ_FAILED 10 /* Set if the subreq failed unretryably */
2023d3c9504SDavid Howells };
2033d3c9504SDavid Howells
204663dfb65SDavid Howells enum netfs_io_origin {
205663dfb65SDavid Howells NETFS_READAHEAD, /* This read was triggered by readahead */
206663dfb65SDavid Howells NETFS_READPAGE, /* This read is a synchronous read */
207ee4cdf7bSDavid Howells NETFS_READ_GAPS, /* This read is a synchronous read to fill gaps */
20849866ce7SDavid Howells NETFS_READ_SINGLE, /* This read should be treated as a single object */
209663dfb65SDavid Howells NETFS_READ_FOR_WRITE, /* This read is to prepare a write */
210c57de2a9SDavid Howells NETFS_DIO_READ, /* This is a direct I/O read */
21116af134cSDavid Howells NETFS_WRITEBACK, /* This write was triggered by writepages */
21249866ce7SDavid Howells NETFS_WRITEBACK_SINGLE, /* This monolithic write was triggered by writepages */
21341d8e767SDavid Howells NETFS_WRITETHROUGH, /* This write was made by netfs_perform_write() */
214153a9961SDavid Howells NETFS_UNBUFFERED_WRITE, /* This is an unbuffered write */
215153a9961SDavid Howells NETFS_DIO_WRITE, /* This is a direct I/O write */
216ee4cdf7bSDavid Howells NETFS_PGPRIV2_COPY_TO_CACHE, /* [DEPRECATED] This is writing read data to the cache */
21716af134cSDavid Howells nr__netfs_io_origin
218663dfb65SDavid Howells } __mode(byte);
219663dfb65SDavid Howells
2203d3c9504SDavid Howells /*
221f18a3785SDavid Howells * Descriptor for an I/O helper request. This is used to make multiple I/O
222f18a3785SDavid Howells * operations to a variety of data stores and then stitch the result together.
2233d3c9504SDavid Howells */
2246a19114bSDavid Howells struct netfs_io_request {
22587b57a04SDavid Howells union {
2263d3c9504SDavid Howells struct work_struct work;
22787b57a04SDavid Howells struct rcu_head rcu;
22887b57a04SDavid Howells };
2293d3c9504SDavid Howells struct inode *inode; /* The file being accessed */
2303d3c9504SDavid Howells struct address_space *mapping; /* The mapping being accessed */
231016dc851SDavid Howells struct kiocb *iocb; /* AIO completion vector */
232726218fdSDavid Howells struct netfs_cache_resources cache_resources;
233e2d46f2eSDavid Howells struct netfs_io_request *copy_to_cache; /* Request to write just-read data to the cache */
234ee4cdf7bSDavid Howells struct readahead_control *ractl; /* Readahead descriptor */
23587b57a04SDavid Howells struct list_head proc_link; /* Link in netfs_iorequests */
236288ace2fSDavid Howells struct netfs_io_stream io_streams[2]; /* Streams of parallel I/O operations */
237288ace2fSDavid Howells #define NR_IO_STREAMS 2 //wreq->nr_io_streams
238288ace2fSDavid Howells struct netfs_group *group; /* Writeback group being written back */
23906fa229cSDavid Howells struct rolling_buffer buffer; /* Unencrypted buffer */
24006fa229cSDavid Howells #define NETFS_ROLLBUF_PUT_MARK ROLLBUF_MARK_1
24106fa229cSDavid Howells #define NETFS_ROLLBUF_PAGECACHE_MARK ROLLBUF_MARK_2
242e2d46f2eSDavid Howells wait_queue_head_t waitq; /* Processor waiter */
2433d3c9504SDavid Howells void *netfs_priv; /* Private data for the netfs */
2441ecb146fSDavid Howells void *netfs_priv2; /* Private data for the netfs */
24521d706d5SDavid Howells struct bio_vec *direct_bv; /* DIO buffer list (when handling iovec-iter) */
24621d706d5SDavid Howells unsigned int direct_bv_count; /* Number of elements in direct_bv[] */
2473d3c9504SDavid Howells unsigned int debug_id;
248016dc851SDavid Howells unsigned int rsize; /* Maximum read size (0 for none) */
2490e0f2dfeSDavid Howells unsigned int wsize; /* Maximum write size (0 for none) */
25093bf1cc0SDavid Howells atomic_t subreq_counter; /* Next subreq->debug_index */
251288ace2fSDavid Howells unsigned int nr_group_rel; /* Number of refs to release on ->group */
252288ace2fSDavid Howells spinlock_t lock; /* Lock for queuing subreqs */
2537ba167c4SDavid Howells unsigned long long submitted; /* Amount submitted for I/O so far */
2547ba167c4SDavid Howells unsigned long long len; /* Length of the request */
255016dc851SDavid Howells size_t transferred; /* Amount to be indicated as transferred */
256ee4cdf7bSDavid Howells long error; /* 0 or error that occurred */
257663dfb65SDavid Howells enum netfs_io_origin origin; /* Origin of the request */
25821d706d5SDavid Howells bool direct_bv_unpin; /* T if direct_bv[] must be unpinned */
2597ba167c4SDavid Howells unsigned long long i_size; /* Size of the file */
2607ba167c4SDavid Howells unsigned long long start; /* Start position */
261288ace2fSDavid Howells atomic64_t issued_to; /* Write issuer folio cursor */
262288ace2fSDavid Howells unsigned long long collected_to; /* Point we've collected to */
263288ace2fSDavid Howells unsigned long long cleaned_to; /* Position we've cleaned folios to */
264e2d46f2eSDavid Howells unsigned long long abandon_to; /* Position to abandon folios to */
26578525c74SDavid Howells pgoff_t no_unlock_folio; /* Don't unlock this folio after read */
266e2d46f2eSDavid Howells unsigned char front_folio_order; /* Order (size) of front folio */
267de74023bSDavid Howells refcount_t ref;
2683d3c9504SDavid Howells unsigned long flags;
269e2d46f2eSDavid Howells #define NETFS_RREQ_OFFLOAD_COLLECTION 0 /* Offload collection to workqueue */
27078525c74SDavid Howells #define NETFS_RREQ_NO_UNLOCK_FOLIO 2 /* Don't unlock no_unlock_folio on completion */
27178525c74SDavid Howells #define NETFS_RREQ_DONT_UNLOCK_FOLIOS 3 /* Don't unlock the folios on completion */
2723d3c9504SDavid Howells #define NETFS_RREQ_FAILED 4 /* The request failed */
2733d3c9504SDavid Howells #define NETFS_RREQ_IN_PROGRESS 5 /* Unlocked when the request completes */
274e2d46f2eSDavid Howells #define NETFS_RREQ_FOLIO_COPY_TO_CACHE 6 /* Copy current folio to cache from read */
27516af134cSDavid Howells #define NETFS_RREQ_UPLOAD_TO_SERVER 8 /* Need to write to the server */
276016dc851SDavid Howells #define NETFS_RREQ_NONBLOCK 9 /* Don't block if possible (O_NONBLOCK) */
277016dc851SDavid Howells #define NETFS_RREQ_BLOCKED 10 /* We blocked */
278288ace2fSDavid Howells #define NETFS_RREQ_PAUSE 11 /* Pause subrequest generation */
279288ace2fSDavid Howells #define NETFS_RREQ_USE_IO_ITER 12 /* Use ->io_iter rather than ->i_pages */
280288ace2fSDavid Howells #define NETFS_RREQ_ALL_QUEUED 13 /* All subreqs are now queued */
281*1d001396SDavid Howells #define NETFS_RREQ_RETRYING 14 /* Set if we're in the retry path */
2822ff1e975SDavid Howells #define NETFS_RREQ_USE_PGPRIV2 31 /* [DEPRECATED] Use PG_private_2 to mark
2832ff1e975SDavid Howells * write to cache on read */
2846a19114bSDavid Howells const struct netfs_request_ops *netfs_ops;
2850e0f2dfeSDavid Howells void (*cleanup)(struct netfs_io_request *req);
2863d3c9504SDavid Howells };
2873d3c9504SDavid Howells
2883d3c9504SDavid Howells /*
2893d3c9504SDavid Howells * Operations the network filesystem can/must provide to the helpers.
2903d3c9504SDavid Howells */
2916a19114bSDavid Howells struct netfs_request_ops {
292d9f85a04SDavid Howells mempool_t *request_pool;
293d9f85a04SDavid Howells mempool_t *subrequest_pool;
2942de16041SDavid Howells int (*init_request)(struct netfs_io_request *rreq, struct file *file);
29540a81101SDavid Howells void (*free_request)(struct netfs_io_request *rreq);
2965f5ce7baSDavid Howells void (*free_subrequest)(struct netfs_io_subrequest *rreq);
29740a81101SDavid Howells
298c6dc54ddSDavid Howells /* Read request handling */
2996a19114bSDavid Howells void (*expand_readahead)(struct netfs_io_request *rreq);
300ee4cdf7bSDavid Howells int (*prepare_read)(struct netfs_io_subrequest *subreq);
301f18a3785SDavid Howells void (*issue_read)(struct netfs_io_subrequest *subreq);
3026a19114bSDavid Howells bool (*is_still_valid)(struct netfs_io_request *rreq);
303e1b1240cSDavid Howells int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
304fac47b43SXiubo Li struct folio **foliop, void **_fsdata);
3056a19114bSDavid Howells void (*done)(struct netfs_io_request *rreq);
306c6dc54ddSDavid Howells
307c6dc54ddSDavid Howells /* Modification handling */
308c6dc54ddSDavid Howells void (*update_i_size)(struct inode *inode, loff_t i_size);
30969c3c023SDavid Howells void (*post_modify)(struct inode *inode);
3100e0f2dfeSDavid Howells
3110e0f2dfeSDavid Howells /* Write request handling */
312288ace2fSDavid Howells void (*begin_writeback)(struct netfs_io_request *wreq);
313288ace2fSDavid Howells void (*prepare_write)(struct netfs_io_subrequest *subreq);
314288ace2fSDavid Howells void (*issue_write)(struct netfs_io_subrequest *subreq);
3151ecb146fSDavid Howells void (*retry_request)(struct netfs_io_request *wreq, struct netfs_io_stream *stream);
3160e0f2dfeSDavid Howells void (*invalidate_cache)(struct netfs_io_request *wreq);
3173d3c9504SDavid Howells };
3183d3c9504SDavid Howells
319726218fdSDavid Howells /*
3203a11b3a8SDavid Howells * How to handle reading from a hole.
3213a11b3a8SDavid Howells */
3223a11b3a8SDavid Howells enum netfs_read_from_hole {
3233a11b3a8SDavid Howells NETFS_READ_HOLE_IGNORE,
3243a11b3a8SDavid Howells NETFS_READ_HOLE_CLEAR,
3253a11b3a8SDavid Howells NETFS_READ_HOLE_FAIL,
3263a11b3a8SDavid Howells };
3273a11b3a8SDavid Howells
3283a11b3a8SDavid Howells /*
3294498a8ecSDavid Howells * Table of operations for access to a cache.
330726218fdSDavid Howells */
331726218fdSDavid Howells struct netfs_cache_ops {
332726218fdSDavid Howells /* End an operation */
333726218fdSDavid Howells void (*end_operation)(struct netfs_cache_resources *cres);
334726218fdSDavid Howells
335726218fdSDavid Howells /* Read data from the cache */
336726218fdSDavid Howells int (*read)(struct netfs_cache_resources *cres,
337726218fdSDavid Howells loff_t start_pos,
338726218fdSDavid Howells struct iov_iter *iter,
3393a11b3a8SDavid Howells enum netfs_read_from_hole read_hole,
340726218fdSDavid Howells netfs_io_terminated_t term_func,
341726218fdSDavid Howells void *term_func_priv);
342726218fdSDavid Howells
343726218fdSDavid Howells /* Write data to the cache */
344726218fdSDavid Howells int (*write)(struct netfs_cache_resources *cres,
345726218fdSDavid Howells loff_t start_pos,
346726218fdSDavid Howells struct iov_iter *iter,
347726218fdSDavid Howells netfs_io_terminated_t term_func,
348726218fdSDavid Howells void *term_func_priv);
349726218fdSDavid Howells
350288ace2fSDavid Howells /* Write data to the cache from a netfs subrequest. */
351288ace2fSDavid Howells void (*issue_write)(struct netfs_io_subrequest *subreq);
352288ace2fSDavid Howells
353726218fdSDavid Howells /* Expand readahead request */
354726218fdSDavid Howells void (*expand_readahead)(struct netfs_cache_resources *cres,
3557ba167c4SDavid Howells unsigned long long *_start,
3567ba167c4SDavid Howells unsigned long long *_len,
3577ba167c4SDavid Howells unsigned long long i_size);
358726218fdSDavid Howells
359726218fdSDavid Howells /* Prepare a read operation, shortening it to a cached/uncached
360726218fdSDavid Howells * boundary as appropriate.
361726218fdSDavid Howells */
3626a19114bSDavid Howells enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq,
3637ba167c4SDavid Howells unsigned long long i_size);
364726218fdSDavid Howells
365288ace2fSDavid Howells /* Prepare a write subrequest, working out if we're allowed to do it
366288ace2fSDavid Howells * and finding out the maximum amount of data to gather before
367288ace2fSDavid Howells * attempting to submit. If we're not permitted to do it, the
368288ace2fSDavid Howells * subrequest should be marked failed.
369288ace2fSDavid Howells */
370288ace2fSDavid Howells void (*prepare_write_subreq)(struct netfs_io_subrequest *subreq);
371288ace2fSDavid Howells
372726218fdSDavid Howells /* Prepare a write operation, working out what part of the write we can
373726218fdSDavid Howells * actually do.
374726218fdSDavid Howells */
375726218fdSDavid Howells int (*prepare_write)(struct netfs_cache_resources *cres,
376e0ace6caSDavid Howells loff_t *_start, size_t *_len, size_t upper_len,
377e0ace6caSDavid Howells loff_t i_size, bool no_space_allocated_yet);
378bee9f655SDavid Howells
37986692475SJingbo Xu /* Prepare an on-demand read operation, shortening it to a cached/uncached
38086692475SJingbo Xu * boundary as appropriate.
38186692475SJingbo Xu */
38286692475SJingbo Xu enum netfs_io_source (*prepare_ondemand_read)(struct netfs_cache_resources *cres,
38386692475SJingbo Xu loff_t start, size_t *_len,
38486692475SJingbo Xu loff_t i_size,
38586692475SJingbo Xu unsigned long *_flags, ino_t ino);
38686692475SJingbo Xu
387bee9f655SDavid Howells /* Query the occupancy of the cache in a region, returning where the
388bee9f655SDavid Howells * next chunk of data starts and how long it is.
389bee9f655SDavid Howells */
390bee9f655SDavid Howells int (*query_occupancy)(struct netfs_cache_resources *cres,
391bee9f655SDavid Howells loff_t start, size_t len, size_t granularity,
392bee9f655SDavid Howells loff_t *_data_start, size_t *_data_len);
393726218fdSDavid Howells };
394726218fdSDavid Howells
395016dc851SDavid Howells /* High-level read API. */
39614b1cd25SSteve French ssize_t netfs_unbuffered_read_iter_locked(struct kiocb *iocb, struct iov_iter *iter);
397016dc851SDavid Howells ssize_t netfs_unbuffered_read_iter(struct kiocb *iocb, struct iov_iter *iter);
39880645bd4SDavid Howells ssize_t netfs_buffered_read_iter(struct kiocb *iocb, struct iov_iter *iter);
39980645bd4SDavid Howells ssize_t netfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
400016dc851SDavid Howells
401c38f4e96SDavid Howells /* High-level write API */
402c38f4e96SDavid Howells ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
403c38f4e96SDavid Howells struct netfs_group *netfs_group);
404938e13a7SDavid Howells ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *from,
405938e13a7SDavid Howells struct netfs_group *netfs_group);
406153a9961SDavid Howells ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from);
40716e00683SSteve French ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *iter,
40816e00683SSteve French struct netfs_group *netfs_group);
409938e13a7SDavid Howells ssize_t netfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from);
410c38f4e96SDavid Howells
41149866ce7SDavid Howells /* Single, monolithic object read/write API. */
41249866ce7SDavid Howells void netfs_single_mark_inode_dirty(struct inode *inode);
41349866ce7SDavid Howells ssize_t netfs_read_single(struct inode *inode, struct file *file, struct iov_iter *iter);
41449866ce7SDavid Howells int netfs_writeback_single(struct address_space *mapping,
41549866ce7SDavid Howells struct writeback_control *wbc,
41649866ce7SDavid Howells struct iov_iter *iter);
41749866ce7SDavid Howells
418c38f4e96SDavid Howells /* Address operations API */
4193d3c9504SDavid Howells struct readahead_control;
4200e8e08ccSMatthew Wilcox (Oracle) void netfs_readahead(struct readahead_control *);
4216c62371bSMatthew Wilcox (Oracle) int netfs_read_folio(struct file *, struct folio *);
4220e8e08ccSMatthew Wilcox (Oracle) int netfs_write_begin(struct netfs_inode *, struct file *,
4230e8e08ccSMatthew Wilcox (Oracle) struct address_space *, loff_t pos, unsigned int len,
4240e8e08ccSMatthew Wilcox (Oracle) struct folio **, void **fsdata);
42562c3b748SDavid Howells int netfs_writepages(struct address_space *mapping,
42662c3b748SDavid Howells struct writeback_control *wbc);
427c9c4ff12SDavid Howells bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio);
428c9c4ff12SDavid Howells int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc);
429c9c4ff12SDavid Howells void netfs_clear_inode_writeback(struct inode *inode, const void *aux);
430c1ec4d7cSDavid Howells void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
431c1ec4d7cSDavid Howells bool netfs_release_folio(struct folio *folio, gfp_t gfp);
4323d3c9504SDavid Howells
433102a7e2cSDavid Howells /* VMA operations API. */
434102a7e2cSDavid Howells vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_group);
435102a7e2cSDavid Howells
436102a7e2cSDavid Howells /* (Sub)request management API. */
43731fc366aSDavid Howells void netfs_read_subreq_progress(struct netfs_io_subrequest *subreq);
43831fc366aSDavid Howells void netfs_read_subreq_terminated(struct netfs_io_subrequest *subreq);
4390e8e08ccSMatthew Wilcox (Oracle) void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
4406cd3d6fdSDavid Howells enum netfs_sreq_ref_trace what);
4410e8e08ccSMatthew Wilcox (Oracle) void netfs_put_subrequest(struct netfs_io_subrequest *subreq,
4426cd3d6fdSDavid Howells bool was_async, enum netfs_sreq_ref_trace what);
44385dd2c8fSDavid Howells ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len,
44485dd2c8fSDavid Howells struct iov_iter *new,
44585dd2c8fSDavid Howells iov_iter_extraction_t extraction_flags);
446cae932d3SDavid Howells size_t netfs_limit_iter(const struct iov_iter *iter, size_t start_offset,
447cae932d3SDavid Howells size_t max_size, size_t max_segs);
448288ace2fSDavid Howells void netfs_prepare_write_failed(struct netfs_io_subrequest *subreq);
4490e0f2dfeSDavid Howells void netfs_write_subrequest_terminated(void *_op, ssize_t transferred_or_error,
4500e0f2dfeSDavid Howells bool was_async);
4510e0f2dfeSDavid Howells void netfs_queue_write_request(struct netfs_io_subrequest *subreq);
4523d3c9504SDavid Howells
45346ed60dcSDavid Howells int netfs_start_io_read(struct inode *inode);
45446ed60dcSDavid Howells void netfs_end_io_read(struct inode *inode);
45546ed60dcSDavid Howells int netfs_start_io_write(struct inode *inode);
45646ed60dcSDavid Howells void netfs_end_io_write(struct inode *inode);
45746ed60dcSDavid Howells int netfs_start_io_direct(struct inode *inode);
45846ed60dcSDavid Howells void netfs_end_io_direct(struct inode *inode);
45946ed60dcSDavid Howells
460eb118159SDavid Howells /* Miscellaneous APIs. */
461aabcabf2SDavid Howells struct folio_queue *netfs_folioq_alloc(unsigned int rreq_id, gfp_t gfp,
462aabcabf2SDavid Howells unsigned int trace /*enum netfs_folioq_trace*/);
463aabcabf2SDavid Howells void netfs_folioq_free(struct folio_queue *folioq,
464aabcabf2SDavid Howells unsigned int trace /*enum netfs_trace_folioq*/);
465eb118159SDavid Howells
466e61bfaadSDavid Howells /* Buffer wrangling helpers API. */
467e61bfaadSDavid Howells int netfs_alloc_folioq_buffer(struct address_space *mapping,
468e61bfaadSDavid Howells struct folio_queue **_buffer,
469e61bfaadSDavid Howells size_t *_cur_size, ssize_t size, gfp_t gfp);
470e61bfaadSDavid Howells void netfs_free_folioq_buffer(struct folio_queue *fq);
471e61bfaadSDavid Howells
472bc899ee1SDavid Howells /**
473874c8ca1SDavid Howells * netfs_inode - Get the netfs inode context from the inode
474bc899ee1SDavid Howells * @inode: The inode to query
475bc899ee1SDavid Howells *
476bc899ee1SDavid Howells * Get the netfs lib inode context from the network filesystem's inode. The
477bc899ee1SDavid Howells * context struct is expected to directly follow on from the VFS inode struct.
478bc899ee1SDavid Howells */
netfs_inode(struct inode * inode)479874c8ca1SDavid Howells static inline struct netfs_inode *netfs_inode(struct inode *inode)
480bc899ee1SDavid Howells {
481874c8ca1SDavid Howells return container_of(inode, struct netfs_inode, inode);
482bc899ee1SDavid Howells }
483bc899ee1SDavid Howells
484bc899ee1SDavid Howells /**
485874c8ca1SDavid Howells * netfs_inode_init - Initialise a netfslib inode context
486018ab4faSLinus Torvalds * @ctx: The netfs inode to initialise
487bc899ee1SDavid Howells * @ops: The netfs's operations list
488100ccd18SDavid Howells * @use_zero_point: True to use the zero_point read optimisation
489bc899ee1SDavid Howells *
490bc899ee1SDavid Howells * Initialise the netfs library context struct. This is expected to follow on
491bc899ee1SDavid Howells * directly from the VFS inode struct.
492bc899ee1SDavid Howells */
netfs_inode_init(struct netfs_inode * ctx,const struct netfs_request_ops * ops,bool use_zero_point)493e81fb419SLinus Torvalds static inline void netfs_inode_init(struct netfs_inode *ctx,
494100ccd18SDavid Howells const struct netfs_request_ops *ops,
495100ccd18SDavid Howells bool use_zero_point)
496bc899ee1SDavid Howells {
497bc899ee1SDavid Howells ctx->ops = ops;
498e81fb419SLinus Torvalds ctx->remote_i_size = i_size_read(&ctx->inode);
499100ccd18SDavid Howells ctx->zero_point = LLONG_MAX;
50046ed60dcSDavid Howells ctx->flags = 0;
501f89ea63fSDavid Howells atomic_set(&ctx->io_count, 0);
502874c8ca1SDavid Howells #if IS_ENABLED(CONFIG_FSCACHE)
503874c8ca1SDavid Howells ctx->cache = NULL;
504874c8ca1SDavid Howells #endif
505288ace2fSDavid Howells mutex_init(&ctx->wb_lock);
506100ccd18SDavid Howells /* ->releasepage() drives zero_point */
507100ccd18SDavid Howells if (use_zero_point) {
508100ccd18SDavid Howells ctx->zero_point = ctx->remote_i_size;
509100ccd18SDavid Howells mapping_set_release_always(ctx->inode.i_mapping);
510100ccd18SDavid Howells }
5114058f742SDavid Howells }
5124058f742SDavid Howells
5134058f742SDavid Howells /**
5144058f742SDavid Howells * netfs_resize_file - Note that a file got resized
515e81fb419SLinus Torvalds * @ctx: The netfs inode being resized
5164058f742SDavid Howells * @new_i_size: The new file size
517100ccd18SDavid Howells * @changed_on_server: The change was applied to the server
5184058f742SDavid Howells *
5194058f742SDavid Howells * Inform the netfs lib that a file got resized so that it can adjust its state.
5204058f742SDavid Howells */
netfs_resize_file(struct netfs_inode * ctx,loff_t new_i_size,bool changed_on_server)521100ccd18SDavid Howells static inline void netfs_resize_file(struct netfs_inode *ctx, loff_t new_i_size,
522100ccd18SDavid Howells bool changed_on_server)
5234058f742SDavid Howells {
524100ccd18SDavid Howells if (changed_on_server)
5254058f742SDavid Howells ctx->remote_i_size = new_i_size;
526100ccd18SDavid Howells if (new_i_size < ctx->zero_point)
527100ccd18SDavid Howells ctx->zero_point = new_i_size;
528bc899ee1SDavid Howells }
529bc899ee1SDavid Howells
530bc899ee1SDavid Howells /**
531bc899ee1SDavid Howells * netfs_i_cookie - Get the cache cookie from the inode
532e81fb419SLinus Torvalds * @ctx: The netfs inode to query
533bc899ee1SDavid Howells *
534bc899ee1SDavid Howells * Get the caching cookie (if enabled) from the network filesystem's inode.
535bc899ee1SDavid Howells */
netfs_i_cookie(struct netfs_inode * ctx)536e81fb419SLinus Torvalds static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx)
537bc899ee1SDavid Howells {
538bc899ee1SDavid Howells #if IS_ENABLED(CONFIG_FSCACHE)
539bc899ee1SDavid Howells return ctx->cache;
540bc899ee1SDavid Howells #else
541bc899ee1SDavid Howells return NULL;
542bc899ee1SDavid Howells #endif
543bc899ee1SDavid Howells }
544bc899ee1SDavid Howells
545f89ea63fSDavid Howells /**
546f89ea63fSDavid Howells * netfs_wait_for_outstanding_io - Wait for outstanding I/O to complete
547db003a28SChristian Brauner * @inode: The netfs inode to wait on
548f89ea63fSDavid Howells *
549f89ea63fSDavid Howells * Wait for outstanding I/O requests of any type to complete. This is intended
550f89ea63fSDavid Howells * to be called from inode eviction routines. This makes sure that any
551f89ea63fSDavid Howells * resources held by those requests are cleaned up before we let the inode get
552f89ea63fSDavid Howells * cleaned up.
553f89ea63fSDavid Howells */
netfs_wait_for_outstanding_io(struct inode * inode)554f89ea63fSDavid Howells static inline void netfs_wait_for_outstanding_io(struct inode *inode)
555f89ea63fSDavid Howells {
556f89ea63fSDavid Howells struct netfs_inode *ictx = netfs_inode(inode);
557f89ea63fSDavid Howells
558f89ea63fSDavid Howells wait_var_event(&ictx->io_count, atomic_read(&ictx->io_count) == 0);
559f89ea63fSDavid Howells }
560f89ea63fSDavid Howells
561b533a83fSDavid Howells #endif /* _LINUX_NETFS_H */
562