loop: Limit the number of requests in the bio list
[deliverable/linux.git] / drivers / block / loop.c
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.
32 * Erik I. Bolsø, <eriki@himolde.no>, Oct 31, 1999
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
43 * operations write_begin is not available on the backing filesystem.
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
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>
64 #include <linux/swap.h>
65 #include <linux/slab.h>
66 #include <linux/loop.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/kthread.h>
75 #include <linux/splice.h>
76 #include <linux/sysfs.h>
77 #include <linux/miscdevice.h>
78 #include <linux/falloc.h>
79
80 #include <asm/uaccess.h>
81
82 static DEFINE_IDR(loop_index_idr);
83 static DEFINE_MUTEX(loop_index_mutex);
84
85 static int max_part;
86 static int part_shift;
87
88 /*
89 * Transfer functions
90 */
91 static int transfer_none(struct loop_device *lo, int cmd,
92 struct page *raw_page, unsigned raw_off,
93 struct page *loop_page, unsigned loop_off,
94 int size, sector_t real_block)
95 {
96 char *raw_buf = kmap_atomic(raw_page) + raw_off;
97 char *loop_buf = kmap_atomic(loop_page) + loop_off;
98
99 if (cmd == READ)
100 memcpy(loop_buf, raw_buf, size);
101 else
102 memcpy(raw_buf, loop_buf, size);
103
104 kunmap_atomic(loop_buf);
105 kunmap_atomic(raw_buf);
106 cond_resched();
107 return 0;
108 }
109
110 static int transfer_xor(struct loop_device *lo, int cmd,
111 struct page *raw_page, unsigned raw_off,
112 struct page *loop_page, unsigned loop_off,
113 int size, sector_t real_block)
114 {
115 char *raw_buf = kmap_atomic(raw_page) + raw_off;
116 char *loop_buf = kmap_atomic(loop_page) + loop_off;
117 char *in, *out, *key;
118 int i, keysize;
119
120 if (cmd == READ) {
121 in = raw_buf;
122 out = loop_buf;
123 } else {
124 in = loop_buf;
125 out = raw_buf;
126 }
127
128 key = lo->lo_encrypt_key;
129 keysize = lo->lo_encrypt_key_size;
130 for (i = 0; i < size; i++)
131 *out++ = *in++ ^ key[(i & 511) % keysize];
132
133 kunmap_atomic(loop_buf);
134 kunmap_atomic(raw_buf);
135 cond_resched();
136 return 0;
137 }
138
139 static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
140 {
141 if (unlikely(info->lo_encrypt_key_size <= 0))
142 return -EINVAL;
143 return 0;
144 }
145
146 static struct loop_func_table none_funcs = {
147 .number = LO_CRYPT_NONE,
148 .transfer = transfer_none,
149 };
150
151 static struct loop_func_table xor_funcs = {
152 .number = LO_CRYPT_XOR,
153 .transfer = transfer_xor,
154 .init = xor_init
155 };
156
157 /* xfer_funcs[0] is special - its release function is never called */
158 static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
159 &none_funcs,
160 &xor_funcs
161 };
162
163 static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
164 {
165 loff_t size, loopsize;
166
167 /* Compute loopsize in bytes */
168 size = i_size_read(file->f_mapping->host);
169 loopsize = size - offset;
170 /* offset is beyond i_size, wierd but possible */
171 if (loopsize < 0)
172 return 0;
173
174 if (sizelimit > 0 && sizelimit < loopsize)
175 loopsize = sizelimit;
176 /*
177 * Unfortunately, if we want to do I/O on the device,
178 * the number of 512-byte sectors has to fit into a sector_t.
179 */
180 return loopsize >> 9;
181 }
182
183 static loff_t get_loop_size(struct loop_device *lo, struct file *file)
184 {
185 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
186 }
187
188 static int
189 figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
190 {
191 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
192 sector_t x = (sector_t)size;
193
194 if (unlikely((loff_t)x != size))
195 return -EFBIG;
196 if (lo->lo_offset != offset)
197 lo->lo_offset = offset;
198 if (lo->lo_sizelimit != sizelimit)
199 lo->lo_sizelimit = sizelimit;
200 set_capacity(lo->lo_disk, x);
201 return 0;
202 }
203
204 static inline int
205 lo_do_transfer(struct loop_device *lo, int cmd,
206 struct page *rpage, unsigned roffs,
207 struct page *lpage, unsigned loffs,
208 int size, sector_t rblock)
209 {
210 if (unlikely(!lo->transfer))
211 return 0;
212
213 return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
214 }
215
216 /**
217 * __do_lo_send_write - helper for writing data to a loop device
218 *
219 * This helper just factors out common code between do_lo_send_direct_write()
220 * and do_lo_send_write().
221 */
222 static int __do_lo_send_write(struct file *file,
223 u8 *buf, const int len, loff_t pos)
224 {
225 ssize_t bw;
226 mm_segment_t old_fs = get_fs();
227
228 set_fs(get_ds());
229 bw = file->f_op->write(file, buf, len, &pos);
230 set_fs(old_fs);
231 if (likely(bw == len))
232 return 0;
233 printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n",
234 (unsigned long long)pos, len);
235 if (bw >= 0)
236 bw = -EIO;
237 return bw;
238 }
239
240 /**
241 * do_lo_send_direct_write - helper for writing data to a loop device
242 *
243 * This is the fast, non-transforming version that does not need double
244 * buffering.
245 */
246 static int do_lo_send_direct_write(struct loop_device *lo,
247 struct bio_vec *bvec, loff_t pos, struct page *page)
248 {
249 ssize_t bw = __do_lo_send_write(lo->lo_backing_file,
250 kmap(bvec->bv_page) + bvec->bv_offset,
251 bvec->bv_len, pos);
252 kunmap(bvec->bv_page);
253 cond_resched();
254 return bw;
255 }
256
257 /**
258 * do_lo_send_write - helper for writing data to a loop device
259 *
260 * This is the slow, transforming version that needs to double buffer the
261 * data as it cannot do the transformations in place without having direct
262 * access to the destination pages of the backing file.
263 */
264 static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec,
265 loff_t pos, struct page *page)
266 {
267 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page,
268 bvec->bv_offset, bvec->bv_len, pos >> 9);
269 if (likely(!ret))
270 return __do_lo_send_write(lo->lo_backing_file,
271 page_address(page), bvec->bv_len,
272 pos);
273 printk(KERN_ERR "loop: Transfer error at byte offset %llu, "
274 "length %i.\n", (unsigned long long)pos, bvec->bv_len);
275 if (ret > 0)
276 ret = -EIO;
277 return ret;
278 }
279
280 static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos)
281 {
282 int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t,
283 struct page *page);
284 struct bio_vec *bvec;
285 struct page *page = NULL;
286 int i, ret = 0;
287
288 if (lo->transfer != transfer_none) {
289 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
290 if (unlikely(!page))
291 goto fail;
292 kmap(page);
293 do_lo_send = do_lo_send_write;
294 } else {
295 do_lo_send = do_lo_send_direct_write;
296 }
297
298 bio_for_each_segment(bvec, bio, i) {
299 ret = do_lo_send(lo, bvec, pos, page);
300 if (ret < 0)
301 break;
302 pos += bvec->bv_len;
303 }
304 if (page) {
305 kunmap(page);
306 __free_page(page);
307 }
308 out:
309 return ret;
310 fail:
311 printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n");
312 ret = -ENOMEM;
313 goto out;
314 }
315
316 struct lo_read_data {
317 struct loop_device *lo;
318 struct page *page;
319 unsigned offset;
320 int bsize;
321 };
322
323 static int
324 lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
325 struct splice_desc *sd)
326 {
327 struct lo_read_data *p = sd->u.data;
328 struct loop_device *lo = p->lo;
329 struct page *page = buf->page;
330 sector_t IV;
331 int size;
332
333 IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
334 (buf->offset >> 9);
335 size = sd->len;
336 if (size > p->bsize)
337 size = p->bsize;
338
339 if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
340 printk(KERN_ERR "loop: transfer error block %ld\n",
341 page->index);
342 size = -EINVAL;
343 }
344
345 flush_dcache_page(p->page);
346
347 if (size > 0)
348 p->offset += size;
349
350 return size;
351 }
352
353 static int
354 lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
355 {
356 return __splice_from_pipe(pipe, sd, lo_splice_actor);
357 }
358
359 static ssize_t
360 do_lo_receive(struct loop_device *lo,
361 struct bio_vec *bvec, int bsize, loff_t pos)
362 {
363 struct lo_read_data cookie;
364 struct splice_desc sd;
365 struct file *file;
366 ssize_t retval;
367
368 cookie.lo = lo;
369 cookie.page = bvec->bv_page;
370 cookie.offset = bvec->bv_offset;
371 cookie.bsize = bsize;
372
373 sd.len = 0;
374 sd.total_len = bvec->bv_len;
375 sd.flags = 0;
376 sd.pos = pos;
377 sd.u.data = &cookie;
378
379 file = lo->lo_backing_file;
380 retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
381
382 return retval;
383 }
384
385 static int
386 lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos)
387 {
388 struct bio_vec *bvec;
389 ssize_t s;
390 int i;
391
392 bio_for_each_segment(bvec, bio, i) {
393 s = do_lo_receive(lo, bvec, bsize, pos);
394 if (s < 0)
395 return s;
396
397 if (s != bvec->bv_len) {
398 zero_fill_bio(bio);
399 break;
400 }
401 pos += bvec->bv_len;
402 }
403 return 0;
404 }
405
406 static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
407 {
408 loff_t pos;
409 int ret;
410
411 pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
412
413 if (bio_rw(bio) == WRITE) {
414 struct file *file = lo->lo_backing_file;
415
416 if (bio->bi_rw & REQ_FLUSH) {
417 ret = vfs_fsync(file, 0);
418 if (unlikely(ret && ret != -EINVAL)) {
419 ret = -EIO;
420 goto out;
421 }
422 }
423
424 /*
425 * We use punch hole to reclaim the free space used by the
426 * image a.k.a. discard. However we do not support discard if
427 * encryption is enabled, because it may give an attacker
428 * useful information.
429 */
430 if (bio->bi_rw & REQ_DISCARD) {
431 struct file *file = lo->lo_backing_file;
432 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
433
434 if ((!file->f_op->fallocate) ||
435 lo->lo_encrypt_key_size) {
436 ret = -EOPNOTSUPP;
437 goto out;
438 }
439 ret = file->f_op->fallocate(file, mode, pos,
440 bio->bi_size);
441 if (unlikely(ret && ret != -EINVAL &&
442 ret != -EOPNOTSUPP))
443 ret = -EIO;
444 goto out;
445 }
446
447 ret = lo_send(lo, bio, pos);
448
449 if ((bio->bi_rw & REQ_FUA) && !ret) {
450 ret = vfs_fsync(file, 0);
451 if (unlikely(ret && ret != -EINVAL))
452 ret = -EIO;
453 }
454 } else
455 ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
456
457 out:
458 return ret;
459 }
460
461 /*
462 * Add bio to back of pending list
463 */
464 static void loop_add_bio(struct loop_device *lo, struct bio *bio)
465 {
466 lo->lo_bio_count++;
467 bio_list_add(&lo->lo_bio_list, bio);
468 }
469
470 /*
471 * Grab first pending buffer
472 */
473 static struct bio *loop_get_bio(struct loop_device *lo)
474 {
475 lo->lo_bio_count--;
476 return bio_list_pop(&lo->lo_bio_list);
477 }
478
479 static void loop_make_request(struct request_queue *q, struct bio *old_bio)
480 {
481 struct loop_device *lo = q->queuedata;
482 int rw = bio_rw(old_bio);
483
484 if (rw == READA)
485 rw = READ;
486
487 BUG_ON(!lo || (rw != READ && rw != WRITE));
488
489 spin_lock_irq(&lo->lo_lock);
490 if (lo->lo_state != Lo_bound)
491 goto out;
492 if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY)))
493 goto out;
494 if (lo->lo_bio_count >= q->nr_congestion_on)
495 wait_event_lock_irq(lo->lo_req_wait,
496 lo->lo_bio_count < q->nr_congestion_off,
497 lo->lo_lock);
498 loop_add_bio(lo, old_bio);
499 wake_up(&lo->lo_event);
500 spin_unlock_irq(&lo->lo_lock);
501 return;
502
503 out:
504 spin_unlock_irq(&lo->lo_lock);
505 bio_io_error(old_bio);
506 }
507
508 struct switch_request {
509 struct file *file;
510 struct completion wait;
511 };
512
513 static void do_loop_switch(struct loop_device *, struct switch_request *);
514
515 static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio)
516 {
517 if (unlikely(!bio->bi_bdev)) {
518 do_loop_switch(lo, bio->bi_private);
519 bio_put(bio);
520 } else {
521 int ret = do_bio_filebacked(lo, bio);
522 bio_endio(bio, ret);
523 }
524 }
525
526 /*
527 * worker thread that handles reads/writes to file backed loop devices,
528 * to avoid blocking in our make_request_fn. it also does loop decrypting
529 * on reads for block backed loop, as that is too heavy to do from
530 * b_end_io context where irqs may be disabled.
531 *
532 * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before
533 * calling kthread_stop(). Therefore once kthread_should_stop() is
534 * true, make_request will not place any more requests. Therefore
535 * once kthread_should_stop() is true and lo_bio is NULL, we are
536 * done with the loop.
537 */
538 static int loop_thread(void *data)
539 {
540 struct loop_device *lo = data;
541 struct bio *bio;
542
543 set_user_nice(current, -20);
544
545 while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) {
546
547 wait_event_interruptible(lo->lo_event,
548 !bio_list_empty(&lo->lo_bio_list) ||
549 kthread_should_stop());
550
551 if (bio_list_empty(&lo->lo_bio_list))
552 continue;
553 spin_lock_irq(&lo->lo_lock);
554 bio = loop_get_bio(lo);
555 if (lo->lo_bio_count < lo->lo_queue->nr_congestion_off)
556 wake_up(&lo->lo_req_wait);
557 spin_unlock_irq(&lo->lo_lock);
558
559 BUG_ON(!bio);
560 loop_handle_bio(lo, bio);
561 }
562
563 return 0;
564 }
565
566 /*
567 * loop_switch performs the hard work of switching a backing store.
568 * First it needs to flush existing IO, it does this by sending a magic
569 * BIO down the pipe. The completion of this BIO does the actual switch.
570 */
571 static int loop_switch(struct loop_device *lo, struct file *file)
572 {
573 struct switch_request w;
574 struct bio *bio = bio_alloc(GFP_KERNEL, 0);
575 if (!bio)
576 return -ENOMEM;
577 init_completion(&w.wait);
578 w.file = file;
579 bio->bi_private = &w;
580 bio->bi_bdev = NULL;
581 loop_make_request(lo->lo_queue, bio);
582 wait_for_completion(&w.wait);
583 return 0;
584 }
585
586 /*
587 * Helper to flush the IOs in loop, but keeping loop thread running
588 */
589 static int loop_flush(struct loop_device *lo)
590 {
591 /* loop not yet configured, no running thread, nothing to flush */
592 if (!lo->lo_thread)
593 return 0;
594
595 return loop_switch(lo, NULL);
596 }
597
598 /*
599 * Do the actual switch; called from the BIO completion routine
600 */
601 static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
602 {
603 struct file *file = p->file;
604 struct file *old_file = lo->lo_backing_file;
605 struct address_space *mapping;
606
607 /* if no new file, only flush of queued bios requested */
608 if (!file)
609 goto out;
610
611 mapping = file->f_mapping;
612 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
613 lo->lo_backing_file = file;
614 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
615 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
616 lo->old_gfp_mask = mapping_gfp_mask(mapping);
617 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
618 out:
619 complete(&p->wait);
620 }
621
622
623 /*
624 * loop_change_fd switched the backing store of a loopback device to
625 * a new file. This is useful for operating system installers to free up
626 * the original file and in High Availability environments to switch to
627 * an alternative location for the content in case of server meltdown.
628 * This can only work if the loop device is used read-only, and if the
629 * new backing store is the same size and type as the old backing store.
630 */
631 static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
632 unsigned int arg)
633 {
634 struct file *file, *old_file;
635 struct inode *inode;
636 int error;
637
638 error = -ENXIO;
639 if (lo->lo_state != Lo_bound)
640 goto out;
641
642 /* the loop device has to be read-only */
643 error = -EINVAL;
644 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
645 goto out;
646
647 error = -EBADF;
648 file = fget(arg);
649 if (!file)
650 goto out;
651
652 inode = file->f_mapping->host;
653 old_file = lo->lo_backing_file;
654
655 error = -EINVAL;
656
657 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
658 goto out_putf;
659
660 /* size of the new backing store needs to be the same */
661 if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
662 goto out_putf;
663
664 /* and ... switch */
665 error = loop_switch(lo, file);
666 if (error)
667 goto out_putf;
668
669 fput(old_file);
670 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
671 ioctl_by_bdev(bdev, BLKRRPART, 0);
672 return 0;
673
674 out_putf:
675 fput(file);
676 out:
677 return error;
678 }
679
680 static inline int is_loop_device(struct file *file)
681 {
682 struct inode *i = file->f_mapping->host;
683
684 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
685 }
686
687 /* loop sysfs attributes */
688
689 static ssize_t loop_attr_show(struct device *dev, char *page,
690 ssize_t (*callback)(struct loop_device *, char *))
691 {
692 struct gendisk *disk = dev_to_disk(dev);
693 struct loop_device *lo = disk->private_data;
694
695 return callback(lo, page);
696 }
697
698 #define LOOP_ATTR_RO(_name) \
699 static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \
700 static ssize_t loop_attr_do_show_##_name(struct device *d, \
701 struct device_attribute *attr, char *b) \
702 { \
703 return loop_attr_show(d, b, loop_attr_##_name##_show); \
704 } \
705 static struct device_attribute loop_attr_##_name = \
706 __ATTR(_name, S_IRUGO, loop_attr_do_show_##_name, NULL);
707
708 static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf)
709 {
710 ssize_t ret;
711 char *p = NULL;
712
713 spin_lock_irq(&lo->lo_lock);
714 if (lo->lo_backing_file)
715 p = d_path(&lo->lo_backing_file->f_path, buf, PAGE_SIZE - 1);
716 spin_unlock_irq(&lo->lo_lock);
717
718 if (IS_ERR_OR_NULL(p))
719 ret = PTR_ERR(p);
720 else {
721 ret = strlen(p);
722 memmove(buf, p, ret);
723 buf[ret++] = '\n';
724 buf[ret] = 0;
725 }
726
727 return ret;
728 }
729
730 static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf)
731 {
732 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset);
733 }
734
735 static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf)
736 {
737 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit);
738 }
739
740 static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf)
741 {
742 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR);
743
744 return sprintf(buf, "%s\n", autoclear ? "1" : "0");
745 }
746
747 static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf)
748 {
749 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN);
750
751 return sprintf(buf, "%s\n", partscan ? "1" : "0");
752 }
753
754 LOOP_ATTR_RO(backing_file);
755 LOOP_ATTR_RO(offset);
756 LOOP_ATTR_RO(sizelimit);
757 LOOP_ATTR_RO(autoclear);
758 LOOP_ATTR_RO(partscan);
759
760 static struct attribute *loop_attrs[] = {
761 &loop_attr_backing_file.attr,
762 &loop_attr_offset.attr,
763 &loop_attr_sizelimit.attr,
764 &loop_attr_autoclear.attr,
765 &loop_attr_partscan.attr,
766 NULL,
767 };
768
769 static struct attribute_group loop_attribute_group = {
770 .name = "loop",
771 .attrs= loop_attrs,
772 };
773
774 static int loop_sysfs_init(struct loop_device *lo)
775 {
776 return sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj,
777 &loop_attribute_group);
778 }
779
780 static void loop_sysfs_exit(struct loop_device *lo)
781 {
782 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj,
783 &loop_attribute_group);
784 }
785
786 static void loop_config_discard(struct loop_device *lo)
787 {
788 struct file *file = lo->lo_backing_file;
789 struct inode *inode = file->f_mapping->host;
790 struct request_queue *q = lo->lo_queue;
791
792 /*
793 * We use punch hole to reclaim the free space used by the
794 * image a.k.a. discard. However we do support discard if
795 * encryption is enabled, because it may give an attacker
796 * useful information.
797 */
798 if ((!file->f_op->fallocate) ||
799 lo->lo_encrypt_key_size) {
800 q->limits.discard_granularity = 0;
801 q->limits.discard_alignment = 0;
802 q->limits.max_discard_sectors = 0;
803 q->limits.discard_zeroes_data = 0;
804 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
805 return;
806 }
807
808 q->limits.discard_granularity = inode->i_sb->s_blocksize;
809 q->limits.discard_alignment = 0;
810 q->limits.max_discard_sectors = UINT_MAX >> 9;
811 q->limits.discard_zeroes_data = 1;
812 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
813 }
814
815 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
816 struct block_device *bdev, unsigned int arg)
817 {
818 struct file *file, *f;
819 struct inode *inode;
820 struct address_space *mapping;
821 unsigned lo_blocksize;
822 int lo_flags = 0;
823 int error;
824 loff_t size;
825
826 /* This is safe, since we have a reference from open(). */
827 __module_get(THIS_MODULE);
828
829 error = -EBADF;
830 file = fget(arg);
831 if (!file)
832 goto out;
833
834 error = -EBUSY;
835 if (lo->lo_state != Lo_unbound)
836 goto out_putf;
837
838 /* Avoid recursion */
839 f = file;
840 while (is_loop_device(f)) {
841 struct loop_device *l;
842
843 if (f->f_mapping->host->i_bdev == bdev)
844 goto out_putf;
845
846 l = f->f_mapping->host->i_bdev->bd_disk->private_data;
847 if (l->lo_state == Lo_unbound) {
848 error = -EINVAL;
849 goto out_putf;
850 }
851 f = l->lo_backing_file;
852 }
853
854 mapping = file->f_mapping;
855 inode = mapping->host;
856
857 error = -EINVAL;
858 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
859 goto out_putf;
860
861 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) ||
862 !file->f_op->write)
863 lo_flags |= LO_FLAGS_READ_ONLY;
864
865 lo_blocksize = S_ISBLK(inode->i_mode) ?
866 inode->i_bdev->bd_block_size : PAGE_SIZE;
867
868 error = -EFBIG;
869 size = get_loop_size(lo, file);
870 if ((loff_t)(sector_t)size != size)
871 goto out_putf;
872
873 error = 0;
874
875 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
876
877 lo->lo_blocksize = lo_blocksize;
878 lo->lo_device = bdev;
879 lo->lo_flags = lo_flags;
880 lo->lo_backing_file = file;
881 lo->transfer = transfer_none;
882 lo->ioctl = NULL;
883 lo->lo_sizelimit = 0;
884 lo->lo_bio_count = 0;
885 lo->old_gfp_mask = mapping_gfp_mask(mapping);
886 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
887
888 bio_list_init(&lo->lo_bio_list);
889
890 /*
891 * set queue make_request_fn, and add limits based on lower level
892 * device
893 */
894 blk_queue_make_request(lo->lo_queue, loop_make_request);
895 lo->lo_queue->queuedata = lo;
896
897 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
898 blk_queue_flush(lo->lo_queue, REQ_FLUSH);
899
900 set_capacity(lo->lo_disk, size);
901 bd_set_size(bdev, size << 9);
902 loop_sysfs_init(lo);
903 /* let user-space know about the new size */
904 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
905
906 set_blocksize(bdev, lo_blocksize);
907
908 lo->lo_thread = kthread_create(loop_thread, lo, "loop%d",
909 lo->lo_number);
910 if (IS_ERR(lo->lo_thread)) {
911 error = PTR_ERR(lo->lo_thread);
912 goto out_clr;
913 }
914 lo->lo_state = Lo_bound;
915 wake_up_process(lo->lo_thread);
916 if (part_shift)
917 lo->lo_flags |= LO_FLAGS_PARTSCAN;
918 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
919 ioctl_by_bdev(bdev, BLKRRPART, 0);
920 return 0;
921
922 out_clr:
923 loop_sysfs_exit(lo);
924 lo->lo_thread = NULL;
925 lo->lo_device = NULL;
926 lo->lo_backing_file = NULL;
927 lo->lo_flags = 0;
928 set_capacity(lo->lo_disk, 0);
929 invalidate_bdev(bdev);
930 bd_set_size(bdev, 0);
931 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
932 mapping_set_gfp_mask(mapping, lo->old_gfp_mask);
933 lo->lo_state = Lo_unbound;
934 out_putf:
935 fput(file);
936 out:
937 /* This is safe: open() is still holding a reference. */
938 module_put(THIS_MODULE);
939 return error;
940 }
941
942 static int
943 loop_release_xfer(struct loop_device *lo)
944 {
945 int err = 0;
946 struct loop_func_table *xfer = lo->lo_encryption;
947
948 if (xfer) {
949 if (xfer->release)
950 err = xfer->release(lo);
951 lo->transfer = NULL;
952 lo->lo_encryption = NULL;
953 module_put(xfer->owner);
954 }
955 return err;
956 }
957
958 static int
959 loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
960 const struct loop_info64 *i)
961 {
962 int err = 0;
963
964 if (xfer) {
965 struct module *owner = xfer->owner;
966
967 if (!try_module_get(owner))
968 return -EINVAL;
969 if (xfer->init)
970 err = xfer->init(lo, i);
971 if (err)
972 module_put(owner);
973 else
974 lo->lo_encryption = xfer;
975 }
976 return err;
977 }
978
979 static int loop_clr_fd(struct loop_device *lo)
980 {
981 struct file *filp = lo->lo_backing_file;
982 gfp_t gfp = lo->old_gfp_mask;
983 struct block_device *bdev = lo->lo_device;
984
985 if (lo->lo_state != Lo_bound)
986 return -ENXIO;
987
988 if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
989 return -EBUSY;
990
991 if (filp == NULL)
992 return -EINVAL;
993
994 spin_lock_irq(&lo->lo_lock);
995 lo->lo_state = Lo_rundown;
996 spin_unlock_irq(&lo->lo_lock);
997
998 kthread_stop(lo->lo_thread);
999
1000 spin_lock_irq(&lo->lo_lock);
1001 lo->lo_backing_file = NULL;
1002 spin_unlock_irq(&lo->lo_lock);
1003
1004 loop_release_xfer(lo);
1005 lo->transfer = NULL;
1006 lo->ioctl = NULL;
1007 lo->lo_device = NULL;
1008 lo->lo_encryption = NULL;
1009 lo->lo_offset = 0;
1010 lo->lo_sizelimit = 0;
1011 lo->lo_encrypt_key_size = 0;
1012 lo->lo_thread = NULL;
1013 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
1014 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
1015 memset(lo->lo_file_name, 0, LO_NAME_SIZE);
1016 if (bdev)
1017 invalidate_bdev(bdev);
1018 set_capacity(lo->lo_disk, 0);
1019 loop_sysfs_exit(lo);
1020 if (bdev) {
1021 bd_set_size(bdev, 0);
1022 /* let user-space know about this change */
1023 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
1024 }
1025 mapping_set_gfp_mask(filp->f_mapping, gfp);
1026 lo->lo_state = Lo_unbound;
1027 /* This is safe: open() is still holding a reference. */
1028 module_put(THIS_MODULE);
1029 if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
1030 ioctl_by_bdev(bdev, BLKRRPART, 0);
1031 lo->lo_flags = 0;
1032 if (!part_shift)
1033 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1034 mutex_unlock(&lo->lo_ctl_mutex);
1035 /*
1036 * Need not hold lo_ctl_mutex to fput backing file.
1037 * Calling fput holding lo_ctl_mutex triggers a circular
1038 * lock dependency possibility warning as fput can take
1039 * bd_mutex which is usually taken before lo_ctl_mutex.
1040 */
1041 fput(filp);
1042 return 0;
1043 }
1044
1045 static int
1046 loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1047 {
1048 int err;
1049 struct loop_func_table *xfer;
1050 kuid_t uid = current_uid();
1051
1052 if (lo->lo_encrypt_key_size &&
1053 !uid_eq(lo->lo_key_owner, uid) &&
1054 !capable(CAP_SYS_ADMIN))
1055 return -EPERM;
1056 if (lo->lo_state != Lo_bound)
1057 return -ENXIO;
1058 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
1059 return -EINVAL;
1060
1061 err = loop_release_xfer(lo);
1062 if (err)
1063 return err;
1064
1065 if (info->lo_encrypt_type) {
1066 unsigned int type = info->lo_encrypt_type;
1067
1068 if (type >= MAX_LO_CRYPT)
1069 return -EINVAL;
1070 xfer = xfer_funcs[type];
1071 if (xfer == NULL)
1072 return -EINVAL;
1073 } else
1074 xfer = NULL;
1075
1076 err = loop_init_xfer(lo, xfer, info);
1077 if (err)
1078 return err;
1079
1080 if (lo->lo_offset != info->lo_offset ||
1081 lo->lo_sizelimit != info->lo_sizelimit) {
1082 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit))
1083 return -EFBIG;
1084 }
1085 loop_config_discard(lo);
1086
1087 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
1088 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
1089 lo->lo_file_name[LO_NAME_SIZE-1] = 0;
1090 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
1091
1092 if (!xfer)
1093 xfer = &none_funcs;
1094 lo->transfer = xfer->transfer;
1095 lo->ioctl = xfer->ioctl;
1096
1097 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) !=
1098 (info->lo_flags & LO_FLAGS_AUTOCLEAR))
1099 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR;
1100
1101 if ((info->lo_flags & LO_FLAGS_PARTSCAN) &&
1102 !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
1103 lo->lo_flags |= LO_FLAGS_PARTSCAN;
1104 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
1105 ioctl_by_bdev(lo->lo_device, BLKRRPART, 0);
1106 }
1107
1108 lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
1109 lo->lo_init[0] = info->lo_init[0];
1110 lo->lo_init[1] = info->lo_init[1];
1111 if (info->lo_encrypt_key_size) {
1112 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
1113 info->lo_encrypt_key_size);
1114 lo->lo_key_owner = uid;
1115 }
1116
1117 return 0;
1118 }
1119
1120 static int
1121 loop_get_status(struct loop_device *lo, struct loop_info64 *info)
1122 {
1123 struct file *file = lo->lo_backing_file;
1124 struct kstat stat;
1125 int error;
1126
1127 if (lo->lo_state != Lo_bound)
1128 return -ENXIO;
1129 error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat);
1130 if (error)
1131 return error;
1132 memset(info, 0, sizeof(*info));
1133 info->lo_number = lo->lo_number;
1134 info->lo_device = huge_encode_dev(stat.dev);
1135 info->lo_inode = stat.ino;
1136 info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
1137 info->lo_offset = lo->lo_offset;
1138 info->lo_sizelimit = lo->lo_sizelimit;
1139 info->lo_flags = lo->lo_flags;
1140 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
1141 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
1142 info->lo_encrypt_type =
1143 lo->lo_encryption ? lo->lo_encryption->number : 0;
1144 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
1145 info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
1146 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
1147 lo->lo_encrypt_key_size);
1148 }
1149 return 0;
1150 }
1151
1152 static void
1153 loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
1154 {
1155 memset(info64, 0, sizeof(*info64));
1156 info64->lo_number = info->lo_number;
1157 info64->lo_device = info->lo_device;
1158 info64->lo_inode = info->lo_inode;
1159 info64->lo_rdevice = info->lo_rdevice;
1160 info64->lo_offset = info->lo_offset;
1161 info64->lo_sizelimit = 0;
1162 info64->lo_encrypt_type = info->lo_encrypt_type;
1163 info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
1164 info64->lo_flags = info->lo_flags;
1165 info64->lo_init[0] = info->lo_init[0];
1166 info64->lo_init[1] = info->lo_init[1];
1167 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1168 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
1169 else
1170 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
1171 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
1172 }
1173
1174 static int
1175 loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
1176 {
1177 memset(info, 0, sizeof(*info));
1178 info->lo_number = info64->lo_number;
1179 info->lo_device = info64->lo_device;
1180 info->lo_inode = info64->lo_inode;
1181 info->lo_rdevice = info64->lo_rdevice;
1182 info->lo_offset = info64->lo_offset;
1183 info->lo_encrypt_type = info64->lo_encrypt_type;
1184 info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
1185 info->lo_flags = info64->lo_flags;
1186 info->lo_init[0] = info64->lo_init[0];
1187 info->lo_init[1] = info64->lo_init[1];
1188 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1189 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1190 else
1191 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
1192 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1193
1194 /* error in case values were truncated */
1195 if (info->lo_device != info64->lo_device ||
1196 info->lo_rdevice != info64->lo_rdevice ||
1197 info->lo_inode != info64->lo_inode ||
1198 info->lo_offset != info64->lo_offset)
1199 return -EOVERFLOW;
1200
1201 return 0;
1202 }
1203
1204 static int
1205 loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1206 {
1207 struct loop_info info;
1208 struct loop_info64 info64;
1209
1210 if (copy_from_user(&info, arg, sizeof (struct loop_info)))
1211 return -EFAULT;
1212 loop_info64_from_old(&info, &info64);
1213 return loop_set_status(lo, &info64);
1214 }
1215
1216 static int
1217 loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1218 {
1219 struct loop_info64 info64;
1220
1221 if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
1222 return -EFAULT;
1223 return loop_set_status(lo, &info64);
1224 }
1225
1226 static int
1227 loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
1228 struct loop_info info;
1229 struct loop_info64 info64;
1230 int err = 0;
1231
1232 if (!arg)
1233 err = -EINVAL;
1234 if (!err)
1235 err = loop_get_status(lo, &info64);
1236 if (!err)
1237 err = loop_info64_to_old(&info64, &info);
1238 if (!err && copy_to_user(arg, &info, sizeof(info)))
1239 err = -EFAULT;
1240
1241 return err;
1242 }
1243
1244 static int
1245 loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
1246 struct loop_info64 info64;
1247 int err = 0;
1248
1249 if (!arg)
1250 err = -EINVAL;
1251 if (!err)
1252 err = loop_get_status(lo, &info64);
1253 if (!err && copy_to_user(arg, &info64, sizeof(info64)))
1254 err = -EFAULT;
1255
1256 return err;
1257 }
1258
1259 static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev)
1260 {
1261 int err;
1262 sector_t sec;
1263 loff_t sz;
1264
1265 err = -ENXIO;
1266 if (unlikely(lo->lo_state != Lo_bound))
1267 goto out;
1268 err = figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
1269 if (unlikely(err))
1270 goto out;
1271 sec = get_capacity(lo->lo_disk);
1272 /* the width of sector_t may be narrow for bit-shift */
1273 sz = sec;
1274 sz <<= 9;
1275 mutex_lock(&bdev->bd_mutex);
1276 bd_set_size(bdev, sz);
1277 /* let user-space know about the new size */
1278 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
1279 mutex_unlock(&bdev->bd_mutex);
1280
1281 out:
1282 return err;
1283 }
1284
1285 static int lo_ioctl(struct block_device *bdev, fmode_t mode,
1286 unsigned int cmd, unsigned long arg)
1287 {
1288 struct loop_device *lo = bdev->bd_disk->private_data;
1289 int err;
1290
1291 mutex_lock_nested(&lo->lo_ctl_mutex, 1);
1292 switch (cmd) {
1293 case LOOP_SET_FD:
1294 err = loop_set_fd(lo, mode, bdev, arg);
1295 break;
1296 case LOOP_CHANGE_FD:
1297 err = loop_change_fd(lo, bdev, arg);
1298 break;
1299 case LOOP_CLR_FD:
1300 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
1301 err = loop_clr_fd(lo);
1302 if (!err)
1303 goto out_unlocked;
1304 break;
1305 case LOOP_SET_STATUS:
1306 err = -EPERM;
1307 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1308 err = loop_set_status_old(lo,
1309 (struct loop_info __user *)arg);
1310 break;
1311 case LOOP_GET_STATUS:
1312 err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1313 break;
1314 case LOOP_SET_STATUS64:
1315 err = -EPERM;
1316 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1317 err = loop_set_status64(lo,
1318 (struct loop_info64 __user *) arg);
1319 break;
1320 case LOOP_GET_STATUS64:
1321 err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1322 break;
1323 case LOOP_SET_CAPACITY:
1324 err = -EPERM;
1325 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN))
1326 err = loop_set_capacity(lo, bdev);
1327 break;
1328 default:
1329 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
1330 }
1331 mutex_unlock(&lo->lo_ctl_mutex);
1332
1333 out_unlocked:
1334 return err;
1335 }
1336
1337 #ifdef CONFIG_COMPAT
1338 struct compat_loop_info {
1339 compat_int_t lo_number; /* ioctl r/o */
1340 compat_dev_t lo_device; /* ioctl r/o */
1341 compat_ulong_t lo_inode; /* ioctl r/o */
1342 compat_dev_t lo_rdevice; /* ioctl r/o */
1343 compat_int_t lo_offset;
1344 compat_int_t lo_encrypt_type;
1345 compat_int_t lo_encrypt_key_size; /* ioctl w/o */
1346 compat_int_t lo_flags; /* ioctl r/o */
1347 char lo_name[LO_NAME_SIZE];
1348 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
1349 compat_ulong_t lo_init[2];
1350 char reserved[4];
1351 };
1352
1353 /*
1354 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info
1355 * - noinlined to reduce stack space usage in main part of driver
1356 */
1357 static noinline int
1358 loop_info64_from_compat(const struct compat_loop_info __user *arg,
1359 struct loop_info64 *info64)
1360 {
1361 struct compat_loop_info info;
1362
1363 if (copy_from_user(&info, arg, sizeof(info)))
1364 return -EFAULT;
1365
1366 memset(info64, 0, sizeof(*info64));
1367 info64->lo_number = info.lo_number;
1368 info64->lo_device = info.lo_device;
1369 info64->lo_inode = info.lo_inode;
1370 info64->lo_rdevice = info.lo_rdevice;
1371 info64->lo_offset = info.lo_offset;
1372 info64->lo_sizelimit = 0;
1373 info64->lo_encrypt_type = info.lo_encrypt_type;
1374 info64->lo_encrypt_key_size = info.lo_encrypt_key_size;
1375 info64->lo_flags = info.lo_flags;
1376 info64->lo_init[0] = info.lo_init[0];
1377 info64->lo_init[1] = info.lo_init[1];
1378 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1379 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
1380 else
1381 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
1382 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE);
1383 return 0;
1384 }
1385
1386 /*
1387 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace
1388 * - noinlined to reduce stack space usage in main part of driver
1389 */
1390 static noinline int
1391 loop_info64_to_compat(const struct loop_info64 *info64,
1392 struct compat_loop_info __user *arg)
1393 {
1394 struct compat_loop_info info;
1395
1396 memset(&info, 0, sizeof(info));
1397 info.lo_number = info64->lo_number;
1398 info.lo_device = info64->lo_device;
1399 info.lo_inode = info64->lo_inode;
1400 info.lo_rdevice = info64->lo_rdevice;
1401 info.lo_offset = info64->lo_offset;
1402 info.lo_encrypt_type = info64->lo_encrypt_type;
1403 info.lo_encrypt_key_size = info64->lo_encrypt_key_size;
1404 info.lo_flags = info64->lo_flags;
1405 info.lo_init[0] = info64->lo_init[0];
1406 info.lo_init[1] = info64->lo_init[1];
1407 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
1408 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
1409 else
1410 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
1411 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
1412
1413 /* error in case values were truncated */
1414 if (info.lo_device != info64->lo_device ||
1415 info.lo_rdevice != info64->lo_rdevice ||
1416 info.lo_inode != info64->lo_inode ||
1417 info.lo_offset != info64->lo_offset ||
1418 info.lo_init[0] != info64->lo_init[0] ||
1419 info.lo_init[1] != info64->lo_init[1])
1420 return -EOVERFLOW;
1421
1422 if (copy_to_user(arg, &info, sizeof(info)))
1423 return -EFAULT;
1424 return 0;
1425 }
1426
1427 static int
1428 loop_set_status_compat(struct loop_device *lo,
1429 const struct compat_loop_info __user *arg)
1430 {
1431 struct loop_info64 info64;
1432 int ret;
1433
1434 ret = loop_info64_from_compat(arg, &info64);
1435 if (ret < 0)
1436 return ret;
1437 return loop_set_status(lo, &info64);
1438 }
1439
1440 static int
1441 loop_get_status_compat(struct loop_device *lo,
1442 struct compat_loop_info __user *arg)
1443 {
1444 struct loop_info64 info64;
1445 int err = 0;
1446
1447 if (!arg)
1448 err = -EINVAL;
1449 if (!err)
1450 err = loop_get_status(lo, &info64);
1451 if (!err)
1452 err = loop_info64_to_compat(&info64, arg);
1453 return err;
1454 }
1455
1456 static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
1457 unsigned int cmd, unsigned long arg)
1458 {
1459 struct loop_device *lo = bdev->bd_disk->private_data;
1460 int err;
1461
1462 switch(cmd) {
1463 case LOOP_SET_STATUS:
1464 mutex_lock(&lo->lo_ctl_mutex);
1465 err = loop_set_status_compat(
1466 lo, (const struct compat_loop_info __user *) arg);
1467 mutex_unlock(&lo->lo_ctl_mutex);
1468 break;
1469 case LOOP_GET_STATUS:
1470 mutex_lock(&lo->lo_ctl_mutex);
1471 err = loop_get_status_compat(
1472 lo, (struct compat_loop_info __user *) arg);
1473 mutex_unlock(&lo->lo_ctl_mutex);
1474 break;
1475 case LOOP_SET_CAPACITY:
1476 case LOOP_CLR_FD:
1477 case LOOP_GET_STATUS64:
1478 case LOOP_SET_STATUS64:
1479 arg = (unsigned long) compat_ptr(arg);
1480 case LOOP_SET_FD:
1481 case LOOP_CHANGE_FD:
1482 err = lo_ioctl(bdev, mode, cmd, arg);
1483 break;
1484 default:
1485 err = -ENOIOCTLCMD;
1486 break;
1487 }
1488 return err;
1489 }
1490 #endif
1491
1492 static int lo_open(struct block_device *bdev, fmode_t mode)
1493 {
1494 struct loop_device *lo;
1495 int err = 0;
1496
1497 mutex_lock(&loop_index_mutex);
1498 lo = bdev->bd_disk->private_data;
1499 if (!lo) {
1500 err = -ENXIO;
1501 goto out;
1502 }
1503
1504 mutex_lock(&lo->lo_ctl_mutex);
1505 lo->lo_refcnt++;
1506 mutex_unlock(&lo->lo_ctl_mutex);
1507 out:
1508 mutex_unlock(&loop_index_mutex);
1509 return err;
1510 }
1511
1512 static int lo_release(struct gendisk *disk, fmode_t mode)
1513 {
1514 struct loop_device *lo = disk->private_data;
1515 int err;
1516
1517 mutex_lock(&lo->lo_ctl_mutex);
1518
1519 if (--lo->lo_refcnt)
1520 goto out;
1521
1522 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1523 /*
1524 * In autoclear mode, stop the loop thread
1525 * and remove configuration after last close.
1526 */
1527 err = loop_clr_fd(lo);
1528 if (!err)
1529 goto out_unlocked;
1530 } else {
1531 /*
1532 * Otherwise keep thread (if running) and config,
1533 * but flush possible ongoing bios in thread.
1534 */
1535 loop_flush(lo);
1536 }
1537
1538 out:
1539 mutex_unlock(&lo->lo_ctl_mutex);
1540 out_unlocked:
1541 return 0;
1542 }
1543
1544 static const struct block_device_operations lo_fops = {
1545 .owner = THIS_MODULE,
1546 .open = lo_open,
1547 .release = lo_release,
1548 .ioctl = lo_ioctl,
1549 #ifdef CONFIG_COMPAT
1550 .compat_ioctl = lo_compat_ioctl,
1551 #endif
1552 };
1553
1554 /*
1555 * And now the modules code and kernel interface.
1556 */
1557 static int max_loop;
1558 module_param(max_loop, int, S_IRUGO);
1559 MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
1560 module_param(max_part, int, S_IRUGO);
1561 MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device");
1562 MODULE_LICENSE("GPL");
1563 MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
1564
1565 int loop_register_transfer(struct loop_func_table *funcs)
1566 {
1567 unsigned int n = funcs->number;
1568
1569 if (n >= MAX_LO_CRYPT || xfer_funcs[n])
1570 return -EINVAL;
1571 xfer_funcs[n] = funcs;
1572 return 0;
1573 }
1574
1575 static int unregister_transfer_cb(int id, void *ptr, void *data)
1576 {
1577 struct loop_device *lo = ptr;
1578 struct loop_func_table *xfer = data;
1579
1580 mutex_lock(&lo->lo_ctl_mutex);
1581 if (lo->lo_encryption == xfer)
1582 loop_release_xfer(lo);
1583 mutex_unlock(&lo->lo_ctl_mutex);
1584 return 0;
1585 }
1586
1587 int loop_unregister_transfer(int number)
1588 {
1589 unsigned int n = number;
1590 struct loop_func_table *xfer;
1591
1592 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL)
1593 return -EINVAL;
1594
1595 xfer_funcs[n] = NULL;
1596 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer);
1597 return 0;
1598 }
1599
1600 EXPORT_SYMBOL(loop_register_transfer);
1601 EXPORT_SYMBOL(loop_unregister_transfer);
1602
1603 static int loop_add(struct loop_device **l, int i)
1604 {
1605 struct loop_device *lo;
1606 struct gendisk *disk;
1607 int err;
1608
1609 err = -ENOMEM;
1610 lo = kzalloc(sizeof(*lo), GFP_KERNEL);
1611 if (!lo)
1612 goto out;
1613
1614 if (!idr_pre_get(&loop_index_idr, GFP_KERNEL))
1615 goto out_free_dev;
1616
1617 if (i >= 0) {
1618 int m;
1619
1620 /* create specific i in the index */
1621 err = idr_get_new_above(&loop_index_idr, lo, i, &m);
1622 if (err >= 0 && i != m) {
1623 idr_remove(&loop_index_idr, m);
1624 err = -EEXIST;
1625 }
1626 } else if (i == -1) {
1627 int m;
1628
1629 /* get next free nr */
1630 err = idr_get_new(&loop_index_idr, lo, &m);
1631 if (err >= 0)
1632 i = m;
1633 } else {
1634 err = -EINVAL;
1635 }
1636 if (err < 0)
1637 goto out_free_dev;
1638
1639 lo->lo_queue = blk_alloc_queue(GFP_KERNEL);
1640 if (!lo->lo_queue)
1641 goto out_free_dev;
1642
1643 disk = lo->lo_disk = alloc_disk(1 << part_shift);
1644 if (!disk)
1645 goto out_free_queue;
1646
1647 /*
1648 * Disable partition scanning by default. The in-kernel partition
1649 * scanning can be requested individually per-device during its
1650 * setup. Userspace can always add and remove partitions from all
1651 * devices. The needed partition minors are allocated from the
1652 * extended minor space, the main loop device numbers will continue
1653 * to match the loop minors, regardless of the number of partitions
1654 * used.
1655 *
1656 * If max_part is given, partition scanning is globally enabled for
1657 * all loop devices. The minors for the main loop devices will be
1658 * multiples of max_part.
1659 *
1660 * Note: Global-for-all-devices, set-only-at-init, read-only module
1661 * parameteters like 'max_loop' and 'max_part' make things needlessly
1662 * complicated, are too static, inflexible and may surprise
1663 * userspace tools. Parameters like this in general should be avoided.
1664 */
1665 if (!part_shift)
1666 disk->flags |= GENHD_FL_NO_PART_SCAN;
1667 disk->flags |= GENHD_FL_EXT_DEVT;
1668 mutex_init(&lo->lo_ctl_mutex);
1669 lo->lo_number = i;
1670 lo->lo_thread = NULL;
1671 init_waitqueue_head(&lo->lo_event);
1672 init_waitqueue_head(&lo->lo_req_wait);
1673 spin_lock_init(&lo->lo_lock);
1674 disk->major = LOOP_MAJOR;
1675 disk->first_minor = i << part_shift;
1676 disk->fops = &lo_fops;
1677 disk->private_data = lo;
1678 disk->queue = lo->lo_queue;
1679 sprintf(disk->disk_name, "loop%d", i);
1680 add_disk(disk);
1681 *l = lo;
1682 return lo->lo_number;
1683
1684 out_free_queue:
1685 blk_cleanup_queue(lo->lo_queue);
1686 out_free_dev:
1687 kfree(lo);
1688 out:
1689 return err;
1690 }
1691
1692 static void loop_remove(struct loop_device *lo)
1693 {
1694 del_gendisk(lo->lo_disk);
1695 blk_cleanup_queue(lo->lo_queue);
1696 put_disk(lo->lo_disk);
1697 kfree(lo);
1698 }
1699
1700 static int find_free_cb(int id, void *ptr, void *data)
1701 {
1702 struct loop_device *lo = ptr;
1703 struct loop_device **l = data;
1704
1705 if (lo->lo_state == Lo_unbound) {
1706 *l = lo;
1707 return 1;
1708 }
1709 return 0;
1710 }
1711
1712 static int loop_lookup(struct loop_device **l, int i)
1713 {
1714 struct loop_device *lo;
1715 int ret = -ENODEV;
1716
1717 if (i < 0) {
1718 int err;
1719
1720 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo);
1721 if (err == 1) {
1722 *l = lo;
1723 ret = lo->lo_number;
1724 }
1725 goto out;
1726 }
1727
1728 /* lookup and return a specific i */
1729 lo = idr_find(&loop_index_idr, i);
1730 if (lo) {
1731 *l = lo;
1732 ret = lo->lo_number;
1733 }
1734 out:
1735 return ret;
1736 }
1737
1738 static struct kobject *loop_probe(dev_t dev, int *part, void *data)
1739 {
1740 struct loop_device *lo;
1741 struct kobject *kobj;
1742 int err;
1743
1744 mutex_lock(&loop_index_mutex);
1745 err = loop_lookup(&lo, MINOR(dev) >> part_shift);
1746 if (err < 0)
1747 err = loop_add(&lo, MINOR(dev) >> part_shift);
1748 if (err < 0)
1749 kobj = ERR_PTR(err);
1750 else
1751 kobj = get_disk(lo->lo_disk);
1752 mutex_unlock(&loop_index_mutex);
1753
1754 *part = 0;
1755 return kobj;
1756 }
1757
1758 static long loop_control_ioctl(struct file *file, unsigned int cmd,
1759 unsigned long parm)
1760 {
1761 struct loop_device *lo;
1762 int ret = -ENOSYS;
1763
1764 mutex_lock(&loop_index_mutex);
1765 switch (cmd) {
1766 case LOOP_CTL_ADD:
1767 ret = loop_lookup(&lo, parm);
1768 if (ret >= 0) {
1769 ret = -EEXIST;
1770 break;
1771 }
1772 ret = loop_add(&lo, parm);
1773 break;
1774 case LOOP_CTL_REMOVE:
1775 ret = loop_lookup(&lo, parm);
1776 if (ret < 0)
1777 break;
1778 mutex_lock(&lo->lo_ctl_mutex);
1779 if (lo->lo_state != Lo_unbound) {
1780 ret = -EBUSY;
1781 mutex_unlock(&lo->lo_ctl_mutex);
1782 break;
1783 }
1784 if (lo->lo_refcnt > 0) {
1785 ret = -EBUSY;
1786 mutex_unlock(&lo->lo_ctl_mutex);
1787 break;
1788 }
1789 lo->lo_disk->private_data = NULL;
1790 mutex_unlock(&lo->lo_ctl_mutex);
1791 idr_remove(&loop_index_idr, lo->lo_number);
1792 loop_remove(lo);
1793 break;
1794 case LOOP_CTL_GET_FREE:
1795 ret = loop_lookup(&lo, -1);
1796 if (ret >= 0)
1797 break;
1798 ret = loop_add(&lo, -1);
1799 }
1800 mutex_unlock(&loop_index_mutex);
1801
1802 return ret;
1803 }
1804
1805 static const struct file_operations loop_ctl_fops = {
1806 .open = nonseekable_open,
1807 .unlocked_ioctl = loop_control_ioctl,
1808 .compat_ioctl = loop_control_ioctl,
1809 .owner = THIS_MODULE,
1810 .llseek = noop_llseek,
1811 };
1812
1813 static struct miscdevice loop_misc = {
1814 .minor = LOOP_CTRL_MINOR,
1815 .name = "loop-control",
1816 .fops = &loop_ctl_fops,
1817 };
1818
1819 MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR);
1820 MODULE_ALIAS("devname:loop-control");
1821
1822 static int __init loop_init(void)
1823 {
1824 int i, nr;
1825 unsigned long range;
1826 struct loop_device *lo;
1827 int err;
1828
1829 err = misc_register(&loop_misc);
1830 if (err < 0)
1831 return err;
1832
1833 part_shift = 0;
1834 if (max_part > 0) {
1835 part_shift = fls(max_part);
1836
1837 /*
1838 * Adjust max_part according to part_shift as it is exported
1839 * to user space so that user can decide correct minor number
1840 * if [s]he want to create more devices.
1841 *
1842 * Note that -1 is required because partition 0 is reserved
1843 * for the whole disk.
1844 */
1845 max_part = (1UL << part_shift) - 1;
1846 }
1847
1848 if ((1UL << part_shift) > DISK_MAX_PARTS)
1849 return -EINVAL;
1850
1851 if (max_loop > 1UL << (MINORBITS - part_shift))
1852 return -EINVAL;
1853
1854 /*
1855 * If max_loop is specified, create that many devices upfront.
1856 * This also becomes a hard limit. If max_loop is not specified,
1857 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
1858 * init time. Loop devices can be requested on-demand with the
1859 * /dev/loop-control interface, or be instantiated by accessing
1860 * a 'dead' device node.
1861 */
1862 if (max_loop) {
1863 nr = max_loop;
1864 range = max_loop << part_shift;
1865 } else {
1866 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT;
1867 range = 1UL << MINORBITS;
1868 }
1869
1870 if (register_blkdev(LOOP_MAJOR, "loop"))
1871 return -EIO;
1872
1873 blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
1874 THIS_MODULE, loop_probe, NULL, NULL);
1875
1876 /* pre-create number of devices given by config or max_loop */
1877 mutex_lock(&loop_index_mutex);
1878 for (i = 0; i < nr; i++)
1879 loop_add(&lo, i);
1880 mutex_unlock(&loop_index_mutex);
1881
1882 printk(KERN_INFO "loop: module loaded\n");
1883 return 0;
1884 }
1885
1886 static int loop_exit_cb(int id, void *ptr, void *data)
1887 {
1888 struct loop_device *lo = ptr;
1889
1890 loop_remove(lo);
1891 return 0;
1892 }
1893
1894 static void __exit loop_exit(void)
1895 {
1896 unsigned long range;
1897
1898 range = max_loop ? max_loop << part_shift : 1UL << MINORBITS;
1899
1900 idr_for_each(&loop_index_idr, &loop_exit_cb, NULL);
1901 idr_remove_all(&loop_index_idr);
1902 idr_destroy(&loop_index_idr);
1903
1904 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
1905 unregister_blkdev(LOOP_MAJOR, "loop");
1906
1907 misc_deregister(&loop_misc);
1908 }
1909
1910 module_init(loop_init);
1911 module_exit(loop_exit);
1912
1913 #ifndef MODULE
1914 static int __init max_loop_setup(char *str)
1915 {
1916 max_loop = simple_strtol(str, NULL, 0);
1917 return 1;
1918 }
1919
1920 __setup("max_loop=", max_loop_setup);
1921 #endif
This page took 0.092091 seconds and 5 git commands to generate.