← Documents Documentation/filesystems/iomap/operations.rst GitHub 원문 ↗

Linux 6.18.37 · Filesystems

iomap Supported File Operations

buffered·direct·DAX I/O, writeback, seek, swapfile와 FIEMAP의 iomap 계약을 설명하는 전문 번역입니다.

Source pathDocumentation/filesystems/iomap/operations.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

operations.rst:1-743

iomap은 buffered I/O의 folio 상태와 writeback, pagecache를 우회하는 direct I/O, memory-mapped storage의 fsdax, `SEEK_DATA`·`SEEK_HOLE`, swapfile, FIEMAP을 공통 mapping iterator 위에 구현합니다.

파일시스템은 operation별 begin flag와 lock 규약을 지키고, mutable mapping은 folio lock 뒤 재검증해야 합니다. writeback 실패 시 dirty bit가 지워지고 `-EIO`가 기록된다는 점, direct partial retry에서 누적 `done_before`를 넘겨야 한다는 점, atomic write에서 data와 metadata 모두 동일 범위로 atomic해야 한다는 점이 특히 중요합니다.

Completion hook을 구현할 때는 unwritten conversion, copy-on-write, reservation 회수, custom bio state를 generic iomap 완료 함수와 정확한 순서로 연결해야 합니다.

iomap operation 구현 점검
operation에 맞는 `IOMAP_*` begin flag 선택상위 filesystem lock 규약 확인mapping 획득과 필요 시 validity 재검증folio·bio·DAX 방식으로 data 이동reservation·extent·metadata completion 처리오류·fallback·비동기 반환값 전달

각 I/O 경로가 공통적으로 거치는 검토 항목입니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

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

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

지원하는 상위 수준 파일 연산

1-19

이 문서는 GPL-2.0이며 Sphinx anchor는 `iomap_operations`입니다. 숨김 style note는 diff 가독성을 위해 문장마다 줄을 나누고 heading 장식은 `sphinx.rst`를 따르라고 요청합니다.

local table of contents 아래에서 iomap이 구현하는 상위 수준 file operation을 설명합니다. 이후 각 절은 buffered I/O, direct I/O, fsdax, seek, swapfile, 공간 mapping 보고가 요구하는 callback과 flag, locking을 구체적으로 정의합니다.

.. SPDX-License-Identifier: GPL-2.0
.. _iomap_operations:

..
        Dumb style notes to maintain the author's sanity:
        Please try to start sentences on separate lines so that
        sentence changes don't bleed colors in diff.
        Heading decorations are documented in sphinx.rst.

=========================
Supported File Operations
=========================

.. contents:: Table of Contents
   :local:

Below are a discussion of the high level file operations that iomap
implements.

Buffered I/O와 address space operation

20-59

Buffered I/O는 Linux의 기본 file I/O 경로입니다. file content를 memory의 pagecache에 보관해 read와 write를 처리하고, dirty cache는 나중에 disk로 writeback됩니다. `fsync`와 그 변형으로 writeback을 강제할 수 있습니다.

iomap은 legacy I/O model에서 파일시스템이 직접 구현해야 했던 folio와 pagecache 관리의 거의 전부를 구현합니다. 따라서 파일시스템은 pagecache folio의 allocation, mapping, uptodate·dirty 상태 관리, writeback 세부사항을 알 필요가 없습니다.

legacy model은 folio별 bitmap 대신 buffer head linked list로 이를 비효율적으로 관리했습니다. 파일시스템이 buffer head 사용을 명시적으로 선택하지 않는 한 iomap은 사용하지 않으므로 buffered I/O가 훨씬 효율적입니다.

`struct address_space_operations`에서 `iomap_dirty_folio`, `iomap_release_folio`, `iomap_invalidate_folio`, `iomap_is_partially_uptodate`를 직접 참조할 수 있습니다. `read_folio`, `readahead`, `writepages`, `bmap`, `swap_activate` operation은 iomap helper를 간단히 감싸 구현할 수 있습니다.

iomap과 address_space_operations
형태항목
직접 참조`iomap_dirty_folio`, `iomap_release_folio`
직접 참조`iomap_invalidate_folio`, `iomap_is_partially_uptodate`
간단한 wrapper`read_folio`, `readahead`, `writepages`
간단한 wrapper`bmap`, `swap_activate`

직접 연결할 함수와 wrapper가 필요한 operation을 구분합니다.

Buffered I/O
============

Buffered I/O is the default file I/O path in Linux.
File contents are cached in memory ("pagecache") to satisfy reads and
writes.
Dirty cache will be written back to disk at some point that can be
forced via ``fsync`` and variants.

iomap implements nearly all the folio and pagecache management that
filesystems have to implement themselves under the legacy I/O model.
This means that the filesystem need not know the details of allocating,
mapping, managing uptodate and dirty state, or writeback of pagecache
folios.
Under the legacy I/O model, this was managed very inefficiently with
linked lists of buffer heads instead of the per-folio bitmaps that iomap
uses.
Unless the filesystem explicitly opts in to buffer heads, they will not
be used, which makes buffered I/O much more efficient, and the pagecache
maintainer much happier.

``struct address_space_operations``
-----------------------------------

The following iomap functions can be referenced directly from the
address space operations structure:

 * ``iomap_dirty_folio``
 * ``iomap_release_folio``
 * ``iomap_invalidate_folio``
 * ``iomap_is_partially_uptodate``

The following address space operations can be wrapped easily:

 * ``read_folio``
 * ``readahead``
 * ``writepages``
 * ``bmap``
 * ``swap_activate``

`struct iomap_write_ops`와 mapping 재검증

60-137

`struct iomap_write_ops`는 `get_folio`, `put_folio`, `iomap_valid`, `read_folio_range` callback을 제공합니다.

`get_folio`는 write 시작 전에 locked folio를 할당하고 active reference를 반환합니다. 제공하지 않으면 iomap이 `iomap_get_folio`를 호출합니다. 파일시스템은 이를 이용해 write용 per-folio private state를 준비할 수 있습니다.

`put_folio`는 pagecache 연산 완료 후 folio를 unlock하고 reference를 내려놓습니다. 제공하지 않으면 iomap이 `folio_unlock`과 `folio_put`을 호출합니다. `->get_folio`에서 준비한 per-folio filesystem state를 commit하는 데 사용할 수 있습니다.

pagecache 연산은 folio lock 획득, 사용자 공간 page fault, memory reclaim을 위한 writeback, 그 밖의 오래 걸리는 작업을 수행할 수 있으므로 파일시스템은 `->iomap_begin`과 `->iomap_end` 사이에 lock을 유지할 수 없습니다. file space mapping data가 mutable이면 folio를 할당·설치·lock하는 사이 특정 folio의 mapping이 바뀔 수 있습니다.

writeback이 `i_rwsem` 또는 `invalidate_lock`을 잡지 않고 mapping 정보를 갱신하거나 파일시스템이 concurrent write를 허용하면 pagecache race가 발생할 수 있습니다. 이런 파일은 folio lock을 잡은 뒤 mapping을 반드시 재검증해야 iomap이 folio를 올바르게 관리할 수 있습니다. fsdax에는 writeback도 unwritten extent 지원도 없으므로 이 재검증이 필요 없습니다.

이 race에 노출되는 파일시스템은 mapping이 여전히 유효한지 판단하는 `->iomap_valid`를 제공해야 합니다. 유효하지 않으면 mapping을 다시 sampling합니다.

검증을 돕기 위해 `->iomap_begin`은 다른 field와 함께 `struct iomap::validity_cookie`를 설정할 수 있습니다. 단순 구현은 inode extent map을 바꿀 때마다 증가시키는 sequence counter입니다. begin에서 cookie에 counter를 넣고, `->iomap_valid`로 돌아왔을 때 파일시스템 값과 다르면 iomap은 stale이며 검증은 실패합니다.

