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 DH |
19 | #include <linux/pagemap.h> |
20 | ||
6cd3d6fd DH |
21 | enum netfs_sreq_ref_trace; |
22 | ||
b533a83f DH |
23 | /* |
24 | * Overload PG_private_2 to give us PG_fscache - this is used to indicate that | |
25 | * a page is currently backed by a local disk cache | |
26 | */ | |
6abbaa5b | 27 | #define folio_test_fscache(folio) folio_test_private_2(folio) |
b533a83f DH |
28 | #define PageFsCache(page) PagePrivate2((page)) |
29 | #define SetPageFsCache(page) SetPagePrivate2((page)) | |
30 | #define ClearPageFsCache(page) ClearPagePrivate2((page)) | |
31 | #define TestSetPageFsCache(page) TestSetPagePrivate2((page)) | |
32 | #define TestClearPageFsCache(page) TestClearPagePrivate2((page)) | |
33 | ||
99bff93c | 34 | /** |
6abbaa5b MWO |
35 | * folio_start_fscache - Start an fscache write on a folio. |
36 | * @folio: The folio. | |
99bff93c | 37 | * |
6abbaa5b MWO |
38 | * Call this function before writing a folio to a local cache. Starting a |
39 | * second write before the first one finishes is not allowed. | |
99bff93c | 40 | */ |
6abbaa5b | 41 | static inline void folio_start_fscache(struct folio *folio) |
99bff93c | 42 | { |
6abbaa5b MWO |
43 | VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio); |
44 | folio_get(folio); | |
45 | folio_set_private_2(folio); | |
99bff93c DH |
46 | } |
47 | ||
48 | /** | |
6abbaa5b MWO |
49 | * folio_end_fscache - End an fscache write on a folio. |
50 | * @folio: The folio. | |
99bff93c | 51 | * |
6abbaa5b MWO |
52 | * Call this function after the folio has been written to the local cache. |
53 | * This will wake any sleepers waiting on this folio. | |
99bff93c | 54 | */ |
6abbaa5b | 55 | static inline void folio_end_fscache(struct folio *folio) |
99bff93c | 56 | { |
6abbaa5b | 57 | folio_end_private_2(folio); |
99bff93c DH |
58 | } |
59 | ||
60 | /** | |
6abbaa5b MWO |
61 | * folio_wait_fscache - Wait for an fscache write on this folio to end. |
62 | * @folio: The folio. | |
99bff93c | 63 | * |
6abbaa5b MWO |
64 | * If this folio is currently being written to a local cache, wait for |
65 | * the write to finish. Another write may start after this one finishes, | |
66 | * unless the caller holds the folio lock. | |
99bff93c | 67 | */ |
6abbaa5b | 68 | static inline void folio_wait_fscache(struct folio *folio) |
99bff93c | 69 | { |
6abbaa5b | 70 | folio_wait_private_2(folio); |
99bff93c DH |
71 | } |
72 | ||
73 | /** | |
6abbaa5b MWO |
74 | * folio_wait_fscache_killable - Wait for an fscache write on this folio to end. |
75 | * @folio: The folio. | |
99bff93c | 76 | * |
6abbaa5b MWO |
77 | * If this folio is currently being written to a local cache, wait |
78 | * for the write to finish or for a fatal signal to be received. | |
79 | * Another write may start after this one finishes, unless the caller | |
80 | * holds the folio lock. | |
99bff93c DH |
81 | * |
82 | * Return: | |
83 | * - 0 if successful. | |
84 | * - -EINTR if a fatal signal was encountered. | |
85 | */ | |
6abbaa5b MWO |
86 | static inline int folio_wait_fscache_killable(struct folio *folio) |
87 | { | |
88 | return folio_wait_private_2_killable(folio); | |
89 | } | |
90 | ||
91 | static inline void set_page_fscache(struct page *page) | |
92 | { | |
93 | folio_start_fscache(page_folio(page)); | |
94 | } | |
95 | ||
96 | static inline void end_page_fscache(struct page *page) | |
97 | { | |
98 | folio_end_private_2(page_folio(page)); | |
99 | } | |
100 | ||
101 | static inline void wait_on_page_fscache(struct page *page) | |
102 | { | |
103 | folio_wait_private_2(page_folio(page)); | |
104 | } | |
105 | ||
99bff93c DH |
106 | static inline int wait_on_page_fscache_killable(struct page *page) |
107 | { | |
b47393f8 | 108 | return folio_wait_private_2_killable(page_folio(page)); |
99bff93c DH |
109 | } |
110 | ||
6a19114b | 111 | enum netfs_io_source { |
3d3c9504 DH |
112 | NETFS_FILL_WITH_ZEROES, |
113 | NETFS_DOWNLOAD_FROM_SERVER, | |
114 | NETFS_READ_FROM_CACHE, | |
115 | NETFS_INVALID_READ, | |
116 | } __mode(byte); | |
117 | ||
726218fd DH |
118 | typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error, |
119 | bool was_async); | |
120 | ||
bc899ee1 | 121 | /* |
874c8ca1 | 122 | * Per-inode context. This wraps the VFS inode. |
bc899ee1 | 123 | */ |
874c8ca1 DH |
124 | struct netfs_inode { |
125 | struct inode inode; /* The VFS inode */ | |
bc899ee1 DH |
126 | const struct netfs_request_ops *ops; |
127 | #if IS_ENABLED(CONFIG_FSCACHE) | |
128 | struct fscache_cookie *cache; | |
129 | #endif | |
4058f742 | 130 | loff_t remote_i_size; /* Size of the remote file */ |
bc899ee1 DH |
131 | }; |
132 | ||
726218fd DH |
133 | /* |
134 | * Resources required to do operations on a cache. | |
135 | */ | |
136 | struct netfs_cache_resources { | |
137 | const struct netfs_cache_ops *ops; | |
138 | void *cache_priv; | |
139 | void *cache_priv2; | |
a7e20e31 | 140 | unsigned int debug_id; /* Cookie debug ID */ |
d24af13e | 141 | unsigned int inval_counter; /* object->inval_counter at begin_op */ |
726218fd DH |
142 | }; |
143 | ||
3d3c9504 DH |
144 | /* |
145 | * Descriptor for a single component subrequest. | |
146 | */ | |
6a19114b | 147 | struct netfs_io_subrequest { |
f18a3785 | 148 | struct netfs_io_request *rreq; /* Supervising I/O request */ |
3d3c9504 DH |
149 | struct list_head rreq_link; /* Link in rreq->subrequests */ |
150 | loff_t start; /* Where to start the I/O */ | |
151 | size_t len; /* Size of the I/O */ | |
152 | size_t transferred; /* Amount of data transferred */ | |
6cd3d6fd | 153 | refcount_t ref; |
3d3c9504 DH |
154 | short error; /* 0 or error that occurred */ |
155 | unsigned short debug_index; /* Index in list (for debugging output) */ | |
f18a3785 | 156 | enum netfs_io_source source; /* Where to read from/write to */ |
3d3c9504 | 157 | unsigned long flags; |
f18a3785 | 158 | #define NETFS_SREQ_COPY_TO_CACHE 0 /* Set if should copy the data to the cache */ |
3d3c9504 | 159 | #define NETFS_SREQ_CLEAR_TAIL 1 /* Set if the rest of the read should be cleared */ |
f18a3785 | 160 | #define NETFS_SREQ_SHORT_IO 2 /* Set if the I/O was short */ |
3d3c9504 DH |
161 | #define NETFS_SREQ_SEEK_DATA_READ 3 /* Set if ->read() should SEEK_DATA first */ |
162 | #define NETFS_SREQ_NO_PROGRESS 4 /* Set if we didn't manage to read any data */ | |
9032b6e8 | 163 | #define NETFS_SREQ_ONDEMAND 5 /* Set if it's from on-demand read mode */ |
3d3c9504 DH |
164 | }; |
165 | ||
663dfb65 DH |
166 | enum netfs_io_origin { |
167 | NETFS_READAHEAD, /* This read was triggered by readahead */ | |
168 | NETFS_READPAGE, /* This read is a synchronous read */ | |
169 | NETFS_READ_FOR_WRITE, /* This read is to prepare a write */ | |
170 | } __mode(byte); | |
171 | ||
3d3c9504 | 172 | /* |
f18a3785 DH |
173 | * Descriptor for an I/O helper request. This is used to make multiple I/O |
174 | * operations to a variety of data stores and then stitch the result together. | |
3d3c9504 | 175 | */ |
6a19114b | 176 | struct netfs_io_request { |
3d3c9504 DH |
177 | struct work_struct work; |
178 | struct inode *inode; /* The file being accessed */ | |
179 | struct address_space *mapping; /* The mapping being accessed */ | |
726218fd | 180 | struct netfs_cache_resources cache_resources; |
f18a3785 | 181 | struct list_head subrequests; /* Contributory I/O operations */ |
3d3c9504 DH |
182 | void *netfs_priv; /* Private data for the netfs */ |
183 | unsigned int debug_id; | |
f18a3785 DH |
184 | atomic_t nr_outstanding; /* Number of ops in progress */ |
185 | atomic_t nr_copy_ops; /* Number of copy-to-cache ops in progress */ | |
3d3c9504 DH |
186 | size_t submitted; /* Amount submitted for I/O so far */ |
187 | size_t len; /* Length of the request */ | |
188 | short error; /* 0 or error that occurred */ | |
663dfb65 | 189 | enum netfs_io_origin origin; /* Origin of the request */ |
3d3c9504 DH |
190 | loff_t i_size; /* Size of the file */ |
191 | loff_t start; /* Start position */ | |
78525c74 | 192 | pgoff_t no_unlock_folio; /* Don't unlock this folio after read */ |
de74023b | 193 | refcount_t ref; |
3d3c9504 DH |
194 | unsigned long flags; |
195 | #define NETFS_RREQ_INCOMPLETE_IO 0 /* Some ioreqs terminated short or with error */ | |
f18a3785 | 196 | #define NETFS_RREQ_COPY_TO_CACHE 1 /* Need to write to the cache */ |
78525c74 DH |
197 | #define NETFS_RREQ_NO_UNLOCK_FOLIO 2 /* Don't unlock no_unlock_folio on completion */ |
198 | #define NETFS_RREQ_DONT_UNLOCK_FOLIOS 3 /* Don't unlock the folios on completion */ | |
3d3c9504 DH |
199 | #define NETFS_RREQ_FAILED 4 /* The request failed */ |
200 | #define NETFS_RREQ_IN_PROGRESS 5 /* Unlocked when the request completes */ | |
6a19114b | 201 | const struct netfs_request_ops *netfs_ops; |
3d3c9504 DH |
202 | }; |
203 | ||
204 | /* | |
205 | * Operations the network filesystem can/must provide to the helpers. | |
206 | */ | |
6a19114b | 207 | struct netfs_request_ops { |
2de16041 | 208 | int (*init_request)(struct netfs_io_request *rreq, struct file *file); |
6a19114b DH |
209 | int (*begin_cache_operation)(struct netfs_io_request *rreq); |
210 | void (*expand_readahead)(struct netfs_io_request *rreq); | |
211 | bool (*clamp_length)(struct netfs_io_subrequest *subreq); | |
f18a3785 | 212 | void (*issue_read)(struct netfs_io_subrequest *subreq); |
6a19114b | 213 | bool (*is_still_valid)(struct netfs_io_request *rreq); |
e1b1240c | 214 | int (*check_write_begin)(struct file *file, loff_t pos, unsigned len, |
78525c74 | 215 | struct folio *folio, void **_fsdata); |
6a19114b | 216 | void (*done)(struct netfs_io_request *rreq); |
3d3c9504 DH |
217 | void (*cleanup)(struct address_space *mapping, void *netfs_priv); |
218 | }; | |
219 | ||
3a11b3a8 DH |
220 | /* |
221 | * How to handle reading from a hole. | |
222 | */ | |
223 | enum netfs_read_from_hole { | |
224 | NETFS_READ_HOLE_IGNORE, | |
225 | NETFS_READ_HOLE_CLEAR, | |
226 | NETFS_READ_HOLE_FAIL, | |
227 | }; | |
228 | ||
726218fd DH |
229 | /* |
230 | * Table of operations for access to a cache. This is obtained by | |
231 | * rreq->ops->begin_cache_operation(). | |
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 DH |
268 | |
269 | /* Query the occupancy of the cache in a region, returning where the | |
270 | * next chunk of data starts and how long it is. | |
271 | */ | |
272 | int (*query_occupancy)(struct netfs_cache_resources *cres, | |
273 | loff_t start, size_t len, size_t granularity, | |
274 | loff_t *_data_start, size_t *_data_len); | |
726218fd DH |
275 | }; |
276 | ||
3d3c9504 | 277 | struct readahead_control; |
bc899ee1 | 278 | extern void netfs_readahead(struct readahead_control *); |
6c62371b | 279 | int netfs_read_folio(struct file *, struct folio *); |
e81fb419 LT |
280 | extern int netfs_write_begin(struct netfs_inode *, |
281 | struct file *, struct address_space *, | |
de2a9311 | 282 | loff_t, unsigned int, struct folio **, |
bc899ee1 | 283 | void **); |
3d3c9504 | 284 | |
6a19114b | 285 | extern void netfs_subreq_terminated(struct netfs_io_subrequest *, ssize_t, bool); |
6cd3d6fd DH |
286 | extern void netfs_get_subrequest(struct netfs_io_subrequest *subreq, |
287 | enum netfs_sreq_ref_trace what); | |
288 | extern void netfs_put_subrequest(struct netfs_io_subrequest *subreq, | |
289 | bool was_async, enum netfs_sreq_ref_trace what); | |
289af54c | 290 | extern void netfs_stats_show(struct seq_file *); |
3d3c9504 | 291 | |
bc899ee1 | 292 | /** |
874c8ca1 | 293 | * netfs_inode - Get the netfs inode context from the inode |
bc899ee1 DH |
294 | * @inode: The inode to query |
295 | * | |
296 | * Get the netfs lib inode context from the network filesystem's inode. The | |
297 | * context struct is expected to directly follow on from the VFS inode struct. | |
298 | */ | |
874c8ca1 | 299 | static inline struct netfs_inode *netfs_inode(struct inode *inode) |
bc899ee1 | 300 | { |
874c8ca1 | 301 | return container_of(inode, struct netfs_inode, inode); |
bc899ee1 DH |
302 | } |
303 | ||
304 | /** | |
874c8ca1 | 305 | * netfs_inode_init - Initialise a netfslib inode context |
e81fb419 | 306 | * @inode: The netfs inode to initialise |
bc899ee1 DH |
307 | * @ops: The netfs's operations list |
308 | * | |
309 | * Initialise the netfs library context struct. This is expected to follow on | |
310 | * directly from the VFS inode struct. | |
311 | */ | |
e81fb419 | 312 | static inline void netfs_inode_init(struct netfs_inode *ctx, |
874c8ca1 | 313 | const struct netfs_request_ops *ops) |
bc899ee1 | 314 | { |
bc899ee1 | 315 | ctx->ops = ops; |
e81fb419 | 316 | ctx->remote_i_size = i_size_read(&ctx->inode); |
874c8ca1 DH |
317 | #if IS_ENABLED(CONFIG_FSCACHE) |
318 | ctx->cache = NULL; | |
319 | #endif | |
4058f742 DH |
320 | } |
321 | ||
322 | /** | |
323 | * netfs_resize_file - Note that a file got resized | |
e81fb419 | 324 | * @ctx: The netfs inode being resized |
4058f742 DH |
325 | * @new_i_size: The new file size |
326 | * | |
327 | * Inform the netfs lib that a file got resized so that it can adjust its state. | |
328 | */ | |
e81fb419 | 329 | static inline void netfs_resize_file(struct netfs_inode *ctx, loff_t new_i_size) |
4058f742 | 330 | { |
4058f742 | 331 | ctx->remote_i_size = new_i_size; |
bc899ee1 DH |
332 | } |
333 | ||
334 | /** | |
335 | * netfs_i_cookie - Get the cache cookie from the inode | |
e81fb419 | 336 | * @ctx: The netfs inode to query |
bc899ee1 DH |
337 | * |
338 | * Get the caching cookie (if enabled) from the network filesystem's inode. | |
339 | */ | |
e81fb419 | 340 | static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx) |
bc899ee1 DH |
341 | { |
342 | #if IS_ENABLED(CONFIG_FSCACHE) | |
bc899ee1 DH |
343 | return ctx->cache; |
344 | #else | |
345 | return NULL; | |
346 | #endif | |
347 | } | |
348 | ||
b533a83f | 349 | #endif /* _LINUX_NETFS_H */ |