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>
20 #include <linux/uio.h>
22 enum netfs_sreq_ref_trace;
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
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))
36 * folio_start_fscache - Start an fscache write on a folio.
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.
42 static inline void folio_start_fscache(struct folio *folio)
44 VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio);
46 folio_set_private_2(folio);
50 * folio_end_fscache - End an fscache write on a folio.
53 * Call this function after the folio has been written to the local cache.
54 * This will wake any sleepers waiting on this folio.
56 static inline void folio_end_fscache(struct folio *folio)
58 folio_end_private_2(folio);
62 * folio_wait_fscache - Wait for an fscache write on this folio to end.
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.
69 static inline void folio_wait_fscache(struct folio *folio)
71 folio_wait_private_2(folio);
75 * folio_wait_fscache_killable - Wait for an fscache write on this folio to end.
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.
85 * - -EINTR if a fatal signal was encountered.
87 static inline int folio_wait_fscache_killable(struct folio *folio)
89 return folio_wait_private_2_killable(folio);
92 static inline void set_page_fscache(struct page *page)
94 folio_start_fscache(page_folio(page));
97 static inline void end_page_fscache(struct page *page)
99 folio_end_private_2(page_folio(page));
102 static inline void wait_on_page_fscache(struct page *page)
104 folio_wait_private_2(page_folio(page));
107 static inline int wait_on_page_fscache_killable(struct page *page)
109 return folio_wait_private_2_killable(page_folio(page));
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 */
116 enum netfs_io_source {
117 NETFS_FILL_WITH_ZEROES,
118 NETFS_DOWNLOAD_FROM_SERVER,
119 NETFS_READ_FROM_CACHE,
121 NETFS_UPLOAD_TO_SERVER,
122 NETFS_WRITE_TO_CACHE,
126 typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error,
130 * Per-inode context. This wraps the VFS inode.
133 struct inode inode; /* The VFS inode */
134 const struct netfs_request_ops *ops;
135 #if IS_ENABLED(CONFIG_FSCACHE)
136 struct fscache_cookie *cache;
138 loff_t remote_i_size; /* Size of the remote file */
140 #define NETFS_ICTX_ODIRECT 0 /* The file has DIO in progress */
141 #define NETFS_ICTX_UNBUFFERED 1 /* I/O should not use the pagecache */
145 * A netfs group - for instance a ceph snap. This is marked on dirty pages and
146 * pages marked with a group must be flushed before they can be written under
147 * the domain of another group.
151 void (*free)(struct netfs_group *netfs_group);
155 * Information about a dirty page (attached only if necessary).
159 struct netfs_group *netfs_group; /* Filesystem's grouping marker (or NULL). */
160 unsigned int dirty_offset; /* Write-streaming dirty data offset */
161 unsigned int dirty_len; /* Write-streaming dirty data length */
163 #define NETFS_FOLIO_INFO 0x1UL /* OR'd with folio->private. */
165 static inline struct netfs_folio *netfs_folio_info(struct folio *folio)
167 void *priv = folio_get_private(folio);
169 if ((unsigned long)priv & NETFS_FOLIO_INFO)
170 return (struct netfs_folio *)((unsigned long)priv & ~NETFS_FOLIO_INFO);
174 static inline struct netfs_group *netfs_folio_group(struct folio *folio)
176 struct netfs_folio *finfo;
177 void *priv = folio_get_private(folio);
179 finfo = netfs_folio_info(folio);
181 return finfo->netfs_group;
186 * Resources required to do operations on a cache.
188 struct netfs_cache_resources {
189 const struct netfs_cache_ops *ops;
192 unsigned int debug_id; /* Cookie debug ID */
193 unsigned int inval_counter; /* object->inval_counter at begin_op */
197 * Descriptor for a single component subrequest. Each operation represents an
198 * individual read/write from/to a server, a cache, a journal, etc..
200 * The buffer iterator is persistent for the life of the subrequest struct and
201 * the pages it points to can be relied on to exist for the duration.
203 struct netfs_io_subrequest {
204 struct work_struct work;
205 struct netfs_io_request *rreq; /* Supervising I/O request */
206 struct list_head rreq_link; /* Link in rreq->subrequests */
207 struct iov_iter io_iter; /* Iterator for this subrequest */
208 loff_t start; /* Where to start the I/O */
209 size_t len; /* Size of the I/O */
210 size_t transferred; /* Amount of data transferred */
212 short error; /* 0 or error that occurred */
213 unsigned short debug_index; /* Index in list (for debugging output) */
214 unsigned int max_nr_segs; /* 0 or max number of segments in an iterator */
215 enum netfs_io_source source; /* Where to read from/write to */
217 #define NETFS_SREQ_COPY_TO_CACHE 0 /* Set if should copy the data to the cache */
218 #define NETFS_SREQ_CLEAR_TAIL 1 /* Set if the rest of the read should be cleared */
219 #define NETFS_SREQ_SHORT_IO 2 /* Set if the I/O was short */
220 #define NETFS_SREQ_SEEK_DATA_READ 3 /* Set if ->read() should SEEK_DATA first */
221 #define NETFS_SREQ_NO_PROGRESS 4 /* Set if we didn't manage to read any data */
222 #define NETFS_SREQ_ONDEMAND 5 /* Set if it's from on-demand read mode */
225 enum netfs_io_origin {
226 NETFS_READAHEAD, /* This read was triggered by readahead */
227 NETFS_READPAGE, /* This read is a synchronous read */
228 NETFS_READ_FOR_WRITE, /* This read is to prepare a write */
229 NETFS_WRITEBACK, /* This write was triggered by writepages */
230 NETFS_UNBUFFERED_WRITE, /* This is an unbuffered write */
231 NETFS_DIO_READ, /* This is a direct I/O read */
232 NETFS_DIO_WRITE, /* This is a direct I/O write */
237 * Descriptor for an I/O helper request. This is used to make multiple I/O
238 * operations to a variety of data stores and then stitch the result together.
240 struct netfs_io_request {
242 struct work_struct work;
245 struct inode *inode; /* The file being accessed */
246 struct address_space *mapping; /* The mapping being accessed */
247 struct kiocb *iocb; /* AIO completion vector */
248 struct netfs_cache_resources cache_resources;
249 struct list_head proc_link; /* Link in netfs_iorequests */
250 struct list_head subrequests; /* Contributory I/O operations */
251 struct iov_iter iter; /* Unencrypted-side iterator */
252 struct iov_iter io_iter; /* I/O (Encrypted-side) iterator */
253 void *netfs_priv; /* Private data for the netfs */
254 struct bio_vec *direct_bv; /* DIO buffer list (when handling iovec-iter) */
255 unsigned int direct_bv_count; /* Number of elements in direct_bv[] */
256 unsigned int debug_id;
257 unsigned int rsize; /* Maximum read size (0 for none) */
258 unsigned int wsize; /* Maximum write size (0 for none) */
259 unsigned int subreq_counter; /* Next subreq->debug_index */
260 atomic_t nr_outstanding; /* Number of ops in progress */
261 atomic_t nr_copy_ops; /* Number of copy-to-cache ops in progress */
262 size_t submitted; /* Amount submitted for I/O so far */
263 size_t len; /* Length of the request */
264 size_t upper_len; /* Length can be extended to here */
265 size_t transferred; /* Amount to be indicated as transferred */
266 short error; /* 0 or error that occurred */
267 enum netfs_io_origin origin; /* Origin of the request */
268 bool direct_bv_unpin; /* T if direct_bv[] must be unpinned */
269 loff_t i_size; /* Size of the file */
270 loff_t start; /* Start position */
271 pgoff_t no_unlock_folio; /* Don't unlock this folio after read */
274 #define NETFS_RREQ_INCOMPLETE_IO 0 /* Some ioreqs terminated short or with error */
275 #define NETFS_RREQ_COPY_TO_CACHE 1 /* Need to write to the cache */
276 #define NETFS_RREQ_NO_UNLOCK_FOLIO 2 /* Don't unlock no_unlock_folio on completion */
277 #define NETFS_RREQ_DONT_UNLOCK_FOLIOS 3 /* Don't unlock the folios on completion */
278 #define NETFS_RREQ_FAILED 4 /* The request failed */
279 #define NETFS_RREQ_IN_PROGRESS 5 /* Unlocked when the request completes */
280 #define NETFS_RREQ_WRITE_TO_CACHE 7 /* Need to write to the cache */
281 #define NETFS_RREQ_UPLOAD_TO_SERVER 8 /* Need to write to the server */
282 #define NETFS_RREQ_NONBLOCK 9 /* Don't block if possible (O_NONBLOCK) */
283 #define NETFS_RREQ_BLOCKED 10 /* We blocked */
284 const struct netfs_request_ops *netfs_ops;
285 void (*cleanup)(struct netfs_io_request *req);
289 * Operations the network filesystem can/must provide to the helpers.
291 struct netfs_request_ops {
292 unsigned int io_request_size; /* Alloc size for netfs_io_request struct */
293 unsigned int io_subrequest_size; /* Alloc size for netfs_io_subrequest struct */
294 int (*init_request)(struct netfs_io_request *rreq, struct file *file);
295 void (*free_request)(struct netfs_io_request *rreq);
296 void (*free_subrequest)(struct netfs_io_subrequest *rreq);
298 /* Read request handling */
299 void (*expand_readahead)(struct netfs_io_request *rreq);
300 bool (*clamp_length)(struct netfs_io_subrequest *subreq);
301 void (*issue_read)(struct netfs_io_subrequest *subreq);
302 bool (*is_still_valid)(struct netfs_io_request *rreq);
303 int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
304 struct folio **foliop, void **_fsdata);
305 void (*done)(struct netfs_io_request *rreq);
307 /* Modification handling */
308 void (*update_i_size)(struct inode *inode, loff_t i_size);
310 /* Write request handling */
311 void (*create_write_requests)(struct netfs_io_request *wreq,
312 loff_t start, size_t len);
313 void (*invalidate_cache)(struct netfs_io_request *wreq);
317 * How to handle reading from a hole.
319 enum netfs_read_from_hole {
320 NETFS_READ_HOLE_IGNORE,
321 NETFS_READ_HOLE_CLEAR,
322 NETFS_READ_HOLE_FAIL,
326 * Table of operations for access to a cache.
328 struct netfs_cache_ops {
329 /* End an operation */
330 void (*end_operation)(struct netfs_cache_resources *cres);
332 /* Read data from the cache */
333 int (*read)(struct netfs_cache_resources *cres,
335 struct iov_iter *iter,
336 enum netfs_read_from_hole read_hole,
337 netfs_io_terminated_t term_func,
338 void *term_func_priv);
340 /* Write data to the cache */
341 int (*write)(struct netfs_cache_resources *cres,
343 struct iov_iter *iter,
344 netfs_io_terminated_t term_func,
345 void *term_func_priv);
347 /* Expand readahead request */
348 void (*expand_readahead)(struct netfs_cache_resources *cres,
349 loff_t *_start, size_t *_len, loff_t i_size);
351 /* Prepare a read operation, shortening it to a cached/uncached
352 * boundary as appropriate.
354 enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq,
357 /* Prepare a write operation, working out what part of the write we can
360 int (*prepare_write)(struct netfs_cache_resources *cres,
361 loff_t *_start, size_t *_len, size_t upper_len,
362 loff_t i_size, bool no_space_allocated_yet);
364 /* Prepare an on-demand read operation, shortening it to a cached/uncached
365 * boundary as appropriate.
367 enum netfs_io_source (*prepare_ondemand_read)(struct netfs_cache_resources *cres,
368 loff_t start, size_t *_len,
370 unsigned long *_flags, ino_t ino);
372 /* Query the occupancy of the cache in a region, returning where the
373 * next chunk of data starts and how long it is.
375 int (*query_occupancy)(struct netfs_cache_resources *cres,
376 loff_t start, size_t len, size_t granularity,
377 loff_t *_data_start, size_t *_data_len);
380 /* High-level read API. */
381 ssize_t netfs_unbuffered_read_iter(struct kiocb *iocb, struct iov_iter *iter);
382 ssize_t netfs_buffered_read_iter(struct kiocb *iocb, struct iov_iter *iter);
383 ssize_t netfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
385 /* High-level write API */
386 ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
387 struct netfs_group *netfs_group);
388 ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *from,
389 struct netfs_group *netfs_group);
390 ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from);
391 ssize_t netfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from);
393 /* Address operations API */
394 struct readahead_control;
395 void netfs_readahead(struct readahead_control *);
396 int netfs_read_folio(struct file *, struct folio *);
397 int netfs_write_begin(struct netfs_inode *, struct file *,
398 struct address_space *, loff_t pos, unsigned int len,
399 struct folio **, void **fsdata);
400 bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio);
401 int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc);
402 void netfs_clear_inode_writeback(struct inode *inode, const void *aux);
403 void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
404 bool netfs_release_folio(struct folio *folio, gfp_t gfp);
406 /* VMA operations API. */
407 vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_group);
409 /* (Sub)request management API. */
410 void netfs_subreq_terminated(struct netfs_io_subrequest *, ssize_t, bool);
411 void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
412 enum netfs_sreq_ref_trace what);
413 void netfs_put_subrequest(struct netfs_io_subrequest *subreq,
414 bool was_async, enum netfs_sreq_ref_trace what);
415 ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len,
416 struct iov_iter *new,
417 iov_iter_extraction_t extraction_flags);
418 size_t netfs_limit_iter(const struct iov_iter *iter, size_t start_offset,
419 size_t max_size, size_t max_segs);
420 struct netfs_io_subrequest *netfs_create_write_request(
421 struct netfs_io_request *wreq, enum netfs_io_source dest,
422 loff_t start, size_t len, work_func_t worker);
423 void netfs_write_subrequest_terminated(void *_op, ssize_t transferred_or_error,
425 void netfs_queue_write_request(struct netfs_io_subrequest *subreq);
427 int netfs_start_io_read(struct inode *inode);
428 void netfs_end_io_read(struct inode *inode);
429 int netfs_start_io_write(struct inode *inode);
430 void netfs_end_io_write(struct inode *inode);
431 int netfs_start_io_direct(struct inode *inode);
432 void netfs_end_io_direct(struct inode *inode);
435 * netfs_inode - Get the netfs inode context from the inode
436 * @inode: The inode to query
438 * Get the netfs lib inode context from the network filesystem's inode. The
439 * context struct is expected to directly follow on from the VFS inode struct.
441 static inline struct netfs_inode *netfs_inode(struct inode *inode)
443 return container_of(inode, struct netfs_inode, inode);
447 * netfs_inode_init - Initialise a netfslib inode context
448 * @ctx: The netfs inode to initialise
449 * @ops: The netfs's operations list
451 * Initialise the netfs library context struct. This is expected to follow on
452 * directly from the VFS inode struct.
454 static inline void netfs_inode_init(struct netfs_inode *ctx,
455 const struct netfs_request_ops *ops)
458 ctx->remote_i_size = i_size_read(&ctx->inode);
460 #if IS_ENABLED(CONFIG_FSCACHE)
466 * netfs_resize_file - Note that a file got resized
467 * @ctx: The netfs inode being resized
468 * @new_i_size: The new file size
470 * Inform the netfs lib that a file got resized so that it can adjust its state.
472 static inline void netfs_resize_file(struct netfs_inode *ctx, loff_t new_i_size)
474 ctx->remote_i_size = new_i_size;
478 * netfs_i_cookie - Get the cache cookie from the inode
479 * @ctx: The netfs inode to query
481 * Get the caching cookie (if enabled) from the network filesystem's inode.
483 static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx)
485 #if IS_ENABLED(CONFIG_FSCACHE)
492 #endif /* _LINUX_NETFS_H */