`read_folio_range`는 write할 범위를 동기적으로 먼저 읽습니다. 제공하지 않으면 iomap이 bio read request를 제출합니다. Buffered I/O에서 `struct kiocb`의 `IOCB_NOWAIT`는 `IOMAP_NOWAIT`, `IOCB_DONTCACHE`는 `IOMAP_DONTCACHE`를 켭니다.

`struct iomap_write_ops` callback
Callback호출 시점기본 동작
`get_folio`write 전 locked folio 획득`iomap_get_folio`
`put_folio`pagecache 작업 후 해제`folio_unlock` + `folio_put`
`iomap_valid`folio lock 후 mapping 재검증필요한 filesystem이 반드시 제공
`read_folio_range`write 전 기존 범위 동기 readbio read request 제출

folio 수명과 mapping 유효성 검사를 파일시스템이 확장합니다.

pagecache mapping 재검증
`->iomap_begin`에서 extent sequence를 cookie에 저장filesystem mapping lock 해제folio 할당·설치·lock`->iomap_valid`에서 현재 sequence와 cookie 비교같으면 계속, 다르면 mapping을 다시 sampling

lock을 유지할 수 없는 구간에서 stale mapping을 검출합니다.

``struct iomap_write_ops``
--------------------------

.. code-block:: c

 struct iomap_write_ops {
     struct folio *(*get_folio)(struct iomap_iter *iter, loff_t pos,
                                unsigned len);
     void (*put_folio)(struct inode *inode, loff_t pos, unsigned copied,
                       struct folio *folio);
     bool (*iomap_valid)(struct inode *inode, const struct iomap *iomap);
     int (*read_folio_range)(const struct iomap_iter *iter,
                             struct folio *folio, loff_t pos, size_t len);
 };

iomap calls these functions:

  - ``get_folio``: Called to allocate and return an active reference to
    a locked folio prior to starting a write.
    If this function is not provided, iomap will call
    ``iomap_get_folio``.
    This could be used to `set up per-folio filesystem state
    <https://lore.kernel.org/all/20190429220934.10415-5-agruenba@redhat.com/>`_
    for a write.

  - ``put_folio``: Called to unlock and put a folio after a pagecache
    operation completes.
    If this function is not provided, iomap will ``folio_unlock`` and
    ``folio_put`` on its own.
    This could be used to `commit per-folio filesystem state
    <https://lore.kernel.org/all/20180619164137.13720-6-hch@lst.de/>`_
    that was set up by ``->get_folio``.

  - ``iomap_valid``: The filesystem may not hold locks between
    ``->iomap_begin`` and ``->iomap_end`` because pagecache operations
    can take folio locks, fault on userspace pages, initiate writeback
    for memory reclamation, or engage in other time-consuming actions.
    If a file's space mapping data are mutable, it is possible that the
    mapping for a particular pagecache folio can `change in the time it
    takes
    <https://lore.kernel.org/all/20221123055812.747923-8-david@fromorbit.com/>`_
    to allocate, install, and lock that folio.

    For the pagecache, races can happen if writeback doesn't take
    ``i_rwsem`` or ``invalidate_lock`` and updates mapping information.
    Races can also happen if the filesystem allows concurrent writes.
    For such files, the mapping *must* be revalidated after the folio
    lock has been taken so that iomap can manage the folio correctly.

    fsdax does not need this revalidation because there's no writeback
    and no support for unwritten extents.

    Filesystems subject to this kind of race must provide a
    ``->iomap_valid`` function to decide if the mapping is still valid.
    If the mapping is not valid, the mapping will be sampled again.

    To support making the validity decision, the filesystem's
    ``->iomap_begin`` function may set ``struct iomap::validity_cookie``
    at the same time that it populates the other iomap fields.
    A simple validation cookie implementation is a sequence counter.
    If the filesystem bumps the sequence counter every time it modifies
    the inode's extent map, it can be placed in the ``struct
    iomap::validity_cookie`` during ``->iomap_begin``.
    If the value in the cookie is found to be different to the value
    the filesystem holds when the mapping is passed back to
    ``->iomap_valid``, then the iomap should considered stale and the
    validation failed.

  - ``read_folio_range``: Called to synchronously read in the range that will
    be written to. If this function is not provided, iomap will default to
    submitting a bio read request.

These ``struct kiocb`` flags are significant for buffered I/O with iomap:

 * ``IOCB_NOWAIT``: Turns on ``IOMAP_NOWAIT``.

 * ``IOCB_DONTCACHE``: Turns on ``IOMAP_DONTCACHE``.

내부 per-folio 상태

138-174

fsblock 크기와 pagecache folio 크기가 같으면 모든 disk I/O가 folio 전체에 작용한다고 가정합니다. 이 경우 memory content가 disk만큼 최신인지 나타내는 `uptodate`와 memory가 disk보다 최신인지 나타내는 `dirty` folio 상태만 있으면 됩니다.

fsblock이 folio보다 작으면 iomap이 fsblock별 uptodate와 dirty 상태를 직접 추적합니다. 이로써 block size가 page size보다 작은 `bs < ps` 파일시스템과 pagecache의 large folio를 모두 처리합니다.

iomap은 fsblock마다 두 bit를 추적합니다. read 또는 readahead 오류가 난 fsblock은 `uptodate`로 표시하지 않고, folio 안 모든 fsblock이 uptodate일 때 folio도 uptodate로 표시합니다. program이 파일에 쓰면 해당 block의 `dirty`를 설정하고, 하나라도 dirty이면 folio도 dirty로 표시합니다.

진행 중인 read·write disk I/O 개수도 추적합니다. folio마다 구조체 하나만 있고 fsblock별 비용이 104 byte인 `struct buffer_head`와 달리 두 bit뿐이므로 훨씬 가볍습니다. pagecache에서 large folio를 쓰려는 파일시스템은 incore inode 초기화 때 `mapping_set_large_folios`를 호출해야 합니다.

folio와 fsblock 상태 집계
상태fsblock 규칙folio 규칙
`uptodate`read 성공 block만 설정모든 fsblock이 설정될 때 설정
`dirty`program이 쓴 block에 설정하나라도 dirty이면 설정
진행 I/Oread·write 요청 수 추적folio별 단일 state object

작은 fsblock 여러 개가 한 folio의 상태를 결정합니다.

Internal per-Folio State
------------------------

