BTRFS: is it possible to switch to another tree root?

I get my BTRFS partition damaged and

mount -o usebackuproot ...

does not work:

[ 9252.141767] BTRFS info (device dm-1): trying to use backup root at mount time
[ 9252.141779] BTRFS info (device dm-1): disabling disk space caching
[ 9252.141784] BTRFS info (device dm-1): has skinny extents
[ 9252.184586] BTRFS error (device dm-1): parent transid verify failed on 7777276116992 wanted 100336 found 89751
[ 9252.186302] BTRFS error (device dm-1): parent transid verify failed on 7777276116992 wanted 100336 found 89751
[ 9252.186306] BTRFS warning (device dm-1): failed to read tree root
[ 9252.186729] BTRFS error (device dm-1): parent transid verify failed on 7777276116992 wanted 100336 found 89751
[ 9252.187091] BTRFS error (device dm-1): parent transid verify failed on 7777276116992 wanted 100336 found 89751
[ 9252.187094] BTRFS warning (device dm-1): failed to read tree root
[ 9252.187372] BTRFS error (device dm-1): parent transid verify failed on 7777213923328 wanted 100335 found 89750
[ 9252.187589] BTRFS error (device dm-1): parent transid verify failed on 7777213923328 wanted 100335 found 89750
[ 9252.187592] BTRFS warning (device dm-1): failed to read tree root
[ 9252.187948] BTRFS error (device dm-1): parent transid verify failed on 7777188921344 wanted 100334 found 100336
[ 9252.188281] BTRFS error (device dm-1): parent transid verify failed on 7777188921344 wanted 100334 found 100336
[ 9252.188284] BTRFS warning (device dm-1): failed to read tree root
[ 9252.188661] BTRFS error (device dm-1): parent transid verify failed on 7260661022720 wanted 100333 found 96460
[ 9252.189025] BTRFS error (device dm-1): parent transid verify failed on 7260661022720 wanted 100333 found 96460
[ 9252.189029] BTRFS warning (device dm-1): failed to read tree root
[ 9252.236361] BTRFS: open_ctree failed

btrfs check does not work either:

btrfs check /dev/mapper/media
parent transid verify failed on 7777276116992 wanted 100336 found 89751
parent transid verify failed on 7777276116992 wanted 100336 found 89751
parent transid verify failed on 7777276116992 wanted 100336 found 89751
parent transid verify failed on 7777276116992 wanted 100336 found 89751
Ignoring transid failure
Couldn't setup extent tree
Couldn't open file system

I found some tree roots blocks that I could use with "btrfs restore":

btrfs restore -t 8889016483840 -D -i -v /dev/mapper/media /dev/null | wc -l
2264442

But I'm getting the problem here. My btrfs storage is very big, it's over 20TB, and I don't have any spare disks of such size to backup and restore it. Is it possible to repair such file system or switch to a given tree root block "in place", without reformatting it?


First, ensure you've got valid superblocks:

btrfs rescue super-recover -v <device>

Use:

btrfs find-root <device>

to find the best tree root to use in repair:

  1. The root node should have the highest level
  2. The higher generation, the higher chance the fs can be recovered using that root.

Note: in your error message, wanted is the generation in the log, the found is the generation based on the tree root.

Find the cleanest output from the following commands:

btrfs check --tree-root <block> --super <sup>

Where:

  • <sup> is either 0, 1 or 2.
  • <block> is is given by btrfs find-root (note: don't multiply by block size, even though the manual says <bytenr>)

Then repair the filesystem as follows:

btrfs check --repair --tree-root <block> --super <sup>

Note the addition of --repair to actually change the filesystem.