diff options
author | Christoph Hellwig <hch@lst.de> | 2021-06-23 16:59:07 +0200 |
---|---|---|
committer | Jens Axboe <axboe@kernel.dk> | 2021-06-30 15:34:13 -0600 |
commit | b9848081465d8734441408129bd44311c7b6d644 (patch) | |
tree | bdf8030536649a8bdee53542d5932352091d8c23 /drivers/block | |
parent | e5d66a10324f2a0c31153fd6210d99a9d00ef047 (diff) | |
download | lwn-b9848081465d8734441408129bd44311c7b6d644.tar.gz lwn-b9848081465d8734441408129bd44311c7b6d644.zip |
loop: split loop_lookup
loop_lookup has two callers - one wants to do the a find by index and the
other wants any unbound loop device. Open code the respective
functionality in each caller.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20210623145908.92973-9-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'drivers/block')
-rw-r--r-- | drivers/block/loop.c | 57 |
1 files changed, 12 insertions, 45 deletions
diff --git a/drivers/block/loop.c b/drivers/block/loop.c index b217065a6d67..ff7014608ef6 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2353,44 +2353,6 @@ static void loop_remove(struct loop_device *lo) kfree(lo); } -static int find_free_cb(int id, void *ptr, void *data) -{ - struct loop_device *lo = ptr; - struct loop_device **l = data; - - if (lo->lo_state == Lo_unbound) { - *l = lo; - return 1; - } - return 0; -} - -static int loop_lookup(struct loop_device **l, int i) -{ - struct loop_device *lo; - int ret = -ENODEV; - - if (i < 0) { - int err; - - err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); - if (err == 1) { - *l = lo; - ret = lo->lo_number; - } - goto out; - } - - /* lookup and return a specific i */ - lo = idr_find(&loop_index_idr, i); - if (lo) { - *l = lo; - ret = lo->lo_number; - } -out: - return ret; -} - static void loop_probe(dev_t dev) { int idx = MINOR(dev) >> part_shift; @@ -2414,9 +2376,11 @@ static int loop_control_remove(int idx) if (ret) return ret; - ret = loop_lookup(&lo, idx); - if (ret < 0) + lo = idr_find(&loop_index_idr, idx); + if (!lo) { + ret = -ENODEV; goto out_unlock_ctrl; + } ret = mutex_lock_killable(&lo->lo_mutex); if (ret) @@ -2440,17 +2404,20 @@ out_unlock_ctrl: static int loop_control_get_free(int idx) { struct loop_device *lo; - int ret; + int id, ret; ret = mutex_lock_killable(&loop_ctl_mutex); if (ret) return ret; - ret = loop_lookup(&lo, -1); + idr_for_each_entry(&loop_index_idr, lo, id) { + if (lo->lo_state == Lo_unbound) + goto found; + } mutex_unlock(&loop_ctl_mutex); - - if (ret >= 0) - return ret; return loop_add(-1); +found: + mutex_unlock(&loop_ctl_mutex); + return id; } static long loop_control_ioctl(struct file *file, unsigned int cmd, |