Skip to content

Commit 4c8e027

Browse files
Tang Junhuigregkh
authored andcommitted
bcache: fix for data collapse after re-attaching an attached device
[ Upstream commit 73ac105 ] 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: Tang Junhui <[email protected]> Reviewed-by: Michael Lyle <[email protected]> Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 311e314 commit 4c8e027

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

drivers/md/bcache/bcache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ void bcache_write_super(struct cache_set *);
906906

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

909-
int bch_cached_dev_attach(struct cached_dev *, struct cache_set *);
909+
int bch_cached_dev_attach(struct cached_dev *, struct cache_set *, uint8_t *);
910910
void bch_cached_dev_detach(struct cached_dev *);
911911
void bch_cached_dev_run(struct cached_dev *);
912912
void bcache_device_stop(struct bcache_device *);

drivers/md/bcache/super.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,8 @@ void bch_cached_dev_detach(struct cached_dev *dc)
939939
cached_dev_put(dc);
940940
}
941941

942-
int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
942+
int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c,
943+
uint8_t *set_uuid)
943944
{
944945
uint32_t rtime = cpu_to_le32(get_seconds());
945946
struct uuid_entry *u;
@@ -948,7 +949,8 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
948949

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

951-
if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
952+
if ((set_uuid && memcmp(set_uuid, c->sb.set_uuid, 16)) ||
953+
(!set_uuid && memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16)))
952954
return -ENOENT;
953955

954956
if (dc->disk.c) {
@@ -1190,7 +1192,7 @@ static void register_bdev(struct cache_sb *sb, struct page *sb_page,
11901192

11911193
list_add(&dc->list, &uncached_devices);
11921194
list_for_each_entry(c, &bch_cache_sets, list)
1193-
bch_cached_dev_attach(dc, c);
1195+
bch_cached_dev_attach(dc, c, NULL);
11941196

11951197
if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
11961198
BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
@@ -1712,7 +1714,7 @@ static void run_cache_set(struct cache_set *c)
17121714
bcache_write_super(c);
17131715

17141716
list_for_each_entry_safe(dc, t, &uncached_devices, list)
1715-
bch_cached_dev_attach(dc, c);
1717+
bch_cached_dev_attach(dc, c, NULL);
17161718

17171719
flash_devs_run(c);
17181720

drivers/md/bcache/sysfs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,13 @@ STORE(__cached_dev)
265265
}
266266

267267
if (attr == &sysfs_attach) {
268-
if (bch_parse_uuid(buf, dc->sb.set_uuid) < 16)
268+
uint8_t set_uuid[16];
269+
270+
if (bch_parse_uuid(buf, set_uuid) < 16)
269271
return -EINVAL;
270272

271273
list_for_each_entry(c, &bch_cache_sets, list) {
272-
v = bch_cached_dev_attach(dc, c);
274+
v = bch_cached_dev_attach(dc, c, set_uuid);
273275
if (!v)
274276
return size;
275277
}

0 commit comments

Comments
 (0)