blob: 9dcd5ddce94fb66e426697fa77fe273a3be329ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Network block device - make block devices work over TCP
3 *
4 * Note that you can not swap over this thing, yet. Seems to work but
5 * deadlocks sometimes - you can not swap over TCP in general.
6 *
Pavel Macheka2531292010-07-18 14:27:13 +02007 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
9 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070010 * This file is released under GPLv2 or later.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Pavel Machekdbf492d2006-06-25 05:47:42 -070012 * (part of code stolen from loop.c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/major.h>
16
17#include <linux/blkdev.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/sched.h>
21#include <linux/fs.h>
22#include <linux/bio.h>
23#include <linux/stat.h>
24#include <linux/errno.h>
25#include <linux/file.h>
26#include <linux/ioctl.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020027#include <linux/mutex.h>
Herbert Xu4b2f0262006-01-06 00:09:47 -080028#include <linux/compiler.h>
29#include <linux/err.h>
30#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/sock.h>
Trond Myklebust91cf45f2007-11-12 18:10:39 -080033#include <linux/net.h>
Laurent Vivier48cf6062008-04-29 01:02:46 -070034#include <linux/kthread.h>
Markus Pargmannb9c495b2015-04-02 10:11:37 +020035#include <linux/types.h>
Markus Pargmann30d53d92015-08-17 08:20:06 +020036#include <linux/debugfs.h>
Josef Bacikfd8383f2016-09-08 12:33:37 -070037#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/types.h>
41
42#include <linux/nbd.h>
43
Josef Bacikb0d91112017-02-01 16:11:40 -050044static DEFINE_IDR(nbd_index_idr);
45static DEFINE_MUTEX(nbd_index_mutex);
46
Josef Bacik9561a7a2016-11-22 14:04:40 -050047struct nbd_sock {
48 struct socket *sock;
49 struct mutex tx_lock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -040050 struct request *pending;
51 int sent;
Josef Bacik9561a7a2016-11-22 14:04:40 -050052};
53
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070054#define NBD_TIMEDOUT 0
55#define NBD_DISCONNECT_REQUESTED 1
Josef Bacik9561a7a2016-11-22 14:04:40 -050056#define NBD_DISCONNECTED 2
57#define NBD_RUNNING 3
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070058
Markus Pargmann13e71d62015-04-02 10:11:35 +020059struct nbd_device {
Markus Pargmann22d109c2015-08-17 08:20:09 +020060 u32 flags;
Josef Bacik9b4a6ba2016-09-08 12:33:39 -070061 unsigned long runtime_flags;
Josef Bacik9561a7a2016-11-22 14:04:40 -050062 struct nbd_sock **socks;
Markus Pargmann13e71d62015-04-02 10:11:35 +020063 int magic;
64
Josef Bacikfd8383f2016-09-08 12:33:37 -070065 struct blk_mq_tag_set tag_set;
Markus Pargmann13e71d62015-04-02 10:11:35 +020066
Josef Bacik9561a7a2016-11-22 14:04:40 -050067 struct mutex config_lock;
Markus Pargmann13e71d62015-04-02 10:11:35 +020068 struct gendisk *disk;
Josef Bacik9561a7a2016-11-22 14:04:40 -050069 int num_connections;
70 atomic_t recv_threads;
71 wait_queue_head_t recv_wq;
Josef Bacikef77b512016-12-02 16:19:12 -050072 loff_t blksize;
Markus Pargmannb9c495b2015-04-02 10:11:37 +020073 loff_t bytesize;
Markus Pargmann7e2893a2015-08-17 08:20:00 +020074
Markus Pargmann7e2893a2015-08-17 08:20:00 +020075 struct task_struct *task_recv;
Josef Bacik9561a7a2016-11-22 14:04:40 -050076 struct task_struct *task_setup;
Markus Pargmann30d53d92015-08-17 08:20:06 +020077
78#if IS_ENABLED(CONFIG_DEBUG_FS)
79 struct dentry *dbg_dir;
80#endif
Markus Pargmann13e71d62015-04-02 10:11:35 +020081};
82
Josef Bacikfd8383f2016-09-08 12:33:37 -070083struct nbd_cmd {
84 struct nbd_device *nbd;
Josef Bacik9561a7a2016-11-22 14:04:40 -050085 struct completion send_complete;
Josef Bacikfd8383f2016-09-08 12:33:37 -070086};
87
Markus Pargmann30d53d92015-08-17 08:20:06 +020088#if IS_ENABLED(CONFIG_DEBUG_FS)
89static struct dentry *nbd_dbg_dir;
90#endif
91
92#define nbd_name(nbd) ((nbd)->disk->disk_name)
93
Wanlong Gaof4507162012-03-28 14:42:51 -070094#define NBD_MAGIC 0x68797548
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Ingo van Lil9c7a4162006-07-01 04:36:36 -070096static unsigned int nbds_max = 16;
Laurent Vivierd71a6d72008-04-29 01:02:51 -070097static int max_part;
Josef Bacik124d6db2017-02-01 16:11:11 -050098static struct workqueue_struct *recv_workqueue;
Josef Bacikb0d91112017-02-01 16:11:40 -050099static int part_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Josef Bacik9442b732017-02-07 17:10:22 -0500101static int nbd_dev_dbg_init(struct nbd_device *nbd);
102static void nbd_dev_dbg_close(struct nbd_device *nbd);
103
104
Markus Pargmannd18509f2015-04-02 10:11:38 +0200105static inline struct device *nbd_to_dev(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Markus Pargmannd18509f2015-04-02 10:11:38 +0200107 return disk_to_dev(nbd->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Markus Pargmann37091fd2015-07-27 07:36:49 +0200110static bool nbd_is_connected(struct nbd_device *nbd)
111{
112 return !!nbd->task_recv;
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static const char *nbdcmd_to_ascii(int cmd)
116{
117 switch (cmd) {
118 case NBD_CMD_READ: return "read";
119 case NBD_CMD_WRITE: return "write";
120 case NBD_CMD_DISC: return "disconnect";
Alex Bligh75f187a2013-02-27 17:05:23 -0800121 case NBD_CMD_FLUSH: return "flush";
Paul Clementsa336d292012-10-04 17:16:18 -0700122 case NBD_CMD_TRIM: return "trim/discard";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124 return "invalid";
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Markus Pargmann37091fd2015-07-27 07:36:49 +0200127static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev)
128{
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -0400129 if (bdev->bd_openers <= 1)
130 bd_set_size(bdev, 0);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200131 set_capacity(nbd->disk, 0);
132 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
133
134 return 0;
135}
136
137static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev)
138{
Josef Bacike5445412017-02-13 10:39:47 -0500139 blk_queue_logical_block_size(nbd->disk->queue, nbd->blksize);
140 blk_queue_physical_block_size(nbd->disk->queue, nbd->blksize);
141 bd_set_size(bdev, nbd->bytesize);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200142 set_capacity(nbd->disk, nbd->bytesize >> 9);
143 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
144}
145
Josef Bacike5445412017-02-13 10:39:47 -0500146static void nbd_size_set(struct nbd_device *nbd, struct block_device *bdev,
Josef Bacikef77b512016-12-02 16:19:12 -0500147 loff_t blocksize, loff_t nr_blocks)
Markus Pargmann37091fd2015-07-27 07:36:49 +0200148{
Markus Pargmann37091fd2015-07-27 07:36:49 +0200149 nbd->blksize = blocksize;
Josef Bacikef77b512016-12-02 16:19:12 -0500150 nbd->bytesize = blocksize * nr_blocks;
Josef Bacike5445412017-02-13 10:39:47 -0500151 if (nbd_is_connected(nbd))
152 nbd_size_update(nbd, bdev);
Markus Pargmann37091fd2015-07-27 07:36:49 +0200153}
154
Josef Bacikfd8383f2016-09-08 12:33:37 -0700155static void nbd_end_request(struct nbd_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700157 struct nbd_device *nbd = cmd->nbd;
158 struct request *req = blk_mq_rq_from_pdu(cmd);
Kiyoshi Ueda097c94a2007-12-11 17:44:06 -0500159 int error = req->errors ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Josef Bacikfd8383f2016-09-08 12:33:37 -0700161 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", cmd,
Markus Pargmannd18509f2015-04-02 10:11:38 +0200162 error ? "failed" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Josef Bacikfd8383f2016-09-08 12:33:37 -0700164 blk_mq_complete_request(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Markus Pargmanne018e752015-04-02 10:11:39 +0200167/*
168 * Forcibly shutdown the socket causing all listeners to error
169 */
Markus Pargmann36e47be2015-08-17 08:20:01 +0200170static void sock_shutdown(struct nbd_device *nbd)
Paul Clements7fdfd402007-10-16 23:27:37 -0700171{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500172 int i;
Josef Bacikc2611892016-09-08 12:33:38 -0700173
Josef Bacik9561a7a2016-11-22 14:04:40 -0500174 if (nbd->num_connections == 0)
Markus Pargmann260bbce2015-08-17 08:20:02 +0200175 return;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500176 if (test_and_set_bit(NBD_DISCONNECTED, &nbd->runtime_flags))
177 return;
178
179 for (i = 0; i < nbd->num_connections; i++) {
180 struct nbd_sock *nsock = nbd->socks[i];
181 mutex_lock(&nsock->tx_lock);
182 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
183 mutex_unlock(&nsock->tx_lock);
Markus Pargmann23272a672015-10-29 11:51:16 +0100184 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500185 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
Paul Clements7fdfd402007-10-16 23:27:37 -0700186}
187
Josef Bacik0eadf372016-09-08 12:33:40 -0700188static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
189 bool reserved)
Paul Clements7fdfd402007-10-16 23:27:37 -0700190{
Josef Bacik0eadf372016-09-08 12:33:40 -0700191 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
192 struct nbd_device *nbd = cmd->nbd;
Paul Clements7fdfd402007-10-16 23:27:37 -0700193
Markus Pargmann23272a672015-10-29 11:51:16 +0100194 dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
Josef Bacik9561a7a2016-11-22 14:04:40 -0500195 set_bit(NBD_TIMEDOUT, &nbd->runtime_flags);
Josef Bacikc103b4d2017-03-24 14:08:27 -0400196 req->errors = -EIO;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500197
Josef Bacik9561a7a2016-11-22 14:04:40 -0500198 mutex_lock(&nbd->config_lock);
199 sock_shutdown(nbd);
200 mutex_unlock(&nbd->config_lock);
Josef Bacik0eadf372016-09-08 12:33:40 -0700201 return BLK_EH_HANDLED;
Paul Clements7fdfd402007-10-16 23:27:37 -0700202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
205 * Send or receive packet.
206 */
Al Viroc9f2b6a2015-11-12 05:09:35 -0500207static int sock_xmit(struct nbd_device *nbd, int index, int send,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400208 struct iov_iter *iter, int msg_flags, int *sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500210 struct socket *sock = nbd->socks[index]->sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int result;
212 struct msghdr msg;
Mel Gorman7f338fe2012-07-31 16:44:32 -0700213 unsigned long pflags = current->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700215 if (unlikely(!sock)) {
Josef Bacika897b662016-12-05 16:20:29 -0500216 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200217 "Attempted %s on closed socket in sock_xmit\n",
218 (send ? "send" : "recv"));
Mike Snitzerffc41cf2008-04-02 13:04:47 -0700219 return -EINVAL;
220 }
221
Al Viroc9f2b6a2015-11-12 05:09:35 -0500222 msg.msg_iter = *iter;
Al Viroc1696ca2015-11-12 04:51:19 -0500223
Mel Gorman7f338fe2012-07-31 16:44:32 -0700224 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 do {
Mel Gorman7f338fe2012-07-31 16:44:32 -0700226 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 msg.msg_name = NULL;
228 msg.msg_namelen = 0;
229 msg.msg_control = NULL;
230 msg.msg_controllen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
232
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200233 if (send)
Al Viroc1696ca2015-11-12 04:51:19 -0500234 result = sock_sendmsg(sock, &msg);
Markus Pargmann7e2893a2015-08-17 08:20:00 +0200235 else
Al Viroc1696ca2015-11-12 04:51:19 -0500236 result = sock_recvmsg(sock, &msg, msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (result <= 0) {
239 if (result == 0)
240 result = -EPIPE; /* short read */
241 break;
242 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400243 if (sent)
244 *sent += result;
Al Viroc1696ca2015-11-12 04:51:19 -0500245 } while (msg_data_left(&msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Mel Gorman7f338fe2012-07-31 16:44:32 -0700247 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return result;
250}
251
Paul Clements7fdfd402007-10-16 23:27:37 -0700252/* always call with the tx_lock held */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500253static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700255 struct request *req = blk_mq_rq_from_pdu(cmd);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400256 struct nbd_sock *nsock = nbd->socks[index];
Josef Bacikd61b7f92017-01-19 16:08:49 -0500257 int result;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500258 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
259 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
260 struct iov_iter from;
Tejun Heo1011c1b2009-05-07 22:24:45 +0900261 unsigned long size = blk_rq_bytes(req);
Jens Axboe429a7872016-11-17 12:30:37 -0700262 struct bio *bio;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200263 u32 type;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500264 u32 tag = blk_mq_unique_tag(req);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400265 int sent = nsock->sent, skip = 0;
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200266
Al Viroc9f2b6a2015-11-12 05:09:35 -0500267 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
268
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100269 switch (req_op(req)) {
270 case REQ_OP_DISCARD:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200271 type = NBD_CMD_TRIM;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100272 break;
273 case REQ_OP_FLUSH:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200274 type = NBD_CMD_FLUSH;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100275 break;
276 case REQ_OP_WRITE:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200277 type = NBD_CMD_WRITE;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100278 break;
279 case REQ_OP_READ:
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200280 type = NBD_CMD_READ;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100281 break;
282 default:
283 return -EIO;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Christoph Hellwig09fc54cc2017-01-31 16:57:28 +0100286 if (rq_data_dir(req) == WRITE &&
287 (nbd->flags & NBD_FLAG_READ_ONLY)) {
288 dev_err_ratelimited(disk_to_dev(nbd->disk),
289 "Write on read-only\n");
290 return -EIO;
291 }
292
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400293 /* We did a partial send previously, and we at least sent the whole
294 * request struct, so just go and send the rest of the pages in the
295 * request.
296 */
297 if (sent) {
298 if (sent >= sizeof(request)) {
299 skip = sent - sizeof(request);
300 goto send_pages;
301 }
302 iov_iter_advance(&from, sent);
303 }
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200304 request.type = htonl(type);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500305 if (type != NBD_CMD_FLUSH) {
Alex Bligh75f187a2013-02-27 17:05:23 -0800306 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
307 request.len = htonl(size);
308 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500309 memcpy(request.handle, &tag, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Markus Pargmannd18509f2015-04-02 10:11:38 +0200311 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700312 cmd, nbdcmd_to_ascii(type),
Markus Pargmannd18509f2015-04-02 10:11:38 +0200313 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
Al Viroc9f2b6a2015-11-12 05:09:35 -0500314 result = sock_xmit(nbd, index, 1, &from,
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400315 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (result <= 0) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400317 if (result == -ERESTARTSYS) {
318 /* If we havne't sent anything we can just return BUSY,
319 * however if we have sent something we need to make
320 * sure we only allow this req to be sent until we are
321 * completely done.
322 */
323 if (sent) {
324 nsock->pending = req;
325 nsock->sent = sent;
326 }
327 return BLK_MQ_RQ_QUEUE_BUSY;
328 }
Josef Bacika897b662016-12-05 16:20:29 -0500329 dev_err_ratelimited(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200330 "Send control failed (result %d)\n", result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200331 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400333send_pages:
Jens Axboe429a7872016-11-17 12:30:37 -0700334 if (type != NBD_CMD_WRITE)
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400335 goto out;
Jens Axboe429a7872016-11-17 12:30:37 -0700336
Jens Axboe429a7872016-11-17 12:30:37 -0700337 bio = req->bio;
338 while (bio) {
339 struct bio *next = bio->bi_next;
340 struct bvec_iter iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800341 struct bio_vec bvec;
Jens Axboe429a7872016-11-17 12:30:37 -0700342
343 bio_for_each_segment(bvec, bio, iter) {
344 bool is_last = !next && bio_iter_last(bvec, iter);
Josef Bacikd61b7f92017-01-19 16:08:49 -0500345 int flags = is_last ? 0 : MSG_MORE;
Jens Axboe429a7872016-11-17 12:30:37 -0700346
Markus Pargmannd18509f2015-04-02 10:11:38 +0200347 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700348 cmd, bvec.bv_len);
Al Viroc9f2b6a2015-11-12 05:09:35 -0500349 iov_iter_bvec(&from, ITER_BVEC | WRITE,
350 &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400351 if (skip) {
352 if (skip >= iov_iter_count(&from)) {
353 skip -= iov_iter_count(&from);
354 continue;
355 }
356 iov_iter_advance(&from, skip);
357 skip = 0;
358 }
359 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
Jens Axboe6c92e692007-08-16 13:43:12 +0200360 if (result <= 0) {
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400361 if (result == -ERESTARTSYS) {
362 /* We've already sent the header, we
363 * have no choice but to set pending and
364 * return BUSY.
365 */
366 nsock->pending = req;
367 nsock->sent = sent;
368 return BLK_MQ_RQ_QUEUE_BUSY;
369 }
Wanlong Gaof4507162012-03-28 14:42:51 -0700370 dev_err(disk_to_dev(nbd->disk),
WANG Cong7f1b90f2011-08-19 14:48:22 +0200371 "Send data failed (result %d)\n",
372 result);
Markus Pargmanndab53132015-04-02 10:11:40 +0200373 return -EIO;
Jens Axboe6c92e692007-08-16 13:43:12 +0200374 }
Jens Axboe429a7872016-11-17 12:30:37 -0700375 /*
376 * The completion might already have come in,
377 * so break for the last one instead of letting
378 * the iterator do it. This prevents use-after-free
379 * of the bio.
380 */
381 if (is_last)
382 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Jens Axboe429a7872016-11-17 12:30:37 -0700384 bio = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400386out:
387 nsock->pending = NULL;
388 nsock->sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/* NULL returned = something went wrong, inform userspace */
Josef Bacik9561a7a2016-11-22 14:04:40 -0500393static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 int result;
396 struct nbd_reply reply;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700397 struct nbd_cmd *cmd;
398 struct request *req = NULL;
399 u16 hwq;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500400 u32 tag;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500401 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
402 struct iov_iter to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 reply.magic = 0;
Al Viroc9f2b6a2015-11-12 05:09:35 -0500405 iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply));
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400406 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (result <= 0) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500408 if (!test_bit(NBD_DISCONNECTED, &nbd->runtime_flags) &&
409 !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
410 dev_err(disk_to_dev(nbd->disk),
411 "Receive control failed (result %d)\n", result);
Markus Pargmann19391832015-08-17 08:20:03 +0200412 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Michal Feixe4b57e02006-07-30 03:03:31 -0700414
415 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700416 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
Michal Feixe4b57e02006-07-30 03:03:31 -0700417 (unsigned long)ntohl(reply.magic));
Markus Pargmann19391832015-08-17 08:20:03 +0200418 return ERR_PTR(-EPROTO);
Michal Feixe4b57e02006-07-30 03:03:31 -0700419 }
420
Josef Bacik9561a7a2016-11-22 14:04:40 -0500421 memcpy(&tag, reply.handle, sizeof(u32));
Herbert Xu4b2f0262006-01-06 00:09:47 -0800422
Josef Bacikfd8383f2016-09-08 12:33:37 -0700423 hwq = blk_mq_unique_tag_to_hwq(tag);
424 if (hwq < nbd->tag_set.nr_hw_queues)
425 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
426 blk_mq_unique_tag_to_tag(tag));
427 if (!req || !blk_mq_request_started(req)) {
428 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
429 tag, req);
430 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700432 cmd = blk_mq_rq_to_pdu(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (ntohl(reply.error)) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700434 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200435 ntohl(reply.error));
Josef Bacikc103b4d2017-03-24 14:08:27 -0400436 req->errors = -EIO;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700437 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Josef Bacikfd8383f2016-09-08 12:33:37 -0700440 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
Christoph Hellwig9dc6c802015-04-17 22:37:21 +0200441 if (rq_data_dir(req) != WRITE) {
NeilBrown5705f702007-09-25 12:35:59 +0200442 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800443 struct bio_vec bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200444
445 rq_for_each_segment(bvec, req, iter) {
Al Viroc9f2b6a2015-11-12 05:09:35 -0500446 iov_iter_bvec(&to, ITER_BVEC | READ,
447 &bvec, 1, bvec.bv_len);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400448 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
Jens Axboe6c92e692007-08-16 13:43:12 +0200449 if (result <= 0) {
Wanlong Gaof4507162012-03-28 14:42:51 -0700450 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
WANG Cong7f1b90f2011-08-19 14:48:22 +0200451 result);
Josef Bacikc103b4d2017-03-24 14:08:27 -0400452 req->errors = -EIO;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700453 return cmd;
Jens Axboe6c92e692007-08-16 13:43:12 +0200454 }
Markus Pargmannd18509f2015-04-02 10:11:38 +0200455 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
Josef Bacikfd8383f2016-09-08 12:33:37 -0700456 cmd, bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500458 } else {
459 /* See the comment in nbd_queue_rq. */
460 wait_for_completion(&cmd->send_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Josef Bacikfd8383f2016-09-08 12:33:37 -0700462 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200465static ssize_t pid_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
Paul Clements6b39bb62006-12-06 20:40:53 -0800467{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200468 struct gendisk *disk = dev_to_disk(dev);
Markus Pargmann6521d392015-08-17 08:20:05 +0200469 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200470
Markus Pargmann6521d392015-08-17 08:20:05 +0200471 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
Paul Clements6b39bb62006-12-06 20:40:53 -0800472}
473
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200474static struct device_attribute pid_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700475 .attr = { .name = "pid", .mode = S_IRUGO},
Paul Clements6b39bb62006-12-06 20:40:53 -0800476 .show = pid_show,
477};
478
Josef Bacik9561a7a2016-11-22 14:04:40 -0500479struct recv_thread_args {
480 struct work_struct work;
481 struct nbd_device *nbd;
482 int index;
483};
484
485static void recv_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Josef Bacik9561a7a2016-11-22 14:04:40 -0500487 struct recv_thread_args *args = container_of(work,
488 struct recv_thread_args,
489 work);
490 struct nbd_device *nbd = args->nbd;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700491 struct nbd_cmd *cmd;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500492 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Wanlong Gaof4507162012-03-28 14:42:51 -0700494 BUG_ON(nbd->magic != NBD_MAGIC);
Markus Pargmann19391832015-08-17 08:20:03 +0200495 while (1) {
Josef Bacik9561a7a2016-11-22 14:04:40 -0500496 cmd = nbd_read_stat(nbd, args->index);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700497 if (IS_ERR(cmd)) {
498 ret = PTR_ERR(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200499 break;
500 }
501
Josef Bacikfd8383f2016-09-08 12:33:37 -0700502 nbd_end_request(cmd);
Markus Pargmann19391832015-08-17 08:20:03 +0200503 }
Paul Clements6b39bb62006-12-06 20:40:53 -0800504
Josef Bacik9561a7a2016-11-22 14:04:40 -0500505 /*
506 * We got an error, shut everybody down if this wasn't the result of a
507 * disconnect request.
508 */
509 if (ret && !test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
510 sock_shutdown(nbd);
511 atomic_dec(&nbd->recv_threads);
512 wake_up(&nbd->recv_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Josef Bacikfd8383f2016-09-08 12:33:37 -0700515static void nbd_clear_req(struct request *req, void *data, bool reserved)
516{
517 struct nbd_cmd *cmd;
518
519 if (!blk_mq_request_started(req))
520 return;
521 cmd = blk_mq_rq_to_pdu(req);
Josef Bacikc103b4d2017-03-24 14:08:27 -0400522 req->errors = -EIO;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700523 nbd_end_request(cmd);
524}
525
Wanlong Gaof4507162012-03-28 14:42:51 -0700526static void nbd_clear_que(struct nbd_device *nbd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Wanlong Gaof4507162012-03-28 14:42:51 -0700528 BUG_ON(nbd->magic != NBD_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Josef Bacikfd8383f2016-09-08 12:33:37 -0700530 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
Markus Pargmanne78273c2015-08-17 08:20:04 +0200531 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Paul Clements7fdfd402007-10-16 23:27:37 -0700534
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400535static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700536{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700537 struct request *req = blk_mq_rq_from_pdu(cmd);
538 struct nbd_device *nbd = cmd->nbd;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500539 struct nbd_sock *nsock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400540 int ret;
Josef Bacikfd8383f2016-09-08 12:33:37 -0700541
Josef Bacik9561a7a2016-11-22 14:04:40 -0500542 if (index >= nbd->num_connections) {
Josef Bacika897b662016-12-05 16:20:29 -0500543 dev_err_ratelimited(disk_to_dev(nbd->disk),
544 "Attempted send on invalid socket\n");
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400545 return -EINVAL;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500546 }
547
548 if (test_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) {
Josef Bacika897b662016-12-05 16:20:29 -0500549 dev_err_ratelimited(disk_to_dev(nbd->disk),
550 "Attempted send on closed socket\n");
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400551 return -EINVAL;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500552 }
553
Laurent Vivier48cf6062008-04-29 01:02:46 -0700554 req->errors = 0;
555
Josef Bacik9561a7a2016-11-22 14:04:40 -0500556 nsock = nbd->socks[index];
557 mutex_lock(&nsock->tx_lock);
558 if (unlikely(!nsock->sock)) {
559 mutex_unlock(&nsock->tx_lock);
Josef Bacika897b662016-12-05 16:20:29 -0500560 dev_err_ratelimited(disk_to_dev(nbd->disk),
561 "Attempted send on closed socket\n");
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400562 return -EINVAL;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700563 }
564
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400565 /* Handle the case that we have a pending request that was partially
566 * transmitted that _has_ to be serviced first. We need to call requeue
567 * here so that it gets put _after_ the request that is already on the
568 * dispatch list.
569 */
570 if (unlikely(nsock->pending && nsock->pending != req)) {
571 blk_mq_requeue_request(req, true);
572 ret = 0;
573 goto out;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700574 }
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400575 ret = nbd_send_cmd(nbd, cmd, index);
576out:
Josef Bacik9561a7a2016-11-22 14:04:40 -0500577 mutex_unlock(&nsock->tx_lock);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400578 return ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700579}
580
Josef Bacikfd8383f2016-09-08 12:33:37 -0700581static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
582 const struct blk_mq_queue_data *bd)
Laurent Vivier48cf6062008-04-29 01:02:46 -0700583{
Josef Bacikfd8383f2016-09-08 12:33:37 -0700584 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400585 int ret;
Laurent Vivier48cf6062008-04-29 01:02:46 -0700586
Josef Bacik9561a7a2016-11-22 14:04:40 -0500587 /*
588 * Since we look at the bio's to send the request over the network we
589 * need to make sure the completion work doesn't mark this request done
590 * before we are done doing our send. This keeps us from dereferencing
591 * freed data if we have particularly fast completions (ie we get the
592 * completion before we exit sock_xmit on the last bvec) or in the case
593 * that the server is misbehaving (or there was an error) before we're
594 * done sending everything over the wire.
595 */
596 init_completion(&cmd->send_complete);
Josef Bacikfd8383f2016-09-08 12:33:37 -0700597 blk_mq_start_request(bd->rq);
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400598
599 /* We can be called directly from the user space process, which means we
600 * could possibly have signals pending so our sendmsg will fail. In
601 * this case we need to return that we are busy, otherwise error out as
602 * appropriate.
603 */
604 ret = nbd_handle_cmd(cmd, hctx->queue_num);
605 if (ret < 0)
606 ret = BLK_MQ_RQ_QUEUE_ERROR;
607 if (!ret)
608 ret = BLK_MQ_RQ_QUEUE_OK;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500609 complete(&cmd->send_complete);
610
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400611 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Josef Bacik9442b732017-02-07 17:10:22 -0500614static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
615 unsigned long arg)
Markus Pargmann23272a672015-10-29 11:51:16 +0100616{
Josef Bacik9442b732017-02-07 17:10:22 -0500617 struct socket *sock;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500618 struct nbd_sock **socks;
619 struct nbd_sock *nsock;
Josef Bacik9442b732017-02-07 17:10:22 -0500620 int err;
621
622 sock = sockfd_lookup(arg, &err);
623 if (!sock)
624 return err;
Markus Pargmann23272a672015-10-29 11:51:16 +0100625
Josef Bacik9561a7a2016-11-22 14:04:40 -0500626 if (!nbd->task_setup)
627 nbd->task_setup = current;
628 if (nbd->task_setup != current) {
629 dev_err(disk_to_dev(nbd->disk),
630 "Device being setup by another task");
Josef Bacik9b1355d2017-04-06 17:01:56 -0400631 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500632 return -EINVAL;
Markus Pargmann23272a672015-10-29 11:51:16 +0100633 }
634
Josef Bacik9561a7a2016-11-22 14:04:40 -0500635 socks = krealloc(nbd->socks, (nbd->num_connections + 1) *
636 sizeof(struct nbd_sock *), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400637 if (!socks) {
638 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500639 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400640 }
Josef Bacik9561a7a2016-11-22 14:04:40 -0500641 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
Josef Bacik9b1355d2017-04-06 17:01:56 -0400642 if (!nsock) {
643 sockfd_put(sock);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500644 return -ENOMEM;
Josef Bacik9b1355d2017-04-06 17:01:56 -0400645 }
Markus Pargmann23272a672015-10-29 11:51:16 +0100646
Josef Bacik9561a7a2016-11-22 14:04:40 -0500647 nbd->socks = socks;
Markus Pargmann23272a672015-10-29 11:51:16 +0100648
Josef Bacik9561a7a2016-11-22 14:04:40 -0500649 mutex_init(&nsock->tx_lock);
650 nsock->sock = sock;
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400651 nsock->pending = NULL;
652 nsock->sent = 0;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500653 socks[nbd->num_connections++] = nsock;
654
Josef Bacik9442b732017-02-07 17:10:22 -0500655 if (max_part)
656 bdev->bd_invalidated = 1;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500657 return 0;
Markus Pargmann23272a672015-10-29 11:51:16 +0100658}
659
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100660/* Reset all properties of an NBD device */
661static void nbd_reset(struct nbd_device *nbd)
662{
Josef Bacik9b4a6ba2016-09-08 12:33:39 -0700663 nbd->runtime_flags = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100664 nbd->blksize = 1024;
665 nbd->bytesize = 0;
666 set_capacity(nbd->disk, 0);
667 nbd->flags = 0;
Josef Bacik0eadf372016-09-08 12:33:40 -0700668 nbd->tag_set.timeout = 0;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100669 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100670}
671
672static void nbd_bdev_reset(struct block_device *bdev)
673{
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -0400674 if (bdev->bd_openers > 1)
675 return;
Markus Pargmann0e4f0f62015-10-29 12:04:51 +0100676 set_device_ro(bdev, false);
677 bdev->bd_inode->i_size = 0;
678 if (max_part > 0) {
679 blkdev_reread_part(bdev);
680 bdev->bd_invalidated = 1;
681 }
682}
683
Markus Pargmannd02cf532015-10-29 12:06:15 +0100684static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev)
685{
686 if (nbd->flags & NBD_FLAG_READ_ONLY)
687 set_device_ro(bdev, true);
688 if (nbd->flags & NBD_FLAG_SEND_TRIM)
689 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
690 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600691 blk_queue_write_cache(nbd->disk->queue, true, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100692 else
Jens Axboeaafb1ee2016-03-30 10:10:53 -0600693 blk_queue_write_cache(nbd->disk->queue, false, false);
Markus Pargmannd02cf532015-10-29 12:06:15 +0100694}
695
Josef Bacik9561a7a2016-11-22 14:04:40 -0500696static void send_disconnects(struct nbd_device *nbd)
697{
Al Viroc9f2b6a2015-11-12 05:09:35 -0500698 struct nbd_request request = {
699 .magic = htonl(NBD_REQUEST_MAGIC),
700 .type = htonl(NBD_CMD_DISC),
701 };
702 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
703 struct iov_iter from;
Josef Bacik9561a7a2016-11-22 14:04:40 -0500704 int i, ret;
705
Josef Bacik9561a7a2016-11-22 14:04:40 -0500706 for (i = 0; i < nbd->num_connections; i++) {
Al Viroc9f2b6a2015-11-12 05:09:35 -0500707 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
Josef Bacik9dd5d3a2017-03-24 14:08:26 -0400708 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500709 if (ret <= 0)
710 dev_err(disk_to_dev(nbd->disk),
711 "Send disconnect failed %d\n", ret);
712 }
713}
714
Josef Bacik9442b732017-02-07 17:10:22 -0500715static int nbd_disconnect(struct nbd_device *nbd, struct block_device *bdev)
716{
717 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
718 if (!nbd->socks)
719 return -EINVAL;
720
721 mutex_unlock(&nbd->config_lock);
722 fsync_bdev(bdev);
723 mutex_lock(&nbd->config_lock);
724
725 /* Check again after getting mutex back. */
726 if (!nbd->socks)
727 return -EINVAL;
728
729 if (!test_and_set_bit(NBD_DISCONNECT_REQUESTED,
730 &nbd->runtime_flags))
731 send_disconnects(nbd);
732 return 0;
733}
734
735static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev)
736{
737 sock_shutdown(nbd);
738 nbd_clear_que(nbd);
Ratna Manoj Bollaabbbdf12017-03-24 14:08:29 -0400739
740 __invalidate_device(bdev, true);
Josef Bacik9442b732017-02-07 17:10:22 -0500741 nbd_bdev_reset(bdev);
742 /*
743 * We want to give the run thread a chance to wait for everybody
744 * to clean up and then do it's own cleanup.
745 */
746 if (!test_bit(NBD_RUNNING, &nbd->runtime_flags) &&
747 nbd->num_connections) {
748 int i;
749
Josef Bacik6a8a2152017-03-01 11:47:22 -0500750 for (i = 0; i < nbd->num_connections; i++) {
751 sockfd_put(nbd->socks[i]->sock);
Josef Bacik9442b732017-02-07 17:10:22 -0500752 kfree(nbd->socks[i]);
Josef Bacik6a8a2152017-03-01 11:47:22 -0500753 }
Josef Bacik9442b732017-02-07 17:10:22 -0500754 kfree(nbd->socks);
755 nbd->socks = NULL;
756 nbd->num_connections = 0;
757 }
758 nbd->task_setup = NULL;
759
760 return 0;
761}
762
763static int nbd_start_device(struct nbd_device *nbd, struct block_device *bdev)
764{
765 struct recv_thread_args *args;
766 int num_connections = nbd->num_connections;
767 int error = 0, i;
768
769 if (nbd->task_recv)
770 return -EBUSY;
771 if (!nbd->socks)
772 return -EINVAL;
773 if (num_connections > 1 &&
774 !(nbd->flags & NBD_FLAG_CAN_MULTI_CONN)) {
775 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
776 error = -EINVAL;
777 goto out_err;
778 }
779
780 set_bit(NBD_RUNNING, &nbd->runtime_flags);
781 blk_mq_update_nr_hw_queues(&nbd->tag_set, nbd->num_connections);
782 args = kcalloc(num_connections, sizeof(*args), GFP_KERNEL);
783 if (!args) {
784 error = -ENOMEM;
785 goto out_err;
786 }
787 nbd->task_recv = current;
788 mutex_unlock(&nbd->config_lock);
789
790 nbd_parse_flags(nbd, bdev);
791
792 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
793 if (error) {
794 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
795 goto out_recv;
796 }
797
798 nbd_size_update(nbd, bdev);
799
800 nbd_dev_dbg_init(nbd);
801 for (i = 0; i < num_connections; i++) {
802 sk_set_memalloc(nbd->socks[i]->sock->sk);
803 atomic_inc(&nbd->recv_threads);
804 INIT_WORK(&args[i].work, recv_work);
805 args[i].nbd = nbd;
806 args[i].index = i;
807 queue_work(recv_workqueue, &args[i].work);
808 }
809 wait_event_interruptible(nbd->recv_wq,
810 atomic_read(&nbd->recv_threads) == 0);
811 for (i = 0; i < num_connections; i++)
812 flush_work(&args[i].work);
813 nbd_dev_dbg_close(nbd);
814 nbd_size_clear(nbd, bdev);
815 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
816out_recv:
817 mutex_lock(&nbd->config_lock);
818 nbd->task_recv = NULL;
819out_err:
820 clear_bit(NBD_RUNNING, &nbd->runtime_flags);
821 nbd_clear_sock(nbd, bdev);
822
823 /* user requested, ignore socket errors */
824 if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
825 error = 0;
826 if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags))
827 error = -ETIMEDOUT;
828
829 nbd_reset(nbd);
830 return error;
831}
Markus Pargmann30d53d92015-08-17 08:20:06 +0200832
Josef Bacik9561a7a2016-11-22 14:04:40 -0500833/* Must be called with config_lock held */
Wanlong Gaof4507162012-03-28 14:42:51 -0700834static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
Pavel Machek1a2ad212009-04-02 16:58:41 -0700835 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 switch (cmd) {
Josef Bacik9442b732017-02-07 17:10:22 -0500838 case NBD_DISCONNECT:
839 return nbd_disconnect(nbd, bdev);
Markus Pargmann23272a672015-10-29 11:51:16 +0100840 case NBD_CLEAR_SOCK:
Josef Bacik9442b732017-02-07 17:10:22 -0500841 return nbd_clear_sock(nbd, bdev);
842 case NBD_SET_SOCK:
843 return nbd_add_socket(nbd, bdev, arg);
844 case NBD_SET_BLKSIZE:
Josef Bacike5445412017-02-13 10:39:47 -0500845 nbd_size_set(nbd, bdev, arg,
846 div_s64(nbd->bytesize, arg));
847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 case NBD_SET_SIZE:
Josef Bacike5445412017-02-13 10:39:47 -0500849 nbd_size_set(nbd, bdev, nbd->blksize,
850 div_s64(arg, nbd->blksize));
851 return 0;
Markus Pargmann37091fd2015-07-27 07:36:49 +0200852 case NBD_SET_SIZE_BLOCKS:
Josef Bacike5445412017-02-13 10:39:47 -0500853 nbd_size_set(nbd, bdev, nbd->blksize, arg);
854 return 0;
Paul Clements7fdfd402007-10-16 23:27:37 -0700855 case NBD_SET_TIMEOUT:
Josef Bacikf8586852017-03-24 14:08:28 -0400856 if (arg) {
857 nbd->tag_set.timeout = arg * HZ;
858 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
859 }
Paul Clements7fdfd402007-10-16 23:27:37 -0700860 return 0;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700861
Paul Clements2f012502012-10-04 17:16:15 -0700862 case NBD_SET_FLAGS:
863 nbd->flags = arg;
864 return 0;
Josef Bacik9442b732017-02-07 17:10:22 -0500865 case NBD_DO_IT:
866 return nbd_start_device(nbd, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 case NBD_CLEAR_QUE:
Herbert Xu4b2f0262006-01-06 00:09:47 -0800868 /*
869 * This is for compatibility only. The queue is always cleared
870 * by NBD_DO_IT or NBD_CLEAR_SOCK.
871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 0;
873 case NBD_PRINT_DEBUG:
Josef Bacikfd8383f2016-09-08 12:33:37 -0700874 /*
875 * For compatibility only, we no longer keep a list of
876 * outstanding requests.
877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879 }
Pavel Machek1a2ad212009-04-02 16:58:41 -0700880 return -ENOTTY;
881}
882
883static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
884 unsigned int cmd, unsigned long arg)
885{
Wanlong Gaof4507162012-03-28 14:42:51 -0700886 struct nbd_device *nbd = bdev->bd_disk->private_data;
Pavel Machek1a2ad212009-04-02 16:58:41 -0700887 int error;
888
889 if (!capable(CAP_SYS_ADMIN))
890 return -EPERM;
891
Wanlong Gaof4507162012-03-28 14:42:51 -0700892 BUG_ON(nbd->magic != NBD_MAGIC);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700893
Josef Bacik9561a7a2016-11-22 14:04:40 -0500894 mutex_lock(&nbd->config_lock);
Wanlong Gaof4507162012-03-28 14:42:51 -0700895 error = __nbd_ioctl(bdev, nbd, cmd, arg);
Josef Bacik9561a7a2016-11-22 14:04:40 -0500896 mutex_unlock(&nbd->config_lock);
Pavel Machek1a2ad212009-04-02 16:58:41 -0700897
898 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700901static const struct block_device_operations nbd_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 .owner = THIS_MODULE,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200904 .ioctl = nbd_ioctl,
Al Viro263a3df2016-01-07 10:04:37 -0500905 .compat_ioctl = nbd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906};
907
Markus Pargmann30d53d92015-08-17 08:20:06 +0200908#if IS_ENABLED(CONFIG_DEBUG_FS)
909
910static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
911{
912 struct nbd_device *nbd = s->private;
913
914 if (nbd->task_recv)
915 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
Markus Pargmann30d53d92015-08-17 08:20:06 +0200916
917 return 0;
918}
919
920static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
921{
922 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
923}
924
925static const struct file_operations nbd_dbg_tasks_ops = {
926 .open = nbd_dbg_tasks_open,
927 .read = seq_read,
928 .llseek = seq_lseek,
929 .release = single_release,
930};
931
932static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
933{
934 struct nbd_device *nbd = s->private;
935 u32 flags = nbd->flags;
936
937 seq_printf(s, "Hex: 0x%08x\n\n", flags);
938
939 seq_puts(s, "Known flags:\n");
940
941 if (flags & NBD_FLAG_HAS_FLAGS)
942 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
943 if (flags & NBD_FLAG_READ_ONLY)
944 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
945 if (flags & NBD_FLAG_SEND_FLUSH)
946 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
947 if (flags & NBD_FLAG_SEND_TRIM)
948 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
949
950 return 0;
951}
952
953static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
954{
955 return single_open(file, nbd_dbg_flags_show, inode->i_private);
956}
957
958static const struct file_operations nbd_dbg_flags_ops = {
959 .open = nbd_dbg_flags_open,
960 .read = seq_read,
961 .llseek = seq_lseek,
962 .release = single_release,
963};
964
965static int nbd_dev_dbg_init(struct nbd_device *nbd)
966{
967 struct dentry *dir;
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200968
969 if (!nbd_dbg_dir)
970 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200971
972 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200973 if (!dir) {
974 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
975 nbd_name(nbd));
976 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +0200977 }
978 nbd->dbg_dir = dir;
979
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200980 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
981 debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize);
Josef Bacik0eadf372016-09-08 12:33:40 -0700982 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
Josef Bacikef77b512016-12-02 16:19:12 -0500983 debugfs_create_u64("blocksize", 0444, dir, &nbd->blksize);
Josef Bacikd366a0f2016-06-08 10:32:10 -0400984 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
Markus Pargmann30d53d92015-08-17 08:20:06 +0200985
986 return 0;
987}
988
989static void nbd_dev_dbg_close(struct nbd_device *nbd)
990{
991 debugfs_remove_recursive(nbd->dbg_dir);
992}
993
994static int nbd_dbg_init(void)
995{
996 struct dentry *dbg_dir;
997
998 dbg_dir = debugfs_create_dir("nbd", NULL);
Markus Pargmann27ea43f2015-10-24 21:15:34 +0200999 if (!dbg_dir)
1000 return -EIO;
Markus Pargmann30d53d92015-08-17 08:20:06 +02001001
1002 nbd_dbg_dir = dbg_dir;
1003
1004 return 0;
1005}
1006
1007static void nbd_dbg_close(void)
1008{
1009 debugfs_remove_recursive(nbd_dbg_dir);
1010}
1011
1012#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1013
1014static int nbd_dev_dbg_init(struct nbd_device *nbd)
1015{
1016 return 0;
1017}
1018
1019static void nbd_dev_dbg_close(struct nbd_device *nbd)
1020{
1021}
1022
1023static int nbd_dbg_init(void)
1024{
1025 return 0;
1026}
1027
1028static void nbd_dbg_close(void)
1029{
1030}
1031
1032#endif
1033
Josef Bacikfd8383f2016-09-08 12:33:37 -07001034static int nbd_init_request(void *data, struct request *rq,
1035 unsigned int hctx_idx, unsigned int request_idx,
1036 unsigned int numa_node)
1037{
1038 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
Josef Bacikfd8383f2016-09-08 12:33:37 -07001039 cmd->nbd = data;
Josef Bacikfd8383f2016-09-08 12:33:37 -07001040 return 0;
1041}
1042
Eric Biggersf363b082017-03-30 13:39:16 -07001043static const struct blk_mq_ops nbd_mq_ops = {
Josef Bacikfd8383f2016-09-08 12:33:37 -07001044 .queue_rq = nbd_queue_rq,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001045 .init_request = nbd_init_request,
Josef Bacik0eadf372016-09-08 12:33:40 -07001046 .timeout = nbd_xmit_timeout,
Josef Bacikfd8383f2016-09-08 12:33:37 -07001047};
1048
Josef Bacikb0d91112017-02-01 16:11:40 -05001049static void nbd_dev_remove(struct nbd_device *nbd)
1050{
1051 struct gendisk *disk = nbd->disk;
1052 nbd->magic = 0;
1053 if (disk) {
1054 del_gendisk(disk);
1055 blk_cleanup_queue(disk->queue);
1056 blk_mq_free_tag_set(&nbd->tag_set);
1057 put_disk(disk);
1058 }
1059 kfree(nbd);
1060}
1061
1062static int nbd_dev_add(int index)
1063{
1064 struct nbd_device *nbd;
1065 struct gendisk *disk;
1066 struct request_queue *q;
1067 int err = -ENOMEM;
1068
1069 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1070 if (!nbd)
1071 goto out;
1072
1073 disk = alloc_disk(1 << part_shift);
1074 if (!disk)
1075 goto out_free_nbd;
1076
1077 if (index >= 0) {
1078 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1079 GFP_KERNEL);
1080 if (err == -ENOSPC)
1081 err = -EEXIST;
1082 } else {
1083 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1084 if (err >= 0)
1085 index = err;
1086 }
1087 if (err < 0)
1088 goto out_free_disk;
1089
1090 nbd->disk = disk;
1091 nbd->tag_set.ops = &nbd_mq_ops;
1092 nbd->tag_set.nr_hw_queues = 1;
1093 nbd->tag_set.queue_depth = 128;
1094 nbd->tag_set.numa_node = NUMA_NO_NODE;
1095 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1096 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1097 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
1098 nbd->tag_set.driver_data = nbd;
1099
1100 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1101 if (err)
1102 goto out_free_idr;
1103
1104 q = blk_mq_init_queue(&nbd->tag_set);
1105 if (IS_ERR(q)) {
1106 err = PTR_ERR(q);
1107 goto out_free_tags;
1108 }
1109 disk->queue = q;
1110
1111 /*
1112 * Tell the block layer that we are not a rotational device
1113 */
1114 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
1115 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1116 disk->queue->limits.discard_granularity = 512;
1117 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
Josef Bacikb0d91112017-02-01 16:11:40 -05001118 blk_queue_max_hw_sectors(disk->queue, 65536);
1119 disk->queue->limits.max_sectors = 256;
1120
1121 nbd->magic = NBD_MAGIC;
1122 mutex_init(&nbd->config_lock);
1123 disk->major = NBD_MAJOR;
1124 disk->first_minor = index << part_shift;
1125 disk->fops = &nbd_fops;
1126 disk->private_data = nbd;
1127 sprintf(disk->disk_name, "nbd%d", index);
1128 init_waitqueue_head(&nbd->recv_wq);
1129 nbd_reset(nbd);
1130 add_disk(disk);
1131 return index;
1132
1133out_free_tags:
1134 blk_mq_free_tag_set(&nbd->tag_set);
1135out_free_idr:
1136 idr_remove(&nbd_index_idr, index);
1137out_free_disk:
1138 put_disk(disk);
1139out_free_nbd:
1140 kfree(nbd);
1141out:
1142 return err;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/*
1146 * And here should be modules and kernel interface
1147 * (Just smiley confuses emacs :-)
1148 */
1149
1150static int __init nbd_init(void)
1151{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int i;
1153
Adrian Bunk5b7b18c2006-03-25 03:07:04 -08001154 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001156 if (max_part < 0) {
WANG Cong7742ce42011-08-19 14:48:28 +02001157 printk(KERN_ERR "nbd: max_part must be >= 0\n");
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001158 return -EINVAL;
1159 }
1160
1161 part_shift = 0;
Namhyung Kim5988ce22011-05-28 14:44:46 +02001162 if (max_part > 0) {
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001163 part_shift = fls(max_part);
1164
Namhyung Kim5988ce22011-05-28 14:44:46 +02001165 /*
1166 * Adjust max_part according to part_shift as it is exported
1167 * to user space so that user can know the max number of
1168 * partition kernel should be able to manage.
1169 *
1170 * Note that -1 is required because partition 0 is reserved
1171 * for the whole disk.
1172 */
1173 max_part = (1UL << part_shift) - 1;
1174 }
1175
Namhyung Kim3b271082011-05-28 14:44:46 +02001176 if ((1UL << part_shift) > DISK_MAX_PARTS)
1177 return -EINVAL;
1178
1179 if (nbds_max > 1UL << (MINORBITS - part_shift))
1180 return -EINVAL;
Josef Bacik124d6db2017-02-01 16:11:11 -05001181 recv_workqueue = alloc_workqueue("knbd-recv",
1182 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
1183 if (!recv_workqueue)
1184 return -ENOMEM;
Namhyung Kim3b271082011-05-28 14:44:46 +02001185
Josef Bacik6330a2d2017-02-15 16:49:48 -05001186 if (register_blkdev(NBD_MAJOR, "nbd")) {
1187 destroy_workqueue(recv_workqueue);
Josef Bacikb0d91112017-02-01 16:11:40 -05001188 return -EIO;
Josef Bacik6330a2d2017-02-15 16:49:48 -05001189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Markus Pargmann30d53d92015-08-17 08:20:06 +02001191 nbd_dbg_init();
1192
Josef Bacikb0d91112017-02-01 16:11:40 -05001193 mutex_lock(&nbd_index_mutex);
1194 for (i = 0; i < nbds_max; i++)
1195 nbd_dev_add(i);
1196 mutex_unlock(&nbd_index_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return 0;
Josef Bacikb0d91112017-02-01 16:11:40 -05001198}
1199
1200static int nbd_exit_cb(int id, void *ptr, void *data)
1201{
1202 struct nbd_device *nbd = ptr;
1203 nbd_dev_remove(nbd);
1204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207static void __exit nbd_cleanup(void)
1208{
Markus Pargmann30d53d92015-08-17 08:20:06 +02001209 nbd_dbg_close();
1210
Josef Bacikb0d91112017-02-01 16:11:40 -05001211 idr_for_each(&nbd_index_idr, &nbd_exit_cb, NULL);
1212 idr_destroy(&nbd_index_idr);
Josef Bacik124d6db2017-02-01 16:11:11 -05001213 destroy_workqueue(recv_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 unregister_blkdev(NBD_MAJOR, "nbd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217module_init(nbd_init);
1218module_exit(nbd_cleanup);
1219
1220MODULE_DESCRIPTION("Network Block Device");
1221MODULE_LICENSE("GPL");
1222
Lars Marowsky-Bree40be0c22005-05-01 08:59:07 -07001223module_param(nbds_max, int, 0444);
Laurent Vivierd71a6d72008-04-29 01:02:51 -07001224MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
1225module_param(max_part, int, 0444);
1226MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");