Linux-bcache Archive mirror
 help / color / mirror / Atom feed
From: mingzhe.zou@easystack.cn
To: colyli@suse.de, linux-bcache@vger.kernel.org
Cc: zoumingzhe@qq.com, mingzhe <mingzhe.zou@easystack.cn>
Subject: [PATCH] bcache: limit create flash device size
Date: Wed,  7 Sep 2022 19:29:13 +0800	[thread overview]
Message-ID: <20220907112913.16488-1-mingzhe.zou@easystack.cn> (raw)

From: mingzhe <mingzhe.zou@easystack.cn>

Currently, size is specified and not checked when creating a flash device.
This will cause a problem, IO maybe hang when creating a flash device with
the actual size of the device.

```
	if (attr == &sysfs_flash_vol_create) {
		int r;
		uint64_t v;

		strtoi_h_or_return(buf, v);

		r = bch_flash_dev_create(c, v);
		if (r)
			return r;
	}
```

Because the flash device needs some space for superblock, journal and btree.
If the size of data reaches the available size, the new IO cannot allocate
space and will hang. At this time, the gc thread will be started frequently.

Even more unreasonable, we can create flash devices larger than actual size.

```
[root@zou ~]# echo 2G > /sys/block/vdb/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdb
NAME       MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vdb        252:16   0   1G  0 disk
└─bcache0  251:0    0   2G  0 disk
```

This patch will limit the size of flash device, reserving at least 5% of
available size for the btree.

```
[root@zou ~]# echo 2G > /sys/block/vdb/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdb
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdb        252:16   0    1G  0 disk
└─bcache0  251:0    0  950M  0 disk
```

Signed-off-by: mingzhe <mingzhe.zou@easystack.cn>
---
 drivers/md/bcache/super.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index ba3909bb6bea..f41e09e0e8ee 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1579,6 +1579,17 @@ static int flash_devs_run(struct cache_set *c)
 	return ret;
 }
 
+static inline sector_t flash_dev_max_sectors(struct cache_set *c)
+{
+	size_t avail_nbuckets;
+	struct cache *ca = c->cache;
+	size_t first_bucket = ca->sb.first_bucket;
+	size_t njournal_buckets = ca->sb.njournal_buckets;
+
+	avail_nbuckets = c->nbuckets - first_bucket - njournal_buckets;
+	return bucket_to_sector(c, avail_nbuckets / 100 * 95);
+}
+
 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
 {
 	struct uuid_entry *u;
@@ -1600,7 +1611,7 @@ int bch_flash_dev_create(struct cache_set *c, uint64_t size)
 	u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds());
 
 	SET_UUID_FLASH_ONLY(u, 1);
-	u->sectors = size >> 9;
+	u->sectors = min(flash_dev_max_sectors(c), size >> 9);
 
 	bch_uuid_write(c);
 
-- 
2.17.1


             reply	other threads:[~2022-09-07 11:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-07 11:29 mingzhe.zou [this message]
2022-09-07 17:15 ` [PATCH] bcache: limit create flash device size Coly Li
2022-09-08  5:55   ` Zou Mingzhe

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=20220907112913.16488-1-mingzhe.zou@easystack.cn \
    --to=mingzhe.zou@easystack.cn \
    --cc=colyli@suse.de \
    --cc=linux-bcache@vger.kernel.org \
    --cc=zoumingzhe@qq.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 read-only IMAP folder(s) and NNTP newsgroup(s).