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); |
40a81101 | 211 | |
6a19114b DH |
212 | void (*expand_readahead)(struct netfs_io_request *rreq); |
213 | bool (*clamp_length)(struct netfs_io_subrequest *subreq); | |
f18a3785 | 214 | void (*issue_read)(struct netfs_io_subrequest *subreq); |
6a19114b | 215 | bool (*is_still_valid)(struct netfs_io_request *rreq); |
e1b1240c | 216 | int (*check_write_begin)(struct file *file, loff_t pos, unsigned len, |
fac47b43 | 217 | struct folio **foliop, void **_fsdata); |
6a19114b | 218 | void (*done)(struct netfs_io_request *rreq); |
3d3c9504 DH |
219 | }; |
220 | ||
3a11b3a8 DH |
221 | /* |
222 | * How to handle reading from a hole. | |
223 | */ | |
224 | enum netfs_read_from_hole { | |
225 | NETFS_READ_HOLE_IGNORE, | |
226 | NETFS_READ_HOLE_CLEAR, | |
227 | NETFS_READ_HOLE_FAIL, | |
228 | }; | |
229 | ||
726218fd | 230 | /* |
4498a8ec | 231 | * Table of operations for access to a cache. |
726218fd DH |
232 | */ |
233 | struct netfs_cache_ops { | |
234 | /* End an operation */ | |
235 | void (*end_operation)(struct netfs_cache_resources *cres); | |
236 | ||
237 | /* Read data from the cache */ | |
238 | int (*read)(struct netfs_cache_resources *cres, | |
239 | loff_t start_pos, | |
240 | struct iov_iter *iter, | |
3a11b3a8 | 241 | enum netfs_read_from_hole read_hole, |
726218fd DH |
242 | netfs_io_terminated_t term_func, |
243 | void *term_func_priv); | |
244 | ||
245 | /* Write data to the cache */ | |
246 | int (*write)(struct netfs_cache_resources *cres, | |
247 | loff_t start_pos, | |
248 | struct iov_iter *iter, | |
249 | netfs_io_terminated_t term_func, | |
250 | void *term_func_priv); | |
251 | ||
252 | /* Expand readahead request */ | |
253 | void (*expand_readahead)(struct netfs_cache_resources *cres, | |
254 | loff_t *_start, size_t *_len, loff_t i_size); | |
255 | ||
256 | /* Prepare a read operation, shortening it to a cached/uncached | |
257 | * boundary as appropriate. | |
258 | */ | |
6a19114b | 259 | enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq, |
874c8ca1 | 260 | loff_t i_size); |
726218fd DH |
261 | |
262 | /* Prepare a write operation, working out what part of the write we can | |
263 | * actually do. | |
264 | */ | |
265 | int (*prepare_write)(struct netfs_cache_resources *cres, | |
a39c41b8 DH |
266 | loff_t *_start, size_t *_len, loff_t i_size, |
267 | bool no_space_allocated_yet); | |
bee9f655 | 268 | |
86692475 JX |
269 | /* Prepare an on-demand read operation, shortening it to a cached/uncached |
270 | * boundary as appropriate. | |
271 | */ | |
272 | enum netfs_io_source (*prepare_ondemand_read)(struct netfs_cache_resources *cres, | |
273 | loff_t start, size_t *_len, | |
274 | loff_t i_size, | |
275 | unsigned long *_flags, ino_t ino); | |
276 | ||
bee9f655 DH |
277 | /* Query the occupancy of the cache in a region, returning where the |
278 | * next chunk of data starts and how long it is. | |
279 | */ | |
280 | int (*query_occupancy)(struct netfs_cache_resources *cres, | |
281 | loff_t start, size_t len, size_t granularity, | |
282 | loff_t *_data_start, size_t *_data_len); | |
726218fd DH |
283 | }; |
284 | ||
3d3c9504 | 285 | struct readahead_control; |
0e8e08cc | 286 | void netfs_readahead(struct readahead_control *); |
6c62371b | 287 | int netfs_read_folio(struct file *, struct folio *); |
0e8e08cc MWO |
288 | int netfs_write_begin(struct netfs_inode *, struct file *, |
289 | struct address_space *, loff_t pos, unsigned int len, | |
290 | struct folio **, void **fsdata); | |
c9c4ff12 DH |
291 | bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio); |
292 | int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc); | |
293 | void netfs_clear_inode_writeback(struct inode *inode, const void *aux); | |
0e8e08cc MWO |
294 | |
295 | void netfs_subreq_terminated(struct netfs_io_subrequest *, ssize_t, bool); | |
296 | void netfs_get_subrequest(struct netfs_io_subrequest *subreq, | |
297 | enum netfs_sreq_ref_trace what); | |
298 | void netfs_put_subrequest(struct netfs_io_subrequest *subreq, | |
299 | bool was_async, enum netfs_sreq_ref_trace what); | |
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); | |
3d3c9504 | 303 | |
bc899ee1 | 304 | /** |
874c8ca1 | 305 | * netfs_inode - Get the netfs inode context from the inode |
bc899ee1 DH |
306 | * @inode: The inode to query |
307 | * | |
308 | * Get the netfs lib inode context from the network filesystem's inode. The | |
309 | * context struct is expected to directly follow on from the VFS inode struct. | |
310 | */ | |
874c8ca1 | 311 | static inline struct netfs_inode *netfs_inode(struct inode *inode) |
bc899ee1 | 312 | { |
874c8ca1 | 313 | return container_of(inode, struct netfs_inode, inode); |
bc899ee1 DH |
314 | } |
315 | ||
316 | /** | |
874c8ca1 | 317 | * netfs_inode_init - Initialise a netfslib inode context |
018ab4fa | 318 | * @ctx: The netfs inode to initialise |
bc899ee1 DH |
319 | * @ops: The netfs's operations list |
320 | * | |
321 | * Initialise the netfs library context struct. This is expected to follow on | |
322 | * directly from the VFS inode struct. | |
323 | */ | |
e81fb419 | 324 | static inline void netfs_inode_init(struct netfs_inode *ctx, |
874c8ca1 | 325 | const struct netfs_request_ops *ops) |
bc899ee1 | 326 | { |
bc899ee1 | 327 | ctx->ops = ops; |
e81fb419 | 328 | ctx->remote_i_size = i_size_read(&ctx->inode); |
874c8ca1 DH |
329 | #if IS_ENABLED(CONFIG_FSCACHE) |
330 | ctx->cache = NULL; | |
331 | #endif | |
4058f742 DH |
332 | } |
333 | ||
334 | /** | |
335 | * netfs_resize_file - Note that a file got resized | |
e81fb419 | 336 | * @ctx: The netfs inode being resized |
4058f742 DH |
337 | * @new_i_size: The new file size |
338 | * | |
339 | * Inform the netfs lib that a file got resized so that it can adjust its state. | |
340 | */ | |
e81fb419 | 341 | static inline void netfs_resize_file(struct netfs_inode *ctx, loff_t new_i_size) |
4058f742 | 342 | { |
4058f742 | 343 | ctx->remote_i_size = new_i_size; |
bc899ee1 DH |
344 | } |
345 | ||
346 | /** | |
347 | * netfs_i_cookie - Get the cache cookie from the inode | |
e81fb419 | 348 | * @ctx: The netfs inode to query |
bc899ee1 DH |
349 | * |
350 | * Get the caching cookie (if enabled) from the network filesystem's inode. | |
351 | */ | |
e81fb419 | 352 | static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx) |
bc899ee1 DH |
353 | { |
354 | #if IS_ENABLED(CONFIG_FSCACHE) | |
bc899ee1 DH |
355 | return ctx->cache; |
356 | #else | |
357 | return NULL; | |
358 | #endif | |
359 | } | |
360 | ||
b533a83f | 361 | #endif /* _LINUX_NETFS_H */ |