Skip to contentSkip to conversation

nodatacow questions

DISCUSSION Stefanie Leisestreichler 3 participants 7 messages Apr 29, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

Hi.
From the chattr man page:
A file with the 'C' attribute set will not be subject to copy-on-write updates. This flag is only supported on file systems which perform copy-on-write. (Note: For btrfs, the 'C' flag should be set on new or empty files. If it is set on a file which already has data blocks, it is undefined when the blocks assigned to the file will be fully stable. If the 'C' flag is set on a directory, it will have no effect on the directory, but new files created in that directory will have the No_COW attribute set.)

Question 1)
If /var/lib/mysql is a own subvolume and chattr +C /var/lib/mysql is set and mysql is configured to use one directory for every database, will nodatacow apply for a new dir which is created when a new db is created? Just asking, because the last sentence of the man above which states "...new files created in that directory...". Is a dir a file in the context of chattr?

Question 2)
I guess CoW will still happen, if I hold a snapshot of the subvolume which will be mounted to /var/lib/mysql. Is this correct?

Question 3)
How to solve this and avoid defragmentation if my assumption in 2) is correct?

Thanks,
Steffi

#1 lore raw
Quoted 10 lines · Stefanie Leisestreichler

Hi.
From the chattr man page:
A file with the 'C' attribute set will not be subject to copy-on-write updates. This flag is only supported on file systems which perform copy-on-write. (Note: For btrfs, the 'C' flag should be set on new or empty files. If it is set on a file which already has data blocks, it is undefined when the blocks assigned to the file will be fully stable. If the 'C' flag is set on a directory, it will have no effect on the directory, but new files created in that directory will have the No_COW attribute set.)

Quoted 6 lines

Question 1)
If /var/lib/mysql is a own subvolume and chattr +C /var/lib/mysql is set and mysql is configured to use one directory for every database, will nodatacow apply for a new dir which is created when a new db is created? Just asking, because the last sentence of the man above which states "...new files created in that directory...". Is a dir a file in the context of chattr?

Yes.

Quoted 3 lines

Question 2)
I guess CoW will still happen, if I hold a snapshot of the subvolume which will be mounted to /var/lib/mysql. Is this correct?

Correct, but only once. Subsequent writes to the same region will
revert to nodatacow.

Quoted 3 lines

Question 3)
How to solve this and avoid defragmentation if my assumption in 2) is correct?

Don't use snapshots, or don't use nodatacow.

Set autodefrag and don't use nodatacow would be my recommendation.

Hugo.

Signature · 4 lines
Hugo Mills             | 2 + 2 = 5, for sufficiently large values of 2.
hugo@... carfax.org.uk |
http://carfax.org.uk/  |
PGP: E2AB1DE4          |
#2 lore raw
Quoted 3 lines · Hugo Mills

Don't use snapshots, or don't use nodatacow.

Set autodefrag and don't use nodatacow would be my recommendation.

Thanks for this smart tip.

I guess, the same applies to a subvolume which will hold images of virtual machines, right?

Do you have more recommendations, especially for these two use cases (qemu-images and databases)?

Thanks,
Steffi

#3 lore raw
Quoted 9 lines · Stefanie Leisestreichler
Quoted 3 lines · Hugo Mills

Don't use snapshots, or don't use nodatacow.

Set autodefrag and don't use nodatacow would be my recommendation.

Thanks for this smart tip.

I guess, the same applies to a subvolume which will hold images of virtual machines, right?

Correct.

Quoted 2 lines

Do you have more recommendations, especially for these two use cases (qemu-images and databases)?

At least for the VM images, use raw files, not qcow2, and it's
probably best to not use btrfs inside the image -- you want to reduce the number of layers of CoW.

Hugo.

Signature · 4 lines
Hugo Mills             | Once is happenstance; twice is coincidence; three
hugo@... carfax.org.uk | times is enemy action.
http://carfax.org.uk/  |
PGP: E2AB1DE4          |
#4 lore raw
Quoted 3 lines · Hugo Mills