If the fsblock size matches the size of a pagecache folio, it is assumed
that all disk I/O operations will operate on the entire folio.
The uptodate (memory contents are at least as new as what's on disk) and
dirty (memory contents are newer than what's on disk) status of the
folio are all that's needed for this case.

If the fsblock size is less than the size of a pagecache folio, iomap
tracks the per-fsblock uptodate and dirty state itself.
This enables iomap to handle both "bs < ps" `filesystems
<https://lore.kernel.org/all/20230725122932.144426-1-ritesh.list@gmail.com/>`_
and large folios in the pagecache.

iomap internally tracks two state bits per fsblock:

 * ``uptodate``: iomap will try to keep folios fully up to date.
   If there are read(ahead) errors, those fsblocks will not be marked
   uptodate.
   The folio itself will be marked uptodate when all fsblocks within the
   folio are uptodate.

 * ``dirty``: iomap will set the per-block dirty state when programs
   write to the file.
   The folio itself will be marked dirty when any fsblock within the
   folio is dirty.

iomap also tracks the amount of read and write disk IOs that are in
flight.
This structure is much lighter weight than ``struct buffer_head``
because there is only one per folio, and the per-fsblock overhead is two
bits vs. 104 bytes.

Filesystems wishing to turn on large folios in the pagecache should call
``mapping_set_large_folios`` when initializing the incore inode.

Buffered readahead·read·write

175-194

`iomap_readahead`는 pagecache readahead를 시작하고 `iomap_read_folio`는 folio 하나 분량의 data를 pagecache로 읽습니다. 이때 `->iomap_begin`의 `flags`는 0이며, pagecache가 파일시스템을 호출하기 전에 필요한 lock을 잡습니다.

`iomap_file_buffered_write`는 `iocb`를 pagecache에 씁니다. `->iomap_begin`에는 `IOMAP_WRITE` 또는 `IOMAP_WRITE | IOMAP_NOWAIT`가 전달됩니다. 호출자는 보통 이 함수 전에 `i_rwsem`을 shared 또는 exclusive mode로 잡습니다.

기본 buffered operation
OperationAPI`->iomap_begin` flags
readahead`iomap_readahead`0
folio read`iomap_read_folio`0
buffered write`iomap_file_buffered_write``IOMAP_WRITE` + 선택적 `IOMAP_NOWAIT`

각 API와 begin callback flag를 연결합니다.

Buffered Readahead and Reads
----------------------------

The ``iomap_readahead`` function initiates readahead to the pagecache.
The ``iomap_read_folio`` function reads one folio's worth of data into
the pagecache.
The ``flags`` argument to ``->iomap_begin`` will be set to zero.
The pagecache takes whatever locks it needs before calling the
filesystem.

Buffered Writes
---------------

The ``iomap_file_buffered_write`` function writes an ``iocb`` to the
pagecache.
``IOMAP_WRITE`` or ``IOMAP_WRITE`` | ``IOMAP_NOWAIT`` will be passed as
the ``flags`` argument to ``->iomap_begin``.
Callers commonly take ``i_rwsem`` in either shared or exclusive mode
before calling this function.

mmap write fault와 short write 실패 처리

195-226

`iomap_page_mkwrite`는 pagecache folio의 write fault를 처리합니다. `->iomap_begin`에는 `IOMAP_WRITE | IOMAP_FAULT`가 전달됩니다. 호출자는 보통 mmap `invalidate_lock`을 shared 또는 exclusive mode로 잡고 호출합니다.

pagecache short write 뒤에는 쓰지 않은 영역이 dirty로 표시되지 않습니다. writeback이 그 reservation을 소비하지 않으므로 파일시스템이 해당 delayed allocation reservation을 취소해야 합니다.

`->iomap_end`에서 `iomap_write_delalloc_release`를 호출하면 새 `IOMAP_F_NEW` delalloc mapping을 cache하는 folio 중 clean 영역을 모두 찾을 수 있습니다. 이 함수는 `invalidate_lock`을 잡습니다.

파일시스템은 이러한 각 file range에 호출할 `punch` 함수를 제공해야 합니다. 같은 영역에 racing thread가 성공적으로 write하고 writeback으로 dirty data를 disk에 내보낼 수 있으므로 `punch`는 delayed allocation reservation만 제거해야 하며 이미 기록된 data를 없애면 안 됩니다.

short buffered write reservation 정리
`IOMAP_F_NEW` delalloc mapping으로 write 시작short write로 일부 영역은 clean 상태 유지`->iomap_end`에서 `iomap_write_delalloc_release``invalidate_lock` 아래 clean range 탐색`punch`가 delayed allocation reservation만 제거

clean 영역의 미사용 delalloc만 안전하게 회수합니다.

mmap Write Faults
~~~~~~~~~~~~~~~~~

The ``iomap_page_mkwrite`` function handles a write fault to a folio in
the pagecache.
``IOMAP_WRITE | IOMAP_FAULT`` will be passed as the ``flags`` argument
to ``->iomap_begin``.
Callers commonly take the mmap ``invalidate_lock`` in shared or
exclusive mode before calling this function.

Buffered Write Failures
~~~~~~~~~~~~~~~~~~~~~~~

After a short write to the pagecache, the areas not written will not
become marked dirty.
The filesystem must arrange to `cancel
<https://lore.kernel.org/all/20221123055812.747923-6-david@fromorbit.com/>`_
such `reservations
<https://lore.kernel.org/linux-xfs/20220817093627.GZ3600936@dread.disaster.area/>`_
because writeback will not consume the reservation.
The ``iomap_write_delalloc_release`` can be called from a
``->iomap_end`` function to find all the clean areas of the folios
caching a fresh (``IOMAP_F_NEW``) delalloc mapping.
It takes the ``invalidate_lock``.

The filesystem must supply a function ``punch`` to be called for
each file range in this state.
This function must *only* remove delayed allocation reservations, in
case another thread racing with the current thread writes successfully
to the same region and triggers writeback to flush the dirty data out to
disk.

Zeroing, reflink unshare, truncation

227-261

파일시스템은 truncation이 아닌 연산의 범위가 fsblock 크기에 정렬되지 않았을 때 `iomap_zero_range`로 pagecache를 zeroing할 수 있습니다. `->iomap_begin`에는 `IOMAP_ZERO`가 전달되며 호출자는 보통 `i_rwsem`과 `invalidate_lock`을 exclusive mode로 잡습니다.

`iomap_file_unshare`는 다른 파일과 storage를 공유하는 reflink file이 공유 data를 새로 할당한 공간에 미리 copy하도록 강제합니다. `->iomap_begin`에는 `IOMAP_WRITE | IOMAP_UNSHARE`가 전달되고 두 lock을 보통 exclusive mode로 잡습니다.

file truncation 중 `iomap_truncate_page`는 EOF부터 해당 fsblock 끝까지 pagecache byte를 zeroing합니다. EOF block 이후는 `truncate_setsize` 또는 `truncate_pagecache`가 처리합니다. begin flag는 `IOMAP_ZERO`이고 호출자는 보통 `i_rwsem`과 `invalidate_lock`을 exclusive mode로 잡습니다.

Pagecache 범위 변경 helper
연산HelperFlags일반적인 lock
비정렬 범위 zero`iomap_zero_range``IOMAP_ZERO``i_rwsem` + `invalidate_lock` exclusive
reflink unshare`iomap_file_unshare``IOMAP_WRITE | IOMAP_UNSHARE`두 lock exclusive
EOF block truncate`iomap_truncate_page``IOMAP_ZERO`두 lock exclusive

zeroing과 unshare가 요구하는 flag와 lock입니다.

Zeroing for File Operations
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Filesystems can call ``iomap_zero_range`` to perform zeroing of the
pagecache for non-truncation file operations that are not aligned to
the fsblock size.
``IOMAP_ZERO`` will be passed as the ``flags`` argument to
``->iomap_begin``.
Callers typically hold ``i_rwsem`` and ``invalidate_lock`` in exclusive
mode before calling this function.

Unsharing Reflinked File Data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Filesystems can call ``iomap_file_unshare`` to force a file sharing
storage with another file to preemptively copy the shared data to newly
allocate storage.
``IOMAP_WRITE | IOMAP_UNSHARE`` will be passed as the ``flags`` argument
to ``->iomap_begin``.
Callers typically hold ``i_rwsem`` and ``invalidate_lock`` in exclusive
mode before calling this function.

Truncation
----------

Filesystems can call ``iomap_truncate_page`` to zero the bytes in the
pagecache from EOF to the end of the fsblock during a file truncation
operation.
``truncate_setsize`` or ``truncate_pagecache`` will take care of
everything after the EOF block.
``IOMAP_ZERO`` will be passed as the ``flags`` argument to
``->iomap_begin``.
Callers typically hold ``i_rwsem`` and ``invalidate_lock`` in exclusive
mode before calling this function.

Pagecache writeback과 `struct iomap_writeback_ops`

262-330

파일시스템은 dirty pagecache folio를 disk에 쓰라는 요청에 `iomap_writepages`로 응답할 수 있습니다. `mapping`과 `wbc` parameter는 변경 없이 전달하고, 파일시스템이 할당하는 `wpc`는 0으로 초기화해야 합니다.

pagecache는 writeback을 schedule하기 전에 각 folio를 lock하지만 `i_rwsem`이나 `invalidate_lock`은 잡지 않습니다. 아래 `->writeback_range` 경로를 거친 모든 folio는 writeback 실패 여부와 관계없이 dirty bit를 지웁니다. storage device 실패 때 dirty folio가 뭉쳐 남는 것을 막기 위한 동작이며, 사용자 공간이 `fsync`로 회수할 `-EIO`를 기록합니다.

필수 `struct iomap_writeback_ops`는 `writeback_range`와 `writeback_submit` callback을 가집니다.

`writeback_range`는 `offset`과 `len`이 지정한 byte 범위의 공간 mapping을 `wpc->iomap`에 설정합니다. iomap은 dirty folio의 dirty fsblock마다 호출하지만 folio 안에서 연속된 dirty fsblock run은 mapping을 재사용합니다.

이 callback에서는 `IOMAP_INLINE`을 반환하면 안 됩니다. written data 영속화는 `->iomap_end`가 처리해야 하기 때문입니다. `IOMAP_DELALLOC`도 반환할 수 없으며 현재 iomap은 할당된 공간으로의 mapping을 요구합니다.

mapping이 바뀌지 않았다면 비싼 lookup을 생략할 수 있지만 재검증은 파일시스템이 직접 구현해야 합니다. `iomap::validity_cookie`를 재사용할 수 있는지는 불분명합니다.

dirty folio 일부라도 I/O schedule에 실패하면 write를 위해 만든 reservation을 모두 버려야 합니다. folio는 clean으로 표시되고 pagecache에 `-EIO`가 기록됩니다. callback은 delalloc reservation을 제거해 clean pagecache에 reservation이 남지 않게 할 수 있으며 반드시 파일시스템이 제공해야 합니다.

`writeback_submit`은 직전에 만든 writeback context를 제출합니다. block 기반 파일시스템은 `iomap_ioend_writeback_submit` helper를 사용하고 다른 파일시스템은 자체 구현할 수 있습니다. pre-write 공간 회계 갱신이나 custom `->bi_end_io` 설치 등 bio 제출 직전 작업을 hook할 수 있습니다.

custom end I/O는 metadata update transaction을 process context에서 실행하려고 ioend completion을 workqueue로 미루는 데도 쓸 수 있습니다. 이 callback도 반드시 제공해야 합니다.

Pagecache writeback scheduling
`iomap_writepages(mapping, wbc, wpc, ops)`pagecache가 folio lock`writeback_range`가 dirty fsblock mapping 설정연속 dirty fsblock은 mapping 재사용writeback context에 bio 축적`writeback_submit`이 context 제출실패해도 folio dirty bit 제거, `-EIO` 기록

dirty fsblock mapping과 bio 제출의 두 callback 단계입니다.

Pagecache Writeback
-------------------

Filesystems can call ``iomap_writepages`` to respond to a request to
write dirty pagecache folios to disk.
The ``mapping`` and ``wbc`` parameters should be passed unchanged.
The ``wpc`` pointer should be allocated by the filesystem and must
be initialized to zero.

The pagecache will lock each folio before trying to schedule it for
writeback.
It does not lock ``i_rwsem`` or ``invalidate_lock``.

The dirty bit will be cleared for all folios run through the
``->writeback_range`` machinery described below even if the writeback fails.
This is to prevent dirty folio clots when storage devices fail; an
``-EIO`` is recorded for userspace to collect via ``fsync``.

The ``ops`` structure must be specified and is as follows:

``struct iomap_writeback_ops``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: c

 struct iomap_writeback_ops {
    int (*writeback_range)(struct iomap_writepage_ctx *wpc,
        struct folio *folio, u64 pos, unsigned int len, u64 end_pos);
    int (*writeback_submit)(struct iomap_writepage_ctx *wpc, int error);
 };

The fields are as follows:

  - ``writeback_range``: Sets ``wpc->iomap`` to the space mapping of the file
    range (in bytes) given by ``offset`` and ``len``.
    iomap calls this function for each dirty fs block in each dirty folio,
    though it will `reuse mappings
    <https://lore.kernel.org/all/20231207072710.176093-15-hch@lst.de/>`_
    for runs of contiguous dirty fsblocks within a folio.
    Do not return ``IOMAP_INLINE`` mappings here; the ``->iomap_end``
    function must deal with persisting written data.
    Do not return ``IOMAP_DELALLOC`` mappings here; iomap currently
    requires mapping to allocated space.
    Filesystems can skip a potentially expensive mapping lookup if the
    mappings have not changed.
    This revalidation must be open-coded by the filesystem; it is
    unclear if ``iomap::validity_cookie`` can be reused for this
    purpose.

    If this methods fails to schedule I/O for any part of a dirty folio, it
    should throw away any reservations that may have been made for the write.
    The folio will be marked clean and an ``-EIO`` recorded in the
    pagecache.
    Filesystems can use this callback to `remove
    <https://lore.kernel.org/all/20201029163313.1766967-1-bfoster@redhat.com/>`_
    delalloc reservations to avoid having delalloc reservations for
    clean pagecache.
    This function must be supplied by the filesystem.

  - ``writeback_submit``: Submit the previous built writeback context.
    Block based file systems should use the iomap_ioend_writeback_submit
    helper, other file system can implement their own.
    File systems can optionally hook into writeback bio submission.
    This might include pre-write space accounting updates, or installing
    a custom ``->bi_end_io`` function for internal purposes, such as
    deferring the ioend completion to a workqueue to run metadata update
    transactions from process context before submitting the bio.
    This function must be supplied by the filesystem.

Pagecache writeback completion과 ioend batching

331-373

writeback disk I/O 완료 후 회계를 처리하려고 iomap은 pagecache data를 disk에 쓰는 `bio`를 감싼 `struct iomap_ioend` chain을 만듭니다.

기본적으로 iomap은 ioend에 연결된 folio의 writeback bit를 지워 완료합니다. write가 실패하면 folio와 address space에 error bit도 설정합니다. storage device에 따라 interrupt 또는 process context에서 실행될 수 있습니다.

unwritten extent conversion처럼 내부 회계 갱신이 필요한 파일시스템은 `->submit_writeback`이 제출하는 bio에 자체 `bi_end_io`를 설정해야 합니다. 자체 작업을 마친 뒤 `iomap_finish_ioends`를 호출해야 합니다. 원문은 callback 구조체 이름 `writeback_submit`과 별도로 여기에서 `->submit_writeback` 표기를 사용하므로 그대로 보존합니다.

일부 파일시스템은 writeback 후 metadata transaction 비용을 줄이려고 여러 완료를 batch하거나, transaction을 process context에서 실행하려고 batch를 workqueue로 넘겨야 합니다. iomap ioend의 `list_head`가 batching을 지원합니다.

`iomap_sort_ioends`는 list의 ioend를 file offset 순으로 정렬합니다. `iomap_ioend_try_merge`는 어떤 list에도 속하지 않은 ioend와 정렬된 별도 list를 받아 head부터 가능한 ioend를 합칩니다. file range와 storage address가 연속이고 unwritten·shared 상태와 write I/O 결과가 같을 때만 합칠 수 있으며, 합쳐진 ioend는 자체 list가 됩니다.

`iomap_finish_ioends`는 다른 ioend가 연결됐을 수 있는 하나의 ioend를 최종 완료합니다.

Writeback ioend completion
bio 완료와 `iomap_ioend` 생성필요 시 workqueue로 batch 이동`iomap_sort_ioends`로 offset 정렬`iomap_ioend_try_merge`로 호환되는 연속 ioend 병합filesystem metadata update 수행`iomap_finish_ioends`로 folio writeback 완료

완료 회계를 정렬·병합해 metadata transaction 비용을 나눕니다.

Pagecache Writeback Completion
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To handle the bookkeeping that must happen after disk I/O for writeback
completes, iomap creates chains of ``struct iomap_ioend`` objects that
wrap the ``bio`` that is used to write pagecache data to disk.
By default, iomap finishes writeback ioends by clearing the writeback
bit on the folios attached to the ``ioend``.
If the write failed, it will also set the error bits on the folios and
the address space.
This can happen in interrupt or process context, depending on the
storage device.
Filesystems that need to update internal bookkeeping (e.g. unwritten
extent conversions) should set their own bi_end_io on the bios
submitted by ``->submit_writeback``
This function should call ``iomap_finish_ioends`` after finishing its
own work (e.g. unwritten extent conversion).

Some filesystems may wish to `amortize the cost of running metadata
transactions
<https://lore.kernel.org/all/20220120034733.221737-1-david@fromorbit.com/>`_
for post-writeback updates by batching them.
They may also require transactions to run from process context, which
implies punting batches to a workqueue.
iomap ioends contain a ``list_head`` to enable batching.

Given a batch of ioends, iomap has a few helpers to assist with
amortization:

 * ``iomap_sort_ioends``: Sort all the ioends in the list by file
   offset.

 * ``iomap_ioend_try_merge``: Given an ioend that is not in any list and
   a separate list of sorted ioends, merge as many of the ioends from
   the head of the list into the given ioend.
   ioends can only be merged if the file range and storage addresses are
   contiguous; the unwritten and shared status are the same; and the
   write I/O outcome is the same.
   The merged ioends become their own list.

 * ``iomap_finish_ioends``: Finish an ioend that possibly has other
   ioends linked to it.

Direct I/O entry point와 실행 flag

374-461

Linux에서 direct I/O는 pagecache를 우회해 storage에 직접 발행하는 file I/O입니다. `iomap_dio_rw`는 file의 `O_DIRECT` read와 write를 구현하며 `iocb`, `iov_iter`, mapping `ops`, 선택적 direct I/O `dops`, `dio_flags`, private data, `done_before`를 받습니다.

파일시스템은 storage I/O 제출 전후에 추가 작업이 필요하면 `dops`를 제공합니다. `done_before`는 이미 전송된 요청 byte 수입니다. 요청 일부가 동기적으로 끝난 뒤 나머지를 비동기로 이어갈 때 사용하며, `iomap_dio_rw` 호출 전에 같은 `iocb`의 write를 시작했다면 설정해야 합니다. I/O 방향은 전달된 `iocb`에서 결정합니다.

`IOMAP_DIO_FORCE_WAIT`는 `kiocb`가 비동기여도 I/O 완료를 기다립니다.

`IOMAP_DIO_OVERWRITE_ONLY`는 해당 범위를 pure overwrite로 수행하거나 `-EAGAIN`으로 실패하게 합니다. 복잡한 unaligned I/O write 경로를 가진 파일시스템이 정렬되지 않은 write의 최적화 fast path로 사용할 수 있습니다.

pure overwrite가 가능하면 stale data 노출이나 data loss 위험이 없어 같은 fsblock에 대한 다른 I/O와 직렬화할 필요가 없습니다. 불가능하면 파일시스템은 해당 unaligned 범위의 exclusive access를 위한 직렬화를 수행해 allocation과 sub-block zeroing을 안전하게 처리할 수 있습니다. locking contention을 줄일 수 있지만 올바른 구현에는 상세한 검사가 많이 필요합니다.

`IOMAP_DIO_PARTIAL`은 page fault가 발생하면 지금까지의 진행량을 반환합니다. 호출자는 fault를 처리하고 재시도할 수 있으며, 재시도하면 이전 모든 호출의 누적 반환값을 다음 `done_before`로 넘겨야 합니다.

Direct I/O에서 `IOCB_NOWAIT`는 `IOMAP_NOWAIT`를 켭니다. `IOCB_SYNC`는 호출 완료 전에 device가 data를 disk에 영속화하도록 하며 pure overwrite는 FUA를 켜 발행할 수 있습니다. `IOCB_HIPRI`는 interrupt를 기다리지 않고 completion을 polling하며, 비동기 I/O이면서 전체 I/O를 단일 `struct bio`로 발행할 수 있을 때만 의미가 있습니다.

`IOCB_DIO_CALLER_COMP`는 호출자 process context에서 I/O completion을 실행하려고 시도하며 자세한 내용은 `linux/fs.h`에 있습니다.

파일시스템은 `->read_iter`와 `->write_iter`에서 `iomap_dio_rw`를 호출하고 file의 `->open`에서 `FMODE_CAN_ODIRECT`를 설정해야 합니다. deprecated된 `->direct_IO`는 설정하면 안 됩니다.

direct I/O completion 전에 자체 작업을 하려면 `__iomap_dio_rw`를 호출합니다. 반환값이 error pointer도 NULL도 아니면 내부 작업을 마친 뒤 그 값을 `iomap_dio_complete`에 전달해야 합니다.

`iomap_dio_rw` 실행 flag
Flag동작중요 조건
`IOMAP_DIO_FORCE_WAIT`비동기 요청도 완료 대기`kiocb` mode와 무관
`IOMAP_DIO_OVERWRITE_ONLY`pure overwrite 또는 `-EAGAIN`allocation·zeroing 없는 범위
`IOMAP_DIO_PARTIAL`page fault 전 진행량 반환재시도 때 누적 `done_before` 전달

대기, overwrite fast path, partial retry를 제어합니다.

Partial direct I/O retry
첫 `iomap_dio_rw` 호출요청 일부 전송page fault 또는 비동기 전환완료 byte 수 누적다음 호출의 `done_before`로 전달나머지 범위 계속

동기 완료량을 잃지 않고 비동기 또는 fault 재시도를 이어갑니다.

Direct I/O
==========

In Linux, direct I/O is defined as file I/O that is issued directly to
storage, bypassing the pagecache.
The ``iomap_dio_rw`` function implements O_DIRECT (direct I/O) reads and
writes for files.

.. code-block:: c

 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
                      const struct iomap_ops *ops,
                      const struct iomap_dio_ops *dops,
                      unsigned int dio_flags, void *private,
                      size_t done_before);

The filesystem can provide the ``dops`` parameter if it needs to perform
extra work before or after the I/O is issued to storage.
The ``done_before`` parameter tells the how much of the request has
already been transferred.
It is used to continue a request asynchronously when `part of the
request
<https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c03098d4b9ad76bca2966a8769dcfe59f7f85103>`_
has already been completed synchronously.

The ``done_before`` parameter should be set if writes for the ``iocb``
have been initiated prior to the call.
The direction of the I/O is determined from the ``iocb`` passed in.

The ``dio_flags`` argument can be set to any combination of the
following values:

 * ``IOMAP_DIO_FORCE_WAIT``: Wait for the I/O to complete even if the
   kiocb is not synchronous.

 * ``IOMAP_DIO_OVERWRITE_ONLY``: Perform a pure overwrite for this range
   or fail with ``-EAGAIN``.
   This can be used by filesystems with complex unaligned I/O
   write paths to provide an optimised fast path for unaligned writes.
   If a pure overwrite can be performed, then serialisation against
   other I/Os to the same filesystem block(s) is unnecessary as there is
   no risk of stale data exposure or data loss.
   If a pure overwrite cannot be performed, then the filesystem can
   perform the serialisation steps needed to provide exclusive access
   to the unaligned I/O range so that it can perform allocation and
   sub-block zeroing safely.
   Filesystems can use this flag to try to reduce locking contention,
   but a lot of `detailed checking
   <https://lore.kernel.org/linux-ext4/20230314130759.642710-1-bfoster@redhat.com/>`_
   is required to do it `correctly
   <https://lore.kernel.org/linux-ext4/20230810165559.946222-1-bfoster@redhat.com/>`_.

 * ``IOMAP_DIO_PARTIAL``: If a page fault occurs, return whatever
   progress has already been made.
   The caller may deal with the page fault and retry the operation.
   If the caller decides to retry the operation, it should pass the
   accumulated return values of all previous calls as the
   ``done_before`` parameter to the next call.

These ``struct kiocb`` flags are significant for direct I/O with iomap:

 * ``IOCB_NOWAIT``: Turns on ``IOMAP_NOWAIT``.

 * ``IOCB_SYNC``: Ensure that the device has persisted data to disk
   before completing the call.
   In the case of pure overwrites, the I/O may be issued with FUA
   enabled.

 * ``IOCB_HIPRI``: Poll for I/O completion instead of waiting for an
   interrupt.
   Only meaningful for asynchronous I/O, and only if the entire I/O can
   be issued as a single ``struct bio``.

 * ``IOCB_DIO_CALLER_COMP``: Try to run I/O completion from the caller's
   process context.
   See ``linux/fs.h`` for more details.

Filesystems should call ``iomap_dio_rw`` from ``->read_iter`` and
``->write_iter``, and set ``FMODE_CAN_ODIRECT`` in the ``->open``
function for the file.
They should not set ``->direct_IO``, which is deprecated.

If a filesystem wishes to perform its own work before direct I/O
completion, it should call ``__iomap_dio_rw``.
If its return value is not an error pointer or a NULL pointer, the
filesystem should pass the return value to ``iomap_dio_complete`` after
finishing its internal work.

Direct I/O 반환값

462-479

`iomap_dio_rw`는 전송한 byte 수를 나타내는 0 이상의 값을 반환할 수 있습니다.

`-ENOTBLK`는 buffered I/O로 fallback하라는 뜻입니다. storage에 I/O를 내기 전 pagecache를 invalidate할 수 없으면 iomap 자체가 반환하며 `->iomap_begin`이나 `->iomap_end`도 반환할 수 있습니다.

`-EIOCBQUEUED`는 비동기 direct I/O 요청이 queue에 들어갔고 별도로 완료될 것임을 뜻합니다. 그 밖의 음수 error code도 반환할 수 있습니다.

`iomap_dio_rw` 반환
반환값의미
`>= 0`전송된 byte 수
`-ENOTBLK`buffered I/O로 fallback
`-EIOCBQUEUED`비동기 요청 queue 완료 예정
기타 음수해당 errno 오류

완료량, fallback, 비동기 queue 상태를 구분합니다.

Return Values
-------------

``iomap_dio_rw`` can return one of the following:

 * A non-negative number of bytes transferred.

 * ``-ENOTBLK``: Fall back to buffered I/O.
   iomap itself will return this value if it cannot invalidate the page
   cache before issuing the I/O to storage.
   The ``->iomap_begin`` or ``->iomap_end`` functions may also return
   this value.

 * ``-EIOCBQUEUED``: The asynchronous direct I/O request has been
   queued and will be completed separately.

 * Any of the other negative error codes.

Direct read

480-494

Direct I/O read는 storage device에서 호출자 buffer로 read I/O를 시작합니다. 시작하기 전에 pagecache의 dirty 부분을 storage로 flush합니다.

`->iomap_begin`의 기본 flag는 `IOMAP_DIRECT`이고 앞서 정의한 `IOMAP_NOWAIT`를 조합할 수 있습니다. 호출자는 보통 이 함수 전에 `i_rwsem`을 shared mode로 잡습니다.

Direct Reads
------------

A direct I/O read initiates a read I/O from the storage device to the
caller's buffer.
Dirty parts of the pagecache are flushed to storage before initiating
the read io.
The ``flags`` value for ``->iomap_begin`` will be ``IOMAP_DIRECT`` with
any combination of the following enhancements:

 * ``IOMAP_NOWAIT``, as defined previously.

Callers commonly hold ``i_rwsem`` in shared mode before calling this
function.

Direct write와 atomic write

495-548

Direct I/O write는 호출자 buffer에서 storage device로 write I/O를 시작합니다. 시작 전 pagecache의 dirty 부분을 storage로 flush하고, write 전후 모두 pagecache를 invalidate합니다. `->iomap_begin`의 기본 flag는 `IOMAP_DIRECT | IOMAP_WRITE`이며 `IOMAP_NOWAIT` 등을 조합할 수 있습니다.

`IOMAP_OVERWRITE_ONLY`에서는 block allocation과 partial block zeroing을 허용하지 않습니다. 전체 file range가 하나의 written 또는 unwritten extent에 mapping되어야 합니다. mapping이 unwritten이고 파일시스템이 stale content 노출 없이 비정렬 영역을 zeroing할 수 없다면 I/O 범위도 fsblock 크기에 정렬해야 합니다.

`IOMAP_ATOMIC`은 torn-write 보호를 적용한 write입니다. HW-offload 또는 파일시스템의 software mechanism으로 보호할 수 있습니다.

HW-offload에서는 write 전체를 단일 bio로 만들고 여러 I/O request로 나누면 안 되므로 `REQ_ATOMIC`을 설정해야 합니다. write range는 파일시스템과 underlying block device의 atomic commit alignment 요구를 모두 충족해야 합니다.

unwritten extent conversion이나 copy-on-write처럼 metadata update가 필요하면 전체 file range의 모든 update도 atomic하게 commit해야 합니다. untorn write는 fsblock 하나보다 길 수 있지만 언제나 mapping 시작 disk block의 alignment가 write offset 이상이어야 합니다. 파일시스템은 `IOMAP_F_ATOMIC_BIO`로 HW-offload 기반 untorn write임을 iomap core에 알려야 합니다.

Software mechanism 기반 untorn write에는 HW-offload의 disk block alignment와 single-bio 제한이 적용되지 않습니다. write range가 여러 extent를 덮어 single bio를 발행할 수 없는 등 HW-offload를 사용할 수 없을 때 보통 fallback으로 씁니다. 그래도 전체 range의 파일시스템 metadata update는 모두 atomic하게 commit해야 합니다.

호출자는 보통 direct write 전에 `i_rwsem`을 shared 또는 exclusive mode로 잡습니다.

Atomic direct write 방식
조건HW-offloadSoftware mechanism
bio 구성반드시 단일 bio + `REQ_ATOMIC`여러 bio 가능
alignmentfilesystem과 device atomic 요구 충족HW alignment 제한 미적용
여러 extentsingle bio가 불가능하면 사용 불가fallback으로 처리 가능
metadata update전체 range atomic commit전체 range atomic commit
iomap 표시`IOMAP_F_ATOMIC_BIO`filesystem mechanism

HW-offload와 filesystem software 보호의 제약을 비교합니다.

Direct Writes
-------------

A direct I/O write initiates a write I/O to the storage device from the
caller's buffer.
Dirty parts of the pagecache are flushed to storage before initiating
the write io.
The pagecache is invalidated both before and after the write io.
The ``flags`` value for ``->iomap_begin`` will be ``IOMAP_DIRECT |
IOMAP_WRITE`` with any combination of the following enhancements:

 * ``IOMAP_NOWAIT``, as defined previously.

 * ``IOMAP_OVERWRITE_ONLY``: Allocating blocks and zeroing partial
   blocks is not allowed.
   The entire file range must map to a single written or unwritten
   extent.
   The file I/O range must be aligned to the filesystem block size
   if the mapping is unwritten and the filesystem cannot handle zeroing
   the unaligned regions without exposing stale contents.

 * ``IOMAP_ATOMIC``: This write is being issued with torn-write
   protection.
   Torn-write protection may be provided based on HW-offload or by a
   software mechanism provided by the filesystem.

   For HW-offload based support, only a single bio can be created for the
   write, and the write must not be split into multiple I/O requests, i.e.
   flag REQ_ATOMIC must be set.
   The file range to write must be aligned to satisfy the requirements
   of both the filesystem and the underlying block device's atomic
   commit capabilities.
   If filesystem metadata updates are required (e.g. unwritten extent
   conversion or copy-on-write), all updates for the entire file range
   must be committed atomically as well.
   Untorn-writes may be longer than a single file block. In all cases,
   the mapping start disk block must have at least the same alignment as
   the write offset.
   The filesystems must set IOMAP_F_ATOMIC_BIO to inform iomap core of an
   untorn-write based on HW-offload.

   For untorn-writes based on a software mechanism provided by the
   filesystem, all the disk block alignment and single bio restrictions
   which apply for HW-offload based untorn-writes do not apply.
   The mechanism would typically be used as a fallback for when
   HW-offload based untorn-writes may not be issued, e.g. the range of the
   write covers multiple extents, meaning that it is not possible to issue
   a single bio.
   All filesystem metadata updates for the entire file range must be
   committed atomically as well.

Callers commonly hold ``i_rwsem`` in shared or exclusive mode before
calling this function.

`struct iomap_dio_ops`와 bio completion

549-592

원문 heading은 backtick 안에 colon까지 포함한 `struct iomap_dio_ops:`로 되어 있습니다. 구조체는 `submit_io`, `end_io`, `bio_set` field를 가집니다.

`submit_io`는 iomap이 요청 I/O용 `struct bio`를 만들고 block device에 제출하려 할 때 호출합니다. 제공하지 않으면 `submit_bio`를 직접 호출합니다. btrfs data replication처럼 제출 전에 추가 작업이 필요한 파일시스템이 구현합니다.

`end_io`는 `struct bio` 완료 후 호출됩니다. unwritten extent mapping의 post-write conversion, write failure 처리 등을 수행해야 합니다.

`end_io`의 `flags`에는 `IOMAP_DIO_UNWRITTEN`과 `IOMAP_DIO_COW`를 조합할 수 있습니다. UNWRITTEN이면 ioend가 extent를 written으로 표시해야 하고, COW이면 mapping 공간에 대한 write에 copy-on-write가 필요했으므로 ioend가 mapping을 전환해야 합니다.

`bio_set`은 파일시스템이 direct I/O bio 할당용 custom bio_set을 제공해 private per-bio 정보를 저장할 수 있게 합니다. NULL이면 generic `struct bio`를 사용합니다.

I/O completion 뒤 추가 작업이 필요한 파일시스템은 `->submit_io`에서 custom `->bi_end_io`를 설정해야 합니다. custom endio 함수는 마지막에 `iomap_dio_bio_end_io`를 호출해 direct I/O를 완료해야 합니다.

Direct I/O bio hook
iomap이 `struct bio` 구성선택적 `submit_io`에서 replication·private 작업block device에 bio 제출선택적 custom `bi_end_io`와 `end_io` 처리unwritten conversion 또는 COW mapping 전환`iomap_dio_bio_end_io`로 generic 완료

filesystem custom 작업이 generic iomap completion을 감쌉니다.

``struct iomap_dio_ops:``
-------------------------
.. code-block:: c

 struct iomap_dio_ops {
     void (*submit_io)(const struct iomap_iter *iter, struct bio *bio,
                       loff_t file_offset);
     int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
                   unsigned flags);
     struct bio_set *bio_set;
 };

