Bug 217066 - Disabling CoW does not work for Swapfile after running balance
Summary: Disabling CoW does not work for Swapfile after running balance
Status: REOPENED
Alias: None
Product: File System
Classification: Unclassified
Component: btrfs (show other bugs)
Hardware: All Linux
: P1 normal
Assignee: BTRFS virtual assignee
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-02-21 17:48 UTC by ZeskoTron
Modified: 2023-05-12 16:40 UTC (History)
3 users (show)

See Also:
Kernel Version: 6.1.12
Subsystem:
Regression: No
Bisected commit-id:


Attachments

Description ZeskoTron 2023-02-21 17:48:28 UTC
I tested Swapfile on BTRFS in Arch VM using Kernel 6.1.12 or Kernel 5.15.94, but I got the error message of Swapon:
"swapon: /swap/swapfile: swapon failed: Invalid argument"


Steps to reproduce the issue:

1. Create Swapfile in the subvolume @swap:
""""""
# btrfs filesystem mkswapfile /swap/swapfile
""""""

2. Enabled Swapfile, it has no issue on Btrfs
""""""
# swapon /swap/swapfile
""""""

3. Disabled Swapfile:
""""""
# swapoff /swap/swapfile
""""""

4. Running Btrfs balance
""""""
# btrfs balance start / --full-balance
""""""

5. After the balance, enable Swapfile again:
"# swapon /swap/swapfile"
 It shows the error message: "swapon failed: Invalid argument"

I got the warning message of dmesg: "kernel: BTRFS warning (device dm-0): swapfile must not be copy-on-write"

That looks like disabling CoW does not work for Swapfile after running balance, that is why Swapon failed.

I am not alone, but other Btrfs users have the same issue.
See the source: https://forum.endeavouros.com/t/btrfs-assistant-1-0-is-coming-testers-needed/25421/508

________

Kernel 6.1.12 or Kernel 5.15.94
btrfs version: 6.1.3
Comment 1 ZeskoTron 2023-02-26 11:19:28 UTC
I found the issue:

Btrfs balance with the option "--full-balance" essentially breaks all NoCOW and Swapfile.
Do not use "btrfs balance start / --full-balance" when using Swapfile or NoCOW on Btrfs.

Use "btrfs balance -dusage=90 /" that respects the NoCOW and Swapfile with +C attribute.

The source:
https://forum.manjaro.org/t/howto-enable-and-configure-hibernation-with-btrfs/51253/1
Comment 2 David Sterba 2023-02-27 18:26:49 UTC
> Btrfs balance with the option "--full-balance" essentially breaks all NoCOW
> and Swapfile.

That's not true, balance does not break NOCOW (defrag does) and the swapfile extents must be NOCOW but are skipped during balance.

> Use "btrfs balance -dusage=90 /" that respects the NoCOW and Swapfile with +C
> attribute.

That's not true either but maybe as a side efect of the 90% filter the balance does not reach to the swapfile.

You've closed this as documented, if you actually written the above to any documentation please remove it ASAP before this incorrect information spreads. 

There's probably a bug that you found with swapon/swapoff/balance/swapon that should be fixed.
Comment 3 ZeskoTron 2023-02-28 07:56:22 UTC
Thanks for the info!

We fixed the tutorial doc to follow you what you mean, if the doc is not correct, you can let us know to correct it. Thanks!


On the current Kernels 5.15.96, 6.1.14 or 6.2.1, btrfs balance without filter rebalances everything and ignores the +C (nodatacow) attribute. We get the warning message in dmesg if you activate a swapfile after doing a full balancing:

"""""
kernel: BTRFS warning (device XY): swapfile must not be copy-on-write
"""""


> That's not true, balance does not break NOCOW (defrag does) and the swapfile
> extents must be NOCOW but are skipped during balance.
> There's probably a bug that you found with swapon/swapoff/balance/swapon that
> should be fixed.


I see, but we can reproduce the issue on the current kernels on Arch, EOS or Manjaro OS.

That's indeed a bug because the balance actually changes NoCOW or Swapfile to COW. This is not the expected behavior.
Comment 4 David Sterba 2023-02-28 23:37:19 UTC
I can't reproduce it on 6.2-rc, I'll check other versions. The error message is after checking the NOCOW file attribute, so you can verify that it really exists after the rebalance by "lsattr swapfile".