At least for the VM images, use raw files, not qcow2, and it's
probably best to not use btrfs inside the image -- you want to reduce the number of layers of CoW.

Thanks, Hugo.
These machines are already set up in the way you mentioned and will need migration only if the new host is up and running.

Now, after hours of reading, diving and planing a possible btrfs setup I am still not sure if using btrfs is the right choice or if I should stay with mdadm/luks/lvm/ext4...

#5 lore raw
In reply to Stefanie Leisestreichler
Quoted 4 lines · Stefanie Leisestreichler

On 29.04.20 20:33, Hugo Mills wrote:

Quoted 8 lines
Quoted 3 lines

Don't use snapshots, or don't use nodatacow.

Set autodefrag and don't use nodatacow would be my recommendation.

Thanks for this smart tip.

I guess, the same applies to a subvolume which will hold images of virtual machines, right?

For both VMs and loop mounted LUKS encrypted file systems, I use a raw file (no format). In a directory that has +C set. So all the files inherit that.

If the lifecycle is short, or I need to test a virtual size device, then I use truncate to set the virtual size. It only consumes the space actually used, so often I'm in the habit of just 'truncate -s 1T'. Often the file system is Btrfs. Yes it gets fragmented. And since it's a short lifecycle I don't care much. The initial performance is good, maybe better than fallocate.

If the lifecycle is longer, then I use fallocate. This tends to not fragment much at all, even with Btrfs inside.

And if I have some hybrid need, where the file system might need to get better, but I don't want to give up the space right away, I combine:

truncate -s 100G file
fallocate -l 10G file

If I don't use +C for the file, there are better and worse choices for the "guest" file system in the file. Btrfs fragments less than a journaled file system. I don't know the exact mechanism, but I suspect the constant journal overwrites in the guest, turn into COW on Btrfs which is why the raw file becomes so terribly fragmented - way worse than if it were Btrfs.

I think you'd need a benchmark of some kind to see what the latencies are due to fragments, and it might depend significantly between single and multi-queue devices.

Signature · 1 line
Chris Murphy
#6 lore raw
In reply to Stefanie Leisestreichler
Quoted 12 lines · Stefanie Leisestreichler

Hi.
From the chattr man page:
A file with the 'C' attribute set will not be subject to copy-on-write updates. This flag is only supported on file systems which perform copy-on-write. (Note: For btrfs, the 'C' flag should be set on new or empty files. If it is set on a file which already has data blocks, it is undefined when the blocks assigned to the file will be fully stable. If the 'C' flag is set on a directory, it will have no effect on the directory, but new files created in that directory will have the No_COW attribute set.)

Quoted 7 lines

Question 1)
If /var/lib/mysql is a own subvolume and chattr +C /var/lib/mysql is set and mysql is configured to use one directory for every database, will nodatacow apply for a new dir which is created when a new db is created? Just asking, because the last sentence of the man above which states "...new files created in that directory...". Is a dir a file in the context of chattr?

Yes.

$ btrfs sub create haha
Create subvolume './haha'
$ chattr +C haha
$ mkdir haha/test
$ lsattr haha
---------------C---- haha/test
$
Quoted 4 lines

Question 2)
I guess CoW will still happen, if I hold a snapshot of the subvolume which will be mounted to /var/lib/mysql. Is this correct?

Yes, initial changes will COW to new extents, subsequent changes for changed extents are nocow until there's another snapshot.

Quoted 3 lines

Question 3)
How to solve this and avoid defragmentation if my assumption in 2) is correct?

I'm not sure what the problem is you want solved. I'm not certain the behavior is identical on XFS but I expect that reflink copy of a database file on XFS causes any initial changed extent to be COW and subsequent changes to the same extent to be an overwrite.

Signature · 1 line
Chris Murphy