All of lore.kernel.org
 help / color / mirror / Atom feed
From: mingzhe.zou@easystack.cn
To: colyli@suse.de, linux-bcache@vger.kernel.org
Cc: zoumingzhe@qq.com
Subject: [PATCH v2] bcache: limit multiple flash devices size
Date: Fri, 16 Sep 2022 16:53:02 +0800	[thread overview]
Message-ID: <20220916085302.9504-1-mingzhe.zou@easystack.cn> (raw)

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

Bcache allows multiple flash devices to be created on the same cache.
We can create multiple flash devices, and the total size larger than
cache device's actual size.
```
[root@zou ~]# lsblk /dev/vdd
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdd        252:48   0  100G  0 disk
[root@zou ~]# echo 50G > /sys/block/vdd/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdd
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdd        252:48   0  100G  0 disk
└─bcache1  251:128  0   50G  0 disk
[root@zou ~]# echo 50G > /sys/block/vdd/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdd
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdd        252:48   0  100G  0 disk
├─bcache2  251:256  0   50G  0 disk
└─bcache1  251:128  0   50G  0 disk
[root@zou ~]# echo 50G > /sys/block/vdd/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdd
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdd        252:48   0  100G  0 disk
├─bcache3  251:256  0   50G  0 disk
├─bcache2  251:256  0   50G  0 disk
└─bcache1  251:128  0   50G  0 disk
```

This patch will limit the total size of multi-flash device, until no
free space to create a new flash device with an error.
```
[root@zou ~]# lsblk /dev/vdd
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdd        252:48   0  100G  0 disk
[root@zou ~]# echo 50G > /sys/block/vdd/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdd
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdd        252:48   0  100G  0 disk
└─bcache1  251:128  0   50G  0 disk
[root@zou ~]# echo 50G > /sys/block/vdd/bcache/set/flash_vol_create
[root@zou ~]# lsblk /dev/vdd
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdd        252:48   0  100G  0 disk
├─bcache2  251:256  0 39.9G  0 disk
└─bcache1  251:128  0   50G  0 disk
[root@zou ~]# echo 50G > /sys/block/vdd/bcache/set/flash_vol_create
-bash: echo: write error: No space left on device
[root@zou ~]# lsblk /dev/vdd
NAME       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdd        252:48   0  100G  0 disk
├─bcache2  251:256  0 39.9G  0 disk
└─bcache1  251:128  0   50G  0 disk
```

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

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 214a384dc1d7..d4b3ab078ad3 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1581,13 +1581,32 @@ static int flash_devs_run(struct cache_set *c)
 
 static inline sector_t flash_dev_max_sectors(struct cache_set *c)
 {
+	sector_t sectors;
+	struct uuid_entry *u;
+	struct bcache_device *d;
 	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 * FLASH_DEV_AVAILABLE_RATIO);
+	sectors = bucket_to_sector(c, avail_nbuckets / 100 * FLASH_DEV_AVAILABLE_RATIO);
+
+	/*
+	 * Although 10% of buckets are forced to reserve, it should
+	 * be able to meet the needs of gc and btree node allocation.
+	 * But the remaining buckets are not necessarily allocatable
+	 * free buckets. Because some of the buckets might be cached
+	 * dirty data. At the same time, the space that has been
+	 * allocated to other flash devices must be considered.
+	 */
+	for (u = c->uuids; u < c->uuids + c->nr_uuids && sectors > 0; u++) {
+		if (!(d = c->devices[u - c->uuids]))
+			continue;
+		sectors -= min(UUID_FLASH_ONLY(u) ? u->sectors :
+			       bcache_dev_sectors_dirty(d), sectors);
+	}
+	return sectors;
 }
 
 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
@@ -1612,6 +1631,10 @@ int bch_flash_dev_create(struct cache_set *c, uint64_t size)
 
 	SET_UUID_FLASH_ONLY(u, 1);
 	u->sectors = min(flash_dev_max_sectors(c), size >> 9);
+	if (!u->sectors) {
+		pr_err("Can't create volume, no free space");
+		return -ENOSPC;
+	}
 
 	bch_uuid_write(c);
 
-- 
2.17.1


                 reply	other threads:[~2022-09-16  8:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220916085302.9504-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.