atomic_writes.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. _atomic_writes:
  3. Atomic Block Writes
  4. -------------------------
  5. Introduction
  6. ~~~~~~~~~~~~
  7. Atomic (untorn) block writes ensure that either the entire write is committed
  8. to disk or none of it is. This prevents "torn writes" during power loss or
  9. system crashes. The ext4 filesystem supports atomic writes (only with Direct
  10. I/O) on regular files with extents, provided the underlying storage device
  11. supports hardware atomic writes. This is supported in the following two ways:
  12. 1. **Single-fsblock Atomic Writes**:
  13. EXT4 supports atomic write operations with a single filesystem block since
  14. v6.13. In this the atomic write unit minimum and maximum sizes are both set
  15. to filesystem blocksize.
  16. e.g. doing atomic write of 16KB with 16KB filesystem blocksize on 64KB
  17. pagesize system is possible.
  18. 2. **Multi-fsblock Atomic Writes with Bigalloc**:
  19. EXT4 now also supports atomic writes spanning multiple filesystem blocks
  20. using a feature known as bigalloc. The atomic write unit's minimum and
  21. maximum sizes are determined by the filesystem block size and cluster size,
  22. based on the underlying device’s supported atomic write unit limits.
  23. Requirements
  24. ~~~~~~~~~~~~
  25. Basic requirements for atomic writes in ext4:
  26. 1. The extents feature must be enabled (default for ext4)
  27. 2. The underlying block device must support atomic writes
  28. 3. For single-fsblock atomic writes:
  29. 1. A filesystem with appropriate block size (up to the page size)
  30. 4. For multi-fsblock atomic writes:
  31. 1. The bigalloc feature must be enabled
  32. 2. The cluster size must be appropriately configured
  33. NOTE: EXT4 does not support software or COW based atomic write, which means
  34. atomic writes on ext4 are only supported if underlying storage device supports
  35. it.
  36. Multi-fsblock Implementation Details
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. The bigalloc feature changes ext4 to allocate in units of multiple filesystem
  39. blocks, also known as clusters. With bigalloc each bit within block bitmap
  40. represents a cluster (power of 2 number of blocks) rather than individual
  41. filesystem blocks.
  42. EXT4 supports multi-fsblock atomic writes with bigalloc, subject to the
  43. following constraints. The minimum atomic write size is the larger of the fs
  44. block size and the minimum hardware atomic write unit; and the maximum atomic
  45. write size is smaller of the bigalloc cluster size and the maximum hardware
  46. atomic write unit. Bigalloc ensures that all allocations are aligned to the
  47. cluster size, which satisfies the LBA alignment requirements of the hardware
  48. device if the start of the partition/logical volume is itself aligned correctly.
  49. Here is the block allocation strategy in bigalloc for atomic writes:
  50. * For regions with fully mapped extents, no additional work is needed
  51. * For append writes, a new mapped extent is allocated
  52. * For regions that are entirely holes, unwritten extent is created
  53. * For large unwritten extents, the extent gets split into two unwritten
  54. extents of appropriate requested size
  55. * For mixed mapping regions (combinations of holes, unwritten extents, or
  56. mapped extents), ext4_map_blocks() is called in a loop with
  57. EXT4_GET_BLOCKS_ZERO flag to convert the region into a single contiguous
  58. mapped extent by writing zeroes to it and converting any unwritten extents to
  59. written, if found within the range.
  60. Note: Writing on a single contiguous underlying extent, whether mapped or
  61. unwritten, is not inherently problematic. However, writing to a mixed mapping
  62. region (i.e. one containing a combination of mapped and unwritten extents)
  63. must be avoided when performing atomic writes.
  64. The reason is that, atomic writes when issued via pwritev2() with the RWF_ATOMIC
  65. flag, requires that either all data is written or none at all. In the event of
  66. a system crash or unexpected power loss during the write operation, the affected
  67. region (when later read) must reflect either the complete old data or the
  68. complete new data, but never a mix of both.
  69. To enforce this guarantee, we ensure that the write target is backed by
  70. a single, contiguous extent before any data is written. This is critical because
  71. ext4 defers the conversion of unwritten extents to written extents until the I/O
  72. completion path (typically in ->end_io()). If a write is allowed to proceed over
  73. a mixed mapping region (with mapped and unwritten extents) and a failure occurs
  74. mid-write, the system could observe partially updated regions after reboot, i.e.
  75. new data over mapped areas, and stale (old) data over unwritten extents that
  76. were never marked written. This violates the atomicity and/or torn write
  77. prevention guarantee.
  78. To prevent such torn writes, ext4 proactively allocates a single contiguous
  79. extent for the entire requested region in ``ext4_iomap_alloc`` via
  80. ``ext4_map_blocks_atomic()``. EXT4 also force commits the current journalling
  81. transaction in case if allocation is done over mixed mapping. This ensures any
  82. pending metadata updates (like unwritten to written extents conversion) in this
  83. range are in consistent state with the file data blocks, before performing the
  84. actual write I/O. If the commit fails, the whole I/O must be aborted to prevent
  85. from any possible torn writes.
  86. Only after this step, the actual data write operation is performed by the iomap.
  87. Handling Split Extents Across Leaf Blocks
  88. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  89. There can be a special edge case where we have logically and physically
  90. contiguous extents stored in separate leaf nodes of the on-disk extent tree.
  91. This occurs because on-disk extent tree merges only happens within the leaf
  92. blocks except for a case where we have 2-level tree which can get merged and
  93. collapsed entirely into the inode.
  94. If such a layout exists and, in the worst case, the extent status cache entries
  95. are reclaimed due to memory pressure, ``ext4_map_blocks()`` may never return
  96. a single contiguous extent for these split leaf extents.
  97. To address this edge case, a new get block flag
  98. ``EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS flag`` is added to enhance the
  99. ``ext4_map_query_blocks()`` lookup behavior.
  100. This new get block flag allows ``ext4_map_blocks()`` to first check if there is
  101. an entry in the extent status cache for the full range.
  102. If not present, it consults the on-disk extent tree using
  103. ``ext4_map_query_blocks()``.
  104. If the located extent is at the end of a leaf node, it probes the next logical
  105. block (lblk) to detect a contiguous extent in the adjacent leaf.
  106. For now only one additional leaf block is queried to maintain efficiency, as
  107. atomic writes are typically constrained to small sizes
  108. (e.g. [blocksize, clustersize]).
  109. Handling Journal transactions
  110. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. To support multi-fsblock atomic writes, we ensure enough journal credits are
  112. reserved during:
  113. 1. Block allocation time in ``ext4_iomap_alloc()``. We first query if there
  114. could be a mixed mapping for the underlying requested range. If yes, then we
  115. reserve credits of up to ``m_len``, assuming every alternate block can be
  116. an unwritten extent followed by a hole.
  117. 2. During ``->end_io()`` call, we make sure a single transaction is started for
  118. doing unwritten-to-written conversion. The loop for conversion is mainly
  119. only required to handle a split extent across leaf blocks.
  120. How to
  121. ~~~~~~
  122. Creating Filesystems with Atomic Write Support
  123. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  124. First check the atomic write units supported by block device.
  125. See :ref:`atomic_write_bdev_support` for more details.
  126. For single-fsblock atomic writes with a larger block size
  127. (on systems with block size < page size):
  128. .. code-block:: bash
  129. # Create an ext4 filesystem with a 16KB block size
  130. # (requires page size >= 16KB)
  131. mkfs.ext4 -b 16384 /dev/device
  132. For multi-fsblock atomic writes with bigalloc:
  133. .. code-block:: bash
  134. # Create an ext4 filesystem with bigalloc and 64KB cluster size
  135. mkfs.ext4 -F -O bigalloc -b 4096 -C 65536 /dev/device
  136. Where ``-b`` specifies the block size, ``-C`` specifies the cluster size in bytes,
  137. and ``-O bigalloc`` enables the bigalloc feature.
  138. Application Interface
  139. ^^^^^^^^^^^^^^^^^^^^^
  140. Applications can use the ``pwritev2()`` system call with the ``RWF_ATOMIC`` flag
  141. to perform atomic writes:
  142. .. code-block:: c
  143. pwritev2(fd, iov, iovcnt, offset, RWF_ATOMIC);
  144. The write must be aligned to the filesystem's block size and not exceed the
  145. filesystem's maximum atomic write unit size.
  146. See ``generic_atomic_write_valid()`` for more details.
  147. ``statx()`` system call with ``STATX_WRITE_ATOMIC`` flag can provide following
  148. details:
  149. * ``stx_atomic_write_unit_min``: Minimum size of an atomic write request.
  150. * ``stx_atomic_write_unit_max``: Maximum size of an atomic write request.
  151. * ``stx_atomic_write_segments_max``: Upper limit for segments. The number of
  152. separate memory buffers that can be gathered into a write operation
  153. (e.g., the iovcnt parameter for IOV_ITER). Currently, this is always set to one.
  154. The STATX_ATTR_WRITE_ATOMIC flag in ``statx->attributes`` is set if atomic
  155. writes are supported.
  156. .. _atomic_write_bdev_support:
  157. Hardware Support
  158. ~~~~~~~~~~~~~~~~
  159. The underlying storage device must support atomic write operations.
  160. Modern NVMe and SCSI devices often provide this capability.
  161. The Linux kernel exposes this information through sysfs:
  162. * ``/sys/block/<device>/queue/atomic_write_unit_min`` - Minimum atomic write size
  163. * ``/sys/block/<device>/queue/atomic_write_unit_max`` - Maximum atomic write size
  164. Nonzero values for these attributes indicate that the device supports
  165. atomic writes.
  166. See Also
  167. ~~~~~~~~
  168. * :doc:`bigalloc` - Documentation on the bigalloc feature
  169. * :doc:`allocators` - Documentation on block allocation in ext4
  170. * Support for atomic block writes in 6.13:
  171. https://lwn.net/Articles/1009298/