FICLONE(2const)
π FICLONE(2const) π
ok so this is a section-2 const page from the Linux man-pages, and bestie, it's lowkey one of the coolest tricks in the whole kernel: file cloning. no cap.
β¨ NAME β¨
FICLONE, FICLONERANGE β share some the data of one file with another file
(yes the original really says "share some the data", we are not fixing it, we are respecting it π«Ά)
π 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);
likeβ¦ look at that. two little ioctls. absolute icons. π
π DESCRIPTION π
So here's the tea. If a filesystem supports files sharing physical storage between multiple files (that's called a "reflink", iykyk), then this ioctl(2) operation can be used to make some of the data in the src_fd file appear in the dest_fd file by sharing the underlying storage. and that is way faster than making a separate physical copy of the data, because bestie, you're not copying bytes, you're copying pointers. main character energy fr fr.
π¨ both files must reside within the same filesystem. no cross-filesystem situationships allowed.
And if a file write ever happens to a shared region, the filesystem has to make sure the changes stay private to the file being written. this behavior is commonly referred to as "copy on write". periodt.
okay now the actual mechanics, keep up:
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. If src_length is zero, the ioctl reflinks to the end of the source file. all that info is conveyed in a structure of the following form:
struct file_clone_range {
__s64 src_fd;
__u64 src_offset;
__u64 src_length;
__u64 dest_offset;
};
Clones are atomic with regards to concurrent writes, so no locks need to be taken to obtain a consistent cloned copy. no locks babe, we don't do that here π
The FICLONE ioctl clones entire files. (that's the whole file. the whole thing. iconic.)
π₯ RETURN VALUE π₯
On error, -1 is returned, and errno is set to indicate the error.
that's it. that's the section. short and spicy. πΆοΈ
π¦ ERRORS π¦
Error codes can be one of, but are not limited to, the following:
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.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. Disk filesystems generally require the offset and length arguments to be aligned to the fundamental block size.XFSandBtrfsdo not support overlapping reflink ranges in the same file.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. respect it. π)ETXTBSYβ one of the files is a swap file. Swap files cannot share storage.EXDEVβdest_fdandsrc_fdare not on the same mounted filesystem. long distance didn't work out π
π 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 β¨
π CAVEATS π
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 like⦠the sharing is real but the disk space commitment is real-er. plan accordingly bestie.
π SEE ALSO π
and that's the page!! mwah π this was section 2const out of the Linux man-pages, version 6.20, dated 2026-02-08. go forth and reflink responsibly. ππ¦πΈ
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: GPL-2.0-or-later
- Copyright 2016, Oracle
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms