Skip to content

Commit d8e3fb1

Browse files
naotakdave
authored andcommitted
btrfs: zoned: use ZONE_APPEND write for zoned mode
Enable zone append writing for zoned mode. When using zone append, a bio is issued to the start of a target zone and the device decides to place it inside the zone. Upon completion the device reports the actual written position back to the host. Three parts are necessary to enable zone append mode. First, modify the bio to use REQ_OP_ZONE_APPEND in btrfs_submit_bio_hook() and adjust the bi_sector to point the beginning of the zone. Second, record the returned physical address (and disk/partno) to the ordered extent in end_bio_extent_writepage() after the bio has been completed. We cannot resolve the physical address to the logical address because we can neither take locks nor allocate a buffer in this end_bio context. So, we need to record the physical address to resolve it later in btrfs_finish_ordered_io(). And finally, rewrite the logical addresses of the extent mapping and checksum data according to the physical address using btrfs_rmap_block. If the returned address matches the originally allocated address, we can skip this rewriting process. Reviewed-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 24533f6 commit d8e3fb1

File tree

8 files changed

+129
-3
lines changed

8 files changed

+129
-3
lines changed

fs/btrfs/extent_io.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,6 +2735,7 @@ static void end_bio_extent_writepage(struct bio *bio)
27352735
u64 start;
27362736
u64 end;
27372737
struct bvec_iter_all iter_all;
2738+
bool first_bvec = true;
27382739

27392740
ASSERT(!bio_flagged(bio, BIO_CLONED));
27402741
bio_for_each_segment_all(bvec, bio, iter_all) {
@@ -2761,6 +2762,11 @@ static void end_bio_extent_writepage(struct bio *bio)
27612762
start = page_offset(page);
27622763
end = start + bvec->bv_offset + bvec->bv_len - 1;
27632764

2765+
if (first_bvec) {
2766+
btrfs_record_physical_zoned(inode, start, bio);
2767+
first_bvec = false;
2768+
}
2769+
27642770
end_extent_writepage(page, error, start, end);
27652771
end_page_writeback(page);
27662772
}
@@ -3664,6 +3670,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
36643670
struct extent_map *em;
36653671
int ret = 0;
36663672
int nr = 0;
3673+
u32 opf = REQ_OP_WRITE;
36673674
const unsigned int write_flags = wbc_to_write_flags(wbc);
36683675
bool compressed;
36693676

@@ -3710,6 +3717,10 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
37103717

37113718
/* Note that em_end from extent_map_end() is exclusive */
37123719
iosize = min(em_end, end + 1) - cur;
3720+
3721+
if (btrfs_use_zone_append(inode, em))
3722+
opf = REQ_OP_ZONE_APPEND;
3723+
37133724
free_extent_map(em);
37143725
em = NULL;
37153726

@@ -3735,8 +3746,8 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode,
37353746
page->index, cur, end);
37363747
}
37373748

3738-
ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc,
3739-
page, disk_bytenr, iosize,
3749+
ret = submit_extent_page(opf | write_flags, wbc, page,
3750+
disk_bytenr, iosize,
37403751
cur - page_offset(page), &epd->bio,
37413752
end_bio_extent_writepage,
37423753
0, 0, 0, false);

fs/btrfs/file.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2168,8 +2168,12 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
21682168
* commit waits for their completion, to avoid data loss if we fsync,
21692169
* the current transaction commits before the ordered extents complete
21702170
* and a power failure happens right after that.
2171+
*
2172+
* For zoned filesystem, if a write IO uses a ZONE_APPEND command, the
2173+
* logical address recorded in the ordered extent may change. We need
2174+
* to wait for the IO to stabilize the logical address.
21712175
*/
2172-
if (full_sync) {
2176+
if (full_sync || btrfs_is_zoned(fs_info)) {
21732177
ret = btrfs_wait_ordered_range(inode, start, len);
21742178
} else {
21752179
/*

fs/btrfs/inode.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "delalloc-space.h"
5151
#include "block-group.h"
5252
#include "space-info.h"
53+
#include "zoned.h"
5354

5455
struct btrfs_iget_args {
5556
u64 ino;
@@ -2874,6 +2875,9 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
28742875
goto out;
28752876
}
28762877

2878+
if (ordered_extent->disk)
2879+
btrfs_rewrite_logical_zoned(ordered_extent);
2880+
28772881
btrfs_free_io_failure_record(inode, start, end);
28782882

28792883
if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {

fs/btrfs/ordered-data.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ static int __btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset
199199
entry->compress_type = compress_type;
200200
entry->truncated_len = (u64)-1;
201201
entry->qgroup_rsv = ret;
202+
entry->physical = (u64)-1;
203+
entry->disk = NULL;
204+
entry->partno = (u8)-1;
202205

203206
ASSERT(type == BTRFS_ORDERED_REGULAR ||
204207
type == BTRFS_ORDERED_NOCOW ||

fs/btrfs/ordered-data.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ struct btrfs_ordered_extent {
139139
struct completion completion;
140140
struct btrfs_work flush_work;
141141
struct list_head work_list;
142+
143+
/*
144+
* Used to reverse-map physical address returned from ZONE_APPEND write
145+
* command in a workqueue context
146+
*/
147+
u64 physical;
148+
struct gendisk *disk;
149+
u8 partno;
142150
};
143151

144152
/*

fs/btrfs/volumes.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6500,6 +6500,20 @@ static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
65006500
btrfs_io_bio(bio)->device = dev;
65016501
bio->bi_end_io = btrfs_end_bio;
65026502
bio->bi_iter.bi_sector = physical >> 9;
6503+
/*
6504+
* For zone append writing, bi_sector must point the beginning of the
6505+
* zone
6506+
*/
6507+
if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
6508+
if (btrfs_dev_is_sequential(dev, physical)) {
6509+
u64 zone_start = round_down(physical, fs_info->zone_size);
6510+
6511+
bio->bi_iter.bi_sector = zone_start >> SECTOR_SHIFT;
6512+
} else {
6513+
bio->bi_opf &= ~REQ_OP_ZONE_APPEND;
6514+
bio->bi_opf |= REQ_OP_WRITE;
6515+
}
6516+
}
65036517
btrfs_debug_in_rcu(fs_info,
65046518
"btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
65056519
bio_op(bio), bio->bi_opf, bio->bi_iter.bi_sector,

fs/btrfs/zoned.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,3 +1247,73 @@ bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em)
12471247

