2 * linux/drivers/block/loop.c
4 * Written by Theodore Ts'o, 3/29/93
6 * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
7 * permitted under the GNU General Public License.
9 * DES encryption plus some minor changes by Werner Almesberger, 30-MAY-1993
10 * more DES encryption plus IDEA encryption by Nicholas J. Leon, June 20, 1996
12 * Modularized and updated for 1.1.16 kernel - Mitch Dsouza 28th May 1994
13 * Adapted for 1.3.59 kernel - Andries Brouwer, 1 Feb 1996
15 * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
17 * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
19 * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
21 * Loadable modules and other fixes by AK, 1998
23 * Make real block number available to downstream transfer functions, enables
24 * CBC (and relatives) mode encryption requiring unique IVs per data block.
25 * Reed H. Petty, rhp@draper.net
27 * Maximum number of loop devices now dynamic via max_loop module parameter.
28 * Russell Kroll <rkroll@exploits.org> 19990701
30 * Maximum number of loop devices when compiled-in now selectable by passing
31 * max_loop=<1-255> to the kernel on boot.
32 * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999
34 * Completely rewrite request handling to be make_request_fn style and
35 * non blocking, pushing work to a helper thread. Lots of fixes from
37 * Jens Axboe <axboe@suse.de>, Nov 2000
39 * Support up to 256 loop devices
40 * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
42 * Support for falling back on the write file operation when the address space
43 * operations write_begin is not available on the backing filesystem.
44 * Anton Altaparmakov, 16 Feb 2005
47 * - Advisory locking is ignored here.
48 * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
52 #include <linux/module.h>
53 #include <linux/moduleparam.h>
54 #include <linux/sched.h>
56 #include <linux/pagemap.h>
57 #include <linux/file.h>
58 #include <linux/stat.h>
59 #include <linux/errno.h>
60 #include <linux/major.h>
61 #include <linux/wait.h>
62 #include <linux/blkdev.h>
63 #include <linux/blkpg.h>
64 #include <linux/init.h>
65 #include <linux/swap.h>
66 #include <linux/slab.h>
67 #include <linux/compat.h>
68 #include <linux/suspend.h>
69 #include <linux/freezer.h>
70 #include <linux/mutex.h>
71 #include <linux/writeback.h>
72 #include <linux/completion.h>
73 #include <linux/highmem.h>
74 #include <linux/splice.h>
75 #include <linux/sysfs.h>
76 #include <linux/miscdevice.h>
77 #include <linux/falloc.h>
78 #include <linux/uio.h>
79 #include <linux/ioprio.h>
80 #include <linux/blk-cgroup.h>
81 #include <linux/sched/mm.h>
82 #include <linux/statfs.h>
86 #include <linux/uaccess.h>
88 #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ)
90 static DEFINE_IDR(loop_index_idr);
91 static DEFINE_MUTEX(loop_ctl_mutex);
92 static DEFINE_MUTEX(loop_validate_mutex);
95 * loop_global_lock_killable() - take locks for safe loop_validate_file() test
97 * @lo: struct loop_device
98 * @global: true if @lo is about to bind another "struct loop_device", false otherwise
100 * Returns 0 on success, -EINTR otherwise.
102 * Since loop_validate_file() traverses on other "struct loop_device" if
103 * is_loop_device() is true, we need a global lock for serializing concurrent
104 * loop_configure()/loop_change_fd()/__loop_clr_fd() calls.
106 static int loop_global_lock_killable(struct loop_device *lo, bool global)
111 err = mutex_lock_killable(&loop_validate_mutex);
115 err = mutex_lock_killable(&lo->lo_mutex);
117 mutex_unlock(&loop_validate_mutex);
122 * loop_global_unlock() - release locks taken by loop_global_lock_killable()
124 * @lo: struct loop_device
125 * @global: true if @lo was about to bind another "struct loop_device", false otherwise
127 static void loop_global_unlock(struct loop_device *lo, bool global)
129 mutex_unlock(&lo->lo_mutex);
131 mutex_unlock(&loop_validate_mutex);
135 static int part_shift;
137 static int transfer_xor(struct loop_device *lo, int cmd,
138 struct page *raw_page, unsigned raw_off,
139 struct page *loop_page, unsigned loop_off,
140 int size, sector_t real_block)
142 char *raw_buf = kmap_atomic(raw_page) + raw_off;
143 char *loop_buf = kmap_atomic(loop_page) + loop_off;
144 char *in, *out, *key;
155 key = lo->lo_encrypt_key;
156 keysize = lo->lo_encrypt_key_size;
157 for (i = 0; i < size; i++)
158 *out++ = *in++ ^ key[(i & 511) % keysize];
160 kunmap_atomic(loop_buf);
161 kunmap_atomic(raw_buf);
166 static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
168 if (unlikely(info->lo_encrypt_key_size <= 0))
173 static struct loop_func_table none_funcs = {
174 .number = LO_CRYPT_NONE,
177 static struct loop_func_table xor_funcs = {
178 .number = LO_CRYPT_XOR,
179 .transfer = transfer_xor,
183 /* xfer_funcs[0] is special - its release function is never called */
184 static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
189 static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
193 /* Compute loopsize in bytes */
194 loopsize = i_size_read(file->f_mapping->host);
197 /* offset is beyond i_size, weird but possible */
201 if (sizelimit > 0 && sizelimit < loopsize)
202 loopsize = sizelimit;
204 * Unfortunately, if we want to do I/O on the device,
205 * the number of 512-byte sectors has to fit into a sector_t.
207 return loopsize >> 9;
210 static loff_t get_loop_size(struct loop_device *lo, struct file *file)
212 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
215 static void __loop_update_dio(struct loop_device *lo, bool dio)
217 struct file *file = lo->lo_backing_file;
218 struct address_space *mapping = file->f_mapping;
219 struct inode *inode = mapping->host;
220 unsigned short sb_bsize = 0;
221 unsigned dio_align = 0;
224 if (inode->i_sb->s_bdev) {
225 sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
226 dio_align = sb_bsize - 1;
230 * We support direct I/O only if lo_offset is aligned with the
231 * logical I/O size of backing device, and the logical block
232 * size of loop is bigger than the backing device's and the loop
233 * needn't transform transfer.
235 * TODO: the above condition may be loosed in the future, and
236 * direct I/O may be switched runtime at that time because most
237 * of requests in sane applications should be PAGE_SIZE aligned
240 if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
241 !(lo->lo_offset & dio_align) &&
242 mapping->a_ops->direct_IO &&
251 if (lo->use_dio == use_dio)
254 /* flush dirty pages before changing direct IO */
258 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
259 * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
260 * will get updated by ioctl(LOOP_GET_STATUS)
262 if (lo->lo_state == Lo_bound)
263 blk_mq_freeze_queue(lo->lo_queue);
264 lo->use_dio = use_dio;
266 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue);
267 lo->lo_flags |= LO_FLAGS_DIRECT_IO;
269 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
270 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
272 if (lo->lo_state == Lo_bound)
273 blk_mq_unfreeze_queue(lo->lo_queue);
277 * loop_set_size() - sets device size and notifies userspace
278 * @lo: struct loop_device to set the size for
279 * @size: new size of the loop device
281 * Callers must validate that the size passed into this function fits into
282 * a sector_t, eg using loop_validate_size()
284 static void loop_set_size(struct loop_device *lo, loff_t size)
286 if (!set_capacity_and_notify(lo->lo_disk, size))
287 kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
291 lo_do_transfer(struct loop_device *lo, int cmd,
292 struct page *rpage, unsigned roffs,
293 struct page *lpage, unsigned loffs,
294 int size, sector_t rblock)
298 ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
302 printk_ratelimited(KERN_ERR
303 "loop: Transfer error at byte offset %llu, length %i.\n",
304 (unsigned long long)rblock << 9, size);
308 static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
313 iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len);
315 file_start_write(file);
316 bw = vfs_iter_write(file, &i, ppos, 0);
317 file_end_write(file);
319 if (likely(bw == bvec->bv_len))
322 printk_ratelimited(KERN_ERR
323 "loop: Write error at byte offset %llu, length %i.\n",
324 (unsigned long long)*ppos, bvec->bv_len);
330 static int lo_write_simple(struct loop_device *lo, struct request *rq,
334 struct req_iterator iter;
337 rq_for_each_segment(bvec, rq, iter) {
338 ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos);
348 * This is the slow, transforming version that needs to double buffer the
349 * data as it cannot do the transformations in place without having direct
350 * access to the destination pages of the backing file.
352 static int lo_write_transfer(struct loop_device *lo, struct request *rq,
355 struct bio_vec bvec, b;
356 struct req_iterator iter;
360 page = alloc_page(GFP_NOIO);
364 rq_for_each_segment(bvec, rq, iter) {
365 ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page,
366 bvec.bv_offset, bvec.bv_len, pos >> 9);
372 b.bv_len = bvec.bv_len;
373 ret = lo_write_bvec(lo->lo_backing_file, &b, &pos);
382 static int lo_read_simple(struct loop_device *lo, struct request *rq,
386 struct req_iterator iter;
390 rq_for_each_segment(bvec, rq, iter) {
391 iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len);
392 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
396 flush_dcache_page(bvec.bv_page);
398 if (len != bvec.bv_len) {
401 __rq_for_each_bio(bio, rq)
411 static int lo_read_transfer(struct loop_device *lo, struct request *rq,
414 struct bio_vec bvec, b;
415 struct req_iterator iter;
421 page = alloc_page(GFP_NOIO);
425 rq_for_each_segment(bvec, rq, iter) {
430 b.bv_len = bvec.bv_len;
432 iov_iter_bvec(&i, READ, &b, 1, b.bv_len);
433 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0);
439 ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page,
440 bvec.bv_offset, len, offset >> 9);
444 flush_dcache_page(bvec.bv_page);
446 if (len != bvec.bv_len) {
449 __rq_for_each_bio(bio, rq)
461 static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
465 * We use fallocate to manipulate the space mappings used by the image
466 * a.k.a. discard/zerorange. However we do not support this if
467 * encryption is enabled, because it may give an attacker useful
470 struct file *file = lo->lo_backing_file;
471 struct request_queue *q = lo->lo_queue;
474 mode |= FALLOC_FL_KEEP_SIZE;
476 if (!blk_queue_discard(q)) {
481 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
482 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
488 static int lo_req_flush(struct loop_device *lo, struct request *rq)
490 struct file *file = lo->lo_backing_file;
491 int ret = vfs_fsync(file, 0);
492 if (unlikely(ret && ret != -EINVAL))
498 static void lo_complete_rq(struct request *rq)
500 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
501 blk_status_t ret = BLK_STS_OK;
503 if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) ||
504 req_op(rq) != REQ_OP_READ) {
506 ret = errno_to_blk_status(cmd->ret);
511 * Short READ - if we got some data, advance our request and
512 * retry it. If we got no data, end the rest with EIO.
515 blk_update_request(rq, BLK_STS_OK, cmd->ret);
517 blk_mq_requeue_request(rq, true);
520 struct bio *bio = rq->bio;
529 blk_mq_end_request(rq, ret);
533 static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
535 struct request *rq = blk_mq_rq_from_pdu(cmd);
537 if (!atomic_dec_and_test(&cmd->ref))
541 if (likely(!blk_should_fake_timeout(rq->q)))
542 blk_mq_complete_request(rq);
545 static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
547 struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
550 lo_rw_aio_do_completion(cmd);
553 static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
556 struct iov_iter iter;
557 struct req_iterator rq_iter;
558 struct bio_vec *bvec;
559 struct request *rq = blk_mq_rq_from_pdu(cmd);
560 struct bio *bio = rq->bio;
561 struct file *file = lo->lo_backing_file;
567 rq_for_each_bvec(tmp, rq, rq_iter)
570 if (rq->bio != rq->biotail) {
572 bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
579 * The bios of the request may be started from the middle of
580 * the 'bvec' because of bio splitting, so we can't directly
581 * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec
582 * API will take care of all details for us.
584 rq_for_each_bvec(tmp, rq, rq_iter) {
592 * Same here, this bio may be started from the middle of the
593 * 'bvec' because of bio splitting, so offset from the bvec
594 * must be passed to iov iterator
596 offset = bio->bi_iter.bi_bvec_done;
597 bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
599 atomic_set(&cmd->ref, 2);
601 iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq));
602 iter.iov_offset = offset;
604 cmd->iocb.ki_pos = pos;
605 cmd->iocb.ki_filp = file;
606 cmd->iocb.ki_complete = lo_rw_aio_complete;
607 cmd->iocb.ki_flags = IOCB_DIRECT;
608 cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
611 ret = call_write_iter(file, &cmd->iocb, &iter);
613 ret = call_read_iter(file, &cmd->iocb, &iter);
615 lo_rw_aio_do_completion(cmd);
617 if (ret != -EIOCBQUEUED)
618 cmd->iocb.ki_complete(&cmd->iocb, ret, 0);
622 static int do_req_filebacked(struct loop_device *lo, struct request *rq)
624 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
625 loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
628 * lo_write_simple and lo_read_simple should have been covered
629 * by io submit style function like lo_rw_aio(), one blocker
630 * is that lo_read_simple() need to call flush_dcache_page after
631 * the page is written from kernel, and it isn't easy to handle
632 * this in io submit style function which submits all segments
633 * of the req at one time. And direct read IO doesn't need to
634 * run flush_dcache_page().
636 switch (req_op(rq)) {
638 return lo_req_flush(lo, rq);
639 case REQ_OP_WRITE_ZEROES:
641 * If the caller doesn't want deallocation, call zeroout to
642 * write zeroes the range. Otherwise, punch them out.
644 return lo_fallocate(lo, rq, pos,
645 (rq->cmd_flags & REQ_NOUNMAP) ?
646 FALLOC_FL_ZERO_RANGE :
647 FALLOC_FL_PUNCH_HOLE);
649 return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
652 return lo_write_transfer(lo, rq, pos);
653 else if (cmd->use_aio)
654 return lo_rw_aio(lo, cmd, pos, WRITE);
656 return lo_write_simple(lo, rq, pos);
659 return lo_read_transfer(lo, rq, pos);
660 else if (cmd->use_aio)
661 return lo_rw_aio(lo, cmd, pos, READ);
663 return lo_read_simple(lo, rq, pos);
670 static inline void loop_update_dio(struct loop_device *lo)
672 __loop_update_dio(lo, (lo->lo_backing_file->f_flags & O_DIRECT) |
676 static void loop_reread_partitions(struct loop_device *lo)
680 mutex_lock(&lo->lo_disk->open_mutex);
681 rc = bdev_disk_changed(lo->lo_disk, false);
682 mutex_unlock(&lo->lo_disk->open_mutex);
684 pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
685 __func__, lo->lo_number, lo->lo_file_name, rc);
688 static inline int is_loop_device(struct file *file)
690 struct inode *i = file->f_mapping->host;
692 return i && S_ISBLK(i->i_mode) && imajor(i) == LOOP_MAJOR;
695 static int loop_validate_file(struct file *file, struct block_device *bdev)
697 struct inode *inode = file->f_mapping->host;
698 struct file *f = file;
700 /* Avoid recursion */
701 while (is_loop_device(f)) {
702 struct loop_device *l;
704 lockdep_assert_held(&loop_validate_mutex);
705 if (f->f_mapping->host->i_rdev == bdev->bd_dev)
708 l = I_BDEV(f->f_mapping->host)->bd_disk->private_data;
709 if (l->lo_state != Lo_bound)
711 /* Order wrt setting lo->lo_backing_file in loop_configure(). */
713 f = l->lo_backing_file;
715 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
721 * loop_change_fd switched the backing store of a loopback device to
722 * a new file. This is useful for operating system installers to free up
723 * the original file and in High Availability environments to switch to
724 * an alternative location for the content in case of server meltdown.
725 * This can only work if the loop device is used read-only, and if the
726 * new backing store is the same size and type as the old backing store.
728 static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
731 struct file *file = fget(arg);
732 struct file *old_file;
739 is_loop = is_loop_device(file);
740 error = loop_global_lock_killable(lo, is_loop);
744 if (lo->lo_state != Lo_bound)
747 /* the loop device has to be read-only */
749 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
752 error = loop_validate_file(file, bdev);
756 old_file = lo->lo_backing_file;
760 /* size of the new backing store needs to be the same */
761 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
765 disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
766 blk_mq_freeze_queue(lo->lo_queue);
767 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
768 lo->lo_backing_file = file;
769 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
770 mapping_set_gfp_mask(file->f_mapping,
771 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
773 blk_mq_unfreeze_queue(lo->lo_queue);
774 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
775 loop_global_unlock(lo, is_loop);
778 * Flush loop_validate_file() before fput(), for l->lo_backing_file
779 * might be pointing at old_file which might be the last reference.
782 mutex_lock(&loop_validate_mutex);
783 mutex_unlock(&loop_validate_mutex);
786 * We must drop file reference outside of lo_mutex as dropping
787 * the file ref can take open_mutex which creates circular locking
792 loop_reread_partitions(lo);
796 loop_global_unlock(lo, is_loop);
802 /* loop sysfs attributes */
804 static ssize_t loop_attr_show(struct device *dev, char *page,
805 ssize_t (*callback)(struct loop_device *, char *))
807 struct gendisk *disk = dev_to_disk(dev);
808 struct loop_device *lo = disk->private_data;
810 return callback(lo, page);
813 #define LOOP_ATTR_RO(_name) \
814 static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
815 static ssize_t loop_attr_do_show_##_name(struct device *d, \
816 struct device_attribute *attr, char *b) \
818 return loop_attr_show(d, b, loop_attr_##_name##_show); \
820 static struct device_attribute loop_attr_##_name = \
821 __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL);
823 static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
828 spin_lock_irq(&lo->lo_lock);
829 if (lo->lo_backing_file)
830 p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1);
831 spin_unlock_irq(&lo->lo_lock);
833 if (IS_ERR_OR_NULL(p))
837 memmove(buf, p, ret);
845 static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
847 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
850 static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
852 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
855 static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
857 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
859 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
862 static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
864 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
866 return sprintf(buf, "%s\n", partscan ? "1" : "0");
869 static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
871 int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
873 return sprintf(buf, "%s\n", dio ? "1" : "0");
876 LOOP_ATTR_RO(backing_file);
877 LOOP_ATTR_RO(offset);
878 LOOP_ATTR_RO(sizelimit);
879 LOOP_ATTR_RO(autoclear);
880 LOOP_ATTR_RO(partscan);
883 static struct attribute *loop_attrs[] = {
884 &loop_attr_backing_file.attr,
885 &loop_attr_offset.attr,
886 &loop_attr_sizelimit.attr,
887 &loop_attr_autoclear.attr,
888 &loop_attr_partscan.attr,
893 static struct attribute_group loop_attribute_group = {
898 static void loop_sysfs_init(struct loop_device *lo)
900 lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
901 &loop_attribute_group);
904 static void loop_sysfs_exit(struct loop_device *lo)
906 if (lo->sysfs_inited)
907 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
908 &loop_attribute_group);
911 static void loop_config_discard(struct loop_device *lo)
913 struct file *file = lo->lo_backing_file;
914 struct inode *inode = file->f_mapping->host;
915 struct request_queue *q = lo->lo_queue;
916 u32 granularity, max_discard_sectors;
919 * If the backing device is a block device, mirror its zeroing
920 * capability. Set the discard sectors to the block device's zeroing
921 * capabilities because loop discards result in blkdev_issue_zeroout(),
922 * not blkdev_issue_discard(). This maintains consistent behavior with
923 * file-backed loop devices: discarded regions read back as zero.
925 if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) {
926 struct request_queue *backingq = bdev_get_queue(I_BDEV(inode));
928 max_discard_sectors = backingq->limits.max_write_zeroes_sectors;
929 granularity = backingq->limits.discard_granularity ?:
930 queue_physical_block_size(backingq);
933 * We use punch hole to reclaim the free space used by the
934 * image a.k.a. discard. However we do not support discard if
935 * encryption is enabled, because it may give an attacker
936 * useful information.
938 } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) {
939 max_discard_sectors = 0;
945 max_discard_sectors = UINT_MAX >> 9;
946 if (!vfs_statfs(&file->f_path, &sbuf))
947 granularity = sbuf.f_bsize;
949 max_discard_sectors = 0;
952 if (max_discard_sectors) {
953 q->limits.discard_granularity = granularity;
954 blk_queue_max_discard_sectors(q, max_discard_sectors);
955 blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
956 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
958 q->limits.discard_granularity = 0;
959 blk_queue_max_discard_sectors(q, 0);
960 blk_queue_max_write_zeroes_sectors(q, 0);
961 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
963 q->limits.discard_alignment = 0;
967 struct rb_node rb_node;
968 struct work_struct work;
969 struct list_head cmd_list;
970 struct list_head idle_list;
971 struct loop_device *lo;
972 struct cgroup_subsys_state *blkcg_css;
973 unsigned long last_ran_at;
976 static void loop_workfn(struct work_struct *work);
977 static void loop_rootcg_workfn(struct work_struct *work);
978 static void loop_free_idle_workers(struct timer_list *timer);
980 #ifdef CONFIG_BLK_CGROUP
981 static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
983 return !css || css == blkcg_root_css;
986 static inline int queue_on_root_worker(struct cgroup_subsys_state *css)
992 static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd)
994 struct rb_node **node = &(lo->worker_tree.rb_node), *parent = NULL;
995 struct loop_worker *cur_worker, *worker = NULL;
996 struct work_struct *work;
997 struct list_head *cmd_list;
999 spin_lock_irq(&lo->lo_work_lock);
1001 if (queue_on_root_worker(cmd->blkcg_css))
1004 node = &lo->worker_tree.rb_node;
1008 cur_worker = container_of(*node, struct loop_worker, rb_node);
1009 if (cur_worker->blkcg_css == cmd->blkcg_css) {
1010 worker = cur_worker;
1012 } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) {
1013 node = &(*node)->rb_left;
1015 node = &(*node)->rb_right;
1021 worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN);
1023 * In the event we cannot allocate a worker, just queue on the
1024 * rootcg worker and issue the I/O as the rootcg
1027 cmd->blkcg_css = NULL;
1029 css_put(cmd->memcg_css);
1030 cmd->memcg_css = NULL;
1034 worker->blkcg_css = cmd->blkcg_css;
1035 css_get(worker->blkcg_css);
1036 INIT_WORK(&worker->work, loop_workfn);
1037 INIT_LIST_HEAD(&worker->cmd_list);
1038 INIT_LIST_HEAD(&worker->idle_list);
1040 rb_link_node(&worker->rb_node, parent, node);
1041 rb_insert_color(&worker->rb_node, &lo->worker_tree);
1045 * We need to remove from the idle list here while
1046 * holding the lock so that the idle timer doesn't
1049 if (!list_empty(&worker->idle_list))
1050 list_del_init(&worker->idle_list);
1051 work = &worker->work;
1052 cmd_list = &worker->cmd_list;
1054 work = &lo->rootcg_work;
1055 cmd_list = &lo->rootcg_cmd_list;
1057 list_add_tail(&cmd->list_entry, cmd_list);
1058 queue_work(lo->workqueue, work);
1059 spin_unlock_irq(&lo->lo_work_lock);
1062 static void loop_update_rotational(struct loop_device *lo)
1064 struct file *file = lo->lo_backing_file;
1065 struct inode *file_inode = file->f_mapping->host;
1066 struct block_device *file_bdev = file_inode->i_sb->s_bdev;
1067 struct request_queue *q = lo->lo_queue;
1070 /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
1072 nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev));
1075 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
1077 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
1081 loop_release_xfer(struct loop_device *lo)
1084 struct loop_func_table *xfer = lo->lo_encryption;
1088 err = xfer->release(lo);
1089 lo->transfer = NULL;
1090 lo->lo_encryption = NULL;
1091 module_put(xfer->owner);
1097 loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
1098 const struct loop_info64 *i)
1103 struct module *owner = xfer->owner;
1105 if (!try_module_get(owner))
1108 err = xfer->init(lo, i);
1112 lo->lo_encryption = xfer;
1118 * loop_set_status_from_info - configure device from loop_info
1119 * @lo: struct loop_device to configure
1120 * @info: struct loop_info64 to configure the device with
1122 * Configures the loop device parameters according to the passed
1123 * in loop_info64 configuration.
1126 loop_set_status_from_info(struct loop_device *lo,
1127 const struct loop_info64 *info)
1130 struct loop_func_table *xfer;
1131 kuid_t uid = current_uid();
1133 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
1136 err = loop_release_xfer(lo);
1140 if (info->lo_encrypt_type) {
1141 unsigned int type = info->lo_encrypt_type;
1143 if (type >= MAX_LO_CRYPT)
1145 xfer = xfer_funcs[type];
1151 err = loop_init_xfer(lo, xfer, info);
1155 lo->lo_offset = info->lo_offset;
1156 lo->lo_sizelimit = info->lo_sizelimit;
1157 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1158 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
1159 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
1160 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
1164 lo->transfer = xfer->transfer;
1165 lo->ioctl = xfer->ioctl;
1167 lo->lo_flags = info->lo_flags;
1169 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1170 lo->lo_init[0] = info->lo_init[0];
1171 lo->lo_init[1] = info->lo_init[1];
1172 if (info->lo_encrypt_key_size) {
1173 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1174 info->lo_encrypt_key_size);
1175 lo->lo_key_owner = uid;
1181 static int loop_configure(struct loop_device *lo, fmode_t mode,
1182 struct block_device *bdev,
1183 const struct loop_config *config)
1185 struct file *file = fget(config->fd);
1186 struct inode *inode;
1187 struct address_space *mapping;
1191 unsigned short bsize;
1196 is_loop = is_loop_device(file);
1198 /* This is safe, since we have a reference from open(). */
1199 __module_get(THIS_MODULE);
1202 * If we don't hold exclusive handle for the device, upgrade to it
1203 * here to avoid changing device under exclusive owner.
1205 if (!(mode & FMODE_EXCL)) {
1206 error = bd_prepare_to_claim(bdev, loop_configure);
1211 error = loop_global_lock_killable(lo, is_loop);
1216 if (lo->lo_state != Lo_unbound)
1219 error = loop_validate_file(file, bdev);
1223 mapping = file->f_mapping;
1224 inode = mapping->host;
1226 if ((config->info.lo_flags & ~LOOP_CONFIGURE_SETTABLE_FLAGS) != 0) {
1231 if (config->block_size) {
1232 error = blk_validate_block_size(config->block_size);
1237 error = loop_set_status_from_info(lo, &config->info);
1241 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
1242 !file->f_op->write_iter)
1243 lo->lo_flags |= LO_FLAGS_READ_ONLY;
1245 lo->workqueue = alloc_workqueue("loop%d",
1246 WQ_UNBOUND | WQ_FREEZABLE,
1249 if (!lo->workqueue) {
1254 disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
1255 set_disk_ro(lo->lo_disk, (lo->lo_flags & LO_FLAGS_READ_ONLY) != 0);
1257 INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn);
1258 INIT_LIST_HEAD(&lo->rootcg_cmd_list);
1259 INIT_LIST_HEAD(&lo->idle_worker_list);
1260 lo->worker_tree = RB_ROOT;
1261 timer_setup(&lo->timer, loop_free_idle_workers,
1263 lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
1264 lo->lo_device = bdev;
1265 lo->lo_backing_file = file;
1266 lo->old_gfp_mask = mapping_gfp_mask(mapping);
1267 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
1269 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
1270 blk_queue_write_cache(lo->lo_queue, true, false);
1272 if (config->block_size)
1273 bsize = config->block_size;
1274 else if ((lo->lo_backing_file->f_flags & O_DIRECT) && inode->i_sb->s_bdev)
1275 /* In case of direct I/O, match underlying block size */
1276 bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
1280 blk_queue_logical_block_size(lo->lo_queue, bsize);
1281 blk_queue_physical_block_size(lo->lo_queue, bsize);
1282 blk_queue_io_min(lo->lo_queue, bsize);
1284 loop_config_discard(lo);
1285 loop_update_rotational(lo);
1286 loop_update_dio(lo);
1287 loop_sysfs_init(lo);
1289 size = get_loop_size(lo, file);
1290 loop_set_size(lo, size);
1292 /* Order wrt reading lo_state in loop_validate_file(). */
1295 lo->lo_state = Lo_bound;
1297 lo->lo_flags |= LO_FLAGS_PARTSCAN;
1298 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
1300 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1302 loop_global_unlock(lo, is_loop);
1304 loop_reread_partitions(lo);
1305 if (!(mode & FMODE_EXCL))
1306 bd_abort_claiming(bdev, loop_configure);
1310 loop_global_unlock(lo, is_loop);
1312 if (!(mode & FMODE_EXCL))
1313 bd_abort_claiming(bdev, loop_configure);
1316 /* This is safe: open() is still holding a reference. */
1317 module_put(THIS_MODULE);
1321 static int __loop_clr_fd(struct loop_device *lo, bool release)
1323 struct file *filp = NULL;
1324 gfp_t gfp = lo->old_gfp_mask;
1325 struct block_device *bdev = lo->lo_device;
1327 bool partscan = false;
1329 struct loop_worker *pos, *worker;
1332 * Flush loop_configure() and loop_change_fd(). It is acceptable for
1333 * loop_validate_file() to succeed, for actual clear operation has not
1336 mutex_lock(&loop_validate_mutex);
1337 mutex_unlock(&loop_validate_mutex);
1339 * loop_validate_file() now fails because l->lo_state != Lo_bound
1343 mutex_lock(&lo->lo_mutex);
1344 if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) {
1349 filp = lo->lo_backing_file;
1355 if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
1356 blk_queue_write_cache(lo->lo_queue, false, false);
1358 /* freeze request queue during the transition */
1359 blk_mq_freeze_queue(lo->lo_queue);
1361 destroy_workqueue(lo->workqueue);
1362 spin_lock_irq(&lo->lo_work_lock);
1363 list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
1365 list_del(&worker->idle_list);
1366 rb_erase(&worker->rb_node, &lo->worker_tree);
1367 css_put(worker->blkcg_css);
1370 spin_unlock_irq(&lo->lo_work_lock);
1371 del_timer_sync(&lo->timer);
1373 spin_lock_irq(&lo->lo_lock);
1374 lo->lo_backing_file = NULL;
1375 spin_unlock_irq(&lo->lo_lock);
1377 loop_release_xfer(lo);
1378 lo->transfer = NULL;
1380 lo->lo_device = NULL;
1381 lo->lo_encryption = NULL;
1383 lo->lo_sizelimit = 0;
1384 lo->lo_encrypt_key_size = 0;
1385 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
1386 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
1387 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
1388 blk_queue_logical_block_size(lo->lo_queue, 512);
1389 blk_queue_physical_block_size(lo->lo_queue, 512);
1390 blk_queue_io_min(lo->lo_queue, 512);
1392 invalidate_bdev(bdev);
1393 bdev->bd_inode->i_mapping->wb_err = 0;
1395 set_capacity(lo->lo_disk, 0);
1396 loop_sysfs_exit(lo);
1398 /* let user-space know about this change */
1399 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
1401 mapping_set_gfp_mask(filp->f_mapping, gfp);
1402 /* This is safe: open() is still holding a reference. */
1403 module_put(THIS_MODULE);
1404 blk_mq_unfreeze_queue(lo->lo_queue);
1406 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev;
1407 lo_number = lo->lo_number;
1408 disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
1410 mutex_unlock(&lo->lo_mutex);
1413 * open_mutex has been held already in release path, so don't
1414 * acquire it if this function is called in such case.
1416 * If the reread partition isn't from release path, lo_refcnt
1417 * must be at least one and it can only become zero when the
1418 * current holder is released.
1421 mutex_lock(&lo->lo_disk->open_mutex);
1422 err = bdev_disk_changed(lo->lo_disk, false);
1424 mutex_unlock(&lo->lo_disk->open_mutex);
1426 pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
1427 __func__, lo_number, err);
1428 /* Device is gone, no point in returning error */
1433 * lo->lo_state is set to Lo_unbound here after above partscan has
1436 * There cannot be anybody else entering __loop_clr_fd() as
1437 * lo->lo_backing_file is already cleared and Lo_rundown state
1438 * protects us from all the other places trying to change the 'lo'
1441 mutex_lock(&lo->lo_mutex);
1444 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1445 lo->lo_state = Lo_unbound;
1446 mutex_unlock(&lo->lo_mutex);
1449 * Need not hold lo_mutex to fput backing file. Calling fput holding
1450 * lo_mutex triggers a circular lock dependency possibility warning as
1451 * fput can take open_mutex which is usually taken before lo_mutex.
1458 static int loop_clr_fd(struct loop_device *lo)
1462 err = mutex_lock_killable(&lo->lo_mutex);
1465 if (lo->lo_state != Lo_bound) {
1466 mutex_unlock(&lo->lo_mutex);
1470 * If we've explicitly asked to tear down the loop device,
1471 * and it has an elevated reference count, set it for auto-teardown when
1472 * the last reference goes away. This stops $!~#$@ udev from
1473 * preventing teardown because it decided that it needs to run blkid on
1474 * the loopback device whenever they appear. xfstests is notorious for
1475 * failing tests because blkid via udev races with a losetup
1476 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1477 * command to fail with EBUSY.
1479 if (atomic_read(&lo->lo_refcnt) > 1) {
1480 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
1481 mutex_unlock(&lo->lo_mutex);
1484 lo->lo_state = Lo_rundown;
1485 mutex_unlock(&lo->lo_mutex);
1487 return __loop_clr_fd(lo, false);
1491 loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1494 kuid_t uid = current_uid();
1496 bool partscan = false;
1497 bool size_changed = false;
1499 err = mutex_lock_killable(&lo->lo_mutex);
1502 if (lo->lo_encrypt_key_size &&
1503 !uid_eq(lo->lo_key_owner, uid) &&
1504 !capable(CAP_SYS_ADMIN)) {
1508 if (lo->lo_state != Lo_bound) {
1513 if (lo->lo_offset != info->lo_offset ||
1514 lo->lo_sizelimit != info->lo_sizelimit) {
1515 size_changed = true;
1516 sync_blockdev(lo->lo_device);
1517 invalidate_bdev(lo->lo_device);
1520 /* I/O need to be drained during transfer transition */
1521 blk_mq_freeze_queue(lo->lo_queue);
1523 if (size_changed && lo->lo_device->bd_inode->i_mapping->nrpages) {
1524 /* If any pages were dirtied after invalidate_bdev(), try again */
1526 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1527 __func__, lo->lo_number, lo->lo_file_name,
1528 lo->lo_device->bd_inode->i_mapping->nrpages);
1532 prev_lo_flags = lo->lo_flags;
1534 err = loop_set_status_from_info(lo, info);
1538 /* Mask out flags that can't be set using LOOP_SET_STATUS. */
1539 lo->lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS;
1540 /* For those flags, use the previous values instead */
1541 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_SETTABLE_FLAGS;
1542 /* For flags that can't be cleared, use previous values too */
1543 lo->lo_flags |= prev_lo_flags & ~LOOP_SET_STATUS_CLEARABLE_FLAGS;
1546 loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
1547 lo->lo_backing_file);
1548 loop_set_size(lo, new_size);
1551 loop_config_discard(lo);
1553 /* update dio if lo_offset or transfer is changed */
1554 __loop_update_dio(lo, lo->use_dio);
1557 blk_mq_unfreeze_queue(lo->lo_queue);
1559 if (!err && (lo->lo_flags & LO_FLAGS_PARTSCAN) &&
1560 !(prev_lo_flags & LO_FLAGS_PARTSCAN)) {
1561 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1565 mutex_unlock(&lo->lo_mutex);
1567 loop_reread_partitions(lo);
1573 loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1579 ret = mutex_lock_killable(&lo->lo_mutex);
1582 if (lo->lo_state != Lo_bound) {
1583 mutex_unlock(&lo->lo_mutex);
1587 memset(info, 0, sizeof(*info));
1588 info->lo_number = lo->lo_number;
1589 info->lo_offset = lo->lo_offset;
1590 info->lo_sizelimit = lo->lo_sizelimit;
1591 info->lo_flags = lo->lo_flags;
1592 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1593 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1594 info->lo_encrypt_type =
1595 lo->lo_encryption ? lo->lo_encryption->number : 0;
1596 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1597 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1598 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1599 lo->lo_encrypt_key_size);
1602 /* Drop lo_mutex while we call into the filesystem. */
1603 path = lo->lo_backing_file->f_path;
1605 mutex_unlock(&lo->lo_mutex);
1606 ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
1608 info->lo_device = huge_encode_dev(stat.dev);
1609 info->lo_inode = stat.ino;
1610 info->lo_rdevice = huge_encode_dev(stat.rdev);
1617 loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1619 memset(info64, 0, sizeof(*info64));
1620 info64->lo_number = info->lo_number;
1621 info64->lo_device = info->lo_device;
1622 info64->lo_inode = info->lo_inode;
1623 info64->lo_rdevice = info->lo_rdevice;
1624 info64->lo_offset = info->lo_offset;
1625 info64->lo_sizelimit = 0;
1626 info64->lo_encrypt_type = info->lo_encrypt_type;
1627 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1628 info64->lo_flags = info->lo_flags;
1629 info64->lo_init[0] = info->lo_init[0];
1630 info64->lo_init[1] = info->lo_init[1];
1631 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1632 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1634 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1635 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1639 loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1641 memset(info, 0, sizeof(*info));
1642 info->lo_number = info64->lo_number;
1643 info->lo_device = info64->lo_device;
1644 info->lo_inode = info64->lo_inode;
1645 info->lo_rdevice = info64->lo_rdevice;
1646 info->lo_offset = info64->lo_offset;
1647 info->lo_encrypt_type = info64->lo_encrypt_type;
1648 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1649 info->lo_flags = info64->lo_flags;
1650 info->lo_init[0] = info64->lo_init[0];
1651 info->lo_init[1] = info64->lo_init[1];
1652 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1653 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1655 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1656 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1658 /* error in case values were truncated */
1659 if (info->lo_device != info64->lo_device ||
1660 info->lo_rdevice != info64->lo_rdevice ||
1661 info->lo_inode != info64->lo_inode ||
1662 info->lo_offset != info64->lo_offset)
1669 loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1671 struct loop_info info;
1672 struct loop_info64 info64;
1674 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1676 loop_info64_from_old(&info, &info64);
1677 return loop_set_status(lo, &info64);
1681 loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1683 struct loop_info64 info64;
1685 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1687 return loop_set_status(lo, &info64);
1691 loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1692 struct loop_info info;
1693 struct loop_info64 info64;
1698 err = loop_get_status(lo, &info64);
1700 err = loop_info64_to_old(&info64, &info);
1701 if (!err && copy_to_user(arg, &info, sizeof(info)))
1708 loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1709 struct loop_info64 info64;
1714 err = loop_get_status(lo, &info64);
1715 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1721 static int loop_set_capacity(struct loop_device *lo)
1725 if (unlikely(lo->lo_state != Lo_bound))
1728 size = get_loop_size(lo, lo->lo_backing_file);
1729 loop_set_size(lo, size);
1734 static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1737 if (lo->lo_state != Lo_bound)
1740 __loop_update_dio(lo, !!arg);
1741 if (lo->use_dio == !!arg)
1748 static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
1752 if (lo->lo_state != Lo_bound)
1755 err = blk_validate_block_size(arg);
1759 if (lo->lo_queue->limits.logical_block_size == arg)
1762 sync_blockdev(lo->lo_device);
1763 invalidate_bdev(lo->lo_device);
1765 blk_mq_freeze_queue(lo->lo_queue);
1767 /* invalidate_bdev should have truncated all the pages */
1768 if (lo->lo_device->bd_inode->i_mapping->nrpages) {
1770 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1771 __func__, lo->lo_number, lo->lo_file_name,
1772 lo->lo_device->bd_inode->i_mapping->nrpages);
1776 blk_queue_logical_block_size(lo->lo_queue, arg);
1777 blk_queue_physical_block_size(lo->lo_queue, arg);
1778 blk_queue_io_min(lo->lo_queue, arg);
1779 loop_update_dio(lo);
1781 blk_mq_unfreeze_queue(lo->lo_queue);
1786 static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
1791 err = mutex_lock_killable(&lo->lo_mutex);
1795 case LOOP_SET_CAPACITY:
1796 err = loop_set_capacity(lo);
1798 case LOOP_SET_DIRECT_IO:
1799 err = loop_set_dio(lo, arg);
1801 case LOOP_SET_BLOCK_SIZE:
1802 err = loop_set_block_size(lo, arg);
1805 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1807 mutex_unlock(&lo->lo_mutex);
1811 static int lo_ioctl(struct block_device *bdev, fmode_t mode,
1812 unsigned int cmd, unsigned long arg)
1814 struct loop_device *lo = bdev->bd_disk->private_data;
1815 void __user *argp = (void __user *) arg;
1821 * Legacy case - pass in a zeroed out struct loop_config with
1822 * only the file descriptor set , which corresponds with the
1823 * default parameters we'd have used otherwise.
1825 struct loop_config config;
1827 memset(&config, 0, sizeof(config));
1830 return loop_configure(lo, mode, bdev, &config);
1832 case LOOP_CONFIGURE: {
1833 struct loop_config config;
1835 if (copy_from_user(&config, argp, sizeof(config)))
1838 return loop_configure(lo, mode, bdev, &config);
1840 case LOOP_CHANGE_FD:
1841 return loop_change_fd(lo, bdev, arg);
1843 return loop_clr_fd(lo);
1844 case LOOP_SET_STATUS:
1846 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
1847 err = loop_set_status_old(lo, argp);
1850 case LOOP_GET_STATUS:
1851 return loop_get_status_old(lo, argp);
1852 case LOOP_SET_STATUS64:
1854 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) {
1855 err = loop_set_status64(lo, argp);
1858 case LOOP_GET_STATUS64:
1859 return loop_get_status64(lo, argp);
1860 case LOOP_SET_CAPACITY:
1861 case LOOP_SET_DIRECT_IO:
1862 case LOOP_SET_BLOCK_SIZE:
1863 if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN))
1867 err = lo_simple_ioctl(lo, cmd, arg);
1874 #ifdef CONFIG_COMPAT
1875 struct compat_loop_info {
1876 compat_int_t lo_number; /* ioctl r/o */
1877 compat_dev_t lo_device; /* ioctl r/o */
1878 compat_ulong_t lo_inode; /* ioctl r/o */
1879 compat_dev_t lo_rdevice; /* ioctl r/o */
1880 compat_int_t lo_offset;
1881 compat_int_t lo_encrypt_type;
1882 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1883 compat_int_t lo_flags; /* ioctl r/o */
1884 char lo_name[LO_NAME_SIZE];
1885 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1886 compat_ulong_t lo_init[2];
1891 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1892 * - noinlined to reduce stack space usage in main part of driver
1895 loop_info64_from_compat(const struct compat_loop_info __user *arg,
1896 struct loop_info64 *info64)
1898 struct compat_loop_info info;
1900 if (copy_from_user(&info, arg, sizeof(info)))
1903 memset(info64, 0, sizeof(*info64));
1904 info64->lo_number = info.lo_number;
1905 info64->lo_device = info.lo_device;
1906 info64->lo_inode = info.lo_inode;
1907 info64->lo_rdevice = info.lo_rdevice;
1908 info64->lo_offset = info.lo_offset;
1909 info64->lo_sizelimit = 0;
1910 info64->lo_encrypt_type = info.lo_encrypt_type;
1911 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1912 info64->lo_flags = info.lo_flags;
1913 info64->lo_init[0] = info.lo_init[0];
1914 info64->lo_init[1] = info.lo_init[1];
1915 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1916 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1918 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1919 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1924 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1925 * - noinlined to reduce stack space usage in main part of driver
1928 loop_info64_to_compat(const struct loop_info64 *info64,
1929 struct compat_loop_info __user *arg)
1931 struct compat_loop_info info;
1933 memset(&info, 0, sizeof(info));
1934 info.lo_number = info64->lo_number;
1935 info.lo_device = info64->lo_device;
1936 info.lo_inode = info64->lo_inode;
1937 info.lo_rdevice = info64->lo_rdevice;
1938 info.lo_offset = info64->lo_offset;
1939 info.lo_encrypt_type = info64->lo_encrypt_type;
1940 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1941 info.lo_flags = info64->lo_flags;
1942 info.lo_init[0] = info64->lo_init[0];
1943 info.lo_init[1] = info64->lo_init[1];
1944 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1945 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1947 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1948 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1950 /* error in case values were truncated */
1951 if (info.lo_device != info64->lo_device ||
1952 info.lo_rdevice != info64->lo_rdevice ||
1953 info.lo_inode != info64->lo_inode ||
1954 info.lo_offset != info64->lo_offset ||
1955 info.lo_init[0] != info64->lo_init[0] ||
1956 info.lo_init[1] != info64->lo_init[1])
1959 if (copy_to_user(arg, &info, sizeof(info)))
1965 loop_set_status_compat(struct loop_device *lo,
1966 const struct compat_loop_info __user *arg)
1968 struct loop_info64 info64;
1971 ret = loop_info64_from_compat(arg, &info64);
1974 return loop_set_status(lo, &info64);
1978 loop_get_status_compat(struct loop_device *lo,
1979 struct compat_loop_info __user *arg)
1981 struct loop_info64 info64;
1986 err = loop_get_status(lo, &info64);
1988 err = loop_info64_to_compat(&info64, arg);
1992 static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1993 unsigned int cmd, unsigned long arg)
1995 struct loop_device *lo = bdev->bd_disk->private_data;
1999 case LOOP_SET_STATUS:
2000 err = loop_set_status_compat(lo,
2001 (const struct compat_loop_info __user *)arg);
2003 case LOOP_GET_STATUS:
2004 err = loop_get_status_compat(lo,
2005 (struct compat_loop_info __user *)arg);
2007 case LOOP_SET_CAPACITY:
2009 case LOOP_GET_STATUS64:
2010 case LOOP_SET_STATUS64:
2011 case LOOP_CONFIGURE:
2012 arg = (unsigned long) compat_ptr(arg);
2015 case LOOP_CHANGE_FD:
2016 case LOOP_SET_BLOCK_SIZE:
2017 case LOOP_SET_DIRECT_IO:
2018 err = lo_ioctl(bdev, mode, cmd, arg);
2028 static int lo_open(struct block_device *bdev, fmode_t mode)
2030 struct loop_device *lo = bdev->bd_disk->private_data;
2033 err = mutex_lock_killable(&lo->lo_mutex);
2036 if (lo->lo_state == Lo_deleting)
2039 atomic_inc(&lo->lo_refcnt);
2040 mutex_unlock(&lo->lo_mutex);
2044 static void lo_release(struct gendisk *disk, fmode_t mode)
2046 struct loop_device *lo = disk->private_data;
2048 mutex_lock(&lo->lo_mutex);
2049 if (atomic_dec_return(&lo->lo_refcnt))
2052 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
2053 if (lo->lo_state != Lo_bound)
2055 lo->lo_state = Lo_rundown;
2056 mutex_unlock(&lo->lo_mutex);
2058 * In autoclear mode, stop the loop thread
2059 * and remove configuration after last close.
2061 __loop_clr_fd(lo, true);
2063 } else if (lo->lo_state == Lo_bound) {
2065 * Otherwise keep thread (if running) and config,
2066 * but flush possible ongoing bios in thread.
2068 blk_mq_freeze_queue(lo->lo_queue);
2069 blk_mq_unfreeze_queue(lo->lo_queue);
2073 mutex_unlock(&lo->lo_mutex);
2076 static const struct block_device_operations lo_fops = {
2077 .owner = THIS_MODULE,
2079 .release = lo_release,
2081 #ifdef CONFIG_COMPAT
2082 .compat_ioctl = lo_compat_ioctl,
2087 * And now the modules code and kernel interface.
2089 static int max_loop;
2090 module_param(max_loop, int, 0444);
2091 MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
2092 module_param(max_part, int, 0444);
2093 MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
2094 MODULE_LICENSE("GPL");
2095 MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
2097 int loop_register_transfer(struct loop_func_table *funcs)
2099 unsigned int n = funcs->number;
2101 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
2103 xfer_funcs[n] = funcs;
2107 int loop_unregister_transfer(int number)
2109 unsigned int n = number;
2110 struct loop_func_table *xfer;
2112 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
2115 * This function is called from only cleanup_cryptoloop().
2116 * Given that each loop device that has a transfer enabled holds a
2117 * reference to the module implementing it we should never get here
2118 * with a transfer that is set (unless forced module unloading is
2119 * requested). Thus, check module's refcount and warn if this is
2120 * not a clean unloading.
2122 #ifdef CONFIG_MODULE_UNLOAD
2123 if (xfer->owner && module_refcount(xfer->owner) != -1)
2124 pr_err("Danger! Unregistering an in use transfer function.\n");
2127 xfer_funcs[n] = NULL;
2131 EXPORT_SYMBOL(loop_register_transfer);
2132 EXPORT_SYMBOL(loop_unregister_transfer);
2134 static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
2135 const struct blk_mq_queue_data *bd)
2137 struct request *rq = bd->rq;
2138 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
2139 struct loop_device *lo = rq->q->queuedata;
2141 blk_mq_start_request(rq);
2143 if (lo->lo_state != Lo_bound)
2144 return BLK_STS_IOERR;
2146 switch (req_op(rq)) {
2148 case REQ_OP_DISCARD:
2149 case REQ_OP_WRITE_ZEROES:
2150 cmd->use_aio = false;
2153 cmd->use_aio = lo->use_dio;
2157 /* always use the first bio's css */
2158 cmd->blkcg_css = NULL;
2159 cmd->memcg_css = NULL;
2160 #ifdef CONFIG_BLK_CGROUP
2161 if (rq->bio && rq->bio->bi_blkg) {
2162 cmd->blkcg_css = &bio_blkcg(rq->bio)->css;
2165 cgroup_get_e_css(cmd->blkcg_css->cgroup,
2166 &memory_cgrp_subsys);
2170 loop_queue_work(lo, cmd);
2175 static void loop_handle_cmd(struct loop_cmd *cmd)
2177 struct request *rq = blk_mq_rq_from_pdu(cmd);
2178 const bool write = op_is_write(req_op(rq));
2179 struct loop_device *lo = rq->q->queuedata;
2181 struct mem_cgroup *old_memcg = NULL;
2183 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) {
2189 kthread_associate_blkcg(cmd->blkcg_css);
2191 old_memcg = set_active_memcg(
2192 mem_cgroup_from_css(cmd->memcg_css));
2194 ret = do_req_filebacked(lo, rq);
2197 kthread_associate_blkcg(NULL);
2199 if (cmd->memcg_css) {
2200 set_active_memcg(old_memcg);
2201 css_put(cmd->memcg_css);
2204 /* complete non-aio request */
2205 if (!cmd->use_aio || ret) {
2206 if (ret == -EOPNOTSUPP)
2209 cmd->ret = ret ? -EIO : 0;
2210 if (likely(!blk_should_fake_timeout(rq->q)))
2211 blk_mq_complete_request(rq);
2215 static void loop_set_timer(struct loop_device *lo)
2217 timer_reduce(&lo->timer, jiffies + LOOP_IDLE_WORKER_TIMEOUT);
2220 static void loop_process_work(struct loop_worker *worker,
2221 struct list_head *cmd_list, struct loop_device *lo)
2223 int orig_flags = current->flags;
2224 struct loop_cmd *cmd;
2226 current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
2227 spin_lock_irq(&lo->lo_work_lock);
2228 while (!list_empty(cmd_list)) {
2230 cmd_list->next, struct loop_cmd, list_entry);
2231 list_del(cmd_list->next);
2232 spin_unlock_irq(&lo->lo_work_lock);
2234 loop_handle_cmd(cmd);
2237 spin_lock_irq(&lo->lo_work_lock);
2241 * We only add to the idle list if there are no pending cmds
2242 * *and* the worker will not run again which ensures that it
2243 * is safe to free any worker on the idle list
2245 if (worker && !work_pending(&worker->work)) {
2246 worker->last_ran_at = jiffies;
2247 list_add_tail(&worker->idle_list, &lo->idle_worker_list);
2250 spin_unlock_irq(&lo->lo_work_lock);
2251 current->flags = orig_flags;
2254 static void loop_workfn(struct work_struct *work)
2256 struct loop_worker *worker =
2257 container_of(work, struct loop_worker, work);
2258 loop_process_work(worker, &worker->cmd_list, worker->lo);
2261 static void loop_rootcg_workfn(struct work_struct *work)
2263 struct loop_device *lo =
2264 container_of(work, struct loop_device, rootcg_work);
2265 loop_process_work(NULL, &lo->rootcg_cmd_list, lo);
2268 static void loop_free_idle_workers(struct timer_list *timer)
2270 struct loop_device *lo = container_of(timer, struct loop_device, timer);
2271 struct loop_worker *pos, *worker;
2273 spin_lock_irq(&lo->lo_work_lock);
2274 list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
2276 if (time_is_after_jiffies(worker->last_ran_at +
2277 LOOP_IDLE_WORKER_TIMEOUT))
2279 list_del(&worker->idle_list);
2280 rb_erase(&worker->rb_node, &lo->worker_tree);
2281 css_put(worker->blkcg_css);
2284 if (!list_empty(&lo->idle_worker_list))
2286 spin_unlock_irq(&lo->lo_work_lock);
2289 static const struct blk_mq_ops loop_mq_ops = {
2290 .queue_rq = loop_queue_rq,
2291 .complete = lo_complete_rq,
2294 static int loop_add(int i)
2296 struct loop_device *lo;
2297 struct gendisk *disk;
2301 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
2304 lo->lo_state = Lo_unbound;
2306 err = mutex_lock_killable(&loop_ctl_mutex);
2310 /* allocate id, if @id >= 0, we're requesting that specific id */
2312 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
2316 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
2318 mutex_unlock(&loop_ctl_mutex);
2324 lo->tag_set.ops = &loop_mq_ops;
2325 lo->tag_set.nr_hw_queues = 1;
2326 lo->tag_set.queue_depth = 128;
2327 lo->tag_set.numa_node = NUMA_NO_NODE;
2328 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
2329 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING |
2330 BLK_MQ_F_NO_SCHED_BY_DEFAULT;
2331 lo->tag_set.driver_data = lo;
2333 err = blk_mq_alloc_tag_set(&lo->tag_set);
2337 disk = lo->lo_disk = blk_mq_alloc_disk(&lo->tag_set, lo);
2339 err = PTR_ERR(disk);
2340 goto out_cleanup_tags;
2342 lo->lo_queue = lo->lo_disk->queue;
2344 blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
2347 * By default, we do buffer IO, so it doesn't make sense to enable
2348 * merge because the I/O submitted to backing file is handled page by
2349 * page. For directio mode, merge does help to dispatch bigger request
2350 * to underlayer disk. We will enable merge once directio is enabled.
2352 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue);
2355 * Disable partition scanning by default. The in-kernel partition
2356 * scanning can be requested individually per-device during its
2357 * setup. Userspace can always add and remove partitions from all
2358 * devices. The needed partition minors are allocated from the
2359 * extended minor space, the main loop device numbers will continue
2360 * to match the loop minors, regardless of the number of partitions
2363 * If max_part is given, partition scanning is globally enabled for
2364 * all loop devices. The minors for the main loop devices will be
2365 * multiples of max_part.
2367 * Note: Global-for-all-devices, set-only-at-init, read-only module
2368 * parameteters like 'max_loop' and 'max_part' make things needlessly
2369 * complicated, are too static, inflexible and may surprise
2370 * userspace tools. Parameters like this in general should be avoided.
2373 disk->flags |= GENHD_FL_NO_PART_SCAN;
2374 disk->flags |= GENHD_FL_EXT_DEVT;
2375 atomic_set(&lo->lo_refcnt, 0);
2376 mutex_init(&lo->lo_mutex);
2378 spin_lock_init(&lo->lo_lock);
2379 spin_lock_init(&lo->lo_work_lock);
2380 disk->major = LOOP_MAJOR;
2381 disk->first_minor = i << part_shift;
2382 disk->minors = 1 << part_shift;
2383 disk->fops = &lo_fops;
2384 disk->private_data = lo;
2385 disk->queue = lo->lo_queue;
2386 disk->events = DISK_EVENT_MEDIA_CHANGE;
2387 disk->event_flags = DISK_EVENT_FLAG_UEVENT;
2388 sprintf(disk->disk_name, "loop%d", i);
2389 /* Make this loop device reachable from pathname. */
2391 /* Show this loop device. */
2392 mutex_lock(&loop_ctl_mutex);
2393 lo->idr_visible = true;
2394 mutex_unlock(&loop_ctl_mutex);
2398 blk_mq_free_tag_set(&lo->tag_set);
2400 mutex_lock(&loop_ctl_mutex);
2401 idr_remove(&loop_index_idr, i);
2402 mutex_unlock(&loop_ctl_mutex);
2409 static void loop_remove(struct loop_device *lo)
2411 /* Make this loop device unreachable from pathname. */
2412 del_gendisk(lo->lo_disk);
2413 blk_cleanup_disk(lo->lo_disk);
2414 blk_mq_free_tag_set(&lo->tag_set);
2415 mutex_lock(&loop_ctl_mutex);
2416 idr_remove(&loop_index_idr, lo->lo_number);
2417 mutex_unlock(&loop_ctl_mutex);
2418 /* There is no route which can find this loop device. */
2419 mutex_destroy(&lo->lo_mutex);
2423 static void loop_probe(dev_t dev)
2425 int idx = MINOR(dev) >> part_shift;
2427 if (max_loop && idx >= max_loop)
2432 static int loop_control_remove(int idx)
2434 struct loop_device *lo;
2438 pr_warn_once("deleting an unspecified loop device is not supported.\n");
2442 /* Hide this loop device for serialization. */
2443 ret = mutex_lock_killable(&loop_ctl_mutex);
2446 lo = idr_find(&loop_index_idr, idx);
2447 if (!lo || !lo->idr_visible)
2450 lo->idr_visible = false;
2451 mutex_unlock(&loop_ctl_mutex);
2455 /* Check whether this loop device can be removed. */
2456 ret = mutex_lock_killable(&lo->lo_mutex);
2459 if (lo->lo_state != Lo_unbound ||
2460 atomic_read(&lo->lo_refcnt) > 0) {
2461 mutex_unlock(&lo->lo_mutex);
2465 /* Mark this loop device no longer open()-able. */
2466 lo->lo_state = Lo_deleting;
2467 mutex_unlock(&lo->lo_mutex);
2473 /* Show this loop device again. */
2474 mutex_lock(&loop_ctl_mutex);
2475 lo->idr_visible = true;
2476 mutex_unlock(&loop_ctl_mutex);
2480 static int loop_control_get_free(int idx)
2482 struct loop_device *lo;
2485 ret = mutex_lock_killable(&loop_ctl_mutex);
2488 idr_for_each_entry(&loop_index_idr, lo, id) {
2489 /* Hitting a race results in creating a new loop device which is harmless. */
2490 if (lo->idr_visible && data_race(lo->lo_state) == Lo_unbound)
2493 mutex_unlock(&loop_ctl_mutex);
2494 return loop_add(-1);
2496 mutex_unlock(&loop_ctl_mutex);
2500 static long loop_control_ioctl(struct file *file, unsigned int cmd,
2505 return loop_add(parm);
2506 case LOOP_CTL_REMOVE:
2507 return loop_control_remove(parm);
2508 case LOOP_CTL_GET_FREE:
2509 return loop_control_get_free(parm);
2515 static const struct file_operations loop_ctl_fops = {
2516 .open = nonseekable_open,
2517 .unlocked_ioctl = loop_control_ioctl,
2518 .compat_ioctl = loop_control_ioctl,
2519 .owner = THIS_MODULE,
2520 .llseek = noop_llseek,
2523 static struct miscdevice loop_misc = {
2524 .minor = LOOP_CTRL_MINOR,
2525 .name = "loop-control",
2526 .fops = &loop_ctl_fops,
2529 MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
2530 MODULE_ALIAS("devname:loop-control");
2532 static int __init loop_init(void)
2539 part_shift = fls(max_part);
2542 * Adjust max_part according to part_shift as it is exported
2543 * to user space so that user can decide correct minor number
2544 * if [s]he want to create more devices.
2546 * Note that -1 is required because partition 0 is reserved
2547 * for the whole disk.
2549 max_part = (1UL << part_shift) - 1;
2552 if ((1UL << part_shift) > DISK_MAX_PARTS) {
2557 if (max_loop > 1UL << (MINORBITS - part_shift)) {
2563 * If max_loop is specified, create that many devices upfront.
2564 * This also becomes a hard limit. If max_loop is not specified,
2565 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
2566 * init time. Loop devices can be requested on-demand with the
2567 * /dev/loop-control interface, or be instantiated by accessing
2568 * a 'dead' device node.
2573 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
2575 err = misc_register(&loop_misc);
2580 if (__register_blkdev(LOOP_MAJOR, "loop", loop_probe)) {
2585 /* pre-create number of devices given by config or max_loop */
2586 for (i = 0; i < nr; i++)
2589 printk(KERN_INFO "loop: module loaded\n");
2593 misc_deregister(&loop_misc);
2598 static void __exit loop_exit(void)
2600 struct loop_device *lo;
2603 unregister_blkdev(LOOP_MAJOR, "loop");
2604 misc_deregister(&loop_misc);
2607 * There is no need to use loop_ctl_mutex here, for nobody else can
2608 * access loop_index_idr when this module is unloading (unless forced
2609 * module unloading is requested). If this is not a clean unloading,
2610 * we have no means to avoid kernel crash.
2612 idr_for_each_entry(&loop_index_idr, lo, id)
2615 idr_destroy(&loop_index_idr);
2618 module_init(loop_init);
2619 module_exit(loop_exit);
2622 static int __init max_loop_setup(char *str)
2624 max_loop = simple_strtol(str, NULL, 0);
2628 __setup("max_loop=", max_loop_setup);