Bug: trying to create reflink on different device results in empty file
When trying to create a clone (reflink) of a file on a different device, you'll get this error:
Invalid cross-device link
However, an empty file is created on the target location. An invalid clone operation should not result in the creation of a file.
Example:
# mount (...) /dev/sdb1 on /mnt/diskb type btrfs (rw) /dev/sdf1 on /mnt/diskf type btrfs (rw)
# ls -l diskb total 4 -rw-r--r-- 1 root root 11 Dec 11 22:45 testfile.txt
# ls -l diskf total 0
# cp --reflink diskb/testfile.txt diskf/testcopy.txt cp: failed to clone `diskf/testcopy.txt' from `diskb/testfile.txt': Invalid cross-device link
# ls -l diskf total 0 -rw-r--r-- 1 root root 0 Dec 11 22:56 testcopy.txt
Quoted 6 lines · Koen De Wit
When trying to create a clone (reflink) of a file on a different device, you'll get this error:
Invalid cross-device linkHowever, an empty file is created on the target location. An invalid clone operation should not result in the creation of a file.
The clone operation inside btrfs needs 2 files already open (ie. it works with file descriptors, not with filenames), the empty file exists prior to the ioctl call. So the file should be removed by 'cp' (or whoever calls the clone ioctl) because they know if it makes sense to delete the file or not.
david