diff options
-rw-r--r-- | drivers/block/nbd.c | 88 |
1 files changed, 22 insertions, 66 deletions
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 7139c8aae7a1..9e553a753410 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c | |||
@@ -63,21 +63,6 @@ struct nbd_device { | |||
63 | 63 | ||
64 | #define NBD_MAGIC 0x68797548 | 64 | #define NBD_MAGIC 0x68797548 |
65 | 65 | ||
66 | #ifdef NDEBUG | ||
67 | #define dprintk(flags, fmt...) | ||
68 | #else /* NDEBUG */ | ||
69 | #define dprintk(flags, fmt...) do { \ | ||
70 | if (debugflags & (flags)) printk(KERN_DEBUG fmt); \ | ||
71 | } while (0) | ||
72 | #define DBG_IOCTL 0x0004 | ||
73 | #define DBG_INIT 0x0010 | ||
74 | #define DBG_EXIT 0x0020 | ||
75 | #define DBG_BLKDEV 0x0100 | ||
76 | #define DBG_RX 0x0200 | ||
77 | #define DBG_TX 0x0400 | ||
78 | static unsigned int debugflags; | ||
79 | #endif /* NDEBUG */ | ||
80 | |||
81 | static unsigned int nbds_max = 16; | 66 | static unsigned int nbds_max = 16; |
82 | static struct nbd_device *nbd_dev; | 67 | static struct nbd_device *nbd_dev; |
83 | static int max_part; | 68 | static int max_part; |
@@ -94,25 +79,9 @@ static int max_part; | |||
94 | */ | 79 | */ |
95 | static DEFINE_SPINLOCK(nbd_lock); | 80 | static DEFINE_SPINLOCK(nbd_lock); |
96 | 81 | ||
97 | #ifndef NDEBUG | 82 | static inline struct device *nbd_to_dev(struct nbd_device *nbd) |
98 | static const char *ioctl_cmd_to_ascii(int cmd) | ||
99 | { | 83 | { |
100 | switch (cmd) { | 84 | return disk_to_dev(nbd->disk); |
101 | case NBD_SET_SOCK: return "set-sock"; | ||
102 | case NBD_SET_BLKSIZE: return "set-blksize"; | ||
103 | case NBD_SET_SIZE: return "set-size"; | ||
104 | case NBD_SET_TIMEOUT: return "set-timeout"; | ||
105 | case NBD_SET_FLAGS: return "set-flags"; | ||
106 | case NBD_DO_IT: return "do-it"; | ||
107 | case NBD_CLEAR_SOCK: return "clear-sock"; | ||
108 | case NBD_CLEAR_QUE: return "clear-que"; | ||
109 | case NBD_PRINT_DEBUG: return "print-debug"; | ||
110 | case NBD_SET_SIZE_BLOCKS: return "set-size-blocks"; | ||
111 | case NBD_DISCONNECT: return "disconnect"; | ||
112 | case BLKROSET: return "set-read-only"; | ||
113 | case BLKFLSBUF: return "flush-buffer-cache"; | ||
114 | } | ||
115 | return "unknown"; | ||
116 | } | 85 | } |
117 | 86 | ||
118 | static const char *nbdcmd_to_ascii(int cmd) | 87 | static const char *nbdcmd_to_ascii(int cmd) |
@@ -126,16 +95,15 @@ static const char *nbdcmd_to_ascii(int cmd) | |||
126 | } | 95 | } |
127 | return "invalid"; | 96 | return "invalid"; |
128 | } | 97 | } |
129 | #endif /* NDEBUG */ | ||
130 | 98 | ||
131 | static void nbd_end_request(struct request *req) | 99 | static void nbd_end_request(struct nbd_device *nbd, struct request *req) |
132 | { | 100 | { |
133 | int error = req->errors ? -EIO : 0; | 101 | int error = req->errors ? -EIO : 0; |
134 | struct request_queue *q = req->q; | 102 | struct request_queue *q = req->q; |
135 | unsigned long flags; | 103 | unsigned long flags; |
136 | 104 | ||
137 | dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name, | 105 | dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", req, |
138 | req, error ? "failed" : "done"); | 106 | error ? "failed" : "done"); |
139 | 107 | ||
140 | spin_lock_irqsave(q->queue_lock, flags); | 108 | spin_lock_irqsave(q->queue_lock, flags); |
141 | __blk_end_request_all(req, error); | 109 | __blk_end_request_all(req, error); |
@@ -276,11 +244,9 @@ static int nbd_send_req(struct nbd_device *nbd, struct request *req) | |||
276 | } | 244 | } |
277 | memcpy(request.handle, &req, sizeof(req)); | 245 | memcpy(request.handle, &req, sizeof(req)); |
278 | 246 | ||
279 | dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%uB)\n", | 247 | dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n", |
280 | nbd->disk->disk_name, req, | 248 | req, nbdcmd_to_ascii(nbd_cmd(req)), |
281 | nbdcmd_to_ascii(nbd_cmd(req)), | 249 | (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req)); |
282 | (unsigned long long)blk_rq_pos(req) << 9, | ||
283 | blk_rq_bytes(req)); | ||
284 | result = sock_xmit(nbd, 1, &request, sizeof(request), | 250 | result = sock_xmit(nbd, 1, &request, sizeof(request), |
285 | (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0); | 251 | (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0); |
286 | if (result <= 0) { | 252 | if (result <= 0) { |
@@ -300,8 +266,8 @@ static int nbd_send_req(struct nbd_device *nbd, struct request *req) | |||
300 | flags = 0; | 266 | flags = 0; |
301 | if (!rq_iter_last(bvec, iter)) | 267 | if (!rq_iter_last(bvec, iter)) |
302 | flags = MSG_MORE; | 268 | flags = MSG_MORE; |
303 | dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n", | 269 | dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n", |
304 | nbd->disk->disk_name, req, bvec.bv_len); | 270 | req, bvec.bv_len); |
305 | result = sock_send_bvec(nbd, &bvec, flags); | 271 | result = sock_send_bvec(nbd, &bvec, flags); |
306 | if (result <= 0) { | 272 | if (result <= 0) { |
307 | dev_err(disk_to_dev(nbd->disk), | 273 | dev_err(disk_to_dev(nbd->disk), |
@@ -394,8 +360,7 @@ static struct request *nbd_read_stat(struct nbd_device *nbd) | |||
394 | return req; | 360 | return req; |
395 | } | 361 | } |
396 | 362 | ||
397 | dprintk(DBG_RX, "%s: request %p: got reply\n", | 363 | dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req); |
398 | nbd->disk->disk_name, req); | ||
399 | if (nbd_cmd(req) == NBD_CMD_READ) { | 364 | if (nbd_cmd(req) == NBD_CMD_READ) { |
400 | struct req_iterator iter; | 365 | struct req_iterator iter; |
401 | struct bio_vec bvec; | 366 | struct bio_vec bvec; |
@@ -408,8 +373,8 @@ static struct request *nbd_read_stat(struct nbd_device *nbd) | |||
408 | req->errors++; | 373 | req->errors++; |
409 | return req; | 374 | return req; |
410 | } | 375 | } |
411 | dprintk(DBG_RX, "%s: request %p: got %d bytes data\n", | 376 | dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n", |
412 | nbd->disk->disk_name, req, bvec.bv_len); | 377 | req, bvec.bv_len); |
413 | } | 378 | } |
414 | } | 379 | } |
415 | return req; | 380 | return req; |
@@ -449,7 +414,7 @@ static int nbd_do_it(struct nbd_device *nbd) | |||
449 | } | 414 | } |
450 | 415 | ||
451 | while ((req = nbd_read_stat(nbd)) != NULL) | 416 | while ((req = nbd_read_stat(nbd)) != NULL) |
452 | nbd_end_request(req); | 417 | nbd_end_request(nbd, req); |
453 | 418 | ||
454 | device_remove_file(disk_to_dev(nbd->disk), &pid_attr); | 419 | device_remove_file(disk_to_dev(nbd->disk), &pid_attr); |
455 | nbd->pid = 0; | 420 | nbd->pid = 0; |
@@ -478,7 +443,7 @@ static void nbd_clear_que(struct nbd_device *nbd) | |||
478 | queuelist); | 443 | queuelist); |
479 | list_del_init(&req->queuelist); | 444 | list_del_init(&req->queuelist); |
480 | req->errors++; | 445 | req->errors++; |
481 | nbd_end_request(req); | 446 | nbd_end_request(nbd, req); |
482 | } | 447 | } |
483 | 448 | ||
484 | while (!list_empty(&nbd->waiting_queue)) { | 449 | while (!list_empty(&nbd->waiting_queue)) { |
@@ -486,7 +451,7 @@ static void nbd_clear_que(struct nbd_device *nbd) | |||
486 | queuelist); | 451 | queuelist); |
487 | list_del_init(&req->queuelist); | 452 | list_del_init(&req->queuelist); |
488 | req->errors++; | 453 | req->errors++; |
489 | nbd_end_request(req); | 454 | nbd_end_request(nbd, req); |
490 | } | 455 | } |
491 | } | 456 | } |
492 | 457 | ||
@@ -530,7 +495,7 @@ static void nbd_handle_req(struct nbd_device *nbd, struct request *req) | |||
530 | if (nbd_send_req(nbd, req) != 0) { | 495 | if (nbd_send_req(nbd, req) != 0) { |
531 | dev_err(disk_to_dev(nbd->disk), "Request send failed\n"); | 496 | dev_err(disk_to_dev(nbd->disk), "Request send failed\n"); |
532 | req->errors++; | 497 | req->errors++; |
533 | nbd_end_request(req); | 498 | nbd_end_request(nbd, req); |
534 | } else { | 499 | } else { |
535 | spin_lock(&nbd->queue_lock); | 500 | spin_lock(&nbd->queue_lock); |
536 | list_add_tail(&req->queuelist, &nbd->queue_head); | 501 | list_add_tail(&req->queuelist, &nbd->queue_head); |
@@ -545,7 +510,7 @@ static void nbd_handle_req(struct nbd_device *nbd, struct request *req) | |||
545 | 510 | ||
546 | error_out: | 511 | error_out: |
547 | req->errors++; | 512 | req->errors++; |
548 | nbd_end_request(req); | 513 | nbd_end_request(nbd, req); |
549 | } | 514 | } |
550 | 515 | ||
551 | static int nbd_thread(void *data) | 516 | static int nbd_thread(void *data) |
@@ -593,18 +558,18 @@ static void do_nbd_request(struct request_queue *q) | |||
593 | 558 | ||
594 | spin_unlock_irq(q->queue_lock); | 559 | spin_unlock_irq(q->queue_lock); |
595 | 560 | ||
596 | dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%x)\n", | ||
597 | req->rq_disk->disk_name, req, req->cmd_type); | ||
598 | |||
599 | nbd = req->rq_disk->private_data; | 561 | nbd = req->rq_disk->private_data; |
600 | 562 | ||
601 | BUG_ON(nbd->magic != NBD_MAGIC); | 563 | BUG_ON(nbd->magic != NBD_MAGIC); |
602 | 564 | ||
565 | dev_dbg(nbd_to_dev(nbd), "request %p: dequeued (flags=%x)\n", | ||
566 | req, req->cmd_type); | ||
567 | |||
603 | if (unlikely(!nbd->sock)) { | 568 | if (unlikely(!nbd->sock)) { |
604 | dev_err(disk_to_dev(nbd->disk), | 569 | dev_err(disk_to_dev(nbd->disk), |
605 | "Attempted send on closed socket\n"); | 570 | "Attempted send on closed socket\n"); |
606 | req->errors++; | 571 | req->errors++; |
607 | nbd_end_request(req); | 572 | nbd_end_request(nbd, req); |
608 | spin_lock_irq(q->queue_lock); | 573 | spin_lock_irq(q->queue_lock); |
609 | continue; | 574 | continue; |
610 | } | 575 | } |
@@ -791,10 +756,6 @@ static int nbd_ioctl(struct block_device *bdev, fmode_t mode, | |||
791 | 756 | ||
792 | BUG_ON(nbd->magic != NBD_MAGIC); | 757 | BUG_ON(nbd->magic != NBD_MAGIC); |
793 | 758 | ||
794 | /* Anyone capable of this syscall can do *real bad* things */ | ||
795 | dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n", | ||
796 | nbd->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg); | ||
797 | |||
798 | mutex_lock(&nbd->tx_lock); | 759 | mutex_lock(&nbd->tx_lock); |
799 | error = __nbd_ioctl(bdev, nbd, cmd, arg); | 760 | error = __nbd_ioctl(bdev, nbd, cmd, arg); |
800 | mutex_unlock(&nbd->tx_lock); | 761 | mutex_unlock(&nbd->tx_lock); |
@@ -884,7 +845,6 @@ static int __init nbd_init(void) | |||
884 | } | 845 | } |
885 | 846 | ||
886 | printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR); | 847 | printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR); |
887 | dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags); | ||
888 | 848 | ||
889 | for (i = 0; i < nbds_max; i++) { | 849 | for (i = 0; i < nbds_max; i++) { |
890 | struct gendisk *disk = nbd_dev[i].disk; | 850 | struct gendisk *disk = nbd_dev[i].disk; |
@@ -943,7 +903,3 @@ module_param(nbds_max, int, 0444); | |||
943 | MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)"); | 903 | MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)"); |
944 | module_param(max_part, int, 0444); | 904 | module_param(max_part, int, 0444); |
945 | MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)"); | 905 | MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)"); |
946 | #ifndef NDEBUG | ||
947 | module_param(debugflags, int, 0644); | ||
948 | MODULE_PARM_DESC(debugflags, "flags for controlling debug output"); | ||
949 | #endif | ||