Skip to content

Commit de17add

Browse files
naotakdave
authored andcommitted
btrfs: zoned: implement copying for zoned device-replace
This is 3/4 patch to implement device-replace on zoned filesystems. This commit implements copying. To do this, it tracks the write pointer during the device replace process. As device-replace's copy process is smart enough to only copy used extents on the source device, we have to fill the gap to honor the sequential write requirement in the target device. The device-replace process on zoned filesystems must copy or clone all the extents in the source device exactly once. So, we need to ensure allocations started just before the dev-replace process to have their corresponding extent information in the B-trees. finish_extent_writes_for_zoned() implements that functionality, which basically is the removed code in the commit 042528f ("Btrfs: fix block group remaining RO forever after error during device replace"). Reviewed-by: Josef Bacik <[email protected]> Signed-off-by: Naohiro Aota <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 6143c23 commit de17add

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

fs/btrfs/scrub.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ struct scrub_ctx {
166166
int pages_per_rd_bio;
167167

168168
int is_dev_replace;
169+
u64 write_pointer;
169170

170171
struct scrub_bio *wr_curr_bio;
171172
struct mutex wr_lock;
@@ -1619,6 +1620,25 @@ static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
16191620
return scrub_add_page_to_wr_bio(sblock->sctx, spage);
16201621
}
16211622

1623+
static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
1624+
{
1625+
int ret = 0;
1626+
u64 length;
1627+
1628+
if (!btrfs_is_zoned(sctx->fs_info))
1629+
return 0;
1630+
1631+
if (sctx->write_pointer < physical) {
1632+
length = physical - sctx->write_pointer;
1633+
1634+
ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev,
1635+
sctx->write_pointer, length);
1636+
if (!ret)
1637+
sctx->write_pointer = physical;
1638+
}
1639+
return ret;
1640+
}
1641+
16221642
static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
16231643
struct scrub_page *spage)
16241644
{
@@ -1641,6 +1661,13 @@ static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
16411661
if (sbio->page_count == 0) {
16421662
struct bio *bio;
16431663

1664+
ret = fill_writer_pointer_gap(sctx,
1665+
spage->physical_for_dev_replace);
1666+
if (ret) {
1667+
mutex_unlock(&sctx->wr_lock);
1668+
return ret;
1669+
}
1670+
16441671
sbio->physical = spage->physical_for_dev_replace;
16451672
sbio->logical = spage->logical;
16461673
sbio->dev = sctx->wr_tgtdev;
@@ -1702,6 +1729,9 @@ static void scrub_wr_submit(struct scrub_ctx *sctx)
17021729
* doubled the write performance on spinning disks when measured
17031730
* with Linux 3.5 */
17041731
btrfsic_submit_bio(sbio->bio);
1732+
1733+
if (btrfs_is_zoned(sctx->fs_info))
1734+
sctx->write_pointer = sbio->physical + sbio->page_count * PAGE_SIZE;
17051735
}
17061736

17071737
static void scrub_wr_bio_end_io(struct bio *bio)
@@ -3025,6 +3055,20 @@ static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
30253055
return ret < 0 ? ret : 0;
30263056
}
30273057

3058+
static void sync_replace_for_zoned(struct scrub_ctx *sctx)
3059+
{
3060+
if (!btrfs_is_zoned(sctx->fs_info))
3061+
return;
3062+
3063+
sctx->flush_all_writes = true;
3064+
scrub_submit(sctx);
3065+
mutex_lock(&sctx->wr_lock);
3066+
scrub_wr_submit(sctx);
3067+
mutex_unlock(&sctx->wr_lock);
3068+
3069+
wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3070+
}
3071+
30283072
static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
30293073
struct map_lookup *map,
30303074
struct btrfs_device *scrub_dev,
@@ -3165,6 +3209,14 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
31653209
*/
31663210
blk_start_plug(&plug);
31673211

