Commit bfbcef03 authored by Ming Lei's avatar Ming Lei Committed by Jens Axboe

ublk_drv: move ublk_get_device_from_id into ublk_ctrl_uring_cmd

It is annoying for each control command handler to get/put ublk
device and deal with failure.

Control command handler is simplified a lot by moving
ublk_get_device_from_id into ublk_ctrl_uring_cmd().
Reviewed-by: default avatarZiyangZhang <ZiyangZhang@linux.alibaba.com>
Signed-off-by: default avatarMing Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20230106041711.914434-4-ming.lei@redhat.comSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 73a166d9
...@@ -1496,21 +1496,16 @@ static struct ublk_device *ublk_get_device_from_id(int idx) ...@@ -1496,21 +1496,16 @@ static struct ublk_device *ublk_get_device_from_id(int idx)
return ub; return ub;
} }
static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd) static int ublk_ctrl_start_dev(struct ublk_device *ub, struct io_uring_cmd *cmd)
{ {
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
int ublksrv_pid = (int)header->data[0]; int ublksrv_pid = (int)header->data[0];
struct ublk_device *ub;
struct gendisk *disk; struct gendisk *disk;
int ret = -EINVAL; int ret = -EINVAL;
if (ublksrv_pid <= 0) if (ublksrv_pid <= 0)
return -EINVAL; return -EINVAL;
ub = ublk_get_device_from_id(header->dev_id);
if (!ub)
return -EINVAL;
wait_for_completion_interruptible(&ub->completion); wait_for_completion_interruptible(&ub->completion);
schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD); schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD);
...@@ -1559,21 +1554,20 @@ static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd) ...@@ -1559,21 +1554,20 @@ static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd)
put_disk(disk); put_disk(disk);
out_unlock: out_unlock:
mutex_unlock(&ub->mutex); mutex_unlock(&ub->mutex);
ublk_put_device(ub);
return ret; return ret;
} }
static int ublk_ctrl_get_queue_affinity(struct io_uring_cmd *cmd) static int ublk_ctrl_get_queue_affinity(struct ublk_device *ub,
struct io_uring_cmd *cmd)
{ {
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
void __user *argp = (void __user *)(unsigned long)header->addr; void __user *argp = (void __user *)(unsigned long)header->addr;
struct ublk_device *ub;
cpumask_var_t cpumask; cpumask_var_t cpumask;
unsigned long queue; unsigned long queue;
unsigned int retlen; unsigned int retlen;
unsigned int i; unsigned int i;
int ret = -EINVAL; int ret;
if (header->len * BITS_PER_BYTE < nr_cpu_ids) if (header->len * BITS_PER_BYTE < nr_cpu_ids)
return -EINVAL; return -EINVAL;
if (header->len & (sizeof(unsigned long)-1)) if (header->len & (sizeof(unsigned long)-1))
...@@ -1581,17 +1575,12 @@ static int ublk_ctrl_get_queue_affinity(struct io_uring_cmd *cmd) ...@@ -1581,17 +1575,12 @@ static int ublk_ctrl_get_queue_affinity(struct io_uring_cmd *cmd)
if (!header->addr) if (!header->addr)
return -EINVAL; return -EINVAL;
ub = ublk_get_device_from_id(header->dev_id);
if (!ub)
return -EINVAL;
queue = header->data[0]; queue = header->data[0];
if (queue >= ub->dev_info.nr_hw_queues) if (queue >= ub->dev_info.nr_hw_queues)
goto out_put_device; return -EINVAL;
ret = -ENOMEM;
if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
goto out_put_device; return -ENOMEM;
for_each_possible_cpu(i) { for_each_possible_cpu(i) {
if (ub->tag_set.map[HCTX_TYPE_DEFAULT].mq_map[i] == queue) if (ub->tag_set.map[HCTX_TYPE_DEFAULT].mq_map[i] == queue)
...@@ -1609,8 +1598,6 @@ static int ublk_ctrl_get_queue_affinity(struct io_uring_cmd *cmd) ...@@ -1609,8 +1598,6 @@ static int ublk_ctrl_get_queue_affinity(struct io_uring_cmd *cmd)
ret = 0; ret = 0;
out_free_cpumask: out_free_cpumask:
free_cpumask_var(cpumask); free_cpumask_var(cpumask);
out_put_device:
ublk_put_device(ub);
return ret; return ret;
} }
...@@ -1731,30 +1718,27 @@ static inline bool ublk_idr_freed(int id) ...@@ -1731,30 +1718,27 @@ static inline bool ublk_idr_freed(int id)
return ptr == NULL; return ptr == NULL;
} }
static int ublk_ctrl_del_dev(int idx) static int ublk_ctrl_del_dev(struct ublk_device **p_ub)
{ {
struct ublk_device *ub; struct ublk_device *ub = *p_ub;
int idx = ub->ub_number;
int ret; int ret;
ret = mutex_lock_killable(&ublk_ctl_mutex); ret = mutex_lock_killable(&ublk_ctl_mutex);
if (ret) if (ret)
return ret; return ret;
ub = ublk_get_device_from_id(idx); ublk_remove(ub);
if (ub) {
ublk_remove(ub); /* Mark the reference as consumed */
ublk_put_device(ub); *p_ub = NULL;
ret = 0; ublk_put_device(ub);
} else {
ret = -ENODEV;
}
/* /*
* Wait until the idr is removed, then it can be reused after * Wait until the idr is removed, then it can be reused after
* DEL_DEV command is returned. * DEL_DEV command is returned.
*/ */
if (!ret) wait_event(ublk_idr_wq, ublk_idr_freed(idx));
wait_event(ublk_idr_wq, ublk_idr_freed(idx));
mutex_unlock(&ublk_ctl_mutex); mutex_unlock(&ublk_ctl_mutex);
return ret; return ret;
...@@ -1769,50 +1753,36 @@ static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd) ...@@ -1769,50 +1753,36 @@ static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd)
header->data[0], header->addr, header->len); header->data[0], header->addr, header->len);
} }
static int ublk_ctrl_stop_dev(struct io_uring_cmd *cmd) static int ublk_ctrl_stop_dev(struct ublk_device *ub)
{ {
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
struct ublk_device *ub;
ub = ublk_get_device_from_id(header->dev_id);
if (!ub)
return -EINVAL;
ublk_stop_dev(ub); ublk_stop_dev(ub);
cancel_work_sync(&ub->stop_work); cancel_work_sync(&ub->stop_work);
cancel_work_sync(&ub->quiesce_work); cancel_work_sync(&ub->quiesce_work);
ublk_put_device(ub);
return 0; return 0;
} }
static int ublk_ctrl_get_dev_info(struct io_uring_cmd *cmd) static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
struct io_uring_cmd *cmd)
{ {
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
void __user *argp = (void __user *)(unsigned long)header->addr; void __user *argp = (void __user *)(unsigned long)header->addr;
struct ublk_device *ub;
int ret = 0;
if (header->len < sizeof(struct ublksrv_ctrl_dev_info) || !header->addr) if (header->len < sizeof(struct ublksrv_ctrl_dev_info) || !header->addr)
return -EINVAL; return -EINVAL;
ub = ublk_get_device_from_id(header->dev_id);
if (!ub)
return -EINVAL;
if (copy_to_user(argp, &ub->dev_info, sizeof(ub->dev_info))) if (copy_to_user(argp, &ub->dev_info, sizeof(ub->dev_info)))
ret = -EFAULT; return -EFAULT;
ublk_put_device(ub);
return ret; return 0;
} }
static int ublk_ctrl_get_params(struct io_uring_cmd *cmd) static int ublk_ctrl_get_params(struct ublk_device *ub,
struct io_uring_cmd *cmd)
{ {
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
void __user *argp = (void __user *)(unsigned long)header->addr; void __user *argp = (void __user *)(unsigned long)header->addr;
struct ublk_params_header ph; struct ublk_params_header ph;
struct ublk_device *ub;
int ret; int ret;
if (header->len <= sizeof(ph) || !header->addr) if (header->len <= sizeof(ph) || !header->addr)
...@@ -1827,10 +1797,6 @@ static int ublk_ctrl_get_params(struct io_uring_cmd *cmd) ...@@ -1827,10 +1797,6 @@ static int ublk_ctrl_get_params(struct io_uring_cmd *cmd)
if (ph.len > sizeof(struct ublk_params)) if (ph.len > sizeof(struct ublk_params))
ph.len = sizeof(struct ublk_params); ph.len = sizeof(struct ublk_params);
ub = ublk_get_device_from_id(header->dev_id);
if (!ub)
return -EINVAL;
mutex_lock(&ub->mutex); mutex_lock(&ub->mutex);
if (copy_to_user(argp, &ub->params, ph.len)) if (copy_to_user(argp, &ub->params, ph.len))
ret = -EFAULT; ret = -EFAULT;
...@@ -1838,16 +1804,15 @@ static int ublk_ctrl_get_params(struct io_uring_cmd *cmd) ...@@ -1838,16 +1804,15 @@ static int ublk_ctrl_get_params(struct io_uring_cmd *cmd)
ret = 0; ret = 0;
mutex_unlock(&ub->mutex); mutex_unlock(&ub->mutex);
ublk_put_device(ub);
return ret; return ret;
} }
static int ublk_ctrl_set_params(struct io_uring_cmd *cmd) static int ublk_ctrl_set_params(struct ublk_device *ub,
struct io_uring_cmd *cmd)
{ {
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
void __user *argp = (void __user *)(unsigned long)header->addr; void __user *argp = (void __user *)(unsigned long)header->addr;
struct ublk_params_header ph; struct ublk_params_header ph;
struct ublk_device *ub;
int ret = -EFAULT; int ret = -EFAULT;
if (header->len <= sizeof(ph) || !header->addr) if (header->len <= sizeof(ph) || !header->addr)
...@@ -1862,10 +1827,6 @@ static int ublk_ctrl_set_params(struct io_uring_cmd *cmd) ...@@ -1862,10 +1827,6 @@ static int ublk_ctrl_set_params(struct io_uring_cmd *cmd)
if (ph.len > sizeof(struct ublk_params)) if (ph.len > sizeof(struct ublk_params))
ph.len = sizeof(struct ublk_params); ph.len = sizeof(struct ublk_params);
ub = ublk_get_device_from_id(header->dev_id);
if (!ub)
return -EINVAL;
/* parameters can only be changed when device isn't live */ /* parameters can only be changed when device isn't live */
mutex_lock(&ub->mutex); mutex_lock(&ub->mutex);
if (ub->dev_info.state == UBLK_S_DEV_LIVE) { if (ub->dev_info.state == UBLK_S_DEV_LIVE) {
...@@ -1878,7 +1839,6 @@ static int ublk_ctrl_set_params(struct io_uring_cmd *cmd) ...@@ -1878,7 +1839,6 @@ static int ublk_ctrl_set_params(struct io_uring_cmd *cmd)
ret = ublk_validate_params(ub); ret = ublk_validate_params(ub);
} }
mutex_unlock(&ub->mutex); mutex_unlock(&ub->mutex);
ublk_put_device(ub);
return ret; return ret;
} }
...@@ -1905,17 +1865,13 @@ static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq) ...@@ -1905,17 +1865,13 @@ static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq)
} }
} }
static int ublk_ctrl_start_recovery(struct io_uring_cmd *cmd) static int ublk_ctrl_start_recovery(struct ublk_device *ub,
struct io_uring_cmd *cmd)
{ {
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
struct ublk_device *ub;
int ret = -EINVAL; int ret = -EINVAL;
int i; int i;
ub = ublk_get_device_from_id(header->dev_id);
if (!ub)
return ret;
mutex_lock(&ub->mutex); mutex_lock(&ub->mutex);
if (!ublk_can_use_recovery(ub)) if (!ublk_can_use_recovery(ub))
goto out_unlock; goto out_unlock;
...@@ -1948,21 +1904,16 @@ static int ublk_ctrl_start_recovery(struct io_uring_cmd *cmd) ...@@ -1948,21 +1904,16 @@ static int ublk_ctrl_start_recovery(struct io_uring_cmd *cmd)
ret = 0; ret = 0;
out_unlock: out_unlock:
mutex_unlock(&ub->mutex); mutex_unlock(&ub->mutex);
ublk_put_device(ub);
return ret; return ret;
} }
static int ublk_ctrl_end_recovery(struct io_uring_cmd *cmd) static int ublk_ctrl_end_recovery(struct ublk_device *ub,
struct io_uring_cmd *cmd)
{ {
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
int ublksrv_pid = (int)header->data[0]; int ublksrv_pid = (int)header->data[0];
struct ublk_device *ub;
int ret = -EINVAL; int ret = -EINVAL;
ub = ublk_get_device_from_id(header->dev_id);
if (!ub)
return ret;
pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n", pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n",
__func__, ub->dev_info.nr_hw_queues, header->dev_id); __func__, ub->dev_info.nr_hw_queues, header->dev_id);
/* wait until new ubq_daemon sending all FETCH_REQ */ /* wait until new ubq_daemon sending all FETCH_REQ */
...@@ -1990,7 +1941,6 @@ static int ublk_ctrl_end_recovery(struct io_uring_cmd *cmd) ...@@ -1990,7 +1941,6 @@ static int ublk_ctrl_end_recovery(struct io_uring_cmd *cmd)
ret = 0; ret = 0;
out_unlock: out_unlock:
mutex_unlock(&ub->mutex); mutex_unlock(&ub->mutex);
ublk_put_device(ub);
return ret; return ret;
} }
...@@ -1998,6 +1948,7 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd, ...@@ -1998,6 +1948,7 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
unsigned int issue_flags) unsigned int issue_flags)
{ {
struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
struct ublk_device *ub = NULL;
int ret = -EINVAL; int ret = -EINVAL;
if (issue_flags & IO_URING_F_NONBLOCK) if (issue_flags & IO_URING_F_NONBLOCK)
...@@ -2012,41 +1963,50 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd, ...@@ -2012,41 +1963,50 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
goto out; goto out;
ret = -ENODEV; if (cmd->cmd_op != UBLK_CMD_ADD_DEV) {
ret = -ENODEV;
ub = ublk_get_device_from_id(header->dev_id);
if (!ub)
goto out;
}
switch (cmd->cmd_op) { switch (cmd->cmd_op) {
case UBLK_CMD_START_DEV: case UBLK_CMD_START_DEV:
ret = ublk_ctrl_start_dev(cmd); ret = ublk_ctrl_start_dev(ub, cmd);
break; break;
case UBLK_CMD_STOP_DEV: case UBLK_CMD_STOP_DEV:
ret = ublk_ctrl_stop_dev(cmd); ret = ublk_ctrl_stop_dev(ub);
break; break;
case UBLK_CMD_GET_DEV_INFO: case UBLK_CMD_GET_DEV_INFO:
ret = ublk_ctrl_get_dev_info(cmd); ret = ublk_ctrl_get_dev_info(ub, cmd);
break; break;
case UBLK_CMD_ADD_DEV: case UBLK_CMD_ADD_DEV:
ret = ublk_ctrl_add_dev(cmd); ret = ublk_ctrl_add_dev(cmd);
break; break;
case UBLK_CMD_DEL_DEV: case UBLK_CMD_DEL_DEV:
ret = ublk_ctrl_del_dev(header->dev_id); ret = ublk_ctrl_del_dev(&ub);
break; break;
case UBLK_CMD_GET_QUEUE_AFFINITY: case UBLK_CMD_GET_QUEUE_AFFINITY:
ret = ublk_ctrl_get_queue_affinity(cmd); ret = ublk_ctrl_get_queue_affinity(ub, cmd);
break; break;
case UBLK_CMD_GET_PARAMS: case UBLK_CMD_GET_PARAMS:
ret = ublk_ctrl_get_params(cmd); ret = ublk_ctrl_get_params(ub, cmd);
break; break;
case UBLK_CMD_SET_PARAMS: case UBLK_CMD_SET_PARAMS:
ret = ublk_ctrl_set_params(cmd); ret = ublk_ctrl_set_params(ub, cmd);
break; break;
case UBLK_CMD_START_USER_RECOVERY: case UBLK_CMD_START_USER_RECOVERY:
ret = ublk_ctrl_start_recovery(cmd); ret = ublk_ctrl_start_recovery(ub, cmd);
break; break;
case UBLK_CMD_END_USER_RECOVERY: case UBLK_CMD_END_USER_RECOVERY:
ret = ublk_ctrl_end_recovery(cmd); ret = ublk_ctrl_end_recovery(ub, cmd);
break; break;
default: default:
ret = -ENOTSUPP;
break; break;
} }
if (ub)
ublk_put_device(ub);
out: out:
io_uring_cmd_done(cmd, ret, 0); io_uring_cmd_done(cmd, ret, 0);
pr_devel("%s: cmd done ret %d cmd_op %x, dev id %d qid %d\n", pr_devel("%s: cmd done ret %d cmd_op %x, dev id %d qid %d\n",
......
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