aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block/nbd.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/block/nbd.c')
-rw-r--r--drivers/block/nbd.c32
1 files changed, 30 insertions, 2 deletions
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index d07c9f7fded6..043ddcca4abf 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -78,6 +78,8 @@ static const char *ioctl_cmd_to_ascii(int cmd)
78 case NBD_SET_SOCK: return "set-sock"; 78 case NBD_SET_SOCK: return "set-sock";
79 case NBD_SET_BLKSIZE: return "set-blksize"; 79 case NBD_SET_BLKSIZE: return "set-blksize";
80 case NBD_SET_SIZE: return "set-size"; 80 case NBD_SET_SIZE: return "set-size";
81 case NBD_SET_TIMEOUT: return "set-timeout";
82 case NBD_SET_FLAGS: return "set-flags";
81 case NBD_DO_IT: return "do-it"; 83 case NBD_DO_IT: return "do-it";
82 case NBD_CLEAR_SOCK: return "clear-sock"; 84 case NBD_CLEAR_SOCK: return "clear-sock";
83 case NBD_CLEAR_QUE: return "clear-que"; 85 case NBD_CLEAR_QUE: return "clear-que";
@@ -96,6 +98,7 @@ static const char *nbdcmd_to_ascii(int cmd)
96 case NBD_CMD_READ: return "read"; 98 case NBD_CMD_READ: return "read";
97 case NBD_CMD_WRITE: return "write"; 99 case NBD_CMD_WRITE: return "write";
98 case NBD_CMD_DISC: return "disconnect"; 100 case NBD_CMD_DISC: return "disconnect";
101 case NBD_CMD_TRIM: return "trim/discard";
99 } 102 }
100 return "invalid"; 103 return "invalid";
101} 104}
@@ -449,6 +452,14 @@ static void nbd_clear_que(struct nbd_device *nbd)
449 req->errors++; 452 req->errors++;
450 nbd_end_request(req); 453 nbd_end_request(req);
451 } 454 }
455
456 while (!list_empty(&nbd->waiting_queue)) {
457 req = list_entry(nbd->waiting_queue.next, struct request,
458 queuelist);
459 list_del_init(&req->queuelist);
460 req->errors++;
461 nbd_end_request(req);
462 }
452} 463}
453 464
454 465
@@ -459,8 +470,12 @@ static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
459 470
460 nbd_cmd(req) = NBD_CMD_READ; 471 nbd_cmd(req) = NBD_CMD_READ;
461 if (rq_data_dir(req) == WRITE) { 472 if (rq_data_dir(req) == WRITE) {
462 nbd_cmd(req) = NBD_CMD_WRITE; 473 if ((req->cmd_flags & REQ_DISCARD)) {
463 if (nbd->flags & NBD_READ_ONLY) { 474 WARN_ON(!(nbd->flags & NBD_FLAG_SEND_TRIM));
475 nbd_cmd(req) = NBD_CMD_TRIM;
476 } else
477 nbd_cmd(req) = NBD_CMD_WRITE;
478 if (nbd->flags & NBD_FLAG_READ_ONLY) {
464 dev_err(disk_to_dev(nbd->disk), 479 dev_err(disk_to_dev(nbd->disk),
465 "Write on read-only\n"); 480 "Write on read-only\n");
466 goto error_out; 481 goto error_out;
@@ -598,6 +613,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
598 nbd->file = NULL; 613 nbd->file = NULL;
599 nbd_clear_que(nbd); 614 nbd_clear_que(nbd);
600 BUG_ON(!list_empty(&nbd->queue_head)); 615 BUG_ON(!list_empty(&nbd->queue_head));
616 BUG_ON(!list_empty(&nbd->waiting_queue));
601 if (file) 617 if (file)
602 fput(file); 618 fput(file);
603 return 0; 619 return 0;
@@ -642,6 +658,10 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
642 nbd->xmit_timeout = arg * HZ; 658 nbd->xmit_timeout = arg * HZ;
643 return 0; 659 return 0;
644 660
661 case NBD_SET_FLAGS:
662 nbd->flags = arg;
663 return 0;
664
645 case NBD_SET_SIZE_BLOCKS: 665 case NBD_SET_SIZE_BLOCKS:
646 nbd->bytesize = ((u64) arg) * nbd->blksize; 666 nbd->bytesize = ((u64) arg) * nbd->blksize;
647 bdev->bd_inode->i_size = nbd->bytesize; 667 bdev->bd_inode->i_size = nbd->bytesize;
@@ -661,6 +681,10 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
661 681
662 mutex_unlock(&nbd->tx_lock); 682 mutex_unlock(&nbd->tx_lock);
663 683
684 if (nbd->flags & NBD_FLAG_SEND_TRIM)
685 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
686 nbd->disk->queue);
687
664 thread = kthread_create(nbd_thread, nbd, nbd->disk->disk_name); 688 thread = kthread_create(nbd_thread, nbd, nbd->disk->disk_name);
665 if (IS_ERR(thread)) { 689 if (IS_ERR(thread)) {
666 mutex_lock(&nbd->tx_lock); 690 mutex_lock(&nbd->tx_lock);
@@ -678,6 +702,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
678 nbd->file = NULL; 702 nbd->file = NULL;
679 nbd_clear_que(nbd); 703 nbd_clear_que(nbd);
680 dev_warn(disk_to_dev(nbd->disk), "queue cleared\n"); 704 dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
705 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
681 if (file) 706 if (file)
682 fput(file); 707 fput(file);
683 nbd->bytesize = 0; 708 nbd->bytesize = 0;
@@ -796,6 +821,9 @@ static int __init nbd_init(void)
796 * Tell the block layer that we are not a rotational device 821 * Tell the block layer that we are not a rotational device
797 */ 822 */
798 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue); 823 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
824 disk->queue->limits.discard_granularity = 512;
825 disk->queue->limits.max_discard_sectors = UINT_MAX;
826 disk->queue->limits.discard_zeroes_data = 0;
799 } 827 }
800 828
801 if (register_blkdev(NBD_MAJOR, "nbd")) { 829 if (register_blkdev(NBD_MAJOR, "nbd")) {