3212+
if (sctx->is_dev_replace &&
3213+
btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) {
3214+
mutex_lock(&sctx->wr_lock);
3215+
sctx->write_pointer = physical;
3216+
mutex_unlock(&sctx->wr_lock);
3217+
sctx->flush_all_writes = true;
3218+
}
3219+
31683220
/*
31693221
* now find all extents for each stripe and scrub them
31703222
*/
@@ -3353,6 +3405,9 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
33533405
if (ret)
33543406
goto out;
33553407

3408+
if (sctx->is_dev_replace)
3409+
sync_replace_for_zoned(sctx);
3410+
33563411
if (extent_logical + extent_len <
33573412
key.objectid + bytes) {
33583413
if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
@@ -3475,6 +3530,25 @@ static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
34753530
return ret;
34763531
}
34773532

3533+
static int finish_extent_writes_for_zoned(struct btrfs_root *root,
3534+
struct btrfs_block_group *cache)
3535+
{
3536+
struct btrfs_fs_info *fs_info = cache->fs_info;
3537+
struct btrfs_trans_handle *trans;
3538+
3539+
if (!btrfs_is_zoned(fs_info))
3540+
return 0;
3541+
3542+
btrfs_wait_block_group_reservations(cache);
3543+
btrfs_wait_nocow_writers(cache);
3544+
btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, cache->length);
3545+
3546+
trans = btrfs_join_transaction(root);
3547+
if (IS_ERR(trans))
3548+
return PTR_ERR(trans);
3549+
return btrfs_commit_transaction(trans);
3550+
}
3551+
34783552
static noinline_for_stack
34793553
int scrub_enumerate_chunks(struct scrub_ctx *sctx,
34803554
struct btrfs_device *scrub_dev, u64 start, u64 end)
@@ -3629,6 +3703,16 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
36293703
* group is not RO.
36303704
*/
36313705
ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace);
3706+
if (!ret && sctx->is_dev_replace) {
3707+
ret = finish_extent_writes_for_zoned(root, cache);
3708+
if (ret) {
3709+
btrfs_dec_block_group_ro(cache);
3710+
scrub_pause_off(fs_info);
3711+
btrfs_put_block_group(cache);
3712+
break;
3713+
}
3714+
}
3715+
36323716
if (ret == 0) {
36333717
ro_set = 1;
36343718
} else if (ret == -ENOSPC && !sctx->is_dev_replace) {

fs/btrfs/volumes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5978,7 +5978,7 @@ static bool is_block_group_to_copy(struct btrfs_fs_info *fs_info, u64 logical)
59785978
struct btrfs_block_group *cache;
59795979
bool ret;
59805980

5981-
/* Non-ZONED mode does not use "to_copy" flag */
5981+
/* Non zoned filesystem does not use "to_copy" flag */
59825982
if (!btrfs_is_zoned(fs_info))
59835983
return false;
59845984

fs/btrfs/zoned.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,3 +1376,12 @@ void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
13761376
ASSERT(cache->meta_write_pointer == eb->start + eb->len);
13771377
cache->meta_write_pointer = eb->start;
13781378
}
1379+
1380+
int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1381+
{
1382+
if (!btrfs_dev_is_sequential(device, physical))
1383+
return -EOPNOTSUPP;
1384+
1385+
return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1386+
length >> SECTOR_SHIFT, GFP_NOFS, 0);
1387+
}

fs/btrfs/zoned.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
5555
struct btrfs_block_group **cache_ret);
5656
void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
5757
struct extent_buffer *eb);
58+
int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length);
5859
#else /* CONFIG_BLK_DEV_ZONED */
5960
static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
6061
struct blk_zone *zone)
@@ -169,6 +170,12 @@ static inline void btrfs_revert_meta_write_pointer(
169170
{
170171
}
171172

173+
static inline int btrfs_zoned_issue_zeroout(struct btrfs_device *device,
174+
u64 physical, u64 length)
175+
{
176+
return -EOPNOTSUPP;
177+
}
178+
172179
#endif
173180

174181
static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)

0 commit comments

Comments
 (0)