btrfs - cloning a file - how works copy-on-write?

873 views Asked by At

I have a btrfs volume which I use for backups.

It is almost full (few MB free). If I duplicate a (big, too big for the existing free space on the volume) file, it works. That is expected since btrfs is a copy-on-write fs.

cd /btrfs-backup/
cp /home/john/a.mp4 . # 1st instance of the file takes place on the volume.
cp a.mp4 b.mp4        # 2nd instance does not take place and is immediate.

But if I copy that same file from the source fs, it fails:

cp /home/john/a.mp4 c.mp4 # 3rd instance of the same file.
cp: error writing 'a.mp4': No space left on device

Why that error?

Should not the same file be made of the same blocks, which should not be duplicated on btrfs fs?

1

There are 1 answers

0
Damien Clauzel On

You probably want to have a look at the BTRFS documentation about CoW and some use cases.

For short, in order to prevent duplication, the data (meaning, the two files) must be on the same filesystem.

The filesystem must have CoW support, and you need to not have the +C flag on the source file (cf man chattr).

The cp command also need to know that it must use CoW, with the --reflink option (cf man cp).

CoW should be enabled by default on your system, but depending on your setup things may be different.