1.. SPDX-License-Identifier: GPL-2.0 2.. _iomap_operations: 3 4.. 5 Dumb style notes to maintain the author's sanity: 6 Please try to start sentences on separate lines so that 7 sentence changes don't bleed colors in diff. 8 Heading decorations are documented in sphinx.rst. 9 10========================= 11Supported File Operations 12========================= 13 14.. contents:: Table of Contents 15 :local: 16 17Below are a discussion of the high level file operations that iomap 18implements. 19 20Each operation takes an ``iomap_iter_next_fn`` callback that supplies the file 21mappings, as described in the iomap design document. The per-operation 22``flags`` documented below are passed to that callback; references to 23``begin`` and ``end`` name the two steps a typical callback is built from 24via ``iomap_iter_next``. 25 26Buffered I/O 27============ 28 29Buffered I/O is the default file I/O path in Linux. 30File contents are cached in memory ("pagecache") to satisfy reads and 31writes. 32Dirty cache will be written back to disk at some point that can be 33forced via ``fsync`` and variants. 34 35iomap implements nearly all the folio and pagecache management that 36filesystems have to implement themselves under the legacy I/O model. 37This means that the filesystem need not know the details of allocating, 38mapping, managing uptodate and dirty state, or writeback of pagecache 39folios. 40Under the legacy I/O model, this was managed very inefficiently with 41linked lists of buffer heads instead of the per-folio bitmaps that iomap 42uses. 43Unless the filesystem explicitly opts in to buffer heads, they will not 44be used, which makes buffered I/O much more efficient, and the pagecache 45maintainer much happier. 46 47``struct address_space_operations`` 48----------------------------------- 49 50The following iomap functions can be referenced directly from the 51address space operations structure: 52 53 * ``iomap_dirty_folio`` 54 * ``iomap_release_folio`` 55 * ``iomap_invalidate_folio`` 56 * ``iomap_is_partially_uptodate`` 57 58The following address space operations can be wrapped easily: 59 60 * ``read_folio`` 61 * ``readahead`` 62 * ``writepages`` 63 * ``bmap`` 64 * ``swap_activate`` 65 66``struct iomap_write_ops`` 67-------------------------- 68 69.. code-block:: c 70 71 struct iomap_write_ops { 72 struct folio *(*get_folio)(struct iomap_iter *iter, loff_t pos, 73 unsigned len); 74 void (*put_folio)(struct inode *inode, loff_t pos, unsigned copied, 75 struct folio *folio); 76 bool (*iomap_valid)(struct inode *inode, const struct iomap *iomap); 77 int (*read_folio_range)(const struct iomap_iter *iter, 78 struct folio *folio, loff_t pos, size_t len); 79 }; 80 81iomap calls these functions: 82 83 - ``get_folio``: Called to allocate and return an active reference to 84 a locked folio prior to starting a write. 85 If this function is not provided, iomap will call 86 ``iomap_get_folio``. 87 This could be used to `set up per-folio filesystem state 88 <https://lore.kernel.org/all/20190429220934.10415-5-agruenba@redhat.com/>`_ 89 for a write. 90 91 - ``put_folio``: Called to unlock and put a folio after a pagecache 92 operation completes. 93 If this function is not provided, iomap will ``folio_unlock`` and 94 ``folio_put`` on its own. 95 This could be used to `commit per-folio filesystem state 96 <https://lore.kernel.org/all/20180619164137.13720-6-hch@lst.de/>`_ 97 that was set up by ``->get_folio``. 98 99 - ``iomap_valid``: The filesystem may not hold locks between ``begin`` and 100 ``end`` because pagecache operations can take folio locks, fault on 101 userspace pages, initiate writeback for memory reclamation, or engage in 102 other time-consuming actions. 103 If a file's space mapping data are mutable, it is possible that the 104 mapping for a particular pagecache folio can `change in the time it 105 takes 106 <https://lore.kernel.org/all/20221123055812.747923-8-david@fromorbit.com/>`_ 107 to allocate, install, and lock that folio. 108 109 For the pagecache, races can happen if writeback doesn't take 110 ``i_rwsem`` or ``invalidate_lock`` and updates mapping information. 111 Races can also happen if the filesystem allows concurrent writes. 112 For such files, the mapping *must* be revalidated after the folio 113 lock has been taken so that iomap can manage the folio correctly. 114 115 fsdax does not need this revalidation because there's no writeback 116 and no support for unwritten extents. 117 118 Filesystems subject to this kind of race must provide a 119 ``->iomap_valid`` function to decide if the mapping is still valid. 120 If the mapping is not valid, the mapping will be sampled again. 121 122 To support making the validity decision, the filesystem's 123 ``begin`` function may set ``struct iomap::validity_cookie`` 124 at the same time that it populates the other iomap fields. 125 A simple validation cookie implementation is a sequence counter. 126 If the filesystem bumps the sequence counter every time it modifies 127 the inode's extent map, it can be placed in the ``struct 128 iomap::validity_cookie`` during ``begin``. 129 If the value in the cookie is found to be different to the value 130 the filesystem holds when the mapping is passed back to 131 ``->iomap_valid``, then the iomap should considered stale and the 132 validation failed. 133 134 - ``read_folio_range``: Called to synchronously read in the range that will 135 be written to. If this function is not provided, iomap will default to 136 submitting a bio read request. 137 138These ``struct kiocb`` flags are significant for buffered I/O with iomap: 139 140 * ``IOCB_NOWAIT``: Turns on ``IOMAP_NOWAIT``. 141 142 * ``IOCB_DONTCACHE``: Turns on ``IOMAP_DONTCACHE``. 143 144``struct iomap_read_ops`` 145-------------------------- 146 147.. code-block:: c 148 149 struct iomap_read_ops { 150 int (*read_folio_range)(const struct iomap_iter *iter, 151 struct iomap_read_folio_ctx *ctx, size_t len); 152 void (*submit_read)(struct iomap_read_folio_ctx *ctx); 153 }; 154 155iomap calls these functions: 156 157 - ``read_folio_range``: Called to read in the range. This must be provided 158 by the caller. If this succeeds, iomap_finish_folio_read() must be called 159 after the range is read in, regardless of whether the read succeeded or 160 failed. 161 162 - ``submit_read``: Submit any pending read requests. This function is 163 optional. 164 165Internal per-Folio State 166------------------------ 167 168If the fsblock size matches the size of a pagecache folio, it is assumed 169that all disk I/O operations will operate on the entire folio. 170The uptodate (memory contents are at least as new as what's on disk) and 171dirty (memory contents are newer than what's on disk) status of the 172folio are all that's needed for this case. 173 174If the fsblock size is less than the size of a pagecache folio, iomap 175tracks the per-fsblock uptodate and dirty state itself. 176This enables iomap to handle both "bs < ps" `filesystems 177<https://lore.kernel.org/all/20230725122932.144426-1-ritesh.list@gmail.com/>`_ 178and large folios in the pagecache. 179 180iomap internally tracks two state bits per fsblock: 181 182 * ``uptodate``: iomap will try to keep folios fully up to date. 183 If there are read(ahead) errors, those fsblocks will not be marked 184 uptodate. 185 The folio itself will be marked uptodate when all fsblocks within the 186 folio are uptodate. 187 188 * ``dirty``: iomap will set the per-block dirty state when programs 189 write to the file. 190 The folio itself will be marked dirty when any fsblock within the 191 folio is dirty. 192 193iomap also tracks the amount of read and write disk IOs that are in 194flight. 195This structure is much lighter weight than ``struct buffer_head`` 196because there is only one per folio, and the per-fsblock overhead is two 197bits vs. 104 bytes. 198 199Filesystems wishing to turn on large folios in the pagecache should call 200``mapping_set_large_folios`` when initializing the incore inode. 201 202Buffered Readahead and Reads 203---------------------------- 204 205The ``iomap_readahead`` function initiates readahead to the pagecache. 206The ``iomap_read_folio`` function reads one folio's worth of data into 207the pagecache. 208The ``flags`` argument to ``begin`` will be set to zero. 209The pagecache takes whatever locks it needs before calling the 210filesystem. 211 212Both ``iomap_readahead`` and ``iomap_read_folio`` pass in a ``struct 213iomap_read_folio_ctx``: 214 215.. code-block:: c 216 217 struct iomap_read_folio_ctx { 218 const struct iomap_read_ops *ops; 219 struct folio *cur_folio; 220 struct readahead_control *rac; 221 void *read_ctx; 222 }; 223 224``iomap_readahead`` must set: 225 * ``ops->read_folio_range()`` and ``rac`` 226 227``iomap_read_folio`` must set: 228 * ``ops->read_folio_range()`` and ``cur_folio`` 229 230``ops->submit_read()`` and ``read_ctx`` are optional. ``read_ctx`` is used to 231pass in any custom data the caller needs accessible in the ops callbacks for 232fulfilling reads. 233 234Buffered Writes 235--------------- 236 237The ``iomap_file_buffered_write`` function writes an ``iocb`` to the 238pagecache. 239``IOMAP_WRITE`` or ``IOMAP_WRITE`` | ``IOMAP_NOWAIT`` will be passed as 240the ``flags`` argument to ``begin``. 241Callers commonly take ``i_rwsem`` in either shared or exclusive mode 242before calling this function. 243 244mmap Write Faults 245~~~~~~~~~~~~~~~~~ 246 247The ``iomap_page_mkwrite`` function handles a write fault to a folio in 248the pagecache. 249``IOMAP_WRITE | IOMAP_FAULT`` will be passed as the ``flags`` argument 250to ``begin``. 251Callers commonly take the mmap ``invalidate_lock`` in shared or 252exclusive mode before calling this function. 253 254Buffered Write Failures 255~~~~~~~~~~~~~~~~~~~~~~~ 256 257After a short write to the pagecache, the areas not written will not 258become marked dirty. 259The filesystem must arrange to `cancel 260<https://lore.kernel.org/all/20221123055812.747923-6-david@fromorbit.com/>`_ 261such `reservations 262<https://lore.kernel.org/linux-xfs/20220817093627.GZ3600936@dread.disaster.area/>`_ 263because writeback will not consume the reservation. 264The ``iomap_write_delalloc_release`` can be called from a 265``end`` function to find all the clean areas of the folios 266caching a fresh (``IOMAP_F_NEW``) delalloc mapping. 267It takes the ``invalidate_lock``. 268 269The filesystem must supply a function ``punch`` to be called for 270each file range in this state. 271This function must *only* remove delayed allocation reservations, in 272case another thread racing with the current thread writes successfully 273to the same region and triggers writeback to flush the dirty data out to 274disk. 275 276Zeroing for File Operations 277~~~~~~~~~~~~~~~~~~~~~~~~~~~ 278 279Filesystems can call ``iomap_zero_range`` to perform zeroing of the 280pagecache for non-truncation file operations that are not aligned to 281the fsblock size. 282``IOMAP_ZERO`` will be passed as the ``flags`` argument to 283``begin``. 284Callers typically hold ``i_rwsem`` and ``invalidate_lock`` in exclusive 285mode before calling this function. 286 287Unsharing Reflinked File Data 288~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 289 290Filesystems can call ``iomap_file_unshare`` to force a file sharing 291storage with another file to preemptively copy the shared data to newly 292allocate storage. 293``IOMAP_WRITE | IOMAP_UNSHARE`` will be passed as the ``flags`` argument 294to ``begin``. 295Callers typically hold ``i_rwsem`` and ``invalidate_lock`` in exclusive 296mode before calling this function. 297 298Truncation 299---------- 300 301Filesystems can call ``iomap_truncate_page`` to zero the bytes in the 302pagecache from EOF to the end of the fsblock during a file truncation 303operation. 304``truncate_setsize`` or ``truncate_pagecache`` will take care of 305everything after the EOF block. 306``IOMAP_ZERO`` will be passed as the ``flags`` argument to 307``begin``. 308Callers typically hold ``i_rwsem`` and ``invalidate_lock`` in exclusive 309mode before calling this function. 310 311Pagecache Writeback 312------------------- 313 314Filesystems can call ``iomap_writepages`` to respond to a request to 315write dirty pagecache folios to disk. 316The ``mapping`` and ``wbc`` parameters should be passed unchanged. 317The ``wpc`` pointer should be allocated by the filesystem and must 318be initialized to zero. 319 320The pagecache will lock each folio before trying to schedule it for 321writeback. 322It does not lock ``i_rwsem`` or ``invalidate_lock``. 323 324The dirty bit will be cleared for all folios run through the 325``->writeback_range`` machinery described below even if the writeback fails. 326This is to prevent dirty folio clots when storage devices fail; an 327``-EIO`` is recorded for userspace to collect via ``fsync``. 328 329The ``ops`` structure must be specified and is as follows: 330 331``struct iomap_writeback_ops`` 332~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 333 334.. code-block:: c 335 336 struct iomap_writeback_ops { 337 int (*writeback_range)(struct iomap_writepage_ctx *wpc, 338 struct folio *folio, u64 pos, unsigned int len, u64 end_pos); 339 int (*writeback_submit)(struct iomap_writepage_ctx *wpc, int error); 340 }; 341 342The fields are as follows: 343 344 - ``writeback_range``: Sets ``wpc->iomap`` to the space mapping of the file 345 range (in bytes) given by ``offset`` and ``len``. 346 iomap calls this function for each dirty fs block in each dirty folio, 347 though it will `reuse mappings 348 <https://lore.kernel.org/all/20231207072710.176093-15-hch@lst.de/>`_ 349 for runs of contiguous dirty fsblocks within a folio. 350 Do not return ``IOMAP_INLINE`` mappings here; the ``end`` function must 351 deal with persisting written data. 352 Do not return ``IOMAP_DELALLOC`` mappings here; iomap currently 353 requires mapping to allocated space. 354 Filesystems can skip a potentially expensive mapping lookup if the 355 mappings have not changed. 356 This revalidation must be open-coded by the filesystem; it is 357 unclear if ``iomap::validity_cookie`` can be reused for this 358 purpose. 359 360 If this methods fails to schedule I/O for any part of a dirty folio, it 361 should throw away any reservations that may have been made for the write. 362 The folio will be marked clean and an ``-EIO`` recorded in the 363 pagecache. 364 Filesystems can use this callback to `remove 365 <https://lore.kernel.org/all/20201029163313.1766967-1-bfoster@redhat.com/>`_ 366 delalloc reservations to avoid having delalloc reservations for 367 clean pagecache. 368 This function must be supplied by the filesystem. 369 If this succeeds, iomap_finish_folio_write() must be called once writeback 370 completes for the range, regardless of whether the writeback succeeded or 371 failed. 372 373 - ``writeback_submit``: Submit the previous built writeback context. 374 Block based file systems should use the iomap_ioend_writeback_submit 375 helper, other file system can implement their own. 376 File systems can optionally hook into writeback bio submission. 377 This might include pre-write space accounting updates, or installing 378 a custom ``->bi_end_io`` function for internal purposes, such as 379 deferring the ioend completion to a workqueue to run metadata update 380 transactions from process context before submitting the bio. 381 This function must be supplied by the filesystem. 382 383Pagecache Writeback Completion 384~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 385 386To handle the bookkeeping that must happen after disk I/O for writeback 387completes, iomap creates chains of ``struct iomap_ioend`` objects that 388wrap the ``bio`` that is used to write pagecache data to disk. 389By default, iomap finishes writeback ioends by clearing the writeback 390bit on the folios attached to the ``ioend``. 391If the write failed, it will also set the error bits on the folios and 392the address space. 393This can happen in interrupt or process context, depending on the 394storage device. 395Filesystems that need to update internal bookkeeping (e.g. unwritten 396extent conversions) should set their own bi_end_io on the bios 397submitted by ``->submit_writeback`` 398This function should call ``iomap_finish_ioends`` after finishing its 399own work (e.g. unwritten extent conversion). 400 401Some filesystems may wish to `amortize the cost of running metadata 402transactions 403<https://lore.kernel.org/all/20220120034733.221737-1-david@fromorbit.com/>`_ 404for post-writeback updates by batching them. 405They may also require transactions to run from process context, which 406implies punting batches to a workqueue. 407iomap ioends contain a ``list_head`` to enable batching. 408 409Given a batch of ioends, iomap has a few helpers to assist with 410amortization: 411 412 * ``iomap_sort_ioends``: Sort all the ioends in the list by file 413 offset. 414 415 * ``iomap_ioend_try_merge``: Given an ioend that is not in any list and 416 a separate list of sorted ioends, merge as many of the ioends from 417 the head of the list into the given ioend. 418 ioends can only be merged if the file range and storage addresses are 419 contiguous; the unwritten and shared status are the same; and the 420 write I/O outcome is the same. 421 The merged ioends become their own list. 422 423 * ``iomap_finish_ioends``: Finish an ioend that possibly has other 424 ioends linked to it. 425 426Direct I/O 427========== 428 429In Linux, direct I/O is defined as file I/O that is issued directly to 430storage, bypassing the pagecache. 431The ``iomap_dio_rw`` function implements O_DIRECT (direct I/O) reads and 432writes for files. 433 434.. code-block:: c 435 436 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, 437 iomap_iter_next_fn iomap_next, 438 const struct iomap_dio_ops *dops, 439 unsigned int dio_flags, void *private, 440 size_t done_before); 441 442The filesystem can provide the ``dops`` parameter if it needs to perform 443extra work before or after the I/O is issued to storage. 444The ``done_before`` parameter tells the how much of the request has 445already been transferred. 446It is used to continue a request asynchronously when `part of the 447request 448<https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c03098d4b9ad76bca2966a8769dcfe59f7f85103>`_ 449has already been completed synchronously. 450 451The ``done_before`` parameter should be set if writes for the ``iocb`` 452have been initiated prior to the call. 453The direction of the I/O is determined from the ``iocb`` passed in. 454 455The ``dio_flags`` argument can be set to any combination of the 456following values: 457 458 * ``IOMAP_DIO_FORCE_WAIT``: Wait for the I/O to complete even if the 459 kiocb is not synchronous. 460 461 * ``IOMAP_DIO_OVERWRITE_ONLY``: Perform a pure overwrite for this range 462 or fail with ``-EAGAIN``. 463 This can be used by filesystems with complex unaligned I/O 464 write paths to provide an optimised fast path for unaligned writes. 465 If a pure overwrite can be performed, then serialisation against 466 other I/Os to the same filesystem block(s) is unnecessary as there is 467 no risk of stale data exposure or data loss. 468 If a pure overwrite cannot be performed, then the filesystem can 469 perform the serialisation steps needed to provide exclusive access 470 to the unaligned I/O range so that it can perform allocation and 471 sub-block zeroing safely. 472 Filesystems can use this flag to try to reduce locking contention, 473 but a lot of `detailed checking 474 <https://lore.kernel.org/linux-ext4/20230314130759.642710-1-bfoster@redhat.com/>`_ 475 is required to do it `correctly 476 <https://lore.kernel.org/linux-ext4/20230810165559.946222-1-bfoster@redhat.com/>`_. 477 478 * ``IOMAP_DIO_PARTIAL``: If a page fault occurs, return whatever 479 progress has already been made. 480 The caller may deal with the page fault and retry the operation. 481 If the caller decides to retry the operation, it should pass the 482 accumulated return values of all previous calls as the 483 ``done_before`` parameter to the next call. 484 485These ``struct kiocb`` flags are significant for direct I/O with iomap: 486 487 * ``IOCB_NOWAIT``: Turns on ``IOMAP_NOWAIT``. 488 489 * ``IOCB_SYNC``: Ensure that the device has persisted data to disk 490 before completing the call. 491 In the case of pure overwrites, the I/O may be issued with FUA 492 enabled. 493 494 * ``IOCB_HIPRI``: Poll for I/O completion instead of waiting for an 495 interrupt. 496 Only meaningful for asynchronous I/O, and only if the entire I/O can 497 be issued as a single ``struct bio``. 498 499Filesystems should call ``iomap_dio_rw`` from ``->read_iter`` and 500``->write_iter``, and set ``FMODE_CAN_ODIRECT`` in the ``->open`` 501function for the file. 502They should not set ``->direct_IO``, which is deprecated. 503 504If a filesystem wishes to perform its own work before direct I/O 505completion, it should call ``__iomap_dio_rw``. 506If its return value is not an error pointer or a NULL pointer, the 507filesystem should pass the return value to ``iomap_dio_complete`` after 508finishing its internal work. 509 510Return Values 511------------- 512 513``iomap_dio_rw`` can return one of the following: 514 515 * A non-negative number of bytes transferred. 516 517 * ``-ENOTBLK``: Fall back to buffered I/O. 518 iomap itself will return this value if it cannot invalidate the page 519 cache before issuing the I/O to storage. 520 The ``begin`` or ``end`` functions may also return 521 this value. 522 523 * ``-EIOCBQUEUED``: The asynchronous direct I/O request has been 524 queued and will be completed separately. 525 526 * Any of the other negative error codes. 527 528Direct Reads 529------------ 530 531A direct I/O read initiates a read I/O from the storage device to the 532caller's buffer. 533Dirty parts of the pagecache are flushed to storage before initiating 534the read io. 535The ``flags`` value for ``begin`` will be ``IOMAP_DIRECT`` with 536any combination of the following enhancements: 537 538 * ``IOMAP_NOWAIT``, as defined previously. 539 540Callers commonly hold ``i_rwsem`` in shared mode before calling this 541function. 542 543Direct Writes 544------------- 545 546A direct I/O write initiates a write I/O to the storage device from the 547caller's buffer. 548Dirty parts of the pagecache are flushed to storage before initiating 549the write io. 550The pagecache is invalidated both before and after the write io. 551The ``flags`` value for ``begin`` will be ``IOMAP_DIRECT | 552IOMAP_WRITE`` with any combination of the following enhancements: 553 554 * ``IOMAP_NOWAIT``, as defined previously. 555 556 * ``IOMAP_OVERWRITE_ONLY``: Allocating blocks and zeroing partial 557 blocks is not allowed. 558 The entire file range must map to a single written or unwritten 559 extent. 560 The file I/O range must be aligned to the filesystem block size 561 if the mapping is unwritten and the filesystem cannot handle zeroing 562 the unaligned regions without exposing stale contents. 563 564 * ``IOMAP_ATOMIC``: This write is being issued with torn-write 565 protection. 566 Torn-write protection may be provided based on HW-offload or by a 567 software mechanism provided by the filesystem. 568 569 For HW-offload based support, only a single bio can be created for the 570 write, and the write must not be split into multiple I/O requests, i.e. 571 flag REQ_ATOMIC must be set. 572 The file range to write must be aligned to satisfy the requirements 573 of both the filesystem and the underlying block device's atomic 574 commit capabilities. 575 If filesystem metadata updates are required (e.g. unwritten extent 576 conversion or copy-on-write), all updates for the entire file range 577 must be committed atomically as well. 578 Untorn-writes may be longer than a single file block. In all cases, 579 the mapping start disk block must have at least the same alignment as 580 the write offset. 581 The filesystems must set IOMAP_F_ATOMIC_BIO to inform iomap core of an 582 untorn-write based on HW-offload. 583 584 For untorn-writes based on a software mechanism provided by the 585 filesystem, all the disk block alignment and single bio restrictions 586 which apply for HW-offload based untorn-writes do not apply. 587 The mechanism would typically be used as a fallback for when 588 HW-offload based untorn-writes may not be issued, e.g. the range of the 589 write covers multiple extents, meaning that it is not possible to issue 590 a single bio. 591 All filesystem metadata updates for the entire file range must be 592 committed atomically as well. 593 594Callers commonly hold ``i_rwsem`` in shared or exclusive mode before 595calling this function. 596 597``struct iomap_dio_ops:`` 598------------------------- 599.. code-block:: c 600 601 struct iomap_dio_ops { 602 void (*submit_io)(const struct iomap_iter *iter, struct bio *bio, 603 loff_t file_offset); 604 int (*end_io)(struct kiocb *iocb, ssize_t size, int error, 605 unsigned flags); 606 struct bio_set *bio_set; 607 }; 608 609The fields of this structure are as follows: 610 611 - ``submit_io``: iomap calls this function when it has constructed a 612 ``struct bio`` object for the I/O requested, and wishes to submit it 613 to the block device. 614 If no function is provided, ``submit_bio`` will be called directly. 615 Filesystems that would like to perform additional work before (e.g. 616 data replication for btrfs) should implement this function. 617 618 - ``end_io``: This is called after the ``struct bio`` completes. 619 This function should perform post-write conversions of unwritten 620 extent mappings, handle write failures, etc. 621 The ``flags`` argument may be set to a combination of the following: 622 623 * ``IOMAP_DIO_UNWRITTEN``: The mapping was unwritten, so the ioend 624 should mark the extent as written. 625 626 * ``IOMAP_DIO_COW``: Writing to the space in the mapping required a 627 copy on write operation, so the ioend should switch mappings. 628 629 - ``bio_set``: This allows the filesystem to provide a custom bio_set 630 for allocating direct I/O bios. 631 This enables filesystems to `stash additional per-bio information 632 <https://lore.kernel.org/all/20220505201115.937837-3-hch@lst.de/>`_ 633 for private use. 634 If this field is NULL, generic ``struct bio`` objects will be used. 635 636Filesystems that want to perform extra work after an I/O completion 637should set a custom ``->bi_end_io`` function via ``->submit_io``. 638Afterwards, the custom endio function must call 639``iomap_dio_bio_end_io`` to finish the direct I/O. 640 641DAX I/O 642======= 643 644Some storage devices can be directly mapped as memory. 645These devices support a new access mode known as "fsdax" that allows 646loads and stores through the CPU and memory controller. 647 648fsdax Reads 649----------- 650 651A fsdax read performs a memcpy from storage device to the caller's 652buffer. 653The ``flags`` value for ``begin`` will be ``IOMAP_DAX`` with any 654combination of the following enhancements: 655 656 * ``IOMAP_NOWAIT``, as defined previously. 657 658Callers commonly hold ``i_rwsem`` in shared mode before calling this 659function. 660 661fsdax Writes 662------------ 663 664A fsdax write initiates a memcpy to the storage device from the caller's 665buffer. 666The ``flags`` value for ``begin`` will be ``IOMAP_DAX | 667IOMAP_WRITE`` with any combination of the following enhancements: 668 669 * ``IOMAP_NOWAIT``, as defined previously. 670 671 * ``IOMAP_OVERWRITE_ONLY``: The caller requires a pure overwrite to be 672 performed from this mapping. 673 This requires the filesystem extent mapping to already exist as an 674 ``IOMAP_MAPPED`` type and span the entire range of the write I/O 675 request. 676 If the filesystem cannot map this request in a way that allows the 677 iomap infrastructure to perform a pure overwrite, it must fail the 678 mapping operation with ``-EAGAIN``. 679 680Callers commonly hold ``i_rwsem`` in exclusive mode before calling this 681function. 682 683fsdax mmap Faults 684~~~~~~~~~~~~~~~~~ 685 686The ``dax_iomap_fault`` function handles read and write faults to fsdax 687storage. 688For a read fault, ``IOMAP_DAX | IOMAP_FAULT`` will be passed as the 689``flags`` argument to ``begin``. 690For a write fault, ``IOMAP_DAX | IOMAP_FAULT | IOMAP_WRITE`` will be 691passed as the ``flags`` argument to ``begin``. 692 693Callers commonly hold the same locks as they do to call their iomap 694pagecache counterparts. 695 696fsdax Truncation, fallocate, and Unsharing 697------------------------------------------ 698 699For fsdax files, the following functions are provided to replace their 700iomap pagecache I/O counterparts. 701The ``flags`` argument to ``begin`` are the same as the pagecache counterparts, 702with ``IOMAP_DAX`` added. 703 704 * ``dax_file_unshare`` 705 * ``dax_zero_range`` 706 * ``dax_truncate_page`` 707 708Callers commonly hold the same locks as they do to call their iomap 709pagecache counterparts. 710 711fsdax Deduplication 712------------------- 713 714Filesystems implementing the ``FIDEDUPERANGE`` ioctl must call the 715``dax_remap_file_range_prep`` function with their own iomap read ops. 716 717Seeking Files 718============= 719 720iomap implements the two iterating whence modes of the ``llseek`` system 721call. 722 723SEEK_DATA 724--------- 725 726The ``iomap_seek_data`` function implements the SEEK_DATA "whence" value 727for llseek. 728``IOMAP_REPORT`` will be passed as the ``flags`` argument to ``begin``. 729 730For unwritten mappings, the pagecache will be searched. 731Regions of the pagecache with a folio mapped and uptodate fsblocks 732within those folios will be reported as data areas. 733 734Callers commonly hold ``i_rwsem`` in shared mode before calling this 735function. 736 737SEEK_HOLE 738--------- 739 740The ``iomap_seek_hole`` function implements the SEEK_HOLE "whence" value 741for llseek. 742``IOMAP_REPORT`` will be passed as the ``flags`` argument to ``begin``. 743 744For unwritten mappings, the pagecache will be searched. 745Regions of the pagecache with no folio mapped, or a !uptodate fsblock 746within a folio will be reported as sparse hole areas. 747 748Callers commonly hold ``i_rwsem`` in shared mode before calling this 749function. 750 751Swap File Activation 752==================== 753 754The ``iomap_swapfile_activate`` function finds all the base-page aligned 755regions in a file and sets them up as swap space. 756The file will be ``fsync()``'d before activation. 757``IOMAP_REPORT`` will be passed as the ``flags`` argument to ``begin``. 758All mappings must be mapped or unwritten; cannot be dirty or shared, and 759cannot span multiple block devices. 760Callers must hold ``i_rwsem`` in exclusive mode; this is already 761provided by ``swapon``. 762 763File Space Mapping Reporting 764============================ 765 766iomap implements two of the file space mapping system calls. 767 768FS_IOC_FIEMAP 769------------- 770 771The ``iomap_fiemap`` function exports file extent mappings to userspace 772in the format specified by the ``FS_IOC_FIEMAP`` ioctl. 773``IOMAP_REPORT`` will be passed as the ``flags`` argument to ``begin``. 774Callers commonly hold ``i_rwsem`` in shared mode before calling this 775function. 776 777FIBMAP (deprecated) 778------------------- 779 780``iomap_bmap`` implements FIBMAP. 781The calling conventions are the same as for FIEMAP. 782This function is only provided to maintain compatibility for filesystems 783that implemented FIBMAP prior to conversion. 784This ioctl is deprecated; do **not** add a FIBMAP implementation to 785filesystems that do not have it. 786Callers should probably hold ``i_rwsem`` in shared mode before calling 787this function, but this is unclear. 788