With the swapfile active and partially used, the full balance correctly produces the messages "skipping relocation of block group 4920967168 due to active swapfile" in the log.
Comment 5 ZeskoTron 2023-03-01 19:41:55 UTC
We can reproduce this issue step by step on the current Kernel 6.2 
IF LESS THAN 70% single data in filesystem usage:


"""""""""""""""""""""""""""""""""
[root@test-btrfs /]# mount /dev/sda2 /mnt
[root@test-btrfs /]# btrfs subvolume create /mnt/@swap
[root@test-btrfs /]# mkdir /swap
[root@test-btrfs /]# mount /dev/sda2 swap/ -o subvol=/@swap,nodatacow,noatime
[root@test-btrfs /]# truncate -s 0 swap/swapfile
[root@test-btrfs /]# chattr +C swap/swapfile 
[root@test-btrfs /]# fallocate -l 2G swap/swapfile 
[root@test-btrfs /]# chmod 600 sap/swapfile 
[root@test-btrfs /]# mkswap swap/swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
[root@test-btrfs /]# cd swap
[root@test-btrfs swap]# swapon swapfile
[root@test-btrfs swap]# btrfs balance start / --full-balance
[root@test-btrfs swap]# swapoff swapfile
[root@test-btrfs swap]# swapon swapfile
swapon: /swap/swapfile: swapon failed: Invalid argument

[root@test-btrfs swap]# dmesg | grep "BTRFS warning"
kernel: BTRFS warning (device sda2): swapfile must not be copy-on-write
"""""""""""""""""""""""""""""""""

The filesystem usage should show less than 70% data, can you try this guide if the issue is reproduced?
Comment 6 Nikolaos Bezirgiannis 2023-05-12 10:27:35 UTC
I ran into the same issue. 


Kernel: Linux 6.3.1
btrfs-progs: 6.3

Steps that reproduced it for me:


$ btrfs filesystem mkswapfile --size 2g --uuid clear /swapfile
create swapfile /swapfile size 2.00GiB (2147483648)
$ swapon /swapfile  # all good
$ swapoff /swapfile  # all good
$ lsattr /swapfile 
---------------C------ /swapfile

# Start the balance
$ btrfs balance start -dusage=70 /
Done, had to relocate 1 out of 136 chunks
$ lsattr /swapfile  # STILL SHOWS NO COW
---------------C------ /swapfile
$ swapon /swapfile  # all good
swapon: /swapfile: swapon failed: Invalid argument
$journalctl -t kernel | grep swapfile
May 12 08:30:30 lenovo kernel: BTRFS warning (device nvme0n1p2): swapfile must not be copy-on-write
May 12 08:30:32 lenovo kernel: BTRFS warning (device nvme0n1p2): swapfile must not be copy-on-write
May 12 08:40:32 lenovo kernel: BTRFS warning (device nvme0n1p2): swapfile must not be copy-on-write
May 12 08:46:47 lenovo kernel: Adding 2097148k swap on /swapfile.  Priority:-2 extents:3 across:143392768k FS
May 12 08:46:24 lenovo kernel: Adding 2097148k swap on /swapfile.  Priority:-2 extents:3 across:143392768k FS
May 12 08:46:38 lenovo kernel: BTRFS warning (device nvme0n1p2): swapfile must not be copy-on-write
May 12 08:46:40 lenovo kernel: BTRFS warning (device nvme0n1p2): swapfile must not be copy-on-write
May 12 08:46:52 lenovo kernel: BTRFS warning (device nvme0n1p2): swapfile must not be copy-on-write
May 12 08:49:24 lenovo kernel: Adding 2097148k swap on /swapfile.  Priority:-2 extents:2 across:3389868k FS
May 12 08:51:08 lenovo kernel: BTRFS warning (device nvme0n1p2): swapfile must not be copy-on-write
May 12 12:19:48 lenovo kernel: Adding 2097148k swap on /swapfile.  Priority:-2 extents:4 across:9654756k FS
May 12 12:20:42 lenovo kernel: Adding 2097148k swap on /swapfile.  Priority:-2 extents:4 across:9654756k FS
May 12 12:22:50 lenovo kernel: BTRFS warning (device nvme0n1p2): swapfile must not be copy-on-write


The only way to fix it after the balance is done is to delete the /swapfile file and reproduce it with `btrfs filesystem mkswapfile`
Comment 7 David Sterba 2023-05-12 16:40:20 UTC
The bug is real, I'm told the reloc tree is created unconditionally for a block group so this leads to the rejection. A workaround is to recreate the swapfile and a fix should appear soon.

Note You need to log in before you can comment on or make changes to this bug.