diff options
Diffstat (limited to 'drivers/block/loop.c')
-rw-r--r-- | drivers/block/loop.c | 416 |
1 files changed, 204 insertions, 212 deletions
diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 6cb1beb47c25..d1f168b73634 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c | |||
@@ -85,6 +85,8 @@ static DEFINE_MUTEX(loop_index_mutex); | |||
85 | static int max_part; | 85 | static int max_part; |
86 | static int part_shift; | 86 | static int part_shift; |
87 | 87 | ||
88 | static struct workqueue_struct *loop_wq; | ||
89 | |||
88 | /* | 90 | /* |
89 | * Transfer functions | 91 | * Transfer functions |
90 | */ | 92 | */ |
@@ -284,12 +286,12 @@ static int do_lo_send_write(struct loop_device *lo, struct bio_vec *bvec, | |||
284 | return ret; | 286 | return ret; |
285 | } | 287 | } |
286 | 288 | ||
287 | static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos) | 289 | static int lo_send(struct loop_device *lo, struct request *rq, loff_t pos) |
288 | { | 290 | { |
289 | int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t, | 291 | int (*do_lo_send)(struct loop_device *, struct bio_vec *, loff_t, |
290 | struct page *page); | 292 | struct page *page); |
291 | struct bio_vec bvec; | 293 | struct bio_vec bvec; |
292 | struct bvec_iter iter; | 294 | struct req_iterator iter; |
293 | struct page *page = NULL; | 295 | struct page *page = NULL; |
294 | int ret = 0; | 296 | int ret = 0; |
295 | 297 | ||
@@ -303,7 +305,7 @@ static int lo_send(struct loop_device *lo, struct bio *bio, loff_t pos) | |||
303 | do_lo_send = do_lo_send_direct_write; | 305 | do_lo_send = do_lo_send_direct_write; |
304 | } | 306 | } |
305 | 307 | ||
306 | bio_for_each_segment(bvec, bio, iter) { | 308 | rq_for_each_segment(bvec, rq, iter) { |
307 | ret = do_lo_send(lo, &bvec, pos, page); | 309 | ret = do_lo_send(lo, &bvec, pos, page); |
308 | if (ret < 0) | 310 | if (ret < 0) |
309 | break; | 311 | break; |
@@ -391,19 +393,22 @@ do_lo_receive(struct loop_device *lo, | |||
391 | } | 393 | } |
392 | 394 | ||
393 | static int | 395 | static int |
394 | lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos) | 396 | lo_receive(struct loop_device *lo, struct request *rq, int bsize, loff_t pos) |
395 | { | 397 | { |
396 | struct bio_vec bvec; | 398 | struct bio_vec bvec; |
397 | struct bvec_iter iter; | 399 | struct req_iterator iter; |
398 | ssize_t s; | 400 | ssize_t s; |
399 | 401 | ||
400 | bio_for_each_segment(bvec, bio, iter) { | 402 | rq_for_each_segment(bvec, rq, iter) { |
401 | s = do_lo_receive(lo, &bvec, bsize, pos); | 403 | s = do_lo_receive(lo, &bvec, bsize, pos); |
402 | if (s < 0) | 404 | if (s < 0) |
403 | return s; | 405 | return s; |
404 | 406 | ||
405 | if (s != bvec.bv_len) { | 407 | if (s != bvec.bv_len) { |
406 | zero_fill_bio(bio); | 408 | struct bio *bio; |
409 | |||
410 | __rq_for_each_bio(bio, rq) | ||
411 | zero_fill_bio(bio); | ||
407 | break; | 412 | break; |
408 | } | 413 | } |
409 | pos += bvec.bv_len; | 414 | pos += bvec.bv_len; |
@@ -411,106 +416,58 @@ lo_receive(struct loop_device *lo, struct bio *bio, int bsize, loff_t pos) | |||
411 | return 0; | 416 | return 0; |
412 | } | 417 | } |
413 | 418 | ||
414 | static int do_bio_filebacked(struct loop_device *lo, struct bio *bio) | 419 | static int lo_discard(struct loop_device *lo, struct request *rq, loff_t pos) |
415 | { | 420 | { |
416 | loff_t pos; | 421 | /* |
422 | * We use punch hole to reclaim the free space used by the | ||
423 | * image a.k.a. discard. However we do not support discard if | ||
424 | * encryption is enabled, because it may give an attacker | ||
425 | * useful information. | ||
426 | */ | ||
427 | struct file *file = lo->lo_backing_file; | ||
428 | int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; | ||
417 | int ret; | 429 | int ret; |
418 | 430 | ||
419 | pos = ((loff_t) bio->bi_iter.bi_sector << 9) + lo->lo_offset; | 431 | if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { |
420 | 432 | ret = -EOPNOTSUPP; | |
421 | if (bio_rw(bio) == WRITE) { | 433 | goto out; |
422 | struct file *file = lo->lo_backing_file; | 434 | } |
423 | |||
424 | if (bio->bi_rw & REQ_FLUSH) { | ||
425 | ret = vfs_fsync(file, 0); | ||
426 | if (unlikely(ret && ret != -EINVAL)) { | ||
427 | ret = -EIO; | ||
428 | goto out; | ||
429 | } | ||
430 | } | ||
431 | |||
432 | /* | ||
433 | * We use punch hole to reclaim the free space used by the | ||
434 | * image a.k.a. discard. However we do not support discard if | ||
435 | * encryption is enabled, because it may give an attacker | ||
436 | * useful information. | ||
437 | */ | ||
438 | if (bio->bi_rw & REQ_DISCARD) { | ||
439 | struct file *file = lo->lo_backing_file; | ||
440 | int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; | ||
441 | |||
442 | if ((!file->f_op->fallocate) || | ||
443 | lo->lo_encrypt_key_size) { | ||
444 | ret = -EOPNOTSUPP; | ||
445 | goto out; | ||
446 | } | ||
447 | ret = file->f_op->fallocate(file, mode, pos, | ||
448 | bio->bi_iter.bi_size); | ||
449 | if (unlikely(ret && ret != -EINVAL && | ||
450 | ret != -EOPNOTSUPP)) | ||
451 | ret = -EIO; | ||
452 | goto out; | ||
453 | } | ||
454 | |||
455 | ret = lo_send(lo, bio, pos); | ||
456 | |||
457 | if ((bio->bi_rw & REQ_FUA) && !ret) { | ||
458 | ret = vfs_fsync(file, 0); | ||
459 | if (unlikely(ret && ret != -EINVAL)) | ||
460 | ret = -EIO; | ||
461 | } | ||
462 | } else | ||
463 | ret = lo_receive(lo, bio, lo->lo_blocksize, pos); | ||
464 | 435 | ||
465 | out: | 436 | ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); |
437 | if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP)) | ||
438 | ret = -EIO; | ||
439 | out: | ||
466 | return ret; | 440 | return ret; |
467 | } | 441 | } |
468 | 442 | ||
469 | /* | 443 | static int lo_req_flush(struct loop_device *lo, struct request *rq) |
470 | * Add bio to back of pending list | ||
471 | */ | ||
472 | static void loop_add_bio(struct loop_device *lo, struct bio *bio) | ||
473 | { | 444 | { |
474 | lo->lo_bio_count++; | 445 | struct file *file = lo->lo_backing_file; |
475 | bio_list_add(&lo->lo_bio_list, bio); | 446 | int ret = vfs_fsync(file, 0); |
476 | } | 447 | if (unlikely(ret && ret != -EINVAL)) |
448 | ret = -EIO; | ||
477 | 449 | ||
478 | /* | 450 | return ret; |
479 | * Grab first pending buffer | ||
480 | */ | ||
481 | static struct bio *loop_get_bio(struct loop_device *lo) | ||
482 | { | ||
483 | lo->lo_bio_count--; | ||
484 | return bio_list_pop(&lo->lo_bio_list); | ||
485 | } | 451 | } |
486 | 452 | ||
487 | static void loop_make_request(struct request_queue *q, struct bio *old_bio) | 453 | static int do_req_filebacked(struct loop_device *lo, struct request *rq) |
488 | { | 454 | { |
489 | struct loop_device *lo = q->queuedata; | 455 | loff_t pos; |
490 | int rw = bio_rw(old_bio); | 456 | int ret; |
491 | |||
492 | if (rw == READA) | ||
493 | rw = READ; | ||
494 | 457 | ||
495 | BUG_ON(!lo || (rw != READ && rw != WRITE)); | 458 | pos = ((loff_t) blk_rq_pos(rq) << 9) + lo->lo_offset; |
496 | 459 | ||
497 | spin_lock_irq(&lo->lo_lock); | 460 | if (rq->cmd_flags & REQ_WRITE) { |
498 | if (lo->lo_state != Lo_bound) | 461 | if (rq->cmd_flags & REQ_FLUSH) |
499 | goto out; | 462 | ret = lo_req_flush(lo, rq); |
500 | if (unlikely(rw == WRITE && (lo->lo_flags & LO_FLAGS_READ_ONLY))) | 463 | else if (rq->cmd_flags & REQ_DISCARD) |
501 | goto out; | 464 | ret = lo_discard(lo, rq, pos); |
502 | if (lo->lo_bio_count >= q->nr_congestion_on) | 465 | else |
503 | wait_event_lock_irq(lo->lo_req_wait, | 466 | ret = lo_send(lo, rq, pos); |
504 | lo->lo_bio_count < q->nr_congestion_off, | 467 | } else |
505 | lo->lo_lock); | 468 | ret = lo_receive(lo, rq, lo->lo_blocksize, pos); |
506 | loop_add_bio(lo, old_bio); | ||
507 | wake_up(&lo->lo_event); | ||
508 | spin_unlock_irq(&lo->lo_lock); | ||
509 | return; | ||
510 | 469 | ||
511 | out: | 470 | return ret; |
512 | spin_unlock_irq(&lo->lo_lock); | ||
513 | bio_io_error(old_bio); | ||
514 | } | 471 | } |
515 | 472 | ||
516 | struct switch_request { | 473 | struct switch_request { |
@@ -518,57 +475,26 @@ struct switch_request { | |||
518 | struct completion wait; | 475 | struct completion wait; |
519 | }; | 476 | }; |
520 | 477 | ||
521 | static void do_loop_switch(struct loop_device *, struct switch_request *); | ||
522 | |||
523 | static inline void loop_handle_bio(struct loop_device *lo, struct bio *bio) | ||
524 | { | ||
525 | if (unlikely(!bio->bi_bdev)) { | ||
526 | do_loop_switch(lo, bio->bi_private); | ||
527 | bio_put(bio); | ||
528 | } else { | ||
529 | int ret = do_bio_filebacked(lo, bio); | ||
530 | bio_endio(bio, ret); | ||
531 | } | ||
532 | } | ||
533 | |||
534 | /* | 478 | /* |
535 | * worker thread that handles reads/writes to file backed loop devices, | 479 | * Do the actual switch; called from the BIO completion routine |
536 | * to avoid blocking in our make_request_fn. it also does loop decrypting | ||
537 | * on reads for block backed loop, as that is too heavy to do from | ||
538 | * b_end_io context where irqs may be disabled. | ||
539 | * | ||
540 | * Loop explanation: loop_clr_fd() sets lo_state to Lo_rundown before | ||
541 | * calling kthread_stop(). Therefore once kthread_should_stop() is | ||
542 | * true, make_request will not place any more requests. Therefore | ||
543 | * once kthread_should_stop() is true and lo_bio is NULL, we are | ||
544 | * done with the loop. | ||
545 | */ | 480 | */ |
546 | static int loop_thread(void *data) | 481 | static void do_loop_switch(struct loop_device *lo, struct switch_request *p) |
547 | { | 482 | { |
548 | struct loop_device *lo = data; | 483 | struct file *file = p->file; |
549 | struct bio *bio; | 484 | struct file *old_file = lo->lo_backing_file; |
550 | 485 | struct address_space *mapping; | |
551 | set_user_nice(current, MIN_NICE); | ||
552 | |||
553 | while (!kthread_should_stop() || !bio_list_empty(&lo->lo_bio_list)) { | ||
554 | |||
555 | wait_event_interruptible(lo->lo_event, | ||
556 | !bio_list_empty(&lo->lo_bio_list) || | ||
557 | kthread_should_stop()); | ||
558 | |||
559 | if (bio_list_empty(&lo->lo_bio_list)) | ||
560 | continue; | ||
561 | spin_lock_irq(&lo->lo_lock); | ||
562 | bio = loop_get_bio(lo); | ||
563 | if (lo->lo_bio_count < lo->lo_queue->nr_congestion_off) | ||
564 | wake_up(&lo->lo_req_wait); | ||
565 | spin_unlock_irq(&lo->lo_lock); | ||
566 | 486 | ||
567 | BUG_ON(!bio); | 487 | /* if no new file, only flush of queued bios requested */ |
568 | loop_handle_bio(lo, bio); | 488 | if (!file) |
569 | } | 489 | return; |
570 | 490 | ||
571 | return 0; | 491 | mapping = file->f_mapping; |
492 | mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); | ||
493 | lo->lo_backing_file = file; | ||
494 | lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? | ||
495 | mapping->host->i_bdev->bd_block_size : PAGE_SIZE; | ||
496 | lo->old_gfp_mask = mapping_gfp_mask(mapping); | ||
497 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); | ||
572 | } | 498 | } |
573 | 499 | ||
574 | /* | 500 | /* |
@@ -579,15 +505,18 @@ static int loop_thread(void *data) | |||
579 | static int loop_switch(struct loop_device *lo, struct file *file) | 505 | static int loop_switch(struct loop_device *lo, struct file *file) |
580 | { | 506 | { |
581 | struct switch_request w; | 507 | struct switch_request w; |
582 | struct bio *bio = bio_alloc(GFP_KERNEL, 0); | 508 | |
583 | if (!bio) | ||
584 | return -ENOMEM; | ||
585 | init_completion(&w.wait); | ||
586 | w.file = file; | 509 | w.file = file; |
587 | bio->bi_private = &w; | 510 | |
588 | bio->bi_bdev = NULL; | 511 | /* freeze queue and wait for completion of scheduled requests */ |
589 | loop_make_request(lo->lo_queue, bio); | 512 | blk_mq_freeze_queue(lo->lo_queue); |
590 | wait_for_completion(&w.wait); | 513 | |
514 | /* do the switch action */ | ||
515 | do_loop_switch(lo, &w); | ||
516 | |||
517 | /* unfreeze */ | ||
518 | blk_mq_unfreeze_queue(lo->lo_queue); | ||
519 | |||
591 | return 0; | 520 | return 0; |
592 | } | 521 | } |
593 | 522 | ||
@@ -596,39 +525,10 @@ static int loop_switch(struct loop_device *lo, struct file *file) | |||
596 | */ | 525 | */ |
597 | static int loop_flush(struct loop_device *lo) | 526 | static int loop_flush(struct loop_device *lo) |
598 | { | 527 | { |
599 | /* loop not yet configured, no running thread, nothing to flush */ | ||
600 | if (!lo->lo_thread) | ||
601 | return 0; | ||
602 | |||
603 | return loop_switch(lo, NULL); | 528 | return loop_switch(lo, NULL); |
604 | } | 529 | } |
605 | 530 | ||
606 | /* | 531 | /* |
607 | * Do the actual switch; called from the BIO completion routine | ||
608 | */ | ||
609 | static void do_loop_switch(struct loop_device *lo, struct switch_request *p) | ||
610 | { | ||
611 | struct file *file = p->file; | ||
612 | struct file *old_file = lo->lo_backing_file; | ||
613 | struct address_space *mapping; | ||
614 | |||
615 | /* if no new file, only flush of queued bios requested */ | ||
616 | if (!file) | ||
617 | goto out; | ||
618 | |||
619 | mapping = file->f_mapping; | ||
620 | mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); | ||
621 | lo->lo_backing_file = file; | ||
622 | lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ? | ||
623 | mapping->host->i_bdev->bd_block_size : PAGE_SIZE; | ||
624 | lo->old_gfp_mask = mapping_gfp_mask(mapping); | ||
625 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); | ||
626 | out: | ||
627 | complete(&p->wait); | ||
628 | } | ||
629 | |||
630 | |||
631 | /* | ||
632 | * loop_change_fd switched the backing store of a loopback device to | 532 | * loop_change_fd switched the backing store of a loopback device to |
633 | * a new file. This is useful for operating system installers to free up | 533 | * a new file. This is useful for operating system installers to free up |
634 | * the original file and in High Availability environments to switch to | 534 | * the original file and in High Availability environments to switch to |
@@ -889,12 +789,9 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, | |||
889 | lo->transfer = transfer_none; | 789 | lo->transfer = transfer_none; |
890 | lo->ioctl = NULL; | 790 | lo->ioctl = NULL; |
891 | lo->lo_sizelimit = 0; | 791 | lo->lo_sizelimit = 0; |
892 | lo->lo_bio_count = 0; | ||
893 | lo->old_gfp_mask = mapping_gfp_mask(mapping); | 792 | lo->old_gfp_mask = mapping_gfp_mask(mapping); |
894 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); | 793 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); |
895 | 794 | ||
896 | bio_list_init(&lo->lo_bio_list); | ||
897 | |||
898 | if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) | 795 | if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) |
899 | blk_queue_flush(lo->lo_queue, REQ_FLUSH); | 796 | blk_queue_flush(lo->lo_queue, REQ_FLUSH); |
900 | 797 | ||
@@ -906,14 +803,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, | |||
906 | 803 | ||
907 | set_blocksize(bdev, lo_blocksize); | 804 | set_blocksize(bdev, lo_blocksize); |
908 | 805 | ||
909 | lo->lo_thread = kthread_create(loop_thread, lo, "loop%d", | ||
910 | lo->lo_number); | ||
911 | if (IS_ERR(lo->lo_thread)) { | ||
912 | error = PTR_ERR(lo->lo_thread); | ||
913 | goto out_clr; | ||
914 | } | ||
915 | lo->lo_state = Lo_bound; | 806 | lo->lo_state = Lo_bound; |
916 | wake_up_process(lo->lo_thread); | ||
917 | if (part_shift) | 807 | if (part_shift) |
918 | lo->lo_flags |= LO_FLAGS_PARTSCAN; | 808 | lo->lo_flags |= LO_FLAGS_PARTSCAN; |
919 | if (lo->lo_flags & LO_FLAGS_PARTSCAN) | 809 | if (lo->lo_flags & LO_FLAGS_PARTSCAN) |
@@ -925,18 +815,6 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, | |||
925 | bdgrab(bdev); | 815 | bdgrab(bdev); |
926 | return 0; | 816 | return 0; |
927 | 817 | ||
928 | out_clr: | ||
929 | loop_sysfs_exit(lo); | ||
930 | lo->lo_thread = NULL; | ||
931 | lo->lo_device = NULL; | ||
932 | lo->lo_backing_file = NULL; | ||
933 | lo->lo_flags = 0; | ||
934 | set_capacity(lo->lo_disk, 0); | ||
935 | invalidate_bdev(bdev); | ||
936 | bd_set_size(bdev, 0); | ||
937 | kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); | ||
938 | mapping_set_gfp_mask(mapping, lo->old_gfp_mask); | ||
939 | lo->lo_state = Lo_unbound; | ||
940 | out_putf: | 818 | out_putf: |
941 | fput(file); | 819 | fput(file); |
942 | out: | 820 | out: |
@@ -1012,11 +890,6 @@ static int loop_clr_fd(struct loop_device *lo) | |||
1012 | 890 | ||
1013 | spin_lock_irq(&lo->lo_lock); | 891 | spin_lock_irq(&lo->lo_lock); |
1014 | lo->lo_state = Lo_rundown; | 892 | lo->lo_state = Lo_rundown; |
1015 | spin_unlock_irq(&lo->lo_lock); | ||
1016 | |||
1017 | kthread_stop(lo->lo_thread); | ||
1018 | |||
1019 | spin_lock_irq(&lo->lo_lock); | ||
1020 | lo->lo_backing_file = NULL; | 893 | lo->lo_backing_file = NULL; |
1021 | spin_unlock_irq(&lo->lo_lock); | 894 | spin_unlock_irq(&lo->lo_lock); |
1022 | 895 | ||
@@ -1028,7 +901,6 @@ static int loop_clr_fd(struct loop_device *lo) | |||
1028 | lo->lo_offset = 0; | 901 | lo->lo_offset = 0; |
1029 | lo->lo_sizelimit = 0; | 902 | lo->lo_sizelimit = 0; |
1030 | lo->lo_encrypt_key_size = 0; | 903 | lo->lo_encrypt_key_size = 0; |
1031 | lo->lo_thread = NULL; | ||
1032 | memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); | 904 | memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE); |
1033 | memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); | 905 | memset(lo->lo_crypt_name, 0, LO_NAME_SIZE); |
1034 | memset(lo->lo_file_name, 0, LO_NAME_SIZE); | 906 | memset(lo->lo_file_name, 0, LO_NAME_SIZE); |
@@ -1601,6 +1473,105 @@ int loop_unregister_transfer(int number) | |||
1601 | EXPORT_SYMBOL(loop_register_transfer); | 1473 | EXPORT_SYMBOL(loop_register_transfer); |
1602 | EXPORT_SYMBOL(loop_unregister_transfer); | 1474 | EXPORT_SYMBOL(loop_unregister_transfer); |
1603 | 1475 | ||
1476 | static int loop_queue_rq(struct blk_mq_hw_ctx *hctx, | ||
1477 | const struct blk_mq_queue_data *bd) | ||
1478 | { | ||
1479 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); | ||
1480 | |||
1481 | blk_mq_start_request(bd->rq); | ||
1482 | |||
1483 | if (cmd->rq->cmd_flags & REQ_WRITE) { | ||
1484 | struct loop_device *lo = cmd->rq->q->queuedata; | ||
1485 | bool need_sched = true; | ||
1486 | |||
1487 | spin_lock_irq(&lo->lo_lock); | ||
1488 | if (lo->write_started) | ||
1489 | need_sched = false; | ||
1490 | else | ||
1491 | lo->write_started = true; | ||
1492 | list_add_tail(&cmd->list, &lo->write_cmd_head); | ||
1493 | spin_unlock_irq(&lo->lo_lock); | ||
1494 | |||
1495 | if (need_sched) | ||
1496 | queue_work(loop_wq, &lo->write_work); | ||
1497 | } else { | ||
1498 | queue_work(loop_wq, &cmd->read_work); | ||
1499 | } | ||
1500 | |||
1501 | return BLK_MQ_RQ_QUEUE_OK; | ||
1502 | } | ||
1503 | |||
1504 | static void loop_handle_cmd(struct loop_cmd *cmd) | ||
1505 | { | ||
1506 | const bool write = cmd->rq->cmd_flags & REQ_WRITE; | ||
1507 | struct loop_device *lo = cmd->rq->q->queuedata; | ||
1508 | int ret = -EIO; | ||
1509 | |||
1510 | if (lo->lo_state != Lo_bound) | ||
1511 | goto failed; | ||
1512 | |||
1513 | if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) | ||
1514 | goto failed; | ||
1515 | |||
1516 | ret = do_req_filebacked(lo, cmd->rq); | ||
1517 | |||
1518 | failed: | ||
1519 | if (ret) | ||
1520 | cmd->rq->errors = -EIO; | ||
1521 | blk_mq_complete_request(cmd->rq); | ||
1522 | } | ||
1523 | |||
1524 | static void loop_queue_write_work(struct work_struct *work) | ||
1525 | { | ||
1526 | struct loop_device *lo = | ||
1527 | container_of(work, struct loop_device, write_work); | ||
1528 | LIST_HEAD(cmd_list); | ||
1529 | |||
1530 | spin_lock_irq(&lo->lo_lock); | ||
1531 | repeat: | ||
1532 | list_splice_init(&lo->write_cmd_head, &cmd_list); | ||
1533 | spin_unlock_irq(&lo->lo_lock); | ||
1534 | |||
1535 | while (!list_empty(&cmd_list)) { | ||
1536 | struct loop_cmd *cmd = list_first_entry(&cmd_list, | ||
1537 | struct loop_cmd, list); | ||
1538 | list_del_init(&cmd->list); | ||
1539 | loop_handle_cmd(cmd); | ||
1540 | } | ||
1541 | |||
1542 | spin_lock_irq(&lo->lo_lock); | ||
1543 | if (!list_empty(&lo->write_cmd_head)) | ||
1544 | goto repeat; | ||
1545 | lo->write_started = false; | ||
1546 | spin_unlock_irq(&lo->lo_lock); | ||
1547 | } | ||
1548 | |||
1549 | static void loop_queue_read_work(struct work_struct *work) | ||
1550 | { | ||
1551 | struct loop_cmd *cmd = | ||
1552 | container_of(work, struct loop_cmd, read_work); | ||
1553 | |||
1554 | loop_handle_cmd(cmd); | ||
1555 | } | ||
1556 | |||
1557 | static int loop_init_request(void *data, struct request *rq, | ||
1558 | unsigned int hctx_idx, unsigned int request_idx, | ||
1559 | unsigned int numa_node) | ||
1560 | { | ||
1561 | struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq); | ||
1562 | |||
1563 | cmd->rq = rq; | ||
1564 | INIT_WORK(&cmd->read_work, loop_queue_read_work); | ||
1565 | |||
1566 | return 0; | ||
1567 | } | ||
1568 | |||
1569 | static struct blk_mq_ops loop_mq_ops = { | ||
1570 | .queue_rq = loop_queue_rq, | ||
1571 | .map_queue = blk_mq_map_queue, | ||
1572 | .init_request = loop_init_request, | ||
1573 | }; | ||
1574 | |||
1604 | static int loop_add(struct loop_device **l, int i) | 1575 | static int loop_add(struct loop_device **l, int i) |
1605 | { | 1576 | { |
1606 | struct loop_device *lo; | 1577 | struct loop_device *lo; |
@@ -1627,16 +1598,28 @@ static int loop_add(struct loop_device **l, int i) | |||
1627 | i = err; | 1598 | i = err; |
1628 | 1599 | ||
1629 | err = -ENOMEM; | 1600 | err = -ENOMEM; |
1630 | lo->lo_queue = blk_alloc_queue(GFP_KERNEL); | 1601 | lo->tag_set.ops = &loop_mq_ops; |
1631 | if (!lo->lo_queue) | 1602 | lo->tag_set.nr_hw_queues = 1; |
1603 | lo->tag_set.queue_depth = 128; | ||
1604 | lo->tag_set.numa_node = NUMA_NO_NODE; | ||
1605 | lo->tag_set.cmd_size = sizeof(struct loop_cmd); | ||
1606 | lo->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; | ||
1607 | lo->tag_set.driver_data = lo; | ||
1608 | |||
1609 | err = blk_mq_alloc_tag_set(&lo->tag_set); | ||
1610 | if (err) | ||
1632 | goto out_free_idr; | 1611 | goto out_free_idr; |
1633 | 1612 | ||
1634 | /* | 1613 | lo->lo_queue = blk_mq_init_queue(&lo->tag_set); |
1635 | * set queue make_request_fn | 1614 | if (IS_ERR_OR_NULL(lo->lo_queue)) { |
1636 | */ | 1615 | err = PTR_ERR(lo->lo_queue); |
1637 | blk_queue_make_request(lo->lo_queue, loop_make_request); | 1616 | goto out_cleanup_tags; |
1617 | } | ||
1638 | lo->lo_queue->queuedata = lo; | 1618 | lo->lo_queue->queuedata = lo; |
1639 | 1619 | ||
1620 | INIT_LIST_HEAD(&lo->write_cmd_head); | ||
1621 | INIT_WORK(&lo->write_work, loop_queue_write_work); | ||
1622 | |||
1640 | disk = lo->lo_disk = alloc_disk(1 << part_shift); | 1623 | disk = lo->lo_disk = alloc_disk(1 << part_shift); |
1641 | if (!disk) | 1624 | if (!disk) |
1642 | goto out_free_queue; | 1625 | goto out_free_queue; |
@@ -1664,9 +1647,6 @@ static int loop_add(struct loop_device **l, int i) | |||
1664 | disk->flags |= GENHD_FL_EXT_DEVT; | 1647 | disk->flags |= GENHD_FL_EXT_DEVT; |
1665 | mutex_init(&lo->lo_ctl_mutex); | 1648 | mutex_init(&lo->lo_ctl_mutex); |
1666 | lo->lo_number = i; | 1649 | lo->lo_number = i; |
1667 | lo->lo_thread = NULL; | ||
1668 | init_waitqueue_head(&lo->lo_event); | ||
1669 | init_waitqueue_head(&lo->lo_req_wait); | ||
1670 | spin_lock_init(&lo->lo_lock); | 1650 | spin_lock_init(&lo->lo_lock); |
1671 | disk->major = LOOP_MAJOR; | 1651 | disk->major = LOOP_MAJOR; |
1672 | disk->first_minor = i << part_shift; | 1652 | disk->first_minor = i << part_shift; |
@@ -1680,6 +1660,8 @@ static int loop_add(struct loop_device **l, int i) | |||
1680 | 1660 | ||
1681 | out_free_queue: | 1661 | out_free_queue: |
1682 | blk_cleanup_queue(lo->lo_queue); | 1662 | blk_cleanup_queue(lo->lo_queue); |
1663 | out_cleanup_tags: | ||
1664 | blk_mq_free_tag_set(&lo->tag_set); | ||
1683 | out_free_idr: | 1665 | out_free_idr: |
1684 | idr_remove(&loop_index_idr, i); | 1666 | idr_remove(&loop_index_idr, i); |
1685 | out_free_dev: | 1667 | out_free_dev: |
@@ -1692,6 +1674,7 @@ static void loop_remove(struct loop_device *lo) | |||
1692 | { | 1674 | { |
1693 | del_gendisk(lo->lo_disk); | 1675 | del_gendisk(lo->lo_disk); |
1694 | blk_cleanup_queue(lo->lo_queue); | 1676 | blk_cleanup_queue(lo->lo_queue); |
1677 | blk_mq_free_tag_set(&lo->tag_set); | ||
1695 | put_disk(lo->lo_disk); | 1678 | put_disk(lo->lo_disk); |
1696 | kfree(lo); | 1679 | kfree(lo); |
1697 | } | 1680 | } |
@@ -1875,6 +1858,13 @@ static int __init loop_init(void) | |||
1875 | goto misc_out; | 1858 | goto misc_out; |
1876 | } | 1859 | } |
1877 | 1860 | ||
1861 | loop_wq = alloc_workqueue("kloopd", | ||
1862 | WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 0); | ||
1863 | if (!loop_wq) { | ||
1864 | err = -ENOMEM; | ||
1865 | goto misc_out; | ||
1866 | } | ||
1867 | |||
1878 | blk_register_region(MKDEV(LOOP_MAJOR, 0), range, | 1868 | blk_register_region(MKDEV(LOOP_MAJOR, 0), range, |
1879 | THIS_MODULE, loop_probe, NULL, NULL); | 1869 | THIS_MODULE, loop_probe, NULL, NULL); |
1880 | 1870 | ||
@@ -1912,6 +1902,8 @@ static void __exit loop_exit(void) | |||
1912 | blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); | 1902 | blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range); |
1913 | unregister_blkdev(LOOP_MAJOR, "loop"); | 1903 | unregister_blkdev(LOOP_MAJOR, "loop"); |
1914 | 1904 | ||
1905 | destroy_workqueue(loop_wq); | ||
1906 | |||
1915 | misc_deregister(&loop_misc); | 1907 | misc_deregister(&loop_misc); |
1916 | } | 1908 | } |
1917 | 1909 | ||