Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 73ac105b authored by Tang Junhui's avatar Tang Junhui Committed by Jens Axboe
Browse files

bcache: fix for data collapse after re-attaching an attached device



back-end device sdm has already attached a cache_set with ID
f67ebe1f-f8bc-4d73-bfe5-9dc88607f119, then try to attach with
another cache set, and it returns with an error:
[root]# cd /sys/block/sdm/bcache
[root]# echo 5ccd0a63-148e-48b8-afa2-aca9cbd6279f > attach
-bash: echo: write error: Invalid argument

After that, execute a command to modify the label of bcache
device:
[root]# echo data_disk1 > label

Then we reboot the system, when the system power on, the back-end
device can not attach to cache_set, a messages show in the log:
Feb  5 12:05:52 ceph152 kernel: [922385.508498] bcache:
bch_cached_dev_attach() couldn't find uuid for sdm in set

In sysfs_attach(), dc->sb.set_uuid was assigned to the value
which input through sysfs, no matter whether it is success
or not in bch_cached_dev_attach(). For example, If the back-end
device has already attached to an cache set, bch_cached_dev_attach()
would fail, but dc->sb.set_uuid was changed. Then modify the
label of bcache device, it will call bch_write_bdev_super(),
which would write the dc->sb.set_uuid to the super block, so we
record a wrong cache set ID in the super block, after the system
reboot, the cache set couldn't find the uuid of the back-end
device, so the bcache device couldn't exist and use any more.

In this patch, we don't assigned cache set ID to dc->sb.set_uuid
in sysfs_attach() directly, but input it into bch_cached_dev_attach(),
and assigned dc->sb.set_uuid to the cache set ID after the back-end
device attached to the cache set successful.

Signed-off-by: default avatarTang Junhui <tang.junhui@zte.com.cn>
Reviewed-by: default avatarMichael Lyle <mlyle@lyle.org>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 7f4fc93d
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -924,7 +924,7 @@ void bcache_write_super(struct cache_set *);


int bch_flash_dev_create(struct cache_set *c, uint64_t size);
int bch_flash_dev_create(struct cache_set *c, uint64_t size);


int bch_cached_dev_attach(struct cached_dev *, struct cache_set *);
int bch_cached_dev_attach(struct cached_dev *, struct cache_set *, uint8_t *);
void bch_cached_dev_detach(struct cached_dev *);
void bch_cached_dev_detach(struct cached_dev *);
void bch_cached_dev_run(struct cached_dev *);
void bch_cached_dev_run(struct cached_dev *);
void bcache_device_stop(struct bcache_device *);
void bcache_device_stop(struct bcache_device *);
+6 −4
Original line number Original line Diff line number Diff line
@@ -957,7 +957,8 @@ void bch_cached_dev_detach(struct cached_dev *dc)
	cached_dev_put(dc);
	cached_dev_put(dc);
}
}


int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c,
			  uint8_t *set_uuid)
{
{
	uint32_t rtime = cpu_to_le32(get_seconds());
	uint32_t rtime = cpu_to_le32(get_seconds());
	struct uuid_entry *u;
	struct uuid_entry *u;
@@ -965,7 +966,8 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)


	bdevname(dc->bdev, buf);
	bdevname(dc->bdev, buf);


	if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
	if ((set_uuid && memcmp(set_uuid, c->sb.set_uuid, 16)) ||
	    (!set_uuid && memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16)))
		return -ENOENT;
		return -ENOENT;


	if (dc->disk.c) {
	if (dc->disk.c) {
@@ -1194,7 +1196,7 @@ static void register_bdev(struct cache_sb *sb, struct page *sb_page,


	list_add(&dc->list, &uncached_devices);
	list_add(&dc->list, &uncached_devices);
	list_for_each_entry(c, &bch_cache_sets, list)
	list_for_each_entry(c, &bch_cache_sets, list)
		bch_cached_dev_attach(dc, c);
		bch_cached_dev_attach(dc, c, NULL);


	if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
	if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
	    BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
	    BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
@@ -1716,7 +1718,7 @@ static void run_cache_set(struct cache_set *c)
	bcache_write_super(c);
	bcache_write_super(c);


	list_for_each_entry_safe(dc, t, &uncached_devices, list)
	list_for_each_entry_safe(dc, t, &uncached_devices, list)
		bch_cached_dev_attach(dc, c);
		bch_cached_dev_attach(dc, c, NULL);


	flash_devs_run(c);
	flash_devs_run(c);


+4 −2
Original line number Original line Diff line number Diff line
@@ -272,12 +272,14 @@ STORE(__cached_dev)
	}
	}


	if (attr == &sysfs_attach) {
	if (attr == &sysfs_attach) {
		if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16)
		uint8_t		set_uuid[16];

		if (bch_parse_uuid(buf, set_uuid) < 16)
			return -EINVAL;
			return -EINVAL;


		v = -ENOENT;
		v = -ENOENT;
		list_for_each_entry(c, &bch_cache_sets, list) {
		list_for_each_entry(c, &bch_cache_sets, list) {
			v = bch_cached_dev_attach(dc, c);
			v = bch_cached_dev_attach(dc, c, set_uuid);
			if (!v)
			if (!v)
				return size;
				return size;
		}
		}