Skip to content

Commit 12e6cce

Browse files
committed
Merge tag 'for-6.5-rc6-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux
Pull btrfs fixes from David Sterba: - fix infinite loop in readdir(), could happen in a big directory when files get renamed during enumeration - fix extent map handling of skipped pinned ranges - fix a corner case when handling ordered extent length - fix a potential crash when balance cancel races with pause - verify correct uuid when starting scrub or device replace * tag 'for-6.5-rc6-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux: btrfs: fix incorrect splitting in btrfs_drop_extent_map_range btrfs: fix BUG_ON condition in btrfs_cancel_balance btrfs: only subtract from len_to_oe_boundary when it is tracking an extent btrfs: fix replace/scrub failure with metadata_uuid btrfs: fix infinite directory reads
2 parents b5cab28 + c962098 commit 12e6cce

File tree

8 files changed

+113
-62
lines changed

8 files changed

+113
-62
lines changed

fs/btrfs/ctree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ struct btrfs_drop_extents_args {
443443

444444
struct btrfs_file_private {
445445
void *filldir_buf;
446+
u64 last_index;
446447
struct extent_state *llseek_cached_state;
447448
};
448449

fs/btrfs/delayed-inode.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,7 @@ int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
16321632
}
16331633

16341634
bool btrfs_readdir_get_delayed_items(struct inode *inode,
1635+
u64 last_index,
16351636
struct list_head *ins_list,
16361637
struct list_head *del_list)
16371638
{
@@ -1651,14 +1652,14 @@ bool btrfs_readdir_get_delayed_items(struct inode *inode,
16511652

16521653
mutex_lock(&delayed_node->mutex);
16531654
item = __btrfs_first_delayed_insertion_item(delayed_node);
1654-
while (item) {
1655+
while (item && item->index <= last_index) {
16551656
refcount_inc(&item->refs);
16561657
list_add_tail(&item->readdir_list, ins_list);
16571658
item = __btrfs_next_delayed_item(item);
16581659
}
16591660

16601661
item = __btrfs_first_delayed_deletion_item(delayed_node);
1661-
while (item) {
1662+
while (item && item->index <= last_index) {
16621663
refcount_inc(&item->refs);
16631664
list_add_tail(&item->readdir_list, del_list);
16641665
item = __btrfs_next_delayed_item(item);

fs/btrfs/delayed-inode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info);
148148

149149
/* Used for readdir() */
150150
bool btrfs_readdir_get_delayed_items(struct inode *inode,
151+
u64 last_index,
151152
struct list_head *ins_list,
152153
struct list_head *del_list);
153154
void btrfs_readdir_put_delayed_items(struct inode *inode,

fs/btrfs/extent_io.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,30 @@ static void submit_extent_page(struct btrfs_bio_ctrl *bio_ctrl,
902902
size -= len;
903903
pg_offset += len;
904904
disk_bytenr += len;
905-
bio_ctrl->len_to_oe_boundary -= len;
905+
906+
/*
907+
* len_to_oe_boundary defaults to U32_MAX, which isn't page or
908+
* sector aligned. alloc_new_bio() then sets it to the end of
909+
* our ordered extent for writes into zoned devices.
910+
*
911+
* When len_to_oe_boundary is tracking an ordered extent, we
912+
* trust the ordered extent code to align things properly, and
913+
* the check above to cap our write to the ordered extent
914+
* boundary is correct.
915+
*
916+
* When len_to_oe_boundary is U32_MAX, the cap above would
917+
* result in a 4095 byte IO for the last page right before
918+
* we hit the bio limit of UINT_MAX. bio_add_page() has all
919+
* the checks required to make sure we don't overflow the bio,
920+
* and we should just ignore len_to_oe_boundary completely
921+
* unless we're using it to track an ordered extent.
922+
*
923+
* It's pretty hard to make a bio sized U32_MAX, but it can
924+
* happen when the page cache is able to feed us contiguous
925+
* pages for large extents.
926+
*/
927+
if (bio_ctrl->len_to_oe_boundary != U32_MAX)
928+
bio_ctrl->len_to_oe_boundary -= len;
906929

907930
/* Ordered extent boundary: move on to a new bio. */
908931
if (bio_ctrl->len_to_oe_boundary == 0)

fs/btrfs/extent_map.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,6 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end,
760760

761761
if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
762762
start = em_end;
763-
if (end != (u64)-1)
764-
len = start + len - em_end;
765763
goto next;
766764
}
767765

@@ -829,8 +827,8 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end,
829827
if (!split)
830828
goto remove_em;
831829
}
832-
split->start = start + len;
833-
split->len = em_end - (start + len);
830+
split->start = end;
831+
split->len = em_end - end;
834832
split->block_start = em->block_start;
835833
split->flags = flags;
836834
split->compress_type = em->compress_type;

fs/btrfs/inode.c

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5872,6 +5872,74 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
58725872
return d_splice_alias(inode, dentry);
58735873
}
58745874

5875+
/*
5876+
* Find the highest existing sequence number in a directory and then set the
5877+
* in-memory index_cnt variable to the first free sequence number.
5878+
*/
5879+
static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
5880+
{
5881+
struct btrfs_root *root = inode->root;
5882+
struct btrfs_key key, found_key;
5883+
struct btrfs_path *path;
5884+
struct extent_buffer *leaf;
5885+
int ret;
5886+
5887+
key.objectid = btrfs_ino(inode);
5888+
key.type = BTRFS_DIR_INDEX_KEY;
5889+
key.offset = (u64)-1;
5890+
5891+
path = btrfs_alloc_path();
5892+
if (!path)
5893+
return -ENOMEM;
5894+
5895+
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5896+
if (ret < 0)
5897+
goto out;
5898+
/* FIXME: we should be able to handle this */
5899+
if (ret == 0)
5900+
goto out;
5901+
ret = 0;
5902+
5903+
if (path->slots[0] == 0) {
5904+
inode->index_cnt = BTRFS_DIR_START_INDEX;
5905+
goto out;
5906+
}
5907+
5908+
path->slots[0]--;
5909+
5910+
leaf = path->nodes[0];
5911+
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5912+
5913+
if (found_key.objectid != btrfs_ino(inode) ||
5914+
found_key.type != BTRFS_DIR_INDEX_KEY) {
5915+
inode->index_cnt = BTRFS_DIR_START_INDEX;
5916+
goto out;
5917+
}
5918+
5919+
inode->index_cnt = found_key.offset + 1;
5920+
out:
5921+
btrfs_free_path(path);
5922+
return ret;
5923+
}
5924+
5925+
static int btrfs_get_dir_last_index(struct btrfs_inode *dir, u64 *index)
5926+
{
5927+
if (dir->index_cnt == (u64)-1) {
5928+
int ret;
5929+
5930+
ret = btrfs_inode_delayed_dir_index_count(dir);
5931+
if (ret) {
5932+
ret = btrfs_set_inode_index_count(dir);
5933+
if (ret)
5934+
return ret;
5935+
}
5936+
}
5937+
5938+
*index = dir->index_cnt;
5939+
5940+
return 0;
5941+
}
5942+
58755943
/*
58765944
* All this infrastructure exists because dir_emit can fault, and we are holding
58775945
* the tree lock when doing readdir. For now just allocate a buffer and copy
@@ -5884,10 +5952,17 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
58845952
static int btrfs_opendir(struct inode *inode, struct file *file)
58855953
{
58865954
struct btrfs_file_private *private;
5955+
u64 last_index;
5956+
int ret;
5957+
5958+
ret = btrfs_get_dir_last_index(BTRFS_I(inode), &last_index);
5959+
if (ret)
5960+
return ret;
58875961

58885962
private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
58895963
if (!private)
58905964
return -ENOMEM;
5965+
private->last_index = last_index;
58915966
private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
58925967
if (!private->filldir_buf) {
58935968
kfree(private);
@@ -5954,7 +6029,8 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
59546029

59556030
INIT_LIST_HEAD(&ins_list);
59566031
INIT_LIST_HEAD(&del_list);
5957-
put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
6032+
put = btrfs_readdir_get_delayed_items(inode, private->last_index,
6033+
&ins_list, &del_list);
59586034

59596035
again:
59606036
key.type = BTRFS_DIR_INDEX_KEY;
@@ -5972,6 +6048,8 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
59726048
break;
59736049
if (found_key.offset < ctx->pos)
59746050
continue;
6051+
if (found_key.offset > private->last_index)
6052+
break;
59756053
if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
59766054
continue;
59776055
di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
@@ -6107,57 +6185,6 @@ static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
61076185
return dirty ? btrfs_dirty_inode(BTRFS_I(inode)) : 0;
61086186
}
61096187

6110-
/*
6111-
* find the highest existing sequence number in a directory
6112-
* and then set the in-memory index_cnt variable to reflect
6113-
* free sequence numbers
6114-
*/
6115-
static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
6116-
{
6117-
struct btrfs_root *root = inode->root;
6118-
struct btrfs_key key, found_key;
6119-
struct btrfs_path *path;
6120-
struct extent_buffer *leaf;
6121-
int ret;
6122-
6123-
key.objectid = btrfs_ino(inode);
6124-
key.type = BTRFS_DIR_INDEX_KEY;
6125-
key.offset = (u64)-1;
6126-
6127-
path = btrfs_alloc_path();
6128-
if (!path)
6129-
return -ENOMEM;
6130-
6131-
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6132-
if (ret < 0)
6133-
goto out;
6134-
/* FIXME: we should be able to handle this */
6135-
if (ret == 0)
6136-
goto out;
6137-
ret = 0;
6138-
6139-
if (path->slots[0] == 0) {
6140-
inode->index_cnt = BTRFS_DIR_START_INDEX;
6141-
goto out;
6142-
}
6143-
6144-
path->slots[0]--;
6145-
6146-
leaf = path->nodes[0];
6147-
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6148-
6149-
if (found_key.objectid != btrfs_ino(inode) ||
6150-
found_key.type != BTRFS_DIR_INDEX_KEY) {
6151-
inode->index_cnt = BTRFS_DIR_START_INDEX;
6152-
goto out;
6153-
}
6154-
6155-
inode->index_cnt = found_key.offset + 1;
6156-
out:
6157-
btrfs_free_path(path);
6158-
return ret;
6159-
}
6160-
61616188
/*
61626189
* helper to find a free sequence number in a given directory. This current
61636190
* code is very simple, later versions will do smarter things in the btree

fs/btrfs/scrub.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr
605605
btrfs_stack_header_bytenr(header), logical);
606606
return;
607607
}
608-
if (memcmp(header->fsid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE) != 0) {
608+
if (memcmp(header->fsid, fs_info->fs_devices->metadata_uuid,
609+
BTRFS_FSID_SIZE) != 0) {
609610
bitmap_set(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree);
610611
bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree);
611612
btrfs_warn_rl(fs_info,

fs/btrfs/volumes.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4638,8 +4638,7 @@ int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
46384638
}
46394639
}
46404640

4641-
BUG_ON(fs_info->balance_ctl ||
4642-
test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4641+
ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
46434642
atomic_dec(&fs_info->balance_cancel_req);
46444643
mutex_unlock(&fs_info->balance_mutex);
46454644
return 0;

0 commit comments

Comments
 (0)