go-linux

file design decisions

Part of the accepted-design index.

The usage guide owns operations and caller examples; verification owns execution evidence and source-reviewed limits.

Extend standard files without another owner

os.File already supplies ordinary I/O and lifetime management. os.Root supplies common confined filesystem operations, but not the complete openat2 resolution controls, statx validity/attribute masks, or rename exchange/no-replace modes. This package adds those operations while borrowing and returning standard files. A new file owner or filesystem abstraction would duplicate existing Go behavior without improving these contracts.

Preserve the requested kernel operation

Open passes names unchanged. Cleaning a path in userspace can change symlink/parent traversal or trailing-slash semantics; retrying without a restriction could violate the caller’s security policy. Cached lookup failure remains observable for the same reason. Rename uses one renameat2 operation instead of racy existence checks or multi-step exchange emulation. Open supplies lookup restrictions. StatAt, Link, and Rename use ordinary path resolution.

Borrowed descriptors are pinned for the syscall rather than duplicated and retained. Nested lifetime references permit Rename to use the same anchor twice without introducing lock-order synchronization. This protects identity against concurrent close but does not interrupt blocking filesystem work. See the private borrowing boundary.

Unnamed creation and direct and synchronous I/O use the existing openat2 binding so callers can select those kernel flags with the same lookup restrictions. Publishing uses the maintained linkat binding with AT_EMPTY_PATH. Linux 7.0 checks open-time credentials or capability against the source handle, so publication can use that handle directly without a procfs path dependency. Direct-I/O alignment stays with the filesystem and the caller’s buffer allocation. Linux 7.0 open implementation · Linux 7.0 link implementation

Whiteout renames extend the existing single-syscall modes to support layer updates. The kernel can create the marker in the same operation, including when the caller prohibits replacement. Creating a device node separately would leave an observable interval between operations and apply a different permission check. Linux 7.0 XFS rename implementation

Fallocate uses the maintained x/sys binding with the same lifetime protection. One typed mode represents the kernel operation and its permitted size modifier, so a caller can reserve log capacity without inadvertently growing the visible log. The wrapper checks mode combinations and signed range arithmetic before borrowing. It leaves alignment and file-state checks to the filesystem, where the relevant allocation units and EOF are known. Forwarding one request preserves distinctions such as storage reserved by zero-range and storage released by hole punching. Linux 7.0 VFS implementation

Reflink unsharing uses the same operation and size modifier. Rewriting bytes in userspace would trigger ordinary writes, with their own failure and concurrency behavior. Letting the filesystem copy shared extents preserves its allocation rules and leaves support errors visible to the caller. Linux 7.0 XFS implementation

Write-zeroes uses the same binding with its own exclusive allocation mode. Ext4 prepares written mappings for subsequent overwrites. Ordinary zero-range can leave unwritten extents that require conversion on the next write. Forwarding the mode directly preserves backing-device errors and lets the filesystem handle partial blocks. Linux 7.0 ext4 implementation

Access advice and readahead keep their distinct kernel contracts. Readahead requires readable access and kernel support for advance reads on the underlying file. Linux checks those prerequisites before using its advice machinery. Using the dedicated syscall preserves that validation and its errno. The other operations use the maintained x/sys bindings, while readahead uses the x/sys syscall number for the supported native ABIs. Linux 7.0 readahead implementation

Data synchronization and filesystem-wide synchronization have separate entry points so the scope of a write-ahead log flush or filesystem checkpoint is explicit. Filesystem-wide synchronization can surface errors involving other open files on the same filesystem. The standard library’s File.Sync remains the full file synchronization operation. Linux 7.0 synchronization implementation

Whole-file locks borrow the caller’s file for a single flock request through the maintained x/sys binding. Keeping a private duplicate would extend the open file description’s lifetime and could keep a lock alive after the caller closes their file. Forwarding conversion as one request preserves the kernel’s contention and failure behavior. Blocking acquisition uses the kernel’s wait semantics. Linux 7.0 locking implementation

Record locks use the maintained FcntlFlock binding with an explicit ownership choice on every operation. Borrowing the original descriptor preserves both the POSIX rule for closing any descriptor on the inode and the OFD rule for closing the last reference. Resolving signed ranges in the kernel avoids a separate seek or size lookup that would race with another operation. Querying and acquisition stay separate because a conflict observation cannot reserve the range. Linux 7.0 locking implementation

Keep metadata validity separate from values

Metadata retains Linux’s returned field mask and attribute-support mask. An unsupported field cannot be inferred from a zero value, and a single query is not an atomic snapshot. Unsigned Linux sizes and identifiers retain their ranges, while mode bits and valid timestamps convert to Go representations. Unknown output bits survive inspection; unknown input bits are rejected rather than silently ignored.

The maintained x/sys bindings own syscall and ABI layouts. The missing RESOLVE_CACHED constant is defined locally from the versioned UAPI. Linux 7.0 openat2 header

Extended statx observations retain the kernel’s widths, optional-field bits, and raw limit values. A caller can distinguish an unavailable query result from a reported zero, including Linux’s zero optimized limit on an inode that supports atomic writes. Separate read alignment remains independently valid so filesystems can report a weaker constraint for reads. Linux 7.0 statx header