The fields of this structure are as follows:

  - ``submit_io``: iomap calls this function when it has constructed a
    ``struct bio`` object for the I/O requested, and wishes to submit it
    to the block device.
    If no function is provided, ``submit_bio`` will be called directly.
    Filesystems that would like to perform additional work before (e.g.
    data replication for btrfs) should implement this function.

  - ``end_io``: This is called after the ``struct bio`` completes.
    This function should perform post-write conversions of unwritten
    extent mappings, handle write failures, etc.
    The ``flags`` argument may be set to a combination of the following:

    * ``IOMAP_DIO_UNWRITTEN``: The mapping was unwritten, so the ioend
      should mark the extent as written.

    * ``IOMAP_DIO_COW``: Writing to the space in the mapping required a
      copy on write operation, so the ioend should switch mappings.

  - ``bio_set``: This allows the filesystem to provide a custom bio_set
    for allocating direct I/O bios.
    This enables filesystems to `stash additional per-bio information
    <https://lore.kernel.org/all/20220505201115.937837-3-hch@lst.de/>`_
    for private use.
    If this field is NULL, generic ``struct bio`` objects will be used.

Filesystems that want to perform extra work after an I/O completion
should set a custom ``->bi_end_io`` function via ``->submit_io``.
Afterwards, the custom endio function must call
``iomap_dio_bio_end_io`` to finish the direct I/O.

