at commit c03c1a14

1 .. SPDX-License-Identifier: GPL-2.0
2
3 ===================================
4 Network Filesystem Services Library
5 ===================================
6
7 .. Contents:
8
9 - Overview.
10 - Requests and streams.
11 - Subrequests.
12 - Result collection and retry.
13 - Local caching.
14 - Content encryption (fscrypt).
15 - Per-inode context.
16 - Inode context helper functions.
17 - Inode locking.
18 - Inode writeback.
19 - High-level VFS API.
20 - Unlocked read/write iter.
21 - Pre-locked read/write iter.
22 - Monolithic files API.
23 - Memory-mapped I/O API.
24 - High-level VM API.
25 - Deprecated PG_private2 API.
26 - I/O request API.
27 - Request structure.
28 - Stream structure.
29 - Subrequest structure.
30 - Filesystem methods.
31 - Terminating a subrequest.
32 - Local cache API.
33 - API function reference.
34
35
36 Overview
37 ========
38
39 The network filesystem services library, netfslib, is a set of functions
40 designed to aid a network filesystem in implementing VM/VFS API operations. It
41 takes over the normal buffered read, readahead, write and writeback and also
42 handles unbuffered and direct I/O.
43
44 The library provides support for (re-)negotiation of I/O sizes and retrying
45 failed I/O as well as local caching and will, in the future, provide content
46 encryption.
47
48 It insulates the filesystem from VM interface changes as much as possible and
49 handles VM features such as large multipage folios. The filesystem basically
50 just has to provide a way to perform read and write RPC calls.
51
52 The way I/O is organised inside netfslib consists of a number of objects:
53
54 * A *request*. A request is used to track the progress of the I/O overall and
55 to hold on to resources. The collection of results is done at the request
56 level. The I/O within a request is divided into a number of parallel
57 streams of subrequests.
58
59 * A *stream*. A non-overlapping series of subrequests. The subrequests
60 within a stream do not have to be contiguous.
61
62 * A *subrequest*. This is the basic unit of I/O. It represents a single RPC
63 call or a single cache I/O operation. The library passes these to the
64 filesystem and the cache to perform.
65
66 Requests and Streams
67 --------------------
68
69 When actually performing I/O (as opposed to just copying into the pagecache),
70 netfslib will create one or more requests to track the progress of the I/O and
71 to hold resources.
72
73 A read operation will have a single stream and the subrequests within that
74 stream may be of mixed origins, for instance mixing RPC subrequests and cache
75 subrequests.
76
77 On the other hand, a write operation may have multiple streams, where each
78 stream targets a different destination. For instance, there may be one stream
79 writing to the local cache and one to the server. Currently, only two streams
80 are allowed, but this could be increased if parallel writes to multiple servers
81 is desired.
82
83 The subrequests within a write stream do not need to match alignment or size
84 with the subrequests in another write stream and netfslib performs the tiling
85 of subrequests in each stream over the source buffer independently. Further,
86 each stream may contain holes that don't correspond to holes in the other
87 stream.
88
89 In addition, the subrequests do not need to correspond to the boundaries of the
90 folios or vectors in the source/destination buffer. The library handles the
91 collection of results and the wrangling of folio flags and references.
92
93 Subrequests
94 -----------
95
96 Subrequests are at the heart of the interaction between netfslib and the
97 filesystem using it. Each subrequest is expected to correspond to a single
98 read or write RPC or cache operation. The library will stitch together the
99 results from a set of subrequests to provide a higher level operation.
100
101 Netfslib has two interactions with the filesystem or the cache when setting up
102 a subrequest. First, there's an optional preparatory step that allows the
103 filesystem to negotiate the limits on the subrequest, both in terms of maximum
104 number of bytes and maximum number of vectors (e.g. for RDMA). This may
105 involve negotiating with the server (e.g. cifs needing to acquire credits).
106
107 And, secondly, there's the issuing step in which the subrequest is handed off
108 to the filesystem to perform.
109
110 Note that these two steps are done slightly differently between read and write:
111
112 * For reads, the VM/VFS tells us how much is being requested up front, so the
113 library can preset maximum values that the cache and then the filesystem can
114 then reduce. The cache also gets consulted first on whether it wants to do
115 a read before the filesystem is consulted.
116
117 * For writeback, it is unknown how much there will be to write until the
118 pagecache is walked, so no limit is set by the library.
119
120 Once a subrequest is completed, the filesystem or cache informs the library of
121 the completion and then collection is invoked. Depending on whether the
122 request is synchronous or asynchronous, the collection of results will be done
123 in either the application thread or in a work queue.
124
125 Result Collection and Retry
126 ---------------------------
127
128 As subrequests complete, the results are collected and collated by the library
129 and folio unlocking is performed progressively (if appropriate). Once the
130 request is complete, async completion will be invoked (again, if appropriate).
131 It is possible for the filesystem to provide interim progress reports to the
132 library to cause folio unlocking to happen earlier if possible.
133
134 If any subrequests fail, netfslib can retry them. It will wait until all
135 subrequests are completed, offer the filesystem the opportunity to fiddle with
136 the resources/state held by the request and poke at the subrequests before
137 re-preparing and re-issuing the subrequests.
138
139 This allows the tiling of contiguous sets of failed subrequest within a stream
140 to be changed, adding more subrequests or ditching excess as necessary (for
141 instance, if the network sizes change or the server decides it wants smaller
142 chunks).
143
144 Further, if one or more contiguous cache-read subrequests fail, the library
145 will pass them to the filesystem to perform instead, renegotiating and retiling
146 them as necessary to fit with the filesystem's parameters rather than those of
147 the cache.
148
149 Local Caching
150 -------------
151
152 One of the services netfslib provides, via ``fscache``, is the option to cache
153 on local disk a copy of the data obtained from/written to a network filesystem.
154 The library will manage the storing, retrieval and some invalidation of data
155 automatically on behalf of the filesystem if a cookie is attached to the
156 ``netfs_inode``.
157
158 Note that local caching used to use the PG_private_2 (aliased as PG_fscache) to
159 keep track of a page that was being written to the cache, but this is now
160 deprecated as PG_private_2 will be removed.
161
162 Instead, folios that are read from the server for which there was no data in
163 the cache will be marked as dirty and will have ``folio->private`` set to a
164 special value (``NETFS_FOLIO_COPY_TO_CACHE``) and left to writeback to write.
165 If the folio is modified before that happened, the special value will be
166 cleared and the write will become normally dirty.
167
168 When writeback occurs, folios that are so marked will only be written to the
169 cache and not to the server. Writeback handles mixed cache-only writes and
170 server-and-cache writes by using two streams, sending one to the cache and one
171 to the server. The server stream will have gaps in it corresponding to those
172 folios.
173
174 Content Encryption (fscrypt)
175 ----------------------------
176
177 Though it does not do so yet, at some point netfslib will acquire the ability
178 to do client-side content encryption on behalf of the network filesystem (Ceph,
179 for example). fscrypt can be used for this if appropriate (it may not be -
180 cifs, for example).
181
182 The data will be stored encrypted in the local cache using the same manner of
183 encryption as the data written to the server and the library will impose bounce
184 buffering and RMW cycles as necessary.
185
186
187 Per-Inode Context
188 =================
189
190 The network filesystem helper library needs a place to store a bit of state for
191 its use on each netfs inode it is helping to manage. To this end, a context
192 structure is defined::
193
194 struct netfs_inode {
195 struct inode inode;
196 const struct netfs_request_ops *ops;
197 struct fscache_cookie * cache;
198 loff_t remote_i_size;
199 unsigned long flags;
200 ...
201 };
202
203 A network filesystem that wants to use netfslib must place one of these in its
204 inode wrapper struct instead of the VFS ``struct inode``. This can be done in
205 a way similar to the following::
206
207 struct my_inode {
208 struct netfs_inode netfs; /* Netfslib context and vfs inode */
209 ...
210 };
211
212 This allows netfslib to find its state by using ``container_of()`` from the
213 inode pointer, thereby allowing the netfslib helper functions to be pointed to
214 directly by the VFS/VM operation tables.
215
216 The structure contains the following fields that are of interest to the
217 filesystem:
218
219 * ``inode``
220
221 The VFS inode structure.
222
223 * ``ops``
224
225 The set of operations provided by the network filesystem to netfslib.
226
227 * ``cache``
228
229 Local caching cookie, or NULL if no caching is enabled. This field does not
230 exist if fscache is disabled.
231
232 * ``remote_i_size``
233
234 The size of the file on the server. This differs from inode->i_size if
235 local modifications have been made but not yet written back.
236
237 * ``flags``
238
239 A set of flags, some of which the filesystem might be interested in:
240
241 * ``NETFS_ICTX_MODIFIED_ATTR``
242
243 Set if netfslib modifies mtime/ctime. The filesystem is free to ignore
244 this or clear it.
245
246 * ``NETFS_ICTX_UNBUFFERED``
247
248 Do unbuffered I/O upon the file. Like direct I/O but without the
249 alignment limitations. RMW will be performed if necessary. The pagecache
250 will not be used unless mmap() is also used.
251
252 * ``NETFS_ICTX_WRITETHROUGH``
253
254 Do writethrough caching upon the file. I/O will be set up and dispatched
255 as buffered writes are made to the page cache. mmap() does the normal
256 writeback thing.
257
258 * ``NETFS_ICTX_SINGLE_NO_UPLOAD``
259
260 Set if the file has a monolithic content that must be read entirely in a
261 single go and must not be written back to the server, though it can be
262 cached (e.g. AFS directories).
263
264 Inode Context Helper Functions
265 ------------------------------
266
267 To help deal with the per-inode context, a number helper functions are
268 provided. Firstly, a function to perform basic initialisation on a context and
269 set the operations table pointer::
270
271 void netfs_inode_init(struct netfs_inode *ctx,
272 const struct netfs_request_ops *ops);
273
274 then a function to cast from the VFS inode structure to the netfs context::
275
276 struct netfs_inode *netfs_inode(struct inode *inode);
277
278 and finally, a function to get the cache cookie pointer from the context
279 attached to an inode (or NULL if fscache is disabled)::
280
281 struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx);
282
283 Inode Locking
284 -------------
285
286 A number of functions are provided to manage the locking of i_rwsem for I/O and
287 to effectively extend it to provide more separate classes of exclusion::
288
289 int netfs_start_io_read(struct inode *inode);
290 void netfs_end_io_read(struct inode *inode);
291 int netfs_start_io_write(struct inode *inode);
292 void netfs_end_io_write(struct inode *inode);
293 int netfs_start_io_direct(struct inode *inode);
294 void netfs_end_io_direct(struct inode *inode);
295
296 The exclusion breaks down into four separate classes:
297
298 1) Buffered reads and writes.
299
300 Buffered reads can run concurrently each other and with buffered writes,
301 but buffered writes cannot run concurrently with each other.
302
303 2) Direct reads and writes.
304
305 Direct (and unbuffered) reads and writes can run concurrently since they do
306 not share local buffering (i.e. the pagecache) and, in a network
307 filesystem, are expected to have exclusion managed on the server (though
308 this may not be the case for, say, Ceph).
309
310 3) Other major inode modifying operations (e.g. truncate, fallocate).
311
312 These should just access i_rwsem directly.
313
314 4) mmap().
315
316 mmap'd accesses might operate concurrently with any of the other classes.
317 They might form the buffer for an intra-file loopback DIO read/write. They
318 might be permitted on unbuffered files.
319
320 Inode Writeback
321 ---------------
322
323 Netfslib will pin resources on an inode for future writeback (such as pinning
324 use of an fscache cookie) when an inode is dirtied. However, this pinning
325 needs careful management. To manage the pinning, the following sequence
326 occurs:
327
328 1) An inode state flag ``I_PINNING_NETFS_WB`` is set by netfslib when the
329 pinning begins (when a folio is dirtied, for example) if the cache is
330 active to stop the cache structures from being discarded and the cache
331 space from being culled. This also prevents re-getting of cache resources
332 if the flag is already set.
333
334 2) This flag then cleared inside the inode lock during inode writeback in the
335 VM - and the fact that it was set is transferred to ``->unpinned_netfs_wb``
336 in ``struct writeback_control``.
337
338 3) If ``->unpinned_netfs_wb`` is now set, the write_inode procedure is forced.
339
340 4) The filesystem's ``->write_inode()`` function is invoked to do the cleanup.
341
342 5) The filesystem invokes netfs to do its cleanup.
343
344 To do the cleanup, netfslib provides a function to do the resource unpinning::
345
346 int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc);
347
348 If the filesystem doesn't need to do anything else, this may be set as a its
349 ``.write_inode`` method.
350
351 Further, if an inode is deleted, the filesystem's write_inode method may not
352 get called, so::
353
354 void netfs_clear_inode_writeback(struct inode *inode, const void *aux);
355
356 must be called from ``->evict_inode()`` *before* ``clear_inode()`` is called.
357
358
359 High-Level VFS API
360 ==================
361
362 Netfslib provides a number of sets of API calls for the filesystem to delegate
363 VFS operations to. Netfslib, in turn, will call out to the filesystem and the
364 cache to negotiate I/O sizes, issue RPCs and provide places for it to intervene
365 at various times.
366
367 Unlocked Read/Write Iter
368 ------------------------
369
370 The first API set is for the delegation of operations to netfslib when the
371 filesystem is called through the standard VFS read/write_iter methods::
372
373 ssize_t netfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
374 ssize_t netfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from);
375 ssize_t netfs_buffered_read_iter(struct kiocb *iocb, struct iov_iter *iter);
376 ssize_t netfs_unbuffered_read_iter(struct kiocb *iocb, struct iov_iter *iter);
377 ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from);
378
379 They can be assigned directly to ``.read_iter`` and ``.write_iter``. They
380 perform the inode locking themselves and the first two will switch between
381 buffered I/O and DIO as appropriate.
382
383 Pre-Locked Read/Write Iter
384 --------------------------
385
386 The second API set is for the delegation of operations to netfslib when the
387 filesystem is called through the standard VFS methods, but needs to do some
388 other stuff before or after calling netfslib whilst still inside locked section
389 (e.g. Ceph negotiating caps). The unbuffered read function is::
390
391 ssize_t netfs_unbuffered_read_iter_locked(struct kiocb *iocb, struct iov_iter *iter);
392
393 This must not be assigned directly to ``.read_iter`` and the filesystem is
394 responsible for performing the inode locking before calling it. In the case of
395 buffered read, the filesystem should use ``filemap_read()``.
396
397 There are three functions for writes::
398
399 ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *from,
400 struct netfs_group *netfs_group);
401 ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
402 struct netfs_group *netfs_group);
403 ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *iter,
404 struct netfs_group *netfs_group);
405
406 These must not be assigned directly to ``.write_iter`` and the filesystem is
407 responsible for performing the inode locking before calling them.
408
409 The first two functions are for buffered writes; the first just adds some
410 standard write checks and jumps to the second, but if the filesystem wants to
411 do the checks itself, it can use the second directly. The third function is
412 for unbuffered or DIO writes.
413
414 On all three write functions, there is a writeback group pointer (which should
415 be NULL if the filesystem doesn't use this). Writeback groups are set on
416 folios when they're modified. If a folio to-be-modified is already marked with
417 a different group, it is flushed first. The writeback API allows writing back
418 of a specific group.
419
420 Memory-Mapped I/O API
421 ---------------------
422
423 An API for support of mmap()'d I/O is provided::
424
425 vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_group);
426
427 This allows the filesystem to delegate ``.page_mkwrite`` to netfslib. The
428 filesystem should not take the inode lock before calling it, but, as with the
429 locked write functions above, this does take a writeback group pointer. If the
430 page to be made writable is in a different group, it will be flushed first.
431
432 Monolithic Files API
433 --------------------
434
435 There is also a special API set for files for which the content must be read in
436 a single RPC (and not written back) and is maintained as a monolithic blob
437 (e.g. an AFS directory), though it can be stored and updated in the local cache::
438
439 ssize_t netfs_read_single(struct inode *inode, struct file *file, struct iov_iter *iter);
440 void netfs_single_mark_inode_dirty(struct inode *inode);
441 int netfs_writeback_single(struct address_space *mapping,
442 struct writeback_control *wbc,
443 struct iov_iter *iter);
444
445 The first function reads from a file into the given buffer, reading from the
446 cache in preference if the data is cached there; the second function allows the
447 inode to be marked dirty, causing a later writeback; and the third function can
448 be called from the writeback code to write the data to the cache, if there is
449 one.
450
451 The inode should be marked ``NETFS_ICTX_SINGLE_NO_UPLOAD`` if this API is to be
452 used. The writeback function requires the buffer to be of ITER_FOLIOQ type.
453
454 Clearing Stale Post-EOF Pagecache
455 ---------------------------------
456
457 When a file is extended, data left in the pagecache past the old EOF by a write
458 through an mmap must not be exposed as file content. Netfslib clears this on
459 its own write paths, and exports a helper so a filesystem can do the same from
460 a resize path (truncate, setattr, fallocate and the like) that holds the
461 inode's ``i_rwsem`` exclusively across the whole resize::
462
463 void netfs_clear_stale_post_isize(struct inode *inode, uoff_t from,
464 uoff_t to);
465
466 This zeroes any such data within the ``[from, to)`` hole to be made, where
467 @from is the old EOF and @to is the new one, and the caller must have already
468 updated ``i_size`` to @to before calling it. Only the folio straddling @from
469 can hold data written past the EOF through an mmap, as pages wholly beyond the
470 EOF can't be faulted in, so the zeroing is limited to that folio. The folio is
471 zeroed rather than dropped so that a concurrent extending write can't lose
472 data.
473
474 This overlaps with ``pagecache_isize_extended()`` but can't reuse it: that
475 helper is keyed on a sub-page block size and is a no-op when the block size is
476 ``>= PAGE_SIZE`` (as on network filesystems), and it doesn't wait for
477 writeback. As both address the same problem, a change to one should probably
478 be reflected in the other to keep them in sync.
479
480 High-Level VM API
481 ==================
482
483 Netfslib also provides a number of sets of API calls for the filesystem to
484 delegate VM operations to. Again, netfslib, in turn, will call out to the
485 filesystem and the cache to negotiate I/O sizes, issue RPCs and provide places
486 for it to intervene at various times::
487
488 void netfs_readahead(struct readahead_control *);
489 int netfs_read_folio(struct file *, struct folio *);
490 int netfs_writepages(struct address_space *mapping,
491 struct writeback_control *wbc);
492 bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio);
493 void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
494 bool netfs_release_folio(struct folio *folio, gfp_t gfp);
495
496 These are ``address_space_operations`` methods and can be set directly in the
497 operations table.
498
499 Deprecated PG_private_2 API
500 ---------------------------
501
502 There is also a deprecated function for filesystems that still use the
503 ``->write_begin`` method::
504
505 int netfs_write_begin(struct netfs_inode *inode, struct file *file,
506 struct address_space *mapping, loff_t pos, unsigned int len,
507 struct folio **_folio, void **_fsdata);
508
509 It uses the deprecated PG_private_2 flag and so should not be used.
510
511
512 I/O Request API
513 ===============
514
515 The I/O request API comprises a number of structures and a number of functions
516 that the filesystem may need to use.
517
518 Request Structure
519 -----------------
520
521 The request structure manages the request as a whole, holding some resources
522 and state on behalf of the filesystem and tracking the collection of results::
523
524 struct netfs_io_request {
525 enum netfs_io_origin origin;
526 struct inode *inode;
527 struct address_space *mapping;
528 struct netfs_group *group;
529 struct netfs_io_stream io_streams[];
530 void *netfs_priv;
531 void *netfs_priv2;
532 unsigned long long start;
533 unsigned long long len;
534 unsigned long long i_size;
535 unsigned int debug_id;
536 unsigned long flags;
537 ...
538 };
539
540 Many of the fields are for internal use, but the fields shown here are of
541 interest to the filesystem:
542
543 * ``origin``
544
545 The origin of the request (readahead, read_folio, DIO read, writeback, ...).
546
547 * ``inode``
548 * ``mapping``
549
550 The inode and the address space of the file being read from. The mapping
551 may or may not point to inode->i_data.
552
553 * ``group``
554
555 The writeback group this request is dealing with or NULL. This holds a ref
556 on the group.
557
558 * ``io_streams``
559
560 The parallel streams of subrequests available to the request. Currently two
561 are available, but this may be made extensible in future. ``NR_IO_STREAMS``
562 indicates the size of the array.
563
564 * ``netfs_priv``
565 * ``netfs_priv2``
566
567 The network filesystem's private data. The value for this can be passed in
568 to the helper functions or set during the request.
569
570 * ``start``
571 * ``len``
572
573 The file position of the start of the read request and the length. These
574 may be altered by the ->expand_readahead() op.
575
576 * ``i_size``
577
578 The size of the file at the start of the request.
579
580 * ``debug_id``
581
582 A number allocated to this operation that can be displayed in trace lines
583 for reference.
584
585 * ``flags``
586
587 Flags for managing and controlling the operation of the request. Some of
588 these may be of interest to the filesystem:
589
590 * ``NETFS_RREQ_RETRYING``
591
592 Netfslib sets this when generating retries.
593
594 * ``NETFS_RREQ_PAUSE``
595
596 The filesystem can set this to request to pause the library's subrequest
597 issuing loop - but care needs to be taken as netfslib may also set it.
598
599 * ``NETFS_RREQ_NONBLOCK``
600 * ``NETFS_RREQ_BLOCKED``
601
602 Netfslib sets the first to indicate that non-blocking mode was set by the
603 caller and the filesystem can set the second to indicate that it would
604 have had to block.
605
606 * ``NETFS_RREQ_USE_PGPRIV2``
607
608 The filesystem can set this if it wants to use PG_private_2 to track
609 whether a folio is being written to the cache. This is deprecated as
610 PG_private_2 is going to go away.
611
612 If the filesystem wants more private data than is afforded by this structure,
613 then it should wrap it and provide its own allocator.
614
615 Stream Structure
616 ----------------
617
618 A request is comprised of one or more parallel streams and each stream may be
619 aimed at a different target.
620
621 For read requests, only stream 0 is used. This can contain a mixture of
622 subrequests aimed at different sources. For write requests, stream 0 is used
623 for the server and stream 1 is used for the cache. For buffered writeback,
624 stream 0 is not enabled unless a normal dirty folio is encountered, at which
625 point ->begin_writeback() will be invoked and the filesystem can mark the
626 stream available.
627
628 The stream struct looks like::
629
630 struct netfs_io_stream {
631 unsigned char stream_nr;
632 bool avail;
633 size_t sreq_max_len;
634 unsigned int sreq_max_segs;
635 unsigned int submit_extendable_to;
636 ...
637 };
638
639 A number of members are available for access/use by the filesystem:
640
641 * ``stream_nr``
642
643 The number of the stream within the request.
644
645 * ``avail``
646
647 True if the stream is available for use. The filesystem should set this on
648 stream zero if in ->begin_writeback().
649
650 * ``sreq_max_len``
651 * ``sreq_max_segs``
652
653 These are set by the filesystem or the cache in ->prepare_read() or
654 ->prepare_write() for each subrequest to indicate the maximum number of
655 bytes and, optionally, the maximum number of segments (if not 0) that that
656 subrequest can support.
657
658 * ``submit_extendable_to``
659
660 The size that a subrequest can be rounded up to beyond the EOF, given the
661 available buffer. This allows the cache to work out if it can do a DIO read
662 or write that straddles the EOF marker.
663
664 Subrequest Structure
665 --------------------
666
667 Individual units of I/O are managed by the subrequest structure. These
668 represent slices of the overall request and run independently::
669
670 struct netfs_io_subrequest {
671 struct netfs_io_request *rreq;
672 struct iov_iter io_iter;
673 unsigned long long start;
674 size_t len;
675 size_t transferred;
676 unsigned long flags;
677 short error;
678 unsigned short debug_index;
679 unsigned char stream_nr;
680 ...
681 };
682
683 Each subrequest is expected to access a single source, though the library will
684 handle falling back from one source type to another. The members are:
685
686 * ``rreq``
687
688 A pointer to the read request.
689
690 * ``io_iter``
691
692 An I/O iterator representing a slice of the buffer to be read into or
693 written from.
694
695 * ``start``
696 * ``len``
697
698 The file position of the start of this slice of the read request and the
699 length.
700
701 * ``transferred``
702
703 The amount of data transferred so far for this subrequest. This should be
704 added to with the length of the transfer made by this issuance of the
705 subrequest. If this is less than ``len`` then the subrequest may be
706 reissued to continue.
707
708 * ``flags``
709
710 Flags for managing the subrequest. There are a number of interest to the
711 filesystem or cache:
712
713 * ``NETFS_SREQ_MADE_PROGRESS``
714
715 Set by the filesystem to indicates that at least one byte of data was read
716 or written.
717
718 * ``NETFS_SREQ_HIT_EOF``
719
720 The filesystem should set this if a read hit the EOF on the file (in which
721 case ``transferred`` should stop at the EOF). Netfslib may expand the
722 subrequest out to the size of the folio containing the EOF on the off
723 chance that a third party change happened or a DIO read may have asked for
724 more than is available. The library will clear any excess pagecache.
725
726 * ``NETFS_SREQ_CLEAR_TAIL``
727
728 The filesystem can set this to indicate that the remainder of the slice,
729 from transferred to len, should be cleared. Do not set if HIT_EOF is set.
730
731 * ``NETFS_SREQ_NEED_RETRY``
732
733 The filesystem can set this to tell netfslib to retry the subrequest.
734
735 * ``NETFS_SREQ_BOUNDARY``
736
737 This can be set by the filesystem on a subrequest to indicate that it ends
738 at a boundary with the filesystem structure (e.g. at the end of a Ceph
739 object). It tells netfslib not to retile subrequests across it.
740
741 * ``error``
742
743 This is for the filesystem to store result of the subrequest. It should be
744 set to 0 if successful and a negative error code otherwise.
745
746 * ``debug_index``
747 * ``stream_nr``
748
749 A number allocated to this slice that can be displayed in trace lines for
750 reference and the number of the request stream that it belongs to.
751
752 If necessary, the filesystem can get and put extra refs on the subrequest it is
753 given::
754
755 void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
756 enum netfs_sreq_ref_trace what);
757 void netfs_put_subrequest(struct netfs_io_subrequest *subreq,
758 enum netfs_sreq_ref_trace what);
759
760 using netfs trace codes to indicate the reason. Care must be taken, however,
761 as once control of the subrequest is returned to netfslib, the same subrequest
762 can be reissued/retried.
763
764 Filesystem Methods
765 ------------------
766
767 The filesystem sets a table of operations in ``netfs_inode`` for netfslib to
768 use::
769
770 struct netfs_request_ops {
771 mempool_t *request_pool;
772 mempool_t *subrequest_pool;
773 int (*init_request)(struct netfs_io_request *rreq, struct file *file);
774 void (*free_request)(struct netfs_io_request *rreq);
775 void (*free_subrequest)(struct netfs_io_subrequest *rreq);
776 void (*expand_readahead)(struct netfs_io_request *rreq);
777 int (*prepare_read)(struct netfs_io_subrequest *subreq);
778 void (*issue_read)(struct netfs_io_subrequest *subreq);
779 void (*done)(struct netfs_io_request *rreq);
780 void (*update_i_size)(struct inode *inode, loff_t i_size);
781 void (*post_modify)(struct inode *inode);
782 void (*begin_writeback)(struct netfs_io_request *wreq);
783 void (*prepare_write)(struct netfs_io_subrequest *subreq);
784 void (*issue_write)(struct netfs_io_subrequest *subreq);
785 void (*retry_request)(struct netfs_io_request *wreq,
786 struct netfs_io_stream *stream);
787 void (*invalidate_cache)(struct netfs_io_request *wreq);
788 };
789
790 The table starts with a pair of optional pointers to memory pools from which
791 requests and subrequests can be allocated. If these are not given, netfslib
792 has default pools that it will use instead. If the filesystem wraps the netfs
793 structs in its own larger structs, then it will need to use its own pools.
794 Netfslib will allocate directly from the pools.
795
796 The methods defined in the table are:
797
798 * ``init_request()``
799 * ``free_request()``
800 * ``free_subrequest()``
801
802 [Optional] A filesystem may implement these to initialise or clean up any
803 resources that it attaches to the request or subrequest.
804
805 * ``expand_readahead()``
806
807 [Optional] This is called to allow the filesystem to expand the size of a
808 readahead request. The filesystem gets to expand the request in both
809 directions, though it must retain the initial region as that may represent
810 an allocation already made. If local caching is enabled, it gets to expand
811 the request first.
812
813 Expansion is communicated by changing ->start and ->len in the request
814 structure. Note that if any change is made, ->len must be increased by at
815 least as much as ->start is reduced.
816
817 * ``prepare_read()``
818
819 [Optional] This is called to allow the filesystem to limit the size of a
820 subrequest. It may also limit the number of individual regions in iterator,
821 such as required by RDMA. This information should be set on stream zero in::
822
823 rreq->io_streams[0].sreq_max_len
824 rreq->io_streams[0].sreq_max_segs
825
826 The filesystem can use this, for example, to chop up a request that has to
827 be split across multiple servers or to put multiple reads in flight.
828
829 Zero should be returned on success and an error code otherwise.
830
831 * ``issue_read()``
832
833 [Required] Netfslib calls this to dispatch a subrequest to the server for
834 reading. In the subrequest, ->start, ->len and ->transferred indicate what
835 data should be read from the server and ->io_iter indicates the buffer to be
836 used.
837
838 There is no return value; the ``netfs_read_subreq_terminated()`` function
839 should be called to indicate that the subrequest completed either way.
840 ->error, ->transferred and ->flags should be updated before completing. The
841 termination can be done asynchronously.
842
843 Note: the filesystem must not deal with setting folios uptodate, unlocking
844 them or dropping their refs - the library deals with this as it may have to
845 stitch together the results of multiple subrequests that variously overlap
846 the set of folios.
847
848 * ``done()``
849
850 [Optional] This is called after the folios in a read request have all been
851 unlocked (and marked uptodate if applicable).
852
853 * ``update_i_size()``
854
855 [Optional] This is invoked by netfslib at various points during the write
856 paths to ask the filesystem to update its idea of the file size. If not
857 given, netfslib will set i_size and i_blocks and update the local cache
858 cookie.
859
860 * ``post_modify()``
861
862 [Optional] This is called after netfslib writes to the pagecache or when it
863 allows an mmap'd page to be marked as writable.
864
865 * ``begin_writeback()``
866
867 [Optional] Netfslib calls this when processing a writeback request if it
868 finds a dirty page that isn't simply marked NETFS_FOLIO_COPY_TO_CACHE,
869 indicating it must be written to the server. This allows the filesystem to
870 only set up writeback resources when it knows it's going to have to perform
871 a write.
872
873 * ``prepare_write()``
874
875 [Optional] This is called to allow the filesystem to limit the size of a
876 subrequest. It may also limit the number of individual regions in iterator,
877 such as required by RDMA. This information should be set on stream to which
878 the subrequest belongs::
879
880 rreq->io_streams[subreq->stream_nr].sreq_max_len
881 rreq->io_streams[subreq->stream_nr].sreq_max_segs
882
883 The filesystem can use this, for example, to chop up a request that has to
884 be split across multiple servers or to put multiple writes in flight.
885
886 This is not permitted to return an error. Instead, in the event of failure,
887 ``netfs_prepare_write_failed()`` must be called.
888
889 * ``issue_write()``
890
891 [Required] This is used to dispatch a subrequest to the server for writing.
892 In the subrequest, ->start, ->len and ->transferred indicate what data
893 should be written to the server and ->io_iter indicates the buffer to be
894 used.
895
896 There is no return value; the ``netfs_write_subreq_terminated()`` function
897 should be called to indicate that the subrequest completed either way.
898 ->error, ->transferred and ->flags should be updated before completing. The
899 termination can be done asynchronously.
900
901 Note: the filesystem must not deal with removing the dirty or writeback
902 marks on folios involved in the operation and should not take refs or pins
903 on them, but should leave retention to netfslib.
904
905 * ``retry_request()``
906
907 [Optional] Netfslib calls this at the beginning of a retry cycle. This
908 allows the filesystem to examine the state of the request, the subrequests
909 in the indicated stream and of its own data and make adjustments or
910 renegotiate resources.
911
912 * ``invalidate_cache()``
913
914 [Optional] This is called by netfslib to invalidate data stored in the local
915 cache in the event that writing to the local cache fails, providing updated
916 coherency data that netfs can't provide.
917
918 Terminating a subrequest
919 ------------------------
920
921 When a subrequest completes, there are a number of functions that the cache or
922 subrequest can call to inform netfslib of the status change. One function is
923 provided to terminate a write subrequest at the preparation stage and acts
924 synchronously:
925
926 * ``void netfs_prepare_write_failed(struct netfs_io_subrequest *subreq);``
927
928 Indicate that the ->prepare_write() call failed. The ``error`` field should
929 have been updated.
930
931 Note that ->prepare_read() can return an error as a read can simply be aborted.
932 Dealing with writeback failure is trickier.
933
934 The other functions are used for subrequests that got as far as being issued:
935
936 * ``void netfs_read_subreq_terminated(struct netfs_io_subrequest *subreq);``
937
938 Tell netfslib that a read subrequest has terminated. The ``error``,
939 ``flags`` and ``transferred`` fields should have been updated.
940
941 * ``void netfs_write_subrequest_terminated(void *_op, ssize_t transferred_or_error);``
942
943 Tell netfslib that a write subrequest has terminated. Either the amount of
944 data processed or the negative error code can be passed in. This is
945 can be used as a kiocb completion function.
946
947 * ``void netfs_read_subreq_progress(struct netfs_io_subrequest *subreq);``
948
949 This is provided to optionally update netfslib on the incremental progress
950 of a read, allowing some folios to be unlocked early and does not actually
951 terminate the subrequest. The ``transferred`` field should have been
952 updated.
953
954 Local Cache API
955 ---------------
956
957 Netfslib provides a separate API for a local cache to implement, though it
958 provides some somewhat similar routines to the filesystem request API.
959
960 Firstly, the netfs_io_request object contains a place for the cache to hang its
961 state::
962
963 struct netfs_cache_resources {
964 const struct netfs_cache_ops *ops;
965 void *cache_priv;
966 void *cache_priv2;
967 unsigned int debug_id;
968 unsigned int inval_counter;
969 };
970
971 This contains an operations table pointer and two private pointers plus the
972 debug ID of the fscache cookie for tracing purposes and an invalidation counter
973 that is cranked by calls to ``fscache_invalidate()`` allowing cache subrequests
974 to be invalidated after completion.
975
976 The cache operation table looks like the following::
977
978 struct netfs_cache_ops {
979 void (*end_operation)(struct netfs_cache_resources *cres);
980 void (*expand_readahead)(struct netfs_cache_resources *cres,
981 loff_t *_start, size_t *_len, loff_t i_size);
982 enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq,
983 loff_t i_size);
984 int (*read)(struct netfs_cache_resources *cres,
985 loff_t start_pos,
986 struct iov_iter *iter,
987 bool seek_data,
988 netfs_io_terminated_t term_func,
989 void *term_func_priv);
990 void (*prepare_write_subreq)(struct netfs_io_subrequest *subreq);
991 void (*issue_write)(struct netfs_io_subrequest *subreq);
992 };
993
994 With a termination handler function pointer::
995
996 typedef void (*netfs_io_terminated_t)(void *priv,
997 ssize_t transferred_or_error,
998 bool was_async);
999
1000 The methods defined in the table are:
1001
1002 * ``end_operation()``
1003
1004 [Required] Called to clean up the resources at the end of the read request.
1005
1006 * ``expand_readahead()``
1007
1008 [Optional] Called at the beginning of a readahead operation to allow the
1009 cache to expand a request in either direction. This allows the cache to
1010 size the request appropriately for the cache granularity.
1011
1012 * ``prepare_read()``
1013
1014 [Required] Called to configure the next slice of a request. ->start and
1015 ->len in the subrequest indicate where and how big the next slice can be;
1016 the cache gets to reduce the length to match its granularity requirements.
1017
1018 The function is passed pointers to the start and length in its parameters,
1019 plus the size of the file for reference, and adjusts the start and length
1020 appropriately. It should return one of:
1021
1022 * ``NETFS_FILL_WITH_ZEROES``
1023 * ``NETFS_DOWNLOAD_FROM_SERVER``
1024 * ``NETFS_READ_FROM_CACHE``
1025 * ``NETFS_INVALID_READ``
1026
1027 to indicate whether the slice should just be cleared or whether it should be
1028 downloaded from the server or read from the cache - or whether slicing
1029 should be given up at the current point.
1030
1031 * ``read()``
1032
1033 [Required] Called to read from the cache. The start file offset is given
1034 along with an iterator to read to, which gives the length also. It can be
1035 given a hint requesting that it seek forward from that start position for
1036 data.
1037
1038 Also provided is a pointer to a termination handler function and private
1039 data to pass to that function. The termination function should be called
1040 with the number of bytes transferred or an error code, plus a flag
1041 indicating whether the termination is definitely happening in the caller's
1042 context.
1043
1044 * ``prepare_write_subreq()``
1045
1046 [Required] This is called to allow the cache to limit the size of a
1047 subrequest. It may also limit the number of individual regions in iterator,
1048 such as required by DIO/DMA. This information should be set on stream to
1049 which the subrequest belongs::
1050
1051 rreq->io_streams[subreq->stream_nr].sreq_max_len
1052 rreq->io_streams[subreq->stream_nr].sreq_max_segs
1053
1054 The filesystem can use this, for example, to chop up a request that has to
1055 be split across multiple servers or to put multiple writes in flight.
1056
1057 This is not permitted to return an error. In the event of failure,
1058 ``netfs_prepare_write_failed()`` must be called.
1059
1060 * ``issue_write()``
1061
1062 [Required] This is used to dispatch a subrequest to the cache for writing.
1063 In the subrequest, ->start, ->len and ->transferred indicate what data
1064 should be written to the cache and ->io_iter indicates the buffer to be
1065 used.
1066
1067 There is no return value; the ``netfs_write_subreq_terminated()`` function
1068 should be called to indicate that the subrequest completed either way.
1069 ->error, ->transferred and ->flags should be updated before completing. The
1070 termination can be done asynchronously.
1071
1072
1073 API Function Reference
1074 ======================
1075
1076 .. kernel-doc:: include/linux/netfs.h
1077 .. kernel-doc:: fs/netfs/buffered_read.c