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