ioctl_ficlonerange(2)
ioctl_ficlonerange(2) π
okay bestie, gather round, because this is one of those pages from the System Calls Manual that sounds scary but is actually iconic once you get the vibe. we're talking ioctl_ficlonerange and ioctl_ficlone β yes, they share a page, they're a package deal, they're giving duo energy β¨
πΈ NAME πΈ
ioctl_ficlonerange, ioctl_ficlone β share some the data of one file with another file
π LIBRARY π
Standard C library (libc, -lc)
β¨ SYNOPSIS β¨
#include <linux/fs.h> /* Definition of FICLONE* constants */
#include <sys/ioctl.h>
int ioctl(int dest_fd, FICLONERANGE, struct file_clone_range *arg);
int ioctl(int dest_fd, FICLONE, int src_fd);
π DESCRIPTION π
so here's the tea π« if your filesystem supports files sharing physical storage between multiple files (they call it "reflink"), then this ioctl(2) operation lets you make some of the data in the src_fd file show up in the dest_fd file by literally sharing the underlying storage. that's way faster than making a whole separate physical copy of the data, no cap. but listen: both files must live within the same filesystem. same house, same rules, periodt.
and if a write ever happens to a shared region? the filesystem has to make sure those changes stay private to the file being written. this behavior is commonly referred to as "copy on write" and it's the whole main character of this page π
okay so the mechanics: this ioctl reflinks up to src_length bytes from file descriptor src_fd at offset src_offset into the file dest_fd at offset dest_offset β provided that both are files, obviously. and if src_length is zero? then the ioctl reflinks to the end of the source file. cute, right? all this info gets conveyed in a structure that looks like this:
struct file_clone_range {
__s64 src_fd;
__u64 src_offset;
__u64 src_length;
__u64 dest_offset;
};
also very important: clones are atomic with regards to concurrent writes, so no locks need to be taken to obtain a consistent cloned copy. slay, no lock drama π
and the FICLONE ioctl? it clones entire files. simple as that.
β RETURN VALUE β
on error, -1 is returned, and errno is set to indicate the error. that's the whole thing bestie.
π ERRORS π
error codes can be one of, but are not limited to, the following (yes, "not limited to", the universe is chaotic):
EBADFβsrc_fdis not open for reading;dest_fdis not open for writing or is open for append-only writes; or the filesystem whichsrc_fdresides on does not support reflink. basically somebody didn't have their permissions together πEINVALβ the filesystem does not support reflinking the ranges of the given files. this error can also appear if either file descriptor represents a device, FIFO, or socket. and heads up: disk filesystems generally require the offset and length arguments to be aligned to the fundamental block size. also, XFS and Btrfs do not support overlapping reflink ranges in the same file. ngl that one bites people.EISDIRβ one of the files is a directory and the filesystem does not support shared regions in directories.EOPNOTSUPPβ this can appear if the filesystem does not support reflinking either file descriptor, or if either file descriptor refers to special inodes.EPERMβdest_fdis immutable. it said no changes, and it meant it πETXTBSYβ one of the files is a swap file. swap files cannot share storage, sorry bestie.EXDEVβdest_fdandsrc_fdare not on the same mounted filesystem. different filesystems, different worlds, can't reflink across the border π¦
π STANDARDS π
Linux.
π HISTORY π
Linux 4.5.
they were previously known as BTRFS_IOC_CLONE and BTRFS_IOC_CLONE_RANGE, and were private to Btrfs. glow-up era, honestly.
π NOTES π
because a copy-on-write operation requires the allocation of new storage, the fallocate(2) operation may unshare shared blocks to guarantee that subsequent writes will not fail because of lack of disk space. so if you're planning ahead, that's your move β¨
π§ SEE ALSO π§
and that's the whole page, bestie, you ate it π«Ά this was Linux man-pages 6.7, dated 2023-10-31, and yes it's really called ioctl_ficlonerange(2) β go forth and reflink responsibly π
π¦ where this page comes from
- ubuntu noble: manpages-dev 6.7-2
- π the page's license: GPL-2.0-or-later
- Copyright (c) 2016, Oracle. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms