netfs: Limit subrequest by size or number of segments
[linux-2.6-microblaze.git] / include / linux / netfs.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Network filesystem support services.
3  *
4  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
7  * See:
8  *
9  *      Documentation/filesystems/netfs_library.rst
10  *
11  * for a description of the network filesystem interface declared here.
12  */
13
14 #ifndef _LINUX_NETFS_H
15 #define _LINUX_NETFS_H
16
17 #include <linux/workqueue.h>
18 #include <linux/fs.h>
19 #include <linux/pagemap.h>
20 #include <linux/uio.h>
21
22 enum netfs_sreq_ref_trace;
23
24 /*
25  * Overload PG_private_2 to give us PG_fscache - this is used to indicate that
26  * a page is currently backed by a local disk cache
27  */
28 #define folio_test_fscache(folio)       folio_test_private_2(folio)
29 #define PageFsCache(page)               PagePrivate2((page))
30 #define SetPageFsCache(page)            SetPagePrivate2((page))
31 #define ClearPageFsCache(page)          ClearPagePrivate2((page))
32 #define TestSetPageFsCache(page)        TestSetPagePrivate2((page))
33 #define TestClearPageFsCache(page)      TestClearPagePrivate2((page))
34
35 /**
36  * folio_start_fscache - Start an fscache write on a folio.
37  * @folio: The folio.
38  *
39  * Call this function before writing a folio to a local cache.  Starting a
40  * second write before the first one finishes is not allowed.
41  */
42 static inline void folio_start_fscache(struct folio *folio)
43 {
44         VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio);
45         folio_get(folio);
46         folio_set_private_2(folio);
47 }
48
49 /**
50  * folio_end_fscache - End an fscache write on a folio.
51  * @folio: The folio.
52  *
53  * Call this function after the folio has been written to the local cache.
54  * This will wake any sleepers waiting on this folio.
55  */
56 static inline void folio_end_fscache(struct folio *folio)
57 {
58         folio_end_private_2(folio);
59 }
60
61 /**
62  * folio_wait_fscache - Wait for an fscache write on this folio to end.
63  * @folio: The folio.
64  *
65  * If this folio is currently being written to a local cache, wait for
66  * the write to finish.  Another write may start after this one finishes,
67  * unless the caller holds the folio lock.
68  */
69 static inline void folio_wait_fscache(struct folio *folio)
70 {
71         folio_wait_private_2(folio);
72 }
73
74 /**
75  * folio_wait_fscache_killable - Wait for an fscache write on this folio to end.
76  * @folio: The folio.
77  *
78  * If this folio is currently being written to a local cache, wait
79  * for the write to finish or for a fatal signal to be received.
80  * Another write may start after this one finishes, unless the caller
81  * holds the folio lock.
82  *
83  * Return:
84  * - 0 if successful.
85  * - -EINTR if a fatal signal was encountered.
86  */
87 static inline int folio_wait_fscache_killable(struct folio *folio)
88 {
89         return folio_wait_private_2_killable(folio);
90 }
91
92 static inline void set_page_fscache(struct page *page)
93 {
94         folio_start_fscache(page_folio(page));
95 }
96
97 static inline void end_page_fscache(struct page *page)
98 {
99         folio_end_private_2(page_folio(page));
100 }
101
102 static inline void wait_on_page_fscache(struct page *page)
103 {
104         folio_wait_private_2(page_folio(page));
105 }
106
107 static inline int wait_on_page_fscache_killable(struct page *page)
108 {
109         return folio_wait_private_2_killable(page_folio(page));
110 }
111
112 /* Marks used on xarray-based buffers */
113 #define NETFS_BUF_PUT_MARK      XA_MARK_0       /* - Page needs putting  */
114 #define NETFS_BUF_PAGECACHE_MARK XA_MARK_1      /* - Page needs wb/dirty flag wrangling */
115
116 enum netfs_io_source {
117         NETFS_FILL_WITH_ZEROES,
118         NETFS_DOWNLOAD_FROM_SERVER,
119         NETFS_READ_FROM_CACHE,
120         NETFS_INVALID_READ,
121 } __mode(byte);
122
123 typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error,
124                                       bool was_async);
125
126 /*
127  * Per-inode context.  This wraps the VFS inode.
128  */
129 struct netfs_inode {
130         struct inode            inode;          /* The VFS inode */
131         const struct netfs_request_ops *ops;
132 #if IS_ENABLED(CONFIG_FSCACHE)
133         struct fscache_cookie   *cache;
134 #endif
135         loff_t                  remote_i_size;  /* Size of the remote file */
136         unsigned long           flags;
137 #define NETFS_ICTX_ODIRECT      0               /* The file has DIO in progress */
138 };
139
140 /*
141  * Resources required to do operations on a cache.
142  */
143 struct netfs_cache_resources {
144         const struct netfs_cache_ops    *ops;
145         void                            *cache_priv;
146         void                            *cache_priv2;
147         unsigned int                    debug_id;       /* Cookie debug ID */
148         unsigned int                    inval_counter;  /* object->inval_counter at begin_op */
149 };
150
151 /*
152  * Descriptor for a single component subrequest.
153  */
154 struct netfs_io_subrequest {
155         struct netfs_io_request *rreq;          /* Supervising I/O request */
156         struct list_head        rreq_link;      /* Link in rreq->subrequests */
157         struct iov_iter         io_iter;        /* Iterator for this subrequest */
158         loff_t                  start;          /* Where to start the I/O */
159         size_t                  len;            /* Size of the I/O */
160         size_t                  transferred;    /* Amount of data transferred */
161         refcount_t              ref;
162         short                   error;          /* 0 or error that occurred */
163         unsigned short          debug_index;    /* Index in list (for debugging output) */
164         unsigned int            max_nr_segs;    /* 0 or max number of segments in an iterator */
165         enum netfs_io_source    source;         /* Where to read from/write to */
166         unsigned long           flags;
167 #define NETFS_SREQ_COPY_TO_CACHE        0       /* Set if should copy the data to the cache */
168 #define NETFS_SREQ_CLEAR_TAIL           1       /* Set if the rest of the read should be cleared */
169 #define NETFS_SREQ_SHORT_IO             2       /* Set if the I/O was short */
170 #define NETFS_SREQ_SEEK_DATA_READ       3       /* Set if ->read() should SEEK_DATA first */
171 #define NETFS_SREQ_NO_PROGRESS          4       /* Set if we didn't manage to read any data */
172 #define NETFS_SREQ_ONDEMAND             5       /* Set if it's from on-demand read mode */
173 };
174
175 enum netfs_io_origin {
176         NETFS_READAHEAD,                /* This read was triggered by readahead */
177         NETFS_READPAGE,                 /* This read is a synchronous read */
178         NETFS_READ_FOR_WRITE,           /* This read is to prepare a write */
179 } __mode(byte);
180
181 /*
182  * Descriptor for an I/O helper request.  This is used to make multiple I/O
183  * operations to a variety of data stores and then stitch the result together.
184  */
185 struct netfs_io_request {
186         union {
187                 struct work_struct work;
188                 struct rcu_head rcu;
189         };
190         struct inode            *inode;         /* The file being accessed */
191         struct address_space    *mapping;       /* The mapping being accessed */
192         struct netfs_cache_resources cache_resources;
193         struct list_head        proc_link;      /* Link in netfs_iorequests */
194         struct list_head        subrequests;    /* Contributory I/O operations */
195         struct iov_iter         iter;           /* Unencrypted-side iterator */
196         struct iov_iter         io_iter;        /* I/O (Encrypted-side) iterator */
197         void                    *netfs_priv;    /* Private data for the netfs */
198         struct bio_vec          *direct_bv;     /* DIO buffer list (when handling iovec-iter) */
199         unsigned int            direct_bv_count; /* Number of elements in direct_bv[] */
200         unsigned int            debug_id;
201         atomic_t                nr_outstanding; /* Number of ops in progress */
202         atomic_t                nr_copy_ops;    /* Number of copy-to-cache ops in progress */
203         size_t                  submitted;      /* Amount submitted for I/O so far */
204         size_t                  len;            /* Length of the request */
205         short                   error;          /* 0 or error that occurred */
206         enum netfs_io_origin    origin;         /* Origin of the request */
207         bool                    direct_bv_unpin; /* T if direct_bv[] must be unpinned */
208         loff_t                  i_size;         /* Size of the file */
209         loff_t                  start;          /* Start position */
210         pgoff_t                 no_unlock_folio; /* Don't unlock this folio after read */
211         refcount_t              ref;
212         unsigned long           flags;
213 #define NETFS_RREQ_INCOMPLETE_IO        0       /* Some ioreqs terminated short or with error */
214 #define NETFS_RREQ_COPY_TO_CACHE        1       /* Need to write to the cache */
215 #define NETFS_RREQ_NO_UNLOCK_FOLIO      2       /* Don't unlock no_unlock_folio on completion */
216 #define NETFS_RREQ_DONT_UNLOCK_FOLIOS   3       /* Don't unlock the folios on completion */
217 #define NETFS_RREQ_FAILED               4       /* The request failed */
218 #define NETFS_RREQ_IN_PROGRESS          5       /* Unlocked when the request completes */
219         const struct netfs_request_ops *netfs_ops;
220 };
221
222 /*
223  * Operations the network filesystem can/must provide to the helpers.
224  */
225 struct netfs_request_ops {
226         unsigned int    io_request_size;        /* Alloc size for netfs_io_request struct */
227         unsigned int    io_subrequest_size;     /* Alloc size for netfs_io_subrequest struct */
228         int (*init_request)(struct netfs_io_request *rreq, struct file *file);
229         void (*free_request)(struct netfs_io_request *rreq);
230         void (*free_subrequest)(struct netfs_io_subrequest *rreq);
231
232         void (*expand_readahead)(struct netfs_io_request *rreq);
233         bool (*clamp_length)(struct netfs_io_subrequest *subreq);
234         void (*issue_read)(struct netfs_io_subrequest *subreq);
235         bool (*is_still_valid)(struct netfs_io_request *rreq);
236         int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
237                                  struct folio **foliop, void **_fsdata);
238         void (*done)(struct netfs_io_request *rreq);
239 };
240
241 /*
242  * How to handle reading from a hole.
243  */
244 enum netfs_read_from_hole {
245         NETFS_READ_HOLE_IGNORE,
246         NETFS_READ_HOLE_CLEAR,
247         NETFS_READ_HOLE_FAIL,
248 };
249
250 /*
251  * Table of operations for access to a cache.
252  */
253 struct netfs_cache_ops {
254         /* End an operation */
255         void (*end_operation)(struct netfs_cache_resources *cres);
256
257         /* Read data from the cache */
258         int (*read)(struct netfs_cache_resources *cres,
259                     loff_t start_pos,
260                     struct iov_iter *iter,
261                     enum netfs_read_from_hole read_hole,
262                     netfs_io_terminated_t term_func,
263                     void *term_func_priv);
264
265         /* Write data to the cache */
266         int (*write)(struct netfs_cache_resources *cres,
267                      loff_t start_pos,
268                      struct iov_iter *iter,
269                      netfs_io_terminated_t term_func,
270                      void *term_func_priv);
271
272         /* Expand readahead request */
273         void (*expand_readahead)(struct netfs_cache_resources *cres,
274                                  loff_t *_start, size_t *_len, loff_t i_size);
275
276         /* Prepare a read operation, shortening it to a cached/uncached
277          * boundary as appropriate.
278          */
279         enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq,
280                                              loff_t i_size);
281
282         /* Prepare a write operation, working out what part of the write we can
283          * actually do.
284          */
285         int (*prepare_write)(struct netfs_cache_resources *cres,
286                              loff_t *_start, size_t *_len, loff_t i_size,
287                              bool no_space_allocated_yet);
288
289         /* Prepare an on-demand read operation, shortening it to a cached/uncached
290          * boundary as appropriate.
291          */
292         enum netfs_io_source (*prepare_ondemand_read)(struct netfs_cache_resources *cres,
293                                                       loff_t start, size_t *_len,
294                                                       loff_t i_size,
295                                                       unsigned long *_flags, ino_t ino);
296
297         /* Query the occupancy of the cache in a region, returning where the
298          * next chunk of data starts and how long it is.
299          */
300         int (*query_occupancy)(struct netfs_cache_resources *cres,
301                                loff_t start, size_t len, size_t granularity,
302                                loff_t *_data_start, size_t *_data_len);
303 };
304
305 struct readahead_control;
306 void netfs_readahead(struct readahead_control *);
307 int netfs_read_folio(struct file *, struct folio *);
308 int netfs_write_begin(struct netfs_inode *, struct file *,
309                       struct address_space *, loff_t pos, unsigned int len,
310                       struct folio **, void **fsdata);
311 bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio);
312 int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc);
313 void netfs_clear_inode_writeback(struct inode *inode, const void *aux);
314 void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
315 bool netfs_release_folio(struct folio *folio, gfp_t gfp);
316
317 void netfs_subreq_terminated(struct netfs_io_subrequest *, ssize_t, bool);
318 void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
319                           enum netfs_sreq_ref_trace what);
320 void netfs_put_subrequest(struct netfs_io_subrequest *subreq,
321                           bool was_async, enum netfs_sreq_ref_trace what);
322 ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len,
323                                 struct iov_iter *new,
324                                 iov_iter_extraction_t extraction_flags);
325 size_t netfs_limit_iter(const struct iov_iter *iter, size_t start_offset,
326                         size_t max_size, size_t max_segs);
327
328 int netfs_start_io_read(struct inode *inode);
329 void netfs_end_io_read(struct inode *inode);
330 int netfs_start_io_write(struct inode *inode);
331 void netfs_end_io_write(struct inode *inode);
332 int netfs_start_io_direct(struct inode *inode);
333 void netfs_end_io_direct(struct inode *inode);
334
335 /**
336  * netfs_inode - Get the netfs inode context from the inode
337  * @inode: The inode to query
338  *
339  * Get the netfs lib inode context from the network filesystem's inode.  The
340  * context struct is expected to directly follow on from the VFS inode struct.
341  */
342 static inline struct netfs_inode *netfs_inode(struct inode *inode)
343 {
344         return container_of(inode, struct netfs_inode, inode);
345 }
346
347 /**
348  * netfs_inode_init - Initialise a netfslib inode context
349  * @ctx: The netfs inode to initialise
350  * @ops: The netfs's operations list
351  *
352  * Initialise the netfs library context struct.  This is expected to follow on
353  * directly from the VFS inode struct.
354  */
355 static inline void netfs_inode_init(struct netfs_inode *ctx,
356                                     const struct netfs_request_ops *ops)
357 {
358         ctx->ops = ops;
359         ctx->remote_i_size = i_size_read(&ctx->inode);
360         ctx->flags = 0;
361 #if IS_ENABLED(CONFIG_FSCACHE)
362         ctx->cache = NULL;
363 #endif
364 }
365
366 /**
367  * netfs_resize_file - Note that a file got resized
368  * @ctx: The netfs inode being resized
369  * @new_i_size: The new file size
370  *
371  * Inform the netfs lib that a file got resized so that it can adjust its state.
372  */
373 static inline void netfs_resize_file(struct netfs_inode *ctx, loff_t new_i_size)
374 {
375         ctx->remote_i_size = new_i_size;
376 }
377
378 /**
379  * netfs_i_cookie - Get the cache cookie from the inode
380  * @ctx: The netfs inode to query
381  *
382  * Get the caching cookie (if enabled) from the network filesystem's inode.
383  */
384 static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx)
385 {
386 #if IS_ENABLED(CONFIG_FSCACHE)
387         return ctx->cache;
388 #else
389         return NULL;
390 #endif
391 }
392
393 #endif /* _LINUX_NETFS_H */