Erase Bcachefs backup super block (!123)

GParted doesn't clear all bcachefs file system signatures.
    # truncate -s $((1*1024*1024*1024)) /tmp/disk.img
    # losetup --find --show /tmp/disk.img
    /dev/loop0
    # bcachefs format /tmp/disk.img
    # wipefs /dev/loop0
    DEVICE OFFSET     TYPE     UUID                                 LABEL
    loop0  0x1018     bcachefs 15304edb-6dc4-476c-989e-74eaea6660e8
    loop0  0x3ff00018 bcachefs 15304edb-6dc4-476c-989e-74eaea6660e8

Use GParted to format to cleared /dev/loop0.
    # wipefs /dev/loop0
    DEVICE OFFSET     TYPE     UUID                                 LABEL
    loop0  0x3ff00018 bcachefs 15304edb-6dc4-476c-989e-74eaea6660e8

The backup super block is located at -1 MiB before the end of the
device, rounded down to the file system's bucket size [1], where the
bucket size is one of: 128 KiB, 256 KiB, 512 KiB, 1024 KiB [2].
    location = device size - 1 MiB
             = hex(1*1024*1024*1024 - 1*1024*1024)
             = 0x3ff00000
Bcachefs magic is at byte offset 24 (0x18) into the super block [3].

The backup super block was not erased because GParted only writes zeros
from -512 KiB to the end of the device.  It is necessary to clear old
signatures before formatting with a new file system to avoid confusion
from detection of multiple incompatible signatures [4][5].  Fix this by
writing zeros over all possible locations of the bcachefs backup super
block.

[1] bcachefs-tools v1.6.4: c_src/libbcachefs.c:bch2_format()
    https://evilpiepirate.org/git/bcachefs-tools.git/tree/c_src/libbcachefs.c?h=v1.6.4#n313
    [This code locates the backup super block at device size - 1 MiB
    rounded down to the bucket size.]
    321     struct bch_sb_layout *l = &sb.sb->layout;
    322     u64 backup_sb = size_sectors - (1 << l->sb_max_size_bits);
    323
    324     backup_sb = rounddown(backup_sb, i->bucket_size >> 9);
[2] bcachefs-tools v1.6.4: c_src/libbcachefs.c:bch2_pick_bucket_size()
    https://evilpiepirate.org/git/bcachefs-tools.git/tree/c_src/libbcachefs.c?h=v1.6.4#n85
     85      /* Want a bucket size of at least 128k, if possible: */
     86      bucket_size = max(bucket_size, 128ULL << 10);
    ...
     94      /* max bucket size 1 mb */
     95      bucket_size = min(bucket_size * scale, 1ULL << 20);
[3] bcachefs-tools v1.6.4: libcachefs/bcachefs_format.h:struct bch_sb
    https://evilpiepirate.org/git/bcachefs-tools.git/tree/libbcachefs/bcachefs_format.h?h=v1.6.4#n907
[4] 743968ef68
    Add clearing of SWRaid metadata 0.90 and 1.0 super blocks (#756829)
[5] 3c75f3f5b1
    Use wipefs to clear old signatures before creating new file systems (#688882)

Closes !123 - Add support for bcachefs, single device file systems only
This commit is contained in:
Mike Fleetwood 2024-03-16 08:57:42 +00:00
parent b8304a141a
commit 76b27b9f7a

View file

@ -3789,6 +3789,7 @@ bool GParted_Core::erase_filesystem_signatures( const Partition & partition, Ope
// Not covered by the above are:
// * Btrfs super block mirror copies
// * One possible location of Promise FastTrack RAID super block
// * Bcachefs super block backup
//
// Btrfs super blocks are located at: 64 KiB, 64 MiB, 256 GiB and 1 PiB. The
// super block at 64 KiB will be erased by the zeroing from offset 0. The super
@ -3803,6 +3804,16 @@ bool GParted_Core::erase_filesystem_signatures( const Partition & partition, Ope
// location -3087 must be explicitly cleared.
// util-linux v2.38.1: libblkid/src/subperblocks/promise_raid.c:probe_pdcraid()
// https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/tree/libblkid/src/superblocks/promise_raid.c?h=v2.38.1#n27
//
// Bcachefs super block backup is located at -1 MiB before the end of the device,
// rounded down to the file system's bucket size, where the bucket size is one of:
// 128 KiB, 256 KiB, 512 KiB or 1024 KiB.
// * bcachefs-tools v1.6.4: c_src/libbcachefs.c:bch2_format()
// https://evilpiepirate.org/git/bcachefs-tools.git/tree/c_src/libbcachefs.c?h=v1.6.4#n313
// * bcachefs-tools v1.6.4: c_src/libbcachefs.c:bch2_pick_bucket_size()
// https://evilpiepirate.org/git/bcachefs-tools.git/tree/c_src/libbcachefs.c?h=v1.6.4#n85
// * bcachefs-tools v1.6.4: libcachefs/bcachefs_format.h:struct bch_sb
// https://evilpiepirate.org/git/bcachefs-tools.git/tree/libbcachefs/bcachefs_format.h?h=v1.6.4#n907
struct {
Byte_Value offset; // Negative offsets work backwards from the end of the partition
Byte_Value rounding; // Minimum desired rounding for offset
@ -3814,6 +3825,10 @@ bool GParted_Core::erase_filesystem_signatures( const Partition & partition, Ope
{ 256LL * GIBIBYTE, 1LL , 4LL * KIBIBYTE }, // Btrfs super block mirror copy
{ 1LL * PEBIBYTE, 1LL , 4LL * KIBIBYTE }, // Btrfs super block mirror copy
{ -3087LL * 512LL , 1LL , 512LL }, // Promise FastTrack RAID super block
{ -1LL * MEBIBYTE, 128LL * KIBIBYTE, 4LL * KIBIBYTE }, // Bcachefs backup super block
{ -1LL * MEBIBYTE, 256LL * KIBIBYTE, 4LL * KIBIBYTE }, // Bcachefs backup super block
{ -1LL * MEBIBYTE, 512LL * KIBIBYTE, 4LL * KIBIBYTE }, // Bcachefs backup super block
{ -1LL * MEBIBYTE, 1LL * MEBIBYTE, 4LL * KIBIBYTE }, // Bcachefs backup super block
{ -512LL * KIBIBYTE, 256LL * KIBIBYTE, 768LL * KIBIBYTE } // Super blocks at end
} ;
for ( unsigned int i = 0 ; overall_success && i < sizeof( ranges ) / sizeof( ranges[0] ) ; i ++ )