1.. SPDX-License-Identifier: GPL-2.0 2 3=================================== 4Network 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 36Overview 37======== 38 39The network filesystem services library, netfslib, is a set of functions 40designed to aid a network filesystem in implementing VM/VFS API operations. It 41takes over the normal buffered read, readahead, write and writeback and also 42handles unbuffered and direct I/O. 43 44The library provides support for (re-)negotiation of I/O sizes and retrying 45failed I/O as well as local caching and will, in the future, provide content 46encryption. 47 48It insulates the filesystem from VM interface changes as much as possible and 49handles VM features such as large multipage folios. The filesystem basically 50just has to provide a way to perform read and write RPC calls. 51 52The 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 66Requests and Streams 67-------------------- 68 69When actually performing I/O (as opposed to just copying into the pagecache), 70netfslib will create one or more requests to track the progress of the I/O and 71to hold resources. 72 73A read operation will have a single stream and the subrequests within that 74stream may be of mixed origins, for instance mixing RPC subrequests and cache 75subrequests. 76 77On the other hand, a write operation may have multiple streams, where each 78stream targets a different destination. For instance, there may be one stream 79writing to the local cache and one to the server. Currently, only two streams 80are allowed, but this could be increased if parallel writes to multiple servers 81is desired. 82 83The subrequests within a write stream do not need to match alignment or size 84with the subrequests in another write stream and netfslib performs the tiling 85of subrequests in each stream over the source buffer independently. Further, 86each stream may contain holes that don't correspond to holes in the other 87stream. 88 89In addition, the subrequests do not need to correspond to the boundaries of the 90folios or vectors in the source/destination buffer. The library handles the 91collection of results and the wrangling of folio flags and references. 92 93Subrequests 94----------- 95 96Subrequests are at the heart of the interaction between netfslib and the 97filesystem using it. Each subrequest is expected to correspond to a single 98read or write RPC or cache operation. The library will stitch together the 99results from a set of subrequests to provide a higher level operation. 100 101Netfslib has two interactions with the filesystem or the cache when setting up 102a subrequest. First, there's an optional preparatory step that allows the 103filesystem to negotiate the limits on the subrequest, both in terms of maximum 104number of bytes and maximum number of vectors (e.g. for RDMA). This may 105involve negotiating with the server (e.g. cifs needing to acquire credits). 106 107And, secondly, there's the issuing step in which the subrequest is handed off 108to the filesystem to perform. 109 110Note 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 120Once a subrequest is completed, the filesystem or cache informs the library of 121the completion and then collection is invoked. Depending on whether the 122request is synchronous or asynchronous, the collection of results will be done 123in either the application thread or in a work queue. 124 125Result Collection and Retry 126--------------------------- 127 128As subrequests complete, the results are collected and collated by the library 129and folio unlocking is performed progressively (if appropriate). Once the 130request is complete, async completion will be invoked (again, if appropriate). 131It is possible for the filesystem to provide interim progress reports to the 132library to cause folio unlocking to happen earlier if possible. 133 134If any subrequests fail, netfslib can retry them. It will wait until all 135subrequests are completed, offer the filesystem the opportunity to fiddle with 136the resources/state held by the request and poke at the subrequests before 137re-preparing and re-issuing the subrequests. 138 139This allows the tiling of contiguous sets of failed subrequest within a stream 140to be changed, adding more subrequests or ditching excess as necessary (for 141instance, if the network sizes change or the server decides it wants smaller 142chunks). 143 144Further, if one or more contiguous cache-read subrequests fail, the library 145will pass them to the filesystem to perform instead, renegotiating and retiling 146them as necessary to fit with the filesystem's parameters rather than those of 147the cache. 148 149Local Caching 150------------- 151 152One of the services netfslib provides, via ``fscache``, is the option to cache 153on local disk a copy of the data obtained from/written to a network filesystem. 154The library will manage the storing, retrieval and some invalidation of data 155automatically on behalf of the filesystem if a cookie is attached to the 156``netfs_inode``. 157 158Note that local caching used to use the PG_private_2 (aliased as PG_fscache) to 159keep track of a page that was being written to the cache, but this is now 160deprecated as PG_private_2 will be removed. 161 162Instead, folios that are read from the server for which there was no data in 163the cache will be marked as dirty and will have ``folio->private`` set to a 164special value (``NETFS_FOLIO_COPY_TO_CACHE``) and left to writeback to write. 165If the folio is modified before that happened, the special value will be 166cleared and the write will become normally dirty. 167 168When writeback occurs, folios that are so marked will only be written to the 169cache and not to the server. Writeback handles mixed cache-only writes and 170server-and-cache writes by using two streams, sending one to the cache and one 171to the server. The server stream will have gaps in it corresponding to those 172folios. 173 174Content Encryption (fscrypt) 175---------------------------- 176 177Though it does not do so yet, at some point netfslib will acquire the ability 178to do client-side content encryption on behalf of the network filesystem (Ceph, 179for example). fscrypt can be used for this if appropriate (it may not be - 180cifs, for example). 181 182The data will be stored encrypted in the local cache using the same manner of 183encryption as the data written to the server and the library will impose bounce 184buffering and RMW cycles as necessary. 185 186 187Per-Inode Context 188================= 189 190The network filesystem helper library needs a place to store a bit of state for 191its use on each netfs inode it is helping to manage. To this end, a context 192structure 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 203A network filesystem that wants to use netfslib must place one of these in its 204inode wrapper struct instead of the VFS ``struct inode``. This can be done in 205a way similar to the following:: 206 207 struct my_inode { 208 struct netfs_inode netfs; /* Netfslib context and vfs inode */ 209 ... 210 }; 211 212This allows netfslib to find its state by using ``container_of()`` from the 213inode pointer, thereby allowing the netfslib helper functions to be pointed to 214directly by the VFS/VM operation tables. 215 216The structure contains the following fields that are of interest to the 217filesystem: 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 264Inode Context Helper Functions 265------------------------------ 266 267To help deal with the per-inode context, a number helper functions are 268provided. Firstly, a function to perform basic initialisation on a context and 269set the operations table pointer:: 270 271 void netfs_inode_init(struct netfs_inode *ctx, 272 const struct netfs_request_ops *ops); 273 274then a function to cast from the VFS inode structure to the netfs context:: 275 276 struct netfs_inode *netfs_inode(struct inode *inode); 277 278and finally, a function to get the cache cookie pointer from the context 279attached to an inode (or NULL if fscache is disabled):: 280 281 struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx); 282 283Inode Locking 284------------- 285 286A number of functions are provided to manage the locking of i_rwsem for I/O and 287to 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 296The 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 320Inode Writeback 321--------------- 322 323Netfslib will pin resources on an inode for future writeback (such as pinning 324use of an fscache cookie) when an inode is dirtied. However, this pinning 325needs careful management. To manage the pinning, the following sequence 326occurs: 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 344To 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 348If the filesystem doesn't need to do anything else, this may be set as a its 349``.write_inode`` method. 350 351Further, if an inode is deleted, the filesystem's write_inode method may not 352get called, so:: 353 354 void netfs_clear_inode_writeback(struct inode *inode, const void *aux); 355 356must be called from ``->evict_inode()`` *before* ``clear_inode()`` is called. 357 358 359High-Level VFS API 360================== 361 362Netfslib provides a number of sets of API calls for the filesystem to delegate 363VFS operations to. Netfslib, in turn, will call out to the filesystem and the 364cache to negotiate I/O sizes, issue RPCs and provide places for it to intervene 365at various times. 366 367Unlocked Read/Write Iter 368------------------------ 369 370The first API set is for the delegation of operations to netfslib when the 371filesystem 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 379They can be assigned directly to ``.read_iter`` and ``.write_iter``. They 380perform the inode locking themselves and the first two will switch between 381buffered I/O and DIO as appropriate. 382 383Pre-Locked Read/Write Iter 384-------------------------- 385 386The second API set is for the delegation of operations to netfslib when the 387filesystem is called through the standard VFS methods, but needs to do some 388other 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 393This must not be assigned directly to ``.read_iter`` and the filesystem is 394responsible for performing the inode locking before calling it. In the case of 395buffered read, the filesystem should use ``filemap_read()``. 396 397There 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 406These must not be assigned directly to ``.write_iter`` and the filesystem is 407responsible for performing the inode locking before calling them. 408 409The first two functions are for buffered writes; the first just adds some 410standard write checks and jumps to the second, but if the filesystem wants to 411do the checks itself, it can use the second directly. The third function is 412for unbuffered or DIO writes. 413 414On all three write functions, there is a writeback group pointer (which should 415be NULL if the filesystem doesn't use this). Writeback groups are set on 416folios when they're modified. If a folio to-be-modified is already marked with 417a different group, it is flushed first. The writeback API allows writing back 418of a specific group. 419 420Memory-Mapped I/O API 421--------------------- 422 423An 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 427This allows the filesystem to delegate ``.page_mkwrite`` to netfslib. The 428filesystem should not take the inode lock before calling it, but, as with the 429locked write functions above, this does take a writeback group pointer. If the 430page to be made writable is in a different group, it will be flushed first. 431 432Monolithic Files API 433-------------------- 434 435There is also a special API set for files for which the content must be read in 436a 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 445The first function reads from a file into the given buffer, reading from the 446cache in preference if the data is cached there; the second function allows the 447inode to be marked dirty, causing a later writeback; and the third function can 448be called from the writeback code to write the data to the cache, if there is 449one. 450 451The inode should be marked ``NETFS_ICTX_SINGLE_NO_UPLOAD`` if this API is to be 452used. The writeback function requires the buffer to be of ITER_FOLIOQ type. 453 454Clearing Stale Post-EOF Pagecache 455--------------------------------- 456 457When a file is extended, data left in the pagecache past the old EOF by a write 458through an mmap must not be exposed as file content. Netfslib clears this on 459its own write paths, and exports a helper so a filesystem can do the same from 460a resize path (truncate, setattr, fallocate and the like) that holds the 461inode'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 466This 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 468updated ``i_size`` to @to before calling it. Only the folio straddling @from 469can hold data written past the EOF through an mmap, as pages wholly beyond the 470EOF can't be faulted in, so the zeroing is limited to that folio. The folio is 471zeroed rather than dropped so that a concurrent extending write can't lose 472data. 473 474This overlaps with ``pagecache_isize_extended()`` but can't reuse it: that 475helper 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 477writeback. As both address the same problem, a change to one should probably 478be reflected in the other to keep them in sync. 479 480High-Level VM API 481================== 482 483Netfslib also provides a number of sets of API calls for the filesystem to 484delegate VM operations to. Again, netfslib, in turn, will call out to the 485filesystem and the cache to negotiate I/O sizes, issue RPCs and provide places 486for 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 496These are ``address_space_operations`` methods and can be set directly in the 497operations table. 498 499Deprecated PG_private_2 API 500--------------------------- 501 502There 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 509It uses the deprecated PG_private_2 flag and so should not be used. 510 511 512I/O Request API 513=============== 514 515The I/O request API comprises a number of structures and a number of functions 516that the filesystem may need to use. 517 518Request Structure 519----------------- 520 521The request structure manages the request as a whole, holding some resources 522and 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 540Many of the fields are for internal use, but the fields shown here are of 541interest 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 612If the filesystem wants more private data than is afforded by this structure, 613then it should wrap it and provide its own allocator. 614 615Stream Structure 616---------------- 617 618A request is comprised of one or more parallel streams and each stream may be 619aimed at a different target. 620 621For read requests, only stream 0 is used. This can contain a mixture of 622subrequests aimed at different sources. For write requests, stream 0 is used 623for the server and stream 1 is used for the cache. For buffered writeback, 624stream 0 is not enabled unless a normal dirty folio is encountered, at which 625point ->begin_writeback() will be invoked and the filesystem can mark the 626stream available. 627 628The 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 639A 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 664Subrequest Structure 665-------------------- 666 667Individual units of I/O are managed by the subrequest structure. These 668represent 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 683Each subrequest is expected to access a single source, though the library will 684handle 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 752If necessary, the filesystem can get and put extra refs on the subrequest it is 753given:: 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 760using netfs trace codes to indicate the reason. Care must be taken, however, 761as once control of the subrequest is returned to netfslib, the same subrequest 762can be reissued/retried. 763 764Filesystem Methods 765------------------ 766 767The filesystem sets a table of operations in ``netfs_inode`` for netfslib to 768use:: 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 790The table starts with a pair of optional pointers to memory pools from which 791requests and subrequests can be allocated. If these are not given, netfslib 792has default pools that it will use instead. If the filesystem wraps the netfs 793structs in its own larger structs, then it will need to use its own pools. 794Netfslib will allocate directly from the pools. 795 796The 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 918Terminating a subrequest 919------------------------ 920 921When a subrequest completes, there are a number of functions that the cache or 922subrequest can call to inform netfslib of the status change. One function is 923provided to terminate a write subrequest at the preparation stage and acts 924synchronously: 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 931Note that ->prepare_read() can return an error as a read can simply be aborted. 932Dealing with writeback failure is trickier. 933 934The 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 954Local Cache API 955--------------- 956 957Netfslib provides a separate API for a local cache to implement, though it 958provides some somewhat similar routines to the filesystem request API. 959 960Firstly, the netfs_io_request object contains a place for the cache to hang its 961state:: 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 971This contains an operations table pointer and two private pointers plus the 972debug ID of the fscache cookie for tracing purposes and an invalidation counter 973that is cranked by calls to ``fscache_invalidate()`` allowing cache subrequests 974to be invalidated after completion. 975 976The 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 994With 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 1000The 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 1073API Function Reference 1074====================== 1075 1076.. kernel-doc:: include/linux/netfs.h 1077.. kernel-doc:: fs/netfs/buffered_read.c 1078