On 1/26/2021 10:24 AM, Naohiro Aota wrote:
For an ZONED volume, a block group maps to a zone of the device. For
deleted unused block groups, the zone of the block group can be reset to
rewind the zone write pointer at the start of the zone.

Reviewed-by: Josef Bacik <jo...@toxicpanda.com>
Signed-off-by: Naohiro Aota <naohiro.a...@wdc.com>

Reviewed-by: Anand Jain <anand.j...@oracle.com>

Thanks, Anand


---
  fs/btrfs/block-group.c |  8 ++++++--
  fs/btrfs/extent-tree.c | 17 ++++++++++++-----
  fs/btrfs/zoned.h       | 16 ++++++++++++++++
  3 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index f38817a82901..9801df6cbfd8 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -1403,8 +1403,12 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info 
*fs_info)
                if (!async_trim_enabled && btrfs_test_opt(fs_info, 
DISCARD_ASYNC))
                        goto flip_async;
- /* DISCARD can flip during remount */
-               trimming = btrfs_test_opt(fs_info, DISCARD_SYNC);
+               /*
+                * DISCARD can flip during remount. In ZONED mode, we need
+                * to reset sequential required zones.
+                */
+               trimming = btrfs_test_opt(fs_info, DISCARD_SYNC) ||
+                               btrfs_is_zoned(fs_info);
/* Implicit trim during transaction commit. */
                if (trimming)
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 36a105697781..4c126e4ada27 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -1298,6 +1298,9 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, 
u64 bytenr,
stripe = bbio->stripes;
                for (i = 0; i < bbio->num_stripes; i++, stripe++) {
+                       struct btrfs_device *dev = stripe->dev;
+                       u64 physical = stripe->physical;
+                       u64 length = stripe->length;
                        u64 bytes;
                        struct request_queue *req_q;
@@ -1305,14 +1308,18 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
                                ASSERT(btrfs_test_opt(fs_info, DEGRADED));
                                continue;
                        }
+
                        req_q = bdev_get_queue(stripe->dev->bdev);
-                       if (!blk_queue_discard(req_q))
+                       /* Zone reset in ZONED mode */
+                       if (btrfs_can_zone_reset(dev, physical, length))
+                               ret = btrfs_reset_device_zone(dev, physical,
+                                                             length, &bytes);
+                       else if (blk_queue_discard(req_q))
+                               ret = btrfs_issue_discard(dev->bdev, physical,
+                                                         length, &bytes);
+                       else
                                continue;
- ret = btrfs_issue_discard(stripe->dev->bdev,
-                                                 stripe->physical,
-                                                 stripe->length,
-                                                 &bytes);
                        if (!ret) {
                                discarded_bytes += bytes;
                        } else if (ret != -EOPNOTSUPP) {
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index b2ce16de0c22..331951978487 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -210,4 +210,20 @@ static inline bool btrfs_check_super_location(struct 
btrfs_device *device, u64 p
        return device->zone_info == NULL || !btrfs_dev_is_sequential(device, 
pos);
  }
+static inline bool btrfs_can_zone_reset(struct btrfs_device *device,
+                                       u64 physical, u64 length)
+{
+       u64 zone_size;
+
+       if (!btrfs_dev_is_sequential(device, physical))
+               return false;
+
+       zone_size = device->zone_info->zone_size;
+       if (!IS_ALIGNED(physical, zone_size) ||
+           !IS_ALIGNED(length, zone_size))
+               return false;
+
+       return true;
+}
+
  #endif


Reply via email to