ZFS – How Copy on Write Works for Large Files

copy on writecpfile-copyzfs

Let's say I have a large file (8GB) called example.log on ZFS.
I do cp example.log example.bak to make a copy. Then I add or modify a few bytes in original file. What will happen?

Will ZFS copy the entire 8GB file or only the blocks that changed (and all the chain of inodes pointing from the file descriptor to that block)?

Best Answer

As far as I could tell, FreeBSD ZFS does not support copy-on-write using cp; the native cp does not seem to have an option for such lightweight copies, and trying GNU cp with --reflink errors out on the ZFS system I tried with error message "cp: failed to clone 'example.bak' from 'example.log': Operation not supported".

A commenter mentions that Solaris cp has a -z switch to do such copies.

However, and I hope this answers your underlying question, copy-on-write is used for filesystem snapshots: let's say you have 900GB used out of 1000GB available, nothing prevents you from making a snapshot of that filesystem, the snapshot will not occupy 900GB; in fact, it will initially not occupy any new data blocks at all.

After creating a snapshot of your original filesystem containing example.log, you end up with two "copies": the read-only version in the snapshot, and you live version in its original location. What happens when the copy is modified, be it by appending or by being altered in-place? That is where the magic happens: only those blocks that are altered get copied and start using up space. It is not the case that the entire file gets copied as soon as it gets altered.

Related Question