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/compat.h> 67 #include <linux/suspend.h> 68 #include <linux/freezer.h> 69 #include <linux/mutex.h> 70 #include <linux/writeback.h> 71 #include <linux/completion.h> 72 #include <linux/highmem.h> 73 #include <linux/kthread.h> 74 #include <linux/splice.h> 75 #include <linux/sysfs.h> 76 #include <linux/miscdevice.h> 77 #include <linux/falloc.h> 78 #include <linux/uio.h> 79 #include <linux/ioprio.h> 80 #include <linux/blk-cgroup.h> 81 82 #include "loop.h" 83 84 #include <linux/uaccess.h> 85 86 static DEFINE_IDR(loop_index_idr); 87 static DEFINE_MUTEX(loop_ctl_mutex); 88 89 static int max_part; 90 static int part_shift; 91 92 static int transfer_xor(struct loop_device *lo, int cmd, 93 struct page *raw_page, unsigned raw_off, 94 struct page *loop_page, unsigned loop_off, 95 int size, sector_t real_block) 96 { 97 char *raw_buf = kmap_atomic(raw_page) + raw_off; 98 char *loop_buf = kmap_atomic(loop_page) + loop_off; 99 char *in, *out, *key; 100 int i, keysize; 101 102 if (cmd == READ) { 103 in = raw_buf; 104 out = loop_buf; 105 } else { 106 in = loop_buf; 107 out = raw_buf; 108 } 109 110 key = lo->lo_encrypt_key; 111 keysize = lo->lo_encrypt_key_size; 112 for (i = 0; i < size; i++) 113 *out++ = *in++ ^ key[(i & 511) % keysize]; 114 115 kunmap_atomic(loop_buf); 116 kunmap_atomic(raw_buf); 117 cond_resched(); 118 return 0; 119 } 120 121 static int xor_init(struct loop_device *lo, const struct loop_info64 *info) 122 { 123 if (unlikely(info->lo_encrypt_key_size <= 0)) 124 return -EINVAL; 125 return 0; 126 } 127 128 static struct loop_func_table none_funcs = { 129 .number = LO_CRYPT_NONE, 130 }; 131 132 static struct loop_func_table xor_funcs = { 133 .number = LO_CRYPT_XOR, 134 .transfer = transfer_xor, 135 .init = xor_init 136 }; 137 138 /* xfer_funcs[0] is special - its release function is never called */ 139 static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = { 140 &none_funcs, 141 &xor_funcs 142 }; 143 144 static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file) 145 { 146 loff_t loopsize; 147 148 /* Compute loopsize in bytes */ 149 loopsize = i_size_read(file->f_mapping->host); 150 if (offset > 0) 151 loopsize -= offset; 152 /* offset is beyond i_size, weird but possible */ 153 if (loopsize < 0) 154 return 0; 155 156 if (sizelimit > 0 && sizelimit < loopsize) 157 loopsize = sizelimit; 158 /* 159 * Unfortunately, if we want to do I/O on the device, 160 * the number of 512-byte sectors has to fit into a sector_t. 161 */ 162 return loopsize >> 9; 163 } 164 165 static loff_t get_loop_size(struct loop_device *lo, struct file *file) 166 { 167 return get_size(lo->lo_offset, lo->lo_sizelimit, file); 168 } 169 170 static void __loop_update_dio(struct loop_device *lo, bool dio) 171 { 172 struct file *file = lo->lo_backing_file; 173 struct address_space *mapping = file->f_mapping; 174 struct inode *inode = mapping->host; 175 unsigned short sb_bsize = 0; 176 unsigned dio_align = 0; 177 bool use_dio; 178 179 if (inode->i_sb->s_bdev) { 180 sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev); 181 dio_align = sb_bsize - 1; 182 } 183 184 /* 185 * We support direct I/O only if lo_offset is aligned with the 186 * logical I/O size of backing device, and the logical block 187 * size of loop is bigger than the backing device's and the loop 188 * needn't transform transfer. 189 * 190 * TODO: the above condition may be loosed in the future, and 191 * direct I/O may be switched runtime at that time because most 192 * of requests in sane applications should be PAGE_SIZE aligned 193 */ 194 if (dio) { 195 if (queue_logical_block_size(lo->lo_queue) >= sb_bsize && 196 !(lo->lo_offset & dio_align) && 197 mapping->a_ops->direct_IO && 198 !lo->transfer) 199 use_dio = true; 200 else 201 use_dio = false; 202 } else { 203 use_dio = false; 204 } 205 206 if (lo->use_dio == use_dio) 207 return; 208 209 /* flush dirty pages before changing direct IO */ 210 vfs_fsync(file, 0); 211 212 /* 213 * The flag of LO_FLAGS_DIRECT_IO is handled similarly with 214 * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup 215 * will get updated by ioctl(LOOP_GET_STATUS) 216 */ 217 if (lo->lo_state == Lo_bound) 218 blk_mq_freeze_queue(lo->lo_queue); 219 lo->use_dio = use_dio; 220 if (use_dio) { 221 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, lo->lo_queue); 222 lo->lo_flags |= LO_FLAGS_DIRECT_IO; 223 } else { 224 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 225 lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; 226 } 227 if (lo->lo_state == Lo_bound) 228 blk_mq_unfreeze_queue(lo->lo_queue); 229 } 230 231 static int 232 figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) 233 { 234 loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); 235 sector_t x = (sector_t)size; 236 struct block_device *bdev = lo->lo_device; 237 238 if (unlikely((loff_t)x != size)) 239 return -EFBIG; 240 if (lo->lo_offset != offset) 241 lo->lo_offset = offset; 242 if (lo->lo_sizelimit != sizelimit) 243 lo->lo_sizelimit = sizelimit; 244 set_capacity(lo->lo_disk, x); 245 bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); 246 /* let user-space know about the new size */ 247 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 248 return 0; 249 } 250 251 static inline int 252 lo_do_transfer(struct loop_device *lo, int cmd, 253 struct page *rpage, unsigned roffs, 254 struct page *lpage, unsigned loffs, 255 int size, sector_t rblock) 256 { 257 int ret; 258 259 ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock); 260 if (likely(!ret)) 261 return 0; 262 263 printk_ratelimited(KERN_ERR 264 "loop: Transfer error at byte offset %llu, length %i.\n", 265 (unsigned long long)rblock << 9, size); 266 return ret; 267 } 268 269 static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos) 270 { 271 struct iov_iter i; 272 ssize_t bw; 273 274 iov_iter_bvec(&i, WRITE, bvec, 1, bvec->bv_len); 275 276 file_start_write(file); 277 bw = vfs_iter_write(file, &i, ppos, 0); 278 file_end_write(file); 279 280 if (likely(bw == bvec->bv_len)) 281 return 0; 282 283 printk_ratelimited(KERN_ERR 284 "loop: Write error at byte offset %llu, length %i.\n", 285 (unsigned long long)*ppos, bvec->bv_len); 286 if (bw >= 0) 287 bw = -EIO; 288 return bw; 289 } 290 291 static int lo_write_simple(struct loop_device *lo, struct request *rq, 292 loff_t pos) 293 { 294 struct bio_vec bvec; 295 struct req_iterator iter; 296 int ret = 0; 297 298 rq_for_each_segment(bvec, rq, iter) { 299 ret = lo_write_bvec(lo->lo_backing_file, &bvec, &pos); 300 if (ret < 0) 301 break; 302 cond_resched(); 303 } 304 305 return ret; 306 } 307 308 /* 309 * This is the slow, transforming version that needs to double buffer the 310 * data as it cannot do the transformations in place without having direct 311 * access to the destination pages of the backing file. 312 */ 313 static int lo_write_transfer(struct loop_device *lo, struct request *rq, 314 loff_t pos) 315 { 316 struct bio_vec bvec, b; 317 struct req_iterator iter; 318 struct page *page; 319 int ret = 0; 320 321 page = alloc_page(GFP_NOIO); 322 if (unlikely(!page)) 323 return -ENOMEM; 324 325 rq_for_each_segment(bvec, rq, iter) { 326 ret = lo_do_transfer(lo, WRITE, page, 0, bvec.bv_page, 327 bvec.bv_offset, bvec.bv_len, pos >> 9); 328 if (unlikely(ret)) 329 break; 330 331 b.bv_page = page; 332 b.bv_offset = 0; 333 b.bv_len = bvec.bv_len; 334 ret = lo_write_bvec(lo->lo_backing_file, &b, &pos); 335 if (ret < 0) 336 break; 337 } 338 339 __free_page(page); 340 return ret; 341 } 342 343 static int lo_read_simple(struct loop_device *lo, struct request *rq, 344 loff_t pos) 345 { 346 struct bio_vec bvec; 347 struct req_iterator iter; 348 struct iov_iter i; 349 ssize_t len; 350 351 rq_for_each_segment(bvec, rq, iter) { 352 iov_iter_bvec(&i, READ, &bvec, 1, bvec.bv_len); 353 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 354 if (len < 0) 355 return len; 356 357 flush_dcache_page(bvec.bv_page); 358 359 if (len != bvec.bv_len) { 360 struct bio *bio; 361 362 __rq_for_each_bio(bio, rq) 363 zero_fill_bio(bio); 364 break; 365 } 366 cond_resched(); 367 } 368 369 return 0; 370 } 371 372 static int lo_read_transfer(struct loop_device *lo, struct request *rq, 373 loff_t pos) 374 { 375 struct bio_vec bvec, b; 376 struct req_iterator iter; 377 struct iov_iter i; 378 struct page *page; 379 ssize_t len; 380 int ret = 0; 381 382 page = alloc_page(GFP_NOIO); 383 if (unlikely(!page)) 384 return -ENOMEM; 385 386 rq_for_each_segment(bvec, rq, iter) { 387 loff_t offset = pos; 388 389 b.bv_page = page; 390 b.bv_offset = 0; 391 b.bv_len = bvec.bv_len; 392 393 iov_iter_bvec(&i, READ, &b, 1, b.bv_len); 394 len = vfs_iter_read(lo->lo_backing_file, &i, &pos, 0); 395 if (len < 0) { 396 ret = len; 397 goto out_free_page; 398 } 399 400 ret = lo_do_transfer(lo, READ, page, 0, bvec.bv_page, 401 bvec.bv_offset, len, offset >> 9); 402 if (ret) 403 goto out_free_page; 404 405 flush_dcache_page(bvec.bv_page); 406 407 if (len != bvec.bv_len) { 408 struct bio *bio; 409 410 __rq_for_each_bio(bio, rq) 411 zero_fill_bio(bio); 412 break; 413 } 414 } 415 416 ret = 0; 417 out_free_page: 418 __free_page(page); 419 return ret; 420 } 421 422 static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, 423 int mode) 424 { 425 /* 426 * We use fallocate to manipulate the space mappings used by the image 427 * a.k.a. discard/zerorange. However we do not support this if 428 * encryption is enabled, because it may give an attacker useful 429 * information. 430 */ 431 struct file *file = lo->lo_backing_file; 432 int ret; 433 434 mode |= FALLOC_FL_KEEP_SIZE; 435 436 if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { 437 ret = -EOPNOTSUPP; 438 goto out; 439 } 440 441 ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); 442 if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) 443 ret = -EIO; 444 out: 445 return ret; 446 } 447 448 static int lo_req_flush(struct loop_device *lo, struct request *rq) 449 { 450 struct file *file = lo->lo_backing_file; 451 int ret = vfs_fsync(file, 0); 452 if (unlikely(ret && ret != -EINVAL)) 453 ret = -EIO; 454 455 return ret; 456 } 457 458 static void lo_complete_rq(struct request *rq) 459 { 460 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 461 blk_status_t ret = BLK_STS_OK; 462 463 if (!cmd->use_aio || cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) || 464 req_op(rq) != REQ_OP_READ) { 465 if (cmd->ret < 0) 466 ret = BLK_STS_IOERR; 467 goto end_io; 468 } 469 470 /* 471 * Short READ - if we got some data, advance our request and 472 * retry it. If we got no data, end the rest with EIO. 473 */ 474 if (cmd->ret) { 475 blk_update_request(rq, BLK_STS_OK, cmd->ret); 476 cmd->ret = 0; 477 blk_mq_requeue_request(rq, true); 478 } else { 479 if (cmd->use_aio) { 480 struct bio *bio = rq->bio; 481 482 while (bio) { 483 zero_fill_bio(bio); 484 bio = bio->bi_next; 485 } 486 } 487 ret = BLK_STS_IOERR; 488 end_io: 489 blk_mq_end_request(rq, ret); 490 } 491 } 492 493 static void lo_rw_aio_do_completion(struct loop_cmd *cmd) 494 { 495 struct request *rq = blk_mq_rq_from_pdu(cmd); 496 497 if (!atomic_dec_and_test(&cmd->ref)) 498 return; 499 kfree(cmd->bvec); 500 cmd->bvec = NULL; 501 blk_mq_complete_request(rq); 502 } 503 504 static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 505 { 506 struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); 507 508 if (cmd->css) 509 css_put(cmd->css); 510 cmd->ret = ret; 511 lo_rw_aio_do_completion(cmd); 512 } 513 514 static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, 515 loff_t pos, bool rw) 516 { 517 struct iov_iter iter; 518 struct req_iterator rq_iter; 519 struct bio_vec *bvec; 520 struct request *rq = blk_mq_rq_from_pdu(cmd); 521 struct bio *bio = rq->bio; 522 struct file *file = lo->lo_backing_file; 523 struct bio_vec tmp; 524 unsigned int offset; 525 int nr_bvec = 0; 526 int ret; 527 528 rq_for_each_bvec(tmp, rq, rq_iter) 529 nr_bvec++; 530 531 if (rq->bio != rq->biotail) { 532 533 bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), 534 GFP_NOIO); 535 if (!bvec) 536 return -EIO; 537 cmd->bvec = bvec; 538 539 /* 540 * The bios of the request may be started from the middle of 541 * the 'bvec' because of bio splitting, so we can't directly 542 * copy bio->bi_iov_vec to new bvec. The rq_for_each_bvec 543 * API will take care of all details for us. 544 */ 545 rq_for_each_bvec(tmp, rq, rq_iter) { 546 *bvec = tmp; 547 bvec++; 548 } 549 bvec = cmd->bvec; 550 offset = 0; 551 } else { 552 /* 553 * Same here, this bio may be started from the middle of the 554 * 'bvec' because of bio splitting, so offset from the bvec 555 * must be passed to iov iterator 556 */ 557 offset = bio->bi_iter.bi_bvec_done; 558 bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 559 } 560 atomic_set(&cmd->ref, 2); 561 562 iov_iter_bvec(&iter, rw, bvec, nr_bvec, blk_rq_bytes(rq)); 563 iter.iov_offset = offset; 564 565 cmd->iocb.ki_pos = pos; 566 cmd->iocb.ki_filp = file; 567 cmd->iocb.ki_complete = lo_rw_aio_complete; 568 cmd->iocb.ki_flags = IOCB_DIRECT; 569 cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); 570 if (cmd->css) 571 kthread_associate_blkcg(cmd->css); 572 573 if (rw == WRITE) 574 ret = call_write_iter(file, &cmd->iocb, &iter); 575 else 576 ret = call_read_iter(file, &cmd->iocb, &iter); 577 578 lo_rw_aio_do_completion(cmd); 579 kthread_associate_blkcg(NULL); 580 581 if (ret != -EIOCBQUEUED) 582 cmd->iocb.ki_complete(&cmd->iocb, ret, 0); 583 return 0; 584 } 585 586 static int do_req_filebacked(struct loop_device *lo, struct request *rq) 587 { 588 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 589 loff_t pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; 590 591 /* 592 * lo_write_simple and lo_read_simple should have been covered 593 * by io submit style function like lo_rw_aio(), one blocker 594 * is that lo_read_simple() need to call flush_dcache_page after 595 * the page is written from kernel, and it isn't easy to handle 596 * this in io submit style function which submits all segments 597 * of the req at one time. And direct read IO doesn't need to 598 * run flush_dcache_page(). 599 */ 600 switch (req_op(rq)) { 601 case REQ_OP_FLUSH: 602 return lo_req_flush(lo, rq); 603 case REQ_OP_WRITE_ZEROES: 604 /* 605 * If the caller doesn't want deallocation, call zeroout to 606 * write zeroes the range. Otherwise, punch them out. 607 */ 608 return lo_fallocate(lo, rq, pos, 609 (rq->cmd_flags & REQ_NOUNMAP) ? 610 FALLOC_FL_ZERO_RANGE : 611 FALLOC_FL_PUNCH_HOLE); 612 case REQ_OP_DISCARD: 613 return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE); 614 case REQ_OP_WRITE: 615 if (lo->transfer) 616 return lo_write_transfer(lo, rq, pos); 617 else if (cmd->use_aio) 618 return lo_rw_aio(lo, cmd, pos, WRITE); 619 else 620 return lo_write_simple(lo, rq, pos); 621 case REQ_OP_READ: 622 if (lo->transfer) 623 return lo_read_transfer(lo, rq, pos); 624 else if (cmd->use_aio) 625 return lo_rw_aio(lo, cmd, pos, READ); 626 else 627 return lo_read_simple(lo, rq, pos); 628 default: 629 WARN_ON_ONCE(1); 630 return -EIO; 631 } 632 } 633 634 static inline void loop_update_dio(struct loop_device *lo) 635 { 636 __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) | 637 lo->use_dio); 638 } 639 640 static void loop_reread_partitions(struct loop_device *lo, 641 struct block_device *bdev) 642 { 643 int rc; 644 645 mutex_lock(&bdev->bd_mutex); 646 rc = bdev_disk_changed(bdev, false); 647 mutex_unlock(&bdev->bd_mutex); 648 if (rc) 649 pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", 650 __func__, lo->lo_number, lo->lo_file_name, rc); 651 } 652 653 static inline int is_loop_device(struct file *file) 654 { 655 struct inode *i = file->f_mapping->host; 656 657 return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR; 658 } 659 660 static int loop_validate_file(struct file *file, struct block_device *bdev) 661 { 662 struct inode *inode = file->f_mapping->host; 663 struct file *f = file; 664 665 /* Avoid recursion */ 666 while (is_loop_device(f)) { 667 struct loop_device *l; 668 669 if (f->f_mapping->host->i_bdev == bdev) 670 return -EBADF; 671 672 l = f->f_mapping->host->i_bdev->bd_disk->private_data; 673 if (l->lo_state != Lo_bound) { 674 return -EINVAL; 675 } 676 f = l->lo_backing_file; 677 } 678 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 679 return -EINVAL; 680 return 0; 681 } 682 683 /* 684 * loop_change_fd switched the backing store of a loopback device to 685 * a new file. This is useful for operating system installers to free up 686 * the original file and in High Availability environments to switch to 687 * an alternative location for the content in case of server meltdown. 688 * This can only work if the loop device is used read-only, and if the 689 * new backing store is the same size and type as the old backing store. 690 */ 691 static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, 692 unsigned int arg) 693 { 694 struct file *file = NULL, *old_file; 695 int error; 696 bool partscan; 697 698 error = mutex_lock_killable(&loop_ctl_mutex); 699 if (error) 700 return error; 701 error = -ENXIO; 702 if (lo->lo_state != Lo_bound) 703 goto out_err; 704 705 /* the loop device has to be read-only */ 706 error = -EINVAL; 707 if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) 708 goto out_err; 709 710 error = -EBADF; 711 file = fget(arg); 712 if (!file) 713 goto out_err; 714 715 error = loop_validate_file(file, bdev); 716 if (error) 717 goto out_err; 718 719 old_file = lo->lo_backing_file; 720 721 error = -EINVAL; 722 723 /* size of the new backing store needs to be the same */ 724 if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) 725 goto out_err; 726 727 /* and ... switch */ 728 blk_mq_freeze_queue(lo->lo_queue); 729 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); 730 lo->lo_backing_file = file; 731 lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); 732 mapping_set_gfp_mask(file->f_mapping, 733 lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 734 loop_update_dio(lo); 735 blk_mq_unfreeze_queue(lo->lo_queue); 736 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 737 mutex_unlock(&loop_ctl_mutex); 738 /* 739 * We must drop file reference outside of loop_ctl_mutex as dropping 740 * the file ref can take bd_mutex which creates circular locking 741 * dependency. 742 */ 743 fput(old_file); 744 if (partscan) 745 loop_reread_partitions(lo, bdev); 746 return 0; 747 748 out_err: 749 mutex_unlock(&loop_ctl_mutex); 750 if (file) 751 fput(file); 752 return error; 753 } 754 755 /* loop sysfs attributes */ 756 757 static ssize_t loop_attr_show(struct device *dev, char *page, 758 ssize_t (*callback)(struct loop_device *, char *)) 759 { 760 struct gendisk *disk = dev_to_disk(dev); 761 struct loop_device *lo = disk->private_data; 762 763 return callback(lo, page); 764 } 765 766 #define LOOP_ATTR_RO(_name) \ 767 static ssize_t loop_attr_##_name##_show(struct loop_device *, char *); \ 768 static ssize_t loop_attr_do_show_##_name(struct device *d, \ 769 struct device_attribute *attr, char *b) \ 770 { \ 771 return loop_attr_show(d, b, loop_attr_##_name##_show); \ 772 } \ 773 static struct device_attribute loop_attr_##_name = \ 774 __ATTR(_name, 0444, loop_attr_do_show_##_name, NULL); 775 776 static ssize_t loop_attr_backing_file_show(struct loop_device *lo, char *buf) 777 { 778 ssize_t ret; 779 char *p = NULL; 780 781 spin_lock_irq(&lo->lo_lock); 782 if (lo->lo_backing_file) 783 p = file_path(lo->lo_backing_file, buf, PAGE_SIZE - 1); 784 spin_unlock_irq(&lo->lo_lock); 785 786 if (IS_ERR_OR_NULL(p)) 787 ret = PTR_ERR(p); 788 else { 789 ret = strlen(p); 790 memmove(buf, p, ret); 791 buf[ret++] = '\n'; 792 buf[ret] = 0; 793 } 794 795 return ret; 796 } 797 798 static ssize_t loop_attr_offset_show(struct loop_device *lo, char *buf) 799 { 800 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_offset); 801 } 802 803 static ssize_t loop_attr_sizelimit_show(struct loop_device *lo, char *buf) 804 { 805 return sprintf(buf, "%llu\n", (unsigned long long)lo->lo_sizelimit); 806 } 807 808 static ssize_t loop_attr_autoclear_show(struct loop_device *lo, char *buf) 809 { 810 int autoclear = (lo->lo_flags & LO_FLAGS_AUTOCLEAR); 811 812 return sprintf(buf, "%s\n", autoclear ? "1" : "0"); 813 } 814 815 static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) 816 { 817 int partscan = (lo->lo_flags & LO_FLAGS_PARTSCAN); 818 819 return sprintf(buf, "%s\n", partscan ? "1" : "0"); 820 } 821 822 static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf) 823 { 824 int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO); 825 826 return sprintf(buf, "%s\n", dio ? "1" : "0"); 827 } 828 829 LOOP_ATTR_RO(backing_file); 830 LOOP_ATTR_RO(offset); 831 LOOP_ATTR_RO(sizelimit); 832 LOOP_ATTR_RO(autoclear); 833 LOOP_ATTR_RO(partscan); 834 LOOP_ATTR_RO(dio); 835 836 static struct attribute *loop_attrs[] = { 837 &loop_attr_backing_file.attr, 838 &loop_attr_offset.attr, 839 &loop_attr_sizelimit.attr, 840 &loop_attr_autoclear.attr, 841 &loop_attr_partscan.attr, 842 &loop_attr_dio.attr, 843 NULL, 844 }; 845 846 static struct attribute_group loop_attribute_group = { 847 .name = "loop", 848 .attrs= loop_attrs, 849 }; 850 851 static void loop_sysfs_init(struct loop_device *lo) 852 { 853 lo->sysfs_inited = !sysfs_create_group(&disk_to_dev(lo->lo_disk)->kobj, 854 &loop_attribute_group); 855 } 856 857 static void loop_sysfs_exit(struct loop_device *lo) 858 { 859 if (lo->sysfs_inited) 860 sysfs_remove_group(&disk_to_dev(lo->lo_disk)->kobj, 861 &loop_attribute_group); 862 } 863 864 static void loop_config_discard(struct loop_device *lo) 865 { 866 struct file *file = lo->lo_backing_file; 867 struct inode *inode = file->f_mapping->host; 868 struct request_queue *q = lo->lo_queue; 869 870 /* 871 * We use punch hole to reclaim the free space used by the 872 * image a.k.a. discard. However we do not support discard if 873 * encryption is enabled, because it may give an attacker 874 * useful information. 875 */ 876 if ((!file->f_op->fallocate) || 877 lo->lo_encrypt_key_size) { 878 q->limits.discard_granularity = 0; 879 q->limits.discard_alignment = 0; 880 blk_queue_max_discard_sectors(q, 0); 881 blk_queue_max_write_zeroes_sectors(q, 0); 882 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); 883 return; 884 } 885 886 q->limits.discard_granularity = inode->i_sb->s_blocksize; 887 q->limits.discard_alignment = 0; 888 889 blk_queue_max_discard_sectors(q, UINT_MAX >> 9); 890 blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); 891 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 892 } 893 894 static void loop_unprepare_queue(struct loop_device *lo) 895 { 896 kthread_flush_worker(&lo->worker); 897 kthread_stop(lo->worker_task); 898 } 899 900 static int loop_kthread_worker_fn(void *worker_ptr) 901 { 902 current->flags |= PF_LESS_THROTTLE | PF_MEMALLOC_NOIO; 903 return kthread_worker_fn(worker_ptr); 904 } 905 906 static int loop_prepare_queue(struct loop_device *lo) 907 { 908 kthread_init_worker(&lo->worker); 909 lo->worker_task = kthread_run(loop_kthread_worker_fn, 910 &lo->worker, "loop%d", lo->lo_number); 911 if (IS_ERR(lo->worker_task)) 912 return -ENOMEM; 913 set_user_nice(lo->worker_task, MIN_NICE); 914 return 0; 915 } 916 917 static void loop_update_rotational(struct loop_device *lo) 918 { 919 struct file *file = lo->lo_backing_file; 920 struct inode *file_inode = file->f_mapping->host; 921 struct block_device *file_bdev = file_inode->i_sb->s_bdev; 922 struct request_queue *q = lo->lo_queue; 923 bool nonrot = true; 924 925 /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 926 if (file_bdev) 927 nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); 928 929 if (nonrot) 930 blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 931 else 932 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 933 } 934 935 static int loop_set_fd(struct loop_device *lo, fmode_t mode, 936 struct block_device *bdev, unsigned int arg) 937 { 938 struct file *file; 939 struct inode *inode; 940 struct address_space *mapping; 941 struct block_device *claimed_bdev = NULL; 942 int lo_flags = 0; 943 int error; 944 loff_t size; 945 bool partscan; 946 947 /* This is safe, since we have a reference from open(). */ 948 __module_get(THIS_MODULE); 949 950 error = -EBADF; 951 file = fget(arg); 952 if (!file) 953 goto out; 954 955 /* 956 * If we don't hold exclusive handle for the device, upgrade to it 957 * here to avoid changing device under exclusive owner. 958 */ 959 if (!(mode & FMODE_EXCL)) { 960 claimed_bdev = bd_start_claiming(bdev, loop_set_fd); 961 if (IS_ERR(claimed_bdev)) { 962 error = PTR_ERR(claimed_bdev); 963 goto out_putf; 964 } 965 } 966 967 error = mutex_lock_killable(&loop_ctl_mutex); 968 if (error) 969 goto out_bdev; 970 971 error = -EBUSY; 972 if (lo->lo_state != Lo_unbound) 973 goto out_unlock; 974 975 error = loop_validate_file(file, bdev); 976 if (error) 977 goto out_unlock; 978 979 mapping = file->f_mapping; 980 inode = mapping->host; 981 982 if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || 983 !file->f_op->write_iter) 984 lo_flags |= LO_FLAGS_READ_ONLY; 985 986 error = -EFBIG; 987 size = get_loop_size(lo, file); 988 if ((loff_t)(sector_t)size != size) 989 goto out_unlock; 990 error = loop_prepare_queue(lo); 991 if (error) 992 goto out_unlock; 993 994 error = 0; 995 996 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0); 997 998 lo->use_dio = false; 999 lo->lo_device = bdev; 1000 lo->lo_flags = lo_flags; 1001 lo->lo_backing_file = file; 1002 lo->transfer = NULL; 1003 lo->ioctl = NULL; 1004 lo->lo_sizelimit = 0; 1005 lo->old_gfp_mask = mapping_gfp_mask(mapping); 1006 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); 1007 1008 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 1009 blk_queue_write_cache(lo->lo_queue, true, false); 1010 1011 if (io_is_direct(lo->lo_backing_file) && inode->i_sb->s_bdev) { 1012 /* In case of direct I/O, match underlying block size */ 1013 unsigned short bsize = bdev_logical_block_size( 1014 inode->i_sb->s_bdev); 1015 1016 blk_queue_logical_block_size(lo->lo_queue, bsize); 1017 blk_queue_physical_block_size(lo->lo_queue, bsize); 1018 blk_queue_io_min(lo->lo_queue, bsize); 1019 } 1020 1021 loop_update_rotational(lo); 1022 loop_update_dio(lo); 1023 set_capacity(lo->lo_disk, size); 1024 bd_set_size(bdev, size << 9); 1025 loop_sysfs_init(lo); 1026 /* let user-space know about the new size */ 1027 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1028 1029 set_blocksize(bdev, S_ISBLK(inode->i_mode) ? 1030 block_size(inode->i_bdev) : PAGE_SIZE); 1031 1032 lo->lo_state = Lo_bound; 1033 if (part_shift) 1034 lo->lo_flags |= LO_FLAGS_PARTSCAN; 1035 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; 1036 1037 /* Grab the block_device to prevent its destruction after we 1038 * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev). 1039 */ 1040 bdgrab(bdev); 1041 mutex_unlock(&loop_ctl_mutex); 1042 if (partscan) 1043 loop_reread_partitions(lo, bdev); 1044 if (claimed_bdev) 1045 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); 1046 return 0; 1047 1048 out_unlock: 1049 mutex_unlock(&loop_ctl_mutex); 1050 out_bdev: 1051 if (claimed_bdev) 1052 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd); 1053 out_putf: 1054 fput(file); 1055 out: 1056 /* This is safe: open() is still holding a reference. */ 1057 module_put(THIS_MODULE); 1058 return error; 1059 } 1060 1061 static int 1062 loop_release_xfer(struct loop_device *lo) 1063 { 1064 int err = 0; 1065 struct loop_func_table *xfer = lo->lo_encryption; 1066 1067 if (xfer) { 1068 if (xfer->release) 1069 err = xfer->release(lo); 1070 lo->transfer = NULL; 1071 lo->lo_encryption = NULL; 1072 module_put(xfer->owner); 1073 } 1074 return err; 1075 } 1076 1077 static int 1078 loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer, 1079 const struct loop_info64 *i) 1080 { 1081 int err = 0; 1082 1083 if (xfer) { 1084 struct module *owner = xfer->owner; 1085 1086 if (!try_module_get(owner)) 1087 return -EINVAL; 1088 if (xfer->init) 1089 err = xfer->init(lo, i); 1090 if (err) 1091 module_put(owner); 1092 else 1093 lo->lo_encryption = xfer; 1094 } 1095 return err; 1096 } 1097 1098 static int __loop_clr_fd(struct loop_device *lo, bool release) 1099 { 1100 struct file *filp = NULL; 1101 gfp_t gfp = lo->old_gfp_mask; 1102 struct block_device *bdev = lo->lo_device; 1103 int err = 0; 1104 bool partscan = false; 1105 int lo_number; 1106 1107 mutex_lock(&loop_ctl_mutex); 1108 if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) { 1109 err = -ENXIO; 1110 goto out_unlock; 1111 } 1112 1113 filp = lo->lo_backing_file; 1114 if (filp == NULL) { 1115 err = -EINVAL; 1116 goto out_unlock; 1117 } 1118 1119 /* freeze request queue during the transition */ 1120 blk_mq_freeze_queue(lo->lo_queue); 1121 1122 spin_lock_irq(&lo->lo_lock); 1123 lo->lo_backing_file = NULL; 1124 spin_unlock_irq(&lo->lo_lock); 1125 1126 loop_release_xfer(lo); 1127 lo->transfer = NULL; 1128 lo->ioctl = NULL; 1129 lo->lo_device = NULL; 1130 lo->lo_encryption = NULL; 1131 lo->lo_offset = 0; 1132 lo->lo_sizelimit = 0; 1133 lo->lo_encrypt_key_size = 0; 1134 memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); 1135 memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); 1136 memset(lo->lo_file_name, 0, LO_NAME_SIZE); 1137 blk_queue_logical_block_size(lo->lo_queue, 512); 1138 blk_queue_physical_block_size(lo->lo_queue, 512); 1139 blk_queue_io_min(lo->lo_queue, 512); 1140 if (bdev) { 1141 bdput(bdev); 1142 invalidate_bdev(bdev); 1143 bdev->bd_inode->i_mapping->wb_err = 0; 1144 } 1145 set_capacity(lo->lo_disk, 0); 1146 loop_sysfs_exit(lo); 1147 if (bdev) { 1148 bd_set_size(bdev, 0); 1149 /* let user-space know about this change */ 1150 kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); 1151 } 1152 mapping_set_gfp_mask(filp->f_mapping, gfp); 1153 /* This is safe: open() is still holding a reference. */ 1154 module_put(THIS_MODULE); 1155 blk_mq_unfreeze_queue(lo->lo_queue); 1156 1157 partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev; 1158 lo_number = lo->lo_number; 1159 loop_unprepare_queue(lo); 1160 out_unlock: 1161 mutex_unlock(&loop_ctl_mutex); 1162 if (partscan) { 1163 /* 1164 * bd_mutex has been held already in release path, so don't 1165 * acquire it if this function is called in such case. 1166 * 1167 * If the reread partition isn't from release path, lo_refcnt 1168 * must be at least one and it can only become zero when the 1169 * current holder is released. 1170 */ 1171 if (!release) 1172 mutex_lock(&bdev->bd_mutex); 1173 err = bdev_disk_changed(bdev, false); 1174 if (!release) 1175 mutex_unlock(&bdev->bd_mutex); 1176 if (err) 1177 pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", 1178 __func__, lo_number, err); 1179 /* Device is gone, no point in returning error */ 1180 err = 0; 1181 } 1182 1183 /* 1184 * lo->lo_state is set to Lo_unbound here after above partscan has 1185 * finished. 1186 * 1187 * There cannot be anybody else entering __loop_clr_fd() as 1188 * lo->lo_backing_file is already cleared and Lo_rundown state 1189 * protects us from all the other places trying to change the 'lo' 1190 * device. 1191 */ 1192 mutex_lock(&loop_ctl_mutex); 1193 lo->lo_flags = 0; 1194 if (!part_shift) 1195 lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; 1196 lo->lo_state = Lo_unbound; 1197 mutex_unlock(&loop_ctl_mutex); 1198 1199 /* 1200 * Need not hold loop_ctl_mutex to fput backing file. 1201 * Calling fput holding loop_ctl_mutex triggers a circular 1202 * lock dependency possibility warning as fput can take 1203 * bd_mutex which is usually taken before loop_ctl_mutex. 1204 */ 1205 if (filp) 1206 fput(filp); 1207 return err; 1208 } 1209 1210 static int loop_clr_fd(struct loop_device *lo) 1211 { 1212 int err; 1213 1214 err = mutex_lock_killable(&loop_ctl_mutex); 1215 if (err) 1216 return err; 1217 if (lo->lo_state != Lo_bound) { 1218 mutex_unlock(&loop_ctl_mutex); 1219 return -ENXIO; 1220 } 1221 /* 1222 * If we've explicitly asked to tear down the loop device, 1223 * and it has an elevated reference count, set it for auto-teardown when 1224 * the last reference goes away. This stops $!~#$@ udev from 1225 * preventing teardown because it decided that it needs to run blkid on 1226 * the loopback device whenever they appear. xfstests is notorious for 1227 * failing tests because blkid via udev races with a losetup 1228 * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d 1229 * command to fail with EBUSY. 1230 */ 1231 if (atomic_read(&lo->lo_refcnt) > 1) { 1232 lo->lo_flags |= LO_FLAGS_AUTOCLEAR; 1233 mutex_unlock(&loop_ctl_mutex); 1234 return 0; 1235 } 1236 lo->lo_state = Lo_rundown; 1237 mutex_unlock(&loop_ctl_mutex); 1238 1239 return __loop_clr_fd(lo, false); 1240 } 1241 1242 static int 1243 loop_set_status(struct loop_device *lo, const struct loop_info64 *info) 1244 { 1245 int err; 1246 struct loop_func_table *xfer; 1247 kuid_t uid = current_uid(); 1248 struct block_device *bdev; 1249 bool partscan = false; 1250 1251 err = mutex_lock_killable(&loop_ctl_mutex); 1252 if (err) 1253 return err; 1254 if (lo->lo_encrypt_key_size && 1255 !uid_eq(lo->lo_key_owner, uid) && 1256 !capable(CAP_SYS_ADMIN)) { 1257 err = -EPERM; 1258 goto out_unlock; 1259 } 1260 if (lo->lo_state != Lo_bound) { 1261 err = -ENXIO; 1262 goto out_unlock; 1263 } 1264 if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE) { 1265 err = -EINVAL; 1266 goto out_unlock; 1267 } 1268 1269 if (lo->lo_offset != info->lo_offset || 1270 lo->lo_sizelimit != info->lo_sizelimit) { 1271 sync_blockdev(lo->lo_device); 1272 kill_bdev(lo->lo_device); 1273 } 1274 1275 /* I/O need to be drained during transfer transition */ 1276 blk_mq_freeze_queue(lo->lo_queue); 1277 1278 err = loop_release_xfer(lo); 1279 if (err) 1280 goto out_unfreeze; 1281 1282 if (info->lo_encrypt_type) { 1283 unsigned int type = info->lo_encrypt_type; 1284 1285 if (type >= MAX_LO_CRYPT) { 1286 err = -EINVAL; 1287 goto out_unfreeze; 1288 } 1289 xfer = xfer_funcs[type]; 1290 if (xfer == NULL) { 1291 err = -EINVAL; 1292 goto out_unfreeze; 1293 } 1294 } else 1295 xfer = NULL; 1296 1297 err = loop_init_xfer(lo, xfer, info); 1298 if (err) 1299 goto out_unfreeze; 1300 1301 if (lo->lo_offset != info->lo_offset || 1302 lo->lo_sizelimit != info->lo_sizelimit) { 1303 /* kill_bdev should have truncated all the pages */ 1304 if (lo->lo_device->bd_inode->i_mapping->nrpages) { 1305 err = -EAGAIN; 1306 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 1307 __func__, lo->lo_number, lo->lo_file_name, 1308 lo->lo_device->bd_inode->i_mapping->nrpages); 1309 goto out_unfreeze; 1310 } 1311 if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) { 1312 err = -EFBIG; 1313 goto out_unfreeze; 1314 } 1315 } 1316 1317 loop_config_discard(lo); 1318 1319 memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE); 1320 memcpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE); 1321 lo->lo_file_name[LO_NAME_SIZE-1] = 0; 1322 lo->lo_crypt_name[LO_NAME_SIZE-1] = 0; 1323 1324 if (!xfer) 1325 xfer = &none_funcs; 1326 lo->transfer = xfer->transfer; 1327 lo->ioctl = xfer->ioctl; 1328 1329 if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) != 1330 (info->lo_flags & LO_FLAGS_AUTOCLEAR)) 1331 lo->lo_flags ^= LO_FLAGS_AUTOCLEAR; 1332 1333 lo->lo_encrypt_key_size = info->lo_encrypt_key_size; 1334 lo->lo_init[0] = info->lo_init[0]; 1335 lo->lo_init[1] = info->lo_init[1]; 1336 if (info->lo_encrypt_key_size) { 1337 memcpy(lo->lo_encrypt_key, info->lo_encrypt_key, 1338 info->lo_encrypt_key_size); 1339 lo->lo_key_owner = uid; 1340 } 1341 1342 /* update dio if lo_offset or transfer is changed */ 1343 __loop_update_dio(lo, lo->use_dio); 1344 1345 out_unfreeze: 1346 blk_mq_unfreeze_queue(lo->lo_queue); 1347 1348 if (!err && (info->lo_flags & LO_FLAGS_PARTSCAN) && 1349 !(lo->lo_flags & LO_FLAGS_PARTSCAN)) { 1350 lo->lo_flags |= LO_FLAGS_PARTSCAN; 1351 lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN; 1352 bdev = lo->lo_device; 1353 partscan = true; 1354 } 1355 out_unlock: 1356 mutex_unlock(&loop_ctl_mutex); 1357 if (partscan) 1358 loop_reread_partitions(lo, bdev); 1359 1360 return err; 1361 } 1362 1363 static int 1364 loop_get_status(struct loop_device *lo, struct loop_info64 *info) 1365 { 1366 struct path path; 1367 struct kstat stat; 1368 int ret; 1369 1370 ret = mutex_lock_killable(&loop_ctl_mutex); 1371 if (ret) 1372 return ret; 1373 if (lo->lo_state != Lo_bound) { 1374 mutex_unlock(&loop_ctl_mutex); 1375 return -ENXIO; 1376 } 1377 1378 memset(info, 0, sizeof(*info)); 1379 info->lo_number = lo->lo_number; 1380 info->lo_offset = lo->lo_offset; 1381 info->lo_sizelimit = lo->lo_sizelimit; 1382 info->lo_flags = lo->lo_flags; 1383 memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE); 1384 memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE); 1385 info->lo_encrypt_type = 1386 lo->lo_encryption ? lo->lo_encryption->number : 0; 1387 if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) { 1388 info->lo_encrypt_key_size = lo->lo_encrypt_key_size; 1389 memcpy(info->lo_encrypt_key, lo->lo_encrypt_key, 1390 lo->lo_encrypt_key_size); 1391 } 1392 1393 /* Drop loop_ctl_mutex while we call into the filesystem. */ 1394 path = lo->lo_backing_file->f_path; 1395 path_get(&path); 1396 mutex_unlock(&loop_ctl_mutex); 1397 ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT); 1398 if (!ret) { 1399 info->lo_device = huge_encode_dev(stat.dev); 1400 info->lo_inode = stat.ino; 1401 info->lo_rdevice = huge_encode_dev(stat.rdev); 1402 } 1403 path_put(&path); 1404 return ret; 1405 } 1406 1407 static void 1408 loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64) 1409 { 1410 memset(info64, 0, sizeof(*info64)); 1411 info64->lo_number = info->lo_number; 1412 info64->lo_device = info->lo_device; 1413 info64->lo_inode = info->lo_inode; 1414 info64->lo_rdevice = info->lo_rdevice; 1415 info64->lo_offset = info->lo_offset; 1416 info64->lo_sizelimit = 0; 1417 info64->lo_encrypt_type = info->lo_encrypt_type; 1418 info64->lo_encrypt_key_size = info->lo_encrypt_key_size; 1419 info64->lo_flags = info->lo_flags; 1420 info64->lo_init[0] = info->lo_init[0]; 1421 info64->lo_init[1] = info->lo_init[1]; 1422 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1423 memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE); 1424 else 1425 memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE); 1426 memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE); 1427 } 1428 1429 static int 1430 loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info) 1431 { 1432 memset(info, 0, sizeof(*info)); 1433 info->lo_number = info64->lo_number; 1434 info->lo_device = info64->lo_device; 1435 info->lo_inode = info64->lo_inode; 1436 info->lo_rdevice = info64->lo_rdevice; 1437 info->lo_offset = info64->lo_offset; 1438 info->lo_encrypt_type = info64->lo_encrypt_type; 1439 info->lo_encrypt_key_size = info64->lo_encrypt_key_size; 1440 info->lo_flags = info64->lo_flags; 1441 info->lo_init[0] = info64->lo_init[0]; 1442 info->lo_init[1] = info64->lo_init[1]; 1443 if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1444 memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1445 else 1446 memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE); 1447 memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1448 1449 /* error in case values were truncated */ 1450 if (info->lo_device != info64->lo_device || 1451 info->lo_rdevice != info64->lo_rdevice || 1452 info->lo_inode != info64->lo_inode || 1453 info->lo_offset != info64->lo_offset) 1454 return -EOVERFLOW; 1455 1456 return 0; 1457 } 1458 1459 static int 1460 loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg) 1461 { 1462 struct loop_info info; 1463 struct loop_info64 info64; 1464 1465 if (copy_from_user(&info, arg, sizeof (struct loop_info))) 1466 return -EFAULT; 1467 loop_info64_from_old(&info, &info64); 1468 return loop_set_status(lo, &info64); 1469 } 1470 1471 static int 1472 loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg) 1473 { 1474 struct loop_info64 info64; 1475 1476 if (copy_from_user(&info64, arg, sizeof (struct loop_info64))) 1477 return -EFAULT; 1478 return loop_set_status(lo, &info64); 1479 } 1480 1481 static int 1482 loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) { 1483 struct loop_info info; 1484 struct loop_info64 info64; 1485 int err; 1486 1487 if (!arg) 1488 return -EINVAL; 1489 err = loop_get_status(lo, &info64); 1490 if (!err) 1491 err = loop_info64_to_old(&info64, &info); 1492 if (!err && copy_to_user(arg, &info, sizeof(info))) 1493 err = -EFAULT; 1494 1495 return err; 1496 } 1497 1498 static int 1499 loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) { 1500 struct loop_info64 info64; 1501 int err; 1502 1503 if (!arg) 1504 return -EINVAL; 1505 err = loop_get_status(lo, &info64); 1506 if (!err && copy_to_user(arg, &info64, sizeof(info64))) 1507 err = -EFAULT; 1508 1509 return err; 1510 } 1511 1512 static int loop_set_capacity(struct loop_device *lo) 1513 { 1514 if (unlikely(lo->lo_state != Lo_bound)) 1515 return -ENXIO; 1516 1517 return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit); 1518 } 1519 1520 static int loop_set_dio(struct loop_device *lo, unsigned long arg) 1521 { 1522 int error = -ENXIO; 1523 if (lo->lo_state != Lo_bound) 1524 goto out; 1525 1526 __loop_update_dio(lo, !!arg); 1527 if (lo->use_dio == !!arg) 1528 return 0; 1529 error = -EINVAL; 1530 out: 1531 return error; 1532 } 1533 1534 static int loop_set_block_size(struct loop_device *lo, unsigned long arg) 1535 { 1536 int err = 0; 1537 1538 if (lo->lo_state != Lo_bound) 1539 return -ENXIO; 1540 1541 if (arg < 512 || arg > PAGE_SIZE || !is_power_of_2(arg)) 1542 return -EINVAL; 1543 1544 if (lo->lo_queue->limits.logical_block_size == arg) 1545 return 0; 1546 1547 sync_blockdev(lo->lo_device); 1548 kill_bdev(lo->lo_device); 1549 1550 blk_mq_freeze_queue(lo->lo_queue); 1551 1552 /* kill_bdev should have truncated all the pages */ 1553 if (lo->lo_device->bd_inode->i_mapping->nrpages) { 1554 err = -EAGAIN; 1555 pr_warn("%s: loop%d (%s) has still dirty pages (nrpages=%lu)\n", 1556 __func__, lo->lo_number, lo->lo_file_name, 1557 lo->lo_device->bd_inode->i_mapping->nrpages); 1558 goto out_unfreeze; 1559 } 1560 1561 blk_queue_logical_block_size(lo->lo_queue, arg); 1562 blk_queue_physical_block_size(lo->lo_queue, arg); 1563 blk_queue_io_min(lo->lo_queue, arg); 1564 loop_update_dio(lo); 1565 out_unfreeze: 1566 blk_mq_unfreeze_queue(lo->lo_queue); 1567 1568 return err; 1569 } 1570 1571 static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, 1572 unsigned long arg) 1573 { 1574 int err; 1575 1576 err = mutex_lock_killable(&loop_ctl_mutex); 1577 if (err) 1578 return err; 1579 switch (cmd) { 1580 case LOOP_SET_CAPACITY: 1581 err = loop_set_capacity(lo); 1582 break; 1583 case LOOP_SET_DIRECT_IO: 1584 err = loop_set_dio(lo, arg); 1585 break; 1586 case LOOP_SET_BLOCK_SIZE: 1587 err = loop_set_block_size(lo, arg); 1588 break; 1589 default: 1590 err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; 1591 } 1592 mutex_unlock(&loop_ctl_mutex); 1593 return err; 1594 } 1595 1596 static int lo_ioctl(struct block_device *bdev, fmode_t mode, 1597 unsigned int cmd, unsigned long arg) 1598 { 1599 struct loop_device *lo = bdev->bd_disk->private_data; 1600 int err; 1601 1602 switch (cmd) { 1603 case LOOP_SET_FD: 1604 return loop_set_fd(lo, mode, bdev, arg); 1605 case LOOP_CHANGE_FD: 1606 return loop_change_fd(lo, bdev, arg); 1607 case LOOP_CLR_FD: 1608 return loop_clr_fd(lo); 1609 case LOOP_SET_STATUS: 1610 err = -EPERM; 1611 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1612 err = loop_set_status_old(lo, 1613 (struct loop_info __user *)arg); 1614 } 1615 break; 1616 case LOOP_GET_STATUS: 1617 return loop_get_status_old(lo, (struct loop_info __user *) arg); 1618 case LOOP_SET_STATUS64: 1619 err = -EPERM; 1620 if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { 1621 err = loop_set_status64(lo, 1622 (struct loop_info64 __user *) arg); 1623 } 1624 break; 1625 case LOOP_GET_STATUS64: 1626 return loop_get_status64(lo, (struct loop_info64 __user *) arg); 1627 case LOOP_SET_CAPACITY: 1628 case LOOP_SET_DIRECT_IO: 1629 case LOOP_SET_BLOCK_SIZE: 1630 if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) 1631 return -EPERM; 1632 /* Fall through */ 1633 default: 1634 err = lo_simple_ioctl(lo, cmd, arg); 1635 break; 1636 } 1637 1638 return err; 1639 } 1640 1641 #ifdef CONFIG_COMPAT 1642 struct compat_loop_info { 1643 compat_int_t lo_number; /* ioctl r/o */ 1644 compat_dev_t lo_device; /* ioctl r/o */ 1645 compat_ulong_t lo_inode; /* ioctl r/o */ 1646 compat_dev_t lo_rdevice; /* ioctl r/o */ 1647 compat_int_t lo_offset; 1648 compat_int_t lo_encrypt_type; 1649 compat_int_t lo_encrypt_key_size; /* ioctl w/o */ 1650 compat_int_t lo_flags; /* ioctl r/o */ 1651 char lo_name[LO_NAME_SIZE]; 1652 unsigned char lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */ 1653 compat_ulong_t lo_init[2]; 1654 char reserved[4]; 1655 }; 1656 1657 /* 1658 * Transfer 32-bit compatibility structure in userspace to 64-bit loop info 1659 * - noinlined to reduce stack space usage in main part of driver 1660 */ 1661 static noinline int 1662 loop_info64_from_compat(const struct compat_loop_info __user *arg, 1663 struct loop_info64 *info64) 1664 { 1665 struct compat_loop_info info; 1666 1667 if (copy_from_user(&info, arg, sizeof(info))) 1668 return -EFAULT; 1669 1670 memset(info64, 0, sizeof(*info64)); 1671 info64->lo_number = info.lo_number; 1672 info64->lo_device = info.lo_device; 1673 info64->lo_inode = info.lo_inode; 1674 info64->lo_rdevice = info.lo_rdevice; 1675 info64->lo_offset = info.lo_offset; 1676 info64->lo_sizelimit = 0; 1677 info64->lo_encrypt_type = info.lo_encrypt_type; 1678 info64->lo_encrypt_key_size = info.lo_encrypt_key_size; 1679 info64->lo_flags = info.lo_flags; 1680 info64->lo_init[0] = info.lo_init[0]; 1681 info64->lo_init[1] = info.lo_init[1]; 1682 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1683 memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE); 1684 else 1685 memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE); 1686 memcpy(info64->lo_encrypt_key, info.lo_encrypt_key, LO_KEY_SIZE); 1687 return 0; 1688 } 1689 1690 /* 1691 * Transfer 64-bit loop info to 32-bit compatibility structure in userspace 1692 * - noinlined to reduce stack space usage in main part of driver 1693 */ 1694 static noinline int 1695 loop_info64_to_compat(const struct loop_info64 *info64, 1696 struct compat_loop_info __user *arg) 1697 { 1698 struct compat_loop_info info; 1699 1700 memset(&info, 0, sizeof(info)); 1701 info.lo_number = info64->lo_number; 1702 info.lo_device = info64->lo_device; 1703 info.lo_inode = info64->lo_inode; 1704 info.lo_rdevice = info64->lo_rdevice; 1705 info.lo_offset = info64->lo_offset; 1706 info.lo_encrypt_type = info64->lo_encrypt_type; 1707 info.lo_encrypt_key_size = info64->lo_encrypt_key_size; 1708 info.lo_flags = info64->lo_flags; 1709 info.lo_init[0] = info64->lo_init[0]; 1710 info.lo_init[1] = info64->lo_init[1]; 1711 if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI) 1712 memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE); 1713 else 1714 memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE); 1715 memcpy(info.lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE); 1716 1717 /* error in case values were truncated */ 1718 if (info.lo_device != info64->lo_device || 1719 info.lo_rdevice != info64->lo_rdevice || 1720 info.lo_inode != info64->lo_inode || 1721 info.lo_offset != info64->lo_offset || 1722 info.lo_init[0] != info64->lo_init[0] || 1723 info.lo_init[1] != info64->lo_init[1]) 1724 return -EOVERFLOW; 1725 1726 if (copy_to_user(arg, &info, sizeof(info))) 1727 return -EFAULT; 1728 return 0; 1729 } 1730 1731 static int 1732 loop_set_status_compat(struct loop_device *lo, 1733 const struct compat_loop_info __user *arg) 1734 { 1735 struct loop_info64 info64; 1736 int ret; 1737 1738 ret = loop_info64_from_compat(arg, &info64); 1739 if (ret < 0) 1740 return ret; 1741 return loop_set_status(lo, &info64); 1742 } 1743 1744 static int 1745 loop_get_status_compat(struct loop_device *lo, 1746 struct compat_loop_info __user *arg) 1747 { 1748 struct loop_info64 info64; 1749 int err; 1750 1751 if (!arg) 1752 return -EINVAL; 1753 err = loop_get_status(lo, &info64); 1754 if (!err) 1755 err = loop_info64_to_compat(&info64, arg); 1756 return err; 1757 } 1758 1759 static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, 1760 unsigned int cmd, unsigned long arg) 1761 { 1762 struct loop_device *lo = bdev->bd_disk->private_data; 1763 int err; 1764 1765 switch(cmd) { 1766 case LOOP_SET_STATUS: 1767 err = loop_set_status_compat(lo, 1768 (const struct compat_loop_info __user *)arg); 1769 break; 1770 case LOOP_GET_STATUS: 1771 err = loop_get_status_compat(lo, 1772 (struct compat_loop_info __user *)arg); 1773 break; 1774 case LOOP_SET_CAPACITY: 1775 case LOOP_CLR_FD: 1776 case LOOP_GET_STATUS64: 1777 case LOOP_SET_STATUS64: 1778 arg = (unsigned long) compat_ptr(arg); 1779 /* fall through */ 1780 case LOOP_SET_FD: 1781 case LOOP_CHANGE_FD: 1782 case LOOP_SET_BLOCK_SIZE: 1783 case LOOP_SET_DIRECT_IO: 1784 err = lo_ioctl(bdev, mode, cmd, arg); 1785 break; 1786 default: 1787 err = -ENOIOCTLCMD; 1788 break; 1789 } 1790 return err; 1791 } 1792 #endif 1793 1794 static int lo_open(struct block_device *bdev, fmode_t mode) 1795 { 1796 struct loop_device *lo; 1797 int err; 1798 1799 err = mutex_lock_killable(&loop_ctl_mutex); 1800 if (err) 1801 return err; 1802 lo = bdev->bd_disk->private_data; 1803 if (!lo) { 1804 err = -ENXIO; 1805 goto out; 1806 } 1807 1808 atomic_inc(&lo->lo_refcnt); 1809 out: 1810 mutex_unlock(&loop_ctl_mutex); 1811 return err; 1812 } 1813 1814 static void lo_release(struct gendisk *disk, fmode_t mode) 1815 { 1816 struct loop_device *lo; 1817 1818 mutex_lock(&loop_ctl_mutex); 1819 lo = disk->private_data; 1820 if (atomic_dec_return(&lo->lo_refcnt)) 1821 goto out_unlock; 1822 1823 if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { 1824 if (lo->lo_state != Lo_bound) 1825 goto out_unlock; 1826 lo->lo_state = Lo_rundown; 1827 mutex_unlock(&loop_ctl_mutex); 1828 /* 1829 * In autoclear mode, stop the loop thread 1830 * and remove configuration after last close. 1831 */ 1832 __loop_clr_fd(lo, true); 1833 return; 1834 } else if (lo->lo_state == Lo_bound) { 1835 /* 1836 * Otherwise keep thread (if running) and config, 1837 * but flush possible ongoing bios in thread. 1838 */ 1839 blk_mq_freeze_queue(lo->lo_queue); 1840 blk_mq_unfreeze_queue(lo->lo_queue); 1841 } 1842 1843 out_unlock: 1844 mutex_unlock(&loop_ctl_mutex); 1845 } 1846 1847 static const struct block_device_operations lo_fops = { 1848 .owner = THIS_MODULE, 1849 .open = lo_open, 1850 .release = lo_release, 1851 .ioctl = lo_ioctl, 1852 #ifdef CONFIG_COMPAT 1853 .compat_ioctl = lo_compat_ioctl, 1854 #endif 1855 }; 1856 1857 /* 1858 * And now the modules code and kernel interface. 1859 */ 1860 static int max_loop; 1861 module_param(max_loop, int, 0444); 1862 MODULE_PARM_DESC(max_loop, "Maximum number of loop devices"); 1863 module_param(max_part, int, 0444); 1864 MODULE_PARM_DESC(max_part, "Maximum number of partitions per loop device"); 1865 MODULE_LICENSE("GPL"); 1866 MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR); 1867 1868 int loop_register_transfer(struct loop_func_table *funcs) 1869 { 1870 unsigned int n = funcs->number; 1871 1872 if (n >= MAX_LO_CRYPT || xfer_funcs[n]) 1873 return -EINVAL; 1874 xfer_funcs[n] = funcs; 1875 return 0; 1876 } 1877 1878 static int unregister_transfer_cb(int id, void *ptr, void *data) 1879 { 1880 struct loop_device *lo = ptr; 1881 struct loop_func_table *xfer = data; 1882 1883 mutex_lock(&loop_ctl_mutex); 1884 if (lo->lo_encryption == xfer) 1885 loop_release_xfer(lo); 1886 mutex_unlock(&loop_ctl_mutex); 1887 return 0; 1888 } 1889 1890 int loop_unregister_transfer(int number) 1891 { 1892 unsigned int n = number; 1893 struct loop_func_table *xfer; 1894 1895 if (n == 0 || n >= MAX_LO_CRYPT || (xfer = xfer_funcs[n]) == NULL) 1896 return -EINVAL; 1897 1898 xfer_funcs[n] = NULL; 1899 idr_for_each(&loop_index_idr, &unregister_transfer_cb, xfer); 1900 return 0; 1901 } 1902 1903 EXPORT_SYMBOL(loop_register_transfer); 1904 EXPORT_SYMBOL(loop_unregister_transfer); 1905 1906 static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, 1907 const struct blk_mq_queue_data *bd) 1908 { 1909 struct request *rq = bd->rq; 1910 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 1911 struct loop_device *lo = rq->q->queuedata; 1912 1913 blk_mq_start_request(rq); 1914 1915 if (lo->lo_state != Lo_bound) 1916 return BLK_STS_IOERR; 1917 1918 switch (req_op(rq)) { 1919 case REQ_OP_FLUSH: 1920 case REQ_OP_DISCARD: 1921 case REQ_OP_WRITE_ZEROES: 1922 cmd->use_aio = false; 1923 break; 1924 default: 1925 cmd->use_aio = lo->use_dio; 1926 break; 1927 } 1928 1929 /* always use the first bio's css */ 1930 #ifdef CONFIG_BLK_CGROUP 1931 if (cmd->use_aio && rq->bio && rq->bio->bi_blkg) { 1932 cmd->css = &bio_blkcg(rq->bio)->css; 1933 css_get(cmd->css); 1934 } else 1935 #endif 1936 cmd->css = NULL; 1937 kthread_queue_work(&lo->worker, &cmd->work); 1938 1939 return BLK_STS_OK; 1940 } 1941 1942 static void loop_handle_cmd(struct loop_cmd *cmd) 1943 { 1944 struct request *rq = blk_mq_rq_from_pdu(cmd); 1945 const bool write = op_is_write(req_op(rq)); 1946 struct loop_device *lo = rq->q->queuedata; 1947 int ret = 0; 1948 1949 if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { 1950 ret = -EIO; 1951 goto failed; 1952 } 1953 1954 ret = do_req_filebacked(lo, rq); 1955 failed: 1956 /* complete non-aio request */ 1957 if (!cmd->use_aio || ret) { 1958 cmd->ret = ret ? -EIO : 0; 1959 blk_mq_complete_request(rq); 1960 } 1961 } 1962 1963 static void loop_queue_work(struct kthread_work *work) 1964 { 1965 struct loop_cmd *cmd = 1966 container_of(work, struct loop_cmd, work); 1967 1968 loop_handle_cmd(cmd); 1969 } 1970 1971 static int loop_init_request(struct blk_mq_tag_set *set, struct request *rq, 1972 unsigned int hctx_idx, unsigned int numa_node) 1973 { 1974 struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); 1975 1976 kthread_init_work(&cmd->work, loop_queue_work); 1977 return 0; 1978 } 1979 1980 static const struct blk_mq_ops loop_mq_ops = { 1981 .queue_rq = loop_queue_rq, 1982 .init_request = loop_init_request, 1983 .complete = lo_complete_rq, 1984 }; 1985 1986 static int loop_add(struct loop_device **l, int i) 1987 { 1988 struct loop_device *lo; 1989 struct gendisk *disk; 1990 int err; 1991 1992 err = -ENOMEM; 1993 lo = kzalloc(sizeof(*lo), GFP_KERNEL); 1994 if (!lo) 1995 goto out; 1996 1997 lo->lo_state = Lo_unbound; 1998 1999 /* allocate id, if @id >= 0, we're requesting that specific id */ 2000 if (i >= 0) { 2001 err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); 2002 if (err == -ENOSPC) 2003 err = -EEXIST; 2004 } else { 2005 err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); 2006 } 2007 if (err < 0) 2008 goto out_free_dev; 2009 i = err; 2010 2011 err = -ENOMEM; 2012 lo->tag_set.ops = &loop_mq_ops; 2013 lo->tag_set.nr_hw_queues = 1; 2014 lo->tag_set.queue_depth = 128; 2015 lo->tag_set.numa_node = NUMA_NO_NODE; 2016 lo->tag_set.cmd_size = sizeof(struct loop_cmd); 2017 lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 2018 lo->tag_set.driver_data = lo; 2019 2020 err = blk_mq_alloc_tag_set(&lo->tag_set); 2021 if (err) 2022 goto out_free_idr; 2023 2024 lo->lo_queue = blk_mq_init_queue(&lo->tag_set); 2025 if (IS_ERR(lo->lo_queue)) { 2026 err = PTR_ERR(lo->lo_queue); 2027 goto out_cleanup_tags; 2028 } 2029 lo->lo_queue->queuedata = lo; 2030 2031 blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS); 2032 2033 /* 2034 * By default, we do buffer IO, so it doesn't make sense to enable 2035 * merge because the I/O submitted to backing file is handled page by 2036 * page. For directio mode, merge does help to dispatch bigger request 2037 * to underlayer disk. We will enable merge once directio is enabled. 2038 */ 2039 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, lo->lo_queue); 2040 2041 err = -ENOMEM; 2042 disk = lo->lo_disk = alloc_disk(1 << part_shift); 2043 if (!disk) 2044 goto out_free_queue; 2045 2046 /* 2047 * Disable partition scanning by default. The in-kernel partition 2048 * scanning can be requested individually per-device during its 2049 * setup. Userspace can always add and remove partitions from all 2050 * devices. The needed partition minors are allocated from the 2051 * extended minor space, the main loop device numbers will continue 2052 * to match the loop minors, regardless of the number of partitions 2053 * used. 2054 * 2055 * If max_part is given, partition scanning is globally enabled for 2056 * all loop devices. The minors for the main loop devices will be 2057 * multiples of max_part. 2058 * 2059 * Note: Global-for-all-devices, set-only-at-init, read-only module 2060 * parameteters like 'max_loop' and 'max_part' make things needlessly 2061 * complicated, are too static, inflexible and may surprise 2062 * userspace tools. Parameters like this in general should be avoided. 2063 */ 2064 if (!part_shift) 2065 disk->flags |= GENHD_FL_NO_PART_SCAN; 2066 disk->flags |= GENHD_FL_EXT_DEVT; 2067 atomic_set(&lo->lo_refcnt, 0); 2068 lo->lo_number = i; 2069 spin_lock_init(&lo->lo_lock); 2070 disk->major = LOOP_MAJOR; 2071 disk->first_minor = i << part_shift; 2072 disk->fops = &lo_fops; 2073 disk->private_data = lo; 2074 disk->queue = lo->lo_queue; 2075 sprintf(disk->disk_name, "loop%d", i); 2076 add_disk(disk); 2077 *l = lo; 2078 return lo->lo_number; 2079 2080 out_free_queue: 2081 blk_cleanup_queue(lo->lo_queue); 2082 out_cleanup_tags: 2083 blk_mq_free_tag_set(&lo->tag_set); 2084 out_free_idr: 2085 idr_remove(&loop_index_idr, i); 2086 out_free_dev: 2087 kfree(lo); 2088 out: 2089 return err; 2090 } 2091 2092 static void loop_remove(struct loop_device *lo) 2093 { 2094 del_gendisk(lo->lo_disk); 2095 blk_cleanup_queue(lo->lo_queue); 2096 blk_mq_free_tag_set(&lo->tag_set); 2097 put_disk(lo->lo_disk); 2098 kfree(lo); 2099 } 2100 2101 static int find_free_cb(int id, void *ptr, void *data) 2102 { 2103 struct loop_device *lo = ptr; 2104 struct loop_device **l = data; 2105 2106 if (lo->lo_state == Lo_unbound) { 2107 *l = lo; 2108 return 1; 2109 } 2110 return 0; 2111 } 2112 2113 static int loop_lookup(struct loop_device **l, int i) 2114 { 2115 struct loop_device *lo; 2116 int ret = -ENODEV; 2117 2118 if (i < 0) { 2119 int err; 2120 2121 err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); 2122 if (err == 1) { 2123 *l = lo; 2124 ret = lo->lo_number; 2125 } 2126 goto out; 2127 } 2128 2129 /* lookup and return a specific i */ 2130 lo = idr_find(&loop_index_idr, i); 2131 if (lo) { 2132 *l = lo; 2133 ret = lo->lo_number; 2134 } 2135 out: 2136 return ret; 2137 } 2138 2139 static struct kobject *loop_probe(dev_t dev, int *part, void *data) 2140 { 2141 struct loop_device *lo; 2142 struct kobject *kobj; 2143 int err; 2144 2145 mutex_lock(&loop_ctl_mutex); 2146 err = loop_lookup(&lo, MINOR(dev) >> part_shift); 2147 if (err < 0) 2148 err = loop_add(&lo, MINOR(dev) >> part_shift); 2149 if (err < 0) 2150 kobj = NULL; 2151 else 2152 kobj = get_disk_and_module(lo->lo_disk); 2153 mutex_unlock(&loop_ctl_mutex); 2154 2155 *part = 0; 2156 return kobj; 2157 } 2158 2159 static long loop_control_ioctl(struct file *file, unsigned int cmd, 2160 unsigned long parm) 2161 { 2162 struct loop_device *lo; 2163 int ret; 2164 2165 ret = mutex_lock_killable(&loop_ctl_mutex); 2166 if (ret) 2167 return ret; 2168 2169 ret = -ENOSYS; 2170 switch (cmd) { 2171 case LOOP_CTL_ADD: 2172 ret = loop_lookup(&lo, parm); 2173 if (ret >= 0) { 2174 ret = -EEXIST; 2175 break; 2176 } 2177 ret = loop_add(&lo, parm); 2178 break; 2179 case LOOP_CTL_REMOVE: 2180 ret = loop_lookup(&lo, parm); 2181 if (ret < 0) 2182 break; 2183 if (lo->lo_state != Lo_unbound) { 2184 ret = -EBUSY; 2185 break; 2186 } 2187 if (atomic_read(&lo->lo_refcnt) > 0) { 2188 ret = -EBUSY; 2189 break; 2190 } 2191 lo->lo_disk->private_data = NULL; 2192 idr_remove(&loop_index_idr, lo->lo_number); 2193 loop_remove(lo); 2194 break; 2195 case LOOP_CTL_GET_FREE: 2196 ret = loop_lookup(&lo, -1); 2197 if (ret >= 0) 2198 break; 2199 ret = loop_add(&lo, -1); 2200 } 2201 mutex_unlock(&loop_ctl_mutex); 2202 2203 return ret; 2204 } 2205 2206 static const struct file_operations loop_ctl_fops = { 2207 .open = nonseekable_open, 2208 .unlocked_ioctl = loop_control_ioctl, 2209 .compat_ioctl = loop_control_ioctl, 2210 .owner = THIS_MODULE, 2211 .llseek = noop_llseek, 2212 }; 2213 2214 static struct miscdevice loop_misc = { 2215 .minor = LOOP_CTRL_MINOR, 2216 .name = "loop-control", 2217 .fops = &loop_ctl_fops, 2218 }; 2219 2220 MODULE_ALIAS_MISCDEV(LOOP_CTRL_MINOR); 2221 MODULE_ALIAS("devname:loop-control"); 2222 2223 static int __init loop_init(void) 2224 { 2225 int i, nr; 2226 unsigned long range; 2227 struct loop_device *lo; 2228 int err; 2229 2230 part_shift = 0; 2231 if (max_part > 0) { 2232 part_shift = fls(max_part); 2233 2234 /* 2235 * Adjust max_part according to part_shift as it is exported 2236 * to user space so that user can decide correct minor number 2237 * if [s]he want to create more devices. 2238 * 2239 * Note that -1 is required because partition 0 is reserved 2240 * for the whole disk. 2241 */ 2242 max_part = (1UL << part_shift) - 1; 2243 } 2244 2245 if ((1UL << part_shift) > DISK_MAX_PARTS) { 2246 err = -EINVAL; 2247 goto err_out; 2248 } 2249 2250 if (max_loop > 1UL << (MINORBITS - part_shift)) { 2251 err = -EINVAL; 2252 goto err_out; 2253 } 2254 2255 /* 2256 * If max_loop is specified, create that many devices upfront. 2257 * This also becomes a hard limit. If max_loop is not specified, 2258 * create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module 2259 * init time. Loop devices can be requested on-demand with the 2260 * /dev/loop-control interface, or be instantiated by accessing 2261 * a 'dead' device node. 2262 */ 2263 if (max_loop) { 2264 nr = max_loop; 2265 range = max_loop << part_shift; 2266 } else { 2267 nr = CONFIG_BLK_DEV_LOOP_MIN_COUNT; 2268 range = 1UL << MINORBITS; 2269 } 2270 2271 err = misc_register(&loop_misc); 2272 if (err < 0) 2273 goto err_out; 2274 2275 2276 if (register_blkdev(LOOP_MAJOR, "loop")) { 2277 err = -EIO; 2278 goto misc_out; 2279 } 2280 2281 blk_register_region(MKDEV(LOOP_MAJOR, 0), range, 2282 THIS_MODULE, loop_probe, NULL, NULL); 2283 2284 /* pre-create number of devices given by config or max_loop */ 2285 mutex_lock(&loop_ctl_mutex); 2286 for (i = 0; i < nr; i++) 2287 loop_add(&lo, i); 2288 mutex_unlock(&loop_ctl_mutex); 2289 2290 printk(KERN_INFO "loop: module loaded\n"); 2291 return 0; 2292 2293 misc_out: 2294 misc_deregister(&loop_misc); 2295 err_out: 2296 return err; 2297 } 2298 2299 static int loop_exit_cb(int id, void *ptr, void *data) 2300 { 2301 struct loop_device *lo = ptr; 2302 2303 loop_remove(lo); 2304 return 0; 2305 } 2306 2307 static void __exit loop_exit(void) 2308 { 2309 unsigned long range; 2310 2311 range = max_loop ? max_loop << part_shift : 1UL << MINORBITS; 2312 2313 idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); 2314 idr_destroy(&loop_index_idr); 2315 2316 blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); 2317 unregister_blkdev(LOOP_MAJOR, "loop"); 2318 2319 misc_deregister(&loop_misc); 2320 } 2321 2322 module_init(loop_init); 2323 module_exit(loop_exit); 2324 2325 #ifndef MODULE 2326 static int __init max_loop_setup(char *str) 2327 { 2328 max_loop = simple_strtol(str, NULL, 0); 2329 return 1; 2330 } 2331 2332 __setup("max_loop=", max_loop_setup); 2333 #endif 2334