Skip to content

Commit fb5d1d3

Browse files
Christoph Hellwigaxboe
authored andcommitted
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: Christoph Hellwig <[email protected]> Reviewed-by: Richard Weinberger <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent f3c17dc commit fb5d1d3

File tree

1 file changed

+16
-42
lines changed

1 file changed

+16
-42
lines changed

arch/um/drivers/ubd_kern.c

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ static inline void ubd_set_bit(__u64 bit, unsigned char *data)
108108
static DEFINE_MUTEX(ubd_lock);
109109
static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */
110110

111-
static int ubd_open(struct gendisk *disk, blk_mode_t mode);
112-
static void ubd_release(struct gendisk *disk);
113111
static int ubd_ioctl(struct block_device *bdev, blk_mode_t mode,
114112
unsigned int cmd, unsigned long arg);
115113
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);
118116

119117
static const struct block_device_operations ubd_blops = {
120118
.owner = THIS_MODULE,
121-
.open = ubd_open,
122-
.release = ubd_release,
123119
.ioctl = ubd_ioctl,
124120
.compat_ioctl = blkdev_compat_ptr_ioctl,
125121
.getgeo = ubd_getgeo,
@@ -152,7 +148,6 @@ struct ubd {
152148
* backing or the cow file. */
153149
char *file;
154150
char *serial;
155-
int count;
156151
int fd;
157152
__u64 size;
158153
struct openflags boot_openflags;
@@ -178,7 +173,6 @@ struct ubd {
178173
#define DEFAULT_UBD { \
179174
.file = NULL, \
180175
.serial = NULL, \
181-
.count = 0, \
182176
.fd = -1, \
183177
.size = -1, \
184178
.boot_openflags = OPEN_FLAGS, \
@@ -873,6 +867,13 @@ static int ubd_add(int n, char **error_out)
873867
goto out;
874868
}
875869

870+
err = ubd_open_dev(ubd_dev);
871+
if (err) {
872+
pr_err("ubd%c: Can't open \"%s\": errno = %d\n",
873+
'a' + n, ubd_dev->file, -err);
874+
goto out;
875+
}
876+
876877
ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
877878

878879
ubd_dev->tag_set.ops = &ubd_mq_ops;
@@ -884,7 +885,7 @@ static int ubd_add(int n, char **error_out)
884885

885886
err = blk_mq_alloc_tag_set(&ubd_dev->tag_set);
886887
if (err)
887-
goto out;
888+
goto out_close;
888889

889890
disk = blk_mq_alloc_disk(&ubd_dev->tag_set, &lim, ubd_dev);
890891
if (IS_ERR(disk)) {
@@ -919,6 +920,8 @@ static int ubd_add(int n, char **error_out)
919920
put_disk(disk);
920921
out_cleanup_tags:
921922
blk_mq_free_tag_set(&ubd_dev->tag_set);
923+
out_close:
924+
ubd_close_dev(ubd_dev);
922925
out:
923926
return err;
924927
}
@@ -1014,13 +1017,14 @@ static int ubd_remove(int n, char **error_out)
10141017
if(ubd_dev->file == NULL)
10151018
goto out;
10161019

1017-
/* you cannot remove a open disk */
1018-
err = -EBUSY;
1019-
if(ubd_dev->count > 0)
1020-
goto out;
1021-
10221020
if (ubd_dev->disk) {
1021+
/* you cannot remove a open disk */
1022+
err = -EBUSY;
1023+
if (disk_openers(ubd_dev->disk))
1024+
goto out;
1025+
10231026
del_gendisk(ubd_dev->disk);
1027+
ubd_close_dev(ubd_dev);
10241028
put_disk(ubd_dev->disk);
10251029
}
10261030

@@ -1143,36 +1147,6 @@ static int __init ubd_driver_init(void){
11431147

11441148
device_initcall(ubd_driver_init);
11451149

1146-
static int ubd_open(struct gendisk *disk, blk_mode_t mode)
1147-
{
1148-
struct ubd *ubd_dev = disk->private_data;
1149-
int err = 0;
1150-
1151-
mutex_lock(&ubd_mutex);
1152-
if(ubd_dev->count == 0){
1153-
err = ubd_open_dev(ubd_dev);
1154-
if(err){
1155-
printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
1156-
disk->disk_name, ubd_dev->file, -err);
1157-
goto out;
1158-
}
1159-
}
1160-
ubd_dev->count++;
1161-
out:
1162-
mutex_unlock(&ubd_mutex);
1163-
return err;
1164-
}
1165-
1166-
static void ubd_release(struct gendisk *disk)
1167-
{
1168-
struct ubd *ubd_dev = disk->private_data;
1169-
1170-
mutex_lock(&ubd_mutex);
1171-
if(--ubd_dev->count == 0)
1172-
ubd_close_dev(ubd_dev);
1173-
mutex_unlock(&ubd_mutex);
1174-
}
1175-
11761150
static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
11771151
__u64 *cow_offset, unsigned long *bitmap,
11781152
__u64 bitmap_offset, unsigned long *bitmap_words,

0 commit comments

Comments
 (0)