Skip to content

Commit 93ada1b

Browse files
yoavcohaxboe
authored andcommitted
ublk: add UBLK_CMD_TRY_STOP_DEV command
Add a best-effort stop command, UBLK_CMD_TRY_STOP_DEV, which only stops a ublk device when it has no active openers. Unlike UBLK_CMD_STOP_DEV, this command does not disrupt existing users. New opens are blocked only after disk_openers has reached zero; if the device is busy, the command returns -EBUSY and leaves it running. The ub->block_open flag is used only to close a race with an in-progress open and does not otherwise change open behavior. Advertise support via the UBLK_F_SAFE_STOP_DEV feature flag. Signed-off-by: Yoav Cohen <yoav@nvidia.com> Reviewed-by: Ming Lei <ming.lei@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 9e386f4 commit 93ada1b

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

drivers/block/ublk_drv.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#define UBLK_CMD_DEL_DEV_ASYNC _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)
5757
#define UBLK_CMD_UPDATE_SIZE _IOC_NR(UBLK_U_CMD_UPDATE_SIZE)
5858
#define UBLK_CMD_QUIESCE_DEV _IOC_NR(UBLK_U_CMD_QUIESCE_DEV)
59+
#define UBLK_CMD_TRY_STOP_DEV _IOC_NR(UBLK_U_CMD_TRY_STOP_DEV)
5960

6061
#define UBLK_IO_REGISTER_IO_BUF _IOC_NR(UBLK_U_IO_REGISTER_IO_BUF)
6162
#define UBLK_IO_UNREGISTER_IO_BUF _IOC_NR(UBLK_U_IO_UNREGISTER_IO_BUF)
@@ -76,7 +77,8 @@
7677
| UBLK_F_QUIESCE \
7778
| UBLK_F_PER_IO_DAEMON \
7879
| UBLK_F_BUF_REG_OFF_DAEMON \
79-
| (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) ? UBLK_F_INTEGRITY : 0))
80+
| (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) ? UBLK_F_INTEGRITY : 0) \
81+
| UBLK_F_SAFE_STOP_DEV)
8082

8183
#define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
8284
| UBLK_F_USER_RECOVERY_REISSUE \
@@ -243,6 +245,8 @@ struct ublk_device {
243245
struct delayed_work exit_work;
244246
struct work_struct partition_scan_work;
245247

248+
bool block_open; /* protected by open_mutex */
249+
246250
struct ublk_queue *queues[];
247251
};
248252

@@ -984,6 +988,9 @@ static int ublk_open(struct gendisk *disk, blk_mode_t mode)
984988
return -EPERM;
985989
}
986990

991+
if (ub->block_open)
992+
return -ENXIO;
993+
987994
return 0;
988995
}
989996

@@ -3343,7 +3350,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
33433350
ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE |
33443351
UBLK_F_URING_CMD_COMP_IN_TASK |
33453352
UBLK_F_PER_IO_DAEMON |
3346-
UBLK_F_BUF_REG_OFF_DAEMON;
3353+
UBLK_F_BUF_REG_OFF_DAEMON |
3354+
UBLK_F_SAFE_STOP_DEV;
33473355

33483356
/* GET_DATA isn't needed any more with USER_COPY or ZERO COPY */
33493357
if (ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
@@ -3464,6 +3472,34 @@ static void ublk_ctrl_stop_dev(struct ublk_device *ub)
34643472
ublk_stop_dev(ub);
34653473
}
34663474

3475+
static int ublk_ctrl_try_stop_dev(struct ublk_device *ub)
3476+
{
3477+
struct gendisk *disk;
3478+
int ret = 0;
3479+
3480+
disk = ublk_get_disk(ub);
3481+
if (!disk)
3482+
return -ENODEV;
3483+
3484+
mutex_lock(&disk->open_mutex);
3485+
if (disk_openers(disk) > 0) {
3486+
ret = -EBUSY;
3487+
goto unlock;
3488+
}
3489+
ub->block_open = true;
3490+
/* release open_mutex as del_gendisk() will reacquire it */
3491+
mutex_unlock(&disk->open_mutex);
3492+
3493+
ublk_ctrl_stop_dev(ub);
3494+
goto out;
3495+
3496+
unlock:
3497+
mutex_unlock(&disk->open_mutex);
3498+
out:
3499+
ublk_put_disk(disk);
3500+
return ret;
3501+
}
3502+
34673503
static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
34683504
const struct ublksrv_ctrl_cmd *header)
34693505
{
@@ -3859,6 +3895,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
38593895
case UBLK_CMD_END_USER_RECOVERY:
38603896
case UBLK_CMD_UPDATE_SIZE:
38613897
case UBLK_CMD_QUIESCE_DEV:
3898+
case UBLK_CMD_TRY_STOP_DEV:
38623899
mask = MAY_READ | MAY_WRITE;
38633900
break;
38643901
default:
@@ -3972,6 +4009,9 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
39724009
case UBLK_CMD_QUIESCE_DEV:
39734010
ret = ublk_ctrl_quiesce_dev(ub, header);
39744011
break;
4012+
case UBLK_CMD_TRY_STOP_DEV:
4013+
ret = ublk_ctrl_try_stop_dev(ub);
4014+
break;
39754015
default:
39764016
ret = -EOPNOTSUPP;
39774017
break;

include/uapi/linux/ublk_cmd.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
_IOWR('u', 0x15, struct ublksrv_ctrl_cmd)
5656
#define UBLK_U_CMD_QUIESCE_DEV \
5757
_IOWR('u', 0x16, struct ublksrv_ctrl_cmd)
58-
58+
#define UBLK_U_CMD_TRY_STOP_DEV \
59+
_IOWR('u', 0x17, struct ublksrv_ctrl_cmd)
5960
/*
6061
* 64bits are enough now, and it should be easy to extend in case of
6162
* running out of feature flags
@@ -321,6 +322,12 @@
321322
*/
322323
#define UBLK_F_INTEGRITY (1ULL << 16)
323324

325+
/*
326+
* The device supports the UBLK_CMD_TRY_STOP_DEV command, which
327+
* allows stopping the device only if there are no openers.
328+
*/
329+
#define UBLK_F_SAFE_STOP_DEV (1ULL << 17)
330+
324331
/* device state */
325332
#define UBLK_S_DEV_DEAD 0
326333
#define UBLK_S_DEV_LIVE 1

0 commit comments

Comments
 (0)