defconfig: exynos9610: Re-add dropped Wi-Fi AP options lost
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / block / nbd.c
CommitLineData
1da177e4
LT
1/*
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 *
a2531293 7 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
1da177e4
LT
8 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
9 *
dbf492d6 10 * This file is released under GPLv2 or later.
1da177e4 11 *
dbf492d6 12 * (part of code stolen from loop.c)
1da177e4
LT
13 */
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>
f1083048 21#include <linux/sched/mm.h>
1da177e4
LT
22#include <linux/fs.h>
23#include <linux/bio.h>
24#include <linux/stat.h>
25#include <linux/errno.h>
26#include <linux/file.h>
27#include <linux/ioctl.h>
2a48fc0a 28#include <linux/mutex.h>
4b2f0260
HX
29#include <linux/compiler.h>
30#include <linux/err.h>
31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
1da177e4 33#include <net/sock.h>
91cf45f0 34#include <linux/net.h>
48cf6061 35#include <linux/kthread.h>
b9c495bb 36#include <linux/types.h>
30d53d9c 37#include <linux/debugfs.h>
fd8383fd 38#include <linux/blk-mq.h>
1da177e4 39
7c0f6ba6 40#include <linux/uaccess.h>
1da177e4
LT
41#include <asm/types.h>
42
43#include <linux/nbd.h>
e46c7287
JB
44#include <linux/nbd-netlink.h>
45#include <net/genetlink.h>
1da177e4 46
b0d9111a
JB
47static DEFINE_IDR(nbd_index_idr);
48static DEFINE_MUTEX(nbd_index_mutex);
47d902b9 49static int nbd_total_devices = 0;
b0d9111a 50
9561a7ad
JB
51struct nbd_sock {
52 struct socket *sock;
53 struct mutex tx_lock;
9dd5d3ab
JB
54 struct request *pending;
55 int sent;
f3733247
JB
56 bool dead;
57 int fallback_index;
799f9a38 58 int cookie;
9561a7ad
JB
59};
60
5ea8d108
JB
61struct recv_thread_args {
62 struct work_struct work;
63 struct nbd_device *nbd;
64 int index;
65};
66
799f9a38
JB
67struct link_dead_args {
68 struct work_struct work;
69 int index;
70};
71
9b4a6ba9
JB
72#define NBD_TIMEDOUT 0
73#define NBD_DISCONNECT_REQUESTED 1
9561a7ad 74#define NBD_DISCONNECTED 2
5ea8d108 75#define NBD_HAS_PID_FILE 3
e46c7287
JB
76#define NBD_HAS_CONFIG_REF 4
77#define NBD_BOUND 5
a2c97909 78#define NBD_DESTROY_ON_DISCONNECT 6
9b0b6258 79#define NBD_DISCONNECT_ON_CLOSE 7
9b4a6ba9 80
5ea8d108 81struct nbd_config {
22d109c1 82 u32 flags;
9b4a6ba9 83 unsigned long runtime_flags;
560bc4b3 84 u64 dead_conn_timeout;
13e71d69 85
5ea8d108 86 struct nbd_sock **socks;
9561a7ad 87 int num_connections;
560bc4b3
JB
88 atomic_t live_connections;
89 wait_queue_head_t conn_wait;
5ea8d108 90
9561a7ad
JB
91 atomic_t recv_threads;
92 wait_queue_head_t recv_wq;
ef77b515 93 loff_t blksize;
b9c495bb 94 loff_t bytesize;
30d53d9c
MP
95#if IS_ENABLED(CONFIG_DEBUG_FS)
96 struct dentry *dbg_dir;
97#endif
13e71d69
MP
98};
99
5ea8d108
JB
100struct nbd_device {
101 struct blk_mq_tag_set tag_set;
102
e46c7287 103 int index;
5ea8d108 104 refcount_t config_refs;
c6a4759e 105 refcount_t refs;
5ea8d108
JB
106 struct nbd_config *config;
107 struct mutex config_lock;
108 struct gendisk *disk;
109
c6a4759e 110 struct list_head list;
5ea8d108
JB
111 struct task_struct *task_recv;
112 struct task_struct *task_setup;
113};
114
ced413c5
JB
115#define NBD_CMD_REQUEUED 1
116
fd8383fd
JB
117struct nbd_cmd {
118 struct nbd_device *nbd;
05ee6166 119 struct mutex lock;
f3733247 120 int index;
799f9a38 121 int cookie;
2a842aca 122 blk_status_t status;
ced413c5 123 unsigned long flags;
05ee6166 124 u32 cmd_cookie;
fd8383fd
JB
125};
126
30d53d9c
MP
127#if IS_ENABLED(CONFIG_DEBUG_FS)
128static struct dentry *nbd_dbg_dir;
129#endif
130
131#define nbd_name(nbd) ((nbd)->disk->disk_name)
132
f4507164 133#define NBD_MAGIC 0x68797548
1da177e4 134
9c7a4169 135static unsigned int nbds_max = 16;
7a8362a0 136static int max_part = 16;
124d6db0 137static struct workqueue_struct *recv_workqueue;
b0d9111a 138static int part_shift;
1da177e4 139
9442b739
JB
140static int nbd_dev_dbg_init(struct nbd_device *nbd);
141static void nbd_dev_dbg_close(struct nbd_device *nbd);
5ea8d108 142static void nbd_config_put(struct nbd_device *nbd);
e46c7287 143static void nbd_connect_reply(struct genl_info *info, int index);
47d902b9 144static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info);
799f9a38 145static void nbd_dead_link_work(struct work_struct *work);
9b0b6258 146static void nbd_disconnect_and_put(struct nbd_device *nbd);
9442b739 147
d18509f5 148static inline struct device *nbd_to_dev(struct nbd_device *nbd)
1da177e4 149{
d18509f5 150 return disk_to_dev(nbd->disk);
1da177e4
LT
151}
152
ced413c5
JB
153static void nbd_requeue_cmd(struct nbd_cmd *cmd)
154{
155 struct request *req = blk_mq_rq_from_pdu(cmd);
156
157 if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
158 blk_mq_requeue_request(req, true);
159}
160
05ee6166
JB
161#define NBD_COOKIE_BITS 32
162
163static u64 nbd_cmd_handle(struct nbd_cmd *cmd)
164{
165 struct request *req = blk_mq_rq_from_pdu(cmd);
166 u32 tag = blk_mq_unique_tag(req);
167 u64 cookie = cmd->cmd_cookie;
168
169 return (cookie << NBD_COOKIE_BITS) | tag;
170}
171
172static u32 nbd_handle_to_tag(u64 handle)
173{
174 return (u32)handle;
175}
176
177static u32 nbd_handle_to_cookie(u64 handle)
178{
179 return (u32)(handle >> NBD_COOKIE_BITS);
180}
181
1da177e4
LT
182static const char *nbdcmd_to_ascii(int cmd)
183{
184 switch (cmd) {
185 case NBD_CMD_READ: return "read";
186 case NBD_CMD_WRITE: return "write";
187 case NBD_CMD_DISC: return "disconnect";
75f187ab 188 case NBD_CMD_FLUSH: return "flush";
a336d298 189 case NBD_CMD_TRIM: return "trim/discard";
1da177e4
LT
190 }
191 return "invalid";
192}
1da177e4 193
5ea8d108
JB
194static ssize_t pid_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
196{
197 struct gendisk *disk = dev_to_disk(dev);
198 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
199
200 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
201}
202
dfbde552 203static const struct device_attribute pid_attr = {
5ea8d108
JB
204 .attr = { .name = "pid", .mode = S_IRUGO},
205 .show = pid_show,
206};
207
c6a4759e
JB
208static void nbd_dev_remove(struct nbd_device *nbd)
209{
210 struct gendisk *disk = nbd->disk;
edee2e82
JB
211 struct request_queue *q;
212
c6a4759e 213 if (disk) {
edee2e82 214 q = disk->queue;
c6a4759e 215 del_gendisk(disk);
edee2e82 216 blk_cleanup_queue(q);
c6a4759e 217 blk_mq_free_tag_set(&nbd->tag_set);
a2c97909 218 disk->private_data = NULL;
c6a4759e
JB
219 put_disk(disk);
220 }
221 kfree(nbd);
222}
223
224static void nbd_put(struct nbd_device *nbd)
225{
226 if (refcount_dec_and_mutex_lock(&nbd->refs,
227 &nbd_index_mutex)) {
228 idr_remove(&nbd_index_idr, nbd->index);
229 mutex_unlock(&nbd_index_mutex);
230 nbd_dev_remove(nbd);
231 }
232}
233
799f9a38
JB
234static int nbd_disconnected(struct nbd_config *config)
235{
236 return test_bit(NBD_DISCONNECTED, &config->runtime_flags) ||
237 test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
238}
239
240static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
241 int notify)
f3733247 242{
799f9a38
JB
243 if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
244 struct link_dead_args *args;
245 args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO);
246 if (args) {
247 INIT_WORK(&args->work, nbd_dead_link_work);
248 args->index = nbd->index;
249 queue_work(system_wq, &args->work);
250 }
251 }
560bc4b3 252 if (!nsock->dead) {
f3733247 253 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
560bc4b3
JB
254 atomic_dec(&nbd->config->live_connections);
255 }
f3733247
JB
256 nsock->dead = true;
257 nsock->pending = NULL;
258 nsock->sent = 0;
259}
260
29eaadc0 261static void nbd_size_clear(struct nbd_device *nbd)
37091fdd 262{
5ea8d108 263 if (nbd->config->bytesize) {
5ea8d108
JB
264 set_capacity(nbd->disk, 0);
265 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
266 }
37091fdd
MP
267}
268
29eaadc0 269static void nbd_size_update(struct nbd_device *nbd)
37091fdd 270{
5ea8d108 271 struct nbd_config *config = nbd->config;
00946218
JB
272 struct block_device *bdev = bdget_disk(nbd->disk, 0);
273
5ea8d108
JB
274 blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
275 blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
5ea8d108 276 set_capacity(nbd->disk, config->bytesize >> 9);
00946218 277 if (bdev) {
c2912ca3 278 if (bdev->bd_disk) {
00946218 279 bd_set_size(bdev, config->bytesize);
c2912ca3
JK
280 set_blocksize(bdev, config->blksize);
281 } else
00946218
JB
282 bdev->bd_invalidated = 1;
283 bdput(bdev);
284 }
37091fdd
MP
285 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
286}
287
29eaadc0
JB
288static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
289 loff_t nr_blocks)
37091fdd 290{
5ea8d108
JB
291 struct nbd_config *config = nbd->config;
292 config->blksize = blocksize;
293 config->bytesize = blocksize * nr_blocks;
a477d005
JB
294 if (nbd->task_recv != NULL)
295 nbd_size_update(nbd);
37091fdd
MP
296}
297
1e388ae0 298static void nbd_complete_rq(struct request *req)
1da177e4 299{
1e388ae0 300 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
1da177e4 301
1e388ae0
CH
302 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", cmd,
303 cmd->status ? "failed" : "done");
1da177e4 304
1e388ae0 305 blk_mq_end_request(req, cmd->status);
1da177e4
LT
306}
307
e018e757
MP
308/*
309 * Forcibly shutdown the socket causing all listeners to error
310 */
36e47bee 311static void sock_shutdown(struct nbd_device *nbd)
7fdfd406 312{
5ea8d108 313 struct nbd_config *config = nbd->config;
9561a7ad 314 int i;
23272a67 315
5ea8d108 316 if (config->num_connections == 0)
9561a7ad 317 return;
5ea8d108 318 if (test_and_set_bit(NBD_DISCONNECTED, &config->runtime_flags))
260bbce4 319 return;
23272a67 320
5ea8d108
JB
321 for (i = 0; i < config->num_connections; i++) {
322 struct nbd_sock *nsock = config->socks[i];
9561a7ad 323 mutex_lock(&nsock->tx_lock);
799f9a38 324 nbd_mark_nsock_dead(nbd, nsock, 0);
9561a7ad
JB
325 mutex_unlock(&nsock->tx_lock);
326 }
327 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
7fdfd406
PC
328}
329
0eadf37a
JB
330static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
331 bool reserved)
7fdfd406 332{
0eadf37a
JB
333 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
334 struct nbd_device *nbd = cmd->nbd;
5ea8d108
JB
335 struct nbd_config *config;
336
337 if (!refcount_inc_not_zero(&nbd->config_refs)) {
2a842aca 338 cmd->status = BLK_STS_TIMEOUT;
5ea8d108
JB
339 return BLK_EH_HANDLED;
340 }
5ea8d108 341 config = nbd->config;
dcc909d9 342
05ee6166
JB
343 if (!mutex_trylock(&cmd->lock))
344 return BLK_EH_RESET_TIMER;
345
5ea8d108 346 if (config->num_connections > 1) {
f3733247
JB
347 dev_err_ratelimited(nbd_to_dev(nbd),
348 "Connection timed out, retrying\n");
f3733247
JB
349 /*
350 * Hooray we have more connections, requeue this IO, the submit
351 * path will put it on a real connection.
352 */
5ea8d108
JB
353 if (config->socks && config->num_connections > 1) {
354 if (cmd->index < config->num_connections) {
f3733247 355 struct nbd_sock *nsock =
5ea8d108 356 config->socks[cmd->index];
f3733247 357 mutex_lock(&nsock->tx_lock);
799f9a38
JB
358 /* We can have multiple outstanding requests, so
359 * we don't want to mark the nsock dead if we've
360 * already reconnected with a new socket, so
361 * only mark it dead if its the same socket we
362 * were sent out on.
363 */
364 if (cmd->cookie == nsock->cookie)
365 nbd_mark_nsock_dead(nbd, nsock, 1);
f3733247
JB
366 mutex_unlock(&nsock->tx_lock);
367 }
05ee6166 368 mutex_unlock(&cmd->lock);
ced413c5 369 nbd_requeue_cmd(cmd);
5ea8d108 370 nbd_config_put(nbd);
f3733247
JB
371 return BLK_EH_NOT_HANDLED;
372 }
f3733247
JB
373 } else {
374 dev_err_ratelimited(nbd_to_dev(nbd),
375 "Connection timed out\n");
376 }
5ea8d108 377 set_bit(NBD_TIMEDOUT, &config->runtime_flags);
2a842aca 378 cmd->status = BLK_STS_IOERR;
05ee6166 379 mutex_unlock(&cmd->lock);
9561a7ad 380 sock_shutdown(nbd);
5ea8d108
JB
381 nbd_config_put(nbd);
382
0eadf37a 383 return BLK_EH_HANDLED;
7fdfd406
PC
384}
385
1da177e4
LT
386/*
387 * Send or receive packet.
388 */
c9f2b6ae 389static int sock_xmit(struct nbd_device *nbd, int index, int send,
9dd5d3ab 390 struct iov_iter *iter, int msg_flags, int *sent)
1da177e4 391{
5ea8d108
JB
392 struct nbd_config *config = nbd->config;
393 struct socket *sock = config->socks[index]->sock;
1da177e4
LT
394 int result;
395 struct msghdr msg;
f1083048 396 unsigned int noreclaim_flag;
1da177e4 397
ffc41cf8 398 if (unlikely(!sock)) {
a897b666 399 dev_err_ratelimited(disk_to_dev(nbd->disk),
7f1b90f9
WC
400 "Attempted %s on closed socket in sock_xmit\n",
401 (send ? "send" : "recv"));
ffc41cf8
MS
402 return -EINVAL;
403 }
404
c9f2b6ae 405 msg.msg_iter = *iter;
c1696cab 406
f1083048 407 noreclaim_flag = memalloc_noreclaim_save();
1da177e4 408 do {
7f338fe4 409 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
1da177e4
LT
410 msg.msg_name = NULL;
411 msg.msg_namelen = 0;
412 msg.msg_control = NULL;
413 msg.msg_controllen = 0;
1da177e4
LT
414 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
415
7e2893a1 416 if (send)
c1696cab 417 result = sock_sendmsg(sock, &msg);
7e2893a1 418 else
c1696cab 419 result = sock_recvmsg(sock, &msg, msg.msg_flags);
1da177e4 420
1da177e4
LT
421 if (result <= 0) {
422 if (result == 0)
423 result = -EPIPE; /* short read */
424 break;
425 }
9dd5d3ab
JB
426 if (sent)
427 *sent += result;
c1696cab 428 } while (msg_data_left(&msg));
1da177e4 429
f1083048 430 memalloc_noreclaim_restore(noreclaim_flag);
1da177e4
LT
431
432 return result;
433}
434
32e67a3a
JB
435/*
436 * Different settings for sk->sk_sndtimeo can result in different return values
437 * if there is a signal pending when we enter sendmsg, because reasons?
438 */
439static inline int was_interrupted(int result)
440{
441 return result == -ERESTARTSYS || result == -EINTR;
442}
443
7fdfd406 444/* always call with the tx_lock held */
9561a7ad 445static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
1da177e4 446{
fd8383fd 447 struct request *req = blk_mq_rq_from_pdu(cmd);
5ea8d108
JB
448 struct nbd_config *config = nbd->config;
449 struct nbd_sock *nsock = config->socks[index];
d61b7f97 450 int result;
c9f2b6ae
AV
451 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
452 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
453 struct iov_iter from;
1011c1b9 454 unsigned long size = blk_rq_bytes(req);
429a787b 455 struct bio *bio;
05ee6166 456 u64 handle;
9dc6c806 457 u32 type;
685c9b24 458 u32 nbd_cmd_flags = 0;
9dd5d3ab 459 int sent = nsock->sent, skip = 0;
9dc6c806 460
c9f2b6ae
AV
461 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
462
aebf526b
CH
463 switch (req_op(req)) {
464 case REQ_OP_DISCARD:
9dc6c806 465 type = NBD_CMD_TRIM;
aebf526b
CH
466 break;
467 case REQ_OP_FLUSH:
9dc6c806 468 type = NBD_CMD_FLUSH;
aebf526b
CH
469 break;
470 case REQ_OP_WRITE:
9dc6c806 471 type = NBD_CMD_WRITE;
aebf526b
CH
472 break;
473 case REQ_OP_READ:
9dc6c806 474 type = NBD_CMD_READ;
aebf526b
CH
475 break;
476 default:
477 return -EIO;
478 }
1da177e4 479
09fc54cc 480 if (rq_data_dir(req) == WRITE &&
5ea8d108 481 (config->flags & NBD_FLAG_READ_ONLY)) {
09fc54cc
CH
482 dev_err_ratelimited(disk_to_dev(nbd->disk),
483 "Write on read-only\n");
484 return -EIO;
485 }
486
685c9b24
SM
487 if (req->cmd_flags & REQ_FUA)
488 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
489
9dd5d3ab
JB
490 /* We did a partial send previously, and we at least sent the whole
491 * request struct, so just go and send the rest of the pages in the
492 * request.
493 */
494 if (sent) {
495 if (sent >= sizeof(request)) {
496 skip = sent - sizeof(request);
497 goto send_pages;
498 }
499 iov_iter_advance(&from, sent);
05ee6166
JB
500 } else {
501 cmd->cmd_cookie++;
9dd5d3ab 502 }
f3733247 503 cmd->index = index;
799f9a38 504 cmd->cookie = nsock->cookie;
685c9b24 505 request.type = htonl(type | nbd_cmd_flags);
9561a7ad 506 if (type != NBD_CMD_FLUSH) {
75f187ab
AB
507 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
508 request.len = htonl(size);
509 }
05ee6166
JB
510 handle = nbd_cmd_handle(cmd);
511 memcpy(request.handle, &handle, sizeof(handle));
1da177e4 512
d18509f5 513 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
fd8383fd 514 cmd, nbdcmd_to_ascii(type),
d18509f5 515 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
c9f2b6ae 516 result = sock_xmit(nbd, index, 1, &from,
9dd5d3ab 517 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
1da177e4 518 if (result <= 0) {
32e67a3a 519 if (was_interrupted(result)) {
9dd5d3ab
JB
520 /* If we havne't sent anything we can just return BUSY,
521 * however if we have sent something we need to make
522 * sure we only allow this req to be sent until we are
523 * completely done.
524 */
525 if (sent) {
526 nsock->pending = req;
527 nsock->sent = sent;
528 }
ced413c5 529 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
fc17b653 530 return BLK_STS_RESOURCE;
9dd5d3ab 531 }
a897b666 532 dev_err_ratelimited(disk_to_dev(nbd->disk),
7f1b90f9 533 "Send control failed (result %d)\n", result);
f3733247 534 return -EAGAIN;
1da177e4 535 }
9dd5d3ab 536send_pages:
429a787b 537 if (type != NBD_CMD_WRITE)
9dd5d3ab 538 goto out;
429a787b 539
429a787b
JA
540 bio = req->bio;
541 while (bio) {
542 struct bio *next = bio->bi_next;
543 struct bvec_iter iter;
7988613b 544 struct bio_vec bvec;
429a787b
JA
545
546 bio_for_each_segment(bvec, bio, iter) {
547 bool is_last = !next && bio_iter_last(bvec, iter);
d61b7f97 548 int flags = is_last ? 0 : MSG_MORE;
429a787b 549
d18509f5 550 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
fd8383fd 551 cmd, bvec.bv_len);
c9f2b6ae
AV
552 iov_iter_bvec(&from, ITER_BVEC | WRITE,
553 &bvec, 1, bvec.bv_len);
9dd5d3ab
JB
554 if (skip) {
555 if (skip >= iov_iter_count(&from)) {
556 skip -= iov_iter_count(&from);
557 continue;
558 }
559 iov_iter_advance(&from, skip);
560 skip = 0;
561 }
562 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
6c92e699 563 if (result <= 0) {
32e67a3a 564 if (was_interrupted(result)) {
9dd5d3ab
JB
565 /* We've already sent the header, we
566 * have no choice but to set pending and
567 * return BUSY.
568 */
569 nsock->pending = req;
570 nsock->sent = sent;
ced413c5 571 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
fc17b653 572 return BLK_STS_RESOURCE;
9dd5d3ab 573 }
f4507164 574 dev_err(disk_to_dev(nbd->disk),
7f1b90f9
WC
575 "Send data failed (result %d)\n",
576 result);
f3733247 577 return -EAGAIN;
6c92e699 578 }
429a787b
JA
579 /*
580 * The completion might already have come in,
581 * so break for the last one instead of letting
582 * the iterator do it. This prevents use-after-free
583 * of the bio.
584 */
585 if (is_last)
586 break;
1da177e4 587 }
429a787b 588 bio = next;
1da177e4 589 }
9dd5d3ab
JB
590out:
591 nsock->pending = NULL;
592 nsock->sent = 0;
1da177e4 593 return 0;
1da177e4
LT
594}
595
1da177e4 596/* NULL returned = something went wrong, inform userspace */
9561a7ad 597static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
1da177e4 598{
5ea8d108 599 struct nbd_config *config = nbd->config;
1da177e4
LT
600 int result;
601 struct nbd_reply reply;
fd8383fd
JB
602 struct nbd_cmd *cmd;
603 struct request *req = NULL;
05ee6166 604 u64 handle;
fd8383fd 605 u16 hwq;
9561a7ad 606 u32 tag;
c9f2b6ae
AV
607 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
608 struct iov_iter to;
05ee6166 609 int ret = 0;
1da177e4
LT
610
611 reply.magic = 0;
c9f2b6ae 612 iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply));
9dd5d3ab 613 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
1da177e4 614 if (result <= 0) {
5ea8d108 615 if (!nbd_disconnected(config))
9561a7ad
JB
616 dev_err(disk_to_dev(nbd->disk),
617 "Receive control failed (result %d)\n", result);
19391830 618 return ERR_PTR(result);
1da177e4 619 }
e4b57e08
MF
620
621 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
f4507164 622 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
e4b57e08 623 (unsigned long)ntohl(reply.magic));
19391830 624 return ERR_PTR(-EPROTO);
e4b57e08
MF
625 }
626
05ee6166
JB
627 memcpy(&handle, reply.handle, sizeof(handle));
628 tag = nbd_handle_to_tag(handle);
fd8383fd
JB
629 hwq = blk_mq_unique_tag_to_hwq(tag);
630 if (hwq < nbd->tag_set.nr_hw_queues)
631 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
632 blk_mq_unique_tag_to_tag(tag));
633 if (!req || !blk_mq_request_started(req)) {
634 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
635 tag, req);
636 return ERR_PTR(-ENOENT);
1da177e4 637 }
fd8383fd 638 cmd = blk_mq_rq_to_pdu(req);
05ee6166
JB
639
640 mutex_lock(&cmd->lock);
641 if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
642 dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
643 req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
644 ret = -ENOENT;
645 goto out;
646 }
647 if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) {
648 dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n",
649 req);
650 ret = -ENOENT;
651 goto out;
652 }
1da177e4 653 if (ntohl(reply.error)) {
f4507164 654 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
7f1b90f9 655 ntohl(reply.error));
2a842aca 656 cmd->status = BLK_STS_IOERR;
05ee6166 657 goto out;
1da177e4
LT
658 }
659
fd8383fd 660 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd);
9dc6c806 661 if (rq_data_dir(req) != WRITE) {
5705f702 662 struct req_iterator iter;
7988613b 663 struct bio_vec bvec;
5705f702
N
664
665 rq_for_each_segment(bvec, req, iter) {
c9f2b6ae
AV
666 iov_iter_bvec(&to, ITER_BVEC | READ,
667 &bvec, 1, bvec.bv_len);
9dd5d3ab 668 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
6c92e699 669 if (result <= 0) {
f4507164 670 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
7f1b90f9 671 result);
f3733247
JB
672 /*
673 * If we've disconnected or we only have 1
674 * connection then we need to make sure we
675 * complete this request, otherwise error out
676 * and let the timeout stuff handle resubmitting
677 * this request onto another connection.
678 */
5ea8d108
JB
679 if (nbd_disconnected(config) ||
680 config->num_connections <= 1) {
2a842aca 681 cmd->status = BLK_STS_IOERR;
05ee6166 682 goto out;
f3733247 683 }
05ee6166
JB
684 ret = -EIO;
685 goto out;
6c92e699 686 }
d18509f5 687 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
fd8383fd 688 cmd, bvec.bv_len);
1da177e4
LT
689 }
690 }
05ee6166
JB
691out:
692 mutex_unlock(&cmd->lock);
693 return ret ? ERR_PTR(ret) : cmd;
1da177e4
LT
694}
695
9561a7ad 696static void recv_work(struct work_struct *work)
1da177e4 697{
9561a7ad
JB
698 struct recv_thread_args *args = container_of(work,
699 struct recv_thread_args,
700 work);
701 struct nbd_device *nbd = args->nbd;
5ea8d108 702 struct nbd_config *config = nbd->config;
fd8383fd 703 struct nbd_cmd *cmd;
1da177e4 704
19391830 705 while (1) {
9561a7ad 706 cmd = nbd_read_stat(nbd, args->index);
fd8383fd 707 if (IS_ERR(cmd)) {
5ea8d108 708 struct nbd_sock *nsock = config->socks[args->index];
f3733247
JB
709
710 mutex_lock(&nsock->tx_lock);
799f9a38 711 nbd_mark_nsock_dead(nbd, nsock, 1);
f3733247 712 mutex_unlock(&nsock->tx_lock);
19391830
MP
713 break;
714 }
715
08e0029a 716 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd));
19391830 717 }
5ea8d108
JB
718 atomic_dec(&config->recv_threads);
719 wake_up(&config->recv_wq);
720 nbd_config_put(nbd);
721 kfree(args);
1da177e4
LT
722}
723
fd8383fd 724static void nbd_clear_req(struct request *req, void *data, bool reserved)
1da177e4 725{
fd8383fd 726 struct nbd_cmd *cmd;
1da177e4 727
fd8383fd
JB
728 if (!blk_mq_request_started(req))
729 return;
730 cmd = blk_mq_rq_to_pdu(req);
2a842aca 731 cmd->status = BLK_STS_IOERR;
08e0029a 732 blk_mq_complete_request(req);
fd8383fd
JB
733}
734
735static void nbd_clear_que(struct nbd_device *nbd)
736{
b52c2e92 737 blk_mq_quiesce_queue(nbd->disk->queue);
fd8383fd 738 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
b52c2e92 739 blk_mq_unquiesce_queue(nbd->disk->queue);
e78273c8 740 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
1da177e4
LT
741}
742
f3733247
JB
743static int find_fallback(struct nbd_device *nbd, int index)
744{
5ea8d108 745 struct nbd_config *config = nbd->config;
f3733247 746 int new_index = -1;
5ea8d108 747 struct nbd_sock *nsock = config->socks[index];
f3733247
JB
748 int fallback = nsock->fallback_index;
749
5ea8d108 750 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
f3733247
JB
751 return new_index;
752
5ea8d108 753 if (config->num_connections <= 1) {
f3733247
JB
754 dev_err_ratelimited(disk_to_dev(nbd->disk),
755 "Attempted send on invalid socket\n");
756 return new_index;
757 }
758
5ea8d108
JB
759 if (fallback >= 0 && fallback < config->num_connections &&
760 !config->socks[fallback]->dead)
f3733247
JB
761 return fallback;
762
763 if (nsock->fallback_index < 0 ||
5ea8d108
JB
764 nsock->fallback_index >= config->num_connections ||
765 config->socks[nsock->fallback_index]->dead) {
f3733247 766 int i;
5ea8d108 767 for (i = 0; i < config->num_connections; i++) {
f3733247
JB
768 if (i == index)
769 continue;
5ea8d108 770 if (!config->socks[i]->dead) {
f3733247
JB
771 new_index = i;
772 break;
773 }
774 }
775 nsock->fallback_index = new_index;
776 if (new_index < 0) {
777 dev_err_ratelimited(disk_to_dev(nbd->disk),
778 "Dead connection, failed to find a fallback\n");
779 return new_index;
780 }
781 }
782 new_index = nsock->fallback_index;
783 return new_index;
784}
7fdfd406 785
560bc4b3
JB
786static int wait_for_reconnect(struct nbd_device *nbd)
787{
788 struct nbd_config *config = nbd->config;
789 if (!config->dead_conn_timeout)
790 return 0;
791 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
792 return 0;
f6b7c54c
JB
793 wait_event_timeout(config->conn_wait,
794 atomic_read(&config->live_connections),
795 config->dead_conn_timeout);
560bc4b3
JB
796 return atomic_read(&config->live_connections);
797}
798
9dd5d3ab 799static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
48cf6061 800{
fd8383fd
JB
801 struct request *req = blk_mq_rq_from_pdu(cmd);
802 struct nbd_device *nbd = cmd->nbd;
5ea8d108 803 struct nbd_config *config;
9561a7ad 804 struct nbd_sock *nsock;
9dd5d3ab 805 int ret;
fd8383fd 806
5ea8d108
JB
807 if (!refcount_inc_not_zero(&nbd->config_refs)) {
808 dev_err_ratelimited(disk_to_dev(nbd->disk),
809 "Socks array is empty\n");
2031e243 810 blk_mq_start_request(req);
5ea8d108
JB
811 return -EINVAL;
812 }
813 config = nbd->config;
814
815 if (index >= config->num_connections) {
a897b666
JB
816 dev_err_ratelimited(disk_to_dev(nbd->disk),
817 "Attempted send on invalid socket\n");
5ea8d108 818 nbd_config_put(nbd);
2031e243 819 blk_mq_start_request(req);
9dd5d3ab 820 return -EINVAL;
9561a7ad 821 }
2a842aca 822 cmd->status = BLK_STS_OK;
f3733247 823again:
5ea8d108 824 nsock = config->socks[index];
9561a7ad 825 mutex_lock(&nsock->tx_lock);
f3733247 826 if (nsock->dead) {
560bc4b3 827 int old_index = index;
f3733247 828 index = find_fallback(nbd, index);
560bc4b3 829 mutex_unlock(&nsock->tx_lock);
5ea8d108 830 if (index < 0) {
560bc4b3
JB
831 if (wait_for_reconnect(nbd)) {
832 index = old_index;
833 goto again;
834 }
835 /* All the sockets should already be down at this point,
836 * we just want to make sure that DISCONNECTED is set so
837 * any requests that come in that were queue'ed waiting
838 * for the reconnect timer don't trigger the timer again
839 * and instead just error out.
840 */
841 sock_shutdown(nbd);
842 nbd_config_put(nbd);
2031e243 843 blk_mq_start_request(req);
560bc4b3 844 return -EIO;
5ea8d108 845 }
f3733247 846 goto again;
48cf6061
LV
847 }
848
9dd5d3ab
JB
849 /* Handle the case that we have a pending request that was partially
850 * transmitted that _has_ to be serviced first. We need to call requeue
851 * here so that it gets put _after_ the request that is already on the
852 * dispatch list.
853 */
2031e243 854 blk_mq_start_request(req);
9dd5d3ab 855 if (unlikely(nsock->pending && nsock->pending != req)) {
ced413c5 856 nbd_requeue_cmd(cmd);
9dd5d3ab
JB
857 ret = 0;
858 goto out;
48cf6061 859 }
f3733247
JB
860 /*
861 * Some failures are related to the link going down, so anything that
862 * returns EAGAIN can be retried on a different socket.
863 */
9dd5d3ab 864 ret = nbd_send_cmd(nbd, cmd, index);
f3733247
JB
865 if (ret == -EAGAIN) {
866 dev_err_ratelimited(disk_to_dev(nbd->disk),
2031e243 867 "Request send failed, requeueing\n");
799f9a38 868 nbd_mark_nsock_dead(nbd, nsock, 1);
ced413c5 869 nbd_requeue_cmd(cmd);
2031e243 870 ret = 0;
f3733247 871 }
9dd5d3ab 872out:
9561a7ad 873 mutex_unlock(&nsock->tx_lock);
5ea8d108 874 nbd_config_put(nbd);
9dd5d3ab 875 return ret;
48cf6061
LV
876}
877
fc17b653 878static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
fd8383fd 879 const struct blk_mq_queue_data *bd)
1da177e4 880{
fd8383fd 881 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
9dd5d3ab 882 int ret;
1da177e4 883
9561a7ad
JB
884 /*
885 * Since we look at the bio's to send the request over the network we
886 * need to make sure the completion work doesn't mark this request done
887 * before we are done doing our send. This keeps us from dereferencing
888 * freed data if we have particularly fast completions (ie we get the
889 * completion before we exit sock_xmit on the last bvec) or in the case
890 * that the server is misbehaving (or there was an error) before we're
891 * done sending everything over the wire.
892 */
05ee6166 893 mutex_lock(&cmd->lock);
ced413c5 894 clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
9dd5d3ab
JB
895
896 /* We can be called directly from the user space process, which means we
897 * could possibly have signals pending so our sendmsg will fail. In
898 * this case we need to return that we are busy, otherwise error out as
899 * appropriate.
900 */
901 ret = nbd_handle_cmd(cmd, hctx->queue_num);
6e60a3bb
JB
902 if (ret < 0)
903 ret = BLK_STS_IOERR;
904 else if (!ret)
905 ret = BLK_STS_OK;
05ee6166 906 mutex_unlock(&cmd->lock);
9561a7ad 907
6e60a3bb 908 return ret;
1da177e4
LT
909}
910
e46c7287
JB
911static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
912 bool netlink)
23272a67 913{
5ea8d108 914 struct nbd_config *config = nbd->config;
9442b739 915 struct socket *sock;
9561a7ad
JB
916 struct nbd_sock **socks;
917 struct nbd_sock *nsock;
9442b739
JB
918 int err;
919
920 sock = sockfd_lookup(arg, &err);
921 if (!sock)
922 return err;
23272a67 923
e46c7287
JB
924 if (!netlink && !nbd->task_setup &&
925 !test_bit(NBD_BOUND, &config->runtime_flags))
9561a7ad 926 nbd->task_setup = current;
e46c7287
JB
927
928 if (!netlink &&
929 (nbd->task_setup != current ||
930 test_bit(NBD_BOUND, &config->runtime_flags))) {
9561a7ad
JB
931 dev_err(disk_to_dev(nbd->disk),
932 "Device being setup by another task");
9b1355d5 933 sockfd_put(sock);
e46c7287 934 return -EBUSY;
23272a67
MP
935 }
936
5ea8d108 937 socks = krealloc(config->socks, (config->num_connections + 1) *
9561a7ad 938 sizeof(struct nbd_sock *), GFP_KERNEL);
9b1355d5
JB
939 if (!socks) {
940 sockfd_put(sock);
9561a7ad 941 return -ENOMEM;
9b1355d5 942 }
9561a7ad 943 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
9b1355d5
JB
944 if (!nsock) {
945 sockfd_put(sock);
9561a7ad 946 return -ENOMEM;
9b1355d5 947 }
9561a7ad 948
5ea8d108 949 config->socks = socks;
23272a67 950
f3733247
JB
951 nsock->fallback_index = -1;
952 nsock->dead = false;
9561a7ad
JB
953 mutex_init(&nsock->tx_lock);
954 nsock->sock = sock;
9dd5d3ab
JB
955 nsock->pending = NULL;
956 nsock->sent = 0;
799f9a38 957 nsock->cookie = 0;
5ea8d108 958 socks[config->num_connections++] = nsock;
560bc4b3 959 atomic_inc(&config->live_connections);
23272a67 960
9561a7ad 961 return 0;
23272a67
MP
962}
963
b7aa3d39
JB
964static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
965{
966 struct nbd_config *config = nbd->config;
967 struct socket *sock, *old;
968 struct recv_thread_args *args;
969 int i;
970 int err;
971
972 sock = sockfd_lookup(arg, &err);
973 if (!sock)
974 return err;
975
976 args = kzalloc(sizeof(*args), GFP_KERNEL);
977 if (!args) {
978 sockfd_put(sock);
979 return -ENOMEM;
980 }
981
982 for (i = 0; i < config->num_connections; i++) {
983 struct nbd_sock *nsock = config->socks[i];
984
985 if (!nsock->dead)
986 continue;
987
988 mutex_lock(&nsock->tx_lock);
989 if (!nsock->dead) {
990 mutex_unlock(&nsock->tx_lock);
991 continue;
992 }
993 sk_set_memalloc(sock->sk);
a7ee8cf1
JB
994 if (nbd->tag_set.timeout)
995 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
b7aa3d39
JB
996 atomic_inc(&config->recv_threads);
997 refcount_inc(&nbd->config_refs);
998 old = nsock->sock;
999 nsock->fallback_index = -1;
1000 nsock->sock = sock;
1001 nsock->dead = false;
1002 INIT_WORK(&args->work, recv_work);
1003 args->index = i;
1004 args->nbd = nbd;
799f9a38 1005 nsock->cookie++;
b7aa3d39
JB
1006 mutex_unlock(&nsock->tx_lock);
1007 sockfd_put(old);
1008
7a362ea9
JB
1009 clear_bit(NBD_DISCONNECTED, &config->runtime_flags);
1010
b7aa3d39
JB
1011 /* We take the tx_mutex in an error path in the recv_work, so we
1012 * need to queue_work outside of the tx_mutex.
1013 */
1014 queue_work(recv_workqueue, &args->work);
560bc4b3
JB
1015
1016 atomic_inc(&config->live_connections);
1017 wake_up(&config->conn_wait);
b7aa3d39
JB
1018 return 0;
1019 }
1020 sockfd_put(sock);
1021 kfree(args);
1022 return -ENOSPC;
1023}
1024
0e4f0f6f
MP
1025static void nbd_bdev_reset(struct block_device *bdev)
1026{
abbbdf12
RMB
1027 if (bdev->bd_openers > 1)
1028 return;
29eaadc0 1029 bd_set_size(bdev, 0);
0e4f0f6f
MP
1030 if (max_part > 0) {
1031 blkdev_reread_part(bdev);
1032 bdev->bd_invalidated = 1;
1033 }
1034}
1035
29eaadc0 1036static void nbd_parse_flags(struct nbd_device *nbd)
d02cf531 1037{
5ea8d108
JB
1038 struct nbd_config *config = nbd->config;
1039 if (config->flags & NBD_FLAG_READ_ONLY)
29eaadc0
JB
1040 set_disk_ro(nbd->disk, true);
1041 else
1042 set_disk_ro(nbd->disk, false);
5ea8d108 1043 if (config->flags & NBD_FLAG_SEND_TRIM)
d02cf531 1044 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
685c9b24
SM
1045 if (config->flags & NBD_FLAG_SEND_FLUSH) {
1046 if (config->flags & NBD_FLAG_SEND_FUA)
1047 blk_queue_write_cache(nbd->disk->queue, true, true);
1048 else
1049 blk_queue_write_cache(nbd->disk->queue, true, false);
1050 }
d02cf531 1051 else
aafb1eec 1052 blk_queue_write_cache(nbd->disk->queue, false, false);
d02cf531
MP
1053}
1054
9561a7ad
JB
1055static void send_disconnects(struct nbd_device *nbd)
1056{
5ea8d108 1057 struct nbd_config *config = nbd->config;
c9f2b6ae
AV
1058 struct nbd_request request = {
1059 .magic = htonl(NBD_REQUEST_MAGIC),
1060 .type = htonl(NBD_CMD_DISC),
1061 };
1062 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1063 struct iov_iter from;
9561a7ad
JB
1064 int i, ret;
1065
5ea8d108 1066 for (i = 0; i < config->num_connections; i++) {
b4b2aecc
JB
1067 struct nbd_sock *nsock = config->socks[i];
1068
c9f2b6ae 1069 iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
b4b2aecc 1070 mutex_lock(&nsock->tx_lock);
9dd5d3ab 1071 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
9561a7ad
JB
1072 if (ret <= 0)
1073 dev_err(disk_to_dev(nbd->disk),
1074 "Send disconnect failed %d\n", ret);
b4b2aecc 1075 mutex_unlock(&nsock->tx_lock);
9561a7ad
JB
1076 }
1077}
1078
29eaadc0 1079static int nbd_disconnect(struct nbd_device *nbd)
9442b739 1080{
5ea8d108 1081 struct nbd_config *config = nbd->config;
30d53d9c 1082
5ea8d108 1083 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
2e13456f
JB
1084 set_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
1085 send_disconnects(nbd);
9442b739
JB
1086 return 0;
1087}
1088
29eaadc0 1089static void nbd_clear_sock(struct nbd_device *nbd)
1a2ad211 1090{
9442b739
JB
1091 sock_shutdown(nbd);
1092 nbd_clear_que(nbd);
5ea8d108 1093 nbd->task_setup = NULL;
5ea8d108
JB
1094}
1095
1096static void nbd_config_put(struct nbd_device *nbd)
1097{
1098 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1099 &nbd->config_lock)) {
5ea8d108 1100 struct nbd_config *config = nbd->config;
5ea8d108 1101 nbd_dev_dbg_close(nbd);
29eaadc0 1102 nbd_size_clear(nbd);
5ea8d108
JB
1103 if (test_and_clear_bit(NBD_HAS_PID_FILE,
1104 &config->runtime_flags))
1105 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1106 nbd->task_recv = NULL;
29eaadc0 1107 nbd_clear_sock(nbd);
5ea8d108
JB
1108 if (config->num_connections) {
1109 int i;
1110 for (i = 0; i < config->num_connections; i++) {
1111 sockfd_put(config->socks[i]->sock);
1112 kfree(config->socks[i]);
1113 }
1114 kfree(config->socks);
1115 }
fa976532 1116 kfree(nbd->config);
af622b86
ID
1117 nbd->config = NULL;
1118
1119 nbd->tag_set.timeout = 0;
1120 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
a2c97909 1121
5ea8d108 1122 mutex_unlock(&nbd->config_lock);
c6a4759e 1123 nbd_put(nbd);
5ea8d108
JB
1124 module_put(THIS_MODULE);
1125 }
9442b739
JB
1126}
1127
e46c7287 1128static int nbd_start_device(struct nbd_device *nbd)
9442b739 1129{
5ea8d108
JB
1130 struct nbd_config *config = nbd->config;
1131 int num_connections = config->num_connections;
9442b739 1132 int error = 0, i;
1a2ad211 1133
9442b739
JB
1134 if (nbd->task_recv)
1135 return -EBUSY;
5ea8d108 1136 if (!config->socks)
9442b739
JB
1137 return -EINVAL;
1138 if (num_connections > 1 &&
5ea8d108 1139 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
9442b739 1140 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
5ea8d108 1141 return -EINVAL;
9442b739 1142 }
23272a67 1143
5ea8d108 1144 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
9442b739 1145 nbd->task_recv = current;
23272a67 1146
29eaadc0 1147 nbd_parse_flags(nbd);
23272a67 1148
9442b739
JB
1149 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1150 if (error) {
1151 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
5ea8d108 1152 return error;
1a2ad211 1153 }
29eaadc0 1154 set_bit(NBD_HAS_PID_FILE, &config->runtime_flags);
37091fdd 1155
9442b739
JB
1156 nbd_dev_dbg_init(nbd);
1157 for (i = 0; i < num_connections; i++) {
5ea8d108
JB
1158 struct recv_thread_args *args;
1159
1160 args = kzalloc(sizeof(*args), GFP_KERNEL);
1161 if (!args) {
1162 sock_shutdown(nbd);
1163 return -ENOMEM;
1164 }
1165 sk_set_memalloc(config->socks[i]->sock->sk);
a7ee8cf1
JB
1166 if (nbd->tag_set.timeout)
1167 config->socks[i]->sock->sk->sk_sndtimeo =
1168 nbd->tag_set.timeout;
5ea8d108
JB
1169 atomic_inc(&config->recv_threads);
1170 refcount_inc(&nbd->config_refs);
1171 INIT_WORK(&args->work, recv_work);
1172 args->nbd = nbd;
1173 args->index = i;
1174 queue_work(recv_workqueue, &args->work);
37091fdd 1175 }
639812a1 1176 nbd_size_update(nbd);
e46c7287
JB
1177 return error;
1178}
1179
1180static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1181{
1182 struct nbd_config *config = nbd->config;
1183 int ret;
1184
1185 ret = nbd_start_device(nbd);
1186 if (ret)
1187 return ret;
1188
e46c7287
JB
1189 if (max_part)
1190 bdev->bd_invalidated = 1;
1191 mutex_unlock(&nbd->config_lock);
1192 ret = wait_event_interruptible(config->recv_wq,
5ea8d108 1193 atomic_read(&config->recv_threads) == 0);
e46c7287 1194 if (ret)
5ea8d108 1195 sock_shutdown(nbd);
9442b739 1196 mutex_lock(&nbd->config_lock);
e46c7287 1197 bd_set_size(bdev, 0);
9442b739 1198 /* user requested, ignore socket errors */
5ea8d108 1199 if (test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags))
e46c7287 1200 ret = 0;
5ea8d108 1201 if (test_bit(NBD_TIMEDOUT, &config->runtime_flags))
e46c7287
JB
1202 ret = -ETIMEDOUT;
1203 return ret;
9442b739
JB
1204}
1205
29eaadc0
JB
1206static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1207 struct block_device *bdev)
1208{
2516ab15 1209 sock_shutdown(nbd);
29eaadc0
JB
1210 kill_bdev(bdev);
1211 nbd_bdev_reset(bdev);
e46c7287
JB
1212 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1213 &nbd->config->runtime_flags))
1214 nbd_config_put(nbd);
29eaadc0
JB
1215}
1216
9442b739
JB
1217/* Must be called with config_lock held */
1218static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
1219 unsigned int cmd, unsigned long arg)
1220{
5ea8d108
JB
1221 struct nbd_config *config = nbd->config;
1222
9442b739
JB
1223 switch (cmd) {
1224 case NBD_DISCONNECT:
29eaadc0 1225 return nbd_disconnect(nbd);
9442b739 1226 case NBD_CLEAR_SOCK:
29eaadc0
JB
1227 nbd_clear_sock_ioctl(nbd, bdev);
1228 return 0;
9442b739 1229 case NBD_SET_SOCK:
e46c7287 1230 return nbd_add_socket(nbd, arg, false);
9442b739 1231 case NBD_SET_BLKSIZE:
23ecbbad
JA
1232 if (!arg || !is_power_of_2(arg) || arg < 512 ||
1233 arg > PAGE_SIZE)
1234 return -EINVAL;
29eaadc0 1235 nbd_size_set(nbd, arg,
5ea8d108 1236 div_s64(config->bytesize, arg));
e544541b 1237 return 0;
1da177e4 1238 case NBD_SET_SIZE:
29eaadc0 1239 nbd_size_set(nbd, config->blksize,
5ea8d108 1240 div_s64(arg, config->blksize));
e544541b 1241 return 0;
37091fdd 1242 case NBD_SET_SIZE_BLOCKS:
29eaadc0 1243 nbd_size_set(nbd, config->blksize, arg);
e544541b 1244 return 0;
7fdfd406 1245 case NBD_SET_TIMEOUT:
f8586855
JB
1246 if (arg) {
1247 nbd->tag_set.timeout = arg * HZ;
1248 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
1249 }
7fdfd406 1250 return 0;
1a2ad211 1251
2f012508 1252 case NBD_SET_FLAGS:
5ea8d108 1253 config->flags = arg;
2f012508 1254 return 0;
9442b739 1255 case NBD_DO_IT:
e46c7287 1256 return nbd_start_device_ioctl(nbd, bdev);
1da177e4 1257 case NBD_CLEAR_QUE:
4b2f0260
HX
1258 /*
1259 * This is for compatibility only. The queue is always cleared
1260 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1261 */
1da177e4
LT
1262 return 0;
1263 case NBD_PRINT_DEBUG:
fd8383fd
JB
1264 /*
1265 * For compatibility only, we no longer keep a list of
1266 * outstanding requests.
1267 */
1da177e4
LT
1268 return 0;
1269 }
1a2ad211
PM
1270 return -ENOTTY;
1271}
1272
1273static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1274 unsigned int cmd, unsigned long arg)
1275{
f4507164 1276 struct nbd_device *nbd = bdev->bd_disk->private_data;
e46c7287
JB
1277 struct nbd_config *config = nbd->config;
1278 int error = -EINVAL;
1a2ad211
PM
1279
1280 if (!capable(CAP_SYS_ADMIN))
1281 return -EPERM;
1282
1dae69be
JB
1283 /* The block layer will pass back some non-nbd ioctls in case we have
1284 * special handling for them, but we don't so just return an error.
1285 */
1286 if (_IOC_TYPE(cmd) != 0xab)
1287 return -EINVAL;
1288
9561a7ad 1289 mutex_lock(&nbd->config_lock);
e46c7287
JB
1290
1291 /* Don't allow ioctl operations on a nbd device that was created with
1292 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1293 */
1294 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1295 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1296 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1297 else
1298 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
9561a7ad 1299 mutex_unlock(&nbd->config_lock);
1a2ad211 1300 return error;
1da177e4
LT
1301}
1302
5ea8d108
JB
1303static struct nbd_config *nbd_alloc_config(void)
1304{
1305 struct nbd_config *config;
1306
1307 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1308 if (!config)
1309 return NULL;
1310 atomic_set(&config->recv_threads, 0);
1311 init_waitqueue_head(&config->recv_wq);
560bc4b3 1312 init_waitqueue_head(&config->conn_wait);
5ea8d108 1313 config->blksize = 1024;
560bc4b3 1314 atomic_set(&config->live_connections, 0);
5ea8d108
JB
1315 try_module_get(THIS_MODULE);
1316 return config;
1317}
1318
1319static int nbd_open(struct block_device *bdev, fmode_t mode)
1320{
1321 struct nbd_device *nbd;
1322 int ret = 0;
1323
1324 mutex_lock(&nbd_index_mutex);
1325 nbd = bdev->bd_disk->private_data;
1326 if (!nbd) {
1327 ret = -ENXIO;
1328 goto out;
1329 }
c6a4759e
JB
1330 if (!refcount_inc_not_zero(&nbd->refs)) {
1331 ret = -ENXIO;
1332 goto out;
1333 }
5ea8d108
JB
1334 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1335 struct nbd_config *config;
1336
1337 mutex_lock(&nbd->config_lock);
1338 if (refcount_inc_not_zero(&nbd->config_refs)) {
1339 mutex_unlock(&nbd->config_lock);
1340 goto out;
1341 }
1342 config = nbd->config = nbd_alloc_config();
1343 if (!config) {
1344 ret = -ENOMEM;
1345 mutex_unlock(&nbd->config_lock);
1346 goto out;
1347 }
1348 refcount_set(&nbd->config_refs, 1);
c6a4759e 1349 refcount_inc(&nbd->refs);
5ea8d108
JB
1350 mutex_unlock(&nbd->config_lock);
1351 }
1352out:
1353 mutex_unlock(&nbd_index_mutex);
1354 return ret;
1355}
1356
1357static void nbd_release(struct gendisk *disk, fmode_t mode)
1358{
1359 struct nbd_device *nbd = disk->private_data;
9b0b6258
DRK
1360 struct block_device *bdev = bdget_disk(disk, 0);
1361
1362 if (test_bit(NBD_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) &&
1363 bdev->bd_openers == 0)
1364 nbd_disconnect_and_put(nbd);
1365
5ea8d108 1366 nbd_config_put(nbd);
c6a4759e 1367 nbd_put(nbd);
5ea8d108
JB
1368}
1369
83d5cde4 1370static const struct block_device_operations nbd_fops =
1da177e4
LT
1371{
1372 .owner = THIS_MODULE,
5ea8d108
JB
1373 .open = nbd_open,
1374 .release = nbd_release,
8a6cfeb6 1375 .ioctl = nbd_ioctl,
263a3df1 1376 .compat_ioctl = nbd_ioctl,
1da177e4
LT
1377};
1378
30d53d9c
MP
1379#if IS_ENABLED(CONFIG_DEBUG_FS)
1380
1381static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1382{
1383 struct nbd_device *nbd = s->private;
1384
1385 if (nbd->task_recv)
1386 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
30d53d9c
MP
1387
1388 return 0;
1389}
1390
1391static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
1392{
1393 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
1394}
1395
1396static const struct file_operations nbd_dbg_tasks_ops = {
1397 .open = nbd_dbg_tasks_open,
1398 .read = seq_read,
1399 .llseek = seq_lseek,
1400 .release = single_release,
1401};
1402
1403static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1404{
1405 struct nbd_device *nbd = s->private;
5ea8d108 1406 u32 flags = nbd->config->flags;
30d53d9c
MP
1407
1408 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1409
1410 seq_puts(s, "Known flags:\n");
1411
1412 if (flags & NBD_FLAG_HAS_FLAGS)
1413 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1414 if (flags & NBD_FLAG_READ_ONLY)
1415 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1416 if (flags & NBD_FLAG_SEND_FLUSH)
1417 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
685c9b24
SM
1418 if (flags & NBD_FLAG_SEND_FUA)
1419 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
30d53d9c
MP
1420 if (flags & NBD_FLAG_SEND_TRIM)
1421 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1422
1423 return 0;
1424}
1425
1426static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
1427{
1428 return single_open(file, nbd_dbg_flags_show, inode->i_private);
1429}
1430
1431static const struct file_operations nbd_dbg_flags_ops = {
1432 .open = nbd_dbg_flags_open,
1433 .read = seq_read,
1434 .llseek = seq_lseek,
1435 .release = single_release,
1436};
1437
1438static int nbd_dev_dbg_init(struct nbd_device *nbd)
1439{
1440 struct dentry *dir;
5ea8d108 1441 struct nbd_config *config = nbd->config;
27ea43fe
MP
1442
1443 if (!nbd_dbg_dir)
1444 return -EIO;
30d53d9c
MP
1445
1446 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
27ea43fe
MP
1447 if (!dir) {
1448 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1449 nbd_name(nbd));
1450 return -EIO;
30d53d9c 1451 }
5ea8d108 1452 config->dbg_dir = dir;
30d53d9c 1453
27ea43fe 1454 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
5ea8d108 1455 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
0eadf37a 1456 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
5ea8d108 1457 debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
d366a0ff 1458 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
30d53d9c
MP
1459
1460 return 0;
1461}
1462
1463static void nbd_dev_dbg_close(struct nbd_device *nbd)
1464{
5ea8d108 1465 debugfs_remove_recursive(nbd->config->dbg_dir);
30d53d9c
MP
1466}
1467
1468static int nbd_dbg_init(void)
1469{
1470 struct dentry *dbg_dir;
1471
1472 dbg_dir = debugfs_create_dir("nbd", NULL);
27ea43fe
MP
1473 if (!dbg_dir)
1474 return -EIO;
30d53d9c
MP
1475
1476 nbd_dbg_dir = dbg_dir;
1477
1478 return 0;
1479}
1480
1481static void nbd_dbg_close(void)
1482{
1483 debugfs_remove_recursive(nbd_dbg_dir);
1484}
1485
1486#else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1487
1488static int nbd_dev_dbg_init(struct nbd_device *nbd)
1489{
1490 return 0;
1491}
1492
1493static void nbd_dev_dbg_close(struct nbd_device *nbd)
1494{
1495}
1496
1497static int nbd_dbg_init(void)
1498{
1499 return 0;
1500}
1501
1502static void nbd_dbg_close(void)
1503{
1504}
1505
1506#endif
1507
d6296d39
CH
1508static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1509 unsigned int hctx_idx, unsigned int numa_node)
fd8383fd
JB
1510{
1511 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
d6296d39 1512 cmd->nbd = set->driver_data;
ced413c5 1513 cmd->flags = 0;
05ee6166 1514 mutex_init(&cmd->lock);
fd8383fd
JB
1515 return 0;
1516}
1517
f363b089 1518static const struct blk_mq_ops nbd_mq_ops = {
fd8383fd 1519 .queue_rq = nbd_queue_rq,
1e388ae0 1520 .complete = nbd_complete_rq,
fd8383fd 1521 .init_request = nbd_init_request,
0eadf37a 1522 .timeout = nbd_xmit_timeout,
fd8383fd
JB
1523};
1524
b0d9111a
JB
1525static int nbd_dev_add(int index)
1526{
1527 struct nbd_device *nbd;
1528 struct gendisk *disk;
1529 struct request_queue *q;
1530 int err = -ENOMEM;
1531
1532 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1533 if (!nbd)
1534 goto out;
1535
1536 disk = alloc_disk(1 << part_shift);
1537 if (!disk)
1538 goto out_free_nbd;
1539
1540 if (index >= 0) {
1541 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1542 GFP_KERNEL);
1543 if (err == -ENOSPC)
1544 err = -EEXIST;
1545 } else {
1546 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1547 if (err >= 0)
1548 index = err;
1549 }
1550 if (err < 0)
1551 goto out_free_disk;
1552
e46c7287 1553 nbd->index = index;
b0d9111a
JB
1554 nbd->disk = disk;
1555 nbd->tag_set.ops = &nbd_mq_ops;
1556 nbd->tag_set.nr_hw_queues = 1;
1557 nbd->tag_set.queue_depth = 128;
1558 nbd->tag_set.numa_node = NUMA_NO_NODE;
1559 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1560 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1561 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
1562 nbd->tag_set.driver_data = nbd;
1563
1564 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1565 if (err)
1566 goto out_free_idr;
1567
1568 q = blk_mq_init_queue(&nbd->tag_set);
1569 if (IS_ERR(q)) {
1570 err = PTR_ERR(q);
1571 goto out_free_tags;
1572 }
1573 disk->queue = q;
1574
1575 /*
1576 * Tell the block layer that we are not a rotational device
1577 */
1578 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
1579 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1580 disk->queue->limits.discard_granularity = 512;
1581 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
ebb16d0d 1582 blk_queue_max_segment_size(disk->queue, UINT_MAX);
1cc1f17a 1583 blk_queue_max_segments(disk->queue, USHRT_MAX);
b0d9111a
JB
1584 blk_queue_max_hw_sectors(disk->queue, 65536);
1585 disk->queue->limits.max_sectors = 256;
1586
b0d9111a 1587 mutex_init(&nbd->config_lock);
5ea8d108 1588 refcount_set(&nbd->config_refs, 0);
c6a4759e
JB
1589 refcount_set(&nbd->refs, 1);
1590 INIT_LIST_HEAD(&nbd->list);
b0d9111a
JB
1591 disk->major = NBD_MAJOR;
1592 disk->first_minor = index << part_shift;
1593 disk->fops = &nbd_fops;
1594 disk->private_data = nbd;
1595 sprintf(disk->disk_name, "nbd%d", index);
b0d9111a 1596 add_disk(disk);
47d902b9 1597 nbd_total_devices++;
b0d9111a
JB
1598 return index;
1599
1600out_free_tags:
1601 blk_mq_free_tag_set(&nbd->tag_set);
1602out_free_idr:
1603 idr_remove(&nbd_index_idr, index);
1604out_free_disk:
1605 put_disk(disk);
1606out_free_nbd:
1607 kfree(nbd);
1608out:
1609 return err;
1610}
1611
e46c7287
JB
1612static int find_free_cb(int id, void *ptr, void *data)
1613{
1614 struct nbd_device *nbd = ptr;
1615 struct nbd_device **found = data;
1616
1617 if (!refcount_read(&nbd->config_refs)) {
1618 *found = nbd;
1619 return 1;
1620 }
1621 return 0;
1622}
1623
1624/* Netlink interface. */
1625static struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
1626 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1627 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1628 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1629 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1630 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1631 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1632 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
560bc4b3 1633 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
47d902b9 1634 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
e46c7287
JB
1635};
1636
1637static struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
1638 [NBD_SOCK_FD] = { .type = NLA_U32 },
1639};
1640
47d902b9
JB
1641/* We don't use this right now since we don't parse the incoming list, but we
1642 * still want it here so userspace knows what to expect.
1643 */
1644static struct nla_policy __attribute__((unused))
1645nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1646 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1647 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1648};
1649
e46c7287
JB
1650static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1651{
1652 struct nbd_device *nbd = NULL;
1653 struct nbd_config *config;
1654 int index = -1;
1655 int ret;
a2c97909 1656 bool put_dev = false;
e46c7287
JB
1657
1658 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1659 return -EPERM;
1660
1661 if (info->attrs[NBD_ATTR_INDEX])
1662 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1663 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1664 printk(KERN_ERR "nbd: must specify at least one socket\n");
1665 return -EINVAL;
1666 }
1667 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1668 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1669 return -EINVAL;
1670 }
1671again:
1672 mutex_lock(&nbd_index_mutex);
1673 if (index == -1) {
1674 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1675 if (ret == 0) {
1676 int new_index;
1677 new_index = nbd_dev_add(-1);
1678 if (new_index < 0) {
1679 mutex_unlock(&nbd_index_mutex);
1680 printk(KERN_ERR "nbd: failed to add new device\n");
615bf75c 1681 return new_index;
e46c7287
JB
1682 }
1683 nbd = idr_find(&nbd_index_idr, new_index);
1684 }
1685 } else {
1686 nbd = idr_find(&nbd_index_idr, index);
e6a76272
JB
1687 if (!nbd) {
1688 ret = nbd_dev_add(index);
1689 if (ret < 0) {
1690 mutex_unlock(&nbd_index_mutex);
1691 printk(KERN_ERR "nbd: failed to add new device\n");
1692 return ret;
1693 }
1694 nbd = idr_find(&nbd_index_idr, index);
1695 }
e46c7287 1696 }
e46c7287
JB
1697 if (!nbd) {
1698 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1699 index);
c6a4759e
JB
1700 mutex_unlock(&nbd_index_mutex);
1701 return -EINVAL;
1702 }
1703 if (!refcount_inc_not_zero(&nbd->refs)) {
1704 mutex_unlock(&nbd_index_mutex);
1705 if (index == -1)
1706 goto again;
1707 printk(KERN_ERR "nbd: device at index %d is going down\n",
1708 index);
e46c7287
JB
1709 return -EINVAL;
1710 }
c6a4759e 1711 mutex_unlock(&nbd_index_mutex);
e46c7287
JB
1712
1713 mutex_lock(&nbd->config_lock);
1714 if (refcount_read(&nbd->config_refs)) {
1715 mutex_unlock(&nbd->config_lock);
c6a4759e 1716 nbd_put(nbd);
e46c7287
JB
1717 if (index == -1)
1718 goto again;
1719 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1720 return -EBUSY;
1721 }
1722 if (WARN_ON(nbd->config)) {
1723 mutex_unlock(&nbd->config_lock);
c6a4759e 1724 nbd_put(nbd);
e46c7287
JB
1725 return -EINVAL;
1726 }
1727 config = nbd->config = nbd_alloc_config();
1728 if (!nbd->config) {
1729 mutex_unlock(&nbd->config_lock);
c6a4759e 1730 nbd_put(nbd);
e46c7287
JB
1731 printk(KERN_ERR "nbd: couldn't allocate config\n");
1732 return -ENOMEM;
1733 }
1734 refcount_set(&nbd->config_refs, 1);
1735 set_bit(NBD_BOUND, &config->runtime_flags);
1736
1737 if (info->attrs[NBD_ATTR_SIZE_BYTES]) {
1738 u64 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1739 nbd_size_set(nbd, config->blksize,
1740 div64_u64(bytes, config->blksize));
1741 }
1742 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1743 u64 bsize =
1744 nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1745 nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
1746 }
1747 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1748 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1749 nbd->tag_set.timeout = timeout * HZ;
1750 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1751 }
560bc4b3
JB
1752 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1753 config->dead_conn_timeout =
1754 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1755 config->dead_conn_timeout *= HZ;
1756 }
e46c7287
JB
1757 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1758 config->flags =
1759 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
a2c97909
JB
1760 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1761 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1762 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1763 set_bit(NBD_DESTROY_ON_DISCONNECT,
1764 &config->runtime_flags);
1765 put_dev = true;
1766 }
9b0b6258
DRK
1767 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
1768 set_bit(NBD_DISCONNECT_ON_CLOSE,
1769 &config->runtime_flags);
1770 }
a2c97909
JB
1771 }
1772
e46c7287
JB
1773 if (info->attrs[NBD_ATTR_SOCKETS]) {
1774 struct nlattr *attr;
1775 int rem, fd;
1776
1777 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1778 rem) {
1779 struct nlattr *socks[NBD_SOCK_MAX+1];
1780
1781 if (nla_type(attr) != NBD_SOCK_ITEM) {
1782 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1783 ret = -EINVAL;
1784 goto out;
1785 }
1786 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
8d65b08d 1787 nbd_sock_policy, info->extack);
e46c7287
JB
1788 if (ret != 0) {
1789 printk(KERN_ERR "nbd: error processing sock list\n");
1790 ret = -EINVAL;
1791 goto out;
1792 }
1793 if (!socks[NBD_SOCK_FD])
1794 continue;
1795 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1796 ret = nbd_add_socket(nbd, fd, true);
1797 if (ret)
1798 goto out;
1799 }
1800 }
1801 ret = nbd_start_device(nbd);
1802out:
1803 mutex_unlock(&nbd->config_lock);
1804 if (!ret) {
1805 set_bit(NBD_HAS_CONFIG_REF, &config->runtime_flags);
1806 refcount_inc(&nbd->config_refs);
1807 nbd_connect_reply(info, nbd->index);
1808 }
1809 nbd_config_put(nbd);
a2c97909
JB
1810 if (put_dev)
1811 nbd_put(nbd);
e46c7287
JB
1812 return ret;
1813}
1814
9b0b6258
DRK
1815static void nbd_disconnect_and_put(struct nbd_device *nbd)
1816{
1817 mutex_lock(&nbd->config_lock);
1818 nbd_disconnect(nbd);
1819 mutex_unlock(&nbd->config_lock);
1820 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1821 &nbd->config->runtime_flags))
1822 nbd_config_put(nbd);
1823}
1824
e46c7287
JB
1825static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
1826{
1827 struct nbd_device *nbd;
1828 int index;
1829
1830 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1831 return -EPERM;
1832
1833 if (!info->attrs[NBD_ATTR_INDEX]) {
1834 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
1835 return -EINVAL;
1836 }
1837 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1838 mutex_lock(&nbd_index_mutex);
1839 nbd = idr_find(&nbd_index_idr, index);
e46c7287 1840 if (!nbd) {
c6a4759e 1841 mutex_unlock(&nbd_index_mutex);
e46c7287
JB
1842 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1843 index);
1844 return -EINVAL;
1845 }
c6a4759e
JB
1846 if (!refcount_inc_not_zero(&nbd->refs)) {
1847 mutex_unlock(&nbd_index_mutex);
1848 printk(KERN_ERR "nbd: device at index %d is going down\n",
1849 index);
1850 return -EINVAL;
1851 }
1852 mutex_unlock(&nbd_index_mutex);
1853 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1854 nbd_put(nbd);
e46c7287 1855 return 0;
c6a4759e 1856 }
9b0b6258 1857 nbd_disconnect_and_put(nbd);
e46c7287 1858 nbd_config_put(nbd);
c6a4759e 1859 nbd_put(nbd);
e46c7287
JB
1860 return 0;
1861}
1862
b7aa3d39
JB
1863static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
1864{
1865 struct nbd_device *nbd = NULL;
1866 struct nbd_config *config;
1867 int index;
9b0b6258 1868 int ret = 0;
a2c97909 1869 bool put_dev = false;
b7aa3d39
JB
1870
1871 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1872 return -EPERM;
1873
1874 if (!info->attrs[NBD_ATTR_INDEX]) {
1875 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
1876 return -EINVAL;
1877 }
1878 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1879 mutex_lock(&nbd_index_mutex);
1880 nbd = idr_find(&nbd_index_idr, index);
b7aa3d39 1881 if (!nbd) {
c6a4759e 1882 mutex_unlock(&nbd_index_mutex);
b7aa3d39
JB
1883 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
1884 index);
1885 return -EINVAL;
1886 }
c6a4759e
JB
1887 if (!refcount_inc_not_zero(&nbd->refs)) {
1888 mutex_unlock(&nbd_index_mutex);
1889 printk(KERN_ERR "nbd: device at index %d is going down\n",
1890 index);
1891 return -EINVAL;
1892 }
1893 mutex_unlock(&nbd_index_mutex);
b7aa3d39
JB
1894
1895 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1896 dev_err(nbd_to_dev(nbd),
1897 "not configured, cannot reconfigure\n");
c6a4759e 1898 nbd_put(nbd);
b7aa3d39
JB
1899 return -EINVAL;
1900 }
1901
1902 mutex_lock(&nbd->config_lock);
1903 config = nbd->config;
1904 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1905 !nbd->task_recv) {
1906 dev_err(nbd_to_dev(nbd),
1907 "not configured, cannot reconfigure\n");
9b0b6258 1908 ret = -EINVAL;
b7aa3d39
JB
1909 goto out;
1910 }
1911
1912 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1913 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1914 nbd->tag_set.timeout = timeout * HZ;
1915 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1916 }
560bc4b3
JB
1917 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1918 config->dead_conn_timeout =
1919 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1920 config->dead_conn_timeout *= HZ;
1921 }
a2c97909
JB
1922 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1923 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1924 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1925 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
1926 &config->runtime_flags))
1927 put_dev = true;
1928 } else {
1929 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
1930 &config->runtime_flags))
1931 refcount_inc(&nbd->refs);
1932 }
9b0b6258
DRK
1933
1934 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
1935 set_bit(NBD_DISCONNECT_ON_CLOSE,
1936 &config->runtime_flags);
1937 } else {
1938 clear_bit(NBD_DISCONNECT_ON_CLOSE,
1939 &config->runtime_flags);
1940 }
a2c97909 1941 }
b7aa3d39
JB
1942
1943 if (info->attrs[NBD_ATTR_SOCKETS]) {
1944 struct nlattr *attr;
1945 int rem, fd;
1946
1947 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1948 rem) {
1949 struct nlattr *socks[NBD_SOCK_MAX+1];
1950
1951 if (nla_type(attr) != NBD_SOCK_ITEM) {
1952 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1953 ret = -EINVAL;
1954 goto out;
1955 }
1956 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
8d65b08d 1957 nbd_sock_policy, info->extack);
b7aa3d39
JB
1958 if (ret != 0) {
1959 printk(KERN_ERR "nbd: error processing sock list\n");
1960 ret = -EINVAL;
1961 goto out;
1962 }
1963 if (!socks[NBD_SOCK_FD])
1964 continue;
1965 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1966 ret = nbd_reconnect_socket(nbd, fd);
1967 if (ret) {
1968 if (ret == -ENOSPC)
1969 ret = 0;
1970 goto out;
1971 }
1972 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
1973 }
1974 }
1975out:
1976 mutex_unlock(&nbd->config_lock);
1977 nbd_config_put(nbd);
c6a4759e 1978 nbd_put(nbd);
a2c97909
JB
1979 if (put_dev)
1980 nbd_put(nbd);
b7aa3d39
JB
1981 return ret;
1982}
1983
e46c7287
JB
1984static const struct genl_ops nbd_connect_genl_ops[] = {
1985 {
1986 .cmd = NBD_CMD_CONNECT,
1987 .policy = nbd_attr_policy,
1988 .doit = nbd_genl_connect,
1989 },
1990 {
1991 .cmd = NBD_CMD_DISCONNECT,
1992 .policy = nbd_attr_policy,
1993 .doit = nbd_genl_disconnect,
1994 },
b7aa3d39
JB
1995 {
1996 .cmd = NBD_CMD_RECONFIGURE,
1997 .policy = nbd_attr_policy,
1998 .doit = nbd_genl_reconfigure,
1999 },
47d902b9
JB
2000 {
2001 .cmd = NBD_CMD_STATUS,
2002 .policy = nbd_attr_policy,
2003 .doit = nbd_genl_status,
2004 },
e46c7287
JB
2005};
2006
799f9a38
JB
2007static const struct genl_multicast_group nbd_mcast_grps[] = {
2008 { .name = NBD_GENL_MCAST_GROUP_NAME, },
2009};
2010
e46c7287
JB
2011static struct genl_family nbd_genl_family __ro_after_init = {
2012 .hdrsize = 0,
2013 .name = NBD_GENL_FAMILY_NAME,
2014 .version = NBD_GENL_VERSION,
2015 .module = THIS_MODULE,
2016 .ops = nbd_connect_genl_ops,
2017 .n_ops = ARRAY_SIZE(nbd_connect_genl_ops),
2018 .maxattr = NBD_ATTR_MAX,
799f9a38
JB
2019 .mcgrps = nbd_mcast_grps,
2020 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
e46c7287
JB
2021};
2022
47d902b9
JB
2023static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
2024{
2025 struct nlattr *dev_opt;
2026 u8 connected = 0;
2027 int ret;
2028
2029 /* This is a little racey, but for status it's ok. The
2030 * reason we don't take a ref here is because we can't
2031 * take a ref in the index == -1 case as we would need
2032 * to put under the nbd_index_mutex, which could
2033 * deadlock if we are configured to remove ourselves
2034 * once we're disconnected.
2035 */
2036 if (refcount_read(&nbd->config_refs))
2037 connected = 1;
2038 dev_opt = nla_nest_start(reply, NBD_DEVICE_ITEM);
2039 if (!dev_opt)
2040 return -EMSGSIZE;
2041 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
2042 if (ret)
2043 return -EMSGSIZE;
2044 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
2045 connected);
2046 if (ret)
2047 return -EMSGSIZE;
2048 nla_nest_end(reply, dev_opt);
2049 return 0;
2050}
2051
2052static int status_cb(int id, void *ptr, void *data)
2053{
2054 struct nbd_device *nbd = ptr;
2055 return populate_nbd_status(nbd, (struct sk_buff *)data);
2056}
2057
2058static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
2059{
2060 struct nlattr *dev_list;
2061 struct sk_buff *reply;
2062 void *reply_head;
2063 size_t msg_size;
2064 int index = -1;
2065 int ret = -ENOMEM;
2066
2067 if (info->attrs[NBD_ATTR_INDEX])
2068 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2069
2070 mutex_lock(&nbd_index_mutex);
2071
2072 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
2073 nla_attr_size(sizeof(u8)));
2074 msg_size *= (index == -1) ? nbd_total_devices : 1;
2075
2076 reply = genlmsg_new(msg_size, GFP_KERNEL);
2077 if (!reply)
2078 goto out;
2079 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2080 NBD_CMD_STATUS);
2081 if (!reply_head) {
2082 nlmsg_free(reply);
2083 goto out;
2084 }
2085
2086 dev_list = nla_nest_start(reply, NBD_ATTR_DEVICE_LIST);
2087 if (index == -1) {
2088 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2089 if (ret) {
2090 nlmsg_free(reply);
2091 goto out;
2092 }
2093 } else {
2094 struct nbd_device *nbd;
2095 nbd = idr_find(&nbd_index_idr, index);
2096 if (nbd) {
2097 ret = populate_nbd_status(nbd, reply);
2098 if (ret) {
2099 nlmsg_free(reply);
2100 goto out;
2101 }
2102 }
2103 }
2104 nla_nest_end(reply, dev_list);
2105 genlmsg_end(reply, reply_head);
2106 genlmsg_reply(reply, info);
2107 ret = 0;
2108out:
2109 mutex_unlock(&nbd_index_mutex);
2110 return ret;
2111}
2112
e46c7287
JB
2113static void nbd_connect_reply(struct genl_info *info, int index)
2114{
2115 struct sk_buff *skb;
2116 void *msg_head;
2117 int ret;
2118
2119 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2120 if (!skb)
2121 return;
2122 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2123 NBD_CMD_CONNECT);
2124 if (!msg_head) {
2125 nlmsg_free(skb);
2126 return;
2127 }
2128 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2129 if (ret) {
2130 nlmsg_free(skb);
2131 return;
2132 }
2133 genlmsg_end(skb, msg_head);
2134 genlmsg_reply(skb, info);
2135}
1da177e4 2136
799f9a38
JB
2137static void nbd_mcast_index(int index)
2138{
2139 struct sk_buff *skb;
2140 void *msg_head;
2141 int ret;
2142
2143 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2144 if (!skb)
2145 return;
2146 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2147 NBD_CMD_LINK_DEAD);
2148 if (!msg_head) {
2149 nlmsg_free(skb);
2150 return;
2151 }
2152 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2153 if (ret) {
2154 nlmsg_free(skb);
2155 return;
2156 }
2157 genlmsg_end(skb, msg_head);
2158 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2159}
2160
2161static void nbd_dead_link_work(struct work_struct *work)
2162{
2163 struct link_dead_args *args = container_of(work, struct link_dead_args,
2164 work);
2165 nbd_mcast_index(args->index);
2166 kfree(args);
2167}
2168
1da177e4
LT
2169static int __init nbd_init(void)
2170{
1da177e4
LT
2171 int i;
2172
5b7b18cc 2173 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
1da177e4 2174
d71a6d73 2175 if (max_part < 0) {
7742ce4a 2176 printk(KERN_ERR "nbd: max_part must be >= 0\n");
d71a6d73
LV
2177 return -EINVAL;
2178 }
2179
2180 part_shift = 0;
5988ce23 2181 if (max_part > 0) {
d71a6d73
LV
2182 part_shift = fls(max_part);
2183
5988ce23
NK
2184 /*
2185 * Adjust max_part according to part_shift as it is exported
2186 * to user space so that user can know the max number of
2187 * partition kernel should be able to manage.
2188 *
2189 * Note that -1 is required because partition 0 is reserved
2190 * for the whole disk.
2191 */
2192 max_part = (1UL << part_shift) - 1;
2193 }
2194
3b271082
NK
2195 if ((1UL << part_shift) > DISK_MAX_PARTS)
2196 return -EINVAL;
2197
2198 if (nbds_max > 1UL << (MINORBITS - part_shift))
2199 return -EINVAL;
124d6db0
JB
2200 recv_workqueue = alloc_workqueue("knbd-recv",
2201 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2202 if (!recv_workqueue)
2203 return -ENOMEM;
3b271082 2204
6330a2d0
JB
2205 if (register_blkdev(NBD_MAJOR, "nbd")) {
2206 destroy_workqueue(recv_workqueue);
b0d9111a 2207 return -EIO;
6330a2d0 2208 }
1da177e4 2209
e46c7287
JB
2210 if (genl_register_family(&nbd_genl_family)) {
2211 unregister_blkdev(NBD_MAJOR, "nbd");
2212 destroy_workqueue(recv_workqueue);
2213 return -EINVAL;
2214 }
30d53d9c
MP
2215 nbd_dbg_init();
2216
b0d9111a
JB
2217 mutex_lock(&nbd_index_mutex);
2218 for (i = 0; i < nbds_max; i++)
2219 nbd_dev_add(i);
2220 mutex_unlock(&nbd_index_mutex);
2221 return 0;
2222}
1da177e4 2223
b0d9111a
JB
2224static int nbd_exit_cb(int id, void *ptr, void *data)
2225{
c6a4759e 2226 struct list_head *list = (struct list_head *)data;
b0d9111a 2227 struct nbd_device *nbd = ptr;
c6a4759e 2228
c6a4759e 2229 list_add_tail(&nbd->list, list);
1da177e4 2230 return 0;
1da177e4
LT
2231}
2232
2233static void __exit nbd_cleanup(void)
2234{
c6a4759e
JB
2235 struct nbd_device *nbd;
2236 LIST_HEAD(del_list);
2237
30d53d9c
MP
2238 nbd_dbg_close();
2239
c6a4759e
JB
2240 mutex_lock(&nbd_index_mutex);
2241 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2242 mutex_unlock(&nbd_index_mutex);
2243
60ae36ad
JB
2244 while (!list_empty(&del_list)) {
2245 nbd = list_first_entry(&del_list, struct nbd_device, list);
2246 list_del_init(&nbd->list);
2247 if (refcount_read(&nbd->refs) != 1)
c6a4759e
JB
2248 printk(KERN_ERR "nbd: possibly leaking a device\n");
2249 nbd_put(nbd);
c6a4759e
JB
2250 }
2251
b0d9111a 2252 idr_destroy(&nbd_index_idr);
e46c7287 2253 genl_unregister_family(&nbd_genl_family);
124d6db0 2254 destroy_workqueue(recv_workqueue);
1da177e4 2255 unregister_blkdev(NBD_MAJOR, "nbd");
1da177e4
LT
2256}
2257
2258module_init(nbd_init);
2259module_exit(nbd_cleanup);
2260
2261MODULE_DESCRIPTION("Network Block Device");
2262MODULE_LICENSE("GPL");
2263
40be0c28 2264module_param(nbds_max, int, 0444);
d71a6d73
LV
2265MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2266module_param(max_part, int, 0444);
7a8362a0 2267MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");