block: loop: say goodby to bio
[linux-2.6-block.git] / drivers / block / loop.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/block/loop.c
3 *
4 * Written by Theodore Ts'o, 3/29/93
5 *
6 * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
7 * permitted under the GNU General Public License.
8 *
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
11 *
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
14 *
15 * Fixed do_loop_request() re-entrancy - Vincent.Renardias@waw.com Mar 20, 1997
16 *
17 * Added devfs support - Richard Gooch <rgooch@atnf.csiro.au> 16-Jan-1998
18 *
19 * Handle sparse backing files correctly - Kenn Humborg, Jun 28, 1998
20 *
21 * Loadable modules and other fixes by AK, 1998
22 *
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
26 *
27 * Maximum number of loop devices now dynamic via max_loop module parameter.
28 * Russell Kroll <rkroll@exploits.org> 19990701
29 *
30 * Maximum number of loop devices when compiled-in now selectable by passing
31 * max_loop=<1-255> to the kernel on boot.
96de0e25 32 * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999
1da177e4
LT
33 *
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
36 * Al Viro too.
37 * Jens Axboe <axboe@suse.de>, Nov 2000
38 *
39 * Support up to 256 loop devices
40 * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
41 *
42 * Support for falling back on the write file operation when the address space
4e02ed4b 43 * operations write_begin is not available on the backing filesystem.
1da177e4
LT
44 * Anton Altaparmakov, 16 Feb 2005
45 *
46 * Still To Fix:
47 * - Advisory locking is ignored here.
48 * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
49 *
50 */
51
1da177e4
LT
52#include <linux/module.h>
53#include <linux/moduleparam.h>
54#include <linux/sched.h>
55#include <linux/fs.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>
1da177e4
LT
64#include <linux/swap.h>
65#include <linux/slab.h>
863d5b82 66#include <linux/compat.h>
1da177e4 67#include <linux/suspend.h>
83144186 68#include <linux/freezer.h>
2a48fc0a 69#include <linux/mutex.h>
1da177e4 70#include <linux/writeback.h>
1da177e4
LT
71#include <linux/completion.h>
72#include <linux/highmem.h>
6c997918 73#include <linux/kthread.h>
d6b29d7c 74#include <linux/splice.h>
ee862730 75#include <linux/sysfs.h>
770fe30a 76#include <linux/miscdevice.h>
dfaa2ef6 77#include <linux/falloc.h>
b5dd2f60 78#include <linux/blk-mq.h>
83a87611 79#include "loop.h"
1da177e4
LT
80
81#include <asm/uaccess.h>
82
34dd82af
KS
83static DEFINE_IDR(loop_index_idr);
84static DEFINE_MUTEX(loop_index_mutex);
1da177e4 85
476a4813
LV
86static int max_part;
87static int part_shift;
88
b5dd2f60
ML
89static struct workqueue_struct *loop_wq;
90
1da177e4
LT
91/*
92 * Transfer functions
93 */
94static 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)
98{
cfd8005c
CW
99 char *raw_buf = kmap_atomic(raw_page) + raw_off;
100 char *loop_buf = kmap_atomic(loop_page) + loop_off;
1da177e4
LT
101
102 if (cmd == READ)
103 memcpy(loop_buf, raw_buf, size);
104 else
105 memcpy(raw_buf, loop_buf, size);
106
cfd8005c
CW
107 kunmap_atomic(loop_buf);
108 kunmap_atomic(raw_buf);
1da177e4
LT
109 cond_resched();
110 return 0;
111}
112
113static 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)
117{
cfd8005c
CW
118 char *raw_buf = kmap_atomic(raw_page) + raw_off;
119 char *loop_buf = kmap_atomic(loop_page) + loop_off;
1da177e4
LT
120 char *in, *out, *key;
121 int i, keysize;
122
123 if (cmd == READ) {
124 in = raw_buf;
125 out = loop_buf;
126 } else {
127 in = loop_buf;
128 out = raw_buf;
129 }
130
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];
135
cfd8005c
CW
136 kunmap_atomic(loop_buf);
137 kunmap_atomic(raw_buf);
1da177e4
LT
138 cond_resched();
139 return 0;
140}
141
142static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
143{
144 if (unlikely(info->lo_encrypt_key_size <= 0))
145 return -EINVAL;
146 return 0;
147}
148
149static struct loop_func_table none_funcs = {
150 .number = LO_CRYPT_NONE,
151 .transfer = transfer_none,
152};
153
154static struct loop_func_table xor_funcs = {
155 .number = LO_CRYPT_XOR,
156 .transfer = transfer_xor,
157 .init = xor_init
158};
159
160/* xfer_funcs[0] is special - its release function is never called */
161static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
162 &none_funcs,
163 &xor_funcs
164};
165
7035b5df 166static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
1da177e4 167{
b7a1da69 168 loff_t loopsize;
1da177e4
LT
169
170 /* Compute loopsize in bytes */
b7a1da69
GC
171 loopsize = i_size_read(file->f_mapping->host);
172 if (offset > 0)
173 loopsize -= offset;
174 /* offset is beyond i_size, weird but possible */
7035b5df
DM
175 if (loopsize < 0)
176 return 0;
1da177e4 177
7035b5df
DM
178 if (sizelimit > 0 && sizelimit < loopsize)
179 loopsize = sizelimit;
1da177e4
LT
180 /*
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.
183 */
184 return loopsize >> 9;
185}
186
7035b5df
DM
187static loff_t get_loop_size(struct loop_device *lo, struct file *file)
188{
189 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
190}
191
1da177e4 192static int
7035b5df 193figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
1da177e4 194{
7035b5df 195 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
1da177e4 196 sector_t x = (sector_t)size;
7b0576a3 197 struct block_device *bdev = lo->lo_device;
1da177e4
LT
198
199 if (unlikely((loff_t)x != size))
200 return -EFBIG;
7035b5df
DM
201 if (lo->lo_offset != offset)
202 lo->lo_offset = offset;
203 if (lo->lo_sizelimit != sizelimit)
204 lo->lo_sizelimit = sizelimit;
73285082 205 set_capacity(lo->lo_disk, x);
7b0576a3
GC
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);
7035b5df 209 return 0;
1da177e4
LT
210}
211
212static inline int
213lo_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)
217{
218 if (unlikely(!lo->transfer))
219 return 0;
220
221 return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
222}
223
1da177e4
LT
224/**
225 * __do_lo_send_write - helper for writing data to a loop device
226 *
227 * This helper just factors out common code between do_lo_send_direct_write()
228 * and do_lo_send_write().
229 */
858119e1 230static int __do_lo_send_write(struct file *file,
98ae6ccd 231 u8 *buf, const int len, loff_t pos)
1da177e4
LT
232{
233 ssize_t bw;
234 mm_segment_t old_fs = get_fs();
235
03d95eb2 236 file_start_write(file);
1da177e4
LT
237 set_fs(get_ds());
238 bw = file->f_op->write(file, buf, len, &pos);
239 set_fs(old_fs);
03d95eb2 240 file_end_write(file);
1da177e4
LT
241 if (likely(bw == len))
242 return 0;
44bd70c3 243 printk_ratelimited(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
1da177e4
LT
244 (unsigned long long)pos, len);
245 if (bw >= 0)
246 bw = -EIO;
247 return bw;
248}
249
250/**
251 * do_lo_send_direct_write - helper for writing data to a loop device
252 *
456be148
CH
253 * This is the fast, non-transforming version that does not need double
254 * buffering.
1da177e4
LT
255 */
256static int do_lo_send_direct_write(struct loop_device *lo,
511de73f 257 struct bio_vec *bvec, loff_t pos, struct page *page)
1da177e4
LT
258{
259 ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
98ae6ccd 260 kmap(bvec->bv_page) + bvec->bv_offset,
1da177e4
LT
261 bvec->bv_len, pos);
262 kunmap(bvec->bv_page);
263 cond_resched();
264 return bw;
265}
266
267/**
268 * do_lo_send_write - helper for writing data to a loop device
269 *
456be148
CH
270 * This is the slow, transforming version that needs to double buffer the
271 * data as it cannot do the transformations in place without having direct
272 * access to the destination pages of the backing file.
1da177e4
LT
273 */
274static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
511de73f 275 loff_t pos, struct page *page)
1da177e4
LT
276{
277 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
278 bvec->bv_offset, bvec->bv_len, pos >> 9);
279 if (likely(!ret))
280 return __do_lo_send_write(lo->lo_backing_file,
98ae6ccd 281 page_address(page), bvec->bv_len,
1da177e4 282 pos);
44bd70c3 283 printk_ratelimited(KERN_ERR "loop: Transfer error at byte offset %llu, "
1da177e4
LT
284 "length %i.\n", (unsigned long long)pos, bvec->bv_len);
285 if (ret > 0)
286 ret = -EIO;
287 return ret;
288}
289
30112013 290static int lo_send(struct loop_device *lo, struct request *rq, loff_t pos)
1da177e4 291{
511de73f 292 int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
1da177e4 293 struct page *page);
7988613b 294 struct bio_vec bvec;
30112013 295 struct req_iterator iter;
1da177e4 296 struct page *page = NULL;
7988613b 297 int ret = 0;
1da177e4 298
456be148
CH
299 if (lo->transfer != transfer_none) {
300 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
301 if (unlikely(!page))
302 goto fail;
303 kmap(page);
304 do_lo_send = do_lo_send_write;
305 } else {
1da177e4 306 do_lo_send = do_lo_send_direct_write;
1da177e4 307 }
456be148 308
30112013 309 rq_for_each_segment(bvec, rq, iter) {
7988613b 310 ret = do_lo_send(lo, &bvec, pos, page);
1da177e4
LT
311 if (ret < 0)
312 break;
7988613b 313 pos += bvec.bv_len;
1da177e4
LT
314 }
315 if (page) {
316 kunmap(page);
317 __free_page(page);
318 }
319out:
320 return ret;
321fail:
44bd70c3 322 printk_ratelimited(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
1da177e4
LT
323 ret = -ENOMEM;
324 goto out;
325}
326
327struct lo_read_data {
328 struct loop_device *lo;
329 struct page *page;
330 unsigned offset;
331 int bsize;
332};
333
334static int
fd582140
JA
335lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
336 struct splice_desc *sd)
1da177e4 337{
fd582140 338 struct lo_read_data *p = sd->u.data;
1da177e4 339 struct loop_device *lo = p->lo;
fd582140 340 struct page *page = buf->page;
1da177e4 341 sector_t IV;
3603b8ea 342 int size;
1da177e4 343
fd582140
JA
344 IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
345 (buf->offset >> 9);
346 size = sd->len;
347 if (size > p->bsize)
348 size = p->bsize;
1da177e4 349
fd582140 350 if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
44bd70c3 351 printk_ratelimited(KERN_ERR "loop: transfer error block %ld\n",
1da177e4 352 page->index);
fd582140 353 size = -EINVAL;
1da177e4
LT
354 }
355
356 flush_dcache_page(p->page);
357
fd582140
JA
358 if (size > 0)
359 p->offset += size;
360
1da177e4
LT
361 return size;
362}
363
fd582140
JA
364static int
365lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
366{
367 return __splice_from_pipe(pipe, sd, lo_splice_actor);
368}
369
306df071 370static ssize_t
1da177e4
LT
371do_lo_receive(struct loop_device *lo,
372 struct bio_vec *bvec, int bsize, loff_t pos)
373{
374 struct lo_read_data cookie;
fd582140 375 struct splice_desc sd;
1da177e4 376 struct file *file;
306df071 377 ssize_t retval;
1da177e4
LT
378
379 cookie.lo = lo;
380 cookie.page = bvec->bv_page;
381 cookie.offset = bvec->bv_offset;
382 cookie.bsize = bsize;
fd582140
JA
383
384 sd.len = 0;
385 sd.total_len = bvec->bv_len;
386 sd.flags = 0;
387 sd.pos = pos;
388 sd.u.data = &cookie;
389
1da177e4 390 file = lo->lo_backing_file;
fd582140
JA
391 retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
392
306df071 393 return retval;
1da177e4
LT
394}
395
396static int
30112013 397lo_receive(struct loop_device *lo, struct request *rq, int bsize, loff_t pos)
1da177e4 398{
7988613b 399 struct bio_vec bvec;
30112013 400 struct req_iterator iter;
306df071 401 ssize_t s;
1da177e4 402
30112013 403 rq_for_each_segment(bvec, rq, iter) {
7988613b 404 s = do_lo_receive(lo, &bvec, bsize, pos);
306df071
DY
405 if (s < 0)
406 return s;
407
7988613b 408 if (s != bvec.bv_len) {
30112013
ML
409 struct bio *bio;
410
411 __rq_for_each_bio(bio, rq)
412 zero_fill_bio(bio);
1da177e4 413 break;
306df071 414 }
7988613b 415 pos += bvec.bv_len;
1da177e4 416 }
306df071 417 return 0;
1da177e4
LT
418}
419
30112013 420static int do_req_filebacked(struct loop_device *lo, struct request *rq)
1da177e4
LT
421{
422 loff_t pos;
423 int ret;
424
30112013 425 pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset;
68db1961 426
30112013 427 if (rq->cmd_flags & REQ_WRITE) {
68db1961
NK
428 struct file *file = lo->lo_backing_file;
429
30112013 430 if (rq->cmd_flags & REQ_FLUSH) {
8018ab05 431 ret = vfs_fsync(file, 0);
6259f284 432 if (unlikely(ret && ret != -EINVAL)) {
68db1961
NK
433 ret = -EIO;
434 goto out;
435 }
436 }
437
dfaa2ef6
LC
438 /*
439 * We use punch hole to reclaim the free space used by the
ae95757a 440 * image a.k.a. discard. However we do not support discard if
dfaa2ef6
LC
441 * encryption is enabled, because it may give an attacker
442 * useful information.
443 */
30112013 444 if (rq->cmd_flags & REQ_DISCARD) {
dfaa2ef6
LC
445 struct file *file = lo->lo_backing_file;
446 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
447
448 if ((!file->f_op->fallocate) ||
449 lo->lo_encrypt_key_size) {
450 ret = -EOPNOTSUPP;
451 goto out;
452 }
453 ret = file->f_op->fallocate(file, mode, pos,
30112013 454 blk_rq_bytes(rq));
dfaa2ef6
LC
455 if (unlikely(ret && ret != -EINVAL &&
456 ret != -EOPNOTSUPP))
457 ret = -EIO;
458 goto out;
459 }
460
30112013 461 ret = lo_send(lo, rq, pos);
68db1961 462
30112013 463 if ((rq->cmd_flags & REQ_FUA) && !ret) {
8018ab05 464 ret = vfs_fsync(file, 0);
6259f284 465 if (unlikely(ret && ret != -EINVAL))
68db1961
NK
466 ret = -EIO;
467 }
468 } else
30112013 469 ret = lo_receive(lo, rq, lo->lo_blocksize, pos);
68db1961
NK
470
471out:
1da177e4
LT
472 return ret;
473}
474
1da177e4
LT
475struct switch_request {
476 struct file *file;
477 struct completion wait;
478};
479
1da177e4 480/*
b5dd2f60 481 * Do the actual switch; called from the BIO completion routine
1da177e4 482 */
b5dd2f60 483static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
1da177e4 484{
b5dd2f60
ML
485 struct file *file = p->file;
486 struct file *old_file = lo->lo_backing_file;
487 struct address_space *mapping;
35a82d1a 488
b5dd2f60
ML
489 /* if no new file, only flush of queued bios requested */
490 if (!file)
491 return;
1da177e4 492
b5dd2f60
ML
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));
1da177e4
LT
500}
501
502/*
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.
506 */
507static int loop_switch(struct loop_device *lo, struct file *file)
508{
509 struct switch_request w;
b5dd2f60 510
1da177e4 511 w.file = file;
b5dd2f60
ML
512
513 /* freeze queue and wait for completion of scheduled requests */
514 blk_mq_freeze_queue(lo->lo_queue);
515
516 /* do the switch action */
517 do_loop_switch(lo, &w);
518
519 /* unfreeze */
520 blk_mq_unfreeze_queue(lo->lo_queue);
521
1da177e4
LT
522 return 0;
523}
524
14f27939
MB
525/*
526 * Helper to flush the IOs in loop, but keeping loop thread running
527 */
528static int loop_flush(struct loop_device *lo)
529{
14f27939
MB
530 return loop_switch(lo, NULL);
531}
532
1da177e4
LT
533/*
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.
540 */
bb214884
AV
541static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
542 unsigned int arg)
1da177e4
LT
543{
544 struct file *file, *old_file;
545 struct inode *inode;
546 int error;
547
548 error = -ENXIO;
549 if (lo->lo_state != Lo_bound)
550 goto out;
551
552 /* the loop device has to be read-only */
553 error = -EINVAL;
554 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
555 goto out;
556
557 error = -EBADF;
558 file = fget(arg);
559 if (!file)
560 goto out;
561
562 inode = file->f_mapping->host;
563 old_file = lo->lo_backing_file;
564
565 error = -EINVAL;
566
567 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
568 goto out_putf;
569
1da177e4
LT
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))
572 goto out_putf;
573
574 /* and ... switch */
575 error = loop_switch(lo, file);
576 if (error)
577 goto out_putf;
578
579 fput(old_file);
e03c8dd1 580 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
476a4813 581 ioctl_by_bdev(bdev, BLKRRPART, 0);
1da177e4
LT
582 return 0;
583
584 out_putf:
585 fput(file);
586 out:
587 return error;
588}
589
590static inline int is_loop_device(struct file *file)
591{
592 struct inode *i = file->f_mapping->host;
593
594 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
595}
596
ee862730
MB
597/* loop sysfs attributes */
598
599static ssize_t loop_attr_show(struct device *dev, char *page,
600 ssize_t (*callback)(struct loop_device *, char *))
601{
34dd82af
KS
602 struct gendisk *disk = dev_to_disk(dev);
603 struct loop_device *lo = disk->private_data;
ee862730 604
34dd82af 605 return callback(lo, page);
ee862730
MB
606}
607
608#define LOOP_ATTR_RO(_name) \
609static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
610static ssize_t loop_attr_do_show_##_name(struct device *d, \
611 struct device_attribute *attr, char *b) \
612{ \
613 return loop_attr_show(d, b, loop_attr_##_name##_show); \
614} \
615static struct device_attribute loop_attr_##_name = \
616 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
617
618static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
619{
620 ssize_t ret;
621 char *p = NULL;
622
05eb0f25 623 spin_lock_irq(&lo->lo_lock);
ee862730
MB
624 if (lo->lo_backing_file)
625 p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
05eb0f25 626 spin_unlock_irq(&lo->lo_lock);
ee862730
MB
627
628 if (IS_ERR_OR_NULL(p))
629 ret = PTR_ERR(p);
630 else {
631 ret = strlen(p);
632 memmove(buf, p, ret);
633 buf[ret++] = '\n';
634 buf[ret] = 0;
635 }
636
637 return ret;
638}
639
640static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
641{
642 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
643}
644
645static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
646{
647 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
648}
649
650static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
651{
652 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
653
654 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
655}
656
e03c8dd1
KS
657static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
658{
659 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
660
661 return sprintf(buf, "%s\n", partscan ? "1" : "0");
662}
663
ee862730
MB
664LOOP_ATTR_RO(backing_file);
665LOOP_ATTR_RO(offset);
666LOOP_ATTR_RO(sizelimit);
667LOOP_ATTR_RO(autoclear);
e03c8dd1 668LOOP_ATTR_RO(partscan);
ee862730
MB
669
670static 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,
e03c8dd1 675 &loop_attr_partscan.attr,
ee862730
MB
676 NULL,
677};
678
679static struct attribute_group loop_attribute_group = {
680 .name = "loop",
681 .attrs= loop_attrs,
682};
683
684static int loop_sysfs_init(struct loop_device *lo)
685{
686 return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
687 &loop_attribute_group);
688}
689
690static void loop_sysfs_exit(struct loop_device *lo)
691{
692 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
693 &loop_attribute_group);
694}
695
dfaa2ef6
LC
696static void loop_config_discard(struct loop_device *lo)
697{
698 struct file *file = lo->lo_backing_file;
699 struct inode *inode = file->f_mapping->host;
700 struct request_queue *q = lo->lo_queue;
701
702 /*
703 * We use punch hole to reclaim the free space used by the
12a64d2f 704 * image a.k.a. discard. However we do not support discard if
dfaa2ef6
LC
705 * encryption is enabled, because it may give an attacker
706 * useful information.
707 */
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);
715 return;
716 }
717
718 q->limits.discard_granularity = inode->i_sb->s_blocksize;
dfaf3c03 719 q->limits.discard_alignment = 0;
dfaa2ef6
LC
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);
723}
724
bb214884 725static int loop_set_fd(struct loop_device *lo, fmode_t mode,
1da177e4
LT
726 struct block_device *bdev, unsigned int arg)
727{
728 struct file *file, *f;
729 struct inode *inode;
730 struct address_space *mapping;
731 unsigned lo_blocksize;
732 int lo_flags = 0;
733 int error;
734 loff_t size;
735
736 /* This is safe, since we have a reference from open(). */
737 __module_get(THIS_MODULE);
738
739 error = -EBADF;
740 file = fget(arg);
741 if (!file)
742 goto out;
743
744 error = -EBUSY;
745 if (lo->lo_state != Lo_unbound)
746 goto out_putf;
747
748 /* Avoid recursion */
749 f = file;
750 while (is_loop_device(f)) {
751 struct loop_device *l;
752
bb214884 753 if (f->f_mapping->host->i_bdev == bdev)
1da177e4
LT
754 goto out_putf;
755
756 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
757 if (l->lo_state == Lo_unbound) {
758 error = -EINVAL;
759 goto out_putf;
760 }
761 f = l->lo_backing_file;
762 }
763
764 mapping = file->f_mapping;
765 inode = mapping->host;
766
1da177e4 767 error = -EINVAL;
456be148
CH
768 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
769 goto out_putf;
1da177e4 770
456be148
CH
771 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
772 !file->f_op->write)
773 lo_flags |= LO_FLAGS_READ_ONLY;
ba52de12 774
456be148
CH
775 lo_blocksize = S_ISBLK(inode->i_mode) ?
776 inode->i_bdev->bd_block_size : PAGE_SIZE;
1da177e4 777
456be148 778 error = -EFBIG;
1da177e4 779 size = get_loop_size(lo, file);
456be148 780 if ((loff_t)(sector_t)size != size)
1da177e4 781 goto out_putf;
1da177e4 782
456be148 783 error = 0;
1da177e4
LT
784
785 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
786
787 lo->lo_blocksize = lo_blocksize;
788 lo->lo_device = bdev;
789 lo->lo_flags = lo_flags;
790 lo->lo_backing_file = file;
eefe85ee 791 lo->transfer = transfer_none;
1da177e4
LT
792 lo->ioctl = NULL;
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));
796
68db1961 797 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
4913efe4 798 blk_queue_flush(lo->lo_queue, REQ_FLUSH);
68db1961 799
73285082 800 set_capacity(lo->lo_disk, size);
1da177e4 801 bd_set_size(bdev, size << 9);
ee862730 802 loop_sysfs_init(lo);
c3473c63
DZ
803 /* let user-space know about the new size */
804 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
1da177e4
LT
805
806 set_blocksize(bdev, lo_blocksize);
807
6c997918 808 lo->lo_state = Lo_bound;
e03c8dd1
KS
809 if (part_shift)
810 lo->lo_flags |= LO_FLAGS_PARTSCAN;
811 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
476a4813 812 ioctl_by_bdev(bdev, BLKRRPART, 0);
c1681bf8
AP
813
814 /* Grab the block_device to prevent its destruction after we
815 * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev).
816 */
817 bdgrab(bdev);
1da177e4
LT
818 return 0;
819
820 out_putf:
821 fput(file);
822 out:
823 /* This is safe: open() is still holding a reference. */
824 module_put(THIS_MODULE);
825 return error;
826}
827
828static int
829loop_release_xfer(struct loop_device *lo)
830{
831 int err = 0;
832 struct loop_func_table *xfer = lo->lo_encryption;
833
834 if (xfer) {
835 if (xfer->release)
836 err = xfer->release(lo);
837 lo->transfer = NULL;
838 lo->lo_encryption = NULL;
839 module_put(xfer->owner);
840 }
841 return err;
842}
843
844static int
845loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
846 const struct loop_info64 *i)
847{
848 int err = 0;
849
850 if (xfer) {
851 struct module *owner = xfer->owner;
852
853 if (!try_module_get(owner))
854 return -EINVAL;
855 if (xfer->init)
856 err = xfer->init(lo, i);
857 if (err)
858 module_put(owner);
859 else
860 lo->lo_encryption = xfer;
861 }
862 return err;
863}
864
4c823cc3 865static int loop_clr_fd(struct loop_device *lo)
1da177e4
LT
866{
867 struct file *filp = lo->lo_backing_file;
b4e3ca1a 868 gfp_t gfp = lo->old_gfp_mask;
4c823cc3 869 struct block_device *bdev = lo->lo_device;
1da177e4
LT
870
871 if (lo->lo_state != Lo_bound)
872 return -ENXIO;
873
a1ecac3b
DC
874 /*
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.
883 */
884 if (lo->lo_refcnt > 1) {
885 lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
886 mutex_unlock(&lo->lo_ctl_mutex);
887 return 0;
888 }
1da177e4
LT
889
890 if (filp == NULL)
891 return -EINVAL;
892
893 spin_lock_irq(&lo->lo_lock);
894 lo->lo_state = Lo_rundown;
1da177e4 895 lo->lo_backing_file = NULL;
05eb0f25 896 spin_unlock_irq(&lo->lo_lock);
1da177e4
LT
897
898 loop_release_xfer(lo);
899 lo->transfer = NULL;
900 lo->ioctl = NULL;
901 lo->lo_device = NULL;
902 lo->lo_encryption = NULL;
903 lo->lo_offset = 0;
904 lo->lo_sizelimit = 0;
905 lo->lo_encrypt_key_size = 0;
1da177e4
LT
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);
c1681bf8
AP
909 if (bdev) {
910 bdput(bdev);
bb214884 911 invalidate_bdev(bdev);
c1681bf8 912 }
73285082 913 set_capacity(lo->lo_disk, 0);
51a0bb0c 914 loop_sysfs_exit(lo);
c3473c63 915 if (bdev) {
bb214884 916 bd_set_size(bdev, 0);
c3473c63
DZ
917 /* let user-space know about this change */
918 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
919 }
1da177e4
LT
920 mapping_set_gfp_mask(filp->f_mapping, gfp);
921 lo->lo_state = Lo_unbound;
1da177e4
LT
922 /* This is safe: open() is still holding a reference. */
923 module_put(THIS_MODULE);
c2fccc1c
JA
924 if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
925 ioctl_by_bdev(bdev, BLKRRPART, 0);
e03c8dd1
KS
926 lo->lo_flags = 0;
927 if (!part_shift)
928 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
f028f3b2
NK
929 mutex_unlock(&lo->lo_ctl_mutex);
930 /*
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.
935 */
936 fput(filp);
1da177e4
LT
937 return 0;
938}
939
940static int
941loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
942{
943 int err;
944 struct loop_func_table *xfer;
e4849737 945 kuid_t uid = current_uid();
1da177e4 946
b0fafa81 947 if (lo->lo_encrypt_key_size &&
e4849737 948 !uid_eq(lo->lo_key_owner, uid) &&
1da177e4
LT
949 !capable(CAP_SYS_ADMIN))
950 return -EPERM;
951 if (lo->lo_state != Lo_bound)
952 return -ENXIO;
953 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
954 return -EINVAL;
955
956 err = loop_release_xfer(lo);
957 if (err)
958 return err;
959
960 if (info->lo_encrypt_type) {
961 unsigned int type = info->lo_encrypt_type;
962
963 if (type >= MAX_LO_CRYPT)
964 return -EINVAL;
965 xfer = xfer_funcs[type];
966 if (xfer == NULL)
967 return -EINVAL;
968 } else
969 xfer = NULL;
970
971 err = loop_init_xfer(lo, xfer, info);
972 if (err)
973 return err;
974
975 if (lo->lo_offset != info->lo_offset ||
7b0576a3 976 lo->lo_sizelimit != info->lo_sizelimit)
7035b5df 977 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
1da177e4 978 return -EFBIG;
541c742a 979
dfaa2ef6 980 loop_config_discard(lo);
1da177e4
LT
981
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;
986
987 if (!xfer)
988 xfer = &none_funcs;
989 lo->transfer = xfer->transfer;
990 lo->ioctl = xfer->ioctl;
991
96c58655
DW
992 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
993 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
994 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
995
e03c8dd1
KS
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);
1001 }
1002
1da177e4
LT
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);
b0fafa81 1009 lo->lo_key_owner = uid;
1da177e4
LT
1010 }
1011
1012 return 0;
1013}
1014
1015static int
1016loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1017{
1018 struct file *file = lo->lo_backing_file;
1019 struct kstat stat;
1020 int error;
1021
1022 if (lo->lo_state != Lo_bound)
1023 return -ENXIO;
3dadecce 1024 error = vfs_getattr(&file->f_path, &stat);
1da177e4
LT
1025 if (error)
1026 return error;
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);
1043 }
1044 return 0;
1045}
1046
1047static void
1048loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1049{
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);
1064 else
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);
1067}
1068
1069static int
1070loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1071{
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);
1085 else
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);
1088
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)
1094 return -EOVERFLOW;
1095
1096 return 0;
1097}
1098
1099static int
1100loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1101{
1102 struct loop_info info;
1103 struct loop_info64 info64;
1104
1105 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1106 return -EFAULT;
1107 loop_info64_from_old(&info, &info64);
1108 return loop_set_status(lo, &info64);
1109}
1110
1111static int
1112loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1113{
1114 struct loop_info64 info64;
1115
1116 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1117 return -EFAULT;
1118 return loop_set_status(lo, &info64);
1119}
1120
1121static int
1122loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1123 struct loop_info info;
1124 struct loop_info64 info64;
1125 int err = 0;
1126
1127 if (!arg)
1128 err = -EINVAL;
1129 if (!err)
1130 err = loop_get_status(lo, &info64);
1131 if (!err)
1132 err = loop_info64_to_old(&info64, &info);
1133 if (!err && copy_to_user(arg, &info, sizeof(info)))
1134 err = -EFAULT;
1135
1136 return err;
1137}
1138
1139static int
1140loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1141 struct loop_info64 info64;
1142 int err = 0;
1143
1144 if (!arg)
1145 err = -EINVAL;
1146 if (!err)
1147 err = loop_get_status(lo, &info64);
1148 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1149 err = -EFAULT;
1150
1151 return err;
1152}
1153
53d66608
O
1154static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
1155{
53d66608 1156 if (unlikely(lo->lo_state != Lo_bound))
7b0576a3 1157 return -ENXIO;
53d66608 1158
7b0576a3 1159 return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
53d66608
O
1160}
1161
bb214884 1162static int lo_ioctl(struct block_device *bdev, fmode_t mode,
1da177e4
LT
1163 unsigned int cmd, unsigned long arg)
1164{
bb214884 1165 struct loop_device *lo = bdev->bd_disk->private_data;
1da177e4
LT
1166 int err;
1167
f028f3b2 1168 mutex_lock_nested(&lo->lo_ctl_mutex, 1);
1da177e4
LT
1169 switch (cmd) {
1170 case LOOP_SET_FD:
bb214884 1171 err = loop_set_fd(lo, mode, bdev, arg);
1da177e4
LT
1172 break;
1173 case LOOP_CHANGE_FD:
bb214884 1174 err = loop_change_fd(lo, bdev, arg);
1da177e4
LT
1175 break;
1176 case LOOP_CLR_FD:
f028f3b2 1177 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
4c823cc3 1178 err = loop_clr_fd(lo);
f028f3b2
NK
1179 if (!err)
1180 goto out_unlocked;
1da177e4
LT
1181 break;
1182 case LOOP_SET_STATUS:
7035b5df
DM
1183 err = -EPERM;
1184 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1185 err = loop_set_status_old(lo,
1186 (struct loop_info __user *)arg);
1da177e4
LT
1187 break;
1188 case LOOP_GET_STATUS:
1189 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1190 break;
1191 case LOOP_SET_STATUS64:
7035b5df
DM
1192 err = -EPERM;
1193 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1194 err = loop_set_status64(lo,
1195 (struct loop_info64 __user *) arg);
1da177e4
LT
1196 break;
1197 case LOOP_GET_STATUS64:
1198 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1199 break;
53d66608
O
1200 case LOOP_SET_CAPACITY:
1201 err = -EPERM;
1202 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1203 err = loop_set_capacity(lo, bdev);
1204 break;
1da177e4
LT
1205 default:
1206 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1207 }
f85221dd 1208 mutex_unlock(&lo->lo_ctl_mutex);
f028f3b2
NK
1209
1210out_unlocked:
1da177e4
LT
1211 return err;
1212}
1213
863d5b82
DH
1214#ifdef CONFIG_COMPAT
1215struct 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];
1227 char reserved[4];
1228};
1229
1230/*
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
1233 */
1234static noinline int
ba674cfc 1235loop_info64_from_compat(const struct compat_loop_info __user *arg,
863d5b82
DH
1236 struct loop_info64 *info64)
1237{
1238 struct compat_loop_info info;
1239
1240 if (copy_from_user(&info, arg, sizeof(info)))
1241 return -EFAULT;
1242
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);
1257 else
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);
1260 return 0;
1261}
1262
1263/*
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
1266 */
1267static noinline int
1268loop_info64_to_compat(const struct loop_info64 *info64,
1269 struct compat_loop_info __user *arg)
1270{
1271 struct compat_loop_info info;
1272
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);
1286 else
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);
1289
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])
1297 return -EOVERFLOW;
1298
1299 if (copy_to_user(arg, &info, sizeof(info)))
1300 return -EFAULT;
1301 return 0;
1302}
1303
1304static int
1305loop_set_status_compat(struct loop_device *lo,
1306 const struct compat_loop_info __user *arg)
1307{
1308 struct loop_info64 info64;
1309 int ret;
1310
1311 ret = loop_info64_from_compat(arg, &info64);
1312 if (ret < 0)
1313 return ret;
1314 return loop_set_status(lo, &info64);
1315}
1316
1317static int
1318loop_get_status_compat(struct loop_device *lo,
1319 struct compat_loop_info __user *arg)
1320{
1321 struct loop_info64 info64;
1322 int err = 0;
1323
1324 if (!arg)
1325 err = -EINVAL;
1326 if (!err)
1327 err = loop_get_status(lo, &info64);
1328 if (!err)
1329 err = loop_info64_to_compat(&info64, arg);
1330 return err;
1331}
1332
bb214884
AV
1333static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1334 unsigned int cmd, unsigned long arg)
863d5b82 1335{
bb214884 1336 struct loop_device *lo = bdev->bd_disk->private_data;
863d5b82
DH
1337 int err;
1338
863d5b82
DH
1339 switch(cmd) {
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);
1345 break;
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);
1351 break;
53d66608 1352 case LOOP_SET_CAPACITY:
863d5b82
DH
1353 case LOOP_CLR_FD:
1354 case LOOP_GET_STATUS64:
1355 case LOOP_SET_STATUS64:
1356 arg = (unsigned long) compat_ptr(arg);
1357 case LOOP_SET_FD:
1358 case LOOP_CHANGE_FD:
bb214884 1359 err = lo_ioctl(bdev, mode, cmd, arg);
863d5b82
DH
1360 break;
1361 default:
1362 err = -ENOIOCTLCMD;
1363 break;
1364 }
863d5b82
DH
1365 return err;
1366}
1367#endif
1368
bb214884 1369static int lo_open(struct block_device *bdev, fmode_t mode)
1da177e4 1370{
770fe30a
KS
1371 struct loop_device *lo;
1372 int err = 0;
1373
1374 mutex_lock(&loop_index_mutex);
1375 lo = bdev->bd_disk->private_data;
1376 if (!lo) {
1377 err = -ENXIO;
1378 goto out;
1379 }
1da177e4 1380
f85221dd 1381 mutex_lock(&lo->lo_ctl_mutex);
1da177e4 1382 lo->lo_refcnt++;
f85221dd 1383 mutex_unlock(&lo->lo_ctl_mutex);
770fe30a
KS
1384out:
1385 mutex_unlock(&loop_index_mutex);
1386 return err;
1da177e4
LT
1387}
1388
db2a144b 1389static void lo_release(struct gendisk *disk, fmode_t mode)
1da177e4 1390{
bb214884 1391 struct loop_device *lo = disk->private_data;
ffcd7dca 1392 int err;
1da177e4 1393
f85221dd 1394 mutex_lock(&lo->lo_ctl_mutex);
96c58655 1395
14f27939
MB
1396 if (--lo->lo_refcnt)
1397 goto out;
1398
1399 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1400 /*
1401 * In autoclear mode, stop the loop thread
1402 * and remove configuration after last close.
1403 */
4c823cc3 1404 err = loop_clr_fd(lo);
ffcd7dca 1405 if (!err)
db2a144b 1406 return;
14f27939
MB
1407 } else {
1408 /*
1409 * Otherwise keep thread (if running) and config,
1410 * but flush possible ongoing bios in thread.
1411 */
1412 loop_flush(lo);
1413 }
96c58655 1414
14f27939 1415out:
f85221dd 1416 mutex_unlock(&lo->lo_ctl_mutex);
1da177e4
LT
1417}
1418
83d5cde4 1419static const struct block_device_operations lo_fops = {
1da177e4 1420 .owner = THIS_MODULE,
bb214884
AV
1421 .open = lo_open,
1422 .release = lo_release,
1423 .ioctl = lo_ioctl,
863d5b82 1424#ifdef CONFIG_COMPAT
bb214884 1425 .compat_ioctl = lo_compat_ioctl,
863d5b82 1426#endif
1da177e4
LT
1427};
1428
1429/*
1430 * And now the modules code and kernel interface.
1431 */
73285082 1432static int max_loop;
ac04fee0 1433module_param(max_loop, int, S_IRUGO);
a47653fc 1434MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
ac04fee0 1435module_param(max_part, int, S_IRUGO);
476a4813 1436MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
1da177e4
LT
1437MODULE_LICENSE("GPL");
1438MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1439
1440int loop_register_transfer(struct loop_func_table *funcs)
1441{
1442 unsigned int n = funcs->number;
1443
1444 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1445 return -EINVAL;
1446 xfer_funcs[n] = funcs;
1447 return 0;
1448}
1449
34dd82af
KS
1450static int unregister_transfer_cb(int id, void *ptr, void *data)
1451{
1452 struct loop_device *lo = ptr;
1453 struct loop_func_table *xfer = data;
1454
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);
1459 return 0;
1460}
1461
1da177e4
LT
1462int loop_unregister_transfer(int number)
1463{
1464 unsigned int n = number;
1da177e4
LT
1465 struct loop_func_table *xfer;
1466
1467 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1468 return -EINVAL;
1469
1470 xfer_funcs[n] = NULL;
34dd82af 1471 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
1da177e4
LT
1472 return 0;
1473}
1474
1475EXPORT_SYMBOL(loop_register_transfer);
1476EXPORT_SYMBOL(loop_unregister_transfer);
1477
b5dd2f60
ML
1478static int loop_queue_rq(struct blk_mq_hw_ctx *hctx,
1479 const struct blk_mq_queue_data *bd)
1480{
1481 struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
1482
1483 blk_mq_start_request(bd->rq);
1484
1485 if (cmd->rq->cmd_flags & REQ_WRITE) {
1486 struct loop_device *lo = cmd->rq->q->queuedata;
1487 bool need_sched = true;
1488
1489 spin_lock_irq(&lo->lo_lock);
1490 if (lo->write_started)
1491 need_sched = false;
1492 else
1493 lo->write_started = true;
1494 list_add_tail(&cmd->list, &lo->write_cmd_head);
1495 spin_unlock_irq(&lo->lo_lock);
1496
1497 if (need_sched)
1498 queue_work(loop_wq, &lo->write_work);
1499 } else {
1500 queue_work(loop_wq, &cmd->read_work);
1501 }
1502
1503 return BLK_MQ_RQ_QUEUE_OK;
1504}
1505
1506static void loop_handle_cmd(struct loop_cmd *cmd)
1507{
1508 const bool write = cmd->rq->cmd_flags & REQ_WRITE;
1509 struct loop_device *lo = cmd->rq->q->queuedata;
1510 int ret = -EIO;
b5dd2f60
ML
1511
1512 if (lo->lo_state != Lo_bound)
1513 goto failed;
1514
1515 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY))
1516 goto failed;
1517
30112013 1518 ret = do_req_filebacked(lo, cmd->rq);
b5dd2f60
ML
1519
1520 failed:
1521 if (ret)
1522 cmd->rq->errors = -EIO;
1523 blk_mq_complete_request(cmd->rq);
1524}
1525
1526static void loop_queue_write_work(struct work_struct *work)
1527{
1528 struct loop_device *lo =
1529 container_of(work, struct loop_device, write_work);
1530 LIST_HEAD(cmd_list);
1531
1532 spin_lock_irq(&lo->lo_lock);
1533 repeat:
1534 list_splice_init(&lo->write_cmd_head, &cmd_list);
1535 spin_unlock_irq(&lo->lo_lock);
1536
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);
1542 }
1543
1544 spin_lock_irq(&lo->lo_lock);
1545 if (!list_empty(&lo->write_cmd_head))
1546 goto repeat;
1547 lo->write_started = false;
1548 spin_unlock_irq(&lo->lo_lock);
1549}
1550
1551static void loop_queue_read_work(struct work_struct *work)
1552{
1553 struct loop_cmd *cmd =
1554 container_of(work, struct loop_cmd, read_work);
1555
1556 loop_handle_cmd(cmd);
1557}
1558
1559static int loop_init_request(void *data, struct request *rq,
1560 unsigned int hctx_idx, unsigned int request_idx,
1561 unsigned int numa_node)
1562{
1563 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
1564
1565 cmd->rq = rq;
1566 INIT_WORK(&cmd->read_work, loop_queue_read_work);
1567
1568 return 0;
1569}
1570
1571static 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,
1575};
1576
34dd82af 1577static int loop_add(struct loop_device **l, int i)
73285082
KC
1578{
1579 struct loop_device *lo;
1580 struct gendisk *disk;
34dd82af 1581 int err;
73285082 1582
68d740d7 1583 err = -ENOMEM;
73285082 1584 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
68d740d7 1585 if (!lo)
73285082 1586 goto out;
34dd82af 1587
ef7e7c82
MP
1588 lo->lo_state = Lo_unbound;
1589
c718aa65 1590 /* allocate id, if @id >= 0, we're requesting that specific id */
34dd82af 1591 if (i >= 0) {
c718aa65
TH
1592 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL);
1593 if (err == -ENOSPC)
34dd82af 1594 err = -EEXIST;
34dd82af 1595 } else {
c718aa65 1596 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
34dd82af
KS
1597 }
1598 if (err < 0)
1599 goto out_free_dev;
c718aa65 1600 i = err;
73285082 1601
183cfb57 1602 err = -ENOMEM;
b5dd2f60
ML
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;
1610
1611 err = blk_mq_alloc_tag_set(&lo->tag_set);
1612 if (err)
3ec981e3 1613 goto out_free_idr;
73285082 1614
b5dd2f60
ML
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;
1619 }
ef7e7c82
MP
1620 lo->lo_queue->queuedata = lo;
1621
b5dd2f60
ML
1622 INIT_LIST_HEAD(&lo->write_cmd_head);
1623 INIT_WORK(&lo->write_work, loop_queue_write_work);
1624
476a4813 1625 disk = lo->lo_disk = alloc_disk(1 << part_shift);
73285082
KC
1626 if (!disk)
1627 goto out_free_queue;
1628
e03c8dd1
KS
1629 /*
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
1636 * used.
1637 *
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.
1641 *
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.
1646 */
1647 if (!part_shift)
1648 disk->flags |= GENHD_FL_NO_PART_SCAN;
1649 disk->flags |= GENHD_FL_EXT_DEVT;
73285082
KC
1650 mutex_init(&lo->lo_ctl_mutex);
1651 lo->lo_number = i;
73285082
KC
1652 spin_lock_init(&lo->lo_lock);
1653 disk->major = LOOP_MAJOR;
476a4813 1654 disk->first_minor = i << part_shift;
73285082
KC
1655 disk->fops = &lo_fops;
1656 disk->private_data = lo;
1657 disk->queue = lo->lo_queue;
1658 sprintf(disk->disk_name, "loop%d", i);
34dd82af
KS
1659 add_disk(disk);
1660 *l = lo;
1661 return lo->lo_number;
73285082
KC
1662
1663out_free_queue:
1664 blk_cleanup_queue(lo->lo_queue);
b5dd2f60
ML
1665out_cleanup_tags:
1666 blk_mq_free_tag_set(&lo->tag_set);
3ec981e3
MP
1667out_free_idr:
1668 idr_remove(&loop_index_idr, i);
73285082
KC
1669out_free_dev:
1670 kfree(lo);
1671out:
34dd82af 1672 return err;
73285082
KC
1673}
1674
34dd82af 1675static void loop_remove(struct loop_device *lo)
1da177e4 1676{
34dd82af 1677 del_gendisk(lo->lo_disk);
73285082 1678 blk_cleanup_queue(lo->lo_queue);
b5dd2f60 1679 blk_mq_free_tag_set(&lo->tag_set);
73285082 1680 put_disk(lo->lo_disk);
73285082
KC
1681 kfree(lo);
1682}
1da177e4 1683
770fe30a
KS
1684static int find_free_cb(int id, void *ptr, void *data)
1685{
1686 struct loop_device *lo = ptr;
1687 struct loop_device **l = data;
1688
1689 if (lo->lo_state == Lo_unbound) {
1690 *l = lo;
1691 return 1;
1692 }
1693 return 0;
1694}
1695
34dd82af 1696static int loop_lookup(struct loop_device **l, int i)
a47653fc
KC
1697{
1698 struct loop_device *lo;
34dd82af 1699 int ret = -ENODEV;
a47653fc 1700
770fe30a
KS
1701 if (i < 0) {
1702 int err;
1703
1704 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
1705 if (err == 1) {
1706 *l = lo;
1707 ret = lo->lo_number;
1708 }
1709 goto out;
a47653fc
KC
1710 }
1711
770fe30a 1712 /* lookup and return a specific i */
34dd82af 1713 lo = idr_find(&loop_index_idr, i);
a47653fc 1714 if (lo) {
34dd82af
KS
1715 *l = lo;
1716 ret = lo->lo_number;
a47653fc 1717 }
770fe30a 1718out:
34dd82af 1719 return ret;
a47653fc
KC
1720}
1721
73285082
KC
1722static struct kobject *loop_probe(dev_t dev, int *part, void *data)
1723{
705962cc 1724 struct loop_device *lo;
07002e99 1725 struct kobject *kobj;
34dd82af 1726 int err;
73285082 1727
34dd82af
KS
1728 mutex_lock(&loop_index_mutex);
1729 err = loop_lookup(&lo, MINOR(dev) >> part_shift);
1730 if (err < 0)
1731 err = loop_add(&lo, MINOR(dev) >> part_shift);
1732 if (err < 0)
a207f593 1733 kobj = NULL;
34dd82af
KS
1734 else
1735 kobj = get_disk(lo->lo_disk);
1736 mutex_unlock(&loop_index_mutex);
73285082
KC
1737
1738 *part = 0;
07002e99 1739 return kobj;
73285082
KC
1740}
1741
770fe30a
KS
1742static long loop_control_ioctl(struct file *file, unsigned int cmd,
1743 unsigned long parm)
1744{
1745 struct loop_device *lo;
1746 int ret = -ENOSYS;
1747
1748 mutex_lock(&loop_index_mutex);
1749 switch (cmd) {
1750 case LOOP_CTL_ADD:
1751 ret = loop_lookup(&lo, parm);
1752 if (ret >= 0) {
1753 ret = -EEXIST;
1754 break;
1755 }
1756 ret = loop_add(&lo, parm);
1757 break;
1758 case LOOP_CTL_REMOVE:
1759 ret = loop_lookup(&lo, parm);
1760 if (ret < 0)
1761 break;
1762 mutex_lock(&lo->lo_ctl_mutex);
1763 if (lo->lo_state != Lo_unbound) {
1764 ret = -EBUSY;
1765 mutex_unlock(&lo->lo_ctl_mutex);
1766 break;
1767 }
1768 if (lo->lo_refcnt > 0) {
1769 ret = -EBUSY;
1770 mutex_unlock(&lo->lo_ctl_mutex);
1771 break;
1772 }
1773 lo->lo_disk->private_data = NULL;
1774 mutex_unlock(&lo->lo_ctl_mutex);
1775 idr_remove(&loop_index_idr, lo->lo_number);
1776 loop_remove(lo);
1777 break;
1778 case LOOP_CTL_GET_FREE:
1779 ret = loop_lookup(&lo, -1);
1780 if (ret >= 0)
1781 break;
1782 ret = loop_add(&lo, -1);
1783 }
1784 mutex_unlock(&loop_index_mutex);
1785
1786 return ret;
1787}
1788
1789static 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,
1795};
1796
1797static struct miscdevice loop_misc = {
1798 .minor = LOOP_CTRL_MINOR,
1799 .name = "loop-control",
1800 .fops = &loop_ctl_fops,
1801};
1802
1803MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
1804MODULE_ALIAS("devname:loop-control");
1805
73285082
KC
1806static int __init loop_init(void)
1807{
a47653fc
KC
1808 int i, nr;
1809 unsigned long range;
34dd82af 1810 struct loop_device *lo;
770fe30a 1811 int err;
a47653fc 1812
770fe30a
KS
1813 err = misc_register(&loop_misc);
1814 if (err < 0)
1815 return err;
476a4813
LV
1816
1817 part_shift = 0;
ac04fee0 1818 if (max_part > 0) {
476a4813
LV
1819 part_shift = fls(max_part);
1820
ac04fee0
NK
1821 /*
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.
1825 *
1826 * Note that -1 is required because partition 0 is reserved
1827 * for the whole disk.
1828 */
1829 max_part = (1UL << part_shift) - 1;
1830 }
1831
b1a66504
GC
1832 if ((1UL << part_shift) > DISK_MAX_PARTS) {
1833 err = -EINVAL;
1834 goto misc_out;
1835 }
78f4bb36 1836
b1a66504
GC
1837 if (max_loop > 1UL << (MINORBITS - part_shift)) {
1838 err = -EINVAL;
1839 goto misc_out;
1840 }
1da177e4 1841
d134b00b
KS
1842 /*
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.
1849 */
73285082 1850 if (max_loop) {
a47653fc 1851 nr = max_loop;
a1c15c59 1852 range = max_loop << part_shift;
a47653fc 1853 } else {
d134b00b 1854 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
a1c15c59 1855 range = 1UL << MINORBITS;
a47653fc
KC
1856 }
1857
b1a66504
GC
1858 if (register_blkdev(LOOP_MAJOR, "loop")) {
1859 err = -EIO;
1860 goto misc_out;
1861 }
1da177e4 1862
b5dd2f60
ML
1863 loop_wq = alloc_workqueue("kloopd",
1864 WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 0);
1865 if (!loop_wq) {
1866 err = -ENOMEM;
1867 goto misc_out;
1868 }
1869
a47653fc
KC
1870 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
1871 THIS_MODULE, loop_probe, NULL, NULL);
1872
d134b00b 1873 /* pre-create number of devices given by config or max_loop */
34dd82af
KS
1874 mutex_lock(&loop_index_mutex);
1875 for (i = 0; i < nr; i++)
1876 loop_add(&lo, i);
1877 mutex_unlock(&loop_index_mutex);
1878
73285082 1879 printk(KERN_INFO "loop: module loaded\n");
1da177e4 1880 return 0;
b1a66504
GC
1881
1882misc_out:
1883 misc_deregister(&loop_misc);
1884 return err;
34dd82af 1885}
a47653fc 1886
34dd82af
KS
1887static int loop_exit_cb(int id, void *ptr, void *data)
1888{
1889 struct loop_device *lo = ptr;
a47653fc 1890
34dd82af
KS
1891 loop_remove(lo);
1892 return 0;
1da177e4
LT
1893}
1894
73285082 1895static void __exit loop_exit(void)
1da177e4 1896{
a47653fc 1897 unsigned long range;
1da177e4 1898
a1c15c59 1899 range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
a47653fc 1900
34dd82af 1901 idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
34dd82af 1902 idr_destroy(&loop_index_idr);
73285082 1903
a47653fc 1904 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
00d59405 1905 unregister_blkdev(LOOP_MAJOR, "loop");
770fe30a 1906
b5dd2f60
ML
1907 destroy_workqueue(loop_wq);
1908
770fe30a 1909 misc_deregister(&loop_misc);
1da177e4
LT
1910}
1911
1912module_init(loop_init);
1913module_exit(loop_exit);
1914
1915#ifndef MODULE
1916static int __init max_loop_setup(char *str)
1917{
1918 max_loop = simple_strtol(str, NULL, 0);
1919 return 1;
1920}
1921
1922__setup("max_loop=", max_loop_setup);
1923#endif