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_fd is not open for reading; dest_fd is not open for writing or is open for append-only writes; or the filesystem which src_fd resides 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. XFS and Btrfs do 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_fd is immutable. (it said no. respect it. πŸ™ˆ)
  • ETXTBSY β€” one of the files is a swap file. Swap files cannot share storage.
  • EXDEV β€” dest_fd and src_fd are 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms