1 2 3#include <linux/config.h> 4#include <linux/module.h> 5 6#include <linux/sched.h> 7#include <linux/fs.h> 8#include <linux/file.h> 9#include <linux/stat.h> 10#include <linux/errno.h> 11#include <linux/major.h> 12#include <linux/wait.h> 13#include <linux/blk.h> 14#include <linux/blkpg.h> 15#include <linux/init.h> 16#include <linux/devfs_fs_kernel.h> 17#include <linux/smp_lock.h> 18#include <linux/swap.h> 19#include <linux/slab.h> 20 21#include <asm/uaccess.h> 22 23#include <linux/loop.h> 24 25#define MAJOR_NR LOOP_MAJOR 26 27static int max_loop = 8; 28static struct loop_device *loop_dev; 29static int *loop_sizes; 30static int *loop_blksizes; 31static devfs_handle_t devfs_handle; /* For the directory */ 32 33/* 34 * Transfer functions 35 */ 36static int transfer_none(struct loop_device *lo, int cmd, char *raw_buf, 37 char *loop_buf, int size, int real_block) 38{ 39 if (raw_buf != loop_buf) { 40 if (cmd == READ) 41 memcpy(loop_buf, raw_buf, size); 42 else 43 memcpy(raw_buf, loop_buf, size); 44 } 45 46 return 0; 47} 48 49static int transfer_xor(struct loop_device *lo, int cmd, char *raw_buf, 50 char *loop_buf, int size, int real_block) 51{ 52 char *in, *out, *key; 53 int i, keysize; 54 55 if (cmd == READ) { 56 in = raw_buf; 57 out = loop_buf; 58 } else { 59 in = loop_buf; 60 out = raw_buf; 61 } 62 63 key = lo->lo_encrypt_key; 64 keysize = lo->lo_encrypt_key_size; 65 for (i = 0; i < size; i++) 66 *out++ = *in++ ^ key[(i & 511) % keysize]; 67 return 0; 68} 69 70static int none_status(struct loop_device *lo, struct loop_info *info) 71{ 72 lo->lo_flags |= LO_FLAGS_BH_REMAP; 73 return 0; 74} 75 76static int xor_status(struct loop_device *lo, struct loop_info *info) 77{ 78 if (info->lo_encrypt_key_size <= 0) 79 return -EINVAL; 80 return 0; 81} 82 83struct loop_func_table none_funcs = { 84 number: LO_CRYPT_NONE, 85 transfer: transfer_none, 86 init: none_status, 87}; 88 89struct loop_func_table xor_funcs = { 90 number: LO_CRYPT_XOR, 91 transfer: transfer_xor, 92 init: xor_status 93}; 94 95/* xfer_funcs[0] is special - its release function is never called */ 96struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 97 &none_funcs, 98 &xor_funcs 99}; 100 101#define MAX_DISK_SIZE 1024*1024*1024 102 103static int compute_loop_size(struct loop_device *lo, struct dentry * lo_dentry, kdev_t lodev) 104{ 105 if (S_ISREG(lo_dentry->d_inode->i_mode)) 106 return (lo_dentry->d_inode->i_size - lo->lo_offset) >> BLOCK_SIZE_BITS; 107 if (blk_size[MAJOR(lodev)]) 108 return blk_size[MAJOR(lodev)][MINOR(lodev)] - 109 (lo->lo_offset >> BLOCK_SIZE_BITS); 110 return MAX_DISK_SIZE; 111} 112 113static void figure_loop_size(struct loop_device *lo) 114{ 115 loop_sizes[lo->lo_number] = compute_loop_size(lo, 116 lo->lo_backing_file->f_dentry, 117 lo->lo_device); 118} 119 120static int lo_send(struct loop_device *lo, struct buffer_head *bh, int bsize, 121 loff_t pos) 122{ 123 struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */ 124 struct address_space *mapping = file->f_dentry->d_inode->i_mapping; 125 struct address_space_operations *aops = mapping->a_ops; 126 struct page *page; 127 char *kaddr, *data; 128 unsigned long index; 129 unsigned size, offset; 130 int len; 131 132 down(&mapping->host->i_sem); 133 index = pos >> PAGE_CACHE_SHIFT; 134 offset = pos & (PAGE_CACHE_SIZE - 1); 135 len = bh->b_size; 136 data = bh->b_data; 137 while (len > 0) { 138 int IV = index * (PAGE_CACHE_SIZE/bsize) + offset/bsize; 139 int transfer_result; 140 141 size = PAGE_CACHE_SIZE - offset; 142 if (size > len) 143 size = len; 144 145 page = grab_cache_page(mapping, index); 146 if (!page) 147 goto fail; 148 if (aops->prepare_write(file, page, offset, offset+size)) 149 goto unlock; 150 kaddr = page_address(page); 151 flush_dcache_page(page); 152 transfer_result = lo_do_transfer(lo, WRITE, kaddr + offset, data, size, IV); 153 if (transfer_result) { 154 /* 155 * The transfer failed, but we still write the data to 156 * keep prepare/commit calls balanced. 157 */ 158 printk(KERN_ERR "loop: transfer error block %ld\n", index); 159 memset(kaddr + offset, 0, size); 160 } 161 if (aops->commit_write(file, page, offset, offset+size)) 162 goto unlock; 163 if (transfer_result) 164 goto unlock; 165 data += size; 166 len -= size; 167 offset = 0; 168 index++; 169 pos += size; 170 UnlockPage(page); 171 page_cache_release(page); 172 } 173 up(&mapping->host->i_sem); 174 return 0; 175 176unlock: 177 UnlockPage(page); 178 page_cache_release(page); 179fail: 180 up(&mapping->host->i_sem); 181 return -1; 182} 183 184struct lo_read_data { 185 struct loop_device *lo; 186 char *data; 187 int bsize; 188}; 189 190static int lo_read_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size) 191{ 192 char *kaddr; 193 unsigned long count = desc->count; 194 struct lo_read_data *p = (struct lo_read_data*)desc->buf; 195 struct loop_device *lo = p->lo; 196 int IV = page->index * (PAGE_CACHE_SIZE/p->bsize) + offset/p->bsize; 197 198 if (size > count) 199 size = count; 200 201 kaddr = kmap(page); 202 if (lo_do_transfer(lo, READ, kaddr + offset, p->data, size, IV)) { 203 size = 0; 204 printk(KERN_ERR "loop: transfer error block %ld\n",page->index); 205 desc->error = -EINVAL; 206 } 207 kunmap(page); 208 209 desc->count = count - size; 210 desc->written += size; 211 p->data += size; 212 return size; 213} 214 215static int lo_receive(struct loop_device *lo, struct buffer_head *bh, int bsize, 216 loff_t pos) 217{ 218 struct lo_read_data cookie; 219 read_descriptor_t desc; 220 struct file *file; 221 222 cookie.lo = lo; 223 cookie.data = bh->b_data; 224 cookie.bsize = bsize; 225 desc.written = 0; 226 desc.count = bh->b_size; 227 desc.buf = (char*)&cookie; 228 desc.error = 0; 229 spin_lock_irq(&lo->lo_lock); 230 file = lo->lo_backing_file; 231 spin_unlock_irq(&lo->lo_lock); 232 do_generic_file_read(file, &pos, &desc, lo_read_actor); 233 return desc.error; 234} 235 236static inline int loop_get_bs(struct loop_device *lo) 237{ 238 int bs = 0; 239 240 if (blksize_size[MAJOR(lo->lo_device)]) 241 bs = blksize_size[MAJOR(lo->lo_device)][MINOR(lo->lo_device)]; 242 if (!bs) 243 bs = BLOCK_SIZE; 244 245 return bs; 246} 247 248static inline unsigned long loop_get_iv(struct loop_device *lo, 249 unsigned long sector) 250{ 251 int bs = loop_get_bs(lo); 252 unsigned long offset, IV; 253 254 IV = sector / (bs >> 9) + lo->lo_offset / bs; 255 offset = ((sector % (bs >> 9)) << 9) + lo->lo_offset % bs; 256 if (offset >= bs) 257 IV++; 258 259 return IV; 260} 261 262static int do_bh_filebacked(struct loop_device *lo, struct buffer_head *bh, int rw) 263{ 264 loff_t pos; 265 int ret; 266 267 pos = ((loff_t) bh->b_rsector << 9) + lo->lo_offset; 268 269 if (rw == WRITE) 270 ret = lo_send(lo, bh, loop_get_bs(lo), pos); 271 else 272 ret = lo_receive(lo, bh, loop_get_bs(lo), pos); 273 274 return ret; 275} 276 277static void loop_end_io_transfer(struct buffer_head *bh, int uptodate); 278static void loop_put_buffer(struct buffer_head *bh) 279{ 280 /* 281 * check b_end_io, may just be a remapped bh and not an allocated one 282 */ 283 if (bh && bh->b_end_io == loop_end_io_transfer) { 284 __free_page(bh->b_page); 285 kmem_cache_free(bh_cachep, bh); 286 } 287} 288 289/* 290 * Add buffer_head to back of pending list 291 */ 292static void loop_add_bh(struct loop_device *lo, struct buffer_head *bh) 293{ 294 unsigned long flags; 295 296 spin_lock_irqsave(&lo->lo_lock, flags); 297 if (lo->lo_bhtail) { 298 lo->lo_bhtail->b_reqnext = bh; 299 lo->lo_bhtail = bh; 300 } else 301 lo->lo_bh = lo->lo_bhtail = bh; 302 spin_unlock_irqrestore(&lo->lo_lock, flags); 303 304 up(&lo->lo_bh_mutex); 305} 306 307/* 308 * Grab first pending buffer 309 */ 310static struct buffer_head *loop_get_bh(struct loop_device *lo) 311{ 312 struct buffer_head *bh; 313 314 spin_lock_irq(&lo->lo_lock); 315 if ((bh = lo->lo_bh)) { 316 if (bh == lo->lo_bhtail) 317 lo->lo_bhtail = NULL; 318 lo->lo_bh = bh->b_reqnext; 319 bh->b_reqnext = NULL; 320 } 321 spin_unlock_irq(&lo->lo_lock); 322 323 return bh; 324} 325 326/* 327 * when buffer i/o has completed. if BH_Dirty is set, this was a WRITE 328 * and lo->transfer stuff has already been done. if not, it was a READ 329 * so queue it for the loop thread and let it do the transfer out of 330 * b_end_io context (we don't want to do decrypt of a page with irqs 331 * disabled) 332 */ 333static void loop_end_io_transfer(struct buffer_head *bh, int uptodate) 334{ 335 struct loop_device *lo = &loop_dev[MINOR(bh->b_dev)]; 336 337 if (!uptodate || test_bit(BH_Dirty, &bh->b_state)) { 338 struct buffer_head *rbh = bh->b_private; 339 340 rbh->b_end_io(rbh, uptodate); 341 if (atomic_dec_and_test(&lo->lo_pending)) 342 up(&lo->lo_bh_mutex); 343 loop_put_buffer(bh); 344 } else 345 loop_add_bh(lo, bh); 346} 347 348static struct buffer_head *loop_get_buffer(struct loop_device *lo, 349 struct buffer_head *rbh) 350{ 351 struct buffer_head *bh; 352 353 /* 354 * for xfer_funcs that can operate on the same bh, do that 355 */ 356 if (lo->lo_flags & LO_FLAGS_BH_REMAP) { 357 bh = rbh; 358 goto out_bh; 359 } 360 361 do { 362 bh = kmem_cache_alloc(bh_cachep, SLAB_NOIO); 363 if (bh) 364 break; 365 366 run_task_queue(&tq_disk); 367 schedule_timeout(HZ); 368 } while (1); 369 memset(bh, 0, sizeof(*bh)); 370 371 bh->b_size = rbh->b_size; 372 bh->b_dev = rbh->b_rdev; 373 bh->b_state = (1 << BH_Req) | (1 << BH_Mapped) | (1 << BH_Lock); 374 375 /* 376 * easy way out, although it does waste some memory for < PAGE_SIZE 377 * blocks... if highmem bounce buffering can get away with it, 378 * so can we :-) 379 */ 380 do { 381 bh->b_page = alloc_page(GFP_NOIO); 382 if (bh->b_page) 383 break; 384 385 run_task_queue(&tq_disk); 386 schedule_timeout(HZ); 387 } while (1); 388 389 bh->b_data = page_address(bh->b_page); 390 bh->b_end_io = loop_end_io_transfer; 391 bh->b_private = rbh; 392 init_waitqueue_head(&bh->b_wait); 393 394out_bh: 395 bh->b_rsector = rbh->b_rsector + (lo->lo_offset >> 9); 396 spin_lock_irq(&lo->lo_lock); 397 bh->b_rdev = lo->lo_device; 398 spin_unlock_irq(&lo->lo_lock); 399 400 return bh; 401} 402 403static int loop_make_request(request_queue_t *q, int rw, struct buffer_head *rbh) 404{ 405 struct buffer_head *bh = NULL; 406 struct loop_device *lo; 407 unsigned long IV; 408 409 if (!buffer_locked(rbh)) 410 BUG(); 411 412 if (MINOR(rbh->b_rdev) >= max_loop) 413 goto out; 414 415 lo = &loop_dev[MINOR(rbh->b_rdev)]; 416 spin_lock_irq(&lo->lo_lock); 417 if (lo->lo_state != Lo_bound) 418 goto inactive; 419 atomic_inc(&lo->lo_pending); 420 spin_unlock_irq(&lo->lo_lock); 421 422 if (rw == WRITE) { 423 if (lo->lo_flags & LO_FLAGS_READ_ONLY) 424 goto err; 425 } else if (rw == READA) { 426 rw = READ; 427 } else if (rw != READ) { 428 printk(KERN_ERR "loop: unknown command (%d)\n", rw); 429 goto err; 430 } 431 432 rbh = blk_queue_bounce(q, rw, rbh); 433 434 /* 435 * file backed, queue for loop_thread to handle 436 */ 437 if (lo->lo_flags & LO_FLAGS_DO_BMAP) { 438 /* 439 * rbh locked at this point, noone else should clear 440 * the dirty flag 441 */ 442 if (rw == WRITE) 443 set_bit(BH_Dirty, &rbh->b_state); 444 loop_add_bh(lo, rbh); 445 return 0; 446 } 447 448 /* 449 * piggy old buffer on original, and submit for I/O 450 */ 451 bh = loop_get_buffer(lo, rbh); 452 IV = loop_get_iv(lo, rbh->b_rsector); 453 if (rw == WRITE) { 454 set_bit(BH_Dirty, &bh->b_state); 455 if (lo_do_transfer(lo, WRITE, bh->b_data, rbh->b_data, 456 bh->b_size, IV)) 457 goto err; 458 } 459 460 generic_make_request(rw, bh); 461 return 0; 462 463err: 464 if (atomic_dec_and_test(&lo->lo_pending)) 465 up(&lo->lo_bh_mutex); 466 loop_put_buffer(bh); 467out: 468 buffer_IO_error(rbh); 469 return 0; 470inactive: 471 spin_unlock_irq(&lo->lo_lock); 472 goto out; 473} 474 475static inline void loop_handle_bh(struct loop_device *lo,struct buffer_head *bh) 476{ 477 int ret; 478 479 /* 480 * For block backed loop, we know this is a READ 481 */ 482 if (lo->lo_flags & LO_FLAGS_DO_BMAP) { 483 int rw = !!test_and_clear_bit(BH_Dirty, &bh->b_state); 484 485 ret = do_bh_filebacked(lo, bh, rw); 486 bh->b_end_io(bh, !ret); 487 } else { 488 struct buffer_head *rbh = bh->b_private; 489 unsigned long IV = loop_get_iv(lo, rbh->b_rsector); 490 491 ret = lo_do_transfer(lo, READ, bh->b_data, rbh->b_data, 492 bh->b_size, IV); 493 494 rbh->b_end_io(rbh, !ret); 495 loop_put_buffer(bh); 496 } 497} 498 499/* 500 * worker thread that handles reads/writes to file backed loop devices, 501 * to avoid blocking in our make_request_fn. it also does loop decrypting 502 * on reads for block backed loop, as that is too heavy to do from 503 * b_end_io context where irqs may be disabled. 504 */ 505static int loop_thread(void *data) 506{ 507 struct loop_device *lo = data; 508 struct buffer_head *bh; 509 510 daemonize(); 511 exit_files(current); 512 513 sprintf(current->comm, "loop%d", lo->lo_number); 514 515 spin_lock_irq(¤t->sigmask_lock); 516 sigfillset(¤t->blocked); 517 flush_signals(current); 518 spin_unlock_irq(¤t->sigmask_lock); 519 520 current->policy = SCHED_OTHER; 521 current->nice = -20; 522 523 spin_lock_irq(&lo->lo_lock); 524 lo->lo_state = Lo_bound; 525 atomic_inc(&lo->lo_pending); 526 spin_unlock_irq(&lo->lo_lock); 527 528 current->flags |= PF_NOIO; 529 530 /* 531 * up sem, we are running 532 */ 533 up(&lo->lo_sem); 534 535 for (;;) { 536 down_interruptible(&lo->lo_bh_mutex); 537 /* 538 * could be upped because of tear-down, not because of 539 * pending work 540 */ 541 if (!atomic_read(&lo->lo_pending)) 542 break; 543 544 bh = loop_get_bh(lo); 545 if (!bh) { 546 printk("loop: missing bh\n"); 547 continue; 548 } 549 loop_handle_bh(lo, bh); 550 551 /* 552 * upped both for pending work and tear-down, lo_pending 553 * will hit zero then 554 */ 555 if (atomic_dec_and_test(&lo->lo_pending)) 556 break; 557 } 558 559 up(&lo->lo_sem); 560 return 0; 561} 562 563static int loop_set_fd(struct loop_device *lo, struct file *lo_file, kdev_t dev, 564 unsigned int arg) 565{ 566 struct file *file; 567 struct inode *inode; 568 kdev_t lo_device; 569 int lo_flags = 0; 570 int error; 571 int bs; 572 573 MOD_INC_USE_COUNT; 574 575 error = -EBUSY; 576 if (lo->lo_state != Lo_unbound) 577 goto out; 578 579 error = -EBADF; 580 file = fget(arg); 581 if (!file) 582 goto out; 583 584 error = -EINVAL; 585 inode = file->f_dentry->d_inode; 586 587 if (!(file->f_mode & FMODE_WRITE)) 588 lo_flags |= LO_FLAGS_READ_ONLY; 589 590 if (S_ISBLK(inode->i_mode)) { 591 lo_device = inode->i_rdev; 592 if (lo_device == dev) { 593 error = -EBUSY; 594 goto out; 595 } 596 } else if (S_ISREG(inode->i_mode)) { 597 struct address_space_operations *aops = inode->i_mapping->a_ops; 598 /* 599 * If we can't read - sorry. If we only can't write - well, 600 * it's going to be read-only. 601 */ 602 if (!aops->readpage) 603 goto out_putf; 604 605 if (!aops->prepare_write || !aops->commit_write) 606 lo_flags |= LO_FLAGS_READ_ONLY; 607 608 lo_device = inode->i_dev; 609 lo_flags |= LO_FLAGS_DO_BMAP; 610 error = 0; 611 } else 612 goto out_putf; 613 614 get_file(file); 615 616 if (IS_RDONLY (inode) || is_read_only(lo_device) 617 || !(lo_file->f_mode & FMODE_WRITE)) 618 lo_flags |= LO_FLAGS_READ_ONLY; 619 620 set_device_ro(dev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 621 622 lo->lo_device = lo_device; 623 lo->lo_flags = lo_flags; 624 lo->lo_backing_file = file; 625 lo->transfer = NULL; 626 lo->ioctl = NULL; 627 figure_loop_size(lo); 628 lo->old_gfp_mask = inode->i_mapping->gfp_mask; 629 inode->i_mapping->gfp_mask = GFP_NOIO; 630 631 bs = 0; 632 if (blksize_size[MAJOR(lo_device)]) 633 bs = blksize_size[MAJOR(lo_device)][MINOR(lo_device)]; 634 if (!bs) 635 bs = BLOCK_SIZE; 636 637 set_blocksize(dev, bs); 638 639 lo->lo_bh = lo->lo_bhtail = NULL; 640 kernel_thread(loop_thread, lo, CLONE_FS | CLONE_FILES | CLONE_SIGHAND); 641 down(&lo->lo_sem); 642 643 fput(file); 644 return 0; 645 646 out_putf: 647 fput(file); 648 out: 649 MOD_DEC_USE_COUNT; 650 return error; 651} 652 653static int loop_release_xfer(struct loop_device *lo) 654{ 655 int err = 0; 656 if (lo->lo_encrypt_type) { 657 struct loop_func_table *xfer= xfer_funcs[lo->lo_encrypt_type]; 658 if (xfer && xfer->release) 659 err = xfer->release(lo); 660 if (xfer && xfer->unlock) 661 xfer->unlock(lo); 662 lo->lo_encrypt_type = 0; 663 } 664 return err; 665} 666 667static int loop_init_xfer(struct loop_device *lo, int type,struct loop_info *i) 668{ 669 int err = 0; 670 if (type) { 671 struct loop_func_table *xfer = xfer_funcs[type]; 672 if (xfer->init) 673 err = xfer->init(lo, i); 674 if (!err) { 675 lo->lo_encrypt_type = type; 676 if (xfer->lock) 677 xfer->lock(lo); 678 } 679 } 680 return err; 681} 682 683static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev) 684{ 685 struct file *filp = lo->lo_backing_file; 686 int gfp = lo->old_gfp_mask; 687 688 if (lo->lo_state != Lo_bound) 689 return -ENXIO; 690 if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */ 691 return -EBUSY; 692 if (filp==NULL) 693 return -EINVAL; 694 695 spin_lock_irq(&lo->lo_lock); 696 lo->lo_state = Lo_rundown; 697 if (atomic_dec_and_test(&lo->lo_pending)) 698 up(&lo->lo_bh_mutex); 699 spin_unlock_irq(&lo->lo_lock); 700 701 down(&lo->lo_sem); 702 703 lo->lo_backing_file = NULL; 704 705 loop_release_xfer(lo); 706 lo->transfer = NULL; 707 lo->ioctl = NULL; 708 lo->lo_device = 0; 709 lo->lo_encrypt_type = 0; 710 lo->lo_offset = 0; 711 lo->lo_encrypt_key_size = 0; 712 lo->lo_flags = 0; 713 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 714 memset(lo->lo_name, 0, LO_NAME_SIZE); 715 loop_sizes[lo->lo_number] = 0; 716 invalidate_bdev(bdev, 0); 717 filp->f_dentry->d_inode->i_mapping->gfp_mask = gfp; 718 lo->lo_state = Lo_unbound; 719 fput(filp); 720 MOD_DEC_USE_COUNT; 721 return 0; 722} 723 724static int loop_set_status(struct loop_device *lo, struct loop_info *arg) 725{ 726 struct loop_info info; 727 int err; 728 unsigned int type; 729 730 if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid && 731 !capable(CAP_SYS_ADMIN)) 732 return -EPERM; 733 if (lo->lo_state != Lo_bound) 734 return -ENXIO; 735 if (copy_from_user(&info, arg, sizeof (struct loop_info))) 736 return -EFAULT; 737 if ((unsigned int) info.lo_encrypt_key_size > LO_KEY_SIZE) 738 return -EINVAL; 739 type = info.lo_encrypt_type; 740 if (type >= MAX_LO_CRYPT || xfer_funcs[type] == NULL) 741 return -EINVAL; 742 if (type == LO_CRYPT_XOR && info.lo_encrypt_key_size == 0) 743 return -EINVAL; 744 err = loop_release_xfer(lo); 745 if (!err) 746 err = loop_init_xfer(lo, type, &info); 747 if (err) 748 return err; 749 750 lo->lo_offset = info.lo_offset; 751 strncpy(lo->lo_name, info.lo_name, LO_NAME_SIZE); 752 753 lo->transfer = xfer_funcs[type]->transfer; 754 lo->ioctl = xfer_funcs[type]->ioctl; 755 lo->lo_encrypt_key_size = info.lo_encrypt_key_size; 756 lo->lo_init[0] = info.lo_init[0]; 757 lo->lo_init[1] = info.lo_init[1]; 758 if (info.lo_encrypt_key_size) { 759 memcpy(lo->lo_encrypt_key, info.lo_encrypt_key, 760 info.lo_encrypt_key_size); 761 lo->lo_key_owner = current->uid; 762 } 763 figure_loop_size(lo); 764 return 0; 765} 766 767static int loop_get_status(struct loop_device *lo, struct loop_info *arg) 768{ 769 struct loop_info info; 770 struct file *file = lo->lo_backing_file; 771 772 if (lo->lo_state != Lo_bound) 773 return -ENXIO; 774 if (!arg) 775 return -EINVAL; 776 memset(&info, 0, sizeof(info)); 777 info.lo_number = lo->lo_number; 778 info.lo_device = kdev_t_to_nr(file->f_dentry->d_inode->i_dev); 779 info.lo_inode = file->f_dentry->d_inode->i_ino; 780 info.lo_rdevice = kdev_t_to_nr(lo->lo_device); 781 info.lo_offset = lo->lo_offset; 782 info.lo_flags = lo->lo_flags; 783 strncpy(info.lo_name, lo->lo_name, LO_NAME_SIZE); 784 info.lo_encrypt_type = lo->lo_encrypt_type; 785 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 786 info.lo_encrypt_key_size = lo->lo_encrypt_key_size; 787 memcpy(info.lo_encrypt_key, lo->lo_encrypt_key, 788 lo->lo_encrypt_key_size); 789 } 790 return copy_to_user(arg, &info, sizeof(info)) ? -EFAULT : 0; 791} 792 793static int lo_ioctl(struct inode * inode, struct file * file, 794 unsigned int cmd, unsigned long arg) 795{ 796 struct loop_device *lo; 797 int dev, err; 798 799 if (!inode) 800 return -EINVAL; 801 if (MAJOR(inode->i_rdev) != MAJOR_NR) { 802 printk(KERN_WARNING "lo_ioctl: pseudo-major != %d\n", 803 MAJOR_NR); 804 return -ENODEV; 805 } 806 dev = MINOR(inode->i_rdev); 807 if (dev >= max_loop) 808 return -ENODEV; 809 lo = &loop_dev[dev]; 810 down(&lo->lo_ctl_mutex); 811 switch (cmd) { 812 case LOOP_SET_FD: 813 err = loop_set_fd(lo, file, inode->i_rdev, arg); 814 break; 815 case LOOP_CLR_FD: 816 err = loop_clr_fd(lo, inode->i_bdev); 817 break; 818 case LOOP_SET_STATUS: 819 err = loop_set_status(lo, (struct loop_info *) arg); 820 break; 821 case LOOP_GET_STATUS: 822 err = loop_get_status(lo, (struct loop_info *) arg); 823 break; 824 case BLKGETSIZE: 825 if (lo->lo_state != Lo_bound) { 826 err = -ENXIO; 827 break; 828 } 829 err = put_user((unsigned long)loop_sizes[lo->lo_number] << 1, (unsigned long *) arg); 830 break; 831 case BLKGETSIZE64: 832 if (lo->lo_state != Lo_bound) { 833 err = -ENXIO; 834 break; 835 } 836 err = put_user((u64)loop_sizes[lo->lo_number] << 10, (u64*)arg); 837 break; 838 case BLKBSZGET: 839 case BLKBSZSET: 840 err = blk_ioctl(inode->i_rdev, cmd, arg); 841 break; 842 default: 843 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 844 } 845 up(&lo->lo_ctl_mutex); 846 return err; 847} 848 849static int lo_open(struct inode *inode, struct file *file) 850{ 851 struct loop_device *lo; 852 int dev, type; 853 854 if (!inode) 855 return -EINVAL; 856 if (MAJOR(inode->i_rdev) != MAJOR_NR) { 857 printk(KERN_WARNING "lo_open: pseudo-major != %d\n", MAJOR_NR); 858 return -ENODEV; 859 } 860 dev = MINOR(inode->i_rdev); 861 if (dev >= max_loop) 862 return -ENODEV; 863 864 lo = &loop_dev[dev]; 865 MOD_INC_USE_COUNT; 866 down(&lo->lo_ctl_mutex); 867 868 type = lo->lo_encrypt_type; 869 if (type && xfer_funcs[type] && xfer_funcs[type]->lock) 870 xfer_funcs[type]->lock(lo); 871 lo->lo_refcnt++; 872 up(&lo->lo_ctl_mutex); 873 return 0; 874} 875 876static int lo_release(struct inode *inode, struct file *file) 877{ 878 struct loop_device *lo; 879 int dev, type; 880 881 if (!inode) 882 return 0; 883 if (MAJOR(inode->i_rdev) != MAJOR_NR) { 884 printk(KERN_WARNING "lo_release: pseudo-major != %d\n", 885 MAJOR_NR); 886 return 0; 887 } 888 dev = MINOR(inode->i_rdev); 889 if (dev >= max_loop) 890 return 0; 891 892 lo = &loop_dev[dev]; 893 down(&lo->lo_ctl_mutex); 894 type = lo->lo_encrypt_type; 895 --lo->lo_refcnt; 896 if (xfer_funcs[type] && xfer_funcs[type]->unlock) 897 xfer_funcs[type]->unlock(lo); 898 899 up(&lo->lo_ctl_mutex); 900 MOD_DEC_USE_COUNT; 901 return 0; 902} 903 904static struct block_device_operations lo_fops = { 905 owner: THIS_MODULE, 906 open: lo_open, 907 release: lo_release, 908 ioctl: lo_ioctl, 909}; 910 911/* 912 * And now the modules code and kernel interface. 913 */ 914MODULE_PARM(max_loop, "i"); 915MODULE_PARM_DESC(max_loop, "Maximum number of loop devices (1-256)"); 916MODULE_LICENSE("GPL"); 917 918int loop_register_transfer(struct loop_func_table *funcs) 919{ 920 if ((unsigned)funcs->number > MAX_LO_CRYPT || xfer_funcs[funcs->number]) 921 return -EINVAL; 922 xfer_funcs[funcs->number] = funcs; 923 return 0; 924} 925 926int loop_unregister_transfer(int number) 927{ 928 struct loop_device *lo; 929 930 if ((unsigned)number >= MAX_LO_CRYPT) 931 return -EINVAL; 932 for (lo = &loop_dev[0]; lo < &loop_dev[max_loop]; lo++) { 933 int type = lo->lo_encrypt_type; 934 if (type == number) { 935 xfer_funcs[type]->release(lo); 936 lo->transfer = NULL; 937 lo->lo_encrypt_type = 0; 938 } 939 } 940 xfer_funcs[number] = NULL; 941 return 0; 942} 943 944EXPORT_SYMBOL(loop_register_transfer); 945EXPORT_SYMBOL(loop_unregister_transfer); 946 947int __init loop_init(void) 948{ 949 int i; 950 951 if ((max_loop < 1) || (max_loop > 256)) { 952 printk(KERN_WARNING "loop: invalid max_loop (must be between" 953 " 1 and 256), using default (8)\n"); 954 max_loop = 8; 955 } 956 957 if (devfs_register_blkdev(MAJOR_NR, "loop", &lo_fops)) { 958 printk(KERN_WARNING "Unable to get major number %d for loop" 959 " device\n", MAJOR_NR); 960 return -EIO; 961 } 962 963 devfs_handle = devfs_mk_dir(NULL, "loop", NULL); 964 devfs_register_series(devfs_handle, "%u", max_loop, DEVFS_FL_DEFAULT, 965 MAJOR_NR, 0, 966 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP, 967 &lo_fops, NULL); 968 969 loop_dev = kmalloc(max_loop * sizeof(struct loop_device), GFP_KERNEL); 970 if (!loop_dev) 971 return -ENOMEM; 972 973 loop_sizes = kmalloc(max_loop * sizeof(int), GFP_KERNEL); 974 if (!loop_sizes) 975 goto out_sizes; 976 977 loop_blksizes = kmalloc(max_loop * sizeof(int), GFP_KERNEL); 978 if (!loop_blksizes) 979 goto out_blksizes; 980 981 blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), loop_make_request); 982 983 for (i = 0; i < max_loop; i++) { 984 struct loop_device *lo = &loop_dev[i]; 985 memset(lo, 0, sizeof(struct loop_device)); 986 init_MUTEX(&lo->lo_ctl_mutex); 987 init_MUTEX_LOCKED(&lo->lo_sem); 988 init_MUTEX_LOCKED(&lo->lo_bh_mutex); 989 lo->lo_number = i; 990 spin_lock_init(&lo->lo_lock); 991 } 992 993 memset(loop_sizes, 0, max_loop * sizeof(int)); 994 memset(loop_blksizes, 0, max_loop * sizeof(int)); 995 blk_size[MAJOR_NR] = loop_sizes; 996 blksize_size[MAJOR_NR] = loop_blksizes; 997 for (i = 0; i < max_loop; i++) 998 register_disk(NULL, MKDEV(MAJOR_NR, i), 1, &lo_fops, 0); 999 1000 printk(KERN_INFO "loop: loaded (max %d devices)\n", max_loop); 1001 return 0; 1002 1003out_sizes: 1004 kfree(loop_dev); 1005out_blksizes: 1006 kfree(loop_sizes); 1007 printk(KERN_ERR "loop: ran out of memory\n"); 1008 return -ENOMEM; 1009} 1010 1011void loop_exit(void) 1012{ 1013 devfs_unregister(devfs_handle); 1014 if (devfs_unregister_blkdev(MAJOR_NR, "loop")) 1015 printk(KERN_WARNING "loop: cannot unregister blkdev\n"); 1016 1017 kfree(loop_dev); 1018 kfree(loop_sizes); 1019 kfree(loop_blksizes); 1020} 1021 1022module_init(loop_init); 1023module_exit(loop_exit); 1024 1025#ifndef MODULE 1026static int __init max_loop_setup(char *str) 1027{ 1028 max_loop = simple_strtol(str, NULL, 0); 1029 return 1; 1030} 1031 1032__setup("max_loop=", max_loop_setup); 1033#endif 1034