﻿# Atomic file writes protect one bounded range from tearing

RWF_ATOMIC needs reported file limits, alignment and direct-I/O support. Its torn-write protection does not create a whole-checkpoint transaction or durability ordering.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/linux-rwf-atomic-torn-write-boundary/
Language: en

## Untorn is a property of a particular write

A checkpoint format stores a small header whose partial update would make recovery ambiguous. Atomic file writes can protect a supported bounded range from a mixture of old and new data after failure. They do not automatically make all payload files and the final publication one transaction.

The [pwritev2 reference](https://man7.org/linux/man-pages/man2/pwritev2.2.html) defines RWF_ATOMIC for torn-write protection on the range written by that call. The design must identify that range and its recovery meaning before choosing the interface.

## Query the actual file limits

Inspect the reported atomic-write capability and limits for the actual file using the documented statx interface. Preserve the minimum and maximum write unit and segment limit. A device advertising hardware support does not establish that the mounted filesystem and selected file expose the required operation.

The total write length must satisfy the reported bounds and power-of-two requirement, and the offset must be naturally aligned to that length. These conditions add to direct-I/O requirements; satisfying ordinary buffer alignment is not sufficient evidence of atomic-write eligibility.

## Check filesystem and device prerequisites

The [ext4 atomic-write documentation](https://docs.kernel.org/filesystems/ext4/atomic_writes.html) requires direct I/O, extents and underlying hardware support. Multi-block support has additional bigalloc and layout constraints. Do not infer installed support from the current documentation branch or a kernel version number without checking the actual file's interface.

This is not an instruction to reformat a working filesystem. Review capability on a disposable environment or an existing qualified filesystem. Changing filesystem format and evaluating a write flag are separate operations with very different rollback boundaries.

## Separate tearing from persistence

RWF_ATOMIC prevents an accepted atomic update from leaving a mixture within its protected range under the documented failure model. The pwritev2 manual separately specifies synchronized-I/O requirements for persistence consistency. Atomicity is therefore not a substitute for waiting for the required durability operation.

Earlier payload writes also need their own ordering argument. A valid old-or-new header can still point at an incomplete payload if the application publishes it too early. Recovery must validate generation identity and component completeness rather than accepting a structurally intact marker alone.

## Evidence for an atomic-range design

Acceptance includes capability observations, eligible length and offset, supported segment count, returned result and the chosen synchronization path. Negative fixtures should confirm that unsupported or misaligned requests are handled as failures, without silently falling back to an ordinary write while retaining the atomicity claim.

[Direct-I/O alignment](/journal/guide-storage-direct-io-alignment/) supplies the related buffer and path constraints. [File publication durability](/journal/guide-storage-fsync-directory/) explains the separate file and directory boundary when the completed checkpoint is named or renamed.

## Keep the guarantee bounded

The protected call does not provide cross-file atomicity, concurrency control over unrelated writers or application-level correctness. A small atomic header is useful only when the rest of the format has a defensible recovery protocol. Record exactly which range has torn-write protection and which states recovery may accept, leaving unsupported whole-checkpoint guarantees out of the design.
