Skip to content

Commit 1776ad1

Browse files
lorddoskiaskdave
authored andcommitted
btrfs: Refactor btrfs_rmap_block to improve readability
Move variables to appropriate scope. Remove last BUG_ON in the function and rework error handling accordingly. Make the duplicate detection code more straightforward. Use in_range macro. And give variables more descriptive name by explicitly distinguishing between IO stripe size (size recorded in the chunk item) and data stripe size (the size of an actual stripe, constituting a logical chunk/block group). Signed-off-by: Nikolay Borisov <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent bf2e2eb commit 1776ad1

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

fs/btrfs/block-group.c

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,34 +1582,43 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
15821582
struct map_lookup *map;
15831583
u64 *buf;
15841584
u64 bytenr;
1585-
u64 length;
1586-
u64 stripe_nr;
1587-
u64 rmap_len;
1588-
int i, j, nr = 0;
1585+
u64 data_stripe_length;
1586+
u64 io_stripe_size;
1587+
int i, nr = 0;
1588+
int ret = 0;
15891589

15901590
em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
15911591
if (IS_ERR(em))
15921592
return -EIO;
15931593

15941594
map = em->map_lookup;
1595-
length = em->len;
1596-
rmap_len = map->stripe_len;
1595+
data_stripe_length = em->len;
1596+
io_stripe_size = map->stripe_len;
15971597

15981598
if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1599-
length = div_u64(length, map->num_stripes / map->sub_stripes);
1599+
data_stripe_length = div_u64(data_stripe_length,
1600+
map->num_stripes / map->sub_stripes);
16001601
else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1601-
length = div_u64(length, map->num_stripes);
1602+
data_stripe_length = div_u64(data_stripe_length, map->num_stripes);
16021603
else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1603-
length = div_u64(length, nr_data_stripes(map));
1604-
rmap_len = map->stripe_len * nr_data_stripes(map);
1604+
data_stripe_length = div_u64(data_stripe_length,
1605+
nr_data_stripes(map));
1606+
io_stripe_size = map->stripe_len * nr_data_stripes(map);
16051607
}
16061608

16071609
buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
1608-
BUG_ON(!buf); /* -ENOMEM */
1610+
if (!buf) {
1611+
ret = -ENOMEM;
1612+
goto out;
1613+
}
16091614

16101615
for (i = 0; i < map->num_stripes; i++) {
1611-
if (map->stripes[i].physical > physical ||
1612-
map->stripes[i].physical + length <= physical)
1616+
bool already_inserted = false;
1617+
u64 stripe_nr;
1618+
int j;
1619+
1620+
if (!in_range(physical, map->stripes[i].physical,
1621+
data_stripe_length))
16131622
continue;
16141623

16151624
stripe_nr = physical - map->stripes[i].physical;
@@ -1627,24 +1636,26 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
16271636
* instead of map->stripe_len
16281637
*/
16291638

1630-
bytenr = chunk_start + stripe_nr * rmap_len;
1631-
WARN_ON(nr >= map->num_stripes);
1639+
bytenr = chunk_start + stripe_nr * io_stripe_size;
1640+
1641+
/* Ensure we don't add duplicate addresses */
16321642
for (j = 0; j < nr; j++) {
1633-
if (buf[j] == bytenr)
1643+
if (buf[j] == bytenr) {
1644+
already_inserted = true;
16341645
break;
1646+
}
16351647
}
1636-
if (j == nr) {
1637-
WARN_ON(nr >= map->num_stripes);
1648+
1649+
if (!already_inserted)
16381650
buf[nr++] = bytenr;
1639-
}
16401651
}
16411652

16421653
*logical = buf;
16431654
*naddrs = nr;
1644-
*stripe_len = rmap_len;
1645-
1655+
*stripe_len = io_stripe_size;
1656+
out:
16461657
free_extent_map(em);
1647-
return 0;
1658+
return ret;
16481659
}
16491660

16501661
static int exclude_super_stripes(struct btrfs_block_group *cache)

0 commit comments

Comments
 (0)