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

ubd: open the backing files in ubd_add

Opening the backing device only when the block device is opened is
a bit weird as no one configures block devices to not use them.
Opend them at add time, close them at remove time and remove the
now superflous opened counter as remove can simply check for
disk_openers.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarRichard Weinberger <richard@nod.at>
Link: https://lore.kernel.org/r/20240222072417.3773131-8-hch@lst.deSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent f3c17dcc
...@@ -108,8 +108,6 @@ static inline void ubd_set_bit(__u64 bit, unsigned char *data) ...@@ -108,8 +108,6 @@ static inline void ubd_set_bit(__u64 bit, unsigned char *data)
static DEFINE_MUTEX(ubd_lock); static DEFINE_MUTEX(ubd_lock);
static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */ static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */
static int ubd_open(struct gendisk *disk, blk_mode_t mode);
static void ubd_release(struct gendisk *disk);
static int ubd_ioctl(struct block_device *bdev, blk_mode_t mode, static int ubd_ioctl(struct block_device *bdev, blk_mode_t mode,
unsigned int cmd, unsigned long arg); unsigned int cmd, unsigned long arg);
static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo); static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
...@@ -118,8 +116,6 @@ static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo); ...@@ -118,8 +116,6 @@ static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
static const struct block_device_operations ubd_blops = { static const struct block_device_operations ubd_blops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.open = ubd_open,
.release = ubd_release,
.ioctl = ubd_ioctl, .ioctl = ubd_ioctl,
.compat_ioctl = blkdev_compat_ptr_ioctl, .compat_ioctl = blkdev_compat_ptr_ioctl,
.getgeo = ubd_getgeo, .getgeo = ubd_getgeo,
...@@ -152,7 +148,6 @@ struct ubd { ...@@ -152,7 +148,6 @@ struct ubd {
* backing or the cow file. */ * backing or the cow file. */
char *file; char *file;
char *serial; char *serial;
int count;
int fd; int fd;
__u64 size; __u64 size;
struct openflags boot_openflags; struct openflags boot_openflags;
...@@ -178,7 +173,6 @@ struct ubd { ...@@ -178,7 +173,6 @@ struct ubd {
#define DEFAULT_UBD { \ #define DEFAULT_UBD { \
.file = NULL, \ .file = NULL, \
.serial = NULL, \ .serial = NULL, \
.count = 0, \
.fd = -1, \ .fd = -1, \
.size = -1, \ .size = -1, \
.boot_openflags = OPEN_FLAGS, \ .boot_openflags = OPEN_FLAGS, \
...@@ -873,6 +867,13 @@ static int ubd_add(int n, char **error_out) ...@@ -873,6 +867,13 @@ static int ubd_add(int n, char **error_out)
goto out; goto out;
} }
err = ubd_open_dev(ubd_dev);
if (err) {
pr_err("ubd%c: Can't open \"%s\": errno = %d\n",
'a' + n, ubd_dev->file, -err);
goto out;
}
ubd_dev->size = ROUND_BLOCK(ubd_dev->size); ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
ubd_dev->tag_set.ops = &ubd_mq_ops; ubd_dev->tag_set.ops = &ubd_mq_ops;
...@@ -884,7 +885,7 @@ static int ubd_add(int n, char **error_out) ...@@ -884,7 +885,7 @@ static int ubd_add(int n, char **error_out)
err = blk_mq_alloc_tag_set(&ubd_dev->tag_set); err = blk_mq_alloc_tag_set(&ubd_dev->tag_set);
if (err) if (err)
goto out; goto out_close;
disk = blk_mq_alloc_disk(&ubd_dev->tag_set, &lim, ubd_dev); disk = blk_mq_alloc_disk(&ubd_dev->tag_set, &lim, ubd_dev);
if (IS_ERR(disk)) { if (IS_ERR(disk)) {
...@@ -919,6 +920,8 @@ static int ubd_add(int n, char **error_out) ...@@ -919,6 +920,8 @@ static int ubd_add(int n, char **error_out)
put_disk(disk); put_disk(disk);
out_cleanup_tags: out_cleanup_tags:
blk_mq_free_tag_set(&ubd_dev->tag_set); blk_mq_free_tag_set(&ubd_dev->tag_set);
out_close:
ubd_close_dev(ubd_dev);
out: out:
return err; return err;
} }
...@@ -1014,13 +1017,14 @@ static int ubd_remove(int n, char **error_out) ...@@ -1014,13 +1017,14 @@ static int ubd_remove(int n, char **error_out)
if(ubd_dev->file == NULL) if(ubd_dev->file == NULL)
goto out; goto out;
if (ubd_dev->disk) {
/* you cannot remove a open disk */ /* you cannot remove a open disk */
err = -EBUSY; err = -EBUSY;
if(ubd_dev->count > 0) if (disk_openers(ubd_dev->disk))
goto out; goto out;
if (ubd_dev->disk) {
del_gendisk(ubd_dev->disk); del_gendisk(ubd_dev->disk);
ubd_close_dev(ubd_dev);
put_disk(ubd_dev->disk); put_disk(ubd_dev->disk);
} }
...@@ -1143,36 +1147,6 @@ static int __init ubd_driver_init(void){ ...@@ -1143,36 +1147,6 @@ static int __init ubd_driver_init(void){
device_initcall(ubd_driver_init); device_initcall(ubd_driver_init);
static int ubd_open(struct gendisk *disk, blk_mode_t mode)
{
struct ubd *ubd_dev = disk->private_data;
int err = 0;
mutex_lock(&ubd_mutex);
if(ubd_dev->count == 0){
err = ubd_open_dev(ubd_dev);
if(err){
printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
disk->disk_name, ubd_dev->file, -err);
goto out;
}
}
ubd_dev->count++;
out:
mutex_unlock(&ubd_mutex);
return err;
}
static void ubd_release(struct gendisk *disk)
{
struct ubd *ubd_dev = disk->private_data;
mutex_lock(&ubd_mutex);
if(--ubd_dev->count == 0)
ubd_close_dev(ubd_dev);
mutex_unlock(&ubd_mutex);
}
static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask, static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
__u64 *cow_offset, unsigned long *bitmap, __u64 *cow_offset, unsigned long *bitmap,
__u64 bitmap_offset, unsigned long *bitmap_words, __u64 bitmap_offset, unsigned long *bitmap_words,
......
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