DAX I/O와 fsdax read

593-612

일부 storage device는 memory로 직접 mapping할 수 있습니다. 이 device는 CPU와 memory controller를 통한 load와 store를 허용하는 `fsdax` access mode를 지원합니다.

fsdax read는 storage device에서 호출자 buffer로 `memcpy`합니다. `->iomap_begin`에는 `IOMAP_DAX`와 선택적 `IOMAP_NOWAIT`가 전달됩니다. 호출자는 보통 `i_rwsem`을 shared mode로 잡습니다.

DAX I/O
=======

Some storage devices can be directly mapped as memory.
These devices support a new access mode known as "fsdax" that allows
loads and stores through the CPU and memory controller.

fsdax Reads
-----------

A fsdax read performs a memcpy from storage device to the caller's
buffer.
The ``flags`` value for ``->iomap_begin`` will be ``IOMAP_DAX`` with any
combination of the following enhancements:

 * ``IOMAP_NOWAIT``, as defined previously.

Callers commonly hold ``i_rwsem`` in shared mode before calling this
function.

fsdax write

613-634

fsdax write는 호출자 buffer에서 storage device로 `memcpy`합니다. `->iomap_begin`에는 `IOMAP_DAX | IOMAP_WRITE`와 선택적 `IOMAP_NOWAIT` 또는 `IOMAP_OVERWRITE_ONLY`가 전달됩니다.

`IOMAP_OVERWRITE_ONLY`는 해당 mapping에서 pure overwrite를 요구합니다. 파일시스템 extent mapping이 이미 `IOMAP_MAPPED`로 존재하고 write I/O 요청 전체 범위를 덮어야 합니다. iomap이 pure overwrite를 수행할 수 있는 mapping을 만들지 못하면 `-EAGAIN`으로 실패해야 합니다.

호출자는 보통 fsdax write 전에 `i_rwsem`을 exclusive mode로 잡습니다.

fsdax Writes
------------

A fsdax write initiates a memcpy to the storage device from the caller's
buffer.
The ``flags`` value for ``->iomap_begin`` will be ``IOMAP_DAX |
IOMAP_WRITE`` with any combination of the following enhancements:

 * ``IOMAP_NOWAIT``, as defined previously.

 * ``IOMAP_OVERWRITE_ONLY``: The caller requires a pure overwrite to be
   performed from this mapping.
   This requires the filesystem extent mapping to already exist as an
   ``IOMAP_MAPPED`` type and span the entire range of the write I/O
   request.
   If the filesystem cannot map this request in a way that allows the
   iomap infrastructure to perform a pure overwrite, it must fail the
   mapping operation with ``-EAGAIN``.

Callers commonly hold ``i_rwsem`` in exclusive mode before calling this
function.

