Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bcache: don't attach backing with duplicate UUID

This can happen e.g. during disk cloning.

This is an incomplete fix: it does not catch duplicate UUIDs earlier
when things are still unattached. It does not unregister the device.
Further changes to cope better with this are planned but conflict with
Coly's ongoing improvements to handling device errors. In the meantime,
one can manually stop the device after this has happened.

Attempts to attach a duplicate device result in:

[ 136.372404] loop: module loaded
[ 136.424461] bcache: register_bdev() registered backing device loop0
[ 136.424464] bcache: bch_cached_dev_attach() Tried to attach loop0 but duplicate UUID already attached

My test procedure is:

dd if=/dev/sdb1 of=imgfile bs=1024 count=262144
losetup -f imgfile

Signed-off-by: Michael Lyle <mlyle@lyle.org>
Reviewed-by: Tang Junhui <tang.junhui@zte.com.cn>
Cc: <stable@vger.kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Michael Lyle and committed by
Jens Axboe
86755b7a cc40daf9

+11
+11
drivers/md/bcache/super.c
··· 963 963 uint32_t rtime = cpu_to_le32(get_seconds()); 964 964 struct uuid_entry *u; 965 965 char buf[BDEVNAME_SIZE]; 966 + struct cached_dev *exist_dc, *t; 966 967 967 968 bdevname(dc->bdev, buf); 968 969 ··· 986 985 pr_err("Couldn't attach %s: block size less than set's block size", 987 986 buf); 988 987 return -EINVAL; 988 + } 989 + 990 + /* Check whether already attached */ 991 + list_for_each_entry_safe(exist_dc, t, &c->cached_devs, list) { 992 + if (!memcmp(dc->sb.uuid, exist_dc->sb.uuid, 16)) { 993 + pr_err("Tried to attach %s but duplicate UUID already attached", 994 + buf); 995 + 996 + return -EINVAL; 997 + } 989 998 } 990 999 991 1000 u = uuid_find(c, dc->sb.uuid);