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 - [email protected] Mar 20, 1997 16 * 17 * Added devfs support - Richard Gooch <[email protected]> 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, [email protected] 26 * 27 * Maximum number of loop devices now dynamic via max_loop module parameter. 28 * Russell Kroll <[email protected]> 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ø, <[email protected]>, 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 <[email protected]>, Nov 2000 38 * 39 * Support up to 256 loop devices 40 * Heinz Mauelshagen <[email protected]>, 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/writeback.h> 71 #include <linux/buffer_head.h> /* for invalidate_bdev() */ 72 #include <linux/completion.h> 73 #include <linux/highmem.h> 74 #include <linux/kthread.h> 75 #include <linux/splice.h> 76 77 #include <asm/uaccess.h> 78 79 static LIST_HEAD(loop_devices); 80 static DEFINE_MUTEX(loop_devices_mutex); 81 82 static int max_part; 83 static int part_shift; 84 85 /* 86 * Transfer functions 87 */ 88 static int transfer_none(struct loop_device *lo, int cmd, 89 struct page *raw_page, unsigned raw_off, 90 struct page *loop_page, unsigned loop_off, 91 int size, sector_t real_block) 92 { 93 char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off; 94 char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off; 95 96 if (cmd == READ) 97 memcpy(loop_buf, raw_buf, size); 98 else 99 memcpy(raw_buf, loop_buf, size); 100 101 kunmap_atomic(raw_buf, KM_USER0); 102 kunmap_atomic(loop_buf, KM_USER1); 103 cond_resched(); 104 return 0; 105 } 106 107 static int transfer_xor(struct loop_device *lo, int cmd, 108 struct page *raw_page, unsigned raw_off, 109 struct page *loop_page, unsigned loop_off, 110 int size, sector_t real_block) 111 { 112 char *raw_buf = kmap_atomic(raw_page, KM_USER0) + raw_off; 113 char *loop_buf = kmap_atomic(loop_page, KM_USER1) + loop_off; 114 char *in, *out, *key; 115 int i, keysize; 116 117 if (cmd == READ) { 118 in = raw_buf; 119 out = loop_buf; 120 } else { 121 in = loop_buf; 122 out = raw_buf; 123 } 124 125 key = lo->lo_encrypt_key; 126 keysize = lo->lo_encrypt_key_size; 127 for (i = 0; i < size; i++) 128 *out++ = *in++ ^ key[(i & 511) % keysize]; 129 130 kunmap_atomic(raw_buf, KM_USER0); 131 kunmap_atomic(loop_buf, KM_USER1); 132 cond_resched(); 133 return 0; 134 } 135 136 static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 137 { 138 if (unlikely(info->lo_encrypt_key_size <= 0)) 139 return -EINVAL; 140 return 0; 141 } 142 143 static struct loop_func_table none_funcs = { 144 .number = LO_CRYPT_NONE, 145 .transfer = transfer_none, 146 }; 147 148 static struct loop_func_table xor_funcs = { 149 .number = LO_CRYPT_XOR, 150 .transfer = transfer_xor, 151 .init = xor_init 152 }; 153 154 /* xfer_funcs[0] is special - its release function is never called */ 155 static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 156 &none_funcs, 157 &xor_funcs 158 }; 159 160 static loff_t get_loop_size(struct loop_device *lo, struct file *file) 161 { 162 loff_t size, offset, loopsize; 163 164 /* Compute loopsize in bytes */ 165 size = i_size_read(file->f_mapping->host); 166 offset = lo->lo_offset; 167 loopsize = size - offset; 168 if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize) 169 loopsize = lo->lo_sizelimit; 170 171 /* 172 * Unfortunately, if we want to do I/O on the device, 173 * the number of 512-byte sectors has to fit into a sector_t. 174 */ 175 return loopsize >> 9; 176 } 177 178 static int 179 figure_loop_size(struct loop_device *lo) 180 { 181 loff_t size = get_loop_size(lo, lo->lo_backing_file); 182 sector_t x = (sector_t)size; 183 184 if (unlikely((loff_t)x != size)) 185 return -EFBIG; 186 187 set_capacity(lo->lo_disk, x); 188 return 0; 189 } 190 191 static inline int 192 lo_do_transfer(struct loop_device *lo, int cmd, 193 struct page *rpage, unsigned roffs, 194 struct page *lpage, unsigned loffs, 195 int size, sector_t rblock) 196 { 197 if (unlikely(!lo->transfer)) 198 return 0; 199 200 return lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 201 } 202 203 /** 204 * do_lo_send_aops - helper for writing data to a loop device 205 * 206 * This is the fast version for backing filesystems which implement the address 207 * space operations write_begin and write_end. 208 */ 209 static int do_lo_send_aops(struct loop_device *lo, struct bio_vec *bvec, 210 loff_t pos, struct page *unused) 211 { 212 struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */ 213 struct address_space *mapping = file->f_mapping; 214 pgoff_t index; 215 unsigned offset, bv_offs; 216 int len, ret; 217 218 mutex_lock(&mapping->host->i_mutex); 219 index = pos >> PAGE_CACHE_SHIFT; 220 offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1); 221 bv_offs = bvec->bv_offset; 222 len = bvec->bv_len; 223 while (len > 0) { 224 sector_t IV; 225 unsigned size, copied; 226 int transfer_result; 227 struct page *page; 228 void *fsdata; 229 230 IV = ((sector_t)index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9); 231 size = PAGE_CACHE_SIZE - offset; 232 if (size > len) 233 size = len; 234 235 ret = pagecache_write_begin(file, mapping, pos, size, 0, 236 &page, &fsdata); 237 if (ret) 238 goto fail; 239 240 transfer_result = lo_do_transfer(lo, WRITE, page, offset, 241 bvec->bv_page, bv_offs, size, IV); 242 copied = size; 243 if (unlikely(transfer_result)) 244 copied = 0; 245 246 ret = pagecache_write_end(file, mapping, pos, size, copied, 247 page, fsdata); 248 if (ret < 0 || ret != copied) 249 goto fail; 250 251 if (unlikely(transfer_result)) 252 goto fail; 253 254 bv_offs += copied; 255 len -= copied; 256 offset = 0; 257 index++; 258 pos += copied; 259 } 260 ret = 0; 261 out: 262 mutex_unlock(&mapping->host->i_mutex); 263 return ret; 264 fail: 265 ret = -1; 266 goto out; 267 } 268 269 /** 270 * __do_lo_send_write - helper for writing data to a loop device 271 * 272 * This helper just factors out common code between do_lo_send_direct_write() 273 * and do_lo_send_write(). 274 */ 275 static int __do_lo_send_write(struct file *file, 276 u8 *buf, const int len, loff_t pos) 277 { 278 ssize_t bw; 279 mm_segment_t old_fs = get_fs(); 280 281 set_fs(get_ds()); 282 bw = file->f_op->write(file, buf, len, &pos); 283 set_fs(old_fs); 284 if (likely(bw == len)) 285 return 0; 286 printk(KERN_ERR "loop: Write error at byte offset %llu, length %i.\n", 287 (unsigned long long)pos, len); 288 if (bw >= 0) 289 bw = -EIO; 290 return bw; 291 } 292 293 /** 294 * do_lo_send_direct_write - helper for writing data to a loop device 295 * 296 * This is the fast, non-transforming version for backing filesystems which do 297 * not implement the address space operations write_begin and write_end. 298 * It uses the write file operation which should be present on all writeable 299 * filesystems. 300 */ 301 static int do_lo_send_direct_write(struct loop_device *lo, 302 struct bio_vec *bvec, loff_t pos, struct page *page) 303 { 304 ssize_t bw = __do_lo_send_write(lo->lo_backing_file, 305 kmap(bvec->bv_page) + bvec->bv_offset, 306 bvec->bv_len, pos); 307 kunmap(bvec->bv_page); 308 cond_resched(); 309 return bw; 310 } 311 312 /** 313 * do_lo_send_write - helper for writing data to a loop device 314 * 315 * This is the slow, transforming version for filesystems which do not 316 * implement the address space operations write_begin and write_end. It 317 * uses the write file operation which should be present on all writeable 318 * filesystems. 319 * 320 * Using fops->write is slower than using aops->{prepare,commit}_write in the 321 * transforming case because we need to double buffer the data as we cannot do 322 * the transformations in place as we do not have direct access to the 323 * destination pages of the backing file. 324 */ 325 static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec, 326 loff_t pos, struct page *page) 327 { 328 int ret = lo_do_transfer(lo, WRITE, page, 0, bvec->bv_page, 329 bvec->bv_offset, bvec->bv_len, pos >> 9); 330 if (likely(!ret)) 331 return __do_lo_send_write(lo->lo_backing_file, 332 page_address(page), bvec->bv_len, 333 pos); 334 printk(KERN_ERR "loop: Transfer error at byte offset %llu, " 335 "length %i.\n", (unsigned long long)pos, bvec->bv_len); 336 if (ret > 0) 337 ret = -EIO; 338 return ret; 339 } 340 341 static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos) 342 { 343 int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t, 344 struct page *page); 345 struct bio_vec *bvec; 346 struct page *page = NULL; 347 int i, ret = 0; 348 349 do_lo_send = do_lo_send_aops; 350 if (!(lo->lo_flags & LO_FLAGS_USE_AOPS)) { 351 do_lo_send = do_lo_send_direct_write; 352 if (lo->transfer != transfer_none) { 353 page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); 354 if (unlikely(!page)) 355 goto fail; 356 kmap(page); 357 do_lo_send = do_lo_send_write; 358 } 359 } 360 bio_for_each_segment(bvec, bio, i) { 361 ret = do_lo_send(lo, bvec, pos, page); 362 if (ret < 0) 363 break; 364 pos += bvec->bv_len; 365 } 366 if (page) { 367 kunmap(page); 368 __free_page(page); 369 } 370 out: 371 return ret; 372 fail: 373 printk(KERN_ERR "loop: Failed to allocate temporary page for write.\n"); 374 ret = -ENOMEM; 375 goto out; 376 } 377 378 struct lo_read_data { 379 struct loop_device *lo; 380 struct page *page; 381 unsigned offset; 382 int bsize; 383 }; 384 385 static int 386 lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, 387 struct splice_desc *sd) 388 { 389 struct lo_read_data *p = sd->u.data; 390 struct loop_device *lo = p->lo; 391 struct page *page = buf->page; 392 sector_t IV; 393 int size, ret; 394 395 ret = buf->ops->confirm(pipe, buf); 396 if (unlikely(ret)) 397 return ret; 398 399 IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) + 400 (buf->offset >> 9); 401 size = sd->len; 402 if (size > p->bsize) 403 size = p->bsize; 404 405 if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) { 406 printk(KERN_ERR "loop: transfer error block %ld\n", 407 page->index); 408 size = -EINVAL; 409 } 410 411 flush_dcache_page(p->page); 412 413 if (size > 0) 414 p->offset += size; 415 416 return size; 417 } 418 419 static int 420 lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd) 421 { 422 return __splice_from_pipe(pipe, sd, lo_splice_actor); 423 } 424 425 static int 426 do_lo_receive(struct loop_device *lo, 427 struct bio_vec *bvec, int bsize, loff_t pos) 428 { 429 struct lo_read_data cookie; 430 struct splice_desc sd; 431 struct file *file; 432 long retval; 433 434 cookie.lo = lo; 435 cookie.page = bvec->bv_page; 436 cookie.offset = bvec->bv_offset; 437 cookie.bsize = bsize; 438 439 sd.len = 0; 440 sd.total_len = bvec->bv_len; 441 sd.flags = 0; 442 sd.pos = pos; 443 sd.u.data = &cookie; 444 445 file = lo->lo_backing_file; 446 retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor); 447 448 if (retval < 0) 449 return retval; 450 451 return 0; 452 } 453 454 static int 455 lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos) 456 { 457 struct bio_vec *bvec; 458 int i, ret = 0; 459 460 bio_for_each_segment(bvec, bio, i) { 461 ret = do_lo_receive(lo, bvec, bsize, pos); 462 if (ret < 0) 463 break; 464 pos += bvec->bv_len; 465 } 466 return ret; 467 } 468 469 static int do_bio_filebacked(struct loop_device *lo, struct bio *bio) 470 { 471 loff_t pos; 472 int ret; 473 474 pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset; 475 476 if (bio_rw(bio) == WRITE) { 477 bool barrier = bio_rw_flagged(bio, BIO_RW_BARRIER); 478 struct file *file = lo->lo_backing_file; 479 480 if (barrier) { 481 if (unlikely(!file->f_op->fsync)) { 482 ret = -EOPNOTSUPP; 483 goto out; 484 } 485 486 ret = vfs_fsync(file, file->f_path.dentry, 0); 487 if (unlikely(ret)) { 488 ret = -EIO; 489 goto out; 490 } 491 } 492 493 ret = lo_send(lo, bio, pos); 494 495 if (barrier && !ret) { 496 ret = vfs_fsync(file, file->f_path.dentry, 0); 497 if (unlikely(ret)) 498 ret = -EIO; 499 } 500 } else 501 ret = lo_receive(lo, bio, lo->lo_blocksize, pos); 502 503 out: 504 return ret; 505 } 506 507 /* 508 * Add bio to back of pending list 509 */ 510 static void loop_add_bio(struct loop_device *lo, struct bio *bio) 511 { 512 bio_list_add(&lo->lo_bio_list, bio); 513 } 514 515 /* 516 * Grab first pending buffer 517 */ 518 static struct bio *loop_get_bio(struct loop_device *lo) 519 { 520 return bio_list_pop(&lo->lo_bio_list); 521 } 522 523 static int loop_make_request(struct request_queue *q, struct bio *old_bio) 524 { 525 struct loop_device *lo = q->queuedata; 526 int rw = bio_rw(old_bio); 527 528 if (rw == READA) 529 rw = READ; 530 531 BUG_ON(!lo || (rw != READ && rw != WRITE)); 532 533 spin_lock_irq(&lo->lo_lock); 534 if (lo->lo_state != Lo_bound) 535 goto out; 536 if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY))) 537 goto out; 538 loop_add_bio(lo, old_bio); 539 wake_up(&lo->lo_event); 540 spin_unlock_irq(&lo->lo_lock); 541 return 0; 542 543 out: 544 spin_unlock_irq(&lo->lo_lock); 545 bio_io_error(old_bio); 546 return 0; 547 } 548 549 /* 550 * kick off io on the underlying address space 551 */ 552 static void loop_unplug(struct request_queue *q) 553 { 554 struct loop_device *lo = q->queuedata; 555 556 queue_flag_clear_unlocked(QUEUE_FLAG_PLUGGED, q); 557 blk_run_address_space(lo->lo_backing_file->f_mapping); 558 } 559 560 struct switch_request { 561 struct file *file; 562 struct completion wait; 563 }; 564 565 static void do_loop_switch(struct loop_device *, struct switch_request *); 566 567 static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio) 568 { 569 if (unlikely(!bio->bi_bdev)) { 570 do_loop_switch(lo, bio->bi_private); 571 bio_put(bio); 572 } else { 573 int ret = do_bio_filebacked(lo, bio); 574 bio_endio(bio, ret); 575 } 576 } 577 578 /* 579 * worker thread that handles reads/writes to file backed loop devices, 580 * to avoid blocking in our make_request_fn. it also does loop decrypting 581 * on reads for block backed loop, as that is too heavy to do from 582 * b_end_io context where irqs may be disabled. 583 * 584 * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before 585 * calling kthread_stop(). Therefore once kthread_should_stop() is 586 * true, make_request will not place any more requests. Therefore 587 * once kthread_should_stop() is true and lo_bio is NULL, we are 588 * done with the loop. 589 */ 590 static int loop_thread(void *data) 591 { 592 struct loop_device *lo = data; 593 struct bio *bio; 594 595 set_user_nice(current, -20); 596 597 while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) { 598 599 wait_event_interruptible(lo->lo_event, 600 !bio_list_empty(&lo->lo_bio_list) || 601 kthread_should_stop()); 602 603 if (bio_list_empty(&lo->lo_bio_list)) 604 continue; 605 spin_lock_irq(&lo->lo_lock); 606 bio = loop_get_bio(lo); 607 spin_unlock_irq(&lo->lo_lock); 608 609 BUG_ON(!bio); 610 loop_handle_bio(lo, bio); 611 } 612 613 return 0; 614 } 615 616 /* 617 * loop_switch performs the hard work of switching a backing store. 618 * First it needs to flush existing IO, it does this by sending a magic 619 * BIO down the pipe. The completion of this BIO does the actual switch. 620 */ 621 static int loop_switch(struct loop_device *lo, struct file *file) 622 { 623 struct switch_request w; 624 struct bio *bio = bio_alloc(GFP_KERNEL, 0); 625 if (!bio) 626 return -ENOMEM; 627 init_completion(&w.wait); 628 w.file = file; 629 bio->bi_private = &w; 630 bio->bi_bdev = NULL; 631 loop_make_request(lo->lo_queue, bio); 632 wait_for_completion(&w.wait); 633 return 0; 634 } 635 636 /* 637 * Helper to flush the IOs in loop, but keeping loop thread running 638 */ 639 static int loop_flush(struct loop_device *lo) 640 { 641 /* loop not yet configured, no running thread, nothing to flush */ 642 if (!lo->lo_thread) 643 return 0; 644 645 return loop_switch(lo, NULL); 646 } 647 648 /* 649 * Do the actual switch; called from the BIO completion routine 650 */ 651 static void do_loop_switch(struct loop_device *lo, struct switch_request *p) 652 { 653 struct file *file = p->file; 654 struct file *old_file = lo->lo_backing_file; 655 struct address_space *mapping; 656 657 /* if no new file, only flush of queued bios requested */ 658 if (!file) 659 goto out; 660 661 mapping = file->f_mapping; 662 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 663 lo->lo_backing_file = file; 664 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? 665 mapping->host->i_bdev->bd_block_size : PAGE_SIZE; 666 lo->old_gfp_mask = mapping_gfp_mask(mapping); 667 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 668 out: 669 complete(&p->wait); 670 } 671 672 673 /* 674 * loop_change_fd switched the backing store of a loopback device to 675 * a new file. This is useful for operating system installers to free up 676 * the original file and in High Availability environments to switch to 677 * an alternative location for the content in case of server meltdown. 678 * This can only work if the loop device is used read-only, and if the 679 * new backing store is the same size and type as the old backing store. 680 */ 681 static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 682 unsigned int arg) 683 { 684 struct file *file, *old_file; 685 struct inode *inode; 686 int error; 687 688 error = -ENXIO; 689 if (lo->lo_state != Lo_bound) 690 goto out; 691 692 /* the loop device has to be read-only */ 693 error = -EINVAL; 694 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 695 goto out; 696 697 error = -EBADF; 698 file = fget(arg); 699 if (!file) 700 goto out; 701 702 inode = file->f_mapping->host; 703 old_file = lo->lo_backing_file; 704 705 error = -EINVAL; 706 707 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 708 goto out_putf; 709 710 /* size of the new backing store needs to be the same */ 711 if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 712 goto out_putf; 713 714 /* and ... switch */ 715 error = loop_switch(lo, file); 716 if (error) 717 goto out_putf; 718 719 fput(old_file); 720 if (max_part > 0) 721 ioctl_by_bdev(bdev, BLKRRPART, 0); 722 return 0; 723 724 out_putf: 725 fput(file); 726 out: 727 return error; 728 } 729 730 static inline int is_loop_device(struct file *file) 731 { 732 struct inode *i = file->f_mapping->host; 733 734 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 735 } 736 737 static int loop_set_fd(struct loop_device *lo, fmode_t mode, 738 struct block_device *bdev, unsigned int arg) 739 { 740 struct file *file, *f; 741 struct inode *inode; 742 struct address_space *mapping; 743 unsigned lo_blocksize; 744 int lo_flags = 0; 745 int error; 746 loff_t size; 747 748 /* This is safe, since we have a reference from open(). */ 749 __module_get(THIS_MODULE); 750 751 error = -EBADF; 752 file = fget(arg); 753 if (!file) 754 goto out; 755 756 error = -EBUSY; 757 if (lo->lo_state != Lo_unbound) 758 goto out_putf; 759 760 /* Avoid recursion */ 761 f = file; 762 while (is_loop_device(f)) { 763 struct loop_device *l; 764 765 if (f->f_mapping->host->i_bdev == bdev) 766 goto out_putf; 767 768 l = f->f_mapping->host->i_bdev->bd_disk->private_data; 769 if (l->lo_state == Lo_unbound) { 770 error = -EINVAL; 771 goto out_putf; 772 } 773 f = l->lo_backing_file; 774 } 775 776 mapping = file->f_mapping; 777 inode = mapping->host; 778 779 if (!(file->f_mode & FMODE_WRITE)) 780 lo_flags |= LO_FLAGS_READ_ONLY; 781 782 error = -EINVAL; 783 if (S_ISREG(inode->i_mode) || S_ISBLK(inode->i_mode)) { 784 const struct address_space_operations *aops = mapping->a_ops; 785 786 if (aops->write_begin) 787 lo_flags |= LO_FLAGS_USE_AOPS; 788 if (!(lo_flags & LO_FLAGS_USE_AOPS) && !file->f_op->write) 789 lo_flags |= LO_FLAGS_READ_ONLY; 790 791 lo_blocksize = S_ISBLK(inode->i_mode) ? 792 inode->i_bdev->bd_block_size : PAGE_SIZE; 793 794 error = 0; 795 } else { 796 goto out_putf; 797 } 798 799 size = get_loop_size(lo, file); 800 801 if ((loff_t)(sector_t)size != size) { 802 error = -EFBIG; 803 goto out_putf; 804 } 805 806 if (!(mode & FMODE_WRITE)) 807 lo_flags |= LO_FLAGS_READ_ONLY; 808 809 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 810 811 lo->lo_blocksize = lo_blocksize; 812 lo->lo_device = bdev; 813 lo->lo_flags = lo_flags; 814 lo->lo_backing_file = file; 815 lo->transfer = transfer_none; 816 lo->ioctl = NULL; 817 lo->lo_sizelimit = 0; 818 lo->old_gfp_mask = mapping_gfp_mask(mapping); 819 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 820 821 bio_list_init(&lo->lo_bio_list); 822 823 /* 824 * set queue make_request_fn, and add limits based on lower level 825 * device 826 */ 827 blk_queue_make_request(lo->lo_queue, loop_make_request); 828 lo->lo_queue->queuedata = lo; 829 lo->lo_queue->unplug_fn = loop_unplug; 830 831 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 832 blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_DRAIN, NULL); 833 834 set_capacity(lo->lo_disk, size); 835 bd_set_size(bdev, size << 9); 836 837 set_blocksize(bdev, lo_blocksize); 838 839 lo->lo_thread = kthread_create(loop_thread, lo, "loop%d", 840 lo->lo_number); 841 if (IS_ERR(lo->lo_thread)) { 842 error = PTR_ERR(lo->lo_thread); 843 goto out_clr; 844 } 845 lo->lo_state = Lo_bound; 846 wake_up_process(lo->lo_thread); 847 if (max_part > 0) 848 ioctl_by_bdev(bdev, BLKRRPART, 0); 849 return 0; 850 851 out_clr: 852 lo->lo_thread = NULL; 853 lo->lo_device = NULL; 854 lo->lo_backing_file = NULL; 855 lo->lo_flags = 0; 856 set_capacity(lo->lo_disk, 0); 857 invalidate_bdev(bdev); 858 bd_set_size(bdev, 0); 859 mapping_set_gfp_mask(mapping, lo->old_gfp_mask); 860 lo->lo_state = Lo_unbound; 861 out_putf: 862 fput(file); 863 out: 864 /* This is safe: open() is still holding a reference. */ 865 module_put(THIS_MODULE); 866 return error; 867 } 868 869 static int 870 loop_release_xfer(struct loop_device *lo) 871 { 872 int err = 0; 873 struct loop_func_table *xfer = lo->lo_encryption; 874 875 if (xfer) { 876 if (xfer->release) 877 err = xfer->release(lo); 878 lo->transfer = NULL; 879 lo->lo_encryption = NULL; 880 module_put(xfer->owner); 881 } 882 return err; 883 } 884 885 static int 886 loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 887 const struct loop_info64 *i) 888 { 889 int err = 0; 890 891 if (xfer) { 892 struct module *owner = xfer->owner; 893 894 if (!try_module_get(owner)) 895 return -EINVAL; 896 if (xfer->init) 897 err = xfer->init(lo, i); 898 if (err) 899 module_put(owner); 900 else 901 lo->lo_encryption = xfer; 902 } 903 return err; 904 } 905 906 static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev) 907 { 908 struct file *filp = lo->lo_backing_file; 909 gfp_t gfp = lo->old_gfp_mask; 910 911 if (lo->lo_state != Lo_bound) 912 return -ENXIO; 913 914 if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */ 915 return -EBUSY; 916 917 if (filp == NULL) 918 return -EINVAL; 919 920 spin_lock_irq(&lo->lo_lock); 921 lo->lo_state = Lo_rundown; 922 spin_unlock_irq(&lo->lo_lock); 923 924 kthread_stop(lo->lo_thread); 925 926 lo->lo_queue->unplug_fn = NULL; 927 lo->lo_backing_file = NULL; 928 929 loop_release_xfer(lo); 930 lo->transfer = NULL; 931 lo->ioctl = NULL; 932 lo->lo_device = NULL; 933 lo->lo_encryption = NULL; 934 lo->lo_offset = 0; 935 lo->lo_sizelimit = 0; 936 lo->lo_encrypt_key_size = 0; 937 lo->lo_flags = 0; 938 lo->lo_thread = NULL; 939 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 940 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 941 memset(lo->lo_file_name, 0, LO_NAME_SIZE); 942 if (bdev) 943 invalidate_bdev(bdev); 944 set_capacity(lo->lo_disk, 0); 945 if (bdev) 946 bd_set_size(bdev, 0); 947 mapping_set_gfp_mask(filp->f_mapping, gfp); 948 lo->lo_state = Lo_unbound; 949 /* This is safe: open() is still holding a reference. */ 950 module_put(THIS_MODULE); 951 if (max_part > 0 && bdev) 952 ioctl_by_bdev(bdev, BLKRRPART, 0); 953 mutex_unlock(&lo->lo_ctl_mutex); 954 /* 955 * Need not hold lo_ctl_mutex to fput backing file. 956 * Calling fput holding lo_ctl_mutex triggers a circular 957 * lock dependency possibility warning as fput can take 958 * bd_mutex which is usually taken before lo_ctl_mutex. 959 */ 960 fput(filp); 961 return 0; 962 } 963 964 static int 965 loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 966 { 967 int err; 968 struct loop_func_table *xfer; 969 uid_t uid = current_uid(); 970 971 if (lo->lo_encrypt_key_size && 972 lo->lo_key_owner != uid && 973 !capable(CAP_SYS_ADMIN)) 974 return -EPERM; 975 if (lo->lo_state != Lo_bound) 976 return -ENXIO; 977 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) 978 return -EINVAL; 979 980 err = loop_release_xfer(lo); 981 if (err) 982 return err; 983 984 if (info->lo_encrypt_type) { 985 unsigned int type = info->lo_encrypt_type; 986 987 if (type >= MAX_LO_CRYPT) 988 return -EINVAL; 989 xfer = xfer_funcs[type]; 990 if (xfer == NULL) 991 return -EINVAL; 992 } else 993 xfer = NULL; 994 995 err = loop_init_xfer(lo, xfer, info); 996 if (err) 997 return err; 998 999 if (lo->lo_offset != info->lo_offset || 1000 lo->lo_sizelimit != info->lo_sizelimit) { 1001 lo->lo_offset = info->lo_offset; 1002 lo->lo_sizelimit = info->lo_sizelimit; 1003 if (figure_loop_size(lo)) 1004 return -EFBIG; 1005 } 1006 1007 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 1008 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 1009 lo->lo_file_name[LO_NAME_SIZE-1] = 0; 1010 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 1011 1012 if (!xfer) 1013 xfer = &none_funcs; 1014 lo->transfer = xfer->transfer; 1015 lo->ioctl = xfer->ioctl; 1016 1017 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 1018 (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 1019 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 1020 1021 lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 1022 lo->lo_init[0] = info->lo_init[0]; 1023 lo->lo_init[1] = info->lo_init[1]; 1024 if (info->lo_encrypt_key_size) { 1025 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 1026 info->lo_encrypt_key_size); 1027 lo->lo_key_owner = uid; 1028 } 1029 1030 return 0; 1031 } 1032 1033 static int 1034 loop_get_status(struct loop_device *lo, struct loop_info64 *info) 1035 { 1036 struct file *file = lo->lo_backing_file; 1037 struct kstat stat; 1038 int error; 1039 1040 if (lo->lo_state != Lo_bound) 1041 return -ENXIO; 1042 error = vfs_getattr(file->f_path.mnt, file->f_path.dentry, &stat); 1043 if (error) 1044 return error; 1045 memset(info, 0, sizeof(*info)); 1046 info->lo_number = lo->lo_number; 1047 info->lo_device = huge_encode_dev(stat.dev); 1048 info->lo_inode = stat.ino; 1049 info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev); 1050 info->lo_offset = lo->lo_offset; 1051 info->lo_sizelimit = lo->lo_sizelimit; 1052 info->lo_flags = lo->lo_flags; 1053 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 1054 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 1055 info->lo_encrypt_type = 1056 lo->lo_encryption ? lo->lo_encryption->number : 0; 1057 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 1058 info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 1059 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 1060 lo->lo_encrypt_key_size); 1061 } 1062 return 0; 1063 } 1064 1065 static void 1066 loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 1067 { 1068 memset(info64, 0, sizeof(*info64)); 1069 info64->lo_number = info->lo_number; 1070 info64->lo_device = info->lo_device; 1071 info64->lo_inode = info->lo_inode; 1072 info64->lo_rdevice = info->lo_rdevice; 1073 info64->lo_offset = info->lo_offset; 1074 info64->lo_sizelimit = 0; 1075 info64->lo_encrypt_type = info->lo_encrypt_type; 1076 info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 1077 info64->lo_flags = info->lo_flags; 1078 info64->lo_init[0] = info->lo_init[0]; 1079 info64->lo_init[1] = info->lo_init[1]; 1080 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1081 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 1082 else 1083 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 1084 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 1085 } 1086 1087 static int 1088 loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 1089 { 1090 memset(info, 0, sizeof(*info)); 1091 info->lo_number = info64->lo_number; 1092 info->lo_device = info64->lo_device; 1093 info->lo_inode = info64->lo_inode; 1094 info->lo_rdevice = info64->lo_rdevice; 1095 info->lo_offset = info64->lo_offset; 1096 info->lo_encrypt_type = info64->lo_encrypt_type; 1097 info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 1098 info->lo_flags = info64->lo_flags; 1099 info->lo_init[0] = info64->lo_init[0]; 1100 info->lo_init[1] = info64->lo_init[1]; 1101 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1102 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1103 else 1104 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 1105 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1106 1107 /* error in case values were truncated */ 1108 if (info->lo_device != info64->lo_device || 1109 info->lo_rdevice != info64->lo_rdevice || 1110 info->lo_inode != info64->lo_inode || 1111 info->lo_offset != info64->lo_offset) 1112 return -EOVERFLOW; 1113 1114 return 0; 1115 } 1116 1117 static int 1118 loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 1119 { 1120 struct loop_info info; 1121 struct loop_info64 info64; 1122 1123 if (copy_from_user(&info, arg, sizeof (struct loop_info))) 1124 return -EFAULT; 1125 loop_info64_from_old(&info, &info64); 1126 return loop_set_status(lo, &info64); 1127 } 1128 1129 static int 1130 loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 1131 { 1132 struct loop_info64 info64; 1133 1134 if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 1135 return -EFAULT; 1136 return loop_set_status(lo, &info64); 1137 } 1138 1139 static int 1140 loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 1141 struct loop_info info; 1142 struct loop_info64 info64; 1143 int err = 0; 1144 1145 if (!arg) 1146 err = -EINVAL; 1147 if (!err) 1148 err = loop_get_status(lo, &info64); 1149 if (!err) 1150 err = loop_info64_to_old(&info64, &info); 1151 if (!err && copy_to_user(arg, &info, sizeof(info))) 1152 err = -EFAULT; 1153 1154 return err; 1155 } 1156 1157 static int 1158 loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 1159 struct loop_info64 info64; 1160 int err = 0; 1161 1162 if (!arg) 1163 err = -EINVAL; 1164 if (!err) 1165 err = loop_get_status(lo, &info64); 1166 if (!err && copy_to_user(arg, &info64, sizeof(info64))) 1167 err = -EFAULT; 1168 1169 return err; 1170 } 1171 1172 static int loop_set_capacity(struct loop_device *lo, struct block_device *bdev) 1173 { 1174 int err; 1175 sector_t sec; 1176 loff_t sz; 1177 1178 err = -ENXIO; 1179 if (unlikely(lo->lo_state != Lo_bound)) 1180 goto out; 1181 err = figure_loop_size(lo); 1182 if (unlikely(err)) 1183 goto out; 1184 sec = get_capacity(lo->lo_disk); 1185 /* the width of sector_t may be narrow for bit-shift */ 1186 sz = sec; 1187 sz <<= 9; 1188 mutex_lock(&bdev->bd_mutex); 1189 bd_set_size(bdev, sz); 1190 mutex_unlock(&bdev->bd_mutex); 1191 1192 out: 1193 return err; 1194 } 1195 1196 static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1197 unsigned int cmd, unsigned long arg) 1198 { 1199 struct loop_device *lo = bdev->bd_disk->private_data; 1200 int err; 1201 1202 mutex_lock_nested(&lo->lo_ctl_mutex, 1); 1203 switch (cmd) { 1204 case LOOP_SET_FD: 1205 err = loop_set_fd(lo, mode, bdev, arg); 1206 break; 1207 case LOOP_CHANGE_FD: 1208 err = loop_change_fd(lo, bdev, arg); 1209 break; 1210 case LOOP_CLR_FD: 1211 /* loop_clr_fd would have unlocked lo_ctl_mutex on success */ 1212 err = loop_clr_fd(lo, bdev); 1213 if (!err) 1214 goto out_unlocked; 1215 break; 1216 case LOOP_SET_STATUS: 1217 err = loop_set_status_old(lo, (struct loop_info __user *) arg); 1218 break; 1219 case LOOP_GET_STATUS: 1220 err = loop_get_status_old(lo, (struct loop_info __user *) arg); 1221 break; 1222 case LOOP_SET_STATUS64: 1223 err = loop_set_status64(lo, (struct loop_info64 __user *) arg); 1224 break; 1225 case LOOP_GET_STATUS64: 1226 err = loop_get_status64(lo, (struct loop_info64 __user *) arg); 1227 break; 1228 case LOOP_SET_CAPACITY: 1229 err = -EPERM; 1230 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) 1231 err = loop_set_capacity(lo, bdev); 1232 break; 1233 default: 1234 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 1235 } 1236 mutex_unlock(&lo->lo_ctl_mutex); 1237 1238 out_unlocked: 1239 return err; 1240 } 1241 1242 #ifdef CONFIG_COMPAT 1243 struct compat_loop_info { 1244 compat_int_t lo_number; /* ioctl r/o */ 1245 compat_dev_t lo_device; /* ioctl r/o */ 1246 compat_ulong_t lo_inode; /* ioctl r/o */ 1247 compat_dev_t lo_rdevice; /* ioctl r/o */ 1248 compat_int_t lo_offset; 1249 compat_int_t lo_encrypt_type; 1250 compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1251 compat_int_t lo_flags; /* ioctl r/o */ 1252 char lo_name[LO_NAME_SIZE]; 1253 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1254 compat_ulong_t lo_init[2]; 1255 char reserved[4]; 1256 }; 1257 1258 /* 1259 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1260 * - noinlined to reduce stack space usage in main part of driver 1261 */ 1262 static noinline int 1263 loop_info64_from_compat(const struct compat_loop_info __user *arg, 1264 struct loop_info64 *info64) 1265 { 1266 struct compat_loop_info info; 1267 1268 if (copy_from_user(&info, arg, sizeof(info))) 1269 return -EFAULT; 1270 1271 memset(info64, 0, sizeof(*info64)); 1272 info64->lo_number = info.lo_number; 1273 info64->lo_device = info.lo_device; 1274 info64->lo_inode = info.lo_inode; 1275 info64->lo_rdevice = info.lo_rdevice; 1276 info64->lo_offset = info.lo_offset; 1277 info64->lo_sizelimit = 0; 1278 info64->lo_encrypt_type = info.lo_encrypt_type; 1279 info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1280 info64->lo_flags = info.lo_flags; 1281 info64->lo_init[0] = info.lo_init[0]; 1282 info64->lo_init[1] = info.lo_init[1]; 1283 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1284 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1285 else 1286 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1287 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1288 return 0; 1289 } 1290 1291 /* 1292 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1293 * - noinlined to reduce stack space usage in main part of driver 1294 */ 1295 static noinline int 1296 loop_info64_to_compat(const struct loop_info64 *info64, 1297 struct compat_loop_info __user *arg) 1298 { 1299 struct compat_loop_info info; 1300 1301 memset(&info, 0, sizeof(info)); 1302 info.lo_number = info64->lo_number; 1303 info.lo_device = info64->lo_device; 1304 info.lo_inode = info64->lo_inode; 1305 info.lo_rdevice = info64->lo_rdevice; 1306 info.lo_offset = info64->lo_offset; 1307 info.lo_encrypt_type = info64->lo_encrypt_type; 1308 info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1309 info.lo_flags = info64->lo_flags; 1310 info.lo_init[0] = info64->lo_init[0]; 1311 info.lo_init[1] = info64->lo_init[1]; 1312 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1313 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1314 else 1315 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1316 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1317 1318 /* error in case values were truncated */ 1319 if (info.lo_device != info64->lo_device || 1320 info.lo_rdevice != info64->lo_rdevice || 1321 info.lo_inode != info64->lo_inode || 1322 info.lo_offset != info64->lo_offset || 1323 info.lo_init[0] != info64->lo_init[0] || 1324 info.lo_init[1] != info64->lo_init[1]) 1325 return -EOVERFLOW; 1326 1327 if (copy_to_user(arg, &info, sizeof(info))) 1328 return -EFAULT; 1329 return 0; 1330 } 1331 1332 static int 1333 loop_set_status_compat(struct loop_device *lo, 1334 const struct compat_loop_info __user *arg) 1335 { 1336 struct loop_info64 info64; 1337 int ret; 1338 1339 ret = loop_info64_from_compat(arg, &info64); 1340 if (ret < 0) 1341 return ret; 1342 return loop_set_status(lo, &info64); 1343 } 1344 1345 static int 1346 loop_get_status_compat(struct loop_device *lo, 1347 struct compat_loop_info __user *arg) 1348 { 1349 struct loop_info64 info64; 1350 int err = 0; 1351 1352 if (!arg) 1353 err = -EINVAL; 1354 if (!err) 1355 err = loop_get_status(lo, &info64); 1356 if (!err) 1357 err = loop_info64_to_compat(&info64, arg); 1358 return err; 1359 } 1360 1361 static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1362 unsigned int cmd, unsigned long arg) 1363 { 1364 struct loop_device *lo = bdev->bd_disk->private_data; 1365 int err; 1366 1367 switch(cmd) { 1368 case LOOP_SET_STATUS: 1369 mutex_lock(&lo->lo_ctl_mutex); 1370 err = loop_set_status_compat( 1371 lo, (const struct compat_loop_info __user *) arg); 1372 mutex_unlock(&lo->lo_ctl_mutex); 1373 break; 1374 case LOOP_GET_STATUS: 1375 mutex_lock(&lo->lo_ctl_mutex); 1376 err = loop_get_status_compat( 1377 lo, (struct compat_loop_info __user *) arg); 1378 mutex_unlock(&lo->lo_ctl_mutex); 1379 break; 1380 case LOOP_SET_CAPACITY: 1381 case LOOP_CLR_FD: 1382 case LOOP_GET_STATUS64: 1383 case LOOP_SET_STATUS64: 1384 arg = (unsigned long) compat_ptr(arg); 1385 case LOOP_SET_FD: 1386 case LOOP_CHANGE_FD: 1387 err = lo_ioctl(bdev, mode, cmd, arg); 1388 break; 1389 default: 1390 err = -ENOIOCTLCMD; 1391 break; 1392 } 1393 return err; 1394 } 1395 #endif 1396 1397 static int lo_open(struct block_device *bdev, fmode_t mode) 1398 { 1399 struct loop_device *lo = bdev->bd_disk->private_data; 1400 1401 mutex_lock(&lo->lo_ctl_mutex); 1402 lo->lo_refcnt++; 1403 mutex_unlock(&lo->lo_ctl_mutex); 1404 1405 return 0; 1406 } 1407 1408 static int lo_release(struct gendisk *disk, fmode_t mode) 1409 { 1410 struct loop_device *lo = disk->private_data; 1411 int err; 1412 1413 mutex_lock(&lo->lo_ctl_mutex); 1414 1415 if (--lo->lo_refcnt) 1416 goto out; 1417 1418 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1419 /* 1420 * In autoclear mode, stop the loop thread 1421 * and remove configuration after last close. 1422 */ 1423 err = loop_clr_fd(lo, NULL); 1424 if (!err) 1425 goto out_unlocked; 1426 } else { 1427 /* 1428 * Otherwise keep thread (if running) and config, 1429 * but flush possible ongoing bios in thread. 1430 */ 1431 loop_flush(lo); 1432 } 1433 1434 out: 1435 mutex_unlock(&lo->lo_ctl_mutex); 1436 out_unlocked: 1437 return 0; 1438 } 1439 1440 static const struct block_device_operations lo_fops = { 1441 .owner = THIS_MODULE, 1442 .open = lo_open, 1443 .release = lo_release, 1444 .ioctl = lo_ioctl, 1445 #ifdef CONFIG_COMPAT 1446 .compat_ioctl = lo_compat_ioctl, 1447 #endif 1448 }; 1449 1450 /* 1451 * And now the modules code and kernel interface. 1452 */ 1453 static int max_loop; 1454 module_param(max_loop, int, 0); 1455 MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 1456 module_param(max_part, int, 0); 1457 MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 1458 MODULE_LICENSE("GPL"); 1459 MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 1460 1461 int loop_register_transfer(struct loop_func_table *funcs) 1462 { 1463 unsigned int n = funcs->number; 1464 1465 if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 1466 return -EINVAL; 1467 xfer_funcs[n] = funcs; 1468 return 0; 1469 } 1470 1471 int loop_unregister_transfer(int number) 1472 { 1473 unsigned int n = number; 1474 struct loop_device *lo; 1475 struct loop_func_table *xfer; 1476 1477 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 1478 return -EINVAL; 1479 1480 xfer_funcs[n] = NULL; 1481 1482 list_for_each_entry(lo, &loop_devices, lo_list) { 1483 mutex_lock(&lo->lo_ctl_mutex); 1484 1485 if (lo->lo_encryption == xfer) 1486 loop_release_xfer(lo); 1487 1488 mutex_unlock(&lo->lo_ctl_mutex); 1489 } 1490 1491 return 0; 1492 } 1493 1494 EXPORT_SYMBOL(loop_register_transfer); 1495 EXPORT_SYMBOL(loop_unregister_transfer); 1496 1497 static struct loop_device *loop_alloc(int i) 1498 { 1499 struct loop_device *lo; 1500 struct gendisk *disk; 1501 1502 lo = kzalloc(sizeof(*lo), GFP_KERNEL); 1503 if (!lo) 1504 goto out; 1505 1506 lo->lo_queue = blk_alloc_queue(GFP_KERNEL); 1507 if (!lo->lo_queue) 1508 goto out_free_dev; 1509 1510 disk = lo->lo_disk = alloc_disk(1 << part_shift); 1511 if (!disk) 1512 goto out_free_queue; 1513 1514 mutex_init(&lo->lo_ctl_mutex); 1515 lo->lo_number = i; 1516 lo->lo_thread = NULL; 1517 init_waitqueue_head(&lo->lo_event); 1518 spin_lock_init(&lo->lo_lock); 1519 disk->major = LOOP_MAJOR; 1520 disk->first_minor = i << part_shift; 1521 disk->fops = &lo_fops; 1522 disk->private_data = lo; 1523 disk->queue = lo->lo_queue; 1524 sprintf(disk->disk_name, "loop%d", i); 1525 return lo; 1526 1527 out_free_queue: 1528 blk_cleanup_queue(lo->lo_queue); 1529 out_free_dev: 1530 kfree(lo); 1531 out: 1532 return NULL; 1533 } 1534 1535 static void loop_free(struct loop_device *lo) 1536 { 1537 blk_cleanup_queue(lo->lo_queue); 1538 put_disk(lo->lo_disk); 1539 list_del(&lo->lo_list); 1540 kfree(lo); 1541 } 1542 1543 static struct loop_device *loop_init_one(int i) 1544 { 1545 struct loop_device *lo; 1546 1547 list_for_each_entry(lo, &loop_devices, lo_list) { 1548 if (lo->lo_number == i) 1549 return lo; 1550 } 1551 1552 lo = loop_alloc(i); 1553 if (lo) { 1554 add_disk(lo->lo_disk); 1555 list_add_tail(&lo->lo_list, &loop_devices); 1556 } 1557 return lo; 1558 } 1559 1560 static void loop_del_one(struct loop_device *lo) 1561 { 1562 del_gendisk(lo->lo_disk); 1563 loop_free(lo); 1564 } 1565 1566 static struct kobject *loop_probe(dev_t dev, int *part, void *data) 1567 { 1568 struct loop_device *lo; 1569 struct kobject *kobj; 1570 1571 mutex_lock(&loop_devices_mutex); 1572 lo = loop_init_one(dev & MINORMASK); 1573 kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM); 1574 mutex_unlock(&loop_devices_mutex); 1575 1576 *part = 0; 1577 return kobj; 1578 } 1579 1580 static int __init loop_init(void) 1581 { 1582 int i, nr; 1583 unsigned long range; 1584 struct loop_device *lo, *next; 1585 1586 /* 1587 * loop module now has a feature to instantiate underlying device 1588 * structure on-demand, provided that there is an access dev node. 1589 * However, this will not work well with user space tool that doesn't 1590 * know about such "feature". In order to not break any existing 1591 * tool, we do the following: 1592 * 1593 * (1) if max_loop is specified, create that many upfront, and this 1594 * also becomes a hard limit. 1595 * (2) if max_loop is not specified, create 8 loop device on module 1596 * load, user can further extend loop device by create dev node 1597 * themselves and have kernel automatically instantiate actual 1598 * device on-demand. 1599 */ 1600 1601 part_shift = 0; 1602 if (max_part > 0) 1603 part_shift = fls(max_part); 1604 1605 if (max_loop > 1UL << (MINORBITS - part_shift)) 1606 return -EINVAL; 1607 1608 if (max_loop) { 1609 nr = max_loop; 1610 range = max_loop; 1611 } else { 1612 nr = 8; 1613 range = 1UL << (MINORBITS - part_shift); 1614 } 1615 1616 if (register_blkdev(LOOP_MAJOR, "loop")) 1617 return -EIO; 1618 1619 for (i = 0; i < nr; i++) { 1620 lo = loop_alloc(i); 1621 if (!lo) 1622 goto Enomem; 1623 list_add_tail(&lo->lo_list, &loop_devices); 1624 } 1625 1626 /* point of no return */ 1627 1628 list_for_each_entry(lo, &loop_devices, lo_list) 1629 add_disk(lo->lo_disk); 1630 1631 blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 1632 THIS_MODULE, loop_probe, NULL, NULL); 1633 1634 printk(KERN_INFO "loop: module loaded\n"); 1635 return 0; 1636 1637 Enomem: 1638 printk(KERN_INFO "loop: out of memory\n"); 1639 1640 list_for_each_entry_safe(lo, next, &loop_devices, lo_list) 1641 loop_free(lo); 1642 1643 unregister_blkdev(LOOP_MAJOR, "loop"); 1644 return -ENOMEM; 1645 } 1646 1647 static void __exit loop_exit(void) 1648 { 1649 unsigned long range; 1650 struct loop_device *lo, *next; 1651 1652 range = max_loop ? max_loop : 1UL << (MINORBITS - part_shift); 1653 1654 list_for_each_entry_safe(lo, next, &loop_devices, lo_list) 1655 loop_del_one(lo); 1656 1657 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 1658 unregister_blkdev(LOOP_MAJOR, "loop"); 1659 } 1660 1661 module_init(loop_init); 1662 module_exit(loop_exit); 1663 1664 #ifndef MODULE 1665 static int __init max_loop_setup(char *str) 1666 { 1667 max_loop = simple_strtol(str, NULL, 0); 1668 return 1; 1669 } 1670 1671 __setup("max_loop=", max_loop_setup); 1672 #endif 1673