fsdax fault·범위 연산·deduplication

635-668

`dax_iomap_fault`는 fsdax storage의 read와 write fault를 처리합니다. read fault의 begin flag는 `IOMAP_DAX | IOMAP_FAULT`, write fault는 여기에 `IOMAP_WRITE`를 더합니다. 호출자는 보통 대응하는 iomap pagecache 함수와 같은 lock을 잡습니다.

fsdax file에서는 pagecache I/O 대응 함수를 대신해 `dax_file_unshare`, `dax_zero_range`, `dax_truncate_page`를 제공합니다. begin flag는 pagecache 대응 함수와 같고 `IOMAP_DAX`를 추가합니다. lock도 대응 함수와 동일합니다.

`FIDEDUPERANGE` ioctl을 구현하는 파일시스템은 자체 iomap read ops와 함께 `dax_remap_file_range_prep`을 호출해야 합니다.

fsdax operation 대응
연산fsdax API핵심 flag
read fault`dax_iomap_fault``IOMAP_DAX | IOMAP_FAULT`
write fault`dax_iomap_fault`위 flag + `IOMAP_WRITE`
unshare`dax_file_unshare`pagecache flag + `IOMAP_DAX`
zero`dax_zero_range`pagecache flag + `IOMAP_DAX`
truncate`dax_truncate_page`pagecache flag + `IOMAP_DAX`
dedupe`dax_remap_file_range_prep`filesystem iomap read ops

