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>
85 #include <linux/uaccess.h>
87 #define LOOP_IDLE_WORKER_TIMEOUT (60 * HZ)
89 static DEFINE_IDR(loop_index_idr
);
90 static DEFINE_MUTEX(loop_ctl_mutex
);
91 static DEFINE_MUTEX(loop_validate_mutex
);
94 * loop_global_lock_killable() - take locks for safe loop_validate_file() test
96 * @lo: struct loop_device
97 * @global: true if @lo is about to bind another "struct loop_device", false otherwise
99 * Returns 0 on success, -EINTR otherwise.
101 * Since loop_validate_file() traverses on other "struct loop_device" if
102 * is_loop_device() is true, we need a global lock for serializing concurrent
103 * loop_configure()/loop_change_fd()/__loop_clr_fd() calls.
105 static int loop_global_lock_killable(struct loop_device
*lo
, bool global
)
110 err
= mutex_lock_killable(&loop_validate_mutex
);
114 err
= mutex_lock_killable(&lo
->lo_mutex
);
116 mutex_unlock(&loop_validate_mutex
);
121 * loop_global_unlock() - release locks taken by loop_global_lock_killable()
123 * @lo: struct loop_device
124 * @global: true if @lo was about to bind another "struct loop_device", false otherwise
126 static void loop_global_unlock(struct loop_device
*lo
, bool global
)
128 mutex_unlock(&lo
->lo_mutex
);
130 mutex_unlock(&loop_validate_mutex
);
134 static int part_shift
;
136 static int transfer_xor(struct loop_device
*lo
, int cmd
,
137 struct page
*raw_page
, unsigned raw_off
,
138 struct page
*loop_page
, unsigned loop_off
,
139 int size
, sector_t real_block
)
141 char *raw_buf
= kmap_atomic(raw_page
) + raw_off
;
142 char *loop_buf
= kmap_atomic(loop_page
) + loop_off
;
143 char *in
, *out
, *key
;
154 key
= lo
->lo_encrypt_key
;
155 keysize
= lo
->lo_encrypt_key_size
;
156 for (i
= 0; i
< size
; i
++)
157 *out
++ = *in
++ ^ key
[(i
& 511) % keysize
];
159 kunmap_atomic(loop_buf
);
160 kunmap_atomic(raw_buf
);
165 static int xor_init(struct loop_device
*lo
, const struct loop_info64
*info
)
167 if (unlikely(info
->lo_encrypt_key_size
<= 0))
172 static struct loop_func_table none_funcs
= {
173 .number
= LO_CRYPT_NONE
,
176 static struct loop_func_table xor_funcs
= {
177 .number
= LO_CRYPT_XOR
,
178 .transfer
= transfer_xor
,
182 /* xfer_funcs[0] is special - its release function is never called */
183 static struct loop_func_table
*xfer_funcs
[MAX_LO_CRYPT
] = {
188 static loff_t
get_size(loff_t offset
, loff_t sizelimit
, struct file
*file
)
192 /* Compute loopsize in bytes */
193 loopsize
= i_size_read(file
->f_mapping
->host
);
196 /* offset is beyond i_size, weird but possible */
200 if (sizelimit
> 0 && sizelimit
< loopsize
)
201 loopsize
= sizelimit
;
203 * Unfortunately, if we want to do I/O on the device,
204 * the number of 512-byte sectors has to fit into a sector_t.
206 return loopsize
>> 9;
209 static loff_t
get_loop_size(struct loop_device
*lo
, struct file
*file
)
211 return get_size(lo
->lo_offset
, lo
->lo_sizelimit
, file
);
214 static void __loop_update_dio(struct loop_device
*lo
, bool dio
)
216 struct file
*file
= lo
->lo_backing_file
;
217 struct address_space
*mapping
= file
->f_mapping
;
218 struct inode
*inode
= mapping
->host
;
219 unsigned short sb_bsize
= 0;
220 unsigned dio_align
= 0;
223 if (inode
->i_sb
->s_bdev
) {
224 sb_bsize
= bdev_logical_block_size(inode
->i_sb
->s_bdev
);
225 dio_align
= sb_bsize
- 1;
229 * We support direct I/O only if lo_offset is aligned with the
230 * logical I/O size of backing device, and the logical block
231 * size of loop is bigger than the backing device's and the loop
232 * needn't transform transfer.
234 * TODO: the above condition may be loosed in the future, and
235 * direct I/O may be switched runtime at that time because most
236 * of requests in sane applications should be PAGE_SIZE aligned
239 if (queue_logical_block_size(lo
->lo_queue
) >= sb_bsize
&&
240 !(lo
->lo_offset
& dio_align
) &&
241 mapping
->a_ops
->direct_IO
&&
250 if (lo
->use_dio
== use_dio
)
253 /* flush dirty pages before changing direct IO */
257 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
258 * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
259 * will get updated by ioctl(LOOP_GET_STATUS)
261 if (lo
->lo_state
== Lo_bound
)
262 blk_mq_freeze_queue(lo
->lo_queue
);
263 lo
->use_dio
= use_dio
;
265 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES
, lo
->lo_queue
);
266 lo
->lo_flags
|= LO_FLAGS_DIRECT_IO
;
268 blk_queue_flag_set(QUEUE_FLAG_NOMERGES
, lo
->lo_queue
);
269 lo
->lo_flags
&= ~LO_FLAGS_DIRECT_IO
;
271 if (lo
->lo_state
== Lo_bound
)
272 blk_mq_unfreeze_queue(lo
->lo_queue
);
276 * loop_set_size() - sets device size and notifies userspace
277 * @lo: struct loop_device to set the size for
278 * @size: new size of the loop device
280 * Callers must validate that the size passed into this function fits into
281 * a sector_t, eg using loop_validate_size()
283 static void loop_set_size(struct loop_device
*lo
, loff_t size
)
285 if (!set_capacity_and_notify(lo
->lo_disk
, size
))
286 kobject_uevent(&disk_to_dev(lo
->lo_disk
)->kobj
, KOBJ_CHANGE
);
290 lo_do_transfer(struct loop_device
*lo
, int cmd
,
291 struct page
*rpage
, unsigned roffs
,
292 struct page
*lpage
, unsigned loffs
,
293 int size
, sector_t rblock
)
297 ret
= lo
->transfer(lo
, cmd
, rpage
, roffs
, lpage
, loffs
, size
, rblock
);
301 printk_ratelimited(KERN_ERR
302 "loop: Transfer error at byte offset %llu, length %i.\n",
303 (unsigned long long)rblock
<< 9, size
);
307 static int lo_write_bvec(struct file
*file
, struct bio_vec
*bvec
, loff_t
*ppos
)
312 iov_iter_bvec(&i
, WRITE
, bvec
, 1, bvec
->bv_len
);
314 file_start_write(file
);
315 bw
= vfs_iter_write(file
, &i
, ppos
, 0);
316 file_end_write(file
);
318 if (likely(bw
== bvec
->bv_len
))
321 printk_ratelimited(KERN_ERR
322 "loop: Write error at byte offset %llu, length %i.\n",
323 (unsigned long long)*ppos
, bvec
->bv_len
);
329 static int lo_write_simple(struct loop_device
*lo
, struct request
*rq
,
333 struct req_iterator iter
;
336 rq_for_each_segment(bvec
, rq
, iter
) {
337 ret
= lo_write_bvec(lo
->lo_backing_file
, &bvec
, &pos
);
347 * This is the slow, transforming version that needs to double buffer the
348 * data as it cannot do the transformations in place without having direct
349 * access to the destination pages of the backing file.
351 static int lo_write_transfer(struct loop_device
*lo
, struct request
*rq
,
354 struct bio_vec bvec
, b
;
355 struct req_iterator iter
;
359 page
= alloc_page(GFP_NOIO
);
363 rq_for_each_segment(bvec
, rq
, iter
) {
364 ret
= lo_do_transfer(lo
, WRITE
, page
, 0, bvec
.bv_page
,
365 bvec
.bv_offset
, bvec
.bv_len
, pos
>> 9);
371 b
.bv_len
= bvec
.bv_len
;
372 ret
= lo_write_bvec(lo
->lo_backing_file
, &b
, &pos
);
381 static int lo_read_simple(struct loop_device
*lo
, struct request
*rq
,
385 struct req_iterator iter
;
389 rq_for_each_segment(bvec
, rq
, iter
) {
390 iov_iter_bvec(&i
, READ
, &bvec
, 1, bvec
.bv_len
);
391 len
= vfs_iter_read(lo
->lo_backing_file
, &i
, &pos
, 0);
395 flush_dcache_page(bvec
.bv_page
);
397 if (len
!= bvec
.bv_len
) {
400 __rq_for_each_bio(bio
, rq
)
410 static int lo_read_transfer(struct loop_device
*lo
, struct request
*rq
,
413 struct bio_vec bvec
, b
;
414 struct req_iterator iter
;
420 page
= alloc_page(GFP_NOIO
);
424 rq_for_each_segment(bvec
, rq
, iter
) {
429 b
.bv_len
= bvec
.bv_len
;
431 iov_iter_bvec(&i
, READ
, &b
, 1, b
.bv_len
);
432 len
= vfs_iter_read(lo
->lo_backing_file
, &i
, &pos
, 0);
438 ret
= lo_do_transfer(lo
, READ
, page
, 0, bvec
.bv_page
,
439 bvec
.bv_offset
, len
, offset
>> 9);
443 flush_dcache_page(bvec
.bv_page
);
445 if (len
!= bvec
.bv_len
) {
448 __rq_for_each_bio(bio
, rq
)
460 static int lo_fallocate(struct loop_device
*lo
, struct request
*rq
, loff_t pos
,
464 * We use fallocate to manipulate the space mappings used by the image
465 * a.k.a. discard/zerorange. However we do not support this if
466 * encryption is enabled, because it may give an attacker useful
469 struct file
*file
= lo
->lo_backing_file
;
470 struct request_queue
*q
= lo
->lo_queue
;
473 mode
|= FALLOC_FL_KEEP_SIZE
;
475 if (!blk_queue_discard(q
)) {
480 ret
= file
->f_op
->fallocate(file
, mode
, pos
, blk_rq_bytes(rq
));
481 if (unlikely(ret
&& ret
!= -EINVAL
&& ret
!= -EOPNOTSUPP
))
487 static int lo_req_flush(struct loop_device
*lo
, struct request
*rq
)
489 struct file
*file
= lo
->lo_backing_file
;
490 int ret
= vfs_fsync(file
, 0);
491 if (unlikely(ret
&& ret
!= -EINVAL
))
497 static void lo_complete_rq(struct request
*rq
)
499 struct loop_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
500 blk_status_t ret
= BLK_STS_OK
;
502 if (!cmd
->use_aio
|| cmd
->ret
< 0 || cmd
->ret
== blk_rq_bytes(rq
) ||
503 req_op(rq
) != REQ_OP_READ
) {
505 ret
= errno_to_blk_status(cmd
->ret
);
510 * Short READ - if we got some data, advance our request and
511 * retry it. If we got no data, end the rest with EIO.
514 blk_update_request(rq
, BLK_STS_OK
, cmd
->ret
);
516 blk_mq_requeue_request(rq
, true);
519 struct bio
*bio
= rq
->bio
;
528 blk_mq_end_request(rq
, ret
);
532 static void lo_rw_aio_do_completion(struct loop_cmd
*cmd
)
534 struct request
*rq
= blk_mq_rq_from_pdu(cmd
);
536 if (!atomic_dec_and_test(&cmd
->ref
))
540 if (likely(!blk_should_fake_timeout(rq
->q
)))
541 blk_mq_complete_request(rq
);
544 static void lo_rw_aio_complete(struct kiocb
*iocb
, long ret
, long ret2
)
546 struct loop_cmd
*cmd
= container_of(iocb
, struct loop_cmd
, iocb
);
549 lo_rw_aio_do_completion(cmd
);
552 static int lo_rw_aio(struct loop_device
*lo
, struct loop_cmd
*cmd
,
555 struct iov_iter iter
;
556 struct req_iterator rq_iter
;
557 struct bio_vec
*bvec
;
558 struct request
*rq
= blk_mq_rq_from_pdu(cmd
);
559 struct bio
*bio
= rq
->bio
;
560 struct file
*file
= lo
->lo_backing_file
;
566 rq_for_each_bvec(tmp
, rq
, rq_iter
)
569 if (rq
->bio
!= rq
->biotail
) {
571 bvec
= kmalloc_array(nr_bvec
, sizeof(struct bio_vec
),
578 * The bios of the request may be started from the middle of
579 * the 'bvec' because of bio splitting, so we can't directly
580 * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec
581 * API will take care of all details for us.
583 rq_for_each_bvec(tmp
, rq
, rq_iter
) {
591 * Same here, this bio may be started from the middle of the
592 * 'bvec' because of bio splitting, so offset from the bvec
593 * must be passed to iov iterator
595 offset
= bio
->bi_iter
.bi_bvec_done
;
596 bvec
= __bvec_iter_bvec(bio
->bi_io_vec
, bio
->bi_iter
);
598 atomic_set(&cmd
->ref
, 2);
600 iov_iter_bvec(&iter
, rw
, bvec
, nr_bvec
, blk_rq_bytes(rq
));
601 iter
.iov_offset
= offset
;
603 cmd
->iocb
.ki_pos
= pos
;
604 cmd
->iocb
.ki_filp
= file
;
605 cmd
->iocb
.ki_complete
= lo_rw_aio_complete
;
606 cmd
->iocb
.ki_flags
= IOCB_DIRECT
;
607 cmd
->iocb
.ki_ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE
, 0);
610 ret
= call_write_iter(file
, &cmd
->iocb
, &iter
);
612 ret
= call_read_iter(file
, &cmd
->iocb
, &iter
);
614 lo_rw_aio_do_completion(cmd
);
616 if (ret
!= -EIOCBQUEUED
)
617 cmd
->iocb
.ki_complete(&cmd
->iocb
, ret
, 0);
621 static int do_req_filebacked(struct loop_device
*lo
, struct request
*rq
)
623 struct loop_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
624 loff_t pos
= ((loff_t
) blk_rq_pos(rq
) << 9) + lo
->lo_offset
;
627 * lo_write_simple and lo_read_simple should have been covered
628 * by io submit style function like lo_rw_aio(), one blocker
629 * is that lo_read_simple() need to call flush_dcache_page after
630 * the page is written from kernel, and it isn't easy to handle
631 * this in io submit style function which submits all segments
632 * of the req at one time. And direct read IO doesn't need to
633 * run flush_dcache_page().
635 switch (req_op(rq
)) {
637 return lo_req_flush(lo
, rq
);
638 case REQ_OP_WRITE_ZEROES
:
640 * If the caller doesn't want deallocation, call zeroout to
641 * write zeroes the range. Otherwise, punch them out.
643 return lo_fallocate(lo
, rq
, pos
,
644 (rq
->cmd_flags
& REQ_NOUNMAP
) ?
645 FALLOC_FL_ZERO_RANGE
:
646 FALLOC_FL_PUNCH_HOLE
);
648 return lo_fallocate(lo
, rq
, pos
, FALLOC_FL_PUNCH_HOLE
);
651 return lo_write_transfer(lo
, rq
, pos
);
652 else if (cmd
->use_aio
)
653 return lo_rw_aio(lo
, cmd
, pos
, WRITE
);
655 return lo_write_simple(lo
, rq
, pos
);
658 return lo_read_transfer(lo
, rq
, pos
);
659 else if (cmd
->use_aio
)
660 return lo_rw_aio(lo
, cmd
, pos
, READ
);
662 return lo_read_simple(lo
, rq
, pos
);
669 static inline void loop_update_dio(struct loop_device
*lo
)
671 __loop_update_dio(lo
, (lo
->lo_backing_file
->f_flags
& O_DIRECT
) |
675 static struct file
*loop_real_file(struct file
*file
)
677 struct file
*f
= NULL
;
679 if (file
->f_path
.dentry
->d_sb
->s_op
->real_loop
)
680 f
= file
->f_path
.dentry
->d_sb
->s_op
->real_loop(file
);
684 static void loop_reread_partitions(struct loop_device
*lo
)
688 mutex_lock(&lo
->lo_disk
->open_mutex
);
689 rc
= bdev_disk_changed(lo
->lo_disk
, false);
690 mutex_unlock(&lo
->lo_disk
->open_mutex
);
692 pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
693 __func__
, lo
->lo_number
, lo
->lo_file_name
, rc
);
696 static inline int is_loop_device(struct file
*file
)
698 struct inode
*i
= file
->f_mapping
->host
;
700 return i
&& S_ISBLK(i
->i_mode
) && imajor(i
) == LOOP_MAJOR
;
703 static int loop_validate_file(struct file
*file
, struct block_device
*bdev
)
705 struct inode
*inode
= file
->f_mapping
->host
;
706 struct file
*f
= file
;
708 /* Avoid recursion */
709 while (is_loop_device(f
)) {
710 struct loop_device
*l
;
712 lockdep_assert_held(&loop_validate_mutex
);
713 if (f
->f_mapping
->host
->i_rdev
== bdev
->bd_dev
)
716 l
= I_BDEV(f
->f_mapping
->host
)->bd_disk
->private_data
;
717 if (l
->lo_state
!= Lo_bound
)
719 /* Order wrt setting lo->lo_backing_file in loop_configure(). */
721 f
= l
->lo_backing_file
;
723 if (!S_ISREG(inode
->i_mode
) && !S_ISBLK(inode
->i_mode
))
729 * loop_change_fd switched the backing store of a loopback device to
730 * a new file. This is useful for operating system installers to free up
731 * the original file and in High Availability environments to switch to
732 * an alternative location for the content in case of server meltdown.
733 * This can only work if the loop device is used read-only, and if the
734 * new backing store is the same size and type as the old backing store.
736 static int loop_change_fd(struct loop_device
*lo
, struct block_device
*bdev
,
739 struct file
*file
= fget(arg
);
740 struct file
*old_file
;
741 struct file
*f
, *virt_file
= NULL
, *old_virt_file
;
748 is_loop
= is_loop_device(file
);
749 error
= loop_global_lock_killable(lo
, is_loop
);
753 if (lo
->lo_state
!= Lo_bound
)
756 /* the loop device has to be read-only */
758 if (!(lo
->lo_flags
& LO_FLAGS_READ_ONLY
))
761 f
= loop_real_file(file
);
768 error
= loop_validate_file(file
, bdev
);
772 old_file
= lo
->lo_backing_file
;
773 old_virt_file
= lo
->lo_backing_virt_file
;
777 /* size of the new backing store needs to be the same */
778 if (get_loop_size(lo
, file
) != get_loop_size(lo
, old_file
))
782 disk_force_media_change(lo
->lo_disk
, DISK_EVENT_MEDIA_CHANGE
);
783 blk_mq_freeze_queue(lo
->lo_queue
);
784 mapping_set_gfp_mask(old_file
->f_mapping
, lo
->old_gfp_mask
);
785 lo
->lo_backing_file
= file
;
786 lo
->lo_backing_virt_file
= virt_file
;
787 lo
->old_gfp_mask
= mapping_gfp_mask(file
->f_mapping
);
788 mapping_set_gfp_mask(file
->f_mapping
,
789 lo
->old_gfp_mask
& ~(__GFP_IO
|__GFP_FS
));
791 blk_mq_unfreeze_queue(lo
->lo_queue
);
792 partscan
= lo
->lo_flags
& LO_FLAGS_PARTSCAN
;
793 loop_global_unlock(lo
, is_loop
);
796 * Flush loop_validate_file() before fput(), for l->lo_backing_file
797 * might be pointing at old_file which might be the last reference.
800 mutex_lock(&loop_validate_mutex
);
801 mutex_unlock(&loop_validate_mutex
);
804 * We must drop file reference outside of lo_mutex as dropping
805 * the file ref can take open_mutex which creates circular locking
812 loop_reread_partitions(lo
);
816 loop_global_unlock(lo
, is_loop
);
826 * no get/put for file.
828 struct file
*loop_backing_file(struct super_block
*sb
)
831 struct loop_device
*l
;
834 if (MAJOR(sb
->s_dev
) == LOOP_MAJOR
) {
835 l
= sb
->s_bdev
->bd_disk
->private_data
;
836 ret
= l
->lo_backing_file
;
840 EXPORT_SYMBOL_GPL(loop_backing_file
);
842 /* loop sysfs attributes */
844 static ssize_t
loop_attr_show(struct device
*dev
, char *page
,
845 ssize_t (*callback
)(struct loop_device
*, char *))
847 struct gendisk
*disk
= dev_to_disk(dev
);
848 struct loop_device
*lo
= disk
->private_data
;
850 return callback(lo
, page
);
853 #define LOOP_ATTR_RO(_name) \
854 static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
855 static ssize_t loop_attr_do_show_##_name(struct device *d, \
856 struct device_attribute *attr, char *b) \
858 return loop_attr_show(d, b, loop_attr_##_name##_show); \
860 static struct device_attribute loop_attr_##_name = \
861 __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL);
863 static ssize_t
loop_attr_backing_file_show(struct loop_device
*lo
, char *buf
)
868 spin_lock_irq(&lo
->lo_lock
);
869 if (lo
->lo_backing_file
)
870 p
= file_path(lo
->lo_backing_file
, buf
, PAGE_SIZE
- 1);
871 spin_unlock_irq(&lo
->lo_lock
);
873 if (IS_ERR_OR_NULL(p
))
877 memmove(buf
, p
, ret
);
885 static ssize_t
loop_attr_offset_show(struct loop_device
*lo
, char *buf
)
887 return sprintf(buf
, "%llu\n", (unsigned long long)lo
->lo_offset
);
890 static ssize_t
loop_attr_sizelimit_show(struct loop_device
*lo
, char *buf
)
892 return sprintf(buf
, "%llu\n", (unsigned long long)lo
->lo_sizelimit
);
895 static ssize_t
loop_attr_autoclear_show(struct loop_device
*lo
, char *buf
)
897 int autoclear
= (lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
);
899 return sprintf(buf
, "%s\n", autoclear
? "1" : "0");
902 static ssize_t
loop_attr_partscan_show(struct loop_device
*lo
, char *buf
)
904 int partscan
= (lo
->lo_flags
& LO_FLAGS_PARTSCAN
);
906 return sprintf(buf
, "%s\n", partscan
? "1" : "0");
909 static ssize_t
loop_attr_dio_show(struct loop_device
*lo
, char *buf
)
911 int dio
= (lo
->lo_flags
& LO_FLAGS_DIRECT_IO
);
913 return sprintf(buf
, "%s\n", dio
? "1" : "0");
916 LOOP_ATTR_RO(backing_file
);
917 LOOP_ATTR_RO(offset
);
918 LOOP_ATTR_RO(sizelimit
);
919 LOOP_ATTR_RO(autoclear
);
920 LOOP_ATTR_RO(partscan
);
923 static struct attribute
*loop_attrs
[] = {
924 &loop_attr_backing_file
.attr
,
925 &loop_attr_offset
.attr
,
926 &loop_attr_sizelimit
.attr
,
927 &loop_attr_autoclear
.attr
,
928 &loop_attr_partscan
.attr
,
933 static struct attribute_group loop_attribute_group
= {
938 static void loop_sysfs_init(struct loop_device
*lo
)
940 lo
->sysfs_inited
= !sysfs_create_group(&disk_to_dev(lo
->lo_disk
)->kobj
,
941 &loop_attribute_group
);
944 static void loop_sysfs_exit(struct loop_device
*lo
)
946 if (lo
->sysfs_inited
)
947 sysfs_remove_group(&disk_to_dev(lo
->lo_disk
)->kobj
,
948 &loop_attribute_group
);
951 static void loop_config_discard(struct loop_device
*lo
)
953 struct file
*file
= lo
->lo_backing_file
;
954 struct inode
*inode
= file
->f_mapping
->host
;
955 struct request_queue
*q
= lo
->lo_queue
;
956 u32 granularity
, max_discard_sectors
;
959 * If the backing device is a block device, mirror its zeroing
960 * capability. Set the discard sectors to the block device's zeroing
961 * capabilities because loop discards result in blkdev_issue_zeroout(),
962 * not blkdev_issue_discard(). This maintains consistent behavior with
963 * file-backed loop devices: discarded regions read back as zero.
965 if (S_ISBLK(inode
->i_mode
) && !lo
->lo_encrypt_key_size
) {
966 struct request_queue
*backingq
= bdev_get_queue(I_BDEV(inode
));
968 max_discard_sectors
= backingq
->limits
.max_write_zeroes_sectors
;
969 granularity
= backingq
->limits
.discard_granularity
?:
970 queue_physical_block_size(backingq
);
973 * We use punch hole to reclaim the free space used by the
974 * image a.k.a. discard. However we do not support discard if
975 * encryption is enabled, because it may give an attacker
976 * useful information.
978 } else if (!file
->f_op
->fallocate
|| lo
->lo_encrypt_key_size
) {
979 max_discard_sectors
= 0;
983 max_discard_sectors
= UINT_MAX
>> 9;
984 granularity
= inode
->i_sb
->s_blocksize
;
987 if (max_discard_sectors
) {
988 q
->limits
.discard_granularity
= granularity
;
989 blk_queue_max_discard_sectors(q
, max_discard_sectors
);
990 blk_queue_max_write_zeroes_sectors(q
, max_discard_sectors
);
991 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, q
);
993 q
->limits
.discard_granularity
= 0;
994 blk_queue_max_discard_sectors(q
, 0);
995 blk_queue_max_write_zeroes_sectors(q
, 0);
996 blk_queue_flag_clear(QUEUE_FLAG_DISCARD
, q
);
998 q
->limits
.discard_alignment
= 0;
1001 struct loop_worker
{
1002 struct rb_node rb_node
;
1003 struct work_struct work
;
1004 struct list_head cmd_list
;
1005 struct list_head idle_list
;
1006 struct loop_device
*lo
;
1007 struct cgroup_subsys_state
*blkcg_css
;
1008 unsigned long last_ran_at
;
1011 static void loop_workfn(struct work_struct
*work
);
1012 static void loop_rootcg_workfn(struct work_struct
*work
);
1013 static void loop_free_idle_workers(struct timer_list
*timer
);
1015 #ifdef CONFIG_BLK_CGROUP
1016 static inline int queue_on_root_worker(struct cgroup_subsys_state
*css
)
1018 return !css
|| css
== blkcg_root_css
;
1021 static inline int queue_on_root_worker(struct cgroup_subsys_state
*css
)
1027 static void loop_queue_work(struct loop_device
*lo
, struct loop_cmd
*cmd
)
1029 struct rb_node
**node
= &(lo
->worker_tree
.rb_node
), *parent
= NULL
;
1030 struct loop_worker
*cur_worker
, *worker
= NULL
;
1031 struct work_struct
*work
;
1032 struct list_head
*cmd_list
;
1034 spin_lock_irq(&lo
->lo_work_lock
);
1036 if (queue_on_root_worker(cmd
->blkcg_css
))
1039 node
= &lo
->worker_tree
.rb_node
;
1043 cur_worker
= container_of(*node
, struct loop_worker
, rb_node
);
1044 if (cur_worker
->blkcg_css
== cmd
->blkcg_css
) {
1045 worker
= cur_worker
;
1047 } else if ((long)cur_worker
->blkcg_css
< (long)cmd
->blkcg_css
) {
1048 node
= &(*node
)->rb_left
;
1050 node
= &(*node
)->rb_right
;
1056 worker
= kzalloc(sizeof(struct loop_worker
), GFP_NOWAIT
| __GFP_NOWARN
);
1058 * In the event we cannot allocate a worker, just queue on the
1059 * rootcg worker and issue the I/O as the rootcg
1062 cmd
->blkcg_css
= NULL
;
1064 css_put(cmd
->memcg_css
);
1065 cmd
->memcg_css
= NULL
;
1069 worker
->blkcg_css
= cmd
->blkcg_css
;
1070 css_get(worker
->blkcg_css
);
1071 INIT_WORK(&worker
->work
, loop_workfn
);
1072 INIT_LIST_HEAD(&worker
->cmd_list
);
1073 INIT_LIST_HEAD(&worker
->idle_list
);
1075 rb_link_node(&worker
->rb_node
, parent
, node
);
1076 rb_insert_color(&worker
->rb_node
, &lo
->worker_tree
);
1080 * We need to remove from the idle list here while
1081 * holding the lock so that the idle timer doesn't
1084 if (!list_empty(&worker
->idle_list
))
1085 list_del_init(&worker
->idle_list
);
1086 work
= &worker
->work
;
1087 cmd_list
= &worker
->cmd_list
;
1089 work
= &lo
->rootcg_work
;
1090 cmd_list
= &lo
->rootcg_cmd_list
;
1092 list_add_tail(&cmd
->list_entry
, cmd_list
);
1093 queue_work(lo
->workqueue
, work
);
1094 spin_unlock_irq(&lo
->lo_work_lock
);
1097 static void loop_update_rotational(struct loop_device
*lo
)
1099 struct file
*file
= lo
->lo_backing_file
;
1100 struct inode
*file_inode
= file
->f_mapping
->host
;
1101 struct block_device
*file_bdev
= file_inode
->i_sb
->s_bdev
;
1102 struct request_queue
*q
= lo
->lo_queue
;
1105 /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
1107 nonrot
= blk_queue_nonrot(bdev_get_queue(file_bdev
));
1110 blk_queue_flag_set(QUEUE_FLAG_NONROT
, q
);
1112 blk_queue_flag_clear(QUEUE_FLAG_NONROT
, q
);
1116 loop_release_xfer(struct loop_device
*lo
)
1119 struct loop_func_table
*xfer
= lo
->lo_encryption
;
1123 err
= xfer
->release(lo
);
1124 lo
->transfer
= NULL
;
1125 lo
->lo_encryption
= NULL
;
1126 module_put(xfer
->owner
);
1132 loop_init_xfer(struct loop_device
*lo
, struct loop_func_table
*xfer
,
1133 const struct loop_info64
*i
)
1138 struct module
*owner
= xfer
->owner
;
1140 if (!try_module_get(owner
))
1143 err
= xfer
->init(lo
, i
);
1147 lo
->lo_encryption
= xfer
;
1153 * loop_set_status_from_info - configure device from loop_info
1154 * @lo: struct loop_device to configure
1155 * @info: struct loop_info64 to configure the device with
1157 * Configures the loop device parameters according to the passed
1158 * in loop_info64 configuration.
1161 loop_set_status_from_info(struct loop_device
*lo
,
1162 const struct loop_info64
*info
)
1165 struct loop_func_table
*xfer
;
1166 kuid_t uid
= current_uid();
1168 if ((unsigned int) info
->lo_encrypt_key_size
> LO_KEY_SIZE
)
1171 err
= loop_release_xfer(lo
);
1175 if (info
->lo_encrypt_type
) {
1176 unsigned int type
= info
->lo_encrypt_type
;
1178 if (type
>= MAX_LO_CRYPT
)
1180 xfer
= xfer_funcs
[type
];
1186 err
= loop_init_xfer(lo
, xfer
, info
);
1190 lo
->lo_offset
= info
->lo_offset
;
1191 lo
->lo_sizelimit
= info
->lo_sizelimit
;
1192 memcpy(lo
->lo_file_name
, info
->lo_file_name
, LO_NAME_SIZE
);
1193 memcpy(lo
->lo_crypt_name
, info
->lo_crypt_name
, LO_NAME_SIZE
);
1194 lo
->lo_file_name
[LO_NAME_SIZE
-1] = 0;
1195 lo
->lo_crypt_name
[LO_NAME_SIZE
-1] = 0;
1199 lo
->transfer
= xfer
->transfer
;
1200 lo
->ioctl
= xfer
->ioctl
;
1202 lo
->lo_flags
= info
->lo_flags
;
1204 lo
->lo_encrypt_key_size
= info
->lo_encrypt_key_size
;
1205 lo
->lo_init
[0] = info
->lo_init
[0];
1206 lo
->lo_init
[1] = info
->lo_init
[1];
1207 if (info
->lo_encrypt_key_size
) {
1208 memcpy(lo
->lo_encrypt_key
, info
->lo_encrypt_key
,
1209 info
->lo_encrypt_key_size
);
1210 lo
->lo_key_owner
= uid
;
1216 static int loop_configure(struct loop_device
*lo
, fmode_t mode
,
1217 struct block_device
*bdev
,
1218 const struct loop_config
*config
)
1220 struct file
*file
= fget(config
->fd
);
1221 struct file
*f
, *virt_file
= NULL
;
1222 struct inode
*inode
;
1223 struct address_space
*mapping
;
1227 unsigned short bsize
;
1232 is_loop
= is_loop_device(file
);
1234 /* This is safe, since we have a reference from open(). */
1235 __module_get(THIS_MODULE
);
1237 f
= loop_real_file(file
);
1245 * If we don't hold exclusive handle for the device, upgrade to it
1246 * here to avoid changing device under exclusive owner.
1248 if (!(mode
& FMODE_EXCL
)) {
1249 error
= bd_prepare_to_claim(bdev
, loop_configure
);
1254 error
= loop_global_lock_killable(lo
, is_loop
);
1259 if (lo
->lo_state
!= Lo_unbound
)
1262 error
= loop_validate_file(file
, bdev
);
1266 mapping
= file
->f_mapping
;
1267 inode
= mapping
->host
;
1269 if ((config
->info
.lo_flags
& ~LOOP_CONFIGURE_SETTABLE_FLAGS
) != 0) {
1274 if (config
->block_size
) {
1275 error
= blk_validate_block_size(config
->block_size
);
1280 error
= loop_set_status_from_info(lo
, &config
->info
);
1284 if (!(file
->f_mode
& FMODE_WRITE
) || !(mode
& FMODE_WRITE
) ||
1285 !file
->f_op
->write_iter
)
1286 lo
->lo_flags
|= LO_FLAGS_READ_ONLY
;
1288 lo
->workqueue
= alloc_workqueue("loop%d",
1289 WQ_UNBOUND
| WQ_FREEZABLE
,
1292 if (!lo
->workqueue
) {
1297 disk_force_media_change(lo
->lo_disk
, DISK_EVENT_MEDIA_CHANGE
);
1298 set_disk_ro(lo
->lo_disk
, (lo
->lo_flags
& LO_FLAGS_READ_ONLY
) != 0);
1300 INIT_WORK(&lo
->rootcg_work
, loop_rootcg_workfn
);
1301 INIT_LIST_HEAD(&lo
->rootcg_cmd_list
);
1302 INIT_LIST_HEAD(&lo
->idle_worker_list
);
1303 lo
->worker_tree
= RB_ROOT
;
1304 timer_setup(&lo
->timer
, loop_free_idle_workers
,
1306 lo
->use_dio
= lo
->lo_flags
& LO_FLAGS_DIRECT_IO
;
1307 lo
->lo_device
= bdev
;
1308 lo
->lo_backing_file
= file
;
1309 lo
->lo_backing_virt_file
= virt_file
;
1310 lo
->old_gfp_mask
= mapping_gfp_mask(mapping
);
1311 mapping_set_gfp_mask(mapping
, lo
->old_gfp_mask
& ~(__GFP_IO
|__GFP_FS
));
1313 if (!(lo
->lo_flags
& LO_FLAGS_READ_ONLY
) && file
->f_op
->fsync
)
1314 blk_queue_write_cache(lo
->lo_queue
, true, false);
1316 if (config
->block_size
)
1317 bsize
= config
->block_size
;
1318 else if ((lo
->lo_backing_file
->f_flags
& O_DIRECT
) && inode
->i_sb
->s_bdev
)
1319 /* In case of direct I/O, match underlying block size */
1320 bsize
= bdev_logical_block_size(inode
->i_sb
->s_bdev
);
1324 blk_queue_logical_block_size(lo
->lo_queue
, bsize
);
1325 blk_queue_physical_block_size(lo
->lo_queue
, bsize
);
1326 blk_queue_io_min(lo
->lo_queue
, bsize
);
1328 loop_config_discard(lo
);
1329 loop_update_rotational(lo
);
1330 loop_update_dio(lo
);
1331 loop_sysfs_init(lo
);
1333 size
= get_loop_size(lo
, file
);
1334 loop_set_size(lo
, size
);
1336 /* Order wrt reading lo_state in loop_validate_file(). */
1339 lo
->lo_state
= Lo_bound
;
1341 lo
->lo_flags
|= LO_FLAGS_PARTSCAN
;
1342 partscan
= lo
->lo_flags
& LO_FLAGS_PARTSCAN
;
1344 lo
->lo_disk
->flags
&= ~GENHD_FL_NO_PART_SCAN
;
1346 loop_global_unlock(lo
, is_loop
);
1348 loop_reread_partitions(lo
);
1349 if (!(mode
& FMODE_EXCL
))
1350 bd_abort_claiming(bdev
, loop_configure
);
1354 loop_global_unlock(lo
, is_loop
);
1356 if (!(mode
& FMODE_EXCL
))
1357 bd_abort_claiming(bdev
, loop_configure
);
1362 /* This is safe: open() is still holding a reference. */
1363 module_put(THIS_MODULE
);
1367 static int __loop_clr_fd(struct loop_device
*lo
, bool release
)
1369 struct file
*filp
= NULL
;
1370 struct file
*virt_filp
= lo
->lo_backing_virt_file
;
1371 gfp_t gfp
= lo
->old_gfp_mask
;
1372 struct block_device
*bdev
= lo
->lo_device
;
1374 bool partscan
= false;
1376 struct loop_worker
*pos
, *worker
;
1379 * Flush loop_configure() and loop_change_fd(). It is acceptable for
1380 * loop_validate_file() to succeed, for actual clear operation has not
1383 mutex_lock(&loop_validate_mutex
);
1384 mutex_unlock(&loop_validate_mutex
);
1386 * loop_validate_file() now fails because l->lo_state != Lo_bound
1390 mutex_lock(&lo
->lo_mutex
);
1391 if (WARN_ON_ONCE(lo
->lo_state
!= Lo_rundown
)) {
1396 filp
= lo
->lo_backing_file
;
1402 if (test_bit(QUEUE_FLAG_WC
, &lo
->lo_queue
->queue_flags
))
1403 blk_queue_write_cache(lo
->lo_queue
, false, false);
1405 /* freeze request queue during the transition */
1406 blk_mq_freeze_queue(lo
->lo_queue
);
1408 destroy_workqueue(lo
->workqueue
);
1409 spin_lock_irq(&lo
->lo_work_lock
);
1410 list_for_each_entry_safe(worker
, pos
, &lo
->idle_worker_list
,
1412 list_del(&worker
->idle_list
);
1413 rb_erase(&worker
->rb_node
, &lo
->worker_tree
);
1414 css_put(worker
->blkcg_css
);
1417 spin_unlock_irq(&lo
->lo_work_lock
);
1418 del_timer_sync(&lo
->timer
);
1420 spin_lock_irq(&lo
->lo_lock
);
1421 lo
->lo_backing_file
= NULL
;
1422 lo
->lo_backing_virt_file
= NULL
;
1423 spin_unlock_irq(&lo
->lo_lock
);
1425 loop_release_xfer(lo
);
1426 lo
->transfer
= NULL
;
1428 lo
->lo_device
= NULL
;
1429 lo
->lo_encryption
= NULL
;
1431 lo
->lo_sizelimit
= 0;
1432 lo
->lo_encrypt_key_size
= 0;
1433 memset(lo
->lo_encrypt_key
, 0, LO_KEY_SIZE
);
1434 memset(lo
->lo_crypt_name
, 0, LO_NAME_SIZE
);
1435 memset(lo
->lo_file_name
, 0, LO_NAME_SIZE
);
1436 blk_queue_logical_block_size(lo
->lo_queue
, 512);
1437 blk_queue_physical_block_size(lo
->lo_queue
, 512);
1438 blk_queue_io_min(lo
->lo_queue
, 512);
1440 invalidate_bdev(bdev
);
1441 bdev
->bd_inode
->i_mapping
->wb_err
= 0;
1443 set_capacity(lo
->lo_disk
, 0);
1444 loop_sysfs_exit(lo
);
1446 /* let user-space know about this change */
1447 kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, KOBJ_CHANGE
);
1449 mapping_set_gfp_mask(filp
->f_mapping
, gfp
);
1450 /* This is safe: open() is still holding a reference. */
1451 module_put(THIS_MODULE
);
1452 blk_mq_unfreeze_queue(lo
->lo_queue
);
1454 partscan
= lo
->lo_flags
& LO_FLAGS_PARTSCAN
&& bdev
;
1455 lo_number
= lo
->lo_number
;
1456 disk_force_media_change(lo
->lo_disk
, DISK_EVENT_MEDIA_CHANGE
);
1458 mutex_unlock(&lo
->lo_mutex
);
1461 * open_mutex has been held already in release path, so don't
1462 * acquire it if this function is called in such case.
1464 * If the reread partition isn't from release path, lo_refcnt
1465 * must be at least one and it can only become zero when the
1466 * current holder is released.
1469 mutex_lock(&lo
->lo_disk
->open_mutex
);
1470 err
= bdev_disk_changed(lo
->lo_disk
, false);
1472 mutex_unlock(&lo
->lo_disk
->open_mutex
);
1474 pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
1475 __func__
, lo_number
, err
);
1476 /* Device is gone, no point in returning error */
1481 * lo->lo_state is set to Lo_unbound here after above partscan has
1484 * There cannot be anybody else entering __loop_clr_fd() as
1485 * lo->lo_backing_file is already cleared and Lo_rundown state
1486 * protects us from all the other places trying to change the 'lo'
1489 mutex_lock(&lo
->lo_mutex
);
1492 lo
->lo_disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
1493 lo
->lo_state
= Lo_unbound
;
1494 mutex_unlock(&lo
->lo_mutex
);
1497 * Need not hold lo_mutex to fput backing file. Calling fput holding
1498 * lo_mutex triggers a circular lock dependency possibility warning as
1499 * fput can take open_mutex which is usually taken before lo_mutex.
1508 static int loop_clr_fd(struct loop_device
*lo
)
1512 err
= mutex_lock_killable(&lo
->lo_mutex
);
1515 if (lo
->lo_state
!= Lo_bound
) {
1516 mutex_unlock(&lo
->lo_mutex
);
1520 * If we've explicitly asked to tear down the loop device,
1521 * and it has an elevated reference count, set it for auto-teardown when
1522 * the last reference goes away. This stops $!~#$@ udev from
1523 * preventing teardown because it decided that it needs to run blkid on
1524 * the loopback device whenever they appear. xfstests is notorious for
1525 * failing tests because blkid via udev races with a losetup
1526 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1527 * command to fail with EBUSY.
1529 if (atomic_read(&lo
->lo_refcnt
) > 1) {
1530 lo
->lo_flags
|= LO_FLAGS_AUTOCLEAR
;
1531 mutex_unlock(&lo
->lo_mutex
);
1534 lo
->lo_state
= Lo_rundown
;
1535 mutex_unlock(&lo
->lo_mutex
);
1537 return __loop_clr_fd(lo
, false);
1541 loop_set_status(struct loop_device
*lo
, const struct loop_info64
*info
)
1544 kuid_t uid
= current_uid();
1546 bool partscan
= false;
1547 bool size_changed
= false;
1549 err
= mutex_lock_killable(&lo
->lo_mutex
);
1552 if (lo
->lo_encrypt_key_size
&&
1553 !uid_eq(lo
->lo_key_owner
, uid
) &&
1554 !capable(CAP_SYS_ADMIN
)) {
1558 if (lo
->lo_state
!= Lo_bound
) {
1563 if (lo
->lo_offset
!= info
->lo_offset
||
1564 lo
->lo_sizelimit
!= info
->lo_sizelimit
) {
1565 size_changed
= true;
1566 sync_blockdev(lo
->lo_device
);
1567 invalidate_bdev(lo
->lo_device
);
1570 /* I/O need to be drained during transfer transition */
1571 blk_mq_freeze_queue(lo
->lo_queue
);
1573 if (size_changed
&& lo
->lo_device
->bd_inode
->i_mapping
->nrpages
) {
1574 /* If any pages were dirtied after invalidate_bdev(), try again */
1576 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1577 __func__
, lo
->lo_number
, lo
->lo_file_name
,
1578 lo
->lo_device
->bd_inode
->i_mapping
->nrpages
);
1582 prev_lo_flags
= lo
->lo_flags
;
1584 err
= loop_set_status_from_info(lo
, info
);
1588 /* Mask out flags that can't be set using LOOP_SET_STATUS. */
1589 lo
->lo_flags
&= LOOP_SET_STATUS_SETTABLE_FLAGS
;
1590 /* For those flags, use the previous values instead */
1591 lo
->lo_flags
|= prev_lo_flags
& ~LOOP_SET_STATUS_SETTABLE_FLAGS
;
1592 /* For flags that can't be cleared, use previous values too */
1593 lo
->lo_flags
|= prev_lo_flags
& ~LOOP_SET_STATUS_CLEARABLE_FLAGS
;
1596 loff_t new_size
= get_size(lo
->lo_offset
, lo
->lo_sizelimit
,
1597 lo
->lo_backing_file
);
1598 loop_set_size(lo
, new_size
);
1601 loop_config_discard(lo
);
1603 /* update dio if lo_offset or transfer is changed */
1604 __loop_update_dio(lo
, lo
->use_dio
);
1607 blk_mq_unfreeze_queue(lo
->lo_queue
);
1609 if (!err
&& (lo
->lo_flags
& LO_FLAGS_PARTSCAN
) &&
1610 !(prev_lo_flags
& LO_FLAGS_PARTSCAN
)) {
1611 lo
->lo_disk
->flags
&= ~GENHD_FL_NO_PART_SCAN
;
1615 mutex_unlock(&lo
->lo_mutex
);
1617 loop_reread_partitions(lo
);
1623 loop_get_status(struct loop_device
*lo
, struct loop_info64
*info
)
1629 ret
= mutex_lock_killable(&lo
->lo_mutex
);
1632 if (lo
->lo_state
!= Lo_bound
) {
1633 mutex_unlock(&lo
->lo_mutex
);
1637 memset(info
, 0, sizeof(*info
));
1638 info
->lo_number
= lo
->lo_number
;
1639 info
->lo_offset
= lo
->lo_offset
;
1640 info
->lo_sizelimit
= lo
->lo_sizelimit
;
1641 info
->lo_flags
= lo
->lo_flags
;
1642 memcpy(info
->lo_file_name
, lo
->lo_file_name
, LO_NAME_SIZE
);
1643 memcpy(info
->lo_crypt_name
, lo
->lo_crypt_name
, LO_NAME_SIZE
);
1644 info
->lo_encrypt_type
=
1645 lo
->lo_encryption
? lo
->lo_encryption
->number
: 0;
1646 if (lo
->lo_encrypt_key_size
&& capable(CAP_SYS_ADMIN
)) {
1647 info
->lo_encrypt_key_size
= lo
->lo_encrypt_key_size
;
1648 memcpy(info
->lo_encrypt_key
, lo
->lo_encrypt_key
,
1649 lo
->lo_encrypt_key_size
);
1652 /* Drop lo_mutex while we call into the filesystem. */
1653 path
= lo
->lo_backing_file
->f_path
;
1655 mutex_unlock(&lo
->lo_mutex
);
1656 ret
= vfs_getattr(&path
, &stat
, STATX_INO
, AT_STATX_SYNC_AS_STAT
);
1658 info
->lo_device
= huge_encode_dev(stat
.dev
);
1659 info
->lo_inode
= stat
.ino
;
1660 info
->lo_rdevice
= huge_encode_dev(stat
.rdev
);
1667 loop_info64_from_old(const struct loop_info
*info
, struct loop_info64
*info64
)
1669 memset(info64
, 0, sizeof(*info64
));
1670 info64
->lo_number
= info
->lo_number
;
1671 info64
->lo_device
= info
->lo_device
;
1672 info64
->lo_inode
= info
->lo_inode
;
1673 info64
->lo_rdevice
= info
->lo_rdevice
;
1674 info64
->lo_offset
= info
->lo_offset
;
1675 info64
->lo_sizelimit
= 0;
1676 info64
->lo_encrypt_type
= info
->lo_encrypt_type
;
1677 info64
->lo_encrypt_key_size
= info
->lo_encrypt_key_size
;
1678 info64
->lo_flags
= info
->lo_flags
;
1679 info64
->lo_init
[0] = info
->lo_init
[0];
1680 info64
->lo_init
[1] = info
->lo_init
[1];
1681 if (info
->lo_encrypt_type
== LO_CRYPT_CRYPTOAPI
)
1682 memcpy(info64
->lo_crypt_name
, info
->lo_name
, LO_NAME_SIZE
);
1684 memcpy(info64
->lo_file_name
, info
->lo_name
, LO_NAME_SIZE
);
1685 memcpy(info64
->lo_encrypt_key
, info
->lo_encrypt_key
, LO_KEY_SIZE
);
1689 loop_info64_to_old(const struct loop_info64
*info64
, struct loop_info
*info
)
1691 memset(info
, 0, sizeof(*info
));
1692 info
->lo_number
= info64
->lo_number
;
1693 info
->lo_device
= info64
->lo_device
;
1694 info
->lo_inode
= info64
->lo_inode
;
1695 info
->lo_rdevice
= info64
->lo_rdevice
;
1696 info
->lo_offset
= info64
->lo_offset
;
1697 info
->lo_encrypt_type
= info64
->lo_encrypt_type
;
1698 info
->lo_encrypt_key_size
= info64
->lo_encrypt_key_size
;
1699 info
->lo_flags
= info64
->lo_flags
;
1700 info
->lo_init
[0] = info64
->lo_init
[0];
1701 info
->lo_init
[1] = info64
->lo_init
[1];
1702 if (info
->lo_encrypt_type
== LO_CRYPT_CRYPTOAPI
)
1703 memcpy(info
->lo_name
, info64
->lo_crypt_name
, LO_NAME_SIZE
);
1705 memcpy(info
->lo_name
, info64
->lo_file_name
, LO_NAME_SIZE
);
1706 memcpy(info
->lo_encrypt_key
, info64
->lo_encrypt_key
, LO_KEY_SIZE
);
1708 /* error in case values were truncated */
1709 if (info
->lo_device
!= info64
->lo_device
||
1710 info
->lo_rdevice
!= info64
->lo_rdevice
||
1711 info
->lo_inode
!= info64
->lo_inode
||
1712 info
->lo_offset
!= info64
->lo_offset
)
1719 loop_set_status_old(struct loop_device
*lo
, const struct loop_info __user
*arg
)
1721 struct loop_info info
;
1722 struct loop_info64 info64
;
1724 if (copy_from_user(&info
, arg
, sizeof (struct loop_info
)))
1726 loop_info64_from_old(&info
, &info64
);
1727 return loop_set_status(lo
, &info64
);
1731 loop_set_status64(struct loop_device
*lo
, const struct loop_info64 __user
*arg
)
1733 struct loop_info64 info64
;
1735 if (copy_from_user(&info64
, arg
, sizeof (struct loop_info64
)))
1737 return loop_set_status(lo
, &info64
);
1741 loop_get_status_old(struct loop_device
*lo
, struct loop_info __user
*arg
) {
1742 struct loop_info info
;
1743 struct loop_info64 info64
;
1748 err
= loop_get_status(lo
, &info64
);
1750 err
= loop_info64_to_old(&info64
, &info
);
1751 if (!err
&& copy_to_user(arg
, &info
, sizeof(info
)))
1758 loop_get_status64(struct loop_device
*lo
, struct loop_info64 __user
*arg
) {
1759 struct loop_info64 info64
;
1764 err
= loop_get_status(lo
, &info64
);
1765 if (!err
&& copy_to_user(arg
, &info64
, sizeof(info64
)))
1771 static int loop_set_capacity(struct loop_device
*lo
)
1775 if (unlikely(lo
->lo_state
!= Lo_bound
))
1778 size
= get_loop_size(lo
, lo
->lo_backing_file
);
1779 loop_set_size(lo
, size
);
1784 static int loop_set_dio(struct loop_device
*lo
, unsigned long arg
)
1787 if (lo
->lo_state
!= Lo_bound
)
1790 __loop_update_dio(lo
, !!arg
);
1791 if (lo
->use_dio
== !!arg
)
1798 static int loop_set_block_size(struct loop_device
*lo
, unsigned long arg
)
1802 if (lo
->lo_state
!= Lo_bound
)
1805 err
= blk_validate_block_size(arg
);
1809 if (lo
->lo_queue
->limits
.logical_block_size
== arg
)
1812 sync_blockdev(lo
->lo_device
);
1813 invalidate_bdev(lo
->lo_device
);
1815 blk_mq_freeze_queue(lo
->lo_queue
);
1817 /* invalidate_bdev should have truncated all the pages */
1818 if (lo
->lo_device
->bd_inode
->i_mapping
->nrpages
) {
1820 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n",
1821 __func__
, lo
->lo_number
, lo
->lo_file_name
,
1822 lo
->lo_device
->bd_inode
->i_mapping
->nrpages
);
1826 blk_queue_logical_block_size(lo
->lo_queue
, arg
);
1827 blk_queue_physical_block_size(lo
->lo_queue
, arg
);
1828 blk_queue_io_min(lo
->lo_queue
, arg
);
1829 loop_update_dio(lo
);
1831 blk_mq_unfreeze_queue(lo
->lo_queue
);
1836 static int lo_simple_ioctl(struct loop_device
*lo
, unsigned int cmd
,
1841 err
= mutex_lock_killable(&lo
->lo_mutex
);
1845 case LOOP_SET_CAPACITY
:
1846 err
= loop_set_capacity(lo
);
1848 case LOOP_SET_DIRECT_IO
:
1849 err
= loop_set_dio(lo
, arg
);
1851 case LOOP_SET_BLOCK_SIZE
:
1852 err
= loop_set_block_size(lo
, arg
);
1855 err
= lo
->ioctl
? lo
->ioctl(lo
, cmd
, arg
) : -EINVAL
;
1857 mutex_unlock(&lo
->lo_mutex
);
1861 static int lo_ioctl(struct block_device
*bdev
, fmode_t mode
,
1862 unsigned int cmd
, unsigned long arg
)
1864 struct loop_device
*lo
= bdev
->bd_disk
->private_data
;
1865 void __user
*argp
= (void __user
*) arg
;
1871 * Legacy case - pass in a zeroed out struct loop_config with
1872 * only the file descriptor set , which corresponds with the
1873 * default parameters we'd have used otherwise.
1875 struct loop_config config
;
1877 memset(&config
, 0, sizeof(config
));
1880 return loop_configure(lo
, mode
, bdev
, &config
);
1882 case LOOP_CONFIGURE
: {
1883 struct loop_config config
;
1885 if (copy_from_user(&config
, argp
, sizeof(config
)))
1888 return loop_configure(lo
, mode
, bdev
, &config
);
1890 case LOOP_CHANGE_FD
:
1891 return loop_change_fd(lo
, bdev
, arg
);
1893 return loop_clr_fd(lo
);
1894 case LOOP_SET_STATUS
:
1896 if ((mode
& FMODE_WRITE
) || capable(CAP_SYS_ADMIN
)) {
1897 err
= loop_set_status_old(lo
, argp
);
1900 case LOOP_GET_STATUS
:
1901 return loop_get_status_old(lo
, argp
);
1902 case LOOP_SET_STATUS64
:
1904 if ((mode
& FMODE_WRITE
) || capable(CAP_SYS_ADMIN
)) {
1905 err
= loop_set_status64(lo
, argp
);
1908 case LOOP_GET_STATUS64
:
1909 return loop_get_status64(lo
, argp
);
1910 case LOOP_SET_CAPACITY
:
1911 case LOOP_SET_DIRECT_IO
:
1912 case LOOP_SET_BLOCK_SIZE
:
1913 if (!(mode
& FMODE_WRITE
) && !capable(CAP_SYS_ADMIN
))
1917 err
= lo_simple_ioctl(lo
, cmd
, arg
);
1924 #ifdef CONFIG_COMPAT
1925 struct compat_loop_info
{
1926 compat_int_t lo_number
; /* ioctl r/o */
1927 compat_dev_t lo_device
; /* ioctl r/o */
1928 compat_ulong_t lo_inode
; /* ioctl r/o */
1929 compat_dev_t lo_rdevice
; /* ioctl r/o */
1930 compat_int_t lo_offset
;
1931 compat_int_t lo_encrypt_type
;
1932 compat_int_t lo_encrypt_key_size
; /* ioctl w/o */
1933 compat_int_t lo_flags
; /* ioctl r/o */
1934 char lo_name
[LO_NAME_SIZE
];
1935 unsigned char lo_encrypt_key
[LO_KEY_SIZE
]; /* ioctl w/o */
1936 compat_ulong_t lo_init
[2];
1941 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1942 * - noinlined to reduce stack space usage in main part of driver
1945 loop_info64_from_compat(const struct compat_loop_info __user
*arg
,
1946 struct loop_info64
*info64
)
1948 struct compat_loop_info info
;
1950 if (copy_from_user(&info
, arg
, sizeof(info
)))
1953 memset(info64
, 0, sizeof(*info64
));
1954 info64
->lo_number
= info
.lo_number
;
1955 info64
->lo_device
= info
.lo_device
;
1956 info64
->lo_inode
= info
.lo_inode
;
1957 info64
->lo_rdevice
= info
.lo_rdevice
;
1958 info64
->lo_offset
= info
.lo_offset
;
1959 info64
->lo_sizelimit
= 0;
1960 info64
->lo_encrypt_type
= info
.lo_encrypt_type
;
1961 info64
->lo_encrypt_key_size
= info
.lo_encrypt_key_size
;
1962 info64
->lo_flags
= info
.lo_flags
;
1963 info64
->lo_init
[0] = info
.lo_init
[0];
1964 info64
->lo_init
[1] = info
.lo_init
[1];
1965 if (info
.lo_encrypt_type
== LO_CRYPT_CRYPTOAPI
)
1966 memcpy(info64
->lo_crypt_name
, info
.lo_name
, LO_NAME_SIZE
);
1968 memcpy(info64
->lo_file_name
, info
.lo_name
, LO_NAME_SIZE
);
1969 memcpy(info64
->lo_encrypt_key
, info
.lo_encrypt_key
, LO_KEY_SIZE
);
1974 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1975 * - noinlined to reduce stack space usage in main part of driver
1978 loop_info64_to_compat(const struct loop_info64
*info64
,
1979 struct compat_loop_info __user
*arg
)
1981 struct compat_loop_info info
;
1983 memset(&info
, 0, sizeof(info
));
1984 info
.lo_number
= info64
->lo_number
;
1985 info
.lo_device
= info64
->lo_device
;
1986 info
.lo_inode
= info64
->lo_inode
;
1987 info
.lo_rdevice
= info64
->lo_rdevice
;
1988 info
.lo_offset
= info64
->lo_offset
;
1989 info
.lo_encrypt_type
= info64
->lo_encrypt_type
;
1990 info
.lo_encrypt_key_size
= info64
->lo_encrypt_key_size
;
1991 info
.lo_flags
= info64
->lo_flags
;
1992 info
.lo_init
[0] = info64
->lo_init
[0];
1993 info
.lo_init
[1] = info64
->lo_init
[1];
1994 if (info
.lo_encrypt_type
== LO_CRYPT_CRYPTOAPI
)
1995 memcpy(info
.lo_name
, info64
->lo_crypt_name
, LO_NAME_SIZE
);
1997 memcpy(info
.lo_name
, info64
->lo_file_name
, LO_NAME_SIZE
);
1998 memcpy(info
.lo_encrypt_key
, info64
->lo_encrypt_key
, LO_KEY_SIZE
);
2000 /* error in case values were truncated */
2001 if (info
.lo_device
!= info64
->lo_device
||
2002 info
.lo_rdevice
!= info64
->lo_rdevice
||
2003 info
.lo_inode
!= info64
->lo_inode
||
2004 info
.lo_offset
!= info64
->lo_offset
||
2005 info
.lo_init
[0] != info64
->lo_init
[0] ||
2006 info
.lo_init
[1] != info64
->lo_init
[1])
2009 if (copy_to_user(arg
, &info
, sizeof(info
)))
2015 loop_set_status_compat(struct loop_device
*lo
,
2016 const struct compat_loop_info __user
*arg
)
2018 struct loop_info64 info64
;
2021 ret
= loop_info64_from_compat(arg
, &info64
);
2024 return loop_set_status(lo
, &info64
);
2028 loop_get_status_compat(struct loop_device
*lo
,
2029 struct compat_loop_info __user
*arg
)
2031 struct loop_info64 info64
;
2036 err
= loop_get_status(lo
, &info64
);
2038 err
= loop_info64_to_compat(&info64
, arg
);
2042 static int lo_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
2043 unsigned int cmd
, unsigned long arg
)
2045 struct loop_device
*lo
= bdev
->bd_disk
->private_data
;
2049 case LOOP_SET_STATUS
:
2050 err
= loop_set_status_compat(lo
,
2051 (const struct compat_loop_info __user
*)arg
);
2053 case LOOP_GET_STATUS
:
2054 err
= loop_get_status_compat(lo
,
2055 (struct compat_loop_info __user
*)arg
);
2057 case LOOP_SET_CAPACITY
:
2059 case LOOP_GET_STATUS64
:
2060 case LOOP_SET_STATUS64
:
2061 case LOOP_CONFIGURE
:
2062 arg
= (unsigned long) compat_ptr(arg
);
2065 case LOOP_CHANGE_FD
:
2066 case LOOP_SET_BLOCK_SIZE
:
2067 case LOOP_SET_DIRECT_IO
:
2068 err
= lo_ioctl(bdev
, mode
, cmd
, arg
);
2078 static int lo_open(struct block_device
*bdev
, fmode_t mode
)
2080 struct loop_device
*lo
= bdev
->bd_disk
->private_data
;
2083 err
= mutex_lock_killable(&lo
->lo_mutex
);
2086 if (lo
->lo_state
== Lo_deleting
)
2089 atomic_inc(&lo
->lo_refcnt
);
2090 mutex_unlock(&lo
->lo_mutex
);
2094 static void lo_release(struct gendisk
*disk
, fmode_t mode
)
2096 struct loop_device
*lo
= disk
->private_data
;
2098 mutex_lock(&lo
->lo_mutex
);
2099 if (atomic_dec_return(&lo
->lo_refcnt
))
2102 if (lo
->lo_flags
& LO_FLAGS_AUTOCLEAR
) {
2103 if (lo
->lo_state
!= Lo_bound
)
2105 lo
->lo_state
= Lo_rundown
;
2106 mutex_unlock(&lo
->lo_mutex
);
2108 * In autoclear mode, stop the loop thread
2109 * and remove configuration after last close.
2111 __loop_clr_fd(lo
, true);
2113 } else if (lo
->lo_state
== Lo_bound
) {
2115 * Otherwise keep thread (if running) and config,
2116 * but flush possible ongoing bios in thread.
2118 blk_mq_freeze_queue(lo
->lo_queue
);
2119 blk_mq_unfreeze_queue(lo
->lo_queue
);
2123 mutex_unlock(&lo
->lo_mutex
);
2126 static const struct block_device_operations lo_fops
= {
2127 .owner
= THIS_MODULE
,
2129 .release
= lo_release
,
2131 #ifdef CONFIG_COMPAT
2132 .compat_ioctl
= lo_compat_ioctl
,
2137 * And now the modules code and kernel interface.
2139 static int max_loop
;
2140 module_param(max_loop
, int, 0444);
2141 MODULE_PARM_DESC(max_loop
, "Maximum number of loop devices");
2142 module_param(max_part
, int, 0444);
2143 MODULE_PARM_DESC(max_part
, "Maximum number of partitions per loop device");
2144 MODULE_LICENSE("GPL");
2145 MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR
);
2147 int loop_register_transfer(struct loop_func_table
*funcs
)
2149 unsigned int n
= funcs
->number
;
2151 if (n
>= MAX_LO_CRYPT
|| xfer_funcs
[n
])
2153 xfer_funcs
[n
] = funcs
;
2157 int loop_unregister_transfer(int number
)
2159 unsigned int n
= number
;
2160 struct loop_func_table
*xfer
;
2162 if (n
== 0 || n
>= MAX_LO_CRYPT
|| (xfer
= xfer_funcs
[n
]) == NULL
)
2165 * This function is called from only cleanup_cryptoloop().
2166 * Given that each loop device that has a transfer enabled holds a
2167 * reference to the module implementing it we should never get here
2168 * with a transfer that is set (unless forced module unloading is
2169 * requested). Thus, check module's refcount and warn if this is
2170 * not a clean unloading.
2172 #ifdef CONFIG_MODULE_UNLOAD
2173 if (xfer
->owner
&& module_refcount(xfer
->owner
) != -1)
2174 pr_err("Danger! Unregistering an in use transfer function.\n");
2177 xfer_funcs
[n
] = NULL
;
2181 EXPORT_SYMBOL(loop_register_transfer
);
2182 EXPORT_SYMBOL(loop_unregister_transfer
);
2184 static blk_status_t
loop_queue_rq(struct blk_mq_hw_ctx
*hctx
,
2185 const struct blk_mq_queue_data
*bd
)
2187 struct request
*rq
= bd
->rq
;
2188 struct loop_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
2189 struct loop_device
*lo
= rq
->q
->queuedata
;
2191 blk_mq_start_request(rq
);
2193 if (lo
->lo_state
!= Lo_bound
)
2194 return BLK_STS_IOERR
;
2196 switch (req_op(rq
)) {
2198 case REQ_OP_DISCARD
:
2199 case REQ_OP_WRITE_ZEROES
:
2200 cmd
->use_aio
= false;
2203 cmd
->use_aio
= lo
->use_dio
;
2207 /* always use the first bio's css */
2208 cmd
->blkcg_css
= NULL
;
2209 cmd
->memcg_css
= NULL
;
2210 #ifdef CONFIG_BLK_CGROUP
2211 if (rq
->bio
&& rq
->bio
->bi_blkg
) {
2212 cmd
->blkcg_css
= &bio_blkcg(rq
->bio
)->css
;
2215 cgroup_get_e_css(cmd
->blkcg_css
->cgroup
,
2216 &memory_cgrp_subsys
);
2220 loop_queue_work(lo
, cmd
);
2225 static void loop_handle_cmd(struct loop_cmd
*cmd
)
2227 struct request
*rq
= blk_mq_rq_from_pdu(cmd
);
2228 const bool write
= op_is_write(req_op(rq
));
2229 struct loop_device
*lo
= rq
->q
->queuedata
;
2231 struct mem_cgroup
*old_memcg
= NULL
;
2233 if (write
&& (lo
->lo_flags
& LO_FLAGS_READ_ONLY
)) {
2239 kthread_associate_blkcg(cmd
->blkcg_css
);
2241 old_memcg
= set_active_memcg(
2242 mem_cgroup_from_css(cmd
->memcg_css
));
2244 ret
= do_req_filebacked(lo
, rq
);
2247 kthread_associate_blkcg(NULL
);
2249 if (cmd
->memcg_css
) {
2250 set_active_memcg(old_memcg
);
2251 css_put(cmd
->memcg_css
);
2254 /* complete non-aio request */
2255 if (!cmd
->use_aio
|| ret
) {
2256 if (ret
== -EOPNOTSUPP
)
2259 cmd
->ret
= ret
? -EIO
: 0;
2260 if (likely(!blk_should_fake_timeout(rq
->q
)))
2261 blk_mq_complete_request(rq
);
2265 static void loop_set_timer(struct loop_device
*lo
)
2267 timer_reduce(&lo
->timer
, jiffies
+ LOOP_IDLE_WORKER_TIMEOUT
);
2270 static void loop_process_work(struct loop_worker
*worker
,
2271 struct list_head
*cmd_list
, struct loop_device
*lo
)
2273 int orig_flags
= current
->flags
;
2274 struct loop_cmd
*cmd
;
2276 current
->flags
|= PF_LOCAL_THROTTLE
| PF_MEMALLOC_NOIO
;
2277 spin_lock_irq(&lo
->lo_work_lock
);
2278 while (!list_empty(cmd_list
)) {
2280 cmd_list
->next
, struct loop_cmd
, list_entry
);
2281 list_del(cmd_list
->next
);
2282 spin_unlock_irq(&lo
->lo_work_lock
);
2284 loop_handle_cmd(cmd
);
2287 spin_lock_irq(&lo
->lo_work_lock
);
2291 * We only add to the idle list if there are no pending cmds
2292 * *and* the worker will not run again which ensures that it
2293 * is safe to free any worker on the idle list
2295 if (worker
&& !work_pending(&worker
->work
)) {
2296 worker
->last_ran_at
= jiffies
;
2297 list_add_tail(&worker
->idle_list
, &lo
->idle_worker_list
);
2300 spin_unlock_irq(&lo
->lo_work_lock
);
2301 current
->flags
= orig_flags
;
2304 static void loop_workfn(struct work_struct
*work
)
2306 struct loop_worker
*worker
=
2307 container_of(work
, struct loop_worker
, work
);
2308 loop_process_work(worker
, &worker
->cmd_list
, worker
->lo
);
2311 static void loop_rootcg_workfn(struct work_struct
*work
)
2313 struct loop_device
*lo
=
2314 container_of(work
, struct loop_device
, rootcg_work
);
2315 loop_process_work(NULL
, &lo
->rootcg_cmd_list
, lo
);
2318 static void loop_free_idle_workers(struct timer_list
*timer
)
2320 struct loop_device
*lo
= container_of(timer
, struct loop_device
, timer
);
2321 struct loop_worker
*pos
, *worker
;
2323 spin_lock_irq(&lo
->lo_work_lock
);
2324 list_for_each_entry_safe(worker
, pos
, &lo
->idle_worker_list
,
2326 if (time_is_after_jiffies(worker
->last_ran_at
+
2327 LOOP_IDLE_WORKER_TIMEOUT
))
2329 list_del(&worker
->idle_list
);
2330 rb_erase(&worker
->rb_node
, &lo
->worker_tree
);
2331 css_put(worker
->blkcg_css
);
2334 if (!list_empty(&lo
->idle_worker_list
))
2336 spin_unlock_irq(&lo
->lo_work_lock
);
2339 static const struct blk_mq_ops loop_mq_ops
= {
2340 .queue_rq
= loop_queue_rq
,
2341 .complete
= lo_complete_rq
,
2344 static int loop_add(int i
)
2346 struct loop_device
*lo
;
2347 struct gendisk
*disk
;
2351 lo
= kzalloc(sizeof(*lo
), GFP_KERNEL
);
2354 lo
->lo_state
= Lo_unbound
;
2356 err
= mutex_lock_killable(&loop_ctl_mutex
);
2360 /* allocate id, if @id >= 0, we're requesting that specific id */
2362 err
= idr_alloc(&loop_index_idr
, lo
, i
, i
+ 1, GFP_KERNEL
);
2366 err
= idr_alloc(&loop_index_idr
, lo
, 0, 0, GFP_KERNEL
);
2368 mutex_unlock(&loop_ctl_mutex
);
2374 lo
->tag_set
.ops
= &loop_mq_ops
;
2375 lo
->tag_set
.nr_hw_queues
= 1;
2376 lo
->tag_set
.queue_depth
= 128;
2377 lo
->tag_set
.numa_node
= NUMA_NO_NODE
;
2378 lo
->tag_set
.cmd_size
= sizeof(struct loop_cmd
);
2379 lo
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
| BLK_MQ_F_STACKING
|
2380 BLK_MQ_F_NO_SCHED_BY_DEFAULT
;
2381 lo
->tag_set
.driver_data
= lo
;
2383 err
= blk_mq_alloc_tag_set(&lo
->tag_set
);
2387 disk
= lo
->lo_disk
= blk_mq_alloc_disk(&lo
->tag_set
, lo
);
2389 err
= PTR_ERR(disk
);
2390 goto out_cleanup_tags
;
2392 lo
->lo_queue
= lo
->lo_disk
->queue
;
2394 blk_queue_max_hw_sectors(lo
->lo_queue
, BLK_DEF_MAX_SECTORS
);
2397 * By default, we do buffer IO, so it doesn't make sense to enable
2398 * merge because the I/O submitted to backing file is handled page by
2399 * page. For directio mode, merge does help to dispatch bigger request
2400 * to underlayer disk. We will enable merge once directio is enabled.
2402 blk_queue_flag_set(QUEUE_FLAG_NOMERGES
, lo
->lo_queue
);
2405 * Disable partition scanning by default. The in-kernel partition
2406 * scanning can be requested individually per-device during its
2407 * setup. Userspace can always add and remove partitions from all
2408 * devices. The needed partition minors are allocated from the
2409 * extended minor space, the main loop device numbers will continue
2410 * to match the loop minors, regardless of the number of partitions
2413 * If max_part is given, partition scanning is globally enabled for
2414 * all loop devices. The minors for the main loop devices will be
2415 * multiples of max_part.
2417 * Note: Global-for-all-devices, set-only-at-init, read-only module
2418 * parameteters like 'max_loop' and 'max_part' make things needlessly
2419 * complicated, are too static, inflexible and may surprise
2420 * userspace tools. Parameters like this in general should be avoided.
2423 disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
2424 disk
->flags
|= GENHD_FL_EXT_DEVT
;
2425 atomic_set(&lo
->lo_refcnt
, 0);
2426 mutex_init(&lo
->lo_mutex
);
2428 spin_lock_init(&lo
->lo_lock
);
2429 spin_lock_init(&lo
->lo_work_lock
);
2430 disk
->major
= LOOP_MAJOR
;
2431 disk
->first_minor
= i
<< part_shift
;
2432 disk
->minors
= 1 << part_shift
;
2433 disk
->fops
= &lo_fops
;
2434 disk
->private_data
= lo
;
2435 disk
->queue
= lo
->lo_queue
;
2436 disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
2437 disk
->event_flags
= DISK_EVENT_FLAG_UEVENT
;
2438 sprintf(disk
->disk_name
, "loop%d", i
);
2439 /* Make this loop device reachable from pathname. */
2441 /* Show this loop device. */
2442 mutex_lock(&loop_ctl_mutex
);
2443 lo
->idr_visible
= true;
2444 mutex_unlock(&loop_ctl_mutex
);
2448 blk_mq_free_tag_set(&lo
->tag_set
);
2450 mutex_lock(&loop_ctl_mutex
);
2451 idr_remove(&loop_index_idr
, i
);
2452 mutex_unlock(&loop_ctl_mutex
);
2459 static void loop_remove(struct loop_device
*lo
)
2461 /* Make this loop device unreachable from pathname. */
2462 del_gendisk(lo
->lo_disk
);
2463 blk_cleanup_disk(lo
->lo_disk
);
2464 blk_mq_free_tag_set(&lo
->tag_set
);
2465 mutex_lock(&loop_ctl_mutex
);
2466 idr_remove(&loop_index_idr
, lo
->lo_number
);
2467 mutex_unlock(&loop_ctl_mutex
);
2468 /* There is no route which can find this loop device. */
2469 mutex_destroy(&lo
->lo_mutex
);
2473 static void loop_probe(dev_t dev
)
2475 int idx
= MINOR(dev
) >> part_shift
;
2477 if (max_loop
&& idx
>= max_loop
)
2482 static int loop_control_remove(int idx
)
2484 struct loop_device
*lo
;
2488 pr_warn_once("deleting an unspecified loop device is not supported.\n");
2492 /* Hide this loop device for serialization. */
2493 ret
= mutex_lock_killable(&loop_ctl_mutex
);
2496 lo
= idr_find(&loop_index_idr
, idx
);
2497 if (!lo
|| !lo
->idr_visible
)
2500 lo
->idr_visible
= false;
2501 mutex_unlock(&loop_ctl_mutex
);
2505 /* Check whether this loop device can be removed. */
2506 ret
= mutex_lock_killable(&lo
->lo_mutex
);
2509 if (lo
->lo_state
!= Lo_unbound
||
2510 atomic_read(&lo
->lo_refcnt
) > 0) {
2511 mutex_unlock(&lo
->lo_mutex
);
2515 /* Mark this loop device no longer open()-able. */
2516 lo
->lo_state
= Lo_deleting
;
2517 mutex_unlock(&lo
->lo_mutex
);
2523 /* Show this loop device again. */
2524 mutex_lock(&loop_ctl_mutex
);
2525 lo
->idr_visible
= true;
2526 mutex_unlock(&loop_ctl_mutex
);
2530 static int loop_control_get_free(int idx
)
2532 struct loop_device
*lo
;
2535 ret
= mutex_lock_killable(&loop_ctl_mutex
);
2538 idr_for_each_entry(&loop_index_idr
, lo
, id
) {
2539 /* Hitting a race results in creating a new loop device which is harmless. */
2540 if (lo
->idr_visible
&& data_race(lo
->lo_state
) == Lo_unbound
)
2543 mutex_unlock(&loop_ctl_mutex
);
2544 return loop_add(-1);
2546 mutex_unlock(&loop_ctl_mutex
);
2550 static long loop_control_ioctl(struct file
*file
, unsigned int cmd
,
2555 return loop_add(parm
);
2556 case LOOP_CTL_REMOVE
:
2557 return loop_control_remove(parm
);
2558 case LOOP_CTL_GET_FREE
:
2559 return loop_control_get_free(parm
);
2565 static const struct file_operations loop_ctl_fops
= {
2566 .open
= nonseekable_open
,
2567 .unlocked_ioctl
= loop_control_ioctl
,
2568 .compat_ioctl
= loop_control_ioctl
,
2569 .owner
= THIS_MODULE
,
2570 .llseek
= noop_llseek
,
2573 static struct miscdevice loop_misc
= {
2574 .minor
= LOOP_CTRL_MINOR
,
2575 .name
= "loop-control",
2576 .fops
= &loop_ctl_fops
,
2579 MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR
);
2580 MODULE_ALIAS("devname:loop-control");
2582 static int __init
loop_init(void)
2589 part_shift
= fls(max_part
);
2592 * Adjust max_part according to part_shift as it is exported
2593 * to user space so that user can decide correct minor number
2594 * if [s]he want to create more devices.
2596 * Note that -1 is required because partition 0 is reserved
2597 * for the whole disk.
2599 max_part
= (1UL << part_shift
) - 1;
2602 if ((1UL << part_shift
) > DISK_MAX_PARTS
) {
2607 if (max_loop
> 1UL << (MINORBITS
- part_shift
)) {
2613 * If max_loop is specified, create that many devices upfront.
2614 * This also becomes a hard limit. If max_loop is not specified,
2615 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
2616 * init time. Loop devices can be requested on-demand with the
2617 * /dev/loop-control interface, or be instantiated by accessing
2618 * a 'dead' device node.
2623 nr
= CONFIG_BLK_DEV_LOOP_MIN_COUNT
;
2625 err
= misc_register(&loop_misc
);
2630 if (__register_blkdev(LOOP_MAJOR
, "loop", loop_probe
)) {
2635 /* pre-create number of devices given by config or max_loop */
2636 for (i
= 0; i
< nr
; i
++)
2639 printk(KERN_INFO
"loop: module loaded\n");
2643 misc_deregister(&loop_misc
);
2648 static void __exit
loop_exit(void)
2650 struct loop_device
*lo
;
2653 unregister_blkdev(LOOP_MAJOR
, "loop");
2654 misc_deregister(&loop_misc
);
2657 * There is no need to use loop_ctl_mutex here, for nobody else can
2658 * access loop_index_idr when this module is unloading (unless forced
2659 * module unloading is requested). If this is not a clean unloading,
2660 * we have no means to avoid kernel crash.
2662 idr_for_each_entry(&loop_index_idr
, lo
, id
)
2665 idr_destroy(&loop_index_idr
);
2668 module_init(loop_init
);
2669 module_exit(loop_exit
);
2672 static int __init
max_loop_setup(char *str
)
2674 max_loop
= simple_strtol(str
, NULL
, 0);
2678 __setup("max_loop=", max_loop_setup
);