1f21e6e18SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
3f21e6e18SChristoph Hellwig * Copyright 1993 by Theodore Ts'o.
41da177e4SLinus Torvalds */
51da177e4SLinus Torvalds #include <linux/module.h>
61da177e4SLinus Torvalds #include <linux/moduleparam.h>
71da177e4SLinus Torvalds #include <linux/sched.h>
81da177e4SLinus Torvalds #include <linux/fs.h>
94ee60ec1SMatthew Wilcox (Oracle) #include <linux/pagemap.h>
101da177e4SLinus Torvalds #include <linux/file.h>
111da177e4SLinus Torvalds #include <linux/stat.h>
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/major.h>
141da177e4SLinus Torvalds #include <linux/wait.h>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/swap.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
19863d5b82SDavid Howells #include <linux/compat.h>
201da177e4SLinus Torvalds #include <linux/suspend.h>
2183144186SRafael J. Wysocki #include <linux/freezer.h>
222a48fc0aSArnd Bergmann #include <linux/mutex.h>
231da177e4SLinus Torvalds #include <linux/writeback.h>
241da177e4SLinus Torvalds #include <linux/completion.h>
251da177e4SLinus Torvalds #include <linux/highmem.h>
26d6b29d7cSJens Axboe #include <linux/splice.h>
27ee862730SMilan Broz #include <linux/sysfs.h>
28770fe30aSKay Sievers #include <linux/miscdevice.h>
29dfaa2ef6SLukas Czerner #include <linux/falloc.h>
30283e7e5dSAl Viro #include <linux/uio.h>
31d9a08a9eSAdam Manzanares #include <linux/ioprio.h>
32db6638d7SDennis Zhou #include <linux/blk-cgroup.h>
33c74d40e8SDan Schatzberg #include <linux/sched/mm.h>
3406582bc8SMing Lei #include <linux/statfs.h>
357c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
36754d9679SChristoph Hellwig #include <linux/blk-mq.h>
37754d9679SChristoph Hellwig #include <linux/spinlock.h>
38754d9679SChristoph Hellwig #include <uapi/linux/loop.h>
39754d9679SChristoph Hellwig
40754d9679SChristoph Hellwig /* Possible states of device */
41754d9679SChristoph Hellwig enum {
42754d9679SChristoph Hellwig Lo_unbound,
43754d9679SChristoph Hellwig Lo_bound,
44754d9679SChristoph Hellwig Lo_rundown,
45754d9679SChristoph Hellwig Lo_deleting,
46754d9679SChristoph Hellwig };
47754d9679SChristoph Hellwig
48754d9679SChristoph Hellwig struct loop_func_table;
49754d9679SChristoph Hellwig
50754d9679SChristoph Hellwig struct loop_device {
51754d9679SChristoph Hellwig int lo_number;
52754d9679SChristoph Hellwig loff_t lo_offset;
53754d9679SChristoph Hellwig loff_t lo_sizelimit;
54754d9679SChristoph Hellwig int lo_flags;
55754d9679SChristoph Hellwig char lo_file_name[LO_NAME_SIZE];
56754d9679SChristoph Hellwig
57754d9679SChristoph Hellwig struct file * lo_backing_file;
58754d9679SChristoph Hellwig struct block_device *lo_device;
59754d9679SChristoph Hellwig
60754d9679SChristoph Hellwig gfp_t old_gfp_mask;
61754d9679SChristoph Hellwig
62754d9679SChristoph Hellwig spinlock_t lo_lock;
63754d9679SChristoph Hellwig int lo_state;
64754d9679SChristoph Hellwig spinlock_t lo_work_lock;
65754d9679SChristoph Hellwig struct workqueue_struct *workqueue;
66754d9679SChristoph Hellwig struct work_struct rootcg_work;
67754d9679SChristoph Hellwig struct list_head rootcg_cmd_list;
68754d9679SChristoph Hellwig struct list_head idle_worker_list;
69754d9679SChristoph Hellwig struct rb_root worker_tree;
70754d9679SChristoph Hellwig struct timer_list timer;
71754d9679SChristoph Hellwig bool use_dio;
72754d9679SChristoph Hellwig bool sysfs_inited;
73754d9679SChristoph Hellwig
74754d9679SChristoph Hellwig struct request_queue *lo_queue;
75754d9679SChristoph Hellwig struct blk_mq_tag_set tag_set;
76754d9679SChristoph Hellwig struct gendisk *lo_disk;
77754d9679SChristoph Hellwig struct mutex lo_mutex;
78754d9679SChristoph Hellwig bool idr_visible;
79754d9679SChristoph Hellwig };
80754d9679SChristoph Hellwig
81754d9679SChristoph Hellwig struct loop_cmd {
82754d9679SChristoph Hellwig struct list_head list_entry;
83754d9679SChristoph Hellwig bool use_aio; /* use AIO interface to handle I/O */
84754d9679SChristoph Hellwig atomic_t ref; /* only for aio */
85754d9679SChristoph Hellwig long ret;
86754d9679SChristoph Hellwig struct kiocb iocb;
87754d9679SChristoph Hellwig struct bio_vec *bvec;
88754d9679SChristoph Hellwig struct cgroup_subsys_state *blkcg_css;
89754d9679SChristoph Hellwig struct cgroup_subsys_state *memcg_css;
90754d9679SChristoph Hellwig };
911da177e4SLinus Torvalds
9287579e9bSDan Schatzberg #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ)
93e152a05fSBart Van Assche #define LOOP_DEFAULT_HW_Q_DEPTH 128
9487579e9bSDan Schatzberg
9534dd82afSKay Sievers static DEFINE_IDR(loop_index_idr);
96310ca162STetsuo Handa static DEFINE_MUTEX(loop_ctl_mutex);
973ce6e1f6STetsuo Handa static DEFINE_MUTEX(loop_validate_mutex);
983ce6e1f6STetsuo Handa
993ce6e1f6STetsuo Handa /**
1003ce6e1f6STetsuo Handa * loop_global_lock_killable() - take locks for safe loop_validate_file() test
1013ce6e1f6STetsuo Handa *
1023ce6e1f6STetsuo Handa * @lo: struct loop_device
1033ce6e1f6STetsuo Handa * @global: true if @lo is about to bind another "struct loop_device", false otherwise
1043ce6e1f6STetsuo Handa *
1053ce6e1f6STetsuo Handa * Returns 0 on success, -EINTR otherwise.
1063ce6e1f6STetsuo Handa *
1073ce6e1f6STetsuo Handa * Since loop_validate_file() traverses on other "struct loop_device" if
1083ce6e1f6STetsuo Handa * is_loop_device() is true, we need a global lock for serializing concurrent
1093ce6e1f6STetsuo Handa * loop_configure()/loop_change_fd()/__loop_clr_fd() calls.
1103ce6e1f6STetsuo Handa */
loop_global_lock_killable(struct loop_device * lo,bool global)1113ce6e1f6STetsuo Handa static int loop_global_lock_killable(struct loop_device *lo, bool global)
1123ce6e1f6STetsuo Handa {
1133ce6e1f6STetsuo Handa int err;
1143ce6e1f6STetsuo Handa
1153ce6e1f6STetsuo Handa if (global) {
1163ce6e1f6STetsuo Handa err = mutex_lock_killable(&loop_validate_mutex);
1173ce6e1f6STetsuo Handa if (err)
1183ce6e1f6STetsuo Handa return err;
1193ce6e1f6STetsuo Handa }
1203ce6e1f6STetsuo Handa err = mutex_lock_killable(&lo->lo_mutex);
1213ce6e1f6STetsuo Handa if (err && global)
1223ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex);
1233ce6e1f6STetsuo Handa return err;
1243ce6e1f6STetsuo Handa }
1253ce6e1f6STetsuo Handa
1263ce6e1f6STetsuo Handa /**
1273ce6e1f6STetsuo Handa * loop_global_unlock() - release locks taken by loop_global_lock_killable()
1283ce6e1f6STetsuo Handa *
1293ce6e1f6STetsuo Handa * @lo: struct loop_device
1303ce6e1f6STetsuo Handa * @global: true if @lo was about to bind another "struct loop_device", false otherwise
1313ce6e1f6STetsuo Handa */
loop_global_unlock(struct loop_device * lo,bool global)1323ce6e1f6STetsuo Handa static void loop_global_unlock(struct loop_device *lo, bool global)
1333ce6e1f6STetsuo Handa {
1343ce6e1f6STetsuo Handa mutex_unlock(&lo->lo_mutex);
1353ce6e1f6STetsuo Handa if (global)
1363ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex);
1373ce6e1f6STetsuo Handa }
1381da177e4SLinus Torvalds
139476a4813SLaurent Vivier static int max_part;
140476a4813SLaurent Vivier static int part_shift;
141476a4813SLaurent Vivier
get_size(loff_t offset,loff_t sizelimit,struct file * file)1427035b5dfSDmitry Monakhov static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
1431da177e4SLinus Torvalds {
144b7a1da69SGuo Chao loff_t loopsize;
1451da177e4SLinus Torvalds
1461da177e4SLinus Torvalds /* Compute loopsize in bytes */
147b7a1da69SGuo Chao loopsize = i_size_read(file->f_mapping->host);
148b7a1da69SGuo Chao if (offset > 0)
149b7a1da69SGuo Chao loopsize -= offset;
150b7a1da69SGuo Chao /* offset is beyond i_size, weird but possible */
1517035b5dfSDmitry Monakhov if (loopsize < 0)
1527035b5dfSDmitry Monakhov return 0;
1531da177e4SLinus Torvalds
1547035b5dfSDmitry Monakhov if (sizelimit > 0 && sizelimit < loopsize)
1557035b5dfSDmitry Monakhov loopsize = sizelimit;
1561da177e4SLinus Torvalds /*
1571da177e4SLinus Torvalds * Unfortunately, if we want to do I/O on the device,
1581da177e4SLinus Torvalds * the number of 512-byte sectors has to fit into a sector_t.
1591da177e4SLinus Torvalds */
1601da177e4SLinus Torvalds return loopsize >> 9;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds
get_loop_size(struct loop_device * lo,struct file * file)1637035b5dfSDmitry Monakhov static loff_t get_loop_size(struct loop_device *lo, struct file *file)
1641da177e4SLinus Torvalds {
1657035b5dfSDmitry Monakhov return get_size(lo->lo_offset, lo->lo_sizelimit, file);
1667035b5dfSDmitry Monakhov }
1677035b5dfSDmitry Monakhov
16896e84339SChristoph Hellwig /*
16996e84339SChristoph Hellwig * We support direct I/O only if lo_offset is aligned with the logical I/O size
17096e84339SChristoph Hellwig * of backing device, and the logical block size of loop is bigger than that of
17196e84339SChristoph Hellwig * the backing device.
17296e84339SChristoph Hellwig */
lo_bdev_can_use_dio(struct loop_device * lo,struct block_device * backing_bdev)17396e84339SChristoph Hellwig static bool lo_bdev_can_use_dio(struct loop_device *lo,
17496e84339SChristoph Hellwig struct block_device *backing_bdev)
17596e84339SChristoph Hellwig {
17696e84339SChristoph Hellwig unsigned short sb_bsize = bdev_logical_block_size(backing_bdev);
17796e84339SChristoph Hellwig
17896e84339SChristoph Hellwig if (queue_logical_block_size(lo->lo_queue) < sb_bsize)
17996e84339SChristoph Hellwig return false;
18096e84339SChristoph Hellwig if (lo->lo_offset & (sb_bsize - 1))
18196e84339SChristoph Hellwig return false;
18296e84339SChristoph Hellwig return true;
18396e84339SChristoph Hellwig }
18496e84339SChristoph Hellwig
__loop_update_dio(struct loop_device * lo,bool dio)1852e5ab5f3SMing Lei static void __loop_update_dio(struct loop_device *lo, bool dio)
1862e5ab5f3SMing Lei {
1872e5ab5f3SMing Lei struct file *file = lo->lo_backing_file;
18896e84339SChristoph Hellwig struct inode *inode = file->f_mapping->host;
18996e84339SChristoph Hellwig struct block_device *backing_bdev = NULL;
1902e5ab5f3SMing Lei bool use_dio;
1912e5ab5f3SMing Lei
19296e84339SChristoph Hellwig if (S_ISBLK(inode->i_mode))
19396e84339SChristoph Hellwig backing_bdev = I_BDEV(inode);
19496e84339SChristoph Hellwig else if (inode->i_sb->s_bdev)
19596e84339SChristoph Hellwig backing_bdev = inode->i_sb->s_bdev;
1962e5ab5f3SMing Lei
19796e84339SChristoph Hellwig use_dio = dio && (file->f_mode & FMODE_CAN_ODIRECT) &&
19896e84339SChristoph Hellwig (!backing_bdev || lo_bdev_can_use_dio(lo, backing_bdev));
1992e5ab5f3SMing Lei
2002e5ab5f3SMing Lei if (lo->use_dio == use_dio)
2012e5ab5f3SMing Lei return;
2022e5ab5f3SMing Lei
2032e5ab5f3SMing Lei /* flush dirty pages before changing direct IO */
2042e5ab5f3SMing Lei vfs_fsync(file, 0);
2052e5ab5f3SMing Lei
2062e5ab5f3SMing Lei /*
2072e5ab5f3SMing Lei * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
2082e5ab5f3SMing Lei * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
2092e5ab5f3SMing Lei * will get updated by ioctl(LOOP_GET_STATUS)
2102e5ab5f3SMing Lei */
2110fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound)
2122e5ab5f3SMing Lei blk_mq_freeze_queue(lo->lo_queue);
2132e5ab5f3SMing Lei lo->use_dio = use_dio;
214*b66ff9a3SChristoph Hellwig if (use_dio)
2152e5ab5f3SMing Lei lo->lo_flags |= LO_FLAGS_DIRECT_IO;
216*b66ff9a3SChristoph Hellwig else
2172e5ab5f3SMing Lei lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
2180fbcf579SMartijn Coenen if (lo->lo_state == Lo_bound)
2192e5ab5f3SMing Lei blk_mq_unfreeze_queue(lo->lo_queue);
2202e5ab5f3SMing Lei }
2212e5ab5f3SMing Lei
2225795b6f5SMartijn Coenen /**
2235795b6f5SMartijn Coenen * loop_set_size() - sets device size and notifies userspace
2245795b6f5SMartijn Coenen * @lo: struct loop_device to set the size for
2255795b6f5SMartijn Coenen * @size: new size of the loop device
2265795b6f5SMartijn Coenen *
2275795b6f5SMartijn Coenen * Callers must validate that the size passed into this function fits into
2285795b6f5SMartijn Coenen * a sector_t, eg using loop_validate_size()
2295795b6f5SMartijn Coenen */
loop_set_size(struct loop_device * lo,loff_t size)2305795b6f5SMartijn Coenen static void loop_set_size(struct loop_device *lo, loff_t size)
2315795b6f5SMartijn Coenen {
232449f4ec9SChristoph Hellwig if (!set_capacity_and_notify(lo->lo_disk, size))
2333b4f85d0SChristoph Hellwig kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds
lo_write_bvec(struct file * file,struct bio_vec * bvec,loff_t * ppos)236aa4d8616SChristoph Hellwig static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
2371da177e4SLinus Torvalds {
238aa4d8616SChristoph Hellwig struct iov_iter i;
2391da177e4SLinus Torvalds ssize_t bw;
240283e7e5dSAl Viro
241de4eda9dSAl Viro iov_iter_bvec(&i, ITER_SOURCE, bvec, 1, bvec->bv_len);
2421da177e4SLinus Torvalds
24303d95eb2SAl Viro file_start_write(file);
244abbb6589SChristoph Hellwig bw = vfs_iter_write(file, &i, ppos, 0);
24503d95eb2SAl Viro file_end_write(file);
246aa4d8616SChristoph Hellwig
247aa4d8616SChristoph Hellwig if (likely(bw == bvec->bv_len))
2481da177e4SLinus Torvalds return 0;
249aa4d8616SChristoph Hellwig
250aa4d8616SChristoph Hellwig printk_ratelimited(KERN_ERR
251aa4d8616SChristoph Hellwig "loop: Write error at byte offset %llu, length %i.\n",
252aa4d8616SChristoph Hellwig (unsigned long long)*ppos, bvec->bv_len);
2531da177e4SLinus Torvalds if (bw >= 0)
2541da177e4SLinus Torvalds bw = -EIO;
2551da177e4SLinus Torvalds return bw;
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds
lo_write_simple(struct loop_device * lo,struct request * rq,loff_t pos)258aa4d8616SChristoph Hellwig static int lo_write_simple(struct loop_device *lo, struct request *rq,
259aa4d8616SChristoph Hellwig loff_t pos)
2601da177e4SLinus Torvalds {
261aa4d8616SChristoph Hellwig struct bio_vec bvec;
262aa4d8616SChristoph Hellwig struct req_iterator iter;
263aa4d8616SChristoph Hellwig int ret = 0;
264aa4d8616SChristoph Hellwig
265aa4d8616SChristoph Hellwig rq_for_each_segment(bvec, rq, iter) {
266aa4d8616SChristoph Hellwig ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
267aa4d8616SChristoph Hellwig if (ret < 0)
268aa4d8616SChristoph Hellwig break;
2691da177e4SLinus Torvalds cond_resched();
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds
272aa4d8616SChristoph Hellwig return ret;
273aa4d8616SChristoph Hellwig }
274aa4d8616SChristoph Hellwig
lo_read_simple(struct loop_device * lo,struct request * rq,loff_t pos)275aa4d8616SChristoph Hellwig static int lo_read_simple(struct loop_device *lo, struct request *rq,
276aa4d8616SChristoph Hellwig loff_t pos)
2771da177e4SLinus Torvalds {
2787988613bSKent Overstreet struct bio_vec bvec;
27930112013SMing Lei struct req_iterator iter;
280aa4d8616SChristoph Hellwig struct iov_iter i;
281aa4d8616SChristoph Hellwig ssize_t len;
2821da177e4SLinus Torvalds
28330112013SMing Lei rq_for_each_segment(bvec, rq, iter) {
284de4eda9dSAl Viro iov_iter_bvec(&i, ITER_DEST, &bvec, 1, bvec.bv_len);
28518e9710eSChristoph Hellwig len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
286aa4d8616SChristoph Hellwig if (len < 0)
287aa4d8616SChristoph Hellwig return len;
288306df071SDave Young
289aa4d8616SChristoph Hellwig flush_dcache_page(bvec.bv_page);
290aa4d8616SChristoph Hellwig
291aa4d8616SChristoph Hellwig if (len != bvec.bv_len) {
29230112013SMing Lei struct bio *bio;
29330112013SMing Lei
29430112013SMing Lei __rq_for_each_bio(bio, rq)
295306df071SDave Young zero_fill_bio(bio);
2961da177e4SLinus Torvalds break;
297306df071SDave Young }
298aa4d8616SChristoph Hellwig cond_resched();
2991da177e4SLinus Torvalds }
300aa4d8616SChristoph Hellwig
301306df071SDave Young return 0;
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds
lo_fallocate(struct loop_device * lo,struct request * rq,loff_t pos,int mode)304efcfec57SDarrick J. Wong static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
305efcfec57SDarrick J. Wong int mode)
306cf655d95SMing Lei {
307cf655d95SMing Lei /*
308efcfec57SDarrick J. Wong * We use fallocate to manipulate the space mappings used by the image
30947e96246SChristoph Hellwig * a.k.a. discard/zerorange.
310cf655d95SMing Lei */
311cf655d95SMing Lei struct file *file = lo->lo_backing_file;
312cf655d95SMing Lei int ret;
313cf655d95SMing Lei
314efcfec57SDarrick J. Wong mode |= FALLOC_FL_KEEP_SIZE;
315efcfec57SDarrick J. Wong
31670200574SChristoph Hellwig if (!bdev_max_discard_sectors(lo->lo_device))
31770200574SChristoph Hellwig return -EOPNOTSUPP;
318cf655d95SMing Lei
319cf655d95SMing Lei ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
320cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
32170200574SChristoph Hellwig return -EIO;
322cf655d95SMing Lei return ret;
323cf655d95SMing Lei }
324cf655d95SMing Lei
lo_req_flush(struct loop_device * lo,struct request * rq)325cf655d95SMing Lei static int lo_req_flush(struct loop_device *lo, struct request *rq)
326cf655d95SMing Lei {
3279c64e38cSChaitanya Kulkarni int ret = vfs_fsync(lo->lo_backing_file, 0);
328cf655d95SMing Lei if (unlikely(ret && ret != -EINVAL))
329cf655d95SMing Lei ret = -EIO;
330cf655d95SMing Lei
331cf655d95SMing Lei return ret;
332cf655d95SMing Lei }
333cf655d95SMing Lei
lo_complete_rq(struct request * rq)334fe2cb290SChristoph Hellwig static void lo_complete_rq(struct request *rq)
335bc07c10aSMing Lei {
336fe2cb290SChristoph Hellwig struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
337f9de14bcSJens Axboe blk_status_t ret = BLK_STS_OK;
338bc07c10aSMing Lei
339f9de14bcSJens Axboe if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) ||
340f9de14bcSJens Axboe req_op(rq) != REQ_OP_READ) {
341f9de14bcSJens Axboe if (cmd->ret < 0)
3428cd55087SEvan Green ret = errno_to_blk_status(cmd->ret);
343f9de14bcSJens Axboe goto end_io;
344bc07c10aSMing Lei }
345fe2cb290SChristoph Hellwig
346f9de14bcSJens Axboe /*
347f9de14bcSJens Axboe * Short READ - if we got some data, advance our request and
348f9de14bcSJens Axboe * retry it. If we got no data, end the rest with EIO.
349f9de14bcSJens Axboe */
350f9de14bcSJens Axboe if (cmd->ret) {
351f9de14bcSJens Axboe blk_update_request(rq, BLK_STS_OK, cmd->ret);
352f9de14bcSJens Axboe cmd->ret = 0;
353f9de14bcSJens Axboe blk_mq_requeue_request(rq, true);
354f9de14bcSJens Axboe } else {
355f9de14bcSJens Axboe if (cmd->use_aio) {
356f9de14bcSJens Axboe struct bio *bio = rq->bio;
357f9de14bcSJens Axboe
358f9de14bcSJens Axboe while (bio) {
359f9de14bcSJens Axboe zero_fill_bio(bio);
360f9de14bcSJens Axboe bio = bio->bi_next;
361f9de14bcSJens Axboe }
362f9de14bcSJens Axboe }
363f9de14bcSJens Axboe ret = BLK_STS_IOERR;
364f9de14bcSJens Axboe end_io:
365f9de14bcSJens Axboe blk_mq_end_request(rq, ret);
366f9de14bcSJens Axboe }
367bc07c10aSMing Lei }
368bc07c10aSMing Lei
lo_rw_aio_do_completion(struct loop_cmd * cmd)36992d77332SShaohua Li static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
37092d77332SShaohua Li {
3711894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd);
3721894e916SJens Axboe
37392d77332SShaohua Li if (!atomic_dec_and_test(&cmd->ref))
37492d77332SShaohua Li return;
37592d77332SShaohua Li kfree(cmd->bvec);
37692d77332SShaohua Li cmd->bvec = NULL;
37715f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q)))
3781894e916SJens Axboe blk_mq_complete_request(rq);
37992d77332SShaohua Li }
38092d77332SShaohua Li
lo_rw_aio_complete(struct kiocb * iocb,long ret)3816b19b766SJens Axboe static void lo_rw_aio_complete(struct kiocb *iocb, long ret)
382bc07c10aSMing Lei {
383bc07c10aSMing Lei struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
384bc07c10aSMing Lei
385fe2cb290SChristoph Hellwig cmd->ret = ret;
38692d77332SShaohua Li lo_rw_aio_do_completion(cmd);
387bc07c10aSMing Lei }
388bc07c10aSMing Lei
lo_rw_aio(struct loop_device * lo,struct loop_cmd * cmd,loff_t pos,int rw)389bc07c10aSMing Lei static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
390de4eda9dSAl Viro loff_t pos, int rw)
391bc07c10aSMing Lei {
392bc07c10aSMing Lei struct iov_iter iter;
39386af5952SMing Lei struct req_iterator rq_iter;
394bc07c10aSMing Lei struct bio_vec *bvec;
3951894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd);
39640326d8aSShaohua Li struct bio *bio = rq->bio;
397bc07c10aSMing Lei struct file *file = lo->lo_backing_file;
39886af5952SMing Lei struct bio_vec tmp;
39940326d8aSShaohua Li unsigned int offset;
40086af5952SMing Lei int nr_bvec = 0;
401bc07c10aSMing Lei int ret;
402bc07c10aSMing Lei
40386af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter)
40486af5952SMing Lei nr_bvec++;
405bc07c10aSMing Lei
40686af5952SMing Lei if (rq->bio != rq->biotail) {
40786af5952SMing Lei
40886af5952SMing Lei bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
4096da2ec56SKees Cook GFP_NOIO);
41040326d8aSShaohua Li if (!bvec)
41140326d8aSShaohua Li return -EIO;
41240326d8aSShaohua Li cmd->bvec = bvec;
41340326d8aSShaohua Li
414a7297a6aSMing Lei /*
41540326d8aSShaohua Li * The bios of the request may be started from the middle of
41640326d8aSShaohua Li * the 'bvec' because of bio splitting, so we can't directly
41786af5952SMing Lei * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec
41840326d8aSShaohua Li * API will take care of all details for us.
419a7297a6aSMing Lei */
42086af5952SMing Lei rq_for_each_bvec(tmp, rq, rq_iter) {
42140326d8aSShaohua Li *bvec = tmp;
42240326d8aSShaohua Li bvec++;
42340326d8aSShaohua Li }
42440326d8aSShaohua Li bvec = cmd->bvec;
42540326d8aSShaohua Li offset = 0;
42640326d8aSShaohua Li } else {
42740326d8aSShaohua Li /*
42840326d8aSShaohua Li * Same here, this bio may be started from the middle of the
42940326d8aSShaohua Li * 'bvec' because of bio splitting, so offset from the bvec
43040326d8aSShaohua Li * must be passed to iov iterator
43140326d8aSShaohua Li */
43240326d8aSShaohua Li offset = bio->bi_iter.bi_bvec_done;
43340326d8aSShaohua Li bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
43440326d8aSShaohua Li }
43592d77332SShaohua Li atomic_set(&cmd->ref, 2);
43640326d8aSShaohua Li
437b6207430SChristoph Hellwig iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq));
43840326d8aSShaohua Li iter.iov_offset = offset;
439bc07c10aSMing Lei
440bc07c10aSMing Lei cmd->iocb.ki_pos = pos;
441bc07c10aSMing Lei cmd->iocb.ki_filp = file;
442bc07c10aSMing Lei cmd->iocb.ki_complete = lo_rw_aio_complete;
443bc07c10aSMing Lei cmd->iocb.ki_flags = IOCB_DIRECT;
444d9a08a9eSAdam Manzanares cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
445bc07c10aSMing Lei
446de4eda9dSAl Viro if (rw == ITER_SOURCE)
447bb7462b6SMiklos Szeredi ret = call_write_iter(file, &cmd->iocb, &iter);
448bc07c10aSMing Lei else
449bb7462b6SMiklos Szeredi ret = call_read_iter(file, &cmd->iocb, &iter);
450bc07c10aSMing Lei
45192d77332SShaohua Li lo_rw_aio_do_completion(cmd);
45292d77332SShaohua Li
453bc07c10aSMing Lei if (ret != -EIOCBQUEUED)
4546b19b766SJens Axboe lo_rw_aio_complete(&cmd->iocb, ret);
455bc07c10aSMing Lei return 0;
456bc07c10aSMing Lei }
457bc07c10aSMing Lei
do_req_filebacked(struct loop_device * lo,struct request * rq)458c1c87c2bSChristoph Hellwig static int do_req_filebacked(struct loop_device *lo, struct request *rq)
459bc07c10aSMing Lei {
460bc07c10aSMing Lei struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
461c1c87c2bSChristoph Hellwig loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
462bc07c10aSMing Lei
463bc07c10aSMing Lei /*
464bc07c10aSMing Lei * lo_write_simple and lo_read_simple should have been covered
465bc07c10aSMing Lei * by io submit style function like lo_rw_aio(), one blocker
466bc07c10aSMing Lei * is that lo_read_simple() need to call flush_dcache_page after
467bc07c10aSMing Lei * the page is written from kernel, and it isn't easy to handle
468bc07c10aSMing Lei * this in io submit style function which submits all segments
469bc07c10aSMing Lei * of the req at one time. And direct read IO doesn't need to
470bc07c10aSMing Lei * run flush_dcache_page().
471bc07c10aSMing Lei */
472c1c87c2bSChristoph Hellwig switch (req_op(rq)) {
473c1c87c2bSChristoph Hellwig case REQ_OP_FLUSH:
474c1c87c2bSChristoph Hellwig return lo_req_flush(lo, rq);
47519372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES:
476efcfec57SDarrick J. Wong /*
477efcfec57SDarrick J. Wong * If the caller doesn't want deallocation, call zeroout to
478efcfec57SDarrick J. Wong * write zeroes the range. Otherwise, punch them out.
479efcfec57SDarrick J. Wong */
480efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos,
481efcfec57SDarrick J. Wong (rq->cmd_flags & REQ_NOUNMAP) ?
482efcfec57SDarrick J. Wong FALLOC_FL_ZERO_RANGE :
483efcfec57SDarrick J. Wong FALLOC_FL_PUNCH_HOLE);
484efcfec57SDarrick J. Wong case REQ_OP_DISCARD:
485efcfec57SDarrick J. Wong return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
486c1c87c2bSChristoph Hellwig case REQ_OP_WRITE:
48747e96246SChristoph Hellwig if (cmd->use_aio)
488de4eda9dSAl Viro return lo_rw_aio(lo, cmd, pos, ITER_SOURCE);
489c1c87c2bSChristoph Hellwig else
490bc07c10aSMing Lei return lo_write_simple(lo, rq, pos);
491c1c87c2bSChristoph Hellwig case REQ_OP_READ:
49247e96246SChristoph Hellwig if (cmd->use_aio)
493de4eda9dSAl Viro return lo_rw_aio(lo, cmd, pos, ITER_DEST);
494bc07c10aSMing Lei else
495bc07c10aSMing Lei return lo_read_simple(lo, rq, pos);
496c1c87c2bSChristoph Hellwig default:
497c1c87c2bSChristoph Hellwig WARN_ON_ONCE(1);
498c1c87c2bSChristoph Hellwig return -EIO;
499bc07c10aSMing Lei }
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds
loop_update_dio(struct loop_device * lo)5022e5ab5f3SMing Lei static inline void loop_update_dio(struct loop_device *lo)
5032e5ab5f3SMing Lei {
504efbe3c24SIra Weiny __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) |
5052e5ab5f3SMing Lei lo->use_dio);
5062e5ab5f3SMing Lei }
5072e5ab5f3SMing Lei
loop_reread_partitions(struct loop_device * lo)5080384264eSChristoph Hellwig static void loop_reread_partitions(struct loop_device *lo)
50906f0e9e6SMing Lei {
51006f0e9e6SMing Lei int rc;
51106f0e9e6SMing Lei
5120384264eSChristoph Hellwig mutex_lock(&lo->lo_disk->open_mutex);
5130384264eSChristoph Hellwig rc = bdev_disk_changed(lo->lo_disk, false);
5140384264eSChristoph Hellwig mutex_unlock(&lo->lo_disk->open_mutex);
51506f0e9e6SMing Lei if (rc)
51606f0e9e6SMing Lei pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
51706f0e9e6SMing Lei __func__, lo->lo_number, lo->lo_file_name, rc);
51806f0e9e6SMing Lei }
51906f0e9e6SMing Lei
is_loop_device(struct file * file)520d2ac838eSTheodore Ts'o static inline int is_loop_device(struct file *file)
521d2ac838eSTheodore Ts'o {
522d2ac838eSTheodore Ts'o struct inode *i = file->f_mapping->host;
523d2ac838eSTheodore Ts'o
5246f24784fSAl Viro return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR;
525d2ac838eSTheodore Ts'o }
526d2ac838eSTheodore Ts'o
loop_validate_file(struct file * file,struct block_device * bdev)527d2ac838eSTheodore Ts'o static int loop_validate_file(struct file *file, struct block_device *bdev)
528d2ac838eSTheodore Ts'o {
529d2ac838eSTheodore Ts'o struct inode *inode = file->f_mapping->host;
530d2ac838eSTheodore Ts'o struct file *f = file;
531d2ac838eSTheodore Ts'o
532d2ac838eSTheodore Ts'o /* Avoid recursion */
533d2ac838eSTheodore Ts'o while (is_loop_device(f)) {
534d2ac838eSTheodore Ts'o struct loop_device *l;
535d2ac838eSTheodore Ts'o
5363ce6e1f6STetsuo Handa lockdep_assert_held(&loop_validate_mutex);
5374e7b5671SChristoph Hellwig if (f->f_mapping->host->i_rdev == bdev->bd_dev)
538d2ac838eSTheodore Ts'o return -EBADF;
539d2ac838eSTheodore Ts'o
5404e7b5671SChristoph Hellwig l = I_BDEV(f->f_mapping->host)->bd_disk->private_data;
5413ce6e1f6STetsuo Handa if (l->lo_state != Lo_bound)
542d2ac838eSTheodore Ts'o return -EINVAL;
5433ce6e1f6STetsuo Handa /* Order wrt setting lo->lo_backing_file in loop_configure(). */
5443ce6e1f6STetsuo Handa rmb();
545d2ac838eSTheodore Ts'o f = l->lo_backing_file;
546d2ac838eSTheodore Ts'o }
547d2ac838eSTheodore Ts'o if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
548d2ac838eSTheodore Ts'o return -EINVAL;
549d2ac838eSTheodore Ts'o return 0;
550d2ac838eSTheodore Ts'o }
551d2ac838eSTheodore Ts'o
5521da177e4SLinus Torvalds /*
5531da177e4SLinus Torvalds * loop_change_fd switched the backing store of a loopback device to
5541da177e4SLinus Torvalds * a new file. This is useful for operating system installers to free up
5551da177e4SLinus Torvalds * the original file and in High Availability environments to switch to
5561da177e4SLinus Torvalds * an alternative location for the content in case of server meltdown.
5571da177e4SLinus Torvalds * This can only work if the loop device is used read-only, and if the
5581da177e4SLinus Torvalds * new backing store is the same size and type as the old backing store.
5591da177e4SLinus Torvalds */
loop_change_fd(struct loop_device * lo,struct block_device * bdev,unsigned int arg)560bb214884SAl Viro static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
561bb214884SAl Viro unsigned int arg)
5621da177e4SLinus Torvalds {
5633ce6e1f6STetsuo Handa struct file *file = fget(arg);
5643ce6e1f6STetsuo Handa struct file *old_file;
5651da177e4SLinus Torvalds int error;
56685b0a54aSJan Kara bool partscan;
5673ce6e1f6STetsuo Handa bool is_loop;
5681da177e4SLinus Torvalds
5693ce6e1f6STetsuo Handa if (!file)
5703ce6e1f6STetsuo Handa return -EBADF;
571498ef5c7SChristoph Hellwig
572498ef5c7SChristoph Hellwig /* suppress uevents while reconfiguring the device */
573498ef5c7SChristoph Hellwig dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 1);
574498ef5c7SChristoph Hellwig
5753ce6e1f6STetsuo Handa is_loop = is_loop_device(file);
5763ce6e1f6STetsuo Handa error = loop_global_lock_killable(lo, is_loop);
577c3710770SJan Kara if (error)
5783ce6e1f6STetsuo Handa goto out_putf;
5791da177e4SLinus Torvalds error = -ENXIO;
5801da177e4SLinus Torvalds if (lo->lo_state != Lo_bound)
5811dded9acSJan Kara goto out_err;
5821da177e4SLinus Torvalds
5831da177e4SLinus Torvalds /* the loop device has to be read-only */
5841da177e4SLinus Torvalds error = -EINVAL;
5851da177e4SLinus Torvalds if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
5861dded9acSJan Kara goto out_err;
5871da177e4SLinus Torvalds
588d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev);
589d2ac838eSTheodore Ts'o if (error)
5901dded9acSJan Kara goto out_err;
591d2ac838eSTheodore Ts'o
5921da177e4SLinus Torvalds old_file = lo->lo_backing_file;
5931da177e4SLinus Torvalds
5941da177e4SLinus Torvalds error = -EINVAL;
5951da177e4SLinus Torvalds
5961da177e4SLinus Torvalds /* size of the new backing store needs to be the same */
5971da177e4SLinus Torvalds if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
5981dded9acSJan Kara goto out_err;
5991da177e4SLinus Torvalds
6001da177e4SLinus Torvalds /* and ... switch */
601ab6860f6SChristoph Hellwig disk_force_media_change(lo->lo_disk);
60243cade80SOmar Sandoval blk_mq_freeze_queue(lo->lo_queue);
60343cade80SOmar Sandoval mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
60443cade80SOmar Sandoval lo->lo_backing_file = file;
60543cade80SOmar Sandoval lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
60643cade80SOmar Sandoval mapping_set_gfp_mask(file->f_mapping,
60743cade80SOmar Sandoval lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
60843cade80SOmar Sandoval loop_update_dio(lo);
60943cade80SOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue);
61085b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
6113ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop);
6123ce6e1f6STetsuo Handa
6133ce6e1f6STetsuo Handa /*
6143ce6e1f6STetsuo Handa * Flush loop_validate_file() before fput(), for l->lo_backing_file
6153ce6e1f6STetsuo Handa * might be pointing at old_file which might be the last reference.
6163ce6e1f6STetsuo Handa */
6173ce6e1f6STetsuo Handa if (!is_loop) {
6183ce6e1f6STetsuo Handa mutex_lock(&loop_validate_mutex);
6193ce6e1f6STetsuo Handa mutex_unlock(&loop_validate_mutex);
6203ce6e1f6STetsuo Handa }
6211dded9acSJan Kara /*
6226cc8e743SPavel Tatashin * We must drop file reference outside of lo_mutex as dropping
623a8698707SChristoph Hellwig * the file ref can take open_mutex which creates circular locking
6241dded9acSJan Kara * dependency.
6251dded9acSJan Kara */
6261dded9acSJan Kara fput(old_file);
62785b0a54aSJan Kara if (partscan)
6280384264eSChristoph Hellwig loop_reread_partitions(lo);
629498ef5c7SChristoph Hellwig
630498ef5c7SChristoph Hellwig error = 0;
631498ef5c7SChristoph Hellwig done:
632498ef5c7SChristoph Hellwig /* enable and uncork uevent now that we are done */
633498ef5c7SChristoph Hellwig dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 0);
634498ef5c7SChristoph Hellwig return error;
6351da177e4SLinus Torvalds
6361dded9acSJan Kara out_err:
6373ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop);
6383ce6e1f6STetsuo Handa out_putf:
6391dded9acSJan Kara fput(file);
640498ef5c7SChristoph Hellwig goto done;
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds
643ee862730SMilan Broz /* loop sysfs attributes */
644ee862730SMilan Broz
loop_attr_show(struct device * dev,char * page,ssize_t (* callback)(struct loop_device *,char *))645ee862730SMilan Broz static ssize_t loop_attr_show(struct device *dev, char *page,
646ee862730SMilan Broz ssize_t (*callback)(struct loop_device *, char *))
647ee862730SMilan Broz {
64834dd82afSKay Sievers struct gendisk *disk = dev_to_disk(dev);
64934dd82afSKay Sievers struct loop_device *lo = disk->private_data;
650ee862730SMilan Broz
65134dd82afSKay Sievers return callback(lo, page);
652ee862730SMilan Broz }
653ee862730SMilan Broz
654ee862730SMilan Broz #define LOOP_ATTR_RO(_name) \
655ee862730SMilan Broz static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
656ee862730SMilan Broz static ssize_t loop_attr_do_show_##_name(struct device *d, \
657ee862730SMilan Broz struct device_attribute *attr, char *b) \
658ee862730SMilan Broz { \
659ee862730SMilan Broz return loop_attr_show(d, b, loop_attr_##_name##_show); \
660ee862730SMilan Broz } \
661ee862730SMilan Broz static struct device_attribute loop_attr_##_name = \
6625657a819SJoe Perches __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL);
663ee862730SMilan Broz
loop_attr_backing_file_show(struct loop_device * lo,char * buf)664ee862730SMilan Broz static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
665ee862730SMilan Broz {
666ee862730SMilan Broz ssize_t ret;
667ee862730SMilan Broz char *p = NULL;
668ee862730SMilan Broz
66905eb0f25SKay Sievers spin_lock_irq(&lo->lo_lock);
670ee862730SMilan Broz if (lo->lo_backing_file)
6719bf39ab2SMiklos Szeredi p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1);
67205eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock);
673ee862730SMilan Broz
674ee862730SMilan Broz if (IS_ERR_OR_NULL(p))
675ee862730SMilan Broz ret = PTR_ERR(p);
676ee862730SMilan Broz else {
677ee862730SMilan Broz ret = strlen(p);
678ee862730SMilan Broz memmove(buf, p, ret);
679ee862730SMilan Broz buf[ret++] = '\n';
680ee862730SMilan Broz buf[ret] = 0;
681ee862730SMilan Broz }
682ee862730SMilan Broz
683ee862730SMilan Broz return ret;
684ee862730SMilan Broz }
685ee862730SMilan Broz
loop_attr_offset_show(struct loop_device * lo,char * buf)686ee862730SMilan Broz static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
687ee862730SMilan Broz {
688b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_offset);
689ee862730SMilan Broz }
690ee862730SMilan Broz
loop_attr_sizelimit_show(struct loop_device * lo,char * buf)691ee862730SMilan Broz static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
692ee862730SMilan Broz {
693b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
694ee862730SMilan Broz }
695ee862730SMilan Broz
loop_attr_autoclear_show(struct loop_device * lo,char * buf)696ee862730SMilan Broz static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
697ee862730SMilan Broz {
698ee862730SMilan Broz int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
699ee862730SMilan Broz
700b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%s\n", autoclear ? "1" : "0");
701ee862730SMilan Broz }
702ee862730SMilan Broz
loop_attr_partscan_show(struct loop_device * lo,char * buf)703e03c8dd1SKay Sievers static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
704e03c8dd1SKay Sievers {
705e03c8dd1SKay Sievers int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
706e03c8dd1SKay Sievers
707b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%s\n", partscan ? "1" : "0");
708e03c8dd1SKay Sievers }
709e03c8dd1SKay Sievers
loop_attr_dio_show(struct loop_device * lo,char * buf)7102e5ab5f3SMing Lei static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
7112e5ab5f3SMing Lei {
7122e5ab5f3SMing Lei int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
7132e5ab5f3SMing Lei
714b27824d3SChaitanya Kulkarni return sysfs_emit(buf, "%s\n", dio ? "1" : "0");
7152e5ab5f3SMing Lei }
7162e5ab5f3SMing Lei
717ee862730SMilan Broz LOOP_ATTR_RO(backing_file);
718ee862730SMilan Broz LOOP_ATTR_RO(offset);
719ee862730SMilan Broz LOOP_ATTR_RO(sizelimit);
720ee862730SMilan Broz LOOP_ATTR_RO(autoclear);
721e03c8dd1SKay Sievers LOOP_ATTR_RO(partscan);
7222e5ab5f3SMing Lei LOOP_ATTR_RO(dio);
723ee862730SMilan Broz
724ee862730SMilan Broz static struct attribute *loop_attrs[] = {
725ee862730SMilan Broz &loop_attr_backing_file.attr,
726ee862730SMilan Broz &loop_attr_offset.attr,
727ee862730SMilan Broz &loop_attr_sizelimit.attr,
728ee862730SMilan Broz &loop_attr_autoclear.attr,
729e03c8dd1SKay Sievers &loop_attr_partscan.attr,
7302e5ab5f3SMing Lei &loop_attr_dio.attr,
731ee862730SMilan Broz NULL,
732ee862730SMilan Broz };
733ee862730SMilan Broz
734ee862730SMilan Broz static struct attribute_group loop_attribute_group = {
735ee862730SMilan Broz .name = "loop",
736ee862730SMilan Broz .attrs= loop_attrs,
737ee862730SMilan Broz };
738ee862730SMilan Broz
loop_sysfs_init(struct loop_device * lo)739d3349b6bSTetsuo Handa static void loop_sysfs_init(struct loop_device *lo)
740ee862730SMilan Broz {
741d3349b6bSTetsuo Handa lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
742ee862730SMilan Broz &loop_attribute_group);
743ee862730SMilan Broz }
744ee862730SMilan Broz
loop_sysfs_exit(struct loop_device * lo)745ee862730SMilan Broz static void loop_sysfs_exit(struct loop_device *lo)
746ee862730SMilan Broz {
747d3349b6bSTetsuo Handa if (lo->sysfs_inited)
748ee862730SMilan Broz sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
749ee862730SMilan Broz &loop_attribute_group);
750ee862730SMilan Broz }
751ee862730SMilan Broz
loop_config_discard(struct loop_device * lo)752dfaa2ef6SLukas Czerner static void loop_config_discard(struct loop_device *lo)
753dfaa2ef6SLukas Czerner {
754dfaa2ef6SLukas Czerner struct file *file = lo->lo_backing_file;
755dfaa2ef6SLukas Czerner struct inode *inode = file->f_mapping->host;
756dfaa2ef6SLukas Czerner struct request_queue *q = lo->lo_queue;
757bcb21c8cSMing Lei u32 granularity, max_discard_sectors;
758dfaa2ef6SLukas Czerner
759dfaa2ef6SLukas Czerner /*
760c52abf56SEvan Green * If the backing device is a block device, mirror its zeroing
761c52abf56SEvan Green * capability. Set the discard sectors to the block device's zeroing
762c52abf56SEvan Green * capabilities because loop discards result in blkdev_issue_zeroout(),
763c52abf56SEvan Green * not blkdev_issue_discard(). This maintains consistent behavior with
764c52abf56SEvan Green * file-backed loop devices: discarded regions read back as zero.
765c52abf56SEvan Green */
76647e96246SChristoph Hellwig if (S_ISBLK(inode->i_mode)) {
7674e7b5671SChristoph Hellwig struct request_queue *backingq = bdev_get_queue(I_BDEV(inode));
768c52abf56SEvan Green
769bcb21c8cSMing Lei max_discard_sectors = backingq->limits.max_write_zeroes_sectors;
7707b47ef52SChristoph Hellwig granularity = bdev_discard_granularity(I_BDEV(inode)) ?:
771bcb21c8cSMing Lei queue_physical_block_size(backingq);
772c52abf56SEvan Green
773c52abf56SEvan Green /*
774dfaa2ef6SLukas Czerner * We use punch hole to reclaim the free space used by the
77547e96246SChristoph Hellwig * image a.k.a. discard.
776dfaa2ef6SLukas Czerner */
77747e96246SChristoph Hellwig } else if (!file->f_op->fallocate) {
778bcb21c8cSMing Lei max_discard_sectors = 0;
779bcb21c8cSMing Lei granularity = 0;
780dfaa2ef6SLukas Czerner
781c52abf56SEvan Green } else {
78206582bc8SMing Lei struct kstatfs sbuf;
78306582bc8SMing Lei
784bcb21c8cSMing Lei max_discard_sectors = UINT_MAX >> 9;
78506582bc8SMing Lei if (!vfs_statfs(&file->f_path, &sbuf))
78606582bc8SMing Lei granularity = sbuf.f_bsize;
78706582bc8SMing Lei else
78806582bc8SMing Lei max_discard_sectors = 0;
789c52abf56SEvan Green }
790c52abf56SEvan Green
791bcb21c8cSMing Lei if (max_discard_sectors) {
792bcb21c8cSMing Lei q->limits.discard_granularity = granularity;
793bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, max_discard_sectors);
794bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
795bcb21c8cSMing Lei } else {
796bcb21c8cSMing Lei q->limits.discard_granularity = 0;
797bcb21c8cSMing Lei blk_queue_max_discard_sectors(q, 0);
798bcb21c8cSMing Lei blk_queue_max_write_zeroes_sectors(q, 0);
799dfaa2ef6SLukas Czerner }
800bcb21c8cSMing Lei }
801dfaa2ef6SLukas Czerner
80287579e9bSDan Schatzberg struct loop_worker {
80387579e9bSDan Schatzberg struct rb_node rb_node;
80487579e9bSDan Schatzberg struct work_struct work;
80587579e9bSDan Schatzberg struct list_head cmd_list;
80687579e9bSDan Schatzberg struct list_head idle_list;
80787579e9bSDan Schatzberg struct loop_device *lo;
808c74d40e8SDan Schatzberg struct cgroup_subsys_state *blkcg_css;
80987579e9bSDan Schatzberg unsigned long last_ran_at;
81087579e9bSDan Schatzberg };
811e03a3d7aSMing Lei
81287579e9bSDan Schatzberg static void loop_workfn(struct work_struct *work);
813b2ee7d46SNeilBrown
81487579e9bSDan Schatzberg #ifdef CONFIG_BLK_CGROUP
queue_on_root_worker(struct cgroup_subsys_state * css)81587579e9bSDan Schatzberg static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
816e03a3d7aSMing Lei {
81787579e9bSDan Schatzberg return !css || css == blkcg_root_css;
81887579e9bSDan Schatzberg }
81987579e9bSDan Schatzberg #else
queue_on_root_worker(struct cgroup_subsys_state * css)82087579e9bSDan Schatzberg static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
82187579e9bSDan Schatzberg {
82287579e9bSDan Schatzberg return !css;
82387579e9bSDan Schatzberg }
82487579e9bSDan Schatzberg #endif
82587579e9bSDan Schatzberg
loop_queue_work(struct loop_device * lo,struct loop_cmd * cmd)82687579e9bSDan Schatzberg static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd)
82787579e9bSDan Schatzberg {
828413ec805SColin Ian King struct rb_node **node, *parent = NULL;
82987579e9bSDan Schatzberg struct loop_worker *cur_worker, *worker = NULL;
83087579e9bSDan Schatzberg struct work_struct *work;
83187579e9bSDan Schatzberg struct list_head *cmd_list;
83287579e9bSDan Schatzberg
83387579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock);
83487579e9bSDan Schatzberg
835c74d40e8SDan Schatzberg if (queue_on_root_worker(cmd->blkcg_css))
83687579e9bSDan Schatzberg goto queue_work;
83787579e9bSDan Schatzberg
83887579e9bSDan Schatzberg node = &lo->worker_tree.rb_node;
83987579e9bSDan Schatzberg
84087579e9bSDan Schatzberg while (*node) {
84187579e9bSDan Schatzberg parent = *node;
84287579e9bSDan Schatzberg cur_worker = container_of(*node, struct loop_worker, rb_node);
843c74d40e8SDan Schatzberg if (cur_worker->blkcg_css == cmd->blkcg_css) {
84487579e9bSDan Schatzberg worker = cur_worker;
84587579e9bSDan Schatzberg break;
846c74d40e8SDan Schatzberg } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) {
84787579e9bSDan Schatzberg node = &(*node)->rb_left;
84887579e9bSDan Schatzberg } else {
84987579e9bSDan Schatzberg node = &(*node)->rb_right;
85087579e9bSDan Schatzberg }
85187579e9bSDan Schatzberg }
85287579e9bSDan Schatzberg if (worker)
85387579e9bSDan Schatzberg goto queue_work;
85487579e9bSDan Schatzberg
85587579e9bSDan Schatzberg worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN);
85687579e9bSDan Schatzberg /*
85787579e9bSDan Schatzberg * In the event we cannot allocate a worker, just queue on the
858c74d40e8SDan Schatzberg * rootcg worker and issue the I/O as the rootcg
85987579e9bSDan Schatzberg */
860c74d40e8SDan Schatzberg if (!worker) {
861c74d40e8SDan Schatzberg cmd->blkcg_css = NULL;
862c74d40e8SDan Schatzberg if (cmd->memcg_css)
863c74d40e8SDan Schatzberg css_put(cmd->memcg_css);
864c74d40e8SDan Schatzberg cmd->memcg_css = NULL;
86587579e9bSDan Schatzberg goto queue_work;
866c74d40e8SDan Schatzberg }
86787579e9bSDan Schatzberg
868c74d40e8SDan Schatzberg worker->blkcg_css = cmd->blkcg_css;
869c74d40e8SDan Schatzberg css_get(worker->blkcg_css);
87087579e9bSDan Schatzberg INIT_WORK(&worker->work, loop_workfn);
87187579e9bSDan Schatzberg INIT_LIST_HEAD(&worker->cmd_list);
87287579e9bSDan Schatzberg INIT_LIST_HEAD(&worker->idle_list);
87387579e9bSDan Schatzberg worker->lo = lo;
87487579e9bSDan Schatzberg rb_link_node(&worker->rb_node, parent, node);
87587579e9bSDan Schatzberg rb_insert_color(&worker->rb_node, &lo->worker_tree);
87687579e9bSDan Schatzberg queue_work:
87787579e9bSDan Schatzberg if (worker) {
87887579e9bSDan Schatzberg /*
87987579e9bSDan Schatzberg * We need to remove from the idle list here while
88087579e9bSDan Schatzberg * holding the lock so that the idle timer doesn't
88187579e9bSDan Schatzberg * free the worker
88287579e9bSDan Schatzberg */
88387579e9bSDan Schatzberg if (!list_empty(&worker->idle_list))
88487579e9bSDan Schatzberg list_del_init(&worker->idle_list);
88587579e9bSDan Schatzberg work = &worker->work;
88687579e9bSDan Schatzberg cmd_list = &worker->cmd_list;
88787579e9bSDan Schatzberg } else {
88887579e9bSDan Schatzberg work = &lo->rootcg_work;
88987579e9bSDan Schatzberg cmd_list = &lo->rootcg_cmd_list;
89087579e9bSDan Schatzberg }
89187579e9bSDan Schatzberg list_add_tail(&cmd->list_entry, cmd_list);
89287579e9bSDan Schatzberg queue_work(lo->workqueue, work);
89387579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock);
894e03a3d7aSMing Lei }
895e03a3d7aSMing Lei
loop_set_timer(struct loop_device * lo)8962cf429b5SChristoph Hellwig static void loop_set_timer(struct loop_device *lo)
8972cf429b5SChristoph Hellwig {
8982cf429b5SChristoph Hellwig timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT);
8992cf429b5SChristoph Hellwig }
9002cf429b5SChristoph Hellwig
loop_free_idle_workers(struct loop_device * lo,bool delete_all)9012cf429b5SChristoph Hellwig static void loop_free_idle_workers(struct loop_device *lo, bool delete_all)
9022cf429b5SChristoph Hellwig {
9032cf429b5SChristoph Hellwig struct loop_worker *pos, *worker;
9042cf429b5SChristoph Hellwig
9052cf429b5SChristoph Hellwig spin_lock_irq(&lo->lo_work_lock);
9062cf429b5SChristoph Hellwig list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
9072cf429b5SChristoph Hellwig idle_list) {
9082cf429b5SChristoph Hellwig if (!delete_all &&
9092cf429b5SChristoph Hellwig time_is_after_jiffies(worker->last_ran_at +
9102cf429b5SChristoph Hellwig LOOP_IDLE_WORKER_TIMEOUT))
9112cf429b5SChristoph Hellwig break;
9122cf429b5SChristoph Hellwig list_del(&worker->idle_list);
9132cf429b5SChristoph Hellwig rb_erase(&worker->rb_node, &lo->worker_tree);
9142cf429b5SChristoph Hellwig css_put(worker->blkcg_css);
9152cf429b5SChristoph Hellwig kfree(worker);
9162cf429b5SChristoph Hellwig }
9172cf429b5SChristoph Hellwig if (!list_empty(&lo->idle_worker_list))
9182cf429b5SChristoph Hellwig loop_set_timer(lo);
9192cf429b5SChristoph Hellwig spin_unlock_irq(&lo->lo_work_lock);
9202cf429b5SChristoph Hellwig }
9212cf429b5SChristoph Hellwig
loop_free_idle_workers_timer(struct timer_list * timer)9222cf429b5SChristoph Hellwig static void loop_free_idle_workers_timer(struct timer_list *timer)
9232cf429b5SChristoph Hellwig {
9242cf429b5SChristoph Hellwig struct loop_device *lo = container_of(timer, struct loop_device, timer);
9252cf429b5SChristoph Hellwig
9262cf429b5SChristoph Hellwig return loop_free_idle_workers(lo, false);
9272cf429b5SChristoph Hellwig }
9282cf429b5SChristoph Hellwig
loop_update_rotational(struct loop_device * lo)92956a85fd8SHolger Hoffstätte static void loop_update_rotational(struct loop_device *lo)
93056a85fd8SHolger Hoffstätte {
93156a85fd8SHolger Hoffstätte struct file *file = lo->lo_backing_file;
93256a85fd8SHolger Hoffstätte struct inode *file_inode = file->f_mapping->host;
93356a85fd8SHolger Hoffstätte struct block_device *file_bdev = file_inode->i_sb->s_bdev;
93456a85fd8SHolger Hoffstätte struct request_queue *q = lo->lo_queue;
93556a85fd8SHolger Hoffstätte bool nonrot = true;
93656a85fd8SHolger Hoffstätte
93756a85fd8SHolger Hoffstätte /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
93856a85fd8SHolger Hoffstätte if (file_bdev)
93910f0d2a5SChristoph Hellwig nonrot = bdev_nonrot(file_bdev);
94056a85fd8SHolger Hoffstätte
94156a85fd8SHolger Hoffstätte if (nonrot)
94256a85fd8SHolger Hoffstätte blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
94356a85fd8SHolger Hoffstätte else
94456a85fd8SHolger Hoffstätte blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
94556a85fd8SHolger Hoffstätte }
94656a85fd8SHolger Hoffstätte
94762ab466cSMartijn Coenen /**
94862ab466cSMartijn Coenen * loop_set_status_from_info - configure device from loop_info
94962ab466cSMartijn Coenen * @lo: struct loop_device to configure
95062ab466cSMartijn Coenen * @info: struct loop_info64 to configure the device with
95162ab466cSMartijn Coenen *
95262ab466cSMartijn Coenen * Configures the loop device parameters according to the passed
95362ab466cSMartijn Coenen * in loop_info64 configuration.
95462ab466cSMartijn Coenen */
95562ab466cSMartijn Coenen static int
loop_set_status_from_info(struct loop_device * lo,const struct loop_info64 * info)95662ab466cSMartijn Coenen loop_set_status_from_info(struct loop_device *lo,
95762ab466cSMartijn Coenen const struct loop_info64 *info)
95862ab466cSMartijn Coenen {
95962ab466cSMartijn Coenen if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
96062ab466cSMartijn Coenen return -EINVAL;
96162ab466cSMartijn Coenen
96247e96246SChristoph Hellwig switch (info->lo_encrypt_type) {
96347e96246SChristoph Hellwig case LO_CRYPT_NONE:
96447e96246SChristoph Hellwig break;
96547e96246SChristoph Hellwig case LO_CRYPT_XOR:
96647e96246SChristoph Hellwig pr_warn("support for the xor transformation has been removed.\n");
96762ab466cSMartijn Coenen return -EINVAL;
96847e96246SChristoph Hellwig case LO_CRYPT_CRYPTOAPI:
96947e96246SChristoph Hellwig pr_warn("support for cryptoloop has been removed. Use dm-crypt instead.\n");
97062ab466cSMartijn Coenen return -EINVAL;
97147e96246SChristoph Hellwig default:
97247e96246SChristoph Hellwig return -EINVAL;
97347e96246SChristoph Hellwig }
97462ab466cSMartijn Coenen
9759f6ad5d5SZhong Jinghua /* Avoid assigning overflow values */
9769f6ad5d5SZhong Jinghua if (info->lo_offset > LLONG_MAX || info->lo_sizelimit > LLONG_MAX)
9779f6ad5d5SZhong Jinghua return -EOVERFLOW;
9789f6ad5d5SZhong Jinghua
97962ab466cSMartijn Coenen lo->lo_offset = info->lo_offset;
98062ab466cSMartijn Coenen lo->lo_sizelimit = info->lo_sizelimit;
981c490a0b5SSiddh Raman Pant
98262ab466cSMartijn Coenen memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
98362ab466cSMartijn Coenen lo->lo_file_name[LO_NAME_SIZE-1] = 0;
984faf1d254SMartijn Coenen lo->lo_flags = info->lo_flags;
98562ab466cSMartijn Coenen return 0;
98662ab466cSMartijn Coenen }
98762ab466cSMartijn Coenen
loop_configure(struct loop_device * lo,blk_mode_t mode,struct block_device * bdev,const struct loop_config * config)98805bdb996SChristoph Hellwig static int loop_configure(struct loop_device *lo, blk_mode_t mode,
9893448914eSMartijn Coenen struct block_device *bdev,
9903448914eSMartijn Coenen const struct loop_config *config)
9911da177e4SLinus Torvalds {
9923ce6e1f6STetsuo Handa struct file *file = fget(config->fd);
9931da177e4SLinus Torvalds struct inode *inode;
9941da177e4SLinus Torvalds struct address_space *mapping;
9951da177e4SLinus Torvalds int error;
9961da177e4SLinus Torvalds loff_t size;
99785b0a54aSJan Kara bool partscan;
9983448914eSMartijn Coenen unsigned short bsize;
9993ce6e1f6STetsuo Handa bool is_loop;
10003ce6e1f6STetsuo Handa
10013ce6e1f6STetsuo Handa if (!file)
10023ce6e1f6STetsuo Handa return -EBADF;
10033ce6e1f6STetsuo Handa is_loop = is_loop_device(file);
10041da177e4SLinus Torvalds
10051da177e4SLinus Torvalds /* This is safe, since we have a reference from open(). */
10061da177e4SLinus Torvalds __module_get(THIS_MODULE);
10071da177e4SLinus Torvalds
100833ec3e53SJan Kara /*
100933ec3e53SJan Kara * If we don't hold exclusive handle for the device, upgrade to it
101033ec3e53SJan Kara * here to avoid changing device under exclusive owner.
101133ec3e53SJan Kara */
101205bdb996SChristoph Hellwig if (!(mode & BLK_OPEN_EXCL)) {
10130718afd4SChristoph Hellwig error = bd_prepare_to_claim(bdev, loop_configure, NULL);
1014ecbe6bc0SChristoph Hellwig if (error)
1015757ecf40SJan Kara goto out_putf;
101633ec3e53SJan Kara }
101733ec3e53SJan Kara
10183ce6e1f6STetsuo Handa error = loop_global_lock_killable(lo, is_loop);
101933ec3e53SJan Kara if (error)
102033ec3e53SJan Kara goto out_bdev;
1021757ecf40SJan Kara
10221da177e4SLinus Torvalds error = -EBUSY;
10231da177e4SLinus Torvalds if (lo->lo_state != Lo_unbound)
1024757ecf40SJan Kara goto out_unlock;
10251da177e4SLinus Torvalds
1026d2ac838eSTheodore Ts'o error = loop_validate_file(file, bdev);
1027d2ac838eSTheodore Ts'o if (error)
1028757ecf40SJan Kara goto out_unlock;
10291da177e4SLinus Torvalds
10301da177e4SLinus Torvalds mapping = file->f_mapping;
10311da177e4SLinus Torvalds inode = mapping->host;
10321da177e4SLinus Torvalds
10333448914eSMartijn Coenen if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) {
10343448914eSMartijn Coenen error = -EINVAL;
10353448914eSMartijn Coenen goto out_unlock;
10363448914eSMartijn Coenen }
10373448914eSMartijn Coenen
10383448914eSMartijn Coenen if (config->block_size) {
1039af3c570fSXie Yongji error = blk_validate_block_size(config->block_size);
10403448914eSMartijn Coenen if (error)
10413448914eSMartijn Coenen goto out_unlock;
10423448914eSMartijn Coenen }
10433448914eSMartijn Coenen
10443448914eSMartijn Coenen error = loop_set_status_from_info(lo, &config->info);
10453448914eSMartijn Coenen if (error)
10463448914eSMartijn Coenen goto out_unlock;
10473448914eSMartijn Coenen
104805bdb996SChristoph Hellwig if (!(file->f_mode & FMODE_WRITE) || !(mode & BLK_OPEN_WRITE) ||
1049283e7e5dSAl Viro !file->f_op->write_iter)
10503448914eSMartijn Coenen lo->lo_flags |= LO_FLAGS_READ_ONLY;
1051083a6a50SMartijn Coenen
1052d292dc80SChristoph Hellwig if (!lo->workqueue) {
105387579e9bSDan Schatzberg lo->workqueue = alloc_workqueue("loop%d",
105487579e9bSDan Schatzberg WQ_UNBOUND | WQ_FREEZABLE,
1055d292dc80SChristoph Hellwig 0, lo->lo_number);
105687579e9bSDan Schatzberg if (!lo->workqueue) {
105787579e9bSDan Schatzberg error = -ENOMEM;
1058757ecf40SJan Kara goto out_unlock;
105987579e9bSDan Schatzberg }
1060d292dc80SChristoph Hellwig }
10611da177e4SLinus Torvalds
1062bb430b69SAlyssa Ross /* suppress uevents while reconfiguring the device */
1063bb430b69SAlyssa Ross dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 1);
1064bb430b69SAlyssa Ross
1065ab6860f6SChristoph Hellwig disk_force_media_change(lo->lo_disk);
10667a2f0ce1SChristoph Hellwig set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0);
10671da177e4SLinus Torvalds
10683448914eSMartijn Coenen lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
10691da177e4SLinus Torvalds lo->lo_device = bdev;
10701da177e4SLinus Torvalds lo->lo_backing_file = file;
10711da177e4SLinus Torvalds lo->old_gfp_mask = mapping_gfp_mask(mapping);
10721da177e4SLinus Torvalds mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
10731da177e4SLinus Torvalds
10743448914eSMartijn Coenen if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
107521d0727fSJens Axboe blk_queue_write_cache(lo->lo_queue, true, false);
107668db1961SNikanth Karthikesan
10773448914eSMartijn Coenen if (config->block_size)
10783448914eSMartijn Coenen bsize = config->block_size;
107996ed320dSLinus Torvalds else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev)
108085560117SMartijn Coenen /* In case of direct I/O, match underlying block size */
10813448914eSMartijn Coenen bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
10823448914eSMartijn Coenen else
10833448914eSMartijn Coenen bsize = 512;
108485560117SMartijn Coenen
108585560117SMartijn Coenen blk_queue_logical_block_size(lo->lo_queue, bsize);
108685560117SMartijn Coenen blk_queue_physical_block_size(lo->lo_queue, bsize);
108785560117SMartijn Coenen blk_queue_io_min(lo->lo_queue, bsize);
108885560117SMartijn Coenen
10892b9ac22bSKristian Klausen loop_config_discard(lo);
109056a85fd8SHolger Hoffstätte loop_update_rotational(lo);
10912e5ab5f3SMing Lei loop_update_dio(lo);
1092ee862730SMilan Broz loop_sysfs_init(lo);
109379e5dc59SMartijn Coenen
109479e5dc59SMartijn Coenen size = get_loop_size(lo, file);
10955795b6f5SMartijn Coenen loop_set_size(lo, size);
10961da177e4SLinus Torvalds
10973ce6e1f6STetsuo Handa /* Order wrt reading lo_state in loop_validate_file(). */
10983ce6e1f6STetsuo Handa wmb();
10993ce6e1f6STetsuo Handa
11006c997918SSerge E. Hallyn lo->lo_state = Lo_bound;
1101e03c8dd1SKay Sievers if (part_shift)
1102e03c8dd1SKay Sievers lo->lo_flags |= LO_FLAGS_PARTSCAN;
110385b0a54aSJan Kara partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
1104fe6a8fc5SLennart Poettering if (partscan)
1105b9684a71SChristoph Hellwig clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
1106c1681bf8SAnatol Pomozov
1107bb430b69SAlyssa Ross /* enable and uncork uevent now that we are done */
1108bb430b69SAlyssa Ross dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 0);
1109bb430b69SAlyssa Ross
11103ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop);
111185b0a54aSJan Kara if (partscan)
11120384264eSChristoph Hellwig loop_reread_partitions(lo);
1113bb430b69SAlyssa Ross
111405bdb996SChristoph Hellwig if (!(mode & BLK_OPEN_EXCL))
111537c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure);
1116498ef5c7SChristoph Hellwig
1117bb430b69SAlyssa Ross return 0;
11181da177e4SLinus Torvalds
1119757ecf40SJan Kara out_unlock:
11203ce6e1f6STetsuo Handa loop_global_unlock(lo, is_loop);
112133ec3e53SJan Kara out_bdev:
112205bdb996SChristoph Hellwig if (!(mode & BLK_OPEN_EXCL))
112337c3fc9aSChristoph Hellwig bd_abort_claiming(bdev, loop_configure);
11241da177e4SLinus Torvalds out_putf:
11251da177e4SLinus Torvalds fput(file);
11261da177e4SLinus Torvalds /* This is safe: open() is still holding a reference. */
11271da177e4SLinus Torvalds module_put(THIS_MODULE);
1128bb430b69SAlyssa Ross return error;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds
__loop_clr_fd(struct loop_device * lo,bool release)1131bf23747eSTetsuo Handa static void __loop_clr_fd(struct loop_device *lo, bool release)
11321da177e4SLinus Torvalds {
11336050fa4cSTetsuo Handa struct file *filp;
1134b4e3ca1aSAl Viro gfp_t gfp = lo->old_gfp_mask;
11351da177e4SLinus Torvalds
11364ceddce5SMauricio Faria de Oliveira if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
11374ceddce5SMauricio Faria de Oliveira blk_queue_write_cache(lo->lo_queue, false, false);
11384ceddce5SMauricio Faria de Oliveira
11391fe0b1acSChristoph Hellwig /*
11401fe0b1acSChristoph Hellwig * Freeze the request queue when unbinding on a live file descriptor and
11411fe0b1acSChristoph Hellwig * thus an open device. When called from ->release we are guaranteed
11421fe0b1acSChristoph Hellwig * that there is no I/O in progress already.
11431fe0b1acSChristoph Hellwig */
11441fe0b1acSChristoph Hellwig if (!release)
1145f8933667SMing Lei blk_mq_freeze_queue(lo->lo_queue);
1146f8933667SMing Lei
11471da177e4SLinus Torvalds spin_lock_irq(&lo->lo_lock);
11486050fa4cSTetsuo Handa filp = lo->lo_backing_file;
11491da177e4SLinus Torvalds lo->lo_backing_file = NULL;
115005eb0f25SKay Sievers spin_unlock_irq(&lo->lo_lock);
11511da177e4SLinus Torvalds
11521da177e4SLinus Torvalds lo->lo_device = NULL;
11531da177e4SLinus Torvalds lo->lo_offset = 0;
11541da177e4SLinus Torvalds lo->lo_sizelimit = 0;
11551da177e4SLinus Torvalds memset(lo->lo_file_name, 0, LO_NAME_SIZE);
115689e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, 512);
1157bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, 512);
1158bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, 512);
1159e515be8fSXie Yongji invalidate_disk(lo->lo_disk);
116051a0bb0cSMilan Broz loop_sysfs_exit(lo);
1161c3473c63SDavid Zeuthen /* let user-space know about this change */
116219f553dbSXie Yongji kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
11631da177e4SLinus Torvalds mapping_set_gfp_mask(filp->f_mapping, gfp);
1164bf23747eSTetsuo Handa /* This is safe: open() is still holding a reference. */
1165bf23747eSTetsuo Handa module_put(THIS_MODULE);
11661fe0b1acSChristoph Hellwig if (!release)
1167f8933667SMing Lei blk_mq_unfreeze_queue(lo->lo_queue);
1168f8933667SMing Lei
1169ab6860f6SChristoph Hellwig disk_force_media_change(lo->lo_disk);
11706050fa4cSTetsuo Handa
11716050fa4cSTetsuo Handa if (lo->lo_flags & LO_FLAGS_PARTSCAN) {
11726050fa4cSTetsuo Handa int err;
11736050fa4cSTetsuo Handa
1174bf23747eSTetsuo Handa /*
1175bf23747eSTetsuo Handa * open_mutex has been held already in release path, so don't
1176bf23747eSTetsuo Handa * acquire it if this function is called in such case.
1177bf23747eSTetsuo Handa *
1178bf23747eSTetsuo Handa * If the reread partition isn't from release path, lo_refcnt
1179bf23747eSTetsuo Handa * must be at least one and it can only become zero when the
1180bf23747eSTetsuo Handa * current holder is released.
1181bf23747eSTetsuo Handa */
1182bf23747eSTetsuo Handa if (!release)
11830384264eSChristoph Hellwig mutex_lock(&lo->lo_disk->open_mutex);
11840384264eSChristoph Hellwig err = bdev_disk_changed(lo->lo_disk, false);
1185bf23747eSTetsuo Handa if (!release)
11860384264eSChristoph Hellwig mutex_unlock(&lo->lo_disk->open_mutex);
118740853d6fSDongli Zhang if (err)
1188d57f3374SJan Kara pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
11896050fa4cSTetsuo Handa __func__, lo->lo_number, err);
1190d57f3374SJan Kara /* Device is gone, no point in returning error */
1191d57f3374SJan Kara }
1192758a58d0SDongli Zhang
1193bf23747eSTetsuo Handa /*
1194bf23747eSTetsuo Handa * lo->lo_state is set to Lo_unbound here after above partscan has
1195bf23747eSTetsuo Handa * finished. There cannot be anybody else entering __loop_clr_fd() as
1196bf23747eSTetsuo Handa * Lo_rundown state protects us from all the other places trying to
1197bf23747eSTetsuo Handa * change the 'lo' device.
1198bf23747eSTetsuo Handa */
1199758a58d0SDongli Zhang lo->lo_flags = 0;
1200758a58d0SDongli Zhang if (!part_shift)
1201b9684a71SChristoph Hellwig set_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
12026050fa4cSTetsuo Handa mutex_lock(&lo->lo_mutex);
1203758a58d0SDongli Zhang lo->lo_state = Lo_unbound;
12046cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex);
1205758a58d0SDongli Zhang
1206bf23747eSTetsuo Handa /*
1207bf23747eSTetsuo Handa * Need not hold lo_mutex to fput backing file. Calling fput holding
1208bf23747eSTetsuo Handa * lo_mutex triggers a circular lock dependency possibility warning as
1209bf23747eSTetsuo Handa * fput can take open_mutex which is usually taken before lo_mutex.
1210bf23747eSTetsuo Handa */
1211bf23747eSTetsuo Handa fput(filp);
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds
loop_clr_fd(struct loop_device * lo)1214a2505b79SJan Kara static int loop_clr_fd(struct loop_device *lo)
1215a2505b79SJan Kara {
12167ccd0791SJan Kara int err;
12177ccd0791SJan Kara
1218158eaebaSTetsuo Handa /*
1219158eaebaSTetsuo Handa * Since lo_ioctl() is called without locks held, it is possible that
1220158eaebaSTetsuo Handa * loop_configure()/loop_change_fd() and loop_clr_fd() run in parallel.
1221158eaebaSTetsuo Handa *
1222158eaebaSTetsuo Handa * Therefore, use global lock when setting Lo_rundown state in order to
1223158eaebaSTetsuo Handa * make sure that loop_validate_file() will fail if the "struct file"
1224158eaebaSTetsuo Handa * which loop_configure()/loop_change_fd() found via fget() was this
1225158eaebaSTetsuo Handa * loop device.
1226158eaebaSTetsuo Handa */
1227158eaebaSTetsuo Handa err = loop_global_lock_killable(lo, true);
12287ccd0791SJan Kara if (err)
12297ccd0791SJan Kara return err;
12307ccd0791SJan Kara if (lo->lo_state != Lo_bound) {
1231158eaebaSTetsuo Handa loop_global_unlock(lo, true);
1232a2505b79SJan Kara return -ENXIO;
12337ccd0791SJan Kara }
1234a2505b79SJan Kara /*
1235a2505b79SJan Kara * If we've explicitly asked to tear down the loop device,
1236a2505b79SJan Kara * and it has an elevated reference count, set it for auto-teardown when
1237a2505b79SJan Kara * the last reference goes away. This stops $!~#$@ udev from
1238a2505b79SJan Kara * preventing teardown because it decided that it needs to run blkid on
1239a2505b79SJan Kara * the loopback device whenever they appear. xfstests is notorious for
1240a2505b79SJan Kara * failing tests because blkid via udev races with a losetup
1241a2505b79SJan Kara * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1242a2505b79SJan Kara * command to fail with EBUSY.
1243a2505b79SJan Kara */
1244a0e286b6SChristoph Hellwig if (disk_openers(lo->lo_disk) > 1) {
1245a2505b79SJan Kara lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
1246158eaebaSTetsuo Handa loop_global_unlock(lo, true);
1247a2505b79SJan Kara return 0;
1248a2505b79SJan Kara }
1249a2505b79SJan Kara lo->lo_state = Lo_rundown;
1250158eaebaSTetsuo Handa loop_global_unlock(lo, true);
1251a2505b79SJan Kara
1252bf23747eSTetsuo Handa __loop_clr_fd(lo, false);
12536050fa4cSTetsuo Handa return 0;
1254a2505b79SJan Kara }
1255a2505b79SJan Kara
12561da177e4SLinus Torvalds static int
loop_set_status(struct loop_device * lo,const struct loop_info64 * info)12571da177e4SLinus Torvalds loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
12581da177e4SLinus Torvalds {
12591da177e4SLinus Torvalds int err;
1260faf1d254SMartijn Coenen int prev_lo_flags;
126185b0a54aSJan Kara bool partscan = false;
12620c3796c2SMartijn Coenen bool size_changed = false;
12631da177e4SLinus Torvalds
12646cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex);
1265550df5fdSJan Kara if (err)
1266550df5fdSJan Kara return err;
1267550df5fdSJan Kara if (lo->lo_state != Lo_bound) {
1268550df5fdSJan Kara err = -ENXIO;
1269550df5fdSJan Kara goto out_unlock;
1270550df5fdSJan Kara }
12711da177e4SLinus Torvalds
12725db470e2SJaegeuk Kim if (lo->lo_offset != info->lo_offset ||
12735db470e2SJaegeuk Kim lo->lo_sizelimit != info->lo_sizelimit) {
12740c3796c2SMartijn Coenen size_changed = true;
12755db470e2SJaegeuk Kim sync_blockdev(lo->lo_device);
1276f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device);
12775db470e2SJaegeuk Kim }
12785db470e2SJaegeuk Kim
1279ecdd0959SMing Lei /* I/O need to be drained during transfer transition */
1280ecdd0959SMing Lei blk_mq_freeze_queue(lo->lo_queue);
1281ecdd0959SMing Lei
1282faf1d254SMartijn Coenen prev_lo_flags = lo->lo_flags;
1283faf1d254SMartijn Coenen
12840c3796c2SMartijn Coenen err = loop_set_status_from_info(lo, info);
12850c3796c2SMartijn Coenen if (err)
1286550df5fdSJan Kara goto out_unfreeze;
12870c3796c2SMartijn Coenen
1288faf1d254SMartijn Coenen /* Mask out flags that can't be set using LOOP_SET_STATUS. */
12896ac92fb5SMartijn Coenen lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS;
1290faf1d254SMartijn Coenen /* For those flags, use the previous values instead */
1291faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS;
1292faf1d254SMartijn Coenen /* For flags that can't be cleared, use previous values too */
1293faf1d254SMartijn Coenen lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS;
1294faf1d254SMartijn Coenen
1295b0bd158dSMartijn Coenen if (size_changed) {
1296b0bd158dSMartijn Coenen loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
1297b0bd158dSMartijn Coenen lo->lo_backing_file);
1298b0bd158dSMartijn Coenen loop_set_size(lo, new_size);
1299b040ad9cSArnd Bergmann }
1300541c742aSGuo Chao
1301dfaa2ef6SLukas Czerner loop_config_discard(lo);
13021da177e4SLinus Torvalds
13032e5ab5f3SMing Lei /* update dio if lo_offset or transfer is changed */
13042e5ab5f3SMing Lei __loop_update_dio(lo, lo->use_dio);
13052e5ab5f3SMing Lei
1306550df5fdSJan Kara out_unfreeze:
1307ecdd0959SMing Lei blk_mq_unfreeze_queue(lo->lo_queue);
1308e02898b4SOmar Sandoval
1309faf1d254SMartijn Coenen if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) &&
1310faf1d254SMartijn Coenen !(prev_lo_flags & LO_FLAGS_PARTSCAN)) {
1311b9684a71SChristoph Hellwig clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
131285b0a54aSJan Kara partscan = true;
1313e02898b4SOmar Sandoval }
1314550df5fdSJan Kara out_unlock:
13156cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex);
131685b0a54aSJan Kara if (partscan)
13170384264eSChristoph Hellwig loop_reread_partitions(lo);
1318e02898b4SOmar Sandoval
1319ecdd0959SMing Lei return err;
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds
13221da177e4SLinus Torvalds static int
loop_get_status(struct loop_device * lo,struct loop_info64 * info)13231da177e4SLinus Torvalds loop_get_status(struct loop_device *lo, struct loop_info64 *info)
13241da177e4SLinus Torvalds {
1325b1ab5fa3STetsuo Handa struct path path;
13261da177e4SLinus Torvalds struct kstat stat;
13272d1d4c1eSOmar Sandoval int ret;
13281da177e4SLinus Torvalds
13296cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex);
13304a5ce9baSJan Kara if (ret)
13314a5ce9baSJan Kara return ret;
13322d1d4c1eSOmar Sandoval if (lo->lo_state != Lo_bound) {
13336cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex);
13341da177e4SLinus Torvalds return -ENXIO;
13352d1d4c1eSOmar Sandoval }
13362d1d4c1eSOmar Sandoval
13371da177e4SLinus Torvalds memset(info, 0, sizeof(*info));
13381da177e4SLinus Torvalds info->lo_number = lo->lo_number;
13391da177e4SLinus Torvalds info->lo_offset = lo->lo_offset;
13401da177e4SLinus Torvalds info->lo_sizelimit = lo->lo_sizelimit;
13411da177e4SLinus Torvalds info->lo_flags = lo->lo_flags;
13421da177e4SLinus Torvalds memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
13432d1d4c1eSOmar Sandoval
13446cc8e743SPavel Tatashin /* Drop lo_mutex while we call into the filesystem. */
1345b1ab5fa3STetsuo Handa path = lo->lo_backing_file->f_path;
1346b1ab5fa3STetsuo Handa path_get(&path);
13476cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex);
1348b1ab5fa3STetsuo Handa ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
13492d1d4c1eSOmar Sandoval if (!ret) {
13502d1d4c1eSOmar Sandoval info->lo_device = huge_encode_dev(stat.dev);
13512d1d4c1eSOmar Sandoval info->lo_inode = stat.ino;
13522d1d4c1eSOmar Sandoval info->lo_rdevice = huge_encode_dev(stat.rdev);
13532d1d4c1eSOmar Sandoval }
1354b1ab5fa3STetsuo Handa path_put(&path);
13552d1d4c1eSOmar Sandoval return ret;
13561da177e4SLinus Torvalds }
13571da177e4SLinus Torvalds
13581da177e4SLinus Torvalds static void
loop_info64_from_old(const struct loop_info * info,struct loop_info64 * info64)13591da177e4SLinus Torvalds loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
13601da177e4SLinus Torvalds {
13611da177e4SLinus Torvalds memset(info64, 0, sizeof(*info64));
13621da177e4SLinus Torvalds info64->lo_number = info->lo_number;
13631da177e4SLinus Torvalds info64->lo_device = info->lo_device;
13641da177e4SLinus Torvalds info64->lo_inode = info->lo_inode;
13651da177e4SLinus Torvalds info64->lo_rdevice = info->lo_rdevice;
13661da177e4SLinus Torvalds info64->lo_offset = info->lo_offset;
13671da177e4SLinus Torvalds info64->lo_sizelimit = 0;
13681da177e4SLinus Torvalds info64->lo_flags = info->lo_flags;
13691da177e4SLinus Torvalds memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds
13721da177e4SLinus Torvalds static int
loop_info64_to_old(const struct loop_info64 * info64,struct loop_info * info)13731da177e4SLinus Torvalds loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
13741da177e4SLinus Torvalds {
13751da177e4SLinus Torvalds memset(info, 0, sizeof(*info));
13761da177e4SLinus Torvalds info->lo_number = info64->lo_number;
13771da177e4SLinus Torvalds info->lo_device = info64->lo_device;
13781da177e4SLinus Torvalds info->lo_inode = info64->lo_inode;
13791da177e4SLinus Torvalds info->lo_rdevice = info64->lo_rdevice;
13801da177e4SLinus Torvalds info->lo_offset = info64->lo_offset;
13811da177e4SLinus Torvalds info->lo_flags = info64->lo_flags;
13821da177e4SLinus Torvalds memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
13831da177e4SLinus Torvalds
13841da177e4SLinus Torvalds /* error in case values were truncated */
13851da177e4SLinus Torvalds if (info->lo_device != info64->lo_device ||
13861da177e4SLinus Torvalds info->lo_rdevice != info64->lo_rdevice ||
13871da177e4SLinus Torvalds info->lo_inode != info64->lo_inode ||
13881da177e4SLinus Torvalds info->lo_offset != info64->lo_offset)
13891da177e4SLinus Torvalds return -EOVERFLOW;
13901da177e4SLinus Torvalds
13911da177e4SLinus Torvalds return 0;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds
13941da177e4SLinus Torvalds static int
loop_set_status_old(struct loop_device * lo,const struct loop_info __user * arg)13951da177e4SLinus Torvalds loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds struct loop_info info;
13981da177e4SLinus Torvalds struct loop_info64 info64;
13991da177e4SLinus Torvalds
14001da177e4SLinus Torvalds if (copy_from_user(&info, arg, sizeof (struct loop_info)))
14011da177e4SLinus Torvalds return -EFAULT;
14021da177e4SLinus Torvalds loop_info64_from_old(&info, &info64);
14031da177e4SLinus Torvalds return loop_set_status(lo, &info64);
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds
14061da177e4SLinus Torvalds static int
loop_set_status64(struct loop_device * lo,const struct loop_info64 __user * arg)14071da177e4SLinus Torvalds loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
14081da177e4SLinus Torvalds {
14091da177e4SLinus Torvalds struct loop_info64 info64;
14101da177e4SLinus Torvalds
14111da177e4SLinus Torvalds if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
14121da177e4SLinus Torvalds return -EFAULT;
14131da177e4SLinus Torvalds return loop_set_status(lo, &info64);
14141da177e4SLinus Torvalds }
14151da177e4SLinus Torvalds
14161da177e4SLinus Torvalds static int
loop_get_status_old(struct loop_device * lo,struct loop_info __user * arg)14171da177e4SLinus Torvalds loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
14181da177e4SLinus Torvalds struct loop_info info;
14191da177e4SLinus Torvalds struct loop_info64 info64;
1420bdac616dSOmar Sandoval int err;
14211da177e4SLinus Torvalds
14224a5ce9baSJan Kara if (!arg)
1423bdac616dSOmar Sandoval return -EINVAL;
14241da177e4SLinus Torvalds err = loop_get_status(lo, &info64);
14251da177e4SLinus Torvalds if (!err)
14261da177e4SLinus Torvalds err = loop_info64_to_old(&info64, &info);
14271da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info, sizeof(info)))
14281da177e4SLinus Torvalds err = -EFAULT;
14291da177e4SLinus Torvalds
14301da177e4SLinus Torvalds return err;
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds
14331da177e4SLinus Torvalds static int
loop_get_status64(struct loop_device * lo,struct loop_info64 __user * arg)14341da177e4SLinus Torvalds loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
14351da177e4SLinus Torvalds struct loop_info64 info64;
1436bdac616dSOmar Sandoval int err;
14371da177e4SLinus Torvalds
14384a5ce9baSJan Kara if (!arg)
1439bdac616dSOmar Sandoval return -EINVAL;
14401da177e4SLinus Torvalds err = loop_get_status(lo, &info64);
14411da177e4SLinus Torvalds if (!err && copy_to_user(arg, &info64, sizeof(info64)))
14421da177e4SLinus Torvalds err = -EFAULT;
14431da177e4SLinus Torvalds
14441da177e4SLinus Torvalds return err;
14451da177e4SLinus Torvalds }
14461da177e4SLinus Torvalds
loop_set_capacity(struct loop_device * lo)144751001b7dSHannes Reinecke static int loop_set_capacity(struct loop_device *lo)
144853d66608SJ. R. Okajima {
14490a6ed1b5SMartijn Coenen loff_t size;
14500a6ed1b5SMartijn Coenen
145153d66608SJ. R. Okajima if (unlikely(lo->lo_state != Lo_bound))
14527b0576a3SGuo Chao return -ENXIO;
145353d66608SJ. R. Okajima
14540a6ed1b5SMartijn Coenen size = get_loop_size(lo, lo->lo_backing_file);
14550a6ed1b5SMartijn Coenen loop_set_size(lo, size);
1456083a6a50SMartijn Coenen
1457083a6a50SMartijn Coenen return 0;
145853d66608SJ. R. Okajima }
145953d66608SJ. R. Okajima
loop_set_dio(struct loop_device * lo,unsigned long arg)1460ab1cb278SMing Lei static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1461ab1cb278SMing Lei {
1462ab1cb278SMing Lei int error = -ENXIO;
1463ab1cb278SMing Lei if (lo->lo_state != Lo_bound)
1464ab1cb278SMing Lei goto out;
1465ab1cb278SMing Lei
1466ab1cb278SMing Lei __loop_update_dio(lo, !!arg);
1467ab1cb278SMing Lei if (lo->use_dio == !!arg)
1468ab1cb278SMing Lei return 0;
1469ab1cb278SMing Lei error = -EINVAL;
1470ab1cb278SMing Lei out:
1471ab1cb278SMing Lei return error;
1472ab1cb278SMing Lei }
1473ab1cb278SMing Lei
loop_set_block_size(struct loop_device * lo,unsigned long arg)147489e4fdecSOmar Sandoval static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
147589e4fdecSOmar Sandoval {
14765db470e2SJaegeuk Kim int err = 0;
14775db470e2SJaegeuk Kim
147889e4fdecSOmar Sandoval if (lo->lo_state != Lo_bound)
147989e4fdecSOmar Sandoval return -ENXIO;
148089e4fdecSOmar Sandoval
1481af3c570fSXie Yongji err = blk_validate_block_size(arg);
14823448914eSMartijn Coenen if (err)
14833448914eSMartijn Coenen return err;
148489e4fdecSOmar Sandoval
14857e81f99aSMartijn Coenen if (lo->lo_queue->limits.logical_block_size == arg)
14867e81f99aSMartijn Coenen return 0;
14877e81f99aSMartijn Coenen
14885db470e2SJaegeuk Kim sync_blockdev(lo->lo_device);
1489f4bd34b1SZheng Bin invalidate_bdev(lo->lo_device);
14905db470e2SJaegeuk Kim
149189e4fdecSOmar Sandoval blk_mq_freeze_queue(lo->lo_queue);
149289e4fdecSOmar Sandoval blk_queue_logical_block_size(lo->lo_queue, arg);
1493bf093753SOmar Sandoval blk_queue_physical_block_size(lo->lo_queue, arg);
1494bf093753SOmar Sandoval blk_queue_io_min(lo->lo_queue, arg);
149589e4fdecSOmar Sandoval loop_update_dio(lo);
149689e4fdecSOmar Sandoval blk_mq_unfreeze_queue(lo->lo_queue);
149789e4fdecSOmar Sandoval
14985db470e2SJaegeuk Kim return err;
149989e4fdecSOmar Sandoval }
150089e4fdecSOmar Sandoval
lo_simple_ioctl(struct loop_device * lo,unsigned int cmd,unsigned long arg)1501a1316544SJan Kara static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
1502a1316544SJan Kara unsigned long arg)
15031da177e4SLinus Torvalds {
15041da177e4SLinus Torvalds int err;
15051da177e4SLinus Torvalds
15066cc8e743SPavel Tatashin err = mutex_lock_killable(&lo->lo_mutex);
15073148ffbdSOmar Sandoval if (err)
1508a1316544SJan Kara return err;
15091da177e4SLinus Torvalds switch (cmd) {
151053d66608SJ. R. Okajima case LOOP_SET_CAPACITY:
151151001b7dSHannes Reinecke err = loop_set_capacity(lo);
151253d66608SJ. R. Okajima break;
1513ab1cb278SMing Lei case LOOP_SET_DIRECT_IO:
1514ab1cb278SMing Lei err = loop_set_dio(lo, arg);
1515ab1cb278SMing Lei break;
151689e4fdecSOmar Sandoval case LOOP_SET_BLOCK_SIZE:
151789e4fdecSOmar Sandoval err = loop_set_block_size(lo, arg);
151889e4fdecSOmar Sandoval break;
15191da177e4SLinus Torvalds default:
152047e96246SChristoph Hellwig err = -EINVAL;
15211da177e4SLinus Torvalds }
15226cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex);
1523a1316544SJan Kara return err;
1524a1316544SJan Kara }
1525f028f3b2SNikanth Karthikesan
lo_ioctl(struct block_device * bdev,blk_mode_t mode,unsigned int cmd,unsigned long arg)152605bdb996SChristoph Hellwig static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,
1527a1316544SJan Kara unsigned int cmd, unsigned long arg)
1528a1316544SJan Kara {
1529a1316544SJan Kara struct loop_device *lo = bdev->bd_disk->private_data;
1530571fae6eSMartijn Coenen void __user *argp = (void __user *) arg;
1531a1316544SJan Kara int err;
1532a1316544SJan Kara
1533a1316544SJan Kara switch (cmd) {
15343448914eSMartijn Coenen case LOOP_SET_FD: {
15353448914eSMartijn Coenen /*
15363448914eSMartijn Coenen * Legacy case - pass in a zeroed out struct loop_config with
15373448914eSMartijn Coenen * only the file descriptor set , which corresponds with the
15383448914eSMartijn Coenen * default parameters we'd have used otherwise.
15393448914eSMartijn Coenen */
15403448914eSMartijn Coenen struct loop_config config;
15413448914eSMartijn Coenen
15423448914eSMartijn Coenen memset(&config, 0, sizeof(config));
15433448914eSMartijn Coenen config.fd = arg;
15443448914eSMartijn Coenen
15453448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config);
15463448914eSMartijn Coenen }
15473448914eSMartijn Coenen case LOOP_CONFIGURE: {
15483448914eSMartijn Coenen struct loop_config config;
15493448914eSMartijn Coenen
15503448914eSMartijn Coenen if (copy_from_user(&config, argp, sizeof(config)))
15513448914eSMartijn Coenen return -EFAULT;
15523448914eSMartijn Coenen
15533448914eSMartijn Coenen return loop_configure(lo, mode, bdev, &config);
15543448914eSMartijn Coenen }
1555a1316544SJan Kara case LOOP_CHANGE_FD:
1556c3710770SJan Kara return loop_change_fd(lo, bdev, arg);
1557a1316544SJan Kara case LOOP_CLR_FD:
15587ccd0791SJan Kara return loop_clr_fd(lo);
1559a1316544SJan Kara case LOOP_SET_STATUS:
1560a1316544SJan Kara err = -EPERM;
156105bdb996SChristoph Hellwig if ((mode & BLK_OPEN_WRITE) || capable(CAP_SYS_ADMIN))
1562571fae6eSMartijn Coenen err = loop_set_status_old(lo, argp);
1563a1316544SJan Kara break;
1564a1316544SJan Kara case LOOP_GET_STATUS:
1565571fae6eSMartijn Coenen return loop_get_status_old(lo, argp);
1566a1316544SJan Kara case LOOP_SET_STATUS64:
1567a1316544SJan Kara err = -EPERM;
156805bdb996SChristoph Hellwig if ((mode & BLK_OPEN_WRITE) || capable(CAP_SYS_ADMIN))
1569571fae6eSMartijn Coenen err = loop_set_status64(lo, argp);
1570a1316544SJan Kara break;
1571a1316544SJan Kara case LOOP_GET_STATUS64:
1572571fae6eSMartijn Coenen return loop_get_status64(lo, argp);
1573a1316544SJan Kara case LOOP_SET_CAPACITY:
1574a1316544SJan Kara case LOOP_SET_DIRECT_IO:
1575a1316544SJan Kara case LOOP_SET_BLOCK_SIZE:
157605bdb996SChristoph Hellwig if (!(mode & BLK_OPEN_WRITE) && !capable(CAP_SYS_ADMIN))
1577a1316544SJan Kara return -EPERM;
1578df561f66SGustavo A. R. Silva fallthrough;
1579a1316544SJan Kara default:
1580a1316544SJan Kara err = lo_simple_ioctl(lo, cmd, arg);
1581a1316544SJan Kara break;
1582a1316544SJan Kara }
1583a1316544SJan Kara
15841da177e4SLinus Torvalds return err;
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds
1587863d5b82SDavid Howells #ifdef CONFIG_COMPAT
1588863d5b82SDavid Howells struct compat_loop_info {
1589863d5b82SDavid Howells compat_int_t lo_number; /* ioctl r/o */
1590863d5b82SDavid Howells compat_dev_t lo_device; /* ioctl r/o */
1591863d5b82SDavid Howells compat_ulong_t lo_inode; /* ioctl r/o */
1592863d5b82SDavid Howells compat_dev_t lo_rdevice; /* ioctl r/o */
1593863d5b82SDavid Howells compat_int_t lo_offset;
1594f941c51eSCarlos Llamas compat_int_t lo_encrypt_type; /* obsolete, ignored */
1595863d5b82SDavid Howells compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1596863d5b82SDavid Howells compat_int_t lo_flags; /* ioctl r/o */
1597863d5b82SDavid Howells char lo_name[LO_NAME_SIZE];
1598863d5b82SDavid Howells unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1599863d5b82SDavid Howells compat_ulong_t lo_init[2];
1600863d5b82SDavid Howells char reserved[4];
1601863d5b82SDavid Howells };
1602863d5b82SDavid Howells
1603863d5b82SDavid Howells /*
1604863d5b82SDavid Howells * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1605863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver
1606863d5b82SDavid Howells */
1607863d5b82SDavid Howells static noinline int
loop_info64_from_compat(const struct compat_loop_info __user * arg,struct loop_info64 * info64)1608ba674cfcSAl Viro loop_info64_from_compat(const struct compat_loop_info __user *arg,
1609863d5b82SDavid Howells struct loop_info64 *info64)
1610863d5b82SDavid Howells {
1611863d5b82SDavid Howells struct compat_loop_info info;
1612863d5b82SDavid Howells
1613863d5b82SDavid Howells if (copy_from_user(&info, arg, sizeof(info)))
1614863d5b82SDavid Howells return -EFAULT;
1615863d5b82SDavid Howells
1616863d5b82SDavid Howells memset(info64, 0, sizeof(*info64));
1617863d5b82SDavid Howells info64->lo_number = info.lo_number;
1618863d5b82SDavid Howells info64->lo_device = info.lo_device;
1619863d5b82SDavid Howells info64->lo_inode = info.lo_inode;
1620863d5b82SDavid Howells info64->lo_rdevice = info.lo_rdevice;
1621863d5b82SDavid Howells info64->lo_offset = info.lo_offset;
1622863d5b82SDavid Howells info64->lo_sizelimit = 0;
1623863d5b82SDavid Howells info64->lo_flags = info.lo_flags;
1624863d5b82SDavid Howells memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1625863d5b82SDavid Howells return 0;
1626863d5b82SDavid Howells }
1627863d5b82SDavid Howells
1628863d5b82SDavid Howells /*
1629863d5b82SDavid Howells * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1630863d5b82SDavid Howells * - noinlined to reduce stack space usage in main part of driver
1631863d5b82SDavid Howells */
1632863d5b82SDavid Howells static noinline int
loop_info64_to_compat(const struct loop_info64 * info64,struct compat_loop_info __user * arg)1633863d5b82SDavid Howells loop_info64_to_compat(const struct loop_info64 *info64,
1634863d5b82SDavid Howells struct compat_loop_info __user *arg)
1635863d5b82SDavid Howells {
1636863d5b82SDavid Howells struct compat_loop_info info;
1637863d5b82SDavid Howells
1638863d5b82SDavid Howells memset(&info, 0, sizeof(info));
1639863d5b82SDavid Howells info.lo_number = info64->lo_number;
1640863d5b82SDavid Howells info.lo_device = info64->lo_device;
1641863d5b82SDavid Howells info.lo_inode = info64->lo_inode;
1642863d5b82SDavid Howells info.lo_rdevice = info64->lo_rdevice;
1643863d5b82SDavid Howells info.lo_offset = info64->lo_offset;
1644863d5b82SDavid Howells info.lo_flags = info64->lo_flags;
1645863d5b82SDavid Howells memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1646863d5b82SDavid Howells
1647863d5b82SDavid Howells /* error in case values were truncated */
1648863d5b82SDavid Howells if (info.lo_device != info64->lo_device ||
1649863d5b82SDavid Howells info.lo_rdevice != info64->lo_rdevice ||
1650863d5b82SDavid Howells info.lo_inode != info64->lo_inode ||
165147e96246SChristoph Hellwig info.lo_offset != info64->lo_offset)
1652863d5b82SDavid Howells return -EOVERFLOW;
1653863d5b82SDavid Howells
1654863d5b82SDavid Howells if (copy_to_user(arg, &info, sizeof(info)))
1655863d5b82SDavid Howells return -EFAULT;
1656863d5b82SDavid Howells return 0;
1657863d5b82SDavid Howells }
1658863d5b82SDavid Howells
1659863d5b82SDavid Howells static int
loop_set_status_compat(struct loop_device * lo,const struct compat_loop_info __user * arg)1660863d5b82SDavid Howells loop_set_status_compat(struct loop_device *lo,
1661863d5b82SDavid Howells const struct compat_loop_info __user *arg)
1662863d5b82SDavid Howells {
1663863d5b82SDavid Howells struct loop_info64 info64;
1664863d5b82SDavid Howells int ret;
1665863d5b82SDavid Howells
1666863d5b82SDavid Howells ret = loop_info64_from_compat(arg, &info64);
1667863d5b82SDavid Howells if (ret < 0)
1668863d5b82SDavid Howells return ret;
1669863d5b82SDavid Howells return loop_set_status(lo, &info64);
1670863d5b82SDavid Howells }
1671863d5b82SDavid Howells
1672863d5b82SDavid Howells static int
loop_get_status_compat(struct loop_device * lo,struct compat_loop_info __user * arg)1673863d5b82SDavid Howells loop_get_status_compat(struct loop_device *lo,
1674863d5b82SDavid Howells struct compat_loop_info __user *arg)
1675863d5b82SDavid Howells {
1676863d5b82SDavid Howells struct loop_info64 info64;
1677bdac616dSOmar Sandoval int err;
1678863d5b82SDavid Howells
16794a5ce9baSJan Kara if (!arg)
1680bdac616dSOmar Sandoval return -EINVAL;
1681863d5b82SDavid Howells err = loop_get_status(lo, &info64);
1682863d5b82SDavid Howells if (!err)
1683863d5b82SDavid Howells err = loop_info64_to_compat(&info64, arg);
1684863d5b82SDavid Howells return err;
1685863d5b82SDavid Howells }
1686863d5b82SDavid Howells
lo_compat_ioctl(struct block_device * bdev,blk_mode_t mode,unsigned int cmd,unsigned long arg)168705bdb996SChristoph Hellwig static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,
1688bb214884SAl Viro unsigned int cmd, unsigned long arg)
1689863d5b82SDavid Howells {
1690bb214884SAl Viro struct loop_device *lo = bdev->bd_disk->private_data;
1691863d5b82SDavid Howells int err;
1692863d5b82SDavid Howells
1693863d5b82SDavid Howells switch(cmd) {
1694863d5b82SDavid Howells case LOOP_SET_STATUS:
16953148ffbdSOmar Sandoval err = loop_set_status_compat(lo,
16963148ffbdSOmar Sandoval (const struct compat_loop_info __user *)arg);
1697863d5b82SDavid Howells break;
1698863d5b82SDavid Howells case LOOP_GET_STATUS:
16993148ffbdSOmar Sandoval err = loop_get_status_compat(lo,
17003148ffbdSOmar Sandoval (struct compat_loop_info __user *)arg);
1701863d5b82SDavid Howells break;
170253d66608SJ. R. Okajima case LOOP_SET_CAPACITY:
1703863d5b82SDavid Howells case LOOP_CLR_FD:
1704863d5b82SDavid Howells case LOOP_GET_STATUS64:
1705863d5b82SDavid Howells case LOOP_SET_STATUS64:
17063448914eSMartijn Coenen case LOOP_CONFIGURE:
1707863d5b82SDavid Howells arg = (unsigned long) compat_ptr(arg);
1708df561f66SGustavo A. R. Silva fallthrough;
1709863d5b82SDavid Howells case LOOP_SET_FD:
1710863d5b82SDavid Howells case LOOP_CHANGE_FD:
17119fea4b39SEvan Green case LOOP_SET_BLOCK_SIZE:
1712fdbe4eeeSAlessio Balsini case LOOP_SET_DIRECT_IO:
1713bb214884SAl Viro err = lo_ioctl(bdev, mode, cmd, arg);
1714863d5b82SDavid Howells break;
1715863d5b82SDavid Howells default:
1716863d5b82SDavid Howells err = -ENOIOCTLCMD;
1717863d5b82SDavid Howells break;
1718863d5b82SDavid Howells }
1719863d5b82SDavid Howells return err;
1720863d5b82SDavid Howells }
1721863d5b82SDavid Howells #endif
1722863d5b82SDavid Howells
lo_release(struct gendisk * disk)1723ae220766SChristoph Hellwig static void lo_release(struct gendisk *disk)
17241da177e4SLinus Torvalds {
17256cc8e743SPavel Tatashin struct loop_device *lo = disk->private_data;
17261da177e4SLinus Torvalds
1727a0e286b6SChristoph Hellwig if (disk_openers(disk) > 0)
1728a0e286b6SChristoph Hellwig return;
1729f8933667SMing Lei
1730a0e286b6SChristoph Hellwig mutex_lock(&lo->lo_mutex);
1731a0e286b6SChristoph Hellwig if (lo->lo_state == Lo_bound && (lo->lo_flags & LO_FLAGS_AUTOCLEAR)) {
1732a2505b79SJan Kara lo->lo_state = Lo_rundown;
17336cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex);
173414f27939SMilan Broz /*
173514f27939SMilan Broz * In autoclear mode, stop the loop thread
173614f27939SMilan Broz * and remove configuration after last close.
173714f27939SMilan Broz */
1738bf23747eSTetsuo Handa __loop_clr_fd(lo, true);
17390a42e99bSJan Kara return;
174014f27939SMilan Broz }
17416cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex);
1742bb214884SAl Viro }
174396c58655SDavid Woodhouse
lo_free_disk(struct gendisk * disk)1744d2c7f56fSChristoph Hellwig static void lo_free_disk(struct gendisk *disk)
1745d2c7f56fSChristoph Hellwig {
1746d2c7f56fSChristoph Hellwig struct loop_device *lo = disk->private_data;
1747d2c7f56fSChristoph Hellwig
1748d292dc80SChristoph Hellwig if (lo->workqueue)
1749d292dc80SChristoph Hellwig destroy_workqueue(lo->workqueue);
1750d292dc80SChristoph Hellwig loop_free_idle_workers(lo, true);
1751292a089dSSteven Rostedt (Google) timer_shutdown_sync(&lo->timer);
1752d2c7f56fSChristoph Hellwig mutex_destroy(&lo->lo_mutex);
1753d2c7f56fSChristoph Hellwig kfree(lo);
1754ae665016SLinus Torvalds }
1755ae665016SLinus Torvalds
175683d5cde4SAlexey Dobriyan static const struct block_device_operations lo_fops = {
17571da177e4SLinus Torvalds .owner = THIS_MODULE,
1758bb214884SAl Viro .release = lo_release,
1759bb214884SAl Viro .ioctl = lo_ioctl,
1760863d5b82SDavid Howells #ifdef CONFIG_COMPAT
1761bb214884SAl Viro .compat_ioctl = lo_compat_ioctl,
1762863d5b82SDavid Howells #endif
1763d2c7f56fSChristoph Hellwig .free_disk = lo_free_disk,
17641da177e4SLinus Torvalds };
17651da177e4SLinus Torvalds
17661da177e4SLinus Torvalds /*
17671da177e4SLinus Torvalds * And now the modules code and kernel interface.
17681da177e4SLinus Torvalds */
176985c50197SIsaac J. Manjarres
177085c50197SIsaac J. Manjarres /*
177185c50197SIsaac J. Manjarres * If max_loop is specified, create that many devices upfront.
177285c50197SIsaac J. Manjarres * This also becomes a hard limit. If max_loop is not specified,
1773bb5faa99SMauricio Faria de Oliveira * the default isn't a hard limit (as before commit 85c50197716c
1774bb5faa99SMauricio Faria de Oliveira * changed the default value from 0 for max_loop=0 reasons), just
177585c50197SIsaac J. Manjarres * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
177685c50197SIsaac J. Manjarres * init time. Loop devices can be requested on-demand with the
177785c50197SIsaac J. Manjarres * /dev/loop-control interface, or be instantiated by accessing
177885c50197SIsaac J. Manjarres * a 'dead' device node.
177985c50197SIsaac J. Manjarres */
178085c50197SIsaac J. Manjarres static int max_loop = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
1781bb5faa99SMauricio Faria de Oliveira
1782bb5faa99SMauricio Faria de Oliveira #ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD
1783bb5faa99SMauricio Faria de Oliveira static bool max_loop_specified;
1784bb5faa99SMauricio Faria de Oliveira
max_loop_param_set_int(const char * val,const struct kernel_param * kp)1785bb5faa99SMauricio Faria de Oliveira static int max_loop_param_set_int(const char *val,
1786bb5faa99SMauricio Faria de Oliveira const struct kernel_param *kp)
1787bb5faa99SMauricio Faria de Oliveira {
1788bb5faa99SMauricio Faria de Oliveira int ret;
1789bb5faa99SMauricio Faria de Oliveira
1790bb5faa99SMauricio Faria de Oliveira ret = param_set_int(val, kp);
1791bb5faa99SMauricio Faria de Oliveira if (ret < 0)
1792bb5faa99SMauricio Faria de Oliveira return ret;
1793bb5faa99SMauricio Faria de Oliveira
1794bb5faa99SMauricio Faria de Oliveira max_loop_specified = true;
1795bb5faa99SMauricio Faria de Oliveira return 0;
1796bb5faa99SMauricio Faria de Oliveira }
1797bb5faa99SMauricio Faria de Oliveira
1798bb5faa99SMauricio Faria de Oliveira static const struct kernel_param_ops max_loop_param_ops = {
1799bb5faa99SMauricio Faria de Oliveira .set = max_loop_param_set_int,
1800bb5faa99SMauricio Faria de Oliveira .get = param_get_int,
1801bb5faa99SMauricio Faria de Oliveira };
1802bb5faa99SMauricio Faria de Oliveira
1803bb5faa99SMauricio Faria de Oliveira module_param_cb(max_loop, &max_loop_param_ops, &max_loop, 0444);
1804a47653fcSKen Chen MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
1805bb5faa99SMauricio Faria de Oliveira #else
1806bb5faa99SMauricio Faria de Oliveira module_param(max_loop, int, 0444);
1807bb5faa99SMauricio Faria de Oliveira MODULE_PARM_DESC(max_loop, "Initial number of loop devices");
1808bb5faa99SMauricio Faria de Oliveira #endif
1809bb5faa99SMauricio Faria de Oliveira
18105657a819SJoe Perches module_param(max_part, int, 0444);
1811476a4813SLaurent Vivier MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
1812ef44c508SChaitanya Kulkarni
1813ef44c508SChaitanya Kulkarni static int hw_queue_depth = LOOP_DEFAULT_HW_Q_DEPTH;
1814ef44c508SChaitanya Kulkarni
loop_set_hw_queue_depth(const char * s,const struct kernel_param * p)1815ef44c508SChaitanya Kulkarni static int loop_set_hw_queue_depth(const char *s, const struct kernel_param *p)
1816ef44c508SChaitanya Kulkarni {
1817e152a05fSBart Van Assche int qd, ret;
1818ef44c508SChaitanya Kulkarni
1819e152a05fSBart Van Assche ret = kstrtoint(s, 0, &qd);
1820e152a05fSBart Van Assche if (ret < 0)
1821e152a05fSBart Van Assche return ret;
1822e152a05fSBart Van Assche if (qd < 1)
1823e152a05fSBart Van Assche return -EINVAL;
1824e152a05fSBart Van Assche hw_queue_depth = qd;
1825e152a05fSBart Van Assche return 0;
1826ef44c508SChaitanya Kulkarni }
1827ef44c508SChaitanya Kulkarni
1828ef44c508SChaitanya Kulkarni static const struct kernel_param_ops loop_hw_qdepth_param_ops = {
1829ef44c508SChaitanya Kulkarni .set = loop_set_hw_queue_depth,
1830ef44c508SChaitanya Kulkarni .get = param_get_int,
1831ef44c508SChaitanya Kulkarni };
1832ef44c508SChaitanya Kulkarni
1833ef44c508SChaitanya Kulkarni device_param_cb(hw_queue_depth, &loop_hw_qdepth_param_ops, &hw_queue_depth, 0444);
1834e152a05fSBart Van Assche MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: " __stringify(LOOP_DEFAULT_HW_Q_DEPTH));
1835ef44c508SChaitanya Kulkarni
18361da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18371da177e4SLinus Torvalds MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
18381da177e4SLinus Torvalds
loop_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)1839fc17b653SChristoph Hellwig static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
1840b5dd2f60SMing Lei const struct blk_mq_queue_data *bd)
1841b5dd2f60SMing Lei {
18421894e916SJens Axboe struct request *rq = bd->rq;
18431894e916SJens Axboe struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
18441894e916SJens Axboe struct loop_device *lo = rq->q->queuedata;
1845b5dd2f60SMing Lei
18461894e916SJens Axboe blk_mq_start_request(rq);
1847b5dd2f60SMing Lei
1848f4aa4c7bSMing Lei if (lo->lo_state != Lo_bound)
1849fc17b653SChristoph Hellwig return BLK_STS_IOERR;
1850f4aa4c7bSMing Lei
18511894e916SJens Axboe switch (req_op(rq)) {
1852f0225cacSChristoph Hellwig case REQ_OP_FLUSH:
1853f0225cacSChristoph Hellwig case REQ_OP_DISCARD:
185419372e27SChristoph Hellwig case REQ_OP_WRITE_ZEROES:
1855bc07c10aSMing Lei cmd->use_aio = false;
1856f0225cacSChristoph Hellwig break;
1857f0225cacSChristoph Hellwig default:
1858f0225cacSChristoph Hellwig cmd->use_aio = lo->use_dio;
1859f0225cacSChristoph Hellwig break;
1860f0225cacSChristoph Hellwig }
1861bc07c10aSMing Lei
1862d4478e92SShaohua Li /* always use the first bio's css */
1863c74d40e8SDan Schatzberg cmd->blkcg_css = NULL;
1864c74d40e8SDan Schatzberg cmd->memcg_css = NULL;
18650b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP
1866bbb1ebe7SChristoph Hellwig if (rq->bio) {
1867bbb1ebe7SChristoph Hellwig cmd->blkcg_css = bio_blkcg_css(rq->bio);
1868c74d40e8SDan Schatzberg #ifdef CONFIG_MEMCG
1869bbb1ebe7SChristoph Hellwig if (cmd->blkcg_css) {
1870c74d40e8SDan Schatzberg cmd->memcg_css =
1871c74d40e8SDan Schatzberg cgroup_get_e_css(cmd->blkcg_css->cgroup,
1872c74d40e8SDan Schatzberg &memory_cgrp_subsys);
1873bbb1ebe7SChristoph Hellwig }
1874d4478e92SShaohua Li #endif
1875c74d40e8SDan Schatzberg }
1876c74d40e8SDan Schatzberg #endif
187787579e9bSDan Schatzberg loop_queue_work(lo, cmd);
1878b5dd2f60SMing Lei
1879fc17b653SChristoph Hellwig return BLK_STS_OK;
1880b5dd2f60SMing Lei }
1881b5dd2f60SMing Lei
loop_handle_cmd(struct loop_cmd * cmd)1882b5dd2f60SMing Lei static void loop_handle_cmd(struct loop_cmd *cmd)
1883b5dd2f60SMing Lei {
18849b0cb770SBart Van Assche struct cgroup_subsys_state *cmd_blkcg_css = cmd->blkcg_css;
18859b0cb770SBart Van Assche struct cgroup_subsys_state *cmd_memcg_css = cmd->memcg_css;
18861894e916SJens Axboe struct request *rq = blk_mq_rq_from_pdu(cmd);
18871894e916SJens Axboe const bool write = op_is_write(req_op(rq));
18881894e916SJens Axboe struct loop_device *lo = rq->q->queuedata;
1889f4829a9bSChristoph Hellwig int ret = 0;
1890c74d40e8SDan Schatzberg struct mem_cgroup *old_memcg = NULL;
18919b0cb770SBart Van Assche const bool use_aio = cmd->use_aio;
1892b5dd2f60SMing Lei
1893f4829a9bSChristoph Hellwig if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
1894f4829a9bSChristoph Hellwig ret = -EIO;
1895b5dd2f60SMing Lei goto failed;
1896f4829a9bSChristoph Hellwig }
1897b5dd2f60SMing Lei
18989b0cb770SBart Van Assche if (cmd_blkcg_css)
18999b0cb770SBart Van Assche kthread_associate_blkcg(cmd_blkcg_css);
19009b0cb770SBart Van Assche if (cmd_memcg_css)
1901c74d40e8SDan Schatzberg old_memcg = set_active_memcg(
19029b0cb770SBart Van Assche mem_cgroup_from_css(cmd_memcg_css));
1903c74d40e8SDan Schatzberg
19049b0cb770SBart Van Assche /*
19059b0cb770SBart Van Assche * do_req_filebacked() may call blk_mq_complete_request() synchronously
19069b0cb770SBart Van Assche * or asynchronously if using aio. Hence, do not touch 'cmd' after
19079b0cb770SBart Van Assche * do_req_filebacked() has returned unless we are sure that 'cmd' has
19089b0cb770SBart Van Assche * not yet been completed.
19099b0cb770SBart Van Assche */
19101894e916SJens Axboe ret = do_req_filebacked(lo, rq);
1911c74d40e8SDan Schatzberg
19129b0cb770SBart Van Assche if (cmd_blkcg_css)
1913c74d40e8SDan Schatzberg kthread_associate_blkcg(NULL);
1914c74d40e8SDan Schatzberg
19159b0cb770SBart Van Assche if (cmd_memcg_css) {
1916c74d40e8SDan Schatzberg set_active_memcg(old_memcg);
19179b0cb770SBart Van Assche css_put(cmd_memcg_css);
1918c74d40e8SDan Schatzberg }
1919b5dd2f60SMing Lei failed:
1920bc07c10aSMing Lei /* complete non-aio request */
19219b0cb770SBart Van Assche if (!use_aio || ret) {
19228cd55087SEvan Green if (ret == -EOPNOTSUPP)
19238cd55087SEvan Green cmd->ret = ret;
19248cd55087SEvan Green else
1925fe2cb290SChristoph Hellwig cmd->ret = ret ? -EIO : 0;
192615f73f5bSChristoph Hellwig if (likely(!blk_should_fake_timeout(rq->q)))
19271894e916SJens Axboe blk_mq_complete_request(rq);
1928fe2cb290SChristoph Hellwig }
1929b5dd2f60SMing Lei }
1930b5dd2f60SMing Lei
loop_process_work(struct loop_worker * worker,struct list_head * cmd_list,struct loop_device * lo)193187579e9bSDan Schatzberg static void loop_process_work(struct loop_worker *worker,
193287579e9bSDan Schatzberg struct list_head *cmd_list, struct loop_device *lo)
1933b5dd2f60SMing Lei {
193487579e9bSDan Schatzberg int orig_flags = current->flags;
193587579e9bSDan Schatzberg struct loop_cmd *cmd;
1936b5dd2f60SMing Lei
193787579e9bSDan Schatzberg current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
193887579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock);
193987579e9bSDan Schatzberg while (!list_empty(cmd_list)) {
194087579e9bSDan Schatzberg cmd = container_of(
194187579e9bSDan Schatzberg cmd_list->next, struct loop_cmd, list_entry);
194287579e9bSDan Schatzberg list_del(cmd_list->next);
194387579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock);
194487579e9bSDan Schatzberg
194587579e9bSDan Schatzberg loop_handle_cmd(cmd);
194687579e9bSDan Schatzberg cond_resched();
194787579e9bSDan Schatzberg
194887579e9bSDan Schatzberg spin_lock_irq(&lo->lo_work_lock);
194987579e9bSDan Schatzberg }
195087579e9bSDan Schatzberg
195187579e9bSDan Schatzberg /*
195287579e9bSDan Schatzberg * We only add to the idle list if there are no pending cmds
195387579e9bSDan Schatzberg * *and* the worker will not run again which ensures that it
195487579e9bSDan Schatzberg * is safe to free any worker on the idle list
195587579e9bSDan Schatzberg */
195687579e9bSDan Schatzberg if (worker && !work_pending(&worker->work)) {
195787579e9bSDan Schatzberg worker->last_ran_at = jiffies;
195887579e9bSDan Schatzberg list_add_tail(&worker->idle_list, &lo->idle_worker_list);
195987579e9bSDan Schatzberg loop_set_timer(lo);
196087579e9bSDan Schatzberg }
196187579e9bSDan Schatzberg spin_unlock_irq(&lo->lo_work_lock);
196287579e9bSDan Schatzberg current->flags = orig_flags;
196387579e9bSDan Schatzberg }
196487579e9bSDan Schatzberg
loop_workfn(struct work_struct * work)196587579e9bSDan Schatzberg static void loop_workfn(struct work_struct *work)
196687579e9bSDan Schatzberg {
196787579e9bSDan Schatzberg struct loop_worker *worker =
196887579e9bSDan Schatzberg container_of(work, struct loop_worker, work);
196987579e9bSDan Schatzberg loop_process_work(worker, &worker->cmd_list, worker->lo);
197087579e9bSDan Schatzberg }
197187579e9bSDan Schatzberg
loop_rootcg_workfn(struct work_struct * work)197287579e9bSDan Schatzberg static void loop_rootcg_workfn(struct work_struct *work)
197387579e9bSDan Schatzberg {
197487579e9bSDan Schatzberg struct loop_device *lo =
197587579e9bSDan Schatzberg container_of(work, struct loop_device, rootcg_work);
197687579e9bSDan Schatzberg loop_process_work(NULL, &lo->rootcg_cmd_list, lo);
197787579e9bSDan Schatzberg }
197887579e9bSDan Schatzberg
1979f363b089SEric Biggers static const struct blk_mq_ops loop_mq_ops = {
1980b5dd2f60SMing Lei .queue_rq = loop_queue_rq,
1981fe2cb290SChristoph Hellwig .complete = lo_complete_rq,
1982b5dd2f60SMing Lei };
1983b5dd2f60SMing Lei
loop_add(int i)1984d6da83d0SChristoph Hellwig static int loop_add(int i)
19851da177e4SLinus Torvalds {
198673285082SKen Chen struct loop_device *lo;
198773285082SKen Chen struct gendisk *disk;
198834dd82afSKay Sievers int err;
19891da177e4SLinus Torvalds
199034dd82afSKay Sievers err = -ENOMEM;
199168d740d7SSilva Paulo lo = kzalloc(sizeof(*lo), GFP_KERNEL);
199268d740d7SSilva Paulo if (!lo)
199373285082SKen Chen goto out;
1994b15ed546SChristoph Hellwig lo->worker_tree = RB_ROOT;
1995b15ed546SChristoph Hellwig INIT_LIST_HEAD(&lo->idle_worker_list);
1996b15ed546SChristoph Hellwig timer_setup(&lo->timer, loop_free_idle_workers_timer, TIMER_DEFERRABLE);
1997ef7e7c82SMikulas Patocka lo->lo_state = Lo_unbound;
1998ef7e7c82SMikulas Patocka
199918d1f200SChristoph Hellwig err = mutex_lock_killable(&loop_ctl_mutex);
200018d1f200SChristoph Hellwig if (err)
200118d1f200SChristoph Hellwig goto out_free_dev;
200218d1f200SChristoph Hellwig
2003c718aa65STejun Heo /* allocate id, if @id >= 0, we're requesting that specific id */
200434dd82afSKay Sievers if (i >= 0) {
2005c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
2006c718aa65STejun Heo if (err == -ENOSPC)
200734dd82afSKay Sievers err = -EEXIST;
200834dd82afSKay Sievers } else {
2009c718aa65STejun Heo err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
201034dd82afSKay Sievers }
20111c500ad7STetsuo Handa mutex_unlock(&loop_ctl_mutex);
201234dd82afSKay Sievers if (err < 0)
20131c500ad7STetsuo Handa goto out_free_dev;
2014c718aa65STejun Heo i = err;
20151da177e4SLinus Torvalds
2016b5dd2f60SMing Lei lo->tag_set.ops = &loop_mq_ops;
2017b5dd2f60SMing Lei lo->tag_set.nr_hw_queues = 1;
2018ef44c508SChaitanya Kulkarni lo->tag_set.queue_depth = hw_queue_depth;
2019b5dd2f60SMing Lei lo->tag_set.numa_node = NUMA_NO_NODE;
2020b5dd2f60SMing Lei lo->tag_set.cmd_size = sizeof(struct loop_cmd);
20212112f5c1SBart Van Assche lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING |
20222112f5c1SBart Van Assche BLK_MQ_F_NO_SCHED_BY_DEFAULT;
2023b5dd2f60SMing Lei lo->tag_set.driver_data = lo;
2024b5dd2f60SMing Lei
2025b5dd2f60SMing Lei err = blk_mq_alloc_tag_set(&lo->tag_set);
2026b5dd2f60SMing Lei if (err)
20273ec981e3SMikulas Patocka goto out_free_idr;
202873285082SKen Chen
20291c99502fSChristoph Hellwig disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo);
20301c99502fSChristoph Hellwig if (IS_ERR(disk)) {
20311c99502fSChristoph Hellwig err = PTR_ERR(disk);
2032b5dd2f60SMing Lei goto out_cleanup_tags;
2033b5dd2f60SMing Lei }
20341c99502fSChristoph Hellwig lo->lo_queue = lo->lo_disk->queue;
2035ef7e7c82SMikulas Patocka
203654bb0adeSShaohua Li blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
203740326d8aSShaohua Li
20385b5e20f4SMing Lei /*
2039e03c8dd1SKay Sievers * Disable partition scanning by default. The in-kernel partition
2040e03c8dd1SKay Sievers * scanning can be requested individually per-device during its
2041e03c8dd1SKay Sievers * setup. Userspace can always add and remove partitions from all
2042e03c8dd1SKay Sievers * devices. The needed partition minors are allocated from the
2043e03c8dd1SKay Sievers * extended minor space, the main loop device numbers will continue
2044e03c8dd1SKay Sievers * to match the loop minors, regardless of the number of partitions
2045e03c8dd1SKay Sievers * used.
2046e03c8dd1SKay Sievers *
2047e03c8dd1SKay Sievers * If max_part is given, partition scanning is globally enabled for
2048e03c8dd1SKay Sievers * all loop devices. The minors for the main loop devices will be
2049e03c8dd1SKay Sievers * multiples of max_part.
2050e03c8dd1SKay Sievers *
2051e03c8dd1SKay Sievers * Note: Global-for-all-devices, set-only-at-init, read-only module
2052e03c8dd1SKay Sievers * parameteters like 'max_loop' and 'max_part' make things needlessly
2053e03c8dd1SKay Sievers * complicated, are too static, inflexible and may surprise
2054e03c8dd1SKay Sievers * userspace tools. Parameters like this in general should be avoided.
2055e03c8dd1SKay Sievers */
2056e03c8dd1SKay Sievers if (!part_shift)
2057b9684a71SChristoph Hellwig set_bit(GD_SUPPRESS_PART_SCAN, &disk->state);
20586cc8e743SPavel Tatashin mutex_init(&lo->lo_mutex);
20591da177e4SLinus Torvalds lo->lo_number = i;
20601da177e4SLinus Torvalds spin_lock_init(&lo->lo_lock);
206187579e9bSDan Schatzberg spin_lock_init(&lo->lo_work_lock);
2062d292dc80SChristoph Hellwig INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn);
2063d292dc80SChristoph Hellwig INIT_LIST_HEAD(&lo->rootcg_cmd_list);
20641da177e4SLinus Torvalds disk->major = LOOP_MAJOR;
2065476a4813SLaurent Vivier disk->first_minor = i << part_shift;
20661c99502fSChristoph Hellwig disk->minors = 1 << part_shift;
20671da177e4SLinus Torvalds disk->fops = &lo_fops;
20681da177e4SLinus Torvalds disk->private_data = lo;
20691da177e4SLinus Torvalds disk->queue = lo->lo_queue;
20709f65c489SMatteo Croce disk->events = DISK_EVENT_MEDIA_CHANGE;
20719f65c489SMatteo Croce disk->event_flags = DISK_EVENT_FLAG_UEVENT;
207273285082SKen Chen sprintf(disk->disk_name, "loop%d", i);
20731c500ad7STetsuo Handa /* Make this loop device reachable from pathname. */
2074905705f0SLuis Chamberlain err = add_disk(disk);
2075905705f0SLuis Chamberlain if (err)
2076905705f0SLuis Chamberlain goto out_cleanup_disk;
2077905705f0SLuis Chamberlain
20781c500ad7STetsuo Handa /* Show this loop device. */
20791c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex);
20801c500ad7STetsuo Handa lo->idr_visible = true;
208118d1f200SChristoph Hellwig mutex_unlock(&loop_ctl_mutex);
2082905705f0SLuis Chamberlain
208318d1f200SChristoph Hellwig return i;
208473285082SKen Chen
2085905705f0SLuis Chamberlain out_cleanup_disk:
20868b9ab626SChristoph Hellwig put_disk(disk);
2087b5dd2f60SMing Lei out_cleanup_tags:
2088b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set);
20893ec981e3SMikulas Patocka out_free_idr:
20901c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex);
20913ec981e3SMikulas Patocka idr_remove(&loop_index_idr, i);
209218d1f200SChristoph Hellwig mutex_unlock(&loop_ctl_mutex);
209373285082SKen Chen out_free_dev:
209473285082SKen Chen kfree(lo);
209573285082SKen Chen out:
209634dd82afSKay Sievers return err;
20971da177e4SLinus Torvalds }
20981da177e4SLinus Torvalds
loop_remove(struct loop_device * lo)209934dd82afSKay Sievers static void loop_remove(struct loop_device *lo)
210073285082SKen Chen {
21011c500ad7STetsuo Handa /* Make this loop device unreachable from pathname. */
21026cd18e71SNeilBrown del_gendisk(lo->lo_disk);
2103b5dd2f60SMing Lei blk_mq_free_tag_set(&lo->tag_set);
2104ce8d7861SChristoph Hellwig
21051c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex);
21061c500ad7STetsuo Handa idr_remove(&loop_index_idr, lo->lo_number);
21071c500ad7STetsuo Handa mutex_unlock(&loop_ctl_mutex);
2108d2c7f56fSChristoph Hellwig
2109d2c7f56fSChristoph Hellwig put_disk(lo->lo_disk);
211073285082SKen Chen }
211173285082SKen Chen
211223881aecSMauricio Faria de Oliveira #ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD
loop_probe(dev_t dev)21138410d38cSChristoph Hellwig static void loop_probe(dev_t dev)
211473285082SKen Chen {
21158410d38cSChristoph Hellwig int idx = MINOR(dev) >> part_shift;
21168410d38cSChristoph Hellwig
2117bb5faa99SMauricio Faria de Oliveira if (max_loop_specified && max_loop && idx >= max_loop)
21188410d38cSChristoph Hellwig return;
2119d6da83d0SChristoph Hellwig loop_add(idx);
2120f9d10764SChristoph Hellwig }
212123881aecSMauricio Faria de Oliveira #else
212223881aecSMauricio Faria de Oliveira #define loop_probe NULL
212323881aecSMauricio Faria de Oliveira #endif /* !CONFIG_BLOCK_LEGACY_AUTOLOAD */
2124f9d10764SChristoph Hellwig
loop_control_remove(int idx)2125f9d10764SChristoph Hellwig static int loop_control_remove(int idx)
2126770fe30aSKay Sievers {
2127770fe30aSKay Sievers struct loop_device *lo;
21280a42e99bSJan Kara int ret;
2129e5d66a10SChristoph Hellwig
2130e5d66a10SChristoph Hellwig if (idx < 0) {
2131e3f9387aSTetsuo Handa pr_warn_once("deleting an unspecified loop device is not supported.\n");
2132e5d66a10SChristoph Hellwig return -EINVAL;
2133e5d66a10SChristoph Hellwig }
2134770fe30aSKay Sievers
21351c500ad7STetsuo Handa /* Hide this loop device for serialization. */
21360a42e99bSJan Kara ret = mutex_lock_killable(&loop_ctl_mutex);
21370a42e99bSJan Kara if (ret)
21380a42e99bSJan Kara return ret;
2139b9848081SChristoph Hellwig lo = idr_find(&loop_index_idr, idx);
21401c500ad7STetsuo Handa if (!lo || !lo->idr_visible)
2141b9848081SChristoph Hellwig ret = -ENODEV;
21421c500ad7STetsuo Handa else
21431c500ad7STetsuo Handa lo->idr_visible = false;
21441c500ad7STetsuo Handa mutex_unlock(&loop_ctl_mutex);
21451c500ad7STetsuo Handa if (ret)
21461c500ad7STetsuo Handa return ret;
2147f9d10764SChristoph Hellwig
21481c500ad7STetsuo Handa /* Check whether this loop device can be removed. */
21496cc8e743SPavel Tatashin ret = mutex_lock_killable(&lo->lo_mutex);
21506cc8e743SPavel Tatashin if (ret)
21511c500ad7STetsuo Handa goto mark_visible;
2152a0e286b6SChristoph Hellwig if (lo->lo_state != Lo_unbound || disk_openers(lo->lo_disk) > 0) {
21536cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex);
2154770fe30aSKay Sievers ret = -EBUSY;
21551c500ad7STetsuo Handa goto mark_visible;
2156770fe30aSKay Sievers }
2157a0e286b6SChristoph Hellwig /* Mark this loop device as no more bound, but not quite unbound yet */
2158990e7811SChristoph Hellwig lo->lo_state = Lo_deleting;
21596cc8e743SPavel Tatashin mutex_unlock(&lo->lo_mutex);
2160f9d10764SChristoph Hellwig
2161770fe30aSKay Sievers loop_remove(lo);
21621c500ad7STetsuo Handa return 0;
21631c500ad7STetsuo Handa
21641c500ad7STetsuo Handa mark_visible:
21651c500ad7STetsuo Handa /* Show this loop device again. */
21661c500ad7STetsuo Handa mutex_lock(&loop_ctl_mutex);
21671c500ad7STetsuo Handa lo->idr_visible = true;
2168f9d10764SChristoph Hellwig mutex_unlock(&loop_ctl_mutex);
2169f9d10764SChristoph Hellwig return ret;
2170770fe30aSKay Sievers }
2171f9d10764SChristoph Hellwig
loop_control_get_free(int idx)2172f9d10764SChristoph Hellwig static int loop_control_get_free(int idx)
2173f9d10764SChristoph Hellwig {
2174f9d10764SChristoph Hellwig struct loop_device *lo;
2175b9848081SChristoph Hellwig int id, ret;
2176f9d10764SChristoph Hellwig
2177f9d10764SChristoph Hellwig ret = mutex_lock_killable(&loop_ctl_mutex);
2178f9d10764SChristoph Hellwig if (ret)
2179f9d10764SChristoph Hellwig return ret;
2180b9848081SChristoph Hellwig idr_for_each_entry(&loop_index_idr, lo, id) {
21811c500ad7STetsuo Handa /* Hitting a race results in creating a new loop device which is harmless. */
21821c500ad7STetsuo Handa if (lo->idr_visible && data_race(lo->lo_state) == Lo_unbound)
2183b9848081SChristoph Hellwig goto found;
2184b9848081SChristoph Hellwig }
21850a42e99bSJan Kara mutex_unlock(&loop_ctl_mutex);
218618d1f200SChristoph Hellwig return loop_add(-1);
2187b9848081SChristoph Hellwig found:
2188b9848081SChristoph Hellwig mutex_unlock(&loop_ctl_mutex);
2189b9848081SChristoph Hellwig return id;
2190770fe30aSKay Sievers }
2191770fe30aSKay Sievers
loop_control_ioctl(struct file * file,unsigned int cmd,unsigned long parm)2192f9d10764SChristoph Hellwig static long loop_control_ioctl(struct file *file, unsigned int cmd,
2193f9d10764SChristoph Hellwig unsigned long parm)
2194f9d10764SChristoph Hellwig {
2195f9d10764SChristoph Hellwig switch (cmd) {
2196f9d10764SChristoph Hellwig case LOOP_CTL_ADD:
219718d1f200SChristoph Hellwig return loop_add(parm);
2198f9d10764SChristoph Hellwig case LOOP_CTL_REMOVE:
2199f9d10764SChristoph Hellwig return loop_control_remove(parm);
2200f9d10764SChristoph Hellwig case LOOP_CTL_GET_FREE:
2201f9d10764SChristoph Hellwig return loop_control_get_free(parm);
2202f9d10764SChristoph Hellwig default:
2203f9d10764SChristoph Hellwig return -ENOSYS;
2204f9d10764SChristoph Hellwig }
2205f9d10764SChristoph Hellwig }
2206f9d10764SChristoph Hellwig
2207770fe30aSKay Sievers static const struct file_operations loop_ctl_fops = {
2208770fe30aSKay Sievers .open = nonseekable_open,
2209770fe30aSKay Sievers .unlocked_ioctl = loop_control_ioctl,
2210770fe30aSKay Sievers .compat_ioctl = loop_control_ioctl,
2211770fe30aSKay Sievers .owner = THIS_MODULE,
2212770fe30aSKay Sievers .llseek = noop_llseek,
2213770fe30aSKay Sievers };
2214770fe30aSKay Sievers
2215770fe30aSKay Sievers static struct miscdevice loop_misc = {
2216770fe30aSKay Sievers .minor = LOOP_CTRL_MINOR,
2217770fe30aSKay Sievers .name = "loop-control",
2218770fe30aSKay Sievers .fops = &loop_ctl_fops,
2219770fe30aSKay Sievers };
2220770fe30aSKay Sievers
2221770fe30aSKay Sievers MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
2222770fe30aSKay Sievers MODULE_ALIAS("devname:loop-control");
2223770fe30aSKay Sievers
loop_init(void)222473285082SKen Chen static int __init loop_init(void)
222573285082SKen Chen {
222685c50197SIsaac J. Manjarres int i;
2227770fe30aSKay Sievers int err;
2228a47653fcSKen Chen
2229476a4813SLaurent Vivier part_shift = 0;
2230ac04fee0SNamhyung Kim if (max_part > 0) {
2231476a4813SLaurent Vivier part_shift = fls(max_part);
2232476a4813SLaurent Vivier
2233ac04fee0SNamhyung Kim /*
2234ac04fee0SNamhyung Kim * Adjust max_part according to part_shift as it is exported
2235ac04fee0SNamhyung Kim * to user space so that user can decide correct minor number
2236ac04fee0SNamhyung Kim * if [s]he want to create more devices.
2237ac04fee0SNamhyung Kim *
2238ac04fee0SNamhyung Kim * Note that -1 is required because partition 0 is reserved
2239ac04fee0SNamhyung Kim * for the whole disk.
2240ac04fee0SNamhyung Kim */
2241ac04fee0SNamhyung Kim max_part = (1UL << part_shift) - 1;
2242ac04fee0SNamhyung Kim }
2243ac04fee0SNamhyung Kim
2244b1a66504SGuo Chao if ((1UL << part_shift) > DISK_MAX_PARTS) {
2245b1a66504SGuo Chao err = -EINVAL;
2246a8c1d064SAnton Volkov goto err_out;
2247b1a66504SGuo Chao }
224878f4bb36SNamhyung Kim
2249b1a66504SGuo Chao if (max_loop > 1UL << (MINORBITS - part_shift)) {
2250b1a66504SGuo Chao err = -EINVAL;
2251a8c1d064SAnton Volkov goto err_out;
2252b1a66504SGuo Chao }
225373285082SKen Chen
2254a8c1d064SAnton Volkov err = misc_register(&loop_misc);
2255a8c1d064SAnton Volkov if (err < 0)
2256a8c1d064SAnton Volkov goto err_out;
2257a8c1d064SAnton Volkov
2258a8c1d064SAnton Volkov
22598410d38cSChristoph Hellwig if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) {
2260b1a66504SGuo Chao err = -EIO;
2261b1a66504SGuo Chao goto misc_out;
2262b1a66504SGuo Chao }
2263a47653fcSKen Chen
2264d134b00bSKay Sievers /* pre-create number of devices given by config or max_loop */
226585c50197SIsaac J. Manjarres for (i = 0; i < max_loop; i++)
2266d6da83d0SChristoph Hellwig loop_add(i);
226734dd82afSKay Sievers
226873285082SKen Chen printk(KERN_INFO "loop: module loaded\n");
22691da177e4SLinus Torvalds return 0;
2270b1a66504SGuo Chao
2271b1a66504SGuo Chao misc_out:
2272b1a66504SGuo Chao misc_deregister(&loop_misc);
2273a8c1d064SAnton Volkov err_out:
2274b1a66504SGuo Chao return err;
227534dd82afSKay Sievers }
2276a47653fcSKen Chen
loop_exit(void)227773285082SKen Chen static void __exit loop_exit(void)
22781da177e4SLinus Torvalds {
22798e60947dSChristoph Hellwig struct loop_device *lo;
22808e60947dSChristoph Hellwig int id;
22818e60947dSChristoph Hellwig
22828b52d8beSChristoph Hellwig unregister_blkdev(LOOP_MAJOR, "loop");
22838b52d8beSChristoph Hellwig misc_deregister(&loop_misc);
2284200f9337SLuis Chamberlain
22851c500ad7STetsuo Handa /*
22861c500ad7STetsuo Handa * There is no need to use loop_ctl_mutex here, for nobody else can
22871c500ad7STetsuo Handa * access loop_index_idr when this module is unloading (unless forced
22881c500ad7STetsuo Handa * module unloading is requested). If this is not a clean unloading,
22891c500ad7STetsuo Handa * we have no means to avoid kernel crash.
22901c500ad7STetsuo Handa */
22918e60947dSChristoph Hellwig idr_for_each_entry(&loop_index_idr, lo, id)
22928e60947dSChristoph Hellwig loop_remove(lo);
2293bd5c39edSChristoph Hellwig
2294bd5c39edSChristoph Hellwig idr_destroy(&loop_index_idr);
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds
22971da177e4SLinus Torvalds module_init(loop_init);
22981da177e4SLinus Torvalds module_exit(loop_exit);
22991da177e4SLinus Torvalds
23001da177e4SLinus Torvalds #ifndef MODULE
max_loop_setup(char * str)23011da177e4SLinus Torvalds static int __init max_loop_setup(char *str)
23021da177e4SLinus Torvalds {
23031da177e4SLinus Torvalds max_loop = simple_strtol(str, NULL, 0);
2304bb5faa99SMauricio Faria de Oliveira #ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD
2305bb5faa99SMauricio Faria de Oliveira max_loop_specified = true;
2306bb5faa99SMauricio Faria de Oliveira #endif
23071da177e4SLinus Torvalds return 1;
23081da177e4SLinus Torvalds }
23091da177e4SLinus Torvalds
23101da177e4SLinus Torvalds __setup("max_loop=", max_loop_setup);
23111da177e4SLinus Torvalds #endif
2312