Pagecache operation에 `IOMAP_DAX` 경로를 대응시킵니다.

fsdax mmap Faults
~~~~~~~~~~~~~~~~~

The ``dax_iomap_fault`` function handles read and write faults to fsdax
storage.
For a read fault, ``IOMAP_DAX | IOMAP_FAULT`` will be passed as the
``flags`` argument to ``->iomap_begin``.
For a write fault, ``IOMAP_DAX | IOMAP_FAULT | IOMAP_WRITE`` will be
passed as the ``flags`` argument to ``->iomap_begin``.

Callers commonly hold the same locks as they do to call their iomap
pagecache counterparts.

fsdax Truncation, fallocate, and Unsharing
------------------------------------------

For fsdax files, the following functions are provided to replace their
iomap pagecache I/O counterparts.
The ``flags`` argument to ``->iomap_begin`` are the same as the
pagecache counterparts, with ``IOMAP_DAX`` added.

 * ``dax_file_unshare``
 * ``dax_zero_range``
 * ``dax_truncate_page``

Callers commonly hold the same locks as they do to call their iomap
pagecache counterparts.

fsdax Deduplication
-------------------

Filesystems implementing the ``FIDEDUPERANGE`` ioctl must call the
``dax_remap_file_range_prep`` function with their own iomap read ops.

`SEEK_DATA`와 `SEEK_HOLE`

669-704

iomap은 `llseek` system call의 두 iterating whence mode를 구현합니다.

`iomap_seek_data`는 `SEEK_DATA`를 구현하며 `->iomap_begin`에 `IOMAP_REPORT`를 전달합니다. unwritten mapping에서는 pagecache를 검색하고, folio가 mapping되어 있으며 그 안의 fsblock이 uptodate인 pagecache 영역을 data로 보고합니다. 호출자는 보통 `i_rwsem`을 shared mode로 잡습니다.

`iomap_seek_hole`은 `SEEK_HOLE`을 구현하고 같은 `IOMAP_REPORT` flag를 사용합니다. unwritten mapping에서 folio가 mapping되지 않았거나 folio 안에 `!uptodate` fsblock이 있는 pagecache 영역을 sparse hole로 보고합니다. 호출자는 보통 `i_rwsem` shared mode를 잡습니다.

Unwritten mapping의 seek 판정
WhencePagecache 조건보고 결과
`SEEK_DATA`folio mapped + fsblock uptodatedata area
`SEEK_HOLE`folio 없음 또는 fsblock `!uptodate`sparse hole

Pagecache folio와 fsblock 상태로 data와 hole을 구분합니다.

Seeking Files
=============

iomap implements the two iterating whence modes of the ``llseek`` system
call.

SEEK_DATA
---------

The ``iomap_seek_data`` function implements the SEEK_DATA "whence" value
for llseek.
``IOMAP_REPORT`` will be passed as the ``flags`` argument to
``->iomap_begin``.

For unwritten mappings, the pagecache will be searched.
Regions of the pagecache with a folio mapped and uptodate fsblocks
within those folios will be reported as data areas.

Callers commonly hold ``i_rwsem`` in shared mode before calling this
function.

SEEK_HOLE
---------

The ``iomap_seek_hole`` function implements the SEEK_HOLE "whence" value
for llseek.
``IOMAP_REPORT`` will be passed as the ``flags`` argument to
``->iomap_begin``.

For unwritten mappings, the pagecache will be searched.
Regions of the pagecache with no folio mapped, or a !uptodate fsblock
within a folio will be reported as sparse hole areas.

Callers commonly hold ``i_rwsem`` in shared mode before calling this
function.

Swap file 활성화

705-717

`iomap_swapfile_activate`는 파일에서 base-page에 정렬된 모든 영역을 찾아 swap space로 설정합니다. 활성화 전에 파일을 `fsync()`합니다.

`->iomap_begin`에는 `IOMAP_REPORT`가 전달됩니다. 모든 mapping은 mapped 또는 unwritten이어야 하고 dirty나 shared이면 안 되며 여러 block device에 걸칠 수 없습니다.

호출자는 `i_rwsem`을 exclusive mode로 잡아야 하며 `swapon`이 이미 이를 제공합니다.

Swap File Activation
====================

The ``iomap_swapfile_activate`` function finds all the base-page aligned
regions in a file and sets them up as swap space.
The file will be ``fsync()``'d before activation.
``IOMAP_REPORT`` will be passed as the ``flags`` argument to
``->iomap_begin``.
All mappings must be mapped or unwritten; cannot be dirty or shared, and
cannot span multiple block devices.
Callers must hold ``i_rwsem`` in exclusive mode; this is already
provided by ``swapon``.

FIEMAP과 deprecated FIBMAP

718-743

iomap은 file space mapping을 보고하는 system call 가운데 두 가지를 구현합니다.

`iomap_fiemap`은 `FS_IOC_FIEMAP` ioctl이 지정한 형식으로 file extent mapping을 사용자 공간에 내보냅니다. `->iomap_begin`에는 `IOMAP_REPORT`가 전달되고 호출자는 보통 `i_rwsem`을 shared mode로 잡습니다.

`iomap_bmap`은 FIBMAP을 구현하며 호출 규약은 FIEMAP과 같습니다. iomap 전환 전부터 FIBMAP을 구현했던 파일시스템의 호환성 유지를 위해서만 제공합니다.

FIBMAP ioctl은 deprecated되었으므로 기존에 없는 파일시스템에 새 구현을 추가하면 안 됩니다. 호출자가 `i_rwsem`을 shared mode로 잡아야 할 가능성이 높지만 원문은 이 부분이 불분명하다고 명시합니다.

File space mapping 보고 API
APIiomap 함수상태
`FS_IOC_FIEMAP``iomap_fiemap`현행 extent reporting
FIBMAP`iomap_bmap`deprecated, 새 구현 추가 금지

현행 FIEMAP과 호환성용 FIBMAP을 구분합니다.

File Space Mapping Reporting
============================

iomap implements two of the file space mapping system calls.

FS_IOC_FIEMAP
-------------

The ``iomap_fiemap`` function exports file extent mappings to userspace
in the format specified by the ``FS_IOC_FIEMAP`` ioctl.
``IOMAP_REPORT`` will be passed as the ``flags`` argument to
``->iomap_begin``.
Callers commonly hold ``i_rwsem`` in shared mode before calling this
function.

FIBMAP (deprecated)
-------------------

``iomap_bmap`` implements FIBMAP.
The calling conventions are the same as for FIEMAP.
This function is only provided to maintain compatibility for filesystems
that implemented FIBMAP prior to conversion.
This ioctl is deprecated; do **not** add a FIBMAP implementation to
filesystems that do not have it.
Callers should probably hold ``i_rwsem`` in shared mode before calling
this function, but this is unclear.