I'm running ZFS with de-duplication turned on (when properly tuned it works well; don't let the internet scare you). Each day I need to take several terabytes of qcow2 images and create fixed-VHDX copies of those disk images. My qcow2 images use a 2M cluster size, meaning each 2M block on the virtual disk is 2M aligned in the file. Fixed-VHDX files are just raw disk images with some metadata at the end, so they are also 2M aligned. This means when I do the conversion, ZFS can de-dupe the 2 files and I end up with only one copy of the data (yay).
That's great, but it's still CPU and IO intensive to do the conversion. Each time I want to copy a 2M block from the qcow2 image to the VHDX image I have to read the whole block byte-by-byte from the qcow2 image, then write the whole block byte-by-byte to the VHDX. Then ZFS compares the 2 blocks byte-by-byte to make sure it can de-dupe them.
It occurs to me that it should be possible to copy a block from one file to another without ever having to read the block. After all, each "file" is basically a bunch of pointers to blocks, each referenced by one or more files. Couldn't we just read pointers from the first file and copy them to the 2nd file without having to read the contents of the blocks they point to?
Is there some sort of system call that allows me to create a COW copy of a block-aligned extent from one file to another without having to read the contents of the file from the disk?