Skip to content

Commit bbac586

Browse files
adam900710kdave
authored andcommitted
btrfs: remove device item and update super block in the same transaction
[BUG] There is a report that a btrfs has a bad super block num devices. This makes btrfs to reject the fs completely. BTRFS error (device sdd3): super_num_devices 3 mismatch with num_devices 2 found here BTRFS error (device sdd3): failed to read chunk tree: -22 BTRFS error (device sdd3): open_ctree failed [CAUSE] During btrfs device removal, chunk tree and super block num devs are updated in two different transactions: btrfs_rm_device() |- btrfs_rm_dev_item(device) | |- trans = btrfs_start_transaction() | | Now we got transaction X | | | |- btrfs_del_item() | | Now device item is removed from chunk tree | | | |- btrfs_commit_transaction() | Transaction X got committed, super num devs untouched, | but device item removed from chunk tree. | (AKA, super num devs is already incorrect) | |- cur_devices->num_devices--; |- cur_devices->total_devices--; |- btrfs_set_super_num_devices() All those operations are not in transaction X, thus it will only be written back to disk in next transaction. So after the transaction X in btrfs_rm_dev_item() committed, but before transaction X+1 (which can be minutes away), a power loss happen, then we got the super num mismatch. [FIX] Instead of starting and committing a transaction inside btrfs_rm_dev_item(), start a transaction in side btrfs_rm_device() and pass it to btrfs_rm_dev_item(). And only commit the transaction after everything is done. Reported-by: Luca Béla Palkovics <[email protected]> Link: https://lore.kernel.org/linux-btrfs/CA+8xDSpvdm_U0QLBAnrH=zqDq_cWCOH5TiV46CKmp3igr44okQ@mail.gmail.com/ CC: [email protected] # 4.14+ Reviewed-by: Anand Jain <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent b642b52 commit bbac586

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

fs/btrfs/volumes.c

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,23 +1896,18 @@ static void update_dev_time(const char *device_path)
18961896
path_put(&path);
18971897
}
18981898

1899-
static int btrfs_rm_dev_item(struct btrfs_device *device)
1899+
static int btrfs_rm_dev_item(struct btrfs_trans_handle *trans,
1900+
struct btrfs_device *device)
19001901
{
19011902
struct btrfs_root *root = device->fs_info->chunk_root;
19021903
int ret;
19031904
struct btrfs_path *path;
19041905
struct btrfs_key key;
1905-
struct btrfs_trans_handle *trans;
19061906

19071907
path = btrfs_alloc_path();
19081908
if (!path)
19091909
return -ENOMEM;
19101910

1911-
trans = btrfs_start_transaction(root, 0);
1912-
if (IS_ERR(trans)) {
1913-
btrfs_free_path(path);
1914-
return PTR_ERR(trans);
1915-
}
19161911
key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
19171912
key.type = BTRFS_DEV_ITEM_KEY;
19181913
key.offset = device->devid;
@@ -1923,21 +1918,12 @@ static int btrfs_rm_dev_item(struct btrfs_device *device)
19231918
if (ret) {
19241919
if (ret > 0)
19251920
ret = -ENOENT;
1926-
btrfs_abort_transaction(trans, ret);
1927-
btrfs_end_transaction(trans);
19281921
goto out;
19291922
}
19301923

19311924
ret = btrfs_del_item(trans, root, path);
1932-
if (ret) {
1933-
btrfs_abort_transaction(trans, ret);
1934-
btrfs_end_transaction(trans);
1935-
}
1936-
19371925
out:
19381926
btrfs_free_path(path);
1939-
if (!ret)
1940-
ret = btrfs_commit_transaction(trans);
19411927
return ret;
19421928
}
19431929

@@ -2078,6 +2064,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info,
20782064
struct btrfs_dev_lookup_args *args,
20792065
struct block_device **bdev, fmode_t *mode)
20802066
{
2067+
struct btrfs_trans_handle *trans;
20812068
struct btrfs_device *device;
20822069
struct btrfs_fs_devices *cur_devices;
20832070
struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
@@ -2098,35 +2085,30 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info,
20982085

20992086
ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
21002087
if (ret)
2101-
goto out;
2088+
return ret;
21022089

21032090
device = btrfs_find_device(fs_info->fs_devices, args);
21042091
if (!device) {
21052092
if (args->missing)
21062093
ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
21072094
else
21082095
ret = -ENOENT;
2109-
goto out;
2096+
return ret;
21102097
}
21112098

21122099
if (btrfs_pinned_by_swapfile(fs_info, device)) {
21132100
btrfs_warn_in_rcu(fs_info,
21142101
"cannot remove device %s (devid %llu) due to active swapfile",
21152102
rcu_str_deref(device->name), device->devid);
2116-
ret = -ETXTBSY;
2117-
goto out;
2103+
return -ETXTBSY;
21182104
}
21192105

2120-
if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2121-
ret = BTRFS_ERROR_DEV_TGT_REPLACE;
2122-
goto out;
2123-
}
2106+
if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
2107+
return BTRFS_ERROR_DEV_TGT_REPLACE;
21242108

21252109
if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2126-
fs_info->fs_devices->rw_devices == 1) {
2127-
ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
2128-
goto out;
2129-
}
2110+
fs_info->fs_devices->rw_devices == 1)
2111+
return BTRFS_ERROR_DEV_ONLY_WRITABLE;
21302112

21312113
if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
21322114
mutex_lock(&fs_info->chunk_mutex);
@@ -2139,14 +2121,22 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info,
21392121
if (ret)
21402122
goto error_undo;
21412123

2142-
/*
2143-
* TODO: the superblock still includes this device in its num_devices
2144-
* counter although write_all_supers() is not locked out. This
2145-
* could give a filesystem state which requires a degraded mount.
2146-
*/
2147-
ret = btrfs_rm_dev_item(device);
2148-
if (ret)
2124+
trans = btrfs_start_transaction(fs_info->chunk_root, 0);
2125+
if (IS_ERR(trans)) {
2126+
ret = PTR_ERR(trans);
21492127
goto error_undo;
2128+
}
2129+
2130+
ret = btrfs_rm_dev_item(trans, device);
2131+
if (ret) {
2132+
/* Any error in dev item removal is critical */
2133+
btrfs_crit(fs_info,
2134+
"failed to remove device item for devid %llu: %d",
2135+
device->devid, ret);
2136+
btrfs_abort_transaction(trans, ret);
2137+
btrfs_end_transaction(trans);
2138+
return ret;
2139+
}
21502140

21512141
clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
21522142
btrfs_scrub_cancel_dev(device);
@@ -2229,7 +2219,8 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info,
22292219
free_fs_devices(cur_devices);
22302220
}
22312221

2232-
out:
2222+
ret = btrfs_commit_transaction(trans);
2223+
22332224
return ret;
22342225

22352226
error_undo:
@@ -2240,7 +2231,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info,
22402231
device->fs_devices->rw_devices++;
22412232
mutex_unlock(&fs_info->chunk_mutex);
22422233
}
2243-
goto out;
2234+
return ret;
22442235
}
22452236

22462237
void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)

0 commit comments

Comments
 (0)