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/file.h>
57 #include <linux/stat.h>
58 #include <linux/errno.h>
59 #include <linux/major.h>
60 #include <linux/wait.h>
61 #include <linux/blkdev.h>
62 #include <linux/blkpg.h>
63 #include <linux/init.h>
64 #include <linux/swap.h>
65 #include <linux/slab.h>
66 #include <linux/compat.h>
67 #include <linux/suspend.h>
68 #include <linux/freezer.h>
69 #include <linux/mutex.h>
70 #include <linux/writeback.h>
71 #include <linux/completion.h>
72 #include <linux/highmem.h>
73 #include <linux/kthread.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>
81 #include <asm/uaccess.h>
83 static DEFINE_IDR(loop_index_idr);
84 static DEFINE_MUTEX(loop_index_mutex);
87 static int part_shift;
89 static struct workqueue_struct *loop_wq;
94 static int transfer_none(struct loop_device *lo, int cmd,
95 struct page *raw_page, unsigned raw_off,
96 struct page *loop_page, unsigned loop_off,
97 int size, sector_t real_block)
99 char *raw_buf = kmap_atomic(raw_page) + raw_off;
100 char *loop_buf = kmap_atomic(loop_page) + loop_off;
103 memcpy(loop_buf, raw_buf, size);
105 memcpy(raw_buf, loop_buf, size);
107 kunmap_atomic(loop_buf);
108 kunmap_atomic(raw_buf);
113 static int transfer_xor(struct loop_device *lo, int cmd,
114 struct page *raw_page, unsigned raw_off,
115 struct page *loop_page, unsigned loop_off,
116 int size, sector_t real_block)
118 char *raw_buf = kmap_atomic(raw_page) + raw_off;
119 char *loop_buf = kmap_atomic(loop_page) + loop_off;
120 char *in, *out, *key;
131 key = lo->lo_encrypt_key;
132 keysize = lo->lo_encrypt_key_size;
133 for (i = 0; i < size; i++)
134 *out++ = *in++ ^ key[(i & 511) % keysize];
136 kunmap_atomic(loop_buf);
137 kunmap_atomic(raw_buf);
142 static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
144 if (unlikely(info->lo_encrypt_key_size <= 0))
149 static struct loop_func_table none_funcs = {
150 .number = LO_CRYPT_NONE,
151 .transfer = transfer_none,
154 static struct loop_func_table xor_funcs = {
155 .number = LO_CRYPT_XOR,
156 .transfer = transfer_xor,
160 /* xfer_funcs[0] is special - its release function is never called */
161 static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
166 static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
170 /* Compute loopsize in bytes */
171 loopsize = i_size_read(file->f_mapping->host);
174 /* offset is beyond i_size, weird but possible */
178 if (sizelimit > 0 && sizelimit < loopsize)
179 loopsize = sizelimit;
181 * Unfortunately, if we want to do I/O on the device,
182 * the number of 512-byte sectors has to fit into a sector_t.
184 return loopsize >> 9;
187 static loff_t get_loop_size(struct loop_device *lo, struct file *file)
189 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
193 figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
195 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
196 sector_t x = (sector_t)size;
197 struct block_device *bdev = lo->lo_device;
199 if (unlikely((loff_t)x != size))
201 if (lo->lo_offset != offset)
202 lo->lo_offset = offset;
203 if (lo->lo_sizelimit != sizelimit)
204 lo->lo_sizelimit = sizelimit;
205 set_capacity(lo->lo_disk, x);
206 bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9);
207 /* let user-space know about the new size */
208 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
213 lo_do_transfer(struct loop_device *lo, int cmd,
214 struct page *rpage, unsigned roffs,
215 struct page *lpage, unsigned loffs,
216 int size, sector_t rblock)
218 if (unlikely(!lo->transfer))
221 return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
225 * __do_lo_send_write - helper for writing data to a loop device
227 * This helper just factors out common code between do_lo_send_direct_write()
228 * and do_lo_send_write().
230 static int __do_lo_send_write(struct file *file,
231 u8 *buf, const int len, loff_t pos)
233 struct kvec kvec = {.iov_base = buf, .iov_len = len};
234 struct iov_iter from;
237 iov_iter_kvec(&from, ITER_KVEC | WRITE, &kvec, 1, len);
239 file_start_write(file);
240 bw = vfs_iter_write(file, &from, &pos);
241 file_end_write(file);
242 if (likely(bw == len))
244 printk_ratelimited(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
245 (unsigned long long)pos, len);
252 * do_lo_send_direct_write - helper for writing data to a loop device
254 * This is the fast, non-transforming version that does not need double
257 static int do_lo_send_direct_write(struct loop_device *lo,
258 struct bio_vec *bvec, loff_t pos, struct page *page)
260 ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
261 kmap(bvec->bv_page) + bvec->bv_offset,
263 kunmap(bvec->bv_page);
269 * do_lo_send_write - helper for writing data to a loop device
271 * This is the slow, transforming version that needs to double buffer the
272 * data as it cannot do the transformations in place without having direct
273 * access to the destination pages of the backing file.
275 static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
276 loff_t pos, struct page *page)
278 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
279 bvec->bv_offset, bvec->bv_len, pos >> 9);
281 return __do_lo_send_write(lo->lo_backing_file,
282 page_address(page), bvec->bv_len,
284 printk_ratelimited(KERN_ERR "loop: Transfer error at byte offset %llu, "
285 "length %i.\n", (unsigned long long)pos, bvec->bv_len);
291 static int lo_send(struct loop_device *lo, struct request *rq, loff_t pos)
293 int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
296 struct req_iterator iter;
297 struct page *page = NULL;
300 if (lo->transfer != transfer_none) {
301 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
305 do_lo_send = do_lo_send_write;
307 do_lo_send = do_lo_send_direct_write;
310 rq_for_each_segment(bvec, rq, iter) {
311 ret = do_lo_send(lo, &bvec, pos, page);
323 printk_ratelimited(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
328 struct lo_read_data {
329 struct loop_device *lo;
336 lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
337 struct splice_desc *sd)
339 struct lo_read_data *p = sd->u.data;
340 struct loop_device *lo = p->lo;
341 struct page *page = buf->page;
345 IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
351 if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
352 printk_ratelimited(KERN_ERR "loop: transfer error block %ld\n",
357 flush_dcache_page(p->page);
366 lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
368 return __splice_from_pipe(pipe, sd, lo_splice_actor);
372 do_lo_receive(struct loop_device *lo,
373 struct bio_vec *bvec, int bsize, loff_t pos)
375 struct lo_read_data cookie;
376 struct splice_desc sd;
381 cookie.page = bvec->bv_page;
382 cookie.offset = bvec->bv_offset;
383 cookie.bsize = bsize;
386 sd.total_len = bvec->bv_len;
391 file = lo->lo_backing_file;
392 retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
398 lo_receive(struct loop_device *lo, struct request *rq, int bsize, loff_t pos)
401 struct req_iterator iter;
404 rq_for_each_segment(bvec, rq, iter) {
405 s = do_lo_receive(lo, &bvec, bsize, pos);
409 if (s != bvec.bv_len) {
412 __rq_for_each_bio(bio, rq)
421 static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos)
424 * We use punch hole to reclaim the free space used by the
425 * image a.k.a. discard. However we do not support discard if
426 * encryption is enabled, because it may give an attacker
427 * useful information.
429 struct file *file = lo->lo_backing_file;
430 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
433 if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) {
438 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
439 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
445 static int lo_req_flush(struct loop_device *lo, struct request *rq)
447 struct file *file = lo->lo_backing_file;
448 int ret = vfs_fsync(file, 0);
449 if (unlikely(ret && ret != -EINVAL))
455 static int do_req_filebacked(struct loop_device *lo, struct request *rq)
460 pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
462 if (rq->cmd_flags & REQ_WRITE) {
463 if (rq->cmd_flags & REQ_FLUSH)
464 ret = lo_req_flush(lo, rq);
465 else if (rq->cmd_flags & REQ_DISCARD)
466 ret = lo_discard(lo, rq, pos);
468 ret = lo_send(lo, rq, pos);
470 ret = lo_receive(lo, rq, lo->lo_blocksize, pos);
475 struct switch_request {
477 struct completion wait;
481 * Do the actual switch; called from the BIO completion routine
483 static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
485 struct file *file = p->file;
486 struct file *old_file = lo->lo_backing_file;
487 struct address_space *mapping;
489 /* if no new file, only flush of queued bios requested */
493 mapping = file->f_mapping;
494 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
495 lo->lo_backing_file = file;
496 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
497 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
498 lo->old_gfp_mask = mapping_gfp_mask(mapping);
499 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
503 * loop_switch performs the hard work of switching a backing store.
504 * First it needs to flush existing IO, it does this by sending a magic
505 * BIO down the pipe. The completion of this BIO does the actual switch.
507 static int loop_switch(struct loop_device *lo, struct file *file)
509 struct switch_request w;
513 /* freeze queue and wait for completion of scheduled requests */
514 blk_mq_freeze_queue(lo->lo_queue);
516 /* do the switch action */
517 do_loop_switch(lo, &w);
520 blk_mq_unfreeze_queue(lo->lo_queue);
526 * Helper to flush the IOs in loop, but keeping loop thread running
528 static int loop_flush(struct loop_device *lo)
530 return loop_switch(lo, NULL);
534 * loop_change_fd switched the backing store of a loopback device to
535 * a new file. This is useful for operating system installers to free up
536 * the original file and in High Availability environments to switch to
537 * an alternative location for the content in case of server meltdown.
538 * This can only work if the loop device is used read-only, and if the
539 * new backing store is the same size and type as the old backing store.
541 static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
544 struct file *file, *old_file;
549 if (lo->lo_state != Lo_bound)
552 /* the loop device has to be read-only */
554 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
562 inode = file->f_mapping->host;
563 old_file = lo->lo_backing_file;
567 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
570 /* size of the new backing store needs to be the same */
571 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
575 error = loop_switch(lo, file);
580 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
581 ioctl_by_bdev(bdev, BLKRRPART, 0);
590 static inline int is_loop_device(struct file *file)
592 struct inode *i = file->f_mapping->host;
594 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
597 /* loop sysfs attributes */
599 static ssize_t loop_attr_show(struct device *dev, char *page,
600 ssize_t (*callback)(struct loop_device *, char *))
602 struct gendisk *disk = dev_to_disk(dev);
603 struct loop_device *lo = disk->private_data;
605 return callback(lo, page);
608 #define LOOP_ATTR_RO(_name) \
609 static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
610 static ssize_t loop_attr_do_show_##_name(struct device *d, \
611 struct device_attribute *attr, char *b) \
613 return loop_attr_show(d, b, loop_attr_##_name##_show); \
615 static struct device_attribute loop_attr_##_name = \
616 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
618 static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
623 spin_lock_irq(&lo->lo_lock);
624 if (lo->lo_backing_file)
625 p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
626 spin_unlock_irq(&lo->lo_lock);
628 if (IS_ERR_OR_NULL(p))
632 memmove(buf, p, ret);
640 static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
642 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
645 static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
647 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
650 static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
652 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
654 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
657 static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
659 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
661 return sprintf(buf, "%s\n", partscan ? "1" : "0");
664 LOOP_ATTR_RO(backing_file);
665 LOOP_ATTR_RO(offset);
666 LOOP_ATTR_RO(sizelimit);
667 LOOP_ATTR_RO(autoclear);
668 LOOP_ATTR_RO(partscan);
670 static struct attribute *loop_attrs[] = {
671 &loop_attr_backing_file.attr,
672 &loop_attr_offset.attr,
673 &loop_attr_sizelimit.attr,
674 &loop_attr_autoclear.attr,
675 &loop_attr_partscan.attr,
679 static struct attribute_group loop_attribute_group = {
684 static int loop_sysfs_init(struct loop_device *lo)
686 return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
687 &loop_attribute_group);
690 static void loop_sysfs_exit(struct loop_device *lo)
692 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
693 &loop_attribute_group);
696 static void loop_config_discard(struct loop_device *lo)
698 struct file *file = lo->lo_backing_file;
699 struct inode *inode = file->f_mapping->host;
700 struct request_queue *q = lo->lo_queue;
703 * We use punch hole to reclaim the free space used by the
704 * image a.k.a. discard. However we do not support discard if
705 * encryption is enabled, because it may give an attacker
706 * useful information.
708 if ((!file->f_op->fallocate) ||
709 lo->lo_encrypt_key_size) {
710 q->limits.discard_granularity = 0;
711 q->limits.discard_alignment = 0;
712 q->limits.max_discard_sectors = 0;
713 q->limits.discard_zeroes_data = 0;
714 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
718 q->limits.discard_granularity = inode->i_sb->s_blocksize;
719 q->limits.discard_alignment = 0;
720 q->limits.max_discard_sectors = UINT_MAX >> 9;
721 q->limits.discard_zeroes_data = 1;
722 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
725 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
726 struct block_device *bdev, unsigned int arg)
728 struct file *file, *f;
730 struct address_space *mapping;
731 unsigned lo_blocksize;
736 /* This is safe, since we have a reference from open(). */
737 __module_get(THIS_MODULE);
745 if (lo->lo_state != Lo_unbound)
748 /* Avoid recursion */
750 while (is_loop_device(f)) {
751 struct loop_device *l;
753 if (f->f_mapping->host->i_bdev == bdev)
756 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
757 if (l->lo_state == Lo_unbound) {
761 f = l->lo_backing_file;
764 mapping = file->f_mapping;
765 inode = mapping->host;
768 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
771 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
772 !file->f_op->write_iter)
773 lo_flags |= LO_FLAGS_READ_ONLY;
775 lo_blocksize = S_ISBLK(inode->i_mode) ?
776 inode->i_bdev->bd_block_size : PAGE_SIZE;
779 size = get_loop_size(lo, file);
780 if ((loff_t)(sector_t)size != size)
785 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
787 lo->lo_blocksize = lo_blocksize;
788 lo->lo_device = bdev;
789 lo->lo_flags = lo_flags;
790 lo->lo_backing_file = file;
791 lo->transfer = transfer_none;
793 lo->lo_sizelimit = 0;
794 lo->old_gfp_mask = mapping_gfp_mask(mapping);
795 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
797 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
798 blk_queue_flush(lo->lo_queue, REQ_FLUSH);
800 set_capacity(lo->lo_disk, size);
801 bd_set_size(bdev, size << 9);
803 /* let user-space know about the new size */
804 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
806 set_blocksize(bdev, lo_blocksize);
808 lo->lo_state = Lo_bound;
810 lo->lo_flags |= LO_FLAGS_PARTSCAN;
811 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
812 ioctl_by_bdev(bdev, BLKRRPART, 0);
814 /* Grab the block_device to prevent its destruction after we
815 * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev).
823 /* This is safe: open() is still holding a reference. */
824 module_put(THIS_MODULE);
829 loop_release_xfer(struct loop_device *lo)
832 struct loop_func_table *xfer = lo->lo_encryption;
836 err = xfer->release(lo);
838 lo->lo_encryption = NULL;
839 module_put(xfer->owner);
845 loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
846 const struct loop_info64 *i)
851 struct module *owner = xfer->owner;
853 if (!try_module_get(owner))
856 err = xfer->init(lo, i);
860 lo->lo_encryption = xfer;
865 static int loop_clr_fd(struct loop_device *lo)
867 struct file *filp = lo->lo_backing_file;
868 gfp_t gfp = lo->old_gfp_mask;
869 struct block_device *bdev = lo->lo_device;
871 if (lo->lo_state != Lo_bound)
875 * If we've explicitly asked to tear down the loop device,
876 * and it has an elevated reference count, set it for auto-teardown when
877 * the last reference goes away. This stops $!~#$@ udev from
878 * preventing teardown because it decided that it needs to run blkid on
879 * the loopback device whenever they appear. xfstests is notorious for
880 * failing tests because blkid via udev races with a losetup
881 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
882 * command to fail with EBUSY.
884 if (lo->lo_refcnt > 1) {
885 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
886 mutex_unlock(&lo->lo_ctl_mutex);
893 spin_lock_irq(&lo->lo_lock);
894 lo->lo_state = Lo_rundown;
895 lo->lo_backing_file = NULL;
896 spin_unlock_irq(&lo->lo_lock);
898 loop_release_xfer(lo);
901 lo->lo_device = NULL;
902 lo->lo_encryption = NULL;
904 lo->lo_sizelimit = 0;
905 lo->lo_encrypt_key_size = 0;
906 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
907 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
908 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
911 invalidate_bdev(bdev);
913 set_capacity(lo->lo_disk, 0);
916 bd_set_size(bdev, 0);
917 /* let user-space know about this change */
918 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
920 mapping_set_gfp_mask(filp->f_mapping, gfp);
921 lo->lo_state = Lo_unbound;
922 /* This is safe: open() is still holding a reference. */
923 module_put(THIS_MODULE);
924 if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
925 ioctl_by_bdev(bdev, BLKRRPART, 0);
928 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
929 mutex_unlock(&lo->lo_ctl_mutex);
931 * Need not hold lo_ctl_mutex to fput backing file.
932 * Calling fput holding lo_ctl_mutex triggers a circular
933 * lock dependency possibility warning as fput can take
934 * bd_mutex which is usually taken before lo_ctl_mutex.
941 loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
944 struct loop_func_table *xfer;
945 kuid_t uid = current_uid();
947 if (lo->lo_encrypt_key_size &&
948 !uid_eq(lo->lo_key_owner, uid) &&
949 !capable(CAP_SYS_ADMIN))
951 if (lo->lo_state != Lo_bound)
953 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
956 err = loop_release_xfer(lo);
960 if (info->lo_encrypt_type) {
961 unsigned int type = info->lo_encrypt_type;
963 if (type >= MAX_LO_CRYPT)
965 xfer = xfer_funcs[type];
971 err = loop_init_xfer(lo, xfer, info);
975 if (lo->lo_offset != info->lo_offset ||
976 lo->lo_sizelimit != info->lo_sizelimit)
977 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
980 loop_config_discard(lo);
982 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
983 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
984 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
985 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
989 lo->transfer = xfer->transfer;
990 lo->ioctl = xfer->ioctl;
992 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
993 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
994 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
996 if ((info->lo_flags & LO_FLAGS_PARTSCAN) &&
997 !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
998 lo->lo_flags |= LO_FLAGS_PARTSCAN;
999 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1000 ioctl_by_bdev(lo->lo_device, BLKRRPART, 0);
1003 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1004 lo->lo_init[0] = info->lo_init[0];
1005 lo->lo_init[1] = info->lo_init[1];
1006 if (info->lo_encrypt_key_size) {
1007 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1008 info->lo_encrypt_key_size);
1009 lo->lo_key_owner = uid;
1016 loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1018 struct file *file = lo->lo_backing_file;
1022 if (lo->lo_state != Lo_bound)
1024 error = vfs_getattr(&file->f_path, &stat);
1027 memset(info, 0, sizeof(*info));
1028 info->lo_number = lo->lo_number;
1029 info->lo_device = huge_encode_dev(stat.dev);
1030 info->lo_inode = stat.ino;
1031 info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
1032 info->lo_offset = lo->lo_offset;
1033 info->lo_sizelimit = lo->lo_sizelimit;
1034 info->lo_flags = lo->lo_flags;
1035 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1036 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1037 info->lo_encrypt_type =
1038 lo->lo_encryption ? lo->lo_encryption->number : 0;
1039 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1040 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1041 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1042 lo->lo_encrypt_key_size);
1048 loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1050 memset(info64, 0, sizeof(*info64));
1051 info64->lo_number = info->lo_number;
1052 info64->lo_device = info->lo_device;
1053 info64->lo_inode = info->lo_inode;
1054 info64->lo_rdevice = info->lo_rdevice;
1055 info64->lo_offset = info->lo_offset;
1056 info64->lo_sizelimit = 0;
1057 info64->lo_encrypt_type = info->lo_encrypt_type;
1058 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1059 info64->lo_flags = info->lo_flags;
1060 info64->lo_init[0] = info->lo_init[0];
1061 info64->lo_init[1] = info->lo_init[1];
1062 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1063 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1065 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1066 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1070 loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1072 memset(info, 0, sizeof(*info));
1073 info->lo_number = info64->lo_number;
1074 info->lo_device = info64->lo_device;
1075 info->lo_inode = info64->lo_inode;
1076 info->lo_rdevice = info64->lo_rdevice;
1077 info->lo_offset = info64->lo_offset;
1078 info->lo_encrypt_type = info64->lo_encrypt_type;
1079 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1080 info->lo_flags = info64->lo_flags;
1081 info->lo_init[0] = info64->lo_init[0];
1082 info->lo_init[1] = info64->lo_init[1];
1083 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1084 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1086 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1087 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1089 /* error in case values were truncated */
1090 if (info->lo_device != info64->lo_device ||
1091 info->lo_rdevice != info64->lo_rdevice ||
1092 info->lo_inode != info64->lo_inode ||
1093 info->lo_offset != info64->lo_offset)
1100 loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1102 struct loop_info info;
1103 struct loop_info64 info64;
1105 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1107 loop_info64_from_old(&info, &info64);
1108 return loop_set_status(lo, &info64);
1112 loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1114 struct loop_info64 info64;
1116 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1118 return loop_set_status(lo, &info64);
1122 loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1123 struct loop_info info;
1124 struct loop_info64 info64;
1130 err = loop_get_status(lo, &info64);
1132 err = loop_info64_to_old(&info64, &info);
1133 if (!err && copy_to_user(arg, &info, sizeof(info)))
1140 loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1141 struct loop_info64 info64;
1147 err = loop_get_status(lo, &info64);
1148 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1154 static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
1156 if (unlikely(lo->lo_state != Lo_bound))
1159 return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
1162 static int lo_ioctl(struct block_device *bdev, fmode_t mode,
1163 unsigned int cmd, unsigned long arg)
1165 struct loop_device *lo = bdev->bd_disk->private_data;
1168 mutex_lock_nested(&lo->lo_ctl_mutex, 1);
1171 err = loop_set_fd(lo, mode, bdev, arg);
1173 case LOOP_CHANGE_FD:
1174 err = loop_change_fd(lo, bdev, arg);
1177 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
1178 err = loop_clr_fd(lo);
1182 case LOOP_SET_STATUS:
1184 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1185 err = loop_set_status_old(lo,
1186 (struct loop_info __user *)arg);
1188 case LOOP_GET_STATUS:
1189 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1191 case LOOP_SET_STATUS64:
1193 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1194 err = loop_set_status64(lo,
1195 (struct loop_info64 __user *) arg);
1197 case LOOP_GET_STATUS64:
1198 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1200 case LOOP_SET_CAPACITY:
1202 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1203 err = loop_set_capacity(lo, bdev);
1206 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1208 mutex_unlock(&lo->lo_ctl_mutex);
1214 #ifdef CONFIG_COMPAT
1215 struct compat_loop_info {
1216 compat_int_t lo_number; /* ioctl r/o */
1217 compat_dev_t lo_device; /* ioctl r/o */
1218 compat_ulong_t lo_inode; /* ioctl r/o */
1219 compat_dev_t lo_rdevice; /* ioctl r/o */
1220 compat_int_t lo_offset;
1221 compat_int_t lo_encrypt_type;
1222 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1223 compat_int_t lo_flags; /* ioctl r/o */
1224 char lo_name[LO_NAME_SIZE];
1225 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1226 compat_ulong_t lo_init[2];
1231 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1232 * - noinlined to reduce stack space usage in main part of driver
1235 loop_info64_from_compat(const struct compat_loop_info __user *arg,
1236 struct loop_info64 *info64)
1238 struct compat_loop_info info;
1240 if (copy_from_user(&info, arg, sizeof(info)))
1243 memset(info64, 0, sizeof(*info64));
1244 info64->lo_number = info.lo_number;
1245 info64->lo_device = info.lo_device;
1246 info64->lo_inode = info.lo_inode;
1247 info64->lo_rdevice = info.lo_rdevice;
1248 info64->lo_offset = info.lo_offset;
1249 info64->lo_sizelimit = 0;
1250 info64->lo_encrypt_type = info.lo_encrypt_type;
1251 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1252 info64->lo_flags = info.lo_flags;
1253 info64->lo_init[0] = info.lo_init[0];
1254 info64->lo_init[1] = info.lo_init[1];
1255 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1256 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1258 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1259 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1264 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1265 * - noinlined to reduce stack space usage in main part of driver
1268 loop_info64_to_compat(const struct loop_info64 *info64,
1269 struct compat_loop_info __user *arg)
1271 struct compat_loop_info info;
1273 memset(&info, 0, sizeof(info));
1274 info.lo_number = info64->lo_number;
1275 info.lo_device = info64->lo_device;
1276 info.lo_inode = info64->lo_inode;
1277 info.lo_rdevice = info64->lo_rdevice;
1278 info.lo_offset = info64->lo_offset;
1279 info.lo_encrypt_type = info64->lo_encrypt_type;
1280 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1281 info.lo_flags = info64->lo_flags;
1282 info.lo_init[0] = info64->lo_init[0];
1283 info.lo_init[1] = info64->lo_init[1];
1284 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1285 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1287 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1288 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1290 /* error in case values were truncated */
1291 if (info.lo_device != info64->lo_device ||
1292 info.lo_rdevice != info64->lo_rdevice ||
1293 info.lo_inode != info64->lo_inode ||
1294 info.lo_offset != info64->lo_offset ||
1295 info.lo_init[0] != info64->lo_init[0] ||
1296 info.lo_init[1] != info64->lo_init[1])
1299 if (copy_to_user(arg, &info, sizeof(info)))
1305 loop_set_status_compat(struct loop_device *lo,
1306 const struct compat_loop_info __user *arg)
1308 struct loop_info64 info64;
1311 ret = loop_info64_from_compat(arg, &info64);
1314 return loop_set_status(lo, &info64);
1318 loop_get_status_compat(struct loop_device *lo,
1319 struct compat_loop_info __user *arg)
1321 struct loop_info64 info64;
1327 err = loop_get_status(lo, &info64);
1329 err = loop_info64_to_compat(&info64, arg);
1333 static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1334 unsigned int cmd, unsigned long arg)
1336 struct loop_device *lo = bdev->bd_disk->private_data;
1340 case LOOP_SET_STATUS:
1341 mutex_lock(&lo->lo_ctl_mutex);
1342 err = loop_set_status_compat(
1343 lo, (const struct compat_loop_info __user *) arg);
1344 mutex_unlock(&lo->lo_ctl_mutex);
1346 case LOOP_GET_STATUS:
1347 mutex_lock(&lo->lo_ctl_mutex);
1348 err = loop_get_status_compat(
1349 lo, (struct compat_loop_info __user *) arg);
1350 mutex_unlock(&lo->lo_ctl_mutex);
1352 case LOOP_SET_CAPACITY:
1354 case LOOP_GET_STATUS64:
1355 case LOOP_SET_STATUS64:
1356 arg = (unsigned long) compat_ptr(arg);
1358 case LOOP_CHANGE_FD:
1359 err = lo_ioctl(bdev, mode, cmd, arg);
1369 static int lo_open(struct block_device *bdev, fmode_t mode)
1371 struct loop_device *lo;
1374 mutex_lock(&loop_index_mutex);
1375 lo = bdev->bd_disk->private_data;
1381 mutex_lock(&lo->lo_ctl_mutex);
1383 mutex_unlock(&lo->lo_ctl_mutex);
1385 mutex_unlock(&loop_index_mutex);
1389 static void lo_release(struct gendisk *disk, fmode_t mode)
1391 struct loop_device *lo = disk->private_data;
1394 mutex_lock(&lo->lo_ctl_mutex);
1396 if (--lo->lo_refcnt)
1399 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1401 * In autoclear mode, stop the loop thread
1402 * and remove configuration after last close.
1404 err = loop_clr_fd(lo);
1409 * Otherwise keep thread (if running) and config,
1410 * but flush possible ongoing bios in thread.
1416 mutex_unlock(&lo->lo_ctl_mutex);
1419 static const struct block_device_operations lo_fops = {
1420 .owner = THIS_MODULE,
1422 .release = lo_release,
1424 #ifdef CONFIG_COMPAT
1425 .compat_ioctl = lo_compat_ioctl,
1430 * And now the modules code and kernel interface.
1432 static int max_loop;
1433 module_param(max_loop, int, S_IRUGO);
1434 MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
1435 module_param(max_part, int, S_IRUGO);
1436 MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
1437 MODULE_LICENSE("GPL");
1438 MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1440 int loop_register_transfer(struct loop_func_table *funcs)
1442 unsigned int n = funcs->number;
1444 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1446 xfer_funcs[n] = funcs;
1450 static int unregister_transfer_cb(int id, void *ptr, void *data)
1452 struct loop_device *lo = ptr;
1453 struct loop_func_table *xfer = data;
1455 mutex_lock(&lo->lo_ctl_mutex);
1456 if (lo->lo_encryption == xfer)
1457 loop_release_xfer(lo);
1458 mutex_unlock(&lo->lo_ctl_mutex);
1462 int loop_unregister_transfer(int number)
1464 unsigned int n = number;
1465 struct loop_func_table *xfer;
1467 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1470 xfer_funcs[n] = NULL;
1471 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
1475 EXPORT_SYMBOL(loop_register_transfer);
1476 EXPORT_SYMBOL(loop_unregister_transfer);
1478 static int loop_queue_rq(struct blk_mq_hw_ctx *hctx,
1479 const struct blk_mq_queue_data *bd)
1481 struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
1483 blk_mq_start_request(bd->rq);
1485 if (cmd->rq->cmd_flags & REQ_WRITE) {
1486 struct loop_device *lo = cmd->rq->q->queuedata;
1487 bool need_sched = true;
1489 spin_lock_irq(&lo->lo_lock);
1490 if (lo->write_started)
1493 lo->write_started = true;
1494 list_add_tail(&cmd->list, &lo->write_cmd_head);
1495 spin_unlock_irq(&lo->lo_lock);
1498 queue_work(loop_wq, &lo->write_work);
1500 queue_work(loop_wq, &cmd->read_work);
1503 return BLK_MQ_RQ_QUEUE_OK;
1506 static void loop_handle_cmd(struct loop_cmd *cmd)
1508 const bool write = cmd->rq->cmd_flags & REQ_WRITE;
1509 struct loop_device *lo = cmd->rq->q->queuedata;
1512 if (lo->lo_state != Lo_bound)
1515 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY))
1518 ret = do_req_filebacked(lo, cmd->rq);
1522 cmd->rq->errors = -EIO;
1523 blk_mq_complete_request(cmd->rq);
1526 static void loop_queue_write_work(struct work_struct *work)
1528 struct loop_device *lo =
1529 container_of(work, struct loop_device, write_work);
1530 LIST_HEAD(cmd_list);
1532 spin_lock_irq(&lo->lo_lock);
1534 list_splice_init(&lo->write_cmd_head, &cmd_list);
1535 spin_unlock_irq(&lo->lo_lock);
1537 while (!list_empty(&cmd_list)) {
1538 struct loop_cmd *cmd = list_first_entry(&cmd_list,
1539 struct loop_cmd, list);
1540 list_del_init(&cmd->list);
1541 loop_handle_cmd(cmd);
1544 spin_lock_irq(&lo->lo_lock);
1545 if (!list_empty(&lo->write_cmd_head))
1547 lo->write_started = false;
1548 spin_unlock_irq(&lo->lo_lock);
1551 static void loop_queue_read_work(struct work_struct *work)
1553 struct loop_cmd *cmd =
1554 container_of(work, struct loop_cmd, read_work);
1556 loop_handle_cmd(cmd);
1559 static int loop_init_request(void *data, struct request *rq,
1560 unsigned int hctx_idx, unsigned int request_idx,
1561 unsigned int numa_node)
1563 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
1566 INIT_WORK(&cmd->read_work, loop_queue_read_work);
1571 static struct blk_mq_ops loop_mq_ops = {
1572 .queue_rq = loop_queue_rq,
1573 .map_queue = blk_mq_map_queue,
1574 .init_request = loop_init_request,
1577 static int loop_add(struct loop_device **l, int i)
1579 struct loop_device *lo;
1580 struct gendisk *disk;
1584 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
1588 lo->lo_state = Lo_unbound;
1590 /* allocate id, if @id >= 0, we're requesting that specific id */
1592 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
1596 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
1603 lo->tag_set.ops = &loop_mq_ops;
1604 lo->tag_set.nr_hw_queues = 1;
1605 lo->tag_set.queue_depth = 128;
1606 lo->tag_set.numa_node = NUMA_NO_NODE;
1607 lo->tag_set.cmd_size = sizeof(struct loop_cmd);
1608 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
1609 lo->tag_set.driver_data = lo;
1611 err = blk_mq_alloc_tag_set(&lo->tag_set);
1615 lo->lo_queue = blk_mq_init_queue(&lo->tag_set);
1616 if (IS_ERR_OR_NULL(lo->lo_queue)) {
1617 err = PTR_ERR(lo->lo_queue);
1618 goto out_cleanup_tags;
1620 lo->lo_queue->queuedata = lo;
1622 INIT_LIST_HEAD(&lo->write_cmd_head);
1623 INIT_WORK(&lo->write_work, loop_queue_write_work);
1625 disk = lo->lo_disk = alloc_disk(1 << part_shift);
1627 goto out_free_queue;
1630 * Disable partition scanning by default. The in-kernel partition
1631 * scanning can be requested individually per-device during its
1632 * setup. Userspace can always add and remove partitions from all
1633 * devices. The needed partition minors are allocated from the
1634 * extended minor space, the main loop device numbers will continue
1635 * to match the loop minors, regardless of the number of partitions
1638 * If max_part is given, partition scanning is globally enabled for
1639 * all loop devices. The minors for the main loop devices will be
1640 * multiples of max_part.
1642 * Note: Global-for-all-devices, set-only-at-init, read-only module
1643 * parameteters like 'max_loop' and 'max_part' make things needlessly
1644 * complicated, are too static, inflexible and may surprise
1645 * userspace tools. Parameters like this in general should be avoided.
1648 disk->flags |= GENHD_FL_NO_PART_SCAN;
1649 disk->flags |= GENHD_FL_EXT_DEVT;
1650 mutex_init(&lo->lo_ctl_mutex);
1652 spin_lock_init(&lo->lo_lock);
1653 disk->major = LOOP_MAJOR;
1654 disk->first_minor = i << part_shift;
1655 disk->fops = &lo_fops;
1656 disk->private_data = lo;
1657 disk->queue = lo->lo_queue;
1658 sprintf(disk->disk_name, "loop%d", i);
1661 return lo->lo_number;
1664 blk_cleanup_queue(lo->lo_queue);
1666 blk_mq_free_tag_set(&lo->tag_set);
1668 idr_remove(&loop_index_idr, i);
1675 static void loop_remove(struct loop_device *lo)
1677 del_gendisk(lo->lo_disk);
1678 blk_cleanup_queue(lo->lo_queue);
1679 blk_mq_free_tag_set(&lo->tag_set);
1680 put_disk(lo->lo_disk);
1684 static int find_free_cb(int id, void *ptr, void *data)
1686 struct loop_device *lo = ptr;
1687 struct loop_device **l = data;
1689 if (lo->lo_state == Lo_unbound) {
1696 static int loop_lookup(struct loop_device **l, int i)
1698 struct loop_device *lo;
1704 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
1707 ret = lo->lo_number;
1712 /* lookup and return a specific i */
1713 lo = idr_find(&loop_index_idr, i);
1716 ret = lo->lo_number;
1722 static struct kobject *loop_probe(dev_t dev, int *part, void *data)
1724 struct loop_device *lo;
1725 struct kobject *kobj;
1728 mutex_lock(&loop_index_mutex);
1729 err = loop_lookup(&lo, MINOR(dev) >> part_shift);
1731 err = loop_add(&lo, MINOR(dev) >> part_shift);
1735 kobj = get_disk(lo->lo_disk);
1736 mutex_unlock(&loop_index_mutex);
1742 static long loop_control_ioctl(struct file *file, unsigned int cmd,
1745 struct loop_device *lo;
1748 mutex_lock(&loop_index_mutex);
1751 ret = loop_lookup(&lo, parm);
1756 ret = loop_add(&lo, parm);
1758 case LOOP_CTL_REMOVE:
1759 ret = loop_lookup(&lo, parm);
1762 mutex_lock(&lo->lo_ctl_mutex);
1763 if (lo->lo_state != Lo_unbound) {
1765 mutex_unlock(&lo->lo_ctl_mutex);
1768 if (lo->lo_refcnt > 0) {
1770 mutex_unlock(&lo->lo_ctl_mutex);
1773 lo->lo_disk->private_data = NULL;
1774 mutex_unlock(&lo->lo_ctl_mutex);
1775 idr_remove(&loop_index_idr, lo->lo_number);
1778 case LOOP_CTL_GET_FREE:
1779 ret = loop_lookup(&lo, -1);
1782 ret = loop_add(&lo, -1);
1784 mutex_unlock(&loop_index_mutex);
1789 static const struct file_operations loop_ctl_fops = {
1790 .open = nonseekable_open,
1791 .unlocked_ioctl = loop_control_ioctl,
1792 .compat_ioctl = loop_control_ioctl,
1793 .owner = THIS_MODULE,
1794 .llseek = noop_llseek,
1797 static struct miscdevice loop_misc = {
1798 .minor = LOOP_CTRL_MINOR,
1799 .name = "loop-control",
1800 .fops = &loop_ctl_fops,
1803 MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
1804 MODULE_ALIAS("devname:loop-control");
1806 static int __init loop_init(void)
1809 unsigned long range;
1810 struct loop_device *lo;
1813 err = misc_register(&loop_misc);
1819 part_shift = fls(max_part);
1822 * Adjust max_part according to part_shift as it is exported
1823 * to user space so that user can decide correct minor number
1824 * if [s]he want to create more devices.
1826 * Note that -1 is required because partition 0 is reserved
1827 * for the whole disk.
1829 max_part = (1UL << part_shift) - 1;
1832 if ((1UL << part_shift) > DISK_MAX_PARTS) {
1837 if (max_loop > 1UL << (MINORBITS - part_shift)) {
1843 * If max_loop is specified, create that many devices upfront.
1844 * This also becomes a hard limit. If max_loop is not specified,
1845 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
1846 * init time. Loop devices can be requested on-demand with the
1847 * /dev/loop-control interface, or be instantiated by accessing
1848 * a 'dead' device node.
1852 range = max_loop << part_shift;
1854 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
1855 range = 1UL << MINORBITS;
1858 if (register_blkdev(LOOP_MAJOR, "loop")) {
1863 loop_wq = alloc_workqueue("kloopd",
1864 WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 0);
1870 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
1871 THIS_MODULE, loop_probe, NULL, NULL);
1873 /* pre-create number of devices given by config or max_loop */
1874 mutex_lock(&loop_index_mutex);
1875 for (i = 0; i < nr; i++)
1877 mutex_unlock(&loop_index_mutex);
1879 printk(KERN_INFO "loop: module loaded\n");
1883 misc_deregister(&loop_misc);
1887 static int loop_exit_cb(int id, void *ptr, void *data)
1889 struct loop_device *lo = ptr;
1895 static void __exit loop_exit(void)
1897 unsigned long range;
1899 range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
1901 idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
1902 idr_destroy(&loop_index_idr);
1904 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
1905 unregister_blkdev(LOOP_MAJOR, "loop");
1907 destroy_workqueue(loop_wq);
1909 misc_deregister(&loop_misc);
1912 module_init(loop_init);
1913 module_exit(loop_exit);
1916 static int __init max_loop_setup(char *str)
1918 max_loop = simple_strtol(str, NULL, 0);
1922 __setup("max_loop=", max_loop_setup);