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