1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Network filesystem support services.
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
9 * Documentation/filesystems/netfs_library.rst
11 * for a description of the network filesystem interface declared here.
14 #ifndef _LINUX_NETFS_H
15 #define _LINUX_NETFS_H
17 #include <linux/workqueue.h>
19 #include <linux/pagemap.h>
22 * Overload PG_private_2 to give us PG_fscache - this is used to indicate that
23 * a page is currently backed by a local disk cache
25 #define folio_test_fscache(folio) folio_test_private_2(folio)
26 #define PageFsCache(page) PagePrivate2((page))
27 #define SetPageFsCache(page) SetPagePrivate2((page))
28 #define ClearPageFsCache(page) ClearPagePrivate2((page))
29 #define TestSetPageFsCache(page) TestSetPagePrivate2((page))
30 #define TestClearPageFsCache(page) TestClearPagePrivate2((page))
33 * folio_start_fscache - Start an fscache write on a folio.
36 * Call this function before writing a folio to a local cache. Starting a
37 * second write before the first one finishes is not allowed.
39 static inline void folio_start_fscache(struct folio *folio)
41 VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio);
43 folio_set_private_2(folio);
47 * folio_end_fscache - End an fscache write on a folio.
50 * Call this function after the folio has been written to the local cache.
51 * This will wake any sleepers waiting on this folio.
53 static inline void folio_end_fscache(struct folio *folio)
55 folio_end_private_2(folio);
59 * folio_wait_fscache - Wait for an fscache write on this folio to end.
62 * If this folio is currently being written to a local cache, wait for
63 * the write to finish. Another write may start after this one finishes,
64 * unless the caller holds the folio lock.
66 static inline void folio_wait_fscache(struct folio *folio)
68 folio_wait_private_2(folio);
72 * folio_wait_fscache_killable - Wait for an fscache write on this folio to end.
75 * If this folio is currently being written to a local cache, wait
76 * for the write to finish or for a fatal signal to be received.
77 * Another write may start after this one finishes, unless the caller
78 * holds the folio lock.
82 * - -EINTR if a fatal signal was encountered.
84 static inline int folio_wait_fscache_killable(struct folio *folio)
86 return folio_wait_private_2_killable(folio);
89 static inline void set_page_fscache(struct page *page)
91 folio_start_fscache(page_folio(page));
94 static inline void end_page_fscache(struct page *page)
96 folio_end_private_2(page_folio(page));
99 static inline void wait_on_page_fscache(struct page *page)
101 folio_wait_private_2(page_folio(page));
104 static inline int wait_on_page_fscache_killable(struct page *page)
106 return folio_wait_private_2_killable(page_folio(page));
109 enum netfs_read_source {
110 NETFS_FILL_WITH_ZEROES,
111 NETFS_DOWNLOAD_FROM_SERVER,
112 NETFS_READ_FROM_CACHE,
116 typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error,
120 * Resources required to do operations on a cache.
122 struct netfs_cache_resources {
123 const struct netfs_cache_ops *ops;
126 unsigned int debug_id; /* Cookie debug ID */
127 unsigned int inval_counter; /* object->inval_counter at begin_op */
131 * Descriptor for a single component subrequest.
133 struct netfs_read_subrequest {
134 struct netfs_read_request *rreq; /* Supervising read request */
135 struct list_head rreq_link; /* Link in rreq->subrequests */
136 loff_t start; /* Where to start the I/O */
137 size_t len; /* Size of the I/O */
138 size_t transferred; /* Amount of data transferred */
140 short error; /* 0 or error that occurred */
141 unsigned short debug_index; /* Index in list (for debugging output) */
142 enum netfs_read_source source; /* Where to read from */
144 #define NETFS_SREQ_WRITE_TO_CACHE 0 /* Set if should write to cache */
145 #define NETFS_SREQ_CLEAR_TAIL 1 /* Set if the rest of the read should be cleared */
146 #define NETFS_SREQ_SHORT_READ 2 /* Set if there was a short read from the cache */
147 #define NETFS_SREQ_SEEK_DATA_READ 3 /* Set if ->read() should SEEK_DATA first */
148 #define NETFS_SREQ_NO_PROGRESS 4 /* Set if we didn't manage to read any data */
152 * Descriptor for a read helper request. This is used to make multiple I/O
153 * requests on a variety of sources and then stitch the result together.
155 struct netfs_read_request {
156 struct work_struct work;
157 struct inode *inode; /* The file being accessed */
158 struct address_space *mapping; /* The mapping being accessed */
159 struct netfs_cache_resources cache_resources;
160 struct list_head subrequests; /* Requests to fetch I/O from disk or net */
161 void *netfs_priv; /* Private data for the netfs */
162 unsigned int debug_id;
163 atomic_t nr_rd_ops; /* Number of read ops in progress */
164 atomic_t nr_wr_ops; /* Number of write ops in progress */
165 size_t submitted; /* Amount submitted for I/O so far */
166 size_t len; /* Length of the request */
167 short error; /* 0 or error that occurred */
168 loff_t i_size; /* Size of the file */
169 loff_t start; /* Start position */
170 pgoff_t no_unlock_folio; /* Don't unlock this folio after read */
173 #define NETFS_RREQ_INCOMPLETE_IO 0 /* Some ioreqs terminated short or with error */
174 #define NETFS_RREQ_WRITE_TO_CACHE 1 /* Need to write to the cache */
175 #define NETFS_RREQ_NO_UNLOCK_FOLIO 2 /* Don't unlock no_unlock_folio on completion */
176 #define NETFS_RREQ_DONT_UNLOCK_FOLIOS 3 /* Don't unlock the folios on completion */
177 #define NETFS_RREQ_FAILED 4 /* The request failed */
178 #define NETFS_RREQ_IN_PROGRESS 5 /* Unlocked when the request completes */
179 const struct netfs_read_request_ops *netfs_ops;
183 * Operations the network filesystem can/must provide to the helpers.
185 struct netfs_read_request_ops {
186 bool (*is_cache_enabled)(struct inode *inode);
187 void (*init_rreq)(struct netfs_read_request *rreq, struct file *file);
188 int (*begin_cache_operation)(struct netfs_read_request *rreq);
189 void (*expand_readahead)(struct netfs_read_request *rreq);
190 bool (*clamp_length)(struct netfs_read_subrequest *subreq);
191 void (*issue_op)(struct netfs_read_subrequest *subreq);
192 bool (*is_still_valid)(struct netfs_read_request *rreq);
193 int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
194 struct folio *folio, void **_fsdata);
195 void (*done)(struct netfs_read_request *rreq);
196 void (*cleanup)(struct address_space *mapping, void *netfs_priv);
200 * How to handle reading from a hole.
202 enum netfs_read_from_hole {
203 NETFS_READ_HOLE_IGNORE,
204 NETFS_READ_HOLE_CLEAR,
205 NETFS_READ_HOLE_FAIL,
209 * Table of operations for access to a cache. This is obtained by
210 * rreq->ops->begin_cache_operation().
212 struct netfs_cache_ops {
213 /* End an operation */
214 void (*end_operation)(struct netfs_cache_resources *cres);
216 /* Read data from the cache */
217 int (*read)(struct netfs_cache_resources *cres,
219 struct iov_iter *iter,
220 enum netfs_read_from_hole read_hole,
221 netfs_io_terminated_t term_func,
222 void *term_func_priv);
224 /* Write data to the cache */
225 int (*write)(struct netfs_cache_resources *cres,
227 struct iov_iter *iter,
228 netfs_io_terminated_t term_func,
229 void *term_func_priv);
231 /* Expand readahead request */
232 void (*expand_readahead)(struct netfs_cache_resources *cres,
233 loff_t *_start, size_t *_len, loff_t i_size);
235 /* Prepare a read operation, shortening it to a cached/uncached
236 * boundary as appropriate.
238 enum netfs_read_source (*prepare_read)(struct netfs_read_subrequest *subreq,
241 /* Prepare a write operation, working out what part of the write we can
244 int (*prepare_write)(struct netfs_cache_resources *cres,
245 loff_t *_start, size_t *_len, loff_t i_size,
246 bool no_space_allocated_yet);
249 struct readahead_control;
250 extern void netfs_readahead(struct readahead_control *,
251 const struct netfs_read_request_ops *,
253 extern int netfs_readpage(struct file *,
255 const struct netfs_read_request_ops *,
257 extern int netfs_write_begin(struct file *, struct address_space *,
258 loff_t, unsigned int, unsigned int, struct folio **,
260 const struct netfs_read_request_ops *,
263 extern void netfs_subreq_terminated(struct netfs_read_subrequest *, ssize_t, bool);
264 extern void netfs_stats_show(struct seq_file *);
266 #endif /* _LINUX_NETFS_H */