12481248
return ret;
12491249
}
1250+
1251+
void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1252+
struct bio *bio)
1253+
{
1254+
struct btrfs_ordered_extent *ordered;
1255+
const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1256+
1257+
if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1258+
return;
1259+
1260+
ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1261+
if (WARN_ON(!ordered))
1262+
return;
1263+
1264+
ordered->physical = physical;
1265+
ordered->disk = bio->bi_disk;
1266+
ordered->partno = bio->bi_partno;
1267+
1268+
btrfs_put_ordered_extent(ordered);
1269+
}
1270+
1271+
void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1272+
{
1273+
struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1274+
struct btrfs_fs_info *fs_info = inode->root->fs_info;
1275+
struct extent_map_tree *em_tree;
1276+
struct extent_map *em;
1277+
struct btrfs_ordered_sum *sum;
1278+
struct block_device *bdev;
1279+
u64 orig_logical = ordered->disk_bytenr;
1280+
u64 *logical = NULL;
1281+
int nr, stripe_len;
1282+
1283+
/* Zoned devices should not have partitions. So, we can assume it is 0 */
1284+
ASSERT(ordered->partno == 0);
1285+
bdev = bdgrab(ordered->disk->part0);
1286+
if (WARN_ON(!bdev))
1287+
return;
1288+
1289+
if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, bdev,
1290+
ordered->physical, &logical, &nr,
1291+
&stripe_len)))
1292+
goto out;
1293+
1294+
WARN_ON(nr != 1);
1295+
1296+
if (orig_logical == *logical)
1297+
goto out;
1298+
1299+
ordered->disk_bytenr = *logical;
1300+
1301+
em_tree = &inode->extent_tree;
1302+
write_lock(&em_tree->lock);
1303+
em = search_extent_mapping(em_tree, ordered->file_offset,
1304+
ordered->num_bytes);
1305+
em->block_start = *logical;
1306+
free_extent_map(em);
1307+
write_unlock(&em_tree->lock);
1308+
1309+
list_for_each_entry(sum, &ordered->list, list) {
1310+
if (*logical < orig_logical)
1311+
sum->bytenr -= orig_logical - *logical;
1312+
else
1313+
sum->bytenr += *logical - orig_logical;
1314+
}
1315+
1316+
out:
1317+
kfree(logical);
1318+
bdput(bdev);
1319+
}

fs/btrfs/zoned.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ void btrfs_redirty_list_add(struct btrfs_transaction *trans,
4747
struct extent_buffer *eb);
4848
void btrfs_free_redirty_list(struct btrfs_transaction *trans);
4949
bool btrfs_use_zone_append(struct btrfs_inode *inode, struct extent_map *em);
50+
void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
51+
struct bio *bio);
52+
void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered);
5053
#else /* CONFIG_BLK_DEV_ZONED */
5154
static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
5255
struct blk_zone *zone)
@@ -139,6 +142,15 @@ static inline bool btrfs_use_zone_append(struct btrfs_inode *inode,
139142
{
140143
return false;
141144
}
145+
146+
static inline void btrfs_record_physical_zoned(struct inode *inode,
147+
u64 file_offset, struct bio *bio)
148+
{
149+
}
150+
151+
static inline void btrfs_rewrite_logical_zoned(
152+
struct btrfs_ordered_extent *ordered) { }
153+
142154
#endif
143155

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

0 commit comments

Comments
 (0)