Commit | Line | Data |
---|---|---|
b533a83f DH |
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 | ||
3d3c9504 DH |
17 | #include <linux/workqueue.h> |
18 | #include <linux/fs.h> | |
b533a83f | 19 | #include <linux/pagemap.h> |
85dd2c8f | 20 | #include <linux/uio.h> |
b533a83f | 21 | |
6cd3d6fd DH |
22 | enum netfs_sreq_ref_trace; |
23 | ||
b533a83f DH |
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 | */ | |
6abbaa5b | 28 | #define folio_test_fscache(folio) folio_test_private_2(folio) |
b533a83f DH |
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 | ||
99bff93c | 35 | /** |
6abbaa5b MWO |
36 | * folio_start_fscache - Start an fscache write on a folio. |
37 | * @folio: The folio. | |
99bff93c | 38 | * |
6abbaa5b MWO |
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. | |
99bff93c | 41 | */ |
6abbaa5b | 42 | static inline void folio_start_fscache(struct folio *folio) |
99bff93c | 43 | { |
6abbaa5b MWO |
44 | VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio); |
45 | folio_get(folio); | |
46 | folio_set_private_2(folio); | |
99bff93c DH |
47 | } |
48 | ||
49 | /** | |
6abbaa5b MWO |
50 | * folio_end_fscache - End an fscache write on a folio. |
51 | * @folio: The folio. | |
99bff93c | 52 | * |
6abbaa5b MWO |
53 | * Call this function after the folio has been written to the local cache. |
54 | * This will wake any sleepers waiting on this folio. | |
99bff93c | 55 | */ |
6abbaa5b | 56 | static inline void folio_end_fscache(struct folio *folio) |
99bff93c | 57 | { |
6abbaa5b | 58 | folio_end_private_2(folio); |
99bff93c DH |
59 | } |
60 | ||
61 | /** | |
6abbaa5b MWO |
62 | * folio_wait_fscache - Wait for an fscache write on this folio to end. |
63 | * @folio: The folio. | |
99bff93c | 64 | * |
6abbaa5b MWO |
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. | |
99bff93c | 68 | */ |
6abbaa5b | 69 | static inline void folio_wait_fscache(struct folio *folio) |
99bff93c | 70 | { |
6abbaa5b | 71 | folio_wait_private_2(folio); |
99bff93c DH |
72 | } |
73 | ||
74 | /** | |
6abbaa5b MWO |
75 | * folio_wait_fscache_killable - Wait for an fscache write on this folio to end. |
76 | * @folio: The folio. | |
99bff93c | 77 | * |
6abbaa5b MWO |
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. | |
99bff93c DH |
82 | * |
83 | * Return: | |
84 | * - 0 if successful. | |
85 | * - -EINTR if a fatal signal was encountered. | |
86 | */ | |
6abbaa5b MWO |
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 | ||
99bff93c DH |
107 | static inline int wait_on_page_fscache_killable(struct page *page) |
108 | { | |
b47393f8 | 109 | return folio_wait_private_2_killable(page_folio(page)); |
99bff93c DH |
110 | } |
111 | ||
6a19114b | 112 | enum netfs_io_source { |
3d3c9504 DH |
113 | NETFS_FILL_WITH_ZEROES, |
114 | NETFS_DOWNLOAD_FROM_SERVER, | |
115 | NETFS_READ_FROM_CACHE, | |
116 | NETFS_INVALID_READ, | |
117 | } __mode(byte); | |
118 | ||
726218fd DH |
119 | typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error, |
120 | bool was_async); | |
121 | ||
bc899ee1 | 122 | /* |
874c8ca1 | 123 | * Per-inode context. This wraps the VFS inode. |
bc899ee1 | 124 | */ |
874c8ca1 DH |
125 | struct netfs_inode { |
126 | struct inode inode; /* The VFS inode */ | |
bc899ee1 DH |
127 | const struct netfs_request_ops *ops; |
128 | #if IS_ENABLED(CONFIG_FSCACHE) | |
129 | struct fscache_cookie *cache; | |
130 | #endif | |
4058f742 | 131 | loff_t remote_i_size; /* Size of the remote file */ |
bc899ee1 DH |
132 | }; |
133 | ||
726218fd DH |
134 | /* |
135 | * Resources required to do operations on a cache. | |
136 | */ | |
137 | struct netfs_cache_resources { | |
138 | const struct netfs_cache_ops *ops; | |
139 | void *cache_priv; | |
140 | void *cache_priv2; | |
a7e20e31 | 141 | unsigned int debug_id; /* Cookie debug ID */ |
d24af13e | 142 | unsigned int inval_counter; /* object->inval_counter at begin_op */ |
726218fd DH |
143 | }; |
144 | ||
3d3c9504 DH |
145 | /* |
146 | * Descriptor for a single component subrequest. | |
147 | */ | |
6a19114b | 148 | struct netfs_io_subrequest { |
f18a3785 | 149 | struct netfs_io_request *rreq; /* Supervising I/O request */ |
3d3c9504 DH |
150 | struct list_head rreq_link; /* Link in rreq->subrequests */ |
151 | loff_t start; /* Where to start the I/O */ | |
152 | size_t len; /* Size of the I/O */ | |
153 | size_t transferred; /* Amount of data transferred */ | |
6cd3d6fd | 154 | refcount_t ref; |
3d3c9504 DH |
155 | short error; /* 0 or error that occurred */ |
156 | unsigned short debug_index; /* Index in list (for debugging output) */ | |
f18a3785 | 157 | enum netfs_io_source source; /* Where to read from/write to */ |
3d3c9504 | 158 | unsigned long flags; |
f18a3785 | 159 | #define NETFS_SREQ_COPY_TO_CACHE 0 /* Set if should copy the data to the cache */ |
3d3c9504 | 160 | #define NETFS_SREQ_CLEAR_TAIL 1 /* Set if the rest of the read should be cleared */ |
f18a3785 | 161 | #define NETFS_SREQ_SHORT_IO 2 /* Set if the I/O was short */ |
3d3c9504 DH |
162 | #define NETFS_SREQ_SEEK_DATA_READ 3 /* Set if ->read() should SEEK_DATA first */ |
163 | #define NETFS_SREQ_NO_PROGRESS 4 /* Set if we didn't manage to read any data */ | |
9032b6e8 | 164 | #define NETFS_SREQ_ONDEMAND 5 /* Set if it's from on-demand read mode */ |
3d3c9504 DH |
165 | }; |
166 | ||
663dfb65 DH |
167 | enum netfs_io_origin { |
168 | NETFS_READAHEAD, /* This read was triggered by readahead */ | |
169 | NETFS_READPAGE, /* This read is a synchronous read */ | |
170 | NETFS_READ_FOR_WRITE, /* This read is to prepare a write */ | |
171 | } __mode(byte); | |
172 | ||
3d3c9504 | 173 | /* |
f18a3785 DH |
174 | * Descriptor for an I/O helper request. This is used to make multiple I/O |
175 | * operations to a variety of data stores and then stitch the result together. | |
3d3c9504 | 176 | */ |
6a19114b | 177 | struct netfs_io_request { |
3d3c9504 DH |
178 | struct work_struct work; |
179 | struct inode *inode; /* The file being accessed */ | |
180 | struct address_space *mapping; /* The mapping being accessed */ | |
726218fd | 181 | struct netfs_cache_resources cache_resources; |
f18a3785 | 182 | struct list_head subrequests; /* Contributory I/O operations */ |
3d3c9504 DH |
183 | void *netfs_priv; /* Private data for the netfs */ |
184 | unsigned int debug_id; | |
f18a3785 DH |
185 | atomic_t nr_outstanding; /* Number of ops in progress */ |
186 | atomic_t nr_copy_ops; /* Number of copy-to-cache ops in progress */ | |
3d3c9504 DH |
187 | size_t submitted; /* Amount submitted for I/O so far */ |
188 | size_t len; /* Length of the request */ | |
189 | short error; /* 0 or error that occurred */ | |
663dfb65 | 190 | enum netfs_io_origin origin; /* Origin of the request */ |
3d3c9504 DH |
191 | loff_t i_size; /* Size of the file */ |
192 | loff_t start; /* Start position */ | |
78525c74 | 193 | pgoff_t no_unlock_folio; /* Don't unlock this folio after read */ |
de74023b | 194 | refcount_t ref; |
3d3c9504 DH |
195 | unsigned long flags; |
196 | #define NETFS_RREQ_INCOMPLETE_IO 0 /* Some ioreqs terminated short or with error */ | |
f18a3785 | 197 | #define NETFS_RREQ_COPY_TO_CACHE 1 /* Need to write to the cache */ |
78525c74 DH |
198 | #define NETFS_RREQ_NO_UNLOCK_FOLIO 2 /* Don't unlock no_unlock_folio on completion */ |
199 | #define NETFS_RREQ_DONT_UNLOCK_FOLIOS 3 /* Don't unlock the folios on completion */ | |
3d3c9504 DH |
200 | #define NETFS_RREQ_FAILED 4 /* The request failed */ |
201 | #define NETFS_RREQ_IN_PROGRESS 5 /* Unlocked when the request completes */ | |
6a19114b | 202 | const struct netfs_request_ops *netfs_ops; |
3d3c9504 DH |
203 | }; |
204 | ||
205 | /* | |
206 | * Operations the network filesystem can/must provide to the helpers. | |
207 | */ | |
6a19114b | 208 | struct netfs_request_ops { |
2de16041 | 209 | int (*init_request)(struct netfs_io_request *rreq, struct file *file); |
40a81101 | 210 | void (*free_request)(struct netfs_io_request *rreq); |
6a19114b | 211 | int (*begin_cache_operation)(struct netfs_io_request *rreq); |
40a81101 | 212 | |
6a19114b DH |
213 | void (*expand_readahead)(struct netfs_io_request *rreq); |
214 | bool (*clamp_length)(struct netfs_io_subrequest *subreq); | |
f18a3785 | 215 | void (*issue_read)(struct netfs_io_subrequest *subreq); |
6a19114b | 216 | bool (*is_still_valid)(struct netfs_io_request *rreq); |
e1b1240c | 217 | int (*check_write_begin)(struct file *file, loff_t pos, unsigned len, |
fac47b43 | 218 | struct folio **foliop, void **_fsdata); |
6a19114b | 219 | void (*done)(struct netfs_io_request *rreq); |
3d3c9504 DH |
220 | }; |
221 | ||
3a11b3a8 DH |
222 | /* |
223 | * How to handle reading from a hole. | |
224 | */ | |
225 | enum netfs_read_from_hole { | |
226 | NETFS_READ_HOLE_IGNORE, | |
227 | NETFS_READ_HOLE_CLEAR, | |
228 | NETFS_READ_HOLE_FAIL, | |
229 | }; | |
230 | ||
726218fd DH |
231 | /* |
232 | * Table of operations for access to a cache. This is obtained by | |
233 | * rreq->ops->begin_cache_operation(). | |
234 | */ | |
235 | struct netfs_cache_ops { | |
236 | /* End an operation */ | |
237 | void (*end_operation)(struct netfs_cache_resources *cres); | |
238 | ||
239 | /* Read data from the cache */ | |
240 | int (*read)(struct netfs_cache_resources *cres, | |
241 | loff_t start_pos, | |
242 | struct iov_iter *iter, | |
3a11b3a8 | 243 | enum netfs_read_from_hole read_hole, |
726218fd DH |
244 | netfs_io_terminated_t term_func, |
245 | void *term_func_priv); | |
246 | ||
247 | /* Write data to the cache */ | |
248 | int (*write)(struct netfs_cache_resources *cres, | |
249 | loff_t start_pos, | |
250 | struct iov_iter *iter, | |
251 | netfs_io_terminated_t term_func, | |
252 | void *term_func_priv); | |
253 | ||
254 | /* Expand readahead request */ | |
255 | void (*expand_readahead)(struct netfs_cache_resources *cres, | |
256 | loff_t *_start, size_t *_len, loff_t i_size); | |
257 | ||
258 | /* Prepare a read operation, shortening it to a cached/uncached | |
259 | * boundary as appropriate. | |
260 | */ | |
6a19114b | 261 | enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq, |
874c8ca1 | 262 | loff_t i_size); |
726218fd DH |
263 | |
264 | /* Prepare a write operation, working out what part of the write we can | |
265 | * actually do. | |
266 | */ | |
267 | int (*prepare_write)(struct netfs_cache_resources *cres, | |
a39c41b8 DH |
268 | loff_t *_start, size_t *_len, loff_t i_size, |
269 | bool no_space_allocated_yet); | |
bee9f655 | 270 | |
86692475 JX |
271 | /* Prepare an on-demand read operation, shortening it to a cached/uncached |
272 | * boundary as appropriate. | |
273 | */ | |
274 | enum netfs_io_source (*prepare_ondemand_read)(struct netfs_cache_resources *cres, | |
275 | loff_t start, size_t *_len, | |
276 | loff_t i_size, | |
277 | unsigned long *_flags, ino_t ino); | |
278 | ||
bee9f655 DH |
279 | /* Query the occupancy of the cache in a region, returning where the |
280 | * next chunk of data starts and how long it is. | |
281 | */ | |
282 | int (*query_occupancy)(struct netfs_cache_resources *cres, | |
283 | loff_t start, size_t len, size_t granularity, | |
284 | loff_t *_data_start, size_t *_data_len); | |
726218fd DH |
285 | }; |
286 | ||
3d3c9504 | 287 | struct readahead_control; |
0e8e08cc | 288 | void netfs_readahead(struct readahead_control *); |
6c62371b | 289 | int netfs_read_folio(struct file *, struct folio *); |
0e8e08cc MWO |
290 | int netfs_write_begin(struct netfs_inode *, struct file *, |
291 | struct address_space *, loff_t pos, unsigned int len, | |
292 | struct folio **, void **fsdata); | |
293 | ||
294 | void netfs_subreq_terminated(struct netfs_io_subrequest *, ssize_t, bool); | |
295 | void netfs_get_subrequest(struct netfs_io_subrequest *subreq, | |
296 | enum netfs_sreq_ref_trace what); | |
297 | void netfs_put_subrequest(struct netfs_io_subrequest *subreq, | |
298 | bool was_async, enum netfs_sreq_ref_trace what); | |
299 | void netfs_stats_show(struct seq_file *); | |
85dd2c8f DH |
300 | ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len, |
301 | struct iov_iter *new, | |
302 | iov_iter_extraction_t extraction_flags); | |
01858469 DH |
303 | struct sg_table; |
304 | ssize_t netfs_extract_iter_to_sg(struct iov_iter *iter, size_t len, | |
305 | struct sg_table *sgtable, unsigned int sg_max, | |
306 | iov_iter_extraction_t extraction_flags); | |
3d3c9504 | 307 | |
bc899ee1 | 308 | /** |
874c8ca1 | 309 | * netfs_inode - Get the netfs inode context from the inode |
bc899ee1 DH |
310 | * @inode: The inode to query |
311 | * | |
312 | * Get the netfs lib inode context from the network filesystem's inode. The | |
313 | * context struct is expected to directly follow on from the VFS inode struct. | |
314 | */ | |
874c8ca1 | 315 | static inline struct netfs_inode *netfs_inode(struct inode *inode) |
bc899ee1 | 316 | { |
874c8ca1 | 317 | return container_of(inode, struct netfs_inode, inode); |
bc899ee1 DH |
318 | } |
319 | ||
320 | /** | |
874c8ca1 | 321 | * netfs_inode_init - Initialise a netfslib inode context |
018ab4fa | 322 | * @ctx: The netfs inode to initialise |
bc899ee1 DH |
323 | * @ops: The netfs's operations list |
324 | * | |
325 | * Initialise the netfs library context struct. This is expected to follow on | |
326 | * directly from the VFS inode struct. | |
327 | */ | |
e81fb419 | 328 | static inline void netfs_inode_init(struct netfs_inode *ctx, |
874c8ca1 | 329 | const struct netfs_request_ops *ops) |
bc899ee1 | 330 | { |
bc899ee1 | 331 | ctx->ops = ops; |
e81fb419 | 332 | ctx->remote_i_size = i_size_read(&ctx->inode); |
874c8ca1 DH |
333 | #if IS_ENABLED(CONFIG_FSCACHE) |
334 | ctx->cache = NULL; | |
335 | #endif | |
4058f742 DH |
336 | } |
337 | ||
338 | /** | |
339 | * netfs_resize_file - Note that a file got resized | |
e81fb419 | 340 | * @ctx: The netfs inode being resized |
4058f742 DH |
341 | * @new_i_size: The new file size |
342 | * | |
343 | * Inform the netfs lib that a file got resized so that it can adjust its state. | |
344 | */ | |
e81fb419 | 345 | static inline void netfs_resize_file(struct netfs_inode *ctx, loff_t new_i_size) |
4058f742 | 346 | { |
4058f742 | 347 | ctx->remote_i_size = new_i_size; |
bc899ee1 DH |
348 | } |
349 | ||
350 | /** | |
351 | * netfs_i_cookie - Get the cache cookie from the inode | |
e81fb419 | 352 | * @ctx: The netfs inode to query |
bc899ee1 DH |
353 | * |
354 | * Get the caching cookie (if enabled) from the network filesystem's inode. | |
355 | */ | |
e81fb419 | 356 | static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx) |
bc899ee1 DH |
357 | { |
358 | #if IS_ENABLED(CONFIG_FSCACHE) | |
bc899ee1 DH |
359 | return ctx->cache; |
360 | #else | |
361 | return NULL; | |
362 | #endif | |
363 | } | |
364 | ||
b533a83f | 365 | #endif /* _LINUX_NETFS_H */ |