Commit d6263387 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jens Axboe

block: support delayed holder registration

device mapper needs to register holders before it is ready to do I/O.
Currently it does so by registering the disk early, which can leave
the disk and queue in a weird half state where the queue is registered
with the disk, except for sysfs and the elevator.  And this state has
been a bit promlematic before, and will get more so when sorting out
the responsibilities between the queue and the disk.

Support registering holders on an initialized but not registered disk
instead by delaying the sysfs registration until the disk is registered.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarMike Snitzer <snitzer@redhat.com>
Link: https://lore.kernel.org/r/20210804094147.459763-5-hch@lst.deSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 0dbcfe24
...@@ -447,6 +447,16 @@ static void register_disk(struct device *parent, struct gendisk *disk, ...@@ -447,6 +447,16 @@ static void register_disk(struct device *parent, struct gendisk *disk,
kobject_create_and_add("holders", &ddev->kobj); kobject_create_and_add("holders", &ddev->kobj);
disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj); disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
/*
* XXX: this is a mess, can't wait for real error handling in add_disk.
* Make sure ->slave_dir is NULL if we failed some of the registration
* so that the cleanup in bd_unlink_disk_holder works properly.
*/
if (bd_register_pending_holders(disk) < 0) {
kobject_put(disk->slave_dir);
disk->slave_dir = NULL;
}
if (disk->flags & GENHD_FL_HIDDEN) if (disk->flags & GENHD_FL_HIDDEN)
return; return;
......
...@@ -28,6 +28,19 @@ static void del_symlink(struct kobject *from, struct kobject *to) ...@@ -28,6 +28,19 @@ static void del_symlink(struct kobject *from, struct kobject *to)
sysfs_remove_link(from, kobject_name(to)); sysfs_remove_link(from, kobject_name(to));
} }
static int __link_disk_holder(struct block_device *bdev, struct gendisk *disk)
{
int ret;
ret = add_symlink(disk->slave_dir, bdev_kobj(bdev));
if (ret)
return ret;
ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
if (ret)
del_symlink(disk->slave_dir, bdev_kobj(bdev));
return ret;
}
/** /**
* bd_link_disk_holder - create symlinks between holding disk and slave bdev * bd_link_disk_holder - create symlinks between holding disk and slave bdev
* @bdev: the claimed slave bdev * @bdev: the claimed slave bdev
...@@ -66,7 +79,7 @@ int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) ...@@ -66,7 +79,7 @@ int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
WARN_ON_ONCE(!bdev->bd_holder); WARN_ON_ONCE(!bdev->bd_holder);
/* FIXME: remove the following once add_disk() handles errors */ /* FIXME: remove the following once add_disk() handles errors */
if (WARN_ON(!disk->slave_dir || !bdev->bd_holder_dir)) if (WARN_ON(!bdev->bd_holder_dir))
goto out_unlock; goto out_unlock;
holder = bd_find_holder_disk(bdev, disk); holder = bd_find_holder_disk(bdev, disk);
...@@ -84,28 +97,28 @@ int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) ...@@ -84,28 +97,28 @@ int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
INIT_LIST_HEAD(&holder->list); INIT_LIST_HEAD(&holder->list);
holder->bdev = bdev; holder->bdev = bdev;
holder->refcnt = 1; holder->refcnt = 1;
if (disk->slave_dir) {
ret = add_symlink(disk->slave_dir, bdev_kobj(bdev)); ret = __link_disk_holder(bdev, disk);
if (ret) if (ret) {
goto out_free; kfree(holder);
ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
if (ret)
goto out_del;
list_add(&holder->list, &disk->slave_bdevs);
goto out_unlock; goto out_unlock;
}
}
out_del: list_add(&holder->list, &disk->slave_bdevs);
del_symlink(disk->slave_dir, bdev_kobj(bdev));
out_free:
kfree(holder);
out_unlock: out_unlock:
mutex_unlock(&disk->open_mutex); mutex_unlock(&disk->open_mutex);
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(bd_link_disk_holder); EXPORT_SYMBOL_GPL(bd_link_disk_holder);
static void __unlink_disk_holder(struct block_device *bdev,
struct gendisk *disk)
{
del_symlink(disk->slave_dir, bdev_kobj(bdev));
del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
}
/** /**
* bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder() * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
* @bdev: the calimed slave bdev * @bdev: the calimed slave bdev
...@@ -123,11 +136,32 @@ void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk) ...@@ -123,11 +136,32 @@ void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
mutex_lock(&disk->open_mutex); mutex_lock(&disk->open_mutex);
holder = bd_find_holder_disk(bdev, disk); holder = bd_find_holder_disk(bdev, disk);
if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) { if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
del_symlink(disk->slave_dir, bdev_kobj(bdev)); if (disk->slave_dir)
del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); __unlink_disk_holder(bdev, disk);
list_del_init(&holder->list); list_del_init(&holder->list);
kfree(holder); kfree(holder);
} }
mutex_unlock(&disk->open_mutex); mutex_unlock(&disk->open_mutex);
} }
EXPORT_SYMBOL_GPL(bd_unlink_disk_holder); EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
int bd_register_pending_holders(struct gendisk *disk)
{
struct bd_holder_disk *holder;
int ret;
mutex_lock(&disk->open_mutex);
list_for_each_entry(holder, &disk->slave_bdevs, list) {
ret = __link_disk_holder(holder->bdev, disk);
if (ret)
goto out_undo;
}
mutex_unlock(&disk->open_mutex);
return 0;
out_undo:
list_for_each_entry_continue_reverse(holder, &disk->slave_bdevs, list)
__unlink_disk_holder(holder->bdev, disk);
mutex_unlock(&disk->open_mutex);
return ret;
}
...@@ -323,6 +323,7 @@ long compat_blkdev_ioctl(struct file *, unsigned, unsigned long); ...@@ -323,6 +323,7 @@ long compat_blkdev_ioctl(struct file *, unsigned, unsigned long);
#ifdef CONFIG_BLOCK_HOLDER_DEPRECATED #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk); int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk); void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
int bd_register_pending_holders(struct gendisk *disk);
#else #else
static inline int bd_link_disk_holder(struct block_device *bdev, static inline int bd_link_disk_holder(struct block_device *bdev,
struct gendisk *disk) struct gendisk *disk)
...@@ -333,6 +334,10 @@ static inline void bd_unlink_disk_holder(struct block_device *bdev, ...@@ -333,6 +334,10 @@ static inline void bd_unlink_disk_holder(struct block_device *bdev,
struct gendisk *disk) struct gendisk *disk)
{ {
} }
static inline int bd_register_pending_holders(struct gendisk *disk)
{
return 0;
}
#endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */ #endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */
dev_t part_devt(struct gendisk *disk, u8 partno); dev_t part_devt(struct gendisk *disk, u8 partno);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment