Skip to content

Commit ce4c854

Browse files
committed
Merge tag 'for-5.18-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux
Pull btrfs fixes from David Sterba: - prevent deleting subvolume with active swapfile - fix qgroup reserve limit calculation overflow - remove device count in superblock and its item in one transaction so they cant't get out of sync - skip defragmenting an isolated sector, this could cause some extra IO - unify handling of mtime/permissions in hole punch with fallocate - zoned mode fixes: - remove assert checking for only single mode, we have the DUP mode implemented - fix potential lockdep warning while traversing devices when checking for zone activation * tag 'for-5.18-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux: btrfs: prevent subvol with swapfile from being deleted btrfs: do not warn for free space inode in cow_file_range btrfs: avoid defragging extents whose next extents are not targets btrfs: fix fallocate to use file_modified to update permissions consistently btrfs: remove device item and update super block in the same transaction btrfs: fix qgroup reserve overflow the qgroup limit btrfs: zoned: remove left over ASSERT checking for single profile btrfs: zoned: traverse devices under chunk_mutex in btrfs_can_activate_zone
2 parents 3123109 + 60021bd commit ce4c854

6 files changed

Lines changed: 81 additions & 55 deletions

File tree

fs/btrfs/extent_io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct btrfs_bio_ctrl {
118118
*/
119119
struct extent_changeset {
120120
/* How many bytes are set/cleared in this operation */
121-
unsigned int bytes_changed;
121+
u64 bytes_changed;
122122

123123
/* Changed ranges */
124124
struct ulist range_changed;

fs/btrfs/file.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,8 +2957,9 @@ int btrfs_replace_file_extents(struct btrfs_inode *inode,
29572957
return ret;
29582958
}
29592959

2960-
static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2960+
static int btrfs_punch_hole(struct file *file, loff_t offset, loff_t len)
29612961
{
2962+
struct inode *inode = file_inode(file);
29622963
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
29632964
struct btrfs_root *root = BTRFS_I(inode)->root;
29642965
struct extent_state *cached_state = NULL;
@@ -2990,6 +2991,10 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
29902991
goto out_only_mutex;
29912992
}
29922993

2994+
ret = file_modified(file);
2995+
if (ret)
2996+
goto out_only_mutex;
2997+
29932998
lockstart = round_up(offset, btrfs_inode_sectorsize(BTRFS_I(inode)));
29942999
lockend = round_down(offset + len,
29953000
btrfs_inode_sectorsize(BTRFS_I(inode))) - 1;
@@ -3430,7 +3435,7 @@ static long btrfs_fallocate(struct file *file, int mode,
34303435
return -EOPNOTSUPP;
34313436

34323437
if (mode & FALLOC_FL_PUNCH_HOLE)
3433-
return btrfs_punch_hole(inode, offset, len);
3438+
return btrfs_punch_hole(file, offset, len);
34343439

34353440
/*
34363441
* Only trigger disk allocation, don't trigger qgroup reserve
@@ -3452,6 +3457,10 @@ static long btrfs_fallocate(struct file *file, int mode,
34523457
goto out;
34533458
}
34543459

3460+
ret = file_modified(file);
3461+
if (ret)
3462+
goto out;
3463+
34553464
/*
34563465
* TODO: Move these two operations after we have checked
34573466
* accurate reserved space, or fallocate can still fail but

fs/btrfs/inode.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,6 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
11281128
int ret = 0;
11291129

11301130
if (btrfs_is_free_space_inode(inode)) {
1131-
WARN_ON_ONCE(1);
11321131
ret = -EINVAL;
11331132
goto out_unlock;
11341133
}
@@ -4488,6 +4487,13 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
44884487
dest->root_key.objectid);
44894488
return -EPERM;
44904489
}
4490+
if (atomic_read(&dest->nr_swapfiles)) {
4491+
spin_unlock(&dest->root_item_lock);
4492+
btrfs_warn(fs_info,
4493+
"attempt to delete subvolume %llu with active swapfile",
4494+
root->root_key.objectid);
4495+
return -EPERM;
4496+
}
44914497
root_flags = btrfs_root_flags(&dest->root_item);
44924498
btrfs_set_root_flags(&dest->root_item,
44934499
root_flags | BTRFS_ROOT_SUBVOL_DEAD);
@@ -11107,8 +11113,23 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
1110711113
* set. We use this counter to prevent snapshots. We must increment it
1110811114
* before walking the extents because we don't want a concurrent
1110911115
* snapshot to run after we've already checked the extents.
11116+
*
11117+
* It is possible that subvolume is marked for deletion but still not
11118+
* removed yet. To prevent this race, we check the root status before
11119+
* activating the swapfile.
1111011120
*/
11121+
spin_lock(&root->root_item_lock);
11122+
if (btrfs_root_dead(root)) {
11123+
spin_unlock(&root->root_item_lock);
11124+
11125+
btrfs_exclop_finish(fs_info);
11126+
btrfs_warn(fs_info,
11127+
"cannot activate swapfile because subvolume %llu is being deleted",
11128+
root->root_key.objectid);
11129+
return -EPERM;
11130+
}
1111111131
atomic_inc(&root->nr_swapfiles);
11132+
spin_unlock(&root->root_item_lock);
1111211133

1111311134
isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
1111411135

fs/btrfs/ioctl.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ static u32 get_extent_max_capacity(const struct extent_map *em)
12391239
}
12401240

12411241
static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
1242-
bool locked)
1242+
u32 extent_thresh, u64 newer_than, bool locked)
12431243
{
12441244
struct extent_map *next;
12451245
bool ret = false;
@@ -1249,11 +1249,12 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
12491249
return false;
12501250

12511251
/*
1252-
* We want to check if the next extent can be merged with the current
1253-
* one, which can be an extent created in a past generation, so we pass
1254-
* a minimum generation of 0 to defrag_lookup_extent().
1252+
* Here we need to pass @newer_then when checking the next extent, or
1253+
* we will hit a case we mark current extent for defrag, but the next
1254+
* one will not be a target.
1255+
* This will just cause extra IO without really reducing the fragments.
12551256
*/
1256-
next = defrag_lookup_extent(inode, em->start + em->len, 0, locked);
1257+
next = defrag_lookup_extent(inode, em->start + em->len, newer_than, locked);
12571258
/* No more em or hole */
12581259
if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
12591260
goto out;
@@ -1265,6 +1266,13 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
12651266
*/
12661267
if (next->len >= get_extent_max_capacity(em))
12671268
goto out;
1269+
/* Skip older extent */
1270+
if (next->generation < newer_than)
1271+
goto out;
1272+
/* Also check extent size */
1273+
if (next->len >= extent_thresh)
1274+
goto out;
1275+
12681276
ret = true;
12691277
out:
12701278
free_extent_map(next);
@@ -1470,7 +1478,7 @@ static int defrag_collect_targets(struct btrfs_inode *inode,
14701478
goto next;
14711479

14721480
next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em,
1473-
locked);
1481+
extent_thresh, newer_than, locked);
14741482
if (!next_mergeable) {
14751483
struct defrag_target_range *last;
14761484

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)

fs/btrfs/zoned.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,6 @@ struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
18011801

18021802
map = em->map_lookup;
18031803
/* We only support single profile for now */
1804-
ASSERT(map->num_stripes == 1);
18051804
device = map->stripes[0].dev;
18061805

18071806
free_extent_map(em);
@@ -1976,18 +1975,16 @@ int btrfs_zone_finish(struct btrfs_block_group *block_group)
19761975

19771976
bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
19781977
{
1978+
struct btrfs_fs_info *fs_info = fs_devices->fs_info;
19791979
struct btrfs_device *device;
19801980
bool ret = false;
19811981

1982-
if (!btrfs_is_zoned(fs_devices->fs_info))
1982+
if (!btrfs_is_zoned(fs_info))
19831983
return true;
19841984

1985-
/* Non-single profiles are not supported yet */
1986-
ASSERT((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0);
1987-
19881985
/* Check if there is a device with active zones left */
1989-
mutex_lock(&fs_devices->device_list_mutex);
1990-
list_for_each_entry(device, &fs_devices->devices, dev_list) {
1986+
mutex_lock(&fs_info->chunk_mutex);
1987+
list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
19911988
struct btrfs_zoned_device_info *zinfo = device->zone_info;
19921989

19931990
if (!device->bdev)
@@ -1999,7 +1996,7 @@ bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
19991996
break;
20001997
}
20011998
}
2002-
mutex_unlock(&fs_devices->device_list_mutex);
1999+
mutex_unlock(&fs_info->chunk_mutex);
20032000

20042001
return ret;
20052002
}

0 commit comments

Comments
 (0)