What’s block cloning?
Block cloning allows to copy files (or parts of it = blocks) without allocating extra space (besides the metadata required for filename, attributes, or the references to existing on disk blocks).
Similar techniques are used to implement things like snapshots or deduplication in filesystems supporting them.
In practice this feature is very usefull if you’re handling big data sets, but you only intend to change small portions or metadata of the files concerned. One common usage scenario may be the cloning of disk images for virtualization: You can simply clone a whole multi-GB disk image for almost no extra allocated space (and in almost no time!). Once you run the virtual machine with this disk image only the written changes to this file require extra space on disk.
Why talk about block cloning in (Open)ZFS?
Recently I needed to automatically sort (rename) quite a large set of files. To prevent unintended data loss while playing around I first copied this data. In order to save space I tried to do so using cloning:
linux > cp -r --reflink=always /zfs/src/ /zfs/dst/
cp: failed to clone 'dst/file' from 'src/file': Operation not supported
Of course to do so the used filesystem (ZFS in my case) needs to support cloning. As you can see above instead of a cloned copy all I got was an error message. However I was quite sure that I once did the very same thing some time ago.
So I started a little research and it seems like with the first release of OpenZFS 2.2.0 the new feature “block cloning” was introduced. However due to problems this feature was soon disabled with the next 2.2.1 release.
Whether this is the case with your distribution / zfs release can be checked like this:
linux > cat /sys/module/zfs/parameters/zfs_bclone_enabled
0
In the meantime the current version is 2.2.6 and this feature is still disabled by default. According to some developers however the stability of this feature improved a lot in the meantime an could be considered stable (and may be enabled by default in an upcoming 2.2.7 or 2.3.x release).
If you don’t want to wait for that to happen, you can enable this feature with
linux > echo 1 > /sys/module/zfs/parameters/zfs_bclone_enabled
After doing so, the cp command from the very top works like a charm.