1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Network block device - make block devices work over TCP
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.
8 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
9 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
11 * (part of code stolen from loop.c)
14 #include <linux/major.h>
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>
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/completion.h>
30 #include <linux/err.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
34 #include <linux/net.h>
35 #include <linux/kthread.h>
36 #include <linux/types.h>
37 #include <linux/debugfs.h>
38 #include <linux/blk-mq.h>
40 #include <linux/uaccess.h>
41 #include <asm/types.h>
43 #include <linux/nbd.h>
44 #include <linux/nbd-netlink.h>
45 #include <net/genetlink.h>
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/nbd.h>
50 static DEFINE_IDR(nbd_index_idr);
51 static DEFINE_MUTEX(nbd_index_mutex);
52 static struct workqueue_struct *nbd_del_wq;
53 static int nbd_total_devices = 0;
58 struct request *pending;
65 struct recv_thread_args {
66 struct work_struct work;
67 struct nbd_device *nbd;
71 struct link_dead_args {
72 struct work_struct work;
76 #define NBD_RT_TIMEDOUT 0
77 #define NBD_RT_DISCONNECT_REQUESTED 1
78 #define NBD_RT_DISCONNECTED 2
79 #define NBD_RT_HAS_PID_FILE 3
80 #define NBD_RT_HAS_CONFIG_REF 4
81 #define NBD_RT_BOUND 5
82 #define NBD_RT_DISCONNECT_ON_CLOSE 6
83 #define NBD_RT_HAS_BACKEND_FILE 7
85 #define NBD_DESTROY_ON_DISCONNECT 0
86 #define NBD_DISCONNECT_REQUESTED 1
90 unsigned long runtime_flags;
91 u64 dead_conn_timeout;
93 struct nbd_sock **socks;
95 atomic_t live_connections;
96 wait_queue_head_t conn_wait;
98 atomic_t recv_threads;
99 wait_queue_head_t recv_wq;
100 unsigned int blksize_bits;
102 #if IS_ENABLED(CONFIG_DEBUG_FS)
103 struct dentry *dbg_dir;
107 static inline unsigned int nbd_blksize(struct nbd_config *config)
109 return 1u << config->blksize_bits;
113 struct blk_mq_tag_set tag_set;
116 refcount_t config_refs;
118 struct nbd_config *config;
119 struct mutex config_lock;
120 struct gendisk *disk;
121 struct workqueue_struct *recv_workq;
122 struct work_struct remove_work;
124 struct list_head list;
125 struct task_struct *task_recv;
126 struct task_struct *task_setup;
133 #define NBD_CMD_REQUEUED 1
135 * This flag will be set if nbd_queue_rq() succeed, and will be checked and
136 * cleared in completion. Both setting and clearing of the flag are protected
139 #define NBD_CMD_INFLIGHT 2
142 struct nbd_device *nbd;
152 #if IS_ENABLED(CONFIG_DEBUG_FS)
153 static struct dentry *nbd_dbg_dir;
156 #define nbd_name(nbd) ((nbd)->disk->disk_name)
158 #define NBD_MAGIC 0x68797548
160 #define NBD_DEF_BLKSIZE_BITS 10
162 static unsigned int nbds_max = 16;
163 static int max_part = 16;
164 static int part_shift;
166 static int nbd_dev_dbg_init(struct nbd_device *nbd);
167 static void nbd_dev_dbg_close(struct nbd_device *nbd);
168 static void nbd_config_put(struct nbd_device *nbd);
169 static void nbd_connect_reply(struct genl_info *info, int index);
170 static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info);
171 static void nbd_dead_link_work(struct work_struct *work);
172 static void nbd_disconnect_and_put(struct nbd_device *nbd);
174 static inline struct device *nbd_to_dev(struct nbd_device *nbd)
176 return disk_to_dev(nbd->disk);
179 static void nbd_requeue_cmd(struct nbd_cmd *cmd)
181 struct request *req = blk_mq_rq_from_pdu(cmd);
183 if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
184 blk_mq_requeue_request(req, true);
187 #define NBD_COOKIE_BITS 32
189 static u64 nbd_cmd_handle(struct nbd_cmd *cmd)
191 struct request *req = blk_mq_rq_from_pdu(cmd);
192 u32 tag = blk_mq_unique_tag(req);
193 u64 cookie = cmd->cmd_cookie;
195 return (cookie << NBD_COOKIE_BITS) | tag;
198 static u32 nbd_handle_to_tag(u64 handle)
203 static u32 nbd_handle_to_cookie(u64 handle)
205 return (u32)(handle >> NBD_COOKIE_BITS);
208 static const char *nbdcmd_to_ascii(int cmd)
211 case NBD_CMD_READ: return "read";
212 case NBD_CMD_WRITE: return "write";
213 case NBD_CMD_DISC: return "disconnect";
214 case NBD_CMD_FLUSH: return "flush";
215 case NBD_CMD_TRIM: return "trim/discard";
220 static ssize_t pid_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
223 struct gendisk *disk = dev_to_disk(dev);
224 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
226 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
229 static const struct device_attribute pid_attr = {
230 .attr = { .name = "pid", .mode = 0444},
234 static ssize_t backend_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct gendisk *disk = dev_to_disk(dev);
238 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
240 return sprintf(buf, "%s\n", nbd->backend ?: "");
243 static const struct device_attribute backend_attr = {
244 .attr = { .name = "backend", .mode = 0444},
245 .show = backend_show,
248 static void nbd_dev_remove(struct nbd_device *nbd)
250 struct gendisk *disk = nbd->disk;
253 blk_cleanup_disk(disk);
254 blk_mq_free_tag_set(&nbd->tag_set);
257 * Remove from idr after del_gendisk() completes, so if the same ID is
258 * reused, the following add_disk() will succeed.
260 mutex_lock(&nbd_index_mutex);
261 idr_remove(&nbd_index_idr, nbd->index);
262 mutex_unlock(&nbd_index_mutex);
267 static void nbd_dev_remove_work(struct work_struct *work)
269 nbd_dev_remove(container_of(work, struct nbd_device, remove_work));
272 static void nbd_put(struct nbd_device *nbd)
274 if (!refcount_dec_and_test(&nbd->refs))
277 /* Call del_gendisk() asynchrounously to prevent deadlock */
278 if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags))
279 queue_work(nbd_del_wq, &nbd->remove_work);
284 static int nbd_disconnected(struct nbd_config *config)
286 return test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags) ||
287 test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
290 static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
293 if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
294 struct link_dead_args *args;
295 args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO);
297 INIT_WORK(&args->work, nbd_dead_link_work);
298 args->index = nbd->index;
299 queue_work(system_wq, &args->work);
303 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
304 if (atomic_dec_return(&nbd->config->live_connections) == 0) {
305 if (test_and_clear_bit(NBD_RT_DISCONNECT_REQUESTED,
306 &nbd->config->runtime_flags)) {
307 set_bit(NBD_RT_DISCONNECTED,
308 &nbd->config->runtime_flags);
309 dev_info(nbd_to_dev(nbd),
310 "Disconnected due to user request.\n");
315 nsock->pending = NULL;
319 static void nbd_size_clear(struct nbd_device *nbd)
321 if (nbd->config->bytesize) {
322 set_capacity(nbd->disk, 0);
323 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
327 static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
331 blksize = 1u << NBD_DEF_BLKSIZE_BITS;
332 if (blksize < 512 || blksize > PAGE_SIZE || !is_power_of_2(blksize))
335 nbd->config->bytesize = bytesize;
336 nbd->config->blksize_bits = __ffs(blksize);
341 if (nbd->config->flags & NBD_FLAG_SEND_TRIM) {
342 nbd->disk->queue->limits.discard_granularity = blksize;
343 nbd->disk->queue->limits.discard_alignment = blksize;
344 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
346 blk_queue_logical_block_size(nbd->disk->queue, blksize);
347 blk_queue_physical_block_size(nbd->disk->queue, blksize);
350 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
351 if (!set_capacity_and_notify(nbd->disk, bytesize >> 9))
352 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
356 static void nbd_complete_rq(struct request *req)
358 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
360 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", req,
361 cmd->status ? "failed" : "done");
363 blk_mq_end_request(req, cmd->status);
367 * Forcibly shutdown the socket causing all listeners to error
369 static void sock_shutdown(struct nbd_device *nbd)
371 struct nbd_config *config = nbd->config;
374 if (config->num_connections == 0)
376 if (test_and_set_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
379 for (i = 0; i < config->num_connections; i++) {
380 struct nbd_sock *nsock = config->socks[i];
381 mutex_lock(&nsock->tx_lock);
382 nbd_mark_nsock_dead(nbd, nsock, 0);
383 mutex_unlock(&nsock->tx_lock);
385 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
388 static u32 req_to_nbd_cmd_type(struct request *req)
390 switch (req_op(req)) {
394 return NBD_CMD_FLUSH;
396 return NBD_CMD_WRITE;
404 static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
407 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
408 struct nbd_device *nbd = cmd->nbd;
409 struct nbd_config *config;
411 if (!mutex_trylock(&cmd->lock))
412 return BLK_EH_RESET_TIMER;
414 __clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
415 if (!refcount_inc_not_zero(&nbd->config_refs)) {
416 cmd->status = BLK_STS_TIMEOUT;
417 mutex_unlock(&cmd->lock);
420 config = nbd->config;
422 if (config->num_connections > 1 ||
423 (config->num_connections == 1 && nbd->tag_set.timeout)) {
424 dev_err_ratelimited(nbd_to_dev(nbd),
425 "Connection timed out, retrying (%d/%d alive)\n",
426 atomic_read(&config->live_connections),
427 config->num_connections);
429 * Hooray we have more connections, requeue this IO, the submit
430 * path will put it on a real connection. Or if only one
431 * connection is configured, the submit path will wait util
432 * a new connection is reconfigured or util dead timeout.
435 if (cmd->index < config->num_connections) {
436 struct nbd_sock *nsock =
437 config->socks[cmd->index];
438 mutex_lock(&nsock->tx_lock);
439 /* We can have multiple outstanding requests, so
440 * we don't want to mark the nsock dead if we've
441 * already reconnected with a new socket, so
442 * only mark it dead if its the same socket we
445 if (cmd->cookie == nsock->cookie)
446 nbd_mark_nsock_dead(nbd, nsock, 1);
447 mutex_unlock(&nsock->tx_lock);
449 mutex_unlock(&cmd->lock);
450 nbd_requeue_cmd(cmd);
456 if (!nbd->tag_set.timeout) {
458 * Userspace sets timeout=0 to disable socket disconnection,
459 * so just warn and reset the timer.
461 struct nbd_sock *nsock = config->socks[cmd->index];
463 dev_info(nbd_to_dev(nbd), "Possible stuck request %p: control (%s@%llu,%uB). Runtime %u seconds\n",
464 req, nbdcmd_to_ascii(req_to_nbd_cmd_type(req)),
465 (unsigned long long)blk_rq_pos(req) << 9,
466 blk_rq_bytes(req), (req->timeout / HZ) * cmd->retries);
468 mutex_lock(&nsock->tx_lock);
469 if (cmd->cookie != nsock->cookie) {
470 nbd_requeue_cmd(cmd);
471 mutex_unlock(&nsock->tx_lock);
472 mutex_unlock(&cmd->lock);
476 mutex_unlock(&nsock->tx_lock);
477 mutex_unlock(&cmd->lock);
479 return BLK_EH_RESET_TIMER;
482 dev_err_ratelimited(nbd_to_dev(nbd), "Connection timed out\n");
483 set_bit(NBD_RT_TIMEDOUT, &config->runtime_flags);
484 cmd->status = BLK_STS_IOERR;
485 mutex_unlock(&cmd->lock);
489 blk_mq_complete_request(req);
494 * Send or receive packet.
496 static int sock_xmit(struct nbd_device *nbd, int index, int send,
497 struct iov_iter *iter, int msg_flags, int *sent)
499 struct nbd_config *config = nbd->config;
500 struct socket *sock = config->socks[index]->sock;
503 unsigned int noreclaim_flag;
505 if (unlikely(!sock)) {
506 dev_err_ratelimited(disk_to_dev(nbd->disk),
507 "Attempted %s on closed socket in sock_xmit\n",
508 (send ? "send" : "recv"));
512 msg.msg_iter = *iter;
514 noreclaim_flag = memalloc_noreclaim_save();
516 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
519 msg.msg_control = NULL;
520 msg.msg_controllen = 0;
521 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
524 result = sock_sendmsg(sock, &msg);
526 result = sock_recvmsg(sock, &msg, msg.msg_flags);
530 result = -EPIPE; /* short read */
535 } while (msg_data_left(&msg));
537 memalloc_noreclaim_restore(noreclaim_flag);
543 * Different settings for sk->sk_sndtimeo can result in different return values
544 * if there is a signal pending when we enter sendmsg, because reasons?
546 static inline int was_interrupted(int result)
548 return result == -ERESTARTSYS || result == -EINTR;
551 /* always call with the tx_lock held */
552 static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
554 struct request *req = blk_mq_rq_from_pdu(cmd);
555 struct nbd_config *config = nbd->config;
556 struct nbd_sock *nsock = config->socks[index];
558 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
559 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
560 struct iov_iter from;
561 unsigned long size = blk_rq_bytes(req);
565 u32 nbd_cmd_flags = 0;
566 int sent = nsock->sent, skip = 0;
568 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
570 type = req_to_nbd_cmd_type(req);
574 if (rq_data_dir(req) == WRITE &&
575 (config->flags & NBD_FLAG_READ_ONLY)) {
576 dev_err_ratelimited(disk_to_dev(nbd->disk),
577 "Write on read-only\n");
581 if (req->cmd_flags & REQ_FUA)
582 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
584 /* We did a partial send previously, and we at least sent the whole
585 * request struct, so just go and send the rest of the pages in the
589 if (sent >= sizeof(request)) {
590 skip = sent - sizeof(request);
592 /* initialize handle for tracing purposes */
593 handle = nbd_cmd_handle(cmd);
597 iov_iter_advance(&from, sent);
602 cmd->cookie = nsock->cookie;
604 request.type = htonl(type | nbd_cmd_flags);
605 if (type != NBD_CMD_FLUSH) {
606 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
607 request.len = htonl(size);
609 handle = nbd_cmd_handle(cmd);
610 memcpy(request.handle, &handle, sizeof(handle));
612 trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
614 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
615 req, nbdcmd_to_ascii(type),
616 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
617 result = sock_xmit(nbd, index, 1, &from,
618 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
619 trace_nbd_header_sent(req, handle);
621 if (was_interrupted(result)) {
622 /* If we havne't sent anything we can just return BUSY,
623 * however if we have sent something we need to make
624 * sure we only allow this req to be sent until we are
628 nsock->pending = req;
631 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
632 return BLK_STS_RESOURCE;
634 dev_err_ratelimited(disk_to_dev(nbd->disk),
635 "Send control failed (result %d)\n", result);
639 if (type != NBD_CMD_WRITE)
644 struct bio *next = bio->bi_next;
645 struct bvec_iter iter;
648 bio_for_each_segment(bvec, bio, iter) {
649 bool is_last = !next && bio_iter_last(bvec, iter);
650 int flags = is_last ? 0 : MSG_MORE;
652 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
654 iov_iter_bvec(&from, WRITE, &bvec, 1, bvec.bv_len);
656 if (skip >= iov_iter_count(&from)) {
657 skip -= iov_iter_count(&from);
660 iov_iter_advance(&from, skip);
663 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
665 if (was_interrupted(result)) {
666 /* We've already sent the header, we
667 * have no choice but to set pending and
670 nsock->pending = req;
672 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
673 return BLK_STS_RESOURCE;
675 dev_err(disk_to_dev(nbd->disk),
676 "Send data failed (result %d)\n",
681 * The completion might already have come in,
682 * so break for the last one instead of letting
683 * the iterator do it. This prevents use-after-free
692 trace_nbd_payload_sent(req, handle);
693 nsock->pending = NULL;
698 /* NULL returned = something went wrong, inform userspace */
699 static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
701 struct nbd_config *config = nbd->config;
703 struct nbd_reply reply;
705 struct request *req = NULL;
709 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
714 iov_iter_kvec(&to, READ, &iov, 1, sizeof(reply));
715 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
717 if (!nbd_disconnected(config))
718 dev_err(disk_to_dev(nbd->disk),
719 "Receive control failed (result %d)\n", result);
720 return ERR_PTR(result);
723 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
724 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
725 (unsigned long)ntohl(reply.magic));
726 return ERR_PTR(-EPROTO);
729 memcpy(&handle, reply.handle, sizeof(handle));
730 tag = nbd_handle_to_tag(handle);
731 hwq = blk_mq_unique_tag_to_hwq(tag);
732 if (hwq < nbd->tag_set.nr_hw_queues)
733 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
734 blk_mq_unique_tag_to_tag(tag));
735 if (!req || !blk_mq_request_started(req)) {
736 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
738 return ERR_PTR(-ENOENT);
740 trace_nbd_header_received(req, handle);
741 cmd = blk_mq_rq_to_pdu(req);
743 mutex_lock(&cmd->lock);
744 if (!__test_and_clear_bit(NBD_CMD_INFLIGHT, &cmd->flags)) {
745 dev_err(disk_to_dev(nbd->disk), "Suspicious reply %d (status %u flags %lu)",
746 tag, cmd->status, cmd->flags);
750 if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
751 dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
752 req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
756 if (cmd->status != BLK_STS_OK) {
757 dev_err(disk_to_dev(nbd->disk), "Command already handled %p\n",
762 if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) {
763 dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n",
768 if (ntohl(reply.error)) {
769 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
771 cmd->status = BLK_STS_IOERR;
775 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
776 if (rq_data_dir(req) != WRITE) {
777 struct req_iterator iter;
780 rq_for_each_segment(bvec, req, iter) {
781 iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len);
782 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
784 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
787 * If we've disconnected, we need to make sure we
788 * complete this request, otherwise error out
789 * and let the timeout stuff handle resubmitting
790 * this request onto another connection.
792 if (nbd_disconnected(config)) {
793 cmd->status = BLK_STS_IOERR;
799 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
804 trace_nbd_payload_received(req, handle);
805 mutex_unlock(&cmd->lock);
806 return ret ? ERR_PTR(ret) : cmd;
809 static void recv_work(struct work_struct *work)
811 struct recv_thread_args *args = container_of(work,
812 struct recv_thread_args,
814 struct nbd_device *nbd = args->nbd;
815 struct nbd_config *config = nbd->config;
820 cmd = nbd_read_stat(nbd, args->index);
822 struct nbd_sock *nsock = config->socks[args->index];
824 mutex_lock(&nsock->tx_lock);
825 nbd_mark_nsock_dead(nbd, nsock, 1);
826 mutex_unlock(&nsock->tx_lock);
830 rq = blk_mq_rq_from_pdu(cmd);
831 if (likely(!blk_should_fake_timeout(rq->q)))
832 blk_mq_complete_request(rq);
835 atomic_dec(&config->recv_threads);
836 wake_up(&config->recv_wq);
840 static bool nbd_clear_req(struct request *req, void *data, bool reserved)
842 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
844 /* don't abort one completed request */
845 if (blk_mq_request_completed(req))
848 mutex_lock(&cmd->lock);
849 __clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
850 cmd->status = BLK_STS_IOERR;
851 mutex_unlock(&cmd->lock);
853 blk_mq_complete_request(req);
857 static void nbd_clear_que(struct nbd_device *nbd)
859 blk_mq_quiesce_queue(nbd->disk->queue);
860 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
861 blk_mq_unquiesce_queue(nbd->disk->queue);
862 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
865 static int find_fallback(struct nbd_device *nbd, int index)
867 struct nbd_config *config = nbd->config;
869 struct nbd_sock *nsock = config->socks[index];
870 int fallback = nsock->fallback_index;
872 if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
875 if (config->num_connections <= 1) {
876 dev_err_ratelimited(disk_to_dev(nbd->disk),
877 "Dead connection, failed to find a fallback\n");
881 if (fallback >= 0 && fallback < config->num_connections &&
882 !config->socks[fallback]->dead)
885 if (nsock->fallback_index < 0 ||
886 nsock->fallback_index >= config->num_connections ||
887 config->socks[nsock->fallback_index]->dead) {
889 for (i = 0; i < config->num_connections; i++) {
892 if (!config->socks[i]->dead) {
897 nsock->fallback_index = new_index;
899 dev_err_ratelimited(disk_to_dev(nbd->disk),
900 "Dead connection, failed to find a fallback\n");
904 new_index = nsock->fallback_index;
908 static int wait_for_reconnect(struct nbd_device *nbd)
910 struct nbd_config *config = nbd->config;
911 if (!config->dead_conn_timeout)
913 if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
915 return wait_event_timeout(config->conn_wait,
916 atomic_read(&config->live_connections) > 0,
917 config->dead_conn_timeout) > 0;
920 static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
922 struct request *req = blk_mq_rq_from_pdu(cmd);
923 struct nbd_device *nbd = cmd->nbd;
924 struct nbd_config *config;
925 struct nbd_sock *nsock;
928 if (!refcount_inc_not_zero(&nbd->config_refs)) {
929 dev_err_ratelimited(disk_to_dev(nbd->disk),
930 "Socks array is empty\n");
931 blk_mq_start_request(req);
934 config = nbd->config;
936 if (index >= config->num_connections) {
937 dev_err_ratelimited(disk_to_dev(nbd->disk),
938 "Attempted send on invalid socket\n");
940 blk_mq_start_request(req);
943 cmd->status = BLK_STS_OK;
945 nsock = config->socks[index];
946 mutex_lock(&nsock->tx_lock);
948 int old_index = index;
949 index = find_fallback(nbd, index);
950 mutex_unlock(&nsock->tx_lock);
952 if (wait_for_reconnect(nbd)) {
956 /* All the sockets should already be down at this point,
957 * we just want to make sure that DISCONNECTED is set so
958 * any requests that come in that were queue'ed waiting
959 * for the reconnect timer don't trigger the timer again
960 * and instead just error out.
964 blk_mq_start_request(req);
970 /* Handle the case that we have a pending request that was partially
971 * transmitted that _has_ to be serviced first. We need to call requeue
972 * here so that it gets put _after_ the request that is already on the
975 blk_mq_start_request(req);
976 if (unlikely(nsock->pending && nsock->pending != req)) {
977 nbd_requeue_cmd(cmd);
982 * Some failures are related to the link going down, so anything that
983 * returns EAGAIN can be retried on a different socket.
985 ret = nbd_send_cmd(nbd, cmd, index);
987 * Access to this flag is protected by cmd->lock, thus it's safe to set
988 * the flag after nbd_send_cmd() succeed to send request to server.
991 __set_bit(NBD_CMD_INFLIGHT, &cmd->flags);
992 else if (ret == -EAGAIN) {
993 dev_err_ratelimited(disk_to_dev(nbd->disk),
994 "Request send failed, requeueing\n");
995 nbd_mark_nsock_dead(nbd, nsock, 1);
996 nbd_requeue_cmd(cmd);
1000 mutex_unlock(&nsock->tx_lock);
1001 nbd_config_put(nbd);
1005 static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
1006 const struct blk_mq_queue_data *bd)
1008 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
1012 * Since we look at the bio's to send the request over the network we
1013 * need to make sure the completion work doesn't mark this request done
1014 * before we are done doing our send. This keeps us from dereferencing
1015 * freed data if we have particularly fast completions (ie we get the
1016 * completion before we exit sock_xmit on the last bvec) or in the case
1017 * that the server is misbehaving (or there was an error) before we're
1018 * done sending everything over the wire.
1020 mutex_lock(&cmd->lock);
1021 clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
1023 /* We can be called directly from the user space process, which means we
1024 * could possibly have signals pending so our sendmsg will fail. In
1025 * this case we need to return that we are busy, otherwise error out as
1028 ret = nbd_handle_cmd(cmd, hctx->queue_num);
1030 ret = BLK_STS_IOERR;
1033 mutex_unlock(&cmd->lock);
1038 static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
1041 struct socket *sock;
1044 sock = sockfd_lookup(fd, err);
1048 if (sock->ops->shutdown == sock_no_shutdown) {
1049 dev_err(disk_to_dev(nbd->disk), "Unsupported socket: shutdown callout must be supported.\n");
1058 static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
1061 struct nbd_config *config = nbd->config;
1062 struct socket *sock;
1063 struct nbd_sock **socks;
1064 struct nbd_sock *nsock;
1067 sock = nbd_get_socket(nbd, arg, &err);
1072 * We need to make sure we don't get any errant requests while we're
1073 * reallocating the ->socks array.
1075 blk_mq_freeze_queue(nbd->disk->queue);
1077 if (!netlink && !nbd->task_setup &&
1078 !test_bit(NBD_RT_BOUND, &config->runtime_flags))
1079 nbd->task_setup = current;
1082 (nbd->task_setup != current ||
1083 test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
1084 dev_err(disk_to_dev(nbd->disk),
1085 "Device being setup by another task");
1090 nsock = kzalloc(sizeof(*nsock), GFP_KERNEL);
1096 socks = krealloc(config->socks, (config->num_connections + 1) *
1097 sizeof(struct nbd_sock *), GFP_KERNEL);
1104 config->socks = socks;
1106 nsock->fallback_index = -1;
1107 nsock->dead = false;
1108 mutex_init(&nsock->tx_lock);
1110 nsock->pending = NULL;
1113 socks[config->num_connections++] = nsock;
1114 atomic_inc(&config->live_connections);
1115 blk_mq_unfreeze_queue(nbd->disk->queue);
1120 blk_mq_unfreeze_queue(nbd->disk->queue);
1125 static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
1127 struct nbd_config *config = nbd->config;
1128 struct socket *sock, *old;
1129 struct recv_thread_args *args;
1133 sock = nbd_get_socket(nbd, arg, &err);
1137 args = kzalloc(sizeof(*args), GFP_KERNEL);
1143 for (i = 0; i < config->num_connections; i++) {
1144 struct nbd_sock *nsock = config->socks[i];
1149 mutex_lock(&nsock->tx_lock);
1151 mutex_unlock(&nsock->tx_lock);
1154 sk_set_memalloc(sock->sk);
1155 if (nbd->tag_set.timeout)
1156 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
1157 atomic_inc(&config->recv_threads);
1158 refcount_inc(&nbd->config_refs);
1160 nsock->fallback_index = -1;
1162 nsock->dead = false;
1163 INIT_WORK(&args->work, recv_work);
1167 mutex_unlock(&nsock->tx_lock);
1170 clear_bit(NBD_RT_DISCONNECTED, &config->runtime_flags);
1172 /* We take the tx_mutex in an error path in the recv_work, so we
1173 * need to queue_work outside of the tx_mutex.
1175 queue_work(nbd->recv_workq, &args->work);
1177 atomic_inc(&config->live_connections);
1178 wake_up(&config->conn_wait);
1186 static void nbd_bdev_reset(struct block_device *bdev)
1188 if (bdev->bd_openers > 1)
1190 set_capacity(bdev->bd_disk, 0);
1193 static void nbd_parse_flags(struct nbd_device *nbd)
1195 struct nbd_config *config = nbd->config;
1196 if (config->flags & NBD_FLAG_READ_ONLY)
1197 set_disk_ro(nbd->disk, true);
1199 set_disk_ro(nbd->disk, false);
1200 if (config->flags & NBD_FLAG_SEND_TRIM)
1201 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nbd->disk->queue);
1202 if (config->flags & NBD_FLAG_SEND_FLUSH) {
1203 if (config->flags & NBD_FLAG_SEND_FUA)
1204 blk_queue_write_cache(nbd->disk->queue, true, true);
1206 blk_queue_write_cache(nbd->disk->queue, true, false);
1209 blk_queue_write_cache(nbd->disk->queue, false, false);
1212 static void send_disconnects(struct nbd_device *nbd)
1214 struct nbd_config *config = nbd->config;
1215 struct nbd_request request = {
1216 .magic = htonl(NBD_REQUEST_MAGIC),
1217 .type = htonl(NBD_CMD_DISC),
1219 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1220 struct iov_iter from;
1223 for (i = 0; i < config->num_connections; i++) {
1224 struct nbd_sock *nsock = config->socks[i];
1226 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
1227 mutex_lock(&nsock->tx_lock);
1228 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
1230 dev_err(disk_to_dev(nbd->disk),
1231 "Send disconnect failed %d\n", ret);
1232 mutex_unlock(&nsock->tx_lock);
1236 static int nbd_disconnect(struct nbd_device *nbd)
1238 struct nbd_config *config = nbd->config;
1240 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
1241 set_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
1242 set_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags);
1243 send_disconnects(nbd);
1247 static void nbd_clear_sock(struct nbd_device *nbd)
1251 nbd->task_setup = NULL;
1254 static void nbd_config_put(struct nbd_device *nbd)
1256 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1257 &nbd->config_lock)) {
1258 struct nbd_config *config = nbd->config;
1259 nbd_dev_dbg_close(nbd);
1260 nbd_size_clear(nbd);
1261 if (test_and_clear_bit(NBD_RT_HAS_PID_FILE,
1262 &config->runtime_flags))
1263 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1264 nbd->task_recv = NULL;
1265 if (test_and_clear_bit(NBD_RT_HAS_BACKEND_FILE,
1266 &config->runtime_flags)) {
1267 device_remove_file(disk_to_dev(nbd->disk), &backend_attr);
1268 kfree(nbd->backend);
1269 nbd->backend = NULL;
1271 nbd_clear_sock(nbd);
1272 if (config->num_connections) {
1274 for (i = 0; i < config->num_connections; i++) {
1275 sockfd_put(config->socks[i]->sock);
1276 kfree(config->socks[i]);
1278 kfree(config->socks);
1283 if (nbd->recv_workq)
1284 destroy_workqueue(nbd->recv_workq);
1285 nbd->recv_workq = NULL;
1287 nbd->tag_set.timeout = 0;
1288 nbd->disk->queue->limits.discard_granularity = 0;
1289 nbd->disk->queue->limits.discard_alignment = 0;
1290 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
1291 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue);
1293 mutex_unlock(&nbd->config_lock);
1295 module_put(THIS_MODULE);
1299 static int nbd_start_device(struct nbd_device *nbd)
1301 struct nbd_config *config = nbd->config;
1302 int num_connections = config->num_connections;
1309 if (num_connections > 1 &&
1310 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
1311 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
1315 nbd->recv_workq = alloc_workqueue("knbd%d-recv",
1316 WQ_MEM_RECLAIM | WQ_HIGHPRI |
1317 WQ_UNBOUND, 0, nbd->index);
1318 if (!nbd->recv_workq) {
1319 dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1323 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
1324 nbd->task_recv = current;
1326 nbd_parse_flags(nbd);
1328 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1330 dev_err(disk_to_dev(nbd->disk), "device_create_file failed for pid!\n");
1333 set_bit(NBD_RT_HAS_PID_FILE, &config->runtime_flags);
1335 nbd_dev_dbg_init(nbd);
1336 for (i = 0; i < num_connections; i++) {
1337 struct recv_thread_args *args;
1339 args = kzalloc(sizeof(*args), GFP_KERNEL);
1343 * If num_connections is m (2 < m),
1344 * and NO.1 ~ NO.n(1 < n < m) kzallocs are successful.
1345 * But NO.(n + 1) failed. We still have n recv threads.
1346 * So, add flush_workqueue here to prevent recv threads
1347 * dropping the last config_refs and trying to destroy
1348 * the workqueue from inside the workqueue.
1351 flush_workqueue(nbd->recv_workq);
1354 sk_set_memalloc(config->socks[i]->sock->sk);
1355 if (nbd->tag_set.timeout)
1356 config->socks[i]->sock->sk->sk_sndtimeo =
1357 nbd->tag_set.timeout;
1358 atomic_inc(&config->recv_threads);
1359 refcount_inc(&nbd->config_refs);
1360 INIT_WORK(&args->work, recv_work);
1363 queue_work(nbd->recv_workq, &args->work);
1365 return nbd_set_size(nbd, config->bytesize, nbd_blksize(config));
1368 static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1370 struct nbd_config *config = nbd->config;
1373 ret = nbd_start_device(nbd);
1378 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
1379 mutex_unlock(&nbd->config_lock);
1380 ret = wait_event_interruptible(config->recv_wq,
1381 atomic_read(&config->recv_threads) == 0);
1384 flush_workqueue(nbd->recv_workq);
1386 mutex_lock(&nbd->config_lock);
1387 nbd_bdev_reset(bdev);
1388 /* user requested, ignore socket errors */
1389 if (test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags))
1391 if (test_bit(NBD_RT_TIMEDOUT, &config->runtime_flags))
1396 static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1397 struct block_device *bdev)
1400 __invalidate_device(bdev, true);
1401 nbd_bdev_reset(bdev);
1402 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
1403 &nbd->config->runtime_flags))
1404 nbd_config_put(nbd);
1407 static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout)
1409 nbd->tag_set.timeout = timeout * HZ;
1411 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1413 blk_queue_rq_timeout(nbd->disk->queue, 30 * HZ);
1416 /* Must be called with config_lock held */
1417 static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
1418 unsigned int cmd, unsigned long arg)
1420 struct nbd_config *config = nbd->config;
1424 case NBD_DISCONNECT:
1425 return nbd_disconnect(nbd);
1426 case NBD_CLEAR_SOCK:
1427 nbd_clear_sock_ioctl(nbd, bdev);
1430 return nbd_add_socket(nbd, arg, false);
1431 case NBD_SET_BLKSIZE:
1432 return nbd_set_size(nbd, config->bytesize, arg);
1434 return nbd_set_size(nbd, arg, nbd_blksize(config));
1435 case NBD_SET_SIZE_BLOCKS:
1436 if (check_shl_overflow(arg, config->blksize_bits, &bytesize))
1438 return nbd_set_size(nbd, bytesize, nbd_blksize(config));
1439 case NBD_SET_TIMEOUT:
1440 nbd_set_cmd_timeout(nbd, arg);
1444 config->flags = arg;
1447 return nbd_start_device_ioctl(nbd, bdev);
1450 * This is for compatibility only. The queue is always cleared
1451 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1454 case NBD_PRINT_DEBUG:
1456 * For compatibility only, we no longer keep a list of
1457 * outstanding requests.
1464 static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1465 unsigned int cmd, unsigned long arg)
1467 struct nbd_device *nbd = bdev->bd_disk->private_data;
1468 struct nbd_config *config = nbd->config;
1469 int error = -EINVAL;
1471 if (!capable(CAP_SYS_ADMIN))
1474 /* The block layer will pass back some non-nbd ioctls in case we have
1475 * special handling for them, but we don't so just return an error.
1477 if (_IOC_TYPE(cmd) != 0xab)
1480 mutex_lock(&nbd->config_lock);
1482 /* Don't allow ioctl operations on a nbd device that was created with
1483 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1485 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
1486 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1487 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1489 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
1490 mutex_unlock(&nbd->config_lock);
1494 static struct nbd_config *nbd_alloc_config(void)
1496 struct nbd_config *config;
1498 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1501 atomic_set(&config->recv_threads, 0);
1502 init_waitqueue_head(&config->recv_wq);
1503 init_waitqueue_head(&config->conn_wait);
1504 config->blksize_bits = NBD_DEF_BLKSIZE_BITS;
1505 atomic_set(&config->live_connections, 0);
1506 try_module_get(THIS_MODULE);
1510 static int nbd_open(struct block_device *bdev, fmode_t mode)
1512 struct nbd_device *nbd;
1515 mutex_lock(&nbd_index_mutex);
1516 nbd = bdev->bd_disk->private_data;
1521 if (!refcount_inc_not_zero(&nbd->refs)) {
1525 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1526 struct nbd_config *config;
1528 mutex_lock(&nbd->config_lock);
1529 if (refcount_inc_not_zero(&nbd->config_refs)) {
1530 mutex_unlock(&nbd->config_lock);
1533 config = nbd->config = nbd_alloc_config();
1536 mutex_unlock(&nbd->config_lock);
1539 refcount_set(&nbd->config_refs, 1);
1540 refcount_inc(&nbd->refs);
1541 mutex_unlock(&nbd->config_lock);
1543 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1544 } else if (nbd_disconnected(nbd->config)) {
1546 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1549 mutex_unlock(&nbd_index_mutex);
1553 static void nbd_release(struct gendisk *disk, fmode_t mode)
1555 struct nbd_device *nbd = disk->private_data;
1557 if (test_bit(NBD_RT_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) &&
1558 disk->part0->bd_openers == 0)
1559 nbd_disconnect_and_put(nbd);
1561 nbd_config_put(nbd);
1565 static const struct block_device_operations nbd_fops =
1567 .owner = THIS_MODULE,
1569 .release = nbd_release,
1571 .compat_ioctl = nbd_ioctl,
1574 #if IS_ENABLED(CONFIG_DEBUG_FS)
1576 static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1578 struct nbd_device *nbd = s->private;
1581 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
1586 DEFINE_SHOW_ATTRIBUTE(nbd_dbg_tasks);
1588 static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1590 struct nbd_device *nbd = s->private;
1591 u32 flags = nbd->config->flags;
1593 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1595 seq_puts(s, "Known flags:\n");
1597 if (flags & NBD_FLAG_HAS_FLAGS)
1598 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1599 if (flags & NBD_FLAG_READ_ONLY)
1600 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1601 if (flags & NBD_FLAG_SEND_FLUSH)
1602 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
1603 if (flags & NBD_FLAG_SEND_FUA)
1604 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
1605 if (flags & NBD_FLAG_SEND_TRIM)
1606 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1611 DEFINE_SHOW_ATTRIBUTE(nbd_dbg_flags);
1613 static int nbd_dev_dbg_init(struct nbd_device *nbd)
1616 struct nbd_config *config = nbd->config;
1621 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
1623 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1627 config->dbg_dir = dir;
1629 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_fops);
1630 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
1631 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
1632 debugfs_create_u32("blocksize_bits", 0444, dir, &config->blksize_bits);
1633 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_fops);
1638 static void nbd_dev_dbg_close(struct nbd_device *nbd)
1640 debugfs_remove_recursive(nbd->config->dbg_dir);
1643 static int nbd_dbg_init(void)
1645 struct dentry *dbg_dir;
1647 dbg_dir = debugfs_create_dir("nbd", NULL);
1651 nbd_dbg_dir = dbg_dir;
1656 static void nbd_dbg_close(void)
1658 debugfs_remove_recursive(nbd_dbg_dir);
1661 #else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1663 static int nbd_dev_dbg_init(struct nbd_device *nbd)
1668 static void nbd_dev_dbg_close(struct nbd_device *nbd)
1672 static int nbd_dbg_init(void)
1677 static void nbd_dbg_close(void)
1683 static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1684 unsigned int hctx_idx, unsigned int numa_node)
1686 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
1687 cmd->nbd = set->driver_data;
1689 mutex_init(&cmd->lock);
1693 static const struct blk_mq_ops nbd_mq_ops = {
1694 .queue_rq = nbd_queue_rq,
1695 .complete = nbd_complete_rq,
1696 .init_request = nbd_init_request,
1697 .timeout = nbd_xmit_timeout,
1700 static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
1702 struct nbd_device *nbd;
1703 struct gendisk *disk;
1706 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1710 nbd->tag_set.ops = &nbd_mq_ops;
1711 nbd->tag_set.nr_hw_queues = 1;
1712 nbd->tag_set.queue_depth = 128;
1713 nbd->tag_set.numa_node = NUMA_NO_NODE;
1714 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1715 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1717 nbd->tag_set.driver_data = nbd;
1718 INIT_WORK(&nbd->remove_work, nbd_dev_remove_work);
1719 nbd->backend = NULL;
1721 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1725 mutex_lock(&nbd_index_mutex);
1727 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1732 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1737 mutex_unlock(&nbd_index_mutex);
1741 disk = blk_mq_alloc_disk(&nbd->tag_set, NULL);
1743 err = PTR_ERR(disk);
1749 * Tell the block layer that we are not a rotational device
1751 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
1752 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1753 disk->queue->limits.discard_granularity = 0;
1754 disk->queue->limits.discard_alignment = 0;
1755 blk_queue_max_discard_sectors(disk->queue, 0);
1756 blk_queue_max_segment_size(disk->queue, UINT_MAX);
1757 blk_queue_max_segments(disk->queue, USHRT_MAX);
1758 blk_queue_max_hw_sectors(disk->queue, 65536);
1759 disk->queue->limits.max_sectors = 256;
1761 mutex_init(&nbd->config_lock);
1762 refcount_set(&nbd->config_refs, 0);
1764 * Start out with a zero references to keep other threads from using
1765 * this device until it is fully initialized.
1767 refcount_set(&nbd->refs, 0);
1768 INIT_LIST_HEAD(&nbd->list);
1769 disk->major = NBD_MAJOR;
1771 /* Too big first_minor can cause duplicate creation of
1772 * sysfs files/links, since first_minor will be truncated to
1773 * byte in __device_add_disk().
1775 disk->first_minor = index << part_shift;
1776 if (disk->first_minor > 0xff) {
1781 disk->minors = 1 << part_shift;
1782 disk->fops = &nbd_fops;
1783 disk->private_data = nbd;
1784 sprintf(disk->disk_name, "nbd%d", index);
1785 err = add_disk(disk);
1790 * Now publish the device.
1792 refcount_set(&nbd->refs, refs);
1793 nbd_total_devices++;
1797 blk_cleanup_disk(disk);
1799 mutex_lock(&nbd_index_mutex);
1800 idr_remove(&nbd_index_idr, index);
1801 mutex_unlock(&nbd_index_mutex);
1803 blk_mq_free_tag_set(&nbd->tag_set);
1807 return ERR_PTR(err);
1810 static struct nbd_device *nbd_find_get_unused(void)
1812 struct nbd_device *nbd;
1815 lockdep_assert_held(&nbd_index_mutex);
1817 idr_for_each_entry(&nbd_index_idr, nbd, id) {
1818 if (refcount_read(&nbd->config_refs) ||
1819 test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags))
1821 if (refcount_inc_not_zero(&nbd->refs))
1828 /* Netlink interface. */
1829 static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
1830 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1831 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1832 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1833 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1834 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1835 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1836 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
1837 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
1838 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
1839 [NBD_ATTR_BACKEND_IDENTIFIER] = { .type = NLA_STRING},
1842 static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
1843 [NBD_SOCK_FD] = { .type = NLA_U32 },
1846 /* We don't use this right now since we don't parse the incoming list, but we
1847 * still want it here so userspace knows what to expect.
1849 static const struct nla_policy __attribute__((unused))
1850 nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1851 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1852 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1855 static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
1857 struct nbd_config *config = nbd->config;
1858 u64 bsize = nbd_blksize(config);
1859 u64 bytes = config->bytesize;
1861 if (info->attrs[NBD_ATTR_SIZE_BYTES])
1862 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1864 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES])
1865 bsize = nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1867 if (bytes != config->bytesize || bsize != nbd_blksize(config))
1868 return nbd_set_size(nbd, bytes, bsize);
1872 static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1874 struct nbd_device *nbd;
1875 struct nbd_config *config;
1878 bool put_dev = false;
1880 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1883 if (info->attrs[NBD_ATTR_INDEX])
1884 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1885 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1886 printk(KERN_ERR "nbd: must specify at least one socket\n");
1889 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1890 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1894 mutex_lock(&nbd_index_mutex);
1896 nbd = nbd_find_get_unused();
1898 nbd = idr_find(&nbd_index_idr, index);
1900 if ((test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
1901 test_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags)) ||
1902 !refcount_inc_not_zero(&nbd->refs)) {
1903 mutex_unlock(&nbd_index_mutex);
1904 pr_err("nbd: device at index %d is going down\n",
1910 mutex_unlock(&nbd_index_mutex);
1913 nbd = nbd_dev_add(index, 2);
1915 pr_err("nbd: failed to add new device\n");
1916 return PTR_ERR(nbd);
1920 mutex_lock(&nbd->config_lock);
1921 if (refcount_read(&nbd->config_refs)) {
1922 mutex_unlock(&nbd->config_lock);
1926 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1929 if (WARN_ON(nbd->config)) {
1930 mutex_unlock(&nbd->config_lock);
1934 config = nbd->config = nbd_alloc_config();
1936 mutex_unlock(&nbd->config_lock);
1938 printk(KERN_ERR "nbd: couldn't allocate config\n");
1941 refcount_set(&nbd->config_refs, 1);
1942 set_bit(NBD_RT_BOUND, &config->runtime_flags);
1944 ret = nbd_genl_size_set(info, nbd);
1948 if (info->attrs[NBD_ATTR_TIMEOUT])
1949 nbd_set_cmd_timeout(nbd,
1950 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
1951 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1952 config->dead_conn_timeout =
1953 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1954 config->dead_conn_timeout *= HZ;
1956 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1958 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
1959 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1960 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1961 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1963 * We have 1 ref to keep the device around, and then 1
1964 * ref for our current operation here, which will be
1965 * inherited by the config. If we already have
1966 * DESTROY_ON_DISCONNECT set then we know we don't have
1967 * that extra ref already held so we don't need the
1970 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
1974 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
1976 refcount_inc(&nbd->refs);
1978 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
1979 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
1980 &config->runtime_flags);
1984 if (info->attrs[NBD_ATTR_SOCKETS]) {
1985 struct nlattr *attr;
1988 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1990 struct nlattr *socks[NBD_SOCK_MAX+1];
1992 if (nla_type(attr) != NBD_SOCK_ITEM) {
1993 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1997 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
2002 printk(KERN_ERR "nbd: error processing sock list\n");
2006 if (!socks[NBD_SOCK_FD])
2008 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2009 ret = nbd_add_socket(nbd, fd, true);
2014 ret = nbd_start_device(nbd);
2017 if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) {
2018 nbd->backend = nla_strdup(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER],
2020 if (!nbd->backend) {
2025 ret = device_create_file(disk_to_dev(nbd->disk), &backend_attr);
2027 dev_err(disk_to_dev(nbd->disk),
2028 "device_create_file failed for backend!\n");
2031 set_bit(NBD_RT_HAS_BACKEND_FILE, &config->runtime_flags);
2033 mutex_unlock(&nbd->config_lock);
2035 set_bit(NBD_RT_HAS_CONFIG_REF, &config->runtime_flags);
2036 refcount_inc(&nbd->config_refs);
2037 nbd_connect_reply(info, nbd->index);
2039 nbd_config_put(nbd);
2045 static void nbd_disconnect_and_put(struct nbd_device *nbd)
2047 mutex_lock(&nbd->config_lock);
2048 nbd_disconnect(nbd);
2051 * Make sure recv thread has finished, so it does not drop the last
2052 * config ref and try to destroy the workqueue from inside the work
2053 * queue. And this also ensure that we can safely call nbd_clear_que()
2054 * to cancel the inflight I/Os.
2056 if (nbd->recv_workq)
2057 flush_workqueue(nbd->recv_workq);
2059 nbd->task_setup = NULL;
2060 mutex_unlock(&nbd->config_lock);
2062 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
2063 &nbd->config->runtime_flags))
2064 nbd_config_put(nbd);
2067 static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
2069 struct nbd_device *nbd;
2072 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2075 if (!info->attrs[NBD_ATTR_INDEX]) {
2076 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
2079 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2080 mutex_lock(&nbd_index_mutex);
2081 nbd = idr_find(&nbd_index_idr, index);
2083 mutex_unlock(&nbd_index_mutex);
2084 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
2088 if (!refcount_inc_not_zero(&nbd->refs)) {
2089 mutex_unlock(&nbd_index_mutex);
2090 printk(KERN_ERR "nbd: device at index %d is going down\n",
2094 mutex_unlock(&nbd_index_mutex);
2095 if (!refcount_inc_not_zero(&nbd->config_refs))
2097 nbd_disconnect_and_put(nbd);
2098 nbd_config_put(nbd);
2104 static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
2106 struct nbd_device *nbd = NULL;
2107 struct nbd_config *config;
2110 bool put_dev = false;
2112 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2115 if (!info->attrs[NBD_ATTR_INDEX]) {
2116 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
2119 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2120 mutex_lock(&nbd_index_mutex);
2121 nbd = idr_find(&nbd_index_idr, index);
2123 mutex_unlock(&nbd_index_mutex);
2124 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
2129 if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) {
2130 if (nla_strcmp(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER],
2132 mutex_unlock(&nbd_index_mutex);
2133 dev_err(nbd_to_dev(nbd),
2134 "backend image doesn't match with %s\n",
2139 mutex_unlock(&nbd_index_mutex);
2140 dev_err(nbd_to_dev(nbd), "must specify backend\n");
2144 if (!refcount_inc_not_zero(&nbd->refs)) {
2145 mutex_unlock(&nbd_index_mutex);
2146 printk(KERN_ERR "nbd: device at index %d is going down\n",
2150 mutex_unlock(&nbd_index_mutex);
2152 if (!refcount_inc_not_zero(&nbd->config_refs)) {
2153 dev_err(nbd_to_dev(nbd),
2154 "not configured, cannot reconfigure\n");
2159 mutex_lock(&nbd->config_lock);
2160 config = nbd->config;
2161 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
2163 dev_err(nbd_to_dev(nbd),
2164 "not configured, cannot reconfigure\n");
2169 ret = nbd_genl_size_set(info, nbd);
2173 if (info->attrs[NBD_ATTR_TIMEOUT])
2174 nbd_set_cmd_timeout(nbd,
2175 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
2176 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
2177 config->dead_conn_timeout =
2178 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
2179 config->dead_conn_timeout *= HZ;
2181 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
2182 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
2183 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
2184 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
2188 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
2190 refcount_inc(&nbd->refs);
2193 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
2194 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
2195 &config->runtime_flags);
2197 clear_bit(NBD_RT_DISCONNECT_ON_CLOSE,
2198 &config->runtime_flags);
2202 if (info->attrs[NBD_ATTR_SOCKETS]) {
2203 struct nlattr *attr;
2206 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
2208 struct nlattr *socks[NBD_SOCK_MAX+1];
2210 if (nla_type(attr) != NBD_SOCK_ITEM) {
2211 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
2215 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
2220 printk(KERN_ERR "nbd: error processing sock list\n");
2224 if (!socks[NBD_SOCK_FD])
2226 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2227 ret = nbd_reconnect_socket(nbd, fd);
2233 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
2237 mutex_unlock(&nbd->config_lock);
2238 nbd_config_put(nbd);
2245 static const struct genl_small_ops nbd_connect_genl_ops[] = {
2247 .cmd = NBD_CMD_CONNECT,
2248 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2249 .doit = nbd_genl_connect,
2252 .cmd = NBD_CMD_DISCONNECT,
2253 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2254 .doit = nbd_genl_disconnect,
2257 .cmd = NBD_CMD_RECONFIGURE,
2258 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2259 .doit = nbd_genl_reconfigure,
2262 .cmd = NBD_CMD_STATUS,
2263 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2264 .doit = nbd_genl_status,
2268 static const struct genl_multicast_group nbd_mcast_grps[] = {
2269 { .name = NBD_GENL_MCAST_GROUP_NAME, },
2272 static struct genl_family nbd_genl_family __ro_after_init = {
2274 .name = NBD_GENL_FAMILY_NAME,
2275 .version = NBD_GENL_VERSION,
2276 .module = THIS_MODULE,
2277 .small_ops = nbd_connect_genl_ops,
2278 .n_small_ops = ARRAY_SIZE(nbd_connect_genl_ops),
2279 .maxattr = NBD_ATTR_MAX,
2280 .policy = nbd_attr_policy,
2281 .mcgrps = nbd_mcast_grps,
2282 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
2285 static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
2287 struct nlattr *dev_opt;
2291 /* This is a little racey, but for status it's ok. The
2292 * reason we don't take a ref here is because we can't
2293 * take a ref in the index == -1 case as we would need
2294 * to put under the nbd_index_mutex, which could
2295 * deadlock if we are configured to remove ourselves
2296 * once we're disconnected.
2298 if (refcount_read(&nbd->config_refs))
2300 dev_opt = nla_nest_start_noflag(reply, NBD_DEVICE_ITEM);
2303 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
2306 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
2310 nla_nest_end(reply, dev_opt);
2314 static int status_cb(int id, void *ptr, void *data)
2316 struct nbd_device *nbd = ptr;
2317 return populate_nbd_status(nbd, (struct sk_buff *)data);
2320 static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
2322 struct nlattr *dev_list;
2323 struct sk_buff *reply;
2329 if (info->attrs[NBD_ATTR_INDEX])
2330 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2332 mutex_lock(&nbd_index_mutex);
2334 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
2335 nla_attr_size(sizeof(u8)));
2336 msg_size *= (index == -1) ? nbd_total_devices : 1;
2338 reply = genlmsg_new(msg_size, GFP_KERNEL);
2341 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2348 dev_list = nla_nest_start_noflag(reply, NBD_ATTR_DEVICE_LIST);
2350 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2356 struct nbd_device *nbd;
2357 nbd = idr_find(&nbd_index_idr, index);
2359 ret = populate_nbd_status(nbd, reply);
2366 nla_nest_end(reply, dev_list);
2367 genlmsg_end(reply, reply_head);
2368 ret = genlmsg_reply(reply, info);
2370 mutex_unlock(&nbd_index_mutex);
2374 static void nbd_connect_reply(struct genl_info *info, int index)
2376 struct sk_buff *skb;
2380 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2383 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2389 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2394 genlmsg_end(skb, msg_head);
2395 genlmsg_reply(skb, info);
2398 static void nbd_mcast_index(int index)
2400 struct sk_buff *skb;
2404 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2407 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2413 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2418 genlmsg_end(skb, msg_head);
2419 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2422 static void nbd_dead_link_work(struct work_struct *work)
2424 struct link_dead_args *args = container_of(work, struct link_dead_args,
2426 nbd_mcast_index(args->index);
2430 static int __init nbd_init(void)
2434 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
2437 printk(KERN_ERR "nbd: max_part must be >= 0\n");
2443 part_shift = fls(max_part);
2446 * Adjust max_part according to part_shift as it is exported
2447 * to user space so that user can know the max number of
2448 * partition kernel should be able to manage.
2450 * Note that -1 is required because partition 0 is reserved
2451 * for the whole disk.
2453 max_part = (1UL << part_shift) - 1;
2456 if ((1UL << part_shift) > DISK_MAX_PARTS)
2459 if (nbds_max > 1UL << (MINORBITS - part_shift))
2462 if (register_blkdev(NBD_MAJOR, "nbd"))
2465 nbd_del_wq = alloc_workqueue("nbd-del", WQ_UNBOUND, 0);
2467 unregister_blkdev(NBD_MAJOR, "nbd");
2471 if (genl_register_family(&nbd_genl_family)) {
2472 destroy_workqueue(nbd_del_wq);
2473 unregister_blkdev(NBD_MAJOR, "nbd");
2478 for (i = 0; i < nbds_max; i++)
2483 static int nbd_exit_cb(int id, void *ptr, void *data)
2485 struct list_head *list = (struct list_head *)data;
2486 struct nbd_device *nbd = ptr;
2488 /* Skip nbd that is being removed asynchronously */
2489 if (refcount_read(&nbd->refs))
2490 list_add_tail(&nbd->list, list);
2495 static void __exit nbd_cleanup(void)
2497 struct nbd_device *nbd;
2498 LIST_HEAD(del_list);
2502 mutex_lock(&nbd_index_mutex);
2503 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2504 mutex_unlock(&nbd_index_mutex);
2506 while (!list_empty(&del_list)) {
2507 nbd = list_first_entry(&del_list, struct nbd_device, list);
2508 list_del_init(&nbd->list);
2509 if (refcount_read(&nbd->refs) != 1)
2510 printk(KERN_ERR "nbd: possibly leaking a device\n");
2514 /* Also wait for nbd_dev_remove_work() completes */
2515 destroy_workqueue(nbd_del_wq);
2517 idr_destroy(&nbd_index_idr);
2518 genl_unregister_family(&nbd_genl_family);
2519 unregister_blkdev(NBD_MAJOR, "nbd");
2522 module_init(nbd_init);
2523 module_exit(nbd_cleanup);
2525 MODULE_DESCRIPTION("Network Block Device");
2526 MODULE_LICENSE("GPL");
2528 module_param(nbds_max, int, 0444);
2529 MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2530 module_param(max_part, int, 0444);
2531 MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");