From: Naohiro Aota <naohiro.aota@wdc.com>
To: linux-btrfs@vger.kernel.org, David Sterba <dsterba@suse.com>
Cc: Chris Mason <clm@fb.com>, Josef Bacik <josef@toxicpanda.com>,
Nikolay Borisov <nborisov@suse.com>,
Damien Le Moal <damien.lemoal@wdc.com>,
Matias Bjorling <Matias.Bjorling@wdc.com>,
Johannes Thumshirn <jthumshirn@suse.de>,
Hannes Reinecke <hare@suse.com>,
linux-fsdevel@vger.kernel.org,
Naohiro Aota <naohiro.aota@wdc.com>
Subject: [PATCH v3 08/15] btrfs-progs: support discarding zoned device
Date: Tue, 20 Aug 2019 13:52:51 +0900 [thread overview]
Message-ID: <20190820045258.1571640-9-naohiro.aota@wdc.com> (raw)
In-Reply-To: <20190820045258.1571640-1-naohiro.aota@wdc.com>
All zones of zoned block devices should be reset before writing. Support
this by introducing PREP_DEVICE_HMZONED.
This commit export discard_blocks() and use it from
btrfs_discard_all_zones().
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
common/device-utils.c | 23 +++++++++++++++++++++--
common/device-utils.h | 2 ++
common/hmzoned.c | 27 +++++++++++++++++++++++++++
common/hmzoned.h | 5 +++++
4 files changed, 55 insertions(+), 2 deletions(-)
diff --git a/common/device-utils.c b/common/device-utils.c
index 7fa9386f4677..c7046c22a9fb 100644
--- a/common/device-utils.c
+++ b/common/device-utils.c
@@ -29,6 +29,7 @@
#include "common/internal.h"
#include "common/messages.h"
#include "common/utils.h"
+#include "common/hmzoned.h"
#ifndef BLKDISCARD
#define BLKDISCARD _IO(0x12,119)
@@ -49,7 +50,7 @@ static int discard_range(int fd, u64 start, u64 len)
/*
* Discard blocks in the given range in 1G chunks, the process is interruptible
*/
-static int discard_blocks(int fd, u64 start, u64 len)
+int discard_blocks(int fd, u64 start, u64 len)
{
while (len > 0) {
/* 1G granularity */
@@ -155,6 +156,7 @@ out:
int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret,
u64 max_block_count, unsigned opflags)
{
+ struct btrfs_zone_info zinfo;
u64 block_count;
struct stat st;
int i, ret;
@@ -173,7 +175,24 @@ int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret,
if (max_block_count)
block_count = min(block_count, max_block_count);
- if (opflags & PREP_DEVICE_DISCARD) {
+ ret = btrfs_get_zone_info(fd, file, opflags & PREP_DEVICE_HMZONED,
+ &zinfo);
+ if (ret < 0)
+ return 1;
+
+ if (opflags & PREP_DEVICE_HMZONED) {
+ printf("Resetting device zones %s (%u zones) ...\n",
+ file, zinfo.nr_zones);
+ /*
+ * We cannot ignore zone discard (reset) errors for a zoned
+ * block device as this could result in the inability to
+ * write to non-empty sequential zones of the device.
+ */
+ if (btrfs_discard_all_zones(fd, &zinfo)) {
+ error("failed to reset device '%s' zones", file);
+ return 1;
+ }
+ } else if (opflags & PREP_DEVICE_DISCARD) {
/*
* We intentionally ignore errors from the discard ioctl. It
* is not necessary for the mkfs functionality but just an
diff --git a/common/device-utils.h b/common/device-utils.h
index d1799323d002..885a46937e0d 100644
--- a/common/device-utils.h
+++ b/common/device-utils.h
@@ -23,7 +23,9 @@
#define PREP_DEVICE_ZERO_END (1U << 0)
#define PREP_DEVICE_DISCARD (1U << 1)
#define PREP_DEVICE_VERBOSE (1U << 2)
+#define PREP_DEVICE_HMZONED (1U << 3)
+int discard_blocks(int fd, u64 start, u64 len);
u64 get_partition_size(const char *dev);
u64 disk_size(const char *path);
u64 btrfs_device_size(int fd, struct stat *st);
diff --git a/common/hmzoned.c b/common/hmzoned.c
index 7114943458ef..70de111f22da 100644
--- a/common/hmzoned.c
+++ b/common/hmzoned.c
@@ -216,3 +216,30 @@ int btrfs_get_zone_info(int fd, const char *file, bool hmzoned,
#endif
return 0;
}
+
+/*
+ * Discard blocks in the zones of a zoned block device.
+ * Process this with zone size granularity so that blocks in
+ * conventional zones are discarded using discard_range and
+ * blocks in sequential zones are discarded though a zone reset.
+ */
+int btrfs_discard_all_zones(int fd, struct btrfs_zone_info *zinfo)
+{
+ unsigned int i;
+
+ /* Zone size granularity */
+ for (i = 0; i < zinfo->nr_zones; i++) {
+ if (zinfo->zones[i].type == BLK_ZONE_TYPE_CONVENTIONAL) {
+ discard_blocks(fd, zinfo->zones[i].start << 9,
+ zinfo->zone_size);
+ } else if (zinfo->zones[i].cond != BLK_ZONE_COND_EMPTY) {
+ struct blk_zone_range range = {
+ zinfo->zones[i].start,
+ zinfo->zone_size >> 9 };
+ if (ioctl(fd, BLKRESETZONE, &range) < 0)
+ return errno;
+ }
+ }
+
+ return 0;
+}
diff --git a/common/hmzoned.h b/common/hmzoned.h
index fbcaaf2da20e..c4e20ae71d21 100644
--- a/common/hmzoned.h
+++ b/common/hmzoned.h
@@ -56,12 +56,17 @@ int btrfs_get_zone_info(int fd, const char *file, bool hmzoned,
#ifdef BTRFS_ZONED
bool zone_is_sequential(struct btrfs_zone_info *zinfo, u64 bytenr);
+int btrfs_discard_all_zones(int fd, struct btrfs_zone_info *zinfo);
#else
static inline bool zone_is_sequential(struct btrfs_zone_info *zinfo,
u64 bytenr)
{
return true;
}
+static inline int btrfs_discard_all_zones(int fd, struct btrfs_zone_info *zinfo)
+{
+ return -EOPNOTSUPP;
+}
#endif /* BTRFS_ZONED */
#endif /* __BTRFS_HMZONED_H__ */
--
2.23.0
next prev parent reply other threads:[~2019-08-20 4:53 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-20 4:52 [PATCH v3 00/15] btrfs-progs: zoned block device support Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 01/15] btrfs-progs: utils: Introduce queue_param helper function Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 02/15] btrfs-progs: introduce raid parameters variables Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 03/15] btrfs-progs: build: Check zoned block device support Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 04/15] btrfs-progs: add new HMZONED feature flag Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 05/15] btrfs-progs: Introduce zone block device helper functions Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 06/15] btrfs-progs: load and check zone information Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 07/15] btrfs-progs: avoid writing super block to sequential zones Naohiro Aota
2019-08-20 4:52 ` Naohiro Aota [this message]
2019-08-20 4:52 ` [PATCH v3 09/15] btrfs-progs: support zero out on zoned block device Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 10/15] btrfs-progs: align device extent allocation to zone boundary Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 11/15] btrfs-progs: do sequential allocation in HMZONED mode Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 12/15] btrfs-progs: redirty clean extent buffers in seq Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 13/15] btrfs-progs: mkfs: Zoned block device support Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 14/15] btrfs-progs: device-add: support HMZONED device Naohiro Aota
2019-08-20 4:52 ` [PATCH v3 15/15] btrfs-progs: introduce support for device replace " Naohiro Aota
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190820045258.1571640-9-naohiro.aota@wdc.com \
--to=naohiro.aota@wdc.com \
--cc=Matias.Bjorling@wdc.com \
--cc=clm@fb.com \
--cc=damien.lemoal@wdc.com \
--cc=dsterba@suse.com \
--cc=hare@suse.com \
--cc=josef@toxicpanda.com \
--cc=jthumshirn@suse.de \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=nborisov@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).