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

loop: split loop_control_ioctl

Split loop_control_ioctl into a helper for each command.  This keeps the
code nicely separated for the upcoming locking changes.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarChaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
Link: https://lore.kernel.org/r/20210623145908.92973-6-hch@lst.deSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 4157fe0b
...@@ -2397,8 +2397,19 @@ static void loop_probe(dev_t dev) ...@@ -2397,8 +2397,19 @@ static void loop_probe(dev_t dev)
mutex_unlock(&loop_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
} }
static long loop_control_ioctl(struct file *file, unsigned int cmd, static int loop_control_add(int idx)
unsigned long parm) {
int ret;
ret = mutex_lock_killable(&loop_ctl_mutex);
if (ret)
return ret;
ret = loop_add(idx);
mutex_unlock(&loop_ctl_mutex);
return ret;
}
static int loop_control_remove(int idx)
{ {
struct loop_device *lo; struct loop_device *lo;
int ret; int ret;
...@@ -2407,44 +2418,60 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd, ...@@ -2407,44 +2418,60 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
if (ret) if (ret)
return ret; return ret;
ret = -ENOSYS; ret = loop_lookup(&lo, idx);
switch (cmd) {
case LOOP_CTL_ADD:
ret = loop_add(parm);
break;
case LOOP_CTL_REMOVE:
ret = loop_lookup(&lo, parm);
if (ret < 0) if (ret < 0)
break; goto out_unlock_ctrl;
ret = mutex_lock_killable(&lo->lo_mutex); ret = mutex_lock_killable(&lo->lo_mutex);
if (ret) if (ret)
break; goto out_unlock_ctrl;
if (lo->lo_state != Lo_unbound) { if (lo->lo_state != Lo_unbound ||
ret = -EBUSY; atomic_read(&lo->lo_refcnt) > 0) {
mutex_unlock(&lo->lo_mutex); mutex_unlock(&lo->lo_mutex);
break;
}
if (atomic_read(&lo->lo_refcnt) > 0) {
ret = -EBUSY; ret = -EBUSY;
mutex_unlock(&lo->lo_mutex); goto out_unlock_ctrl;
break;
} }
lo->lo_state = Lo_deleting; lo->lo_state = Lo_deleting;
mutex_unlock(&lo->lo_mutex); mutex_unlock(&lo->lo_mutex);
idr_remove(&loop_index_idr, lo->lo_number); idr_remove(&loop_index_idr, lo->lo_number);
loop_remove(lo); loop_remove(lo);
break; out_unlock_ctrl:
case LOOP_CTL_GET_FREE: mutex_unlock(&loop_ctl_mutex);
return ret;
}
static int loop_control_get_free(int idx)
{
struct loop_device *lo;
int ret;
ret = mutex_lock_killable(&loop_ctl_mutex);
if (ret)
return ret;
ret = loop_lookup(&lo, -1); ret = loop_lookup(&lo, -1);
if (ret >= 0) if (ret < 0)
break;
ret = loop_add(-1); ret = loop_add(-1);
}
mutex_unlock(&loop_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
return ret; return ret;
} }
static long loop_control_ioctl(struct file *file, unsigned int cmd,
unsigned long parm)
{
switch (cmd) {
case LOOP_CTL_ADD:
return loop_control_add(parm);
case LOOP_CTL_REMOVE:
return loop_control_remove(parm);
case LOOP_CTL_GET_FREE:
return loop_control_get_free(parm);
default:
return -ENOSYS;
}
}
static const struct file_operations loop_ctl_fops = { static const struct file_operations loop_ctl_fops = {
.open = nonseekable_open, .open = nonseekable_open,
.unlocked_ioctl = loop_control_ioctl, .unlocked_ioctl = loop_control_ioctl,
......
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