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_setup;
128 pid_t pid; /* pid of nbd-client, if attached */
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", nbd->pid);
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);
263 destroy_workqueue(nbd->recv_workq);
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 int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
323 blksize = 1u << NBD_DEF_BLKSIZE_BITS;
325 if (blk_validate_block_size(blksize))
328 nbd->config->bytesize = bytesize;
329 nbd->config->blksize_bits = __ffs(blksize);
334 if (nbd->config->flags & NBD_FLAG_SEND_TRIM) {
335 nbd->disk->queue->limits.discard_granularity = blksize;
336 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
338 blk_queue_logical_block_size(nbd->disk->queue, blksize);
339 blk_queue_physical_block_size(nbd->disk->queue, blksize);
342 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
343 if (!set_capacity_and_notify(nbd->disk, bytesize >> 9))
344 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
348 static void nbd_complete_rq(struct request *req)
350 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
352 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", req,
353 cmd->status ? "failed" : "done");
355 blk_mq_end_request(req, cmd->status);
359 * Forcibly shutdown the socket causing all listeners to error
361 static void sock_shutdown(struct nbd_device *nbd)
363 struct nbd_config *config = nbd->config;
366 if (config->num_connections == 0)
368 if (test_and_set_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
371 for (i = 0; i < config->num_connections; i++) {
372 struct nbd_sock *nsock = config->socks[i];
373 mutex_lock(&nsock->tx_lock);
374 nbd_mark_nsock_dead(nbd, nsock, 0);
375 mutex_unlock(&nsock->tx_lock);
377 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
380 static u32 req_to_nbd_cmd_type(struct request *req)
382 switch (req_op(req)) {
386 return NBD_CMD_FLUSH;
388 return NBD_CMD_WRITE;
396 static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
399 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
400 struct nbd_device *nbd = cmd->nbd;
401 struct nbd_config *config;
403 if (!mutex_trylock(&cmd->lock))
404 return BLK_EH_RESET_TIMER;
406 if (!test_bit(NBD_CMD_INFLIGHT, &cmd->flags)) {
407 mutex_unlock(&cmd->lock);
411 if (!refcount_inc_not_zero(&nbd->config_refs)) {
412 cmd->status = BLK_STS_TIMEOUT;
413 __clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
414 mutex_unlock(&cmd->lock);
417 config = nbd->config;
419 if (config->num_connections > 1 ||
420 (config->num_connections == 1 && nbd->tag_set.timeout)) {
421 dev_err_ratelimited(nbd_to_dev(nbd),
422 "Connection timed out, retrying (%d/%d alive)\n",
423 atomic_read(&config->live_connections),
424 config->num_connections);
426 * Hooray we have more connections, requeue this IO, the submit
427 * path will put it on a real connection. Or if only one
428 * connection is configured, the submit path will wait util
429 * a new connection is reconfigured or util dead timeout.
432 if (cmd->index < config->num_connections) {
433 struct nbd_sock *nsock =
434 config->socks[cmd->index];
435 mutex_lock(&nsock->tx_lock);
436 /* We can have multiple outstanding requests, so
437 * we don't want to mark the nsock dead if we've
438 * already reconnected with a new socket, so
439 * only mark it dead if its the same socket we
442 if (cmd->cookie == nsock->cookie)
443 nbd_mark_nsock_dead(nbd, nsock, 1);
444 mutex_unlock(&nsock->tx_lock);
446 mutex_unlock(&cmd->lock);
447 nbd_requeue_cmd(cmd);
453 if (!nbd->tag_set.timeout) {
455 * Userspace sets timeout=0 to disable socket disconnection,
456 * so just warn and reset the timer.
458 struct nbd_sock *nsock = config->socks[cmd->index];
460 dev_info(nbd_to_dev(nbd), "Possible stuck request %p: control (%s@%llu,%uB). Runtime %u seconds\n",
461 req, nbdcmd_to_ascii(req_to_nbd_cmd_type(req)),
462 (unsigned long long)blk_rq_pos(req) << 9,
463 blk_rq_bytes(req), (req->timeout / HZ) * cmd->retries);
465 mutex_lock(&nsock->tx_lock);
466 if (cmd->cookie != nsock->cookie) {
467 nbd_requeue_cmd(cmd);
468 mutex_unlock(&nsock->tx_lock);
469 mutex_unlock(&cmd->lock);
473 mutex_unlock(&nsock->tx_lock);
474 mutex_unlock(&cmd->lock);
476 return BLK_EH_RESET_TIMER;
479 dev_err_ratelimited(nbd_to_dev(nbd), "Connection timed out\n");
480 set_bit(NBD_RT_TIMEDOUT, &config->runtime_flags);
481 cmd->status = BLK_STS_IOERR;
482 __clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
483 mutex_unlock(&cmd->lock);
487 blk_mq_complete_request(req);
492 * Send or receive packet. Return a positive value on success and
493 * negtive value on failue, and never return 0.
495 static int sock_xmit(struct nbd_device *nbd, int index, int send,
496 struct iov_iter *iter, int msg_flags, int *sent)
498 struct nbd_config *config = nbd->config;
499 struct socket *sock = config->socks[index]->sock;
502 unsigned int noreclaim_flag;
504 if (unlikely(!sock)) {
505 dev_err_ratelimited(disk_to_dev(nbd->disk),
506 "Attempted %s on closed socket in sock_xmit\n",
507 (send ? "send" : "recv"));
511 msg.msg_iter = *iter;
513 noreclaim_flag = memalloc_noreclaim_save();
515 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
518 msg.msg_control = NULL;
519 msg.msg_controllen = 0;
520 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
523 result = sock_sendmsg(sock, &msg);
525 result = sock_recvmsg(sock, &msg, msg.msg_flags);
529 result = -EPIPE; /* short read */
534 } while (msg_data_left(&msg));
536 memalloc_noreclaim_restore(noreclaim_flag);
542 * Different settings for sk->sk_sndtimeo can result in different return values
543 * if there is a signal pending when we enter sendmsg, because reasons?
545 static inline int was_interrupted(int result)
547 return result == -ERESTARTSYS || result == -EINTR;
550 /* always call with the tx_lock held */
551 static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
553 struct request *req = blk_mq_rq_from_pdu(cmd);
554 struct nbd_config *config = nbd->config;
555 struct nbd_sock *nsock = config->socks[index];
557 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
558 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
559 struct iov_iter from;
560 unsigned long size = blk_rq_bytes(req);
564 u32 nbd_cmd_flags = 0;
565 int sent = nsock->sent, skip = 0;
567 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
569 type = req_to_nbd_cmd_type(req);
573 if (rq_data_dir(req) == WRITE &&
574 (config->flags & NBD_FLAG_READ_ONLY)) {
575 dev_err_ratelimited(disk_to_dev(nbd->disk),
576 "Write on read-only\n");
580 if (req->cmd_flags & REQ_FUA)
581 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
583 /* We did a partial send previously, and we at least sent the whole
584 * request struct, so just go and send the rest of the pages in the
588 if (sent >= sizeof(request)) {
589 skip = sent - sizeof(request);
591 /* initialize handle for tracing purposes */
592 handle = nbd_cmd_handle(cmd);
596 iov_iter_advance(&from, sent);
601 cmd->cookie = nsock->cookie;
603 request.type = htonl(type | nbd_cmd_flags);
604 if (type != NBD_CMD_FLUSH) {
605 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
606 request.len = htonl(size);
608 handle = nbd_cmd_handle(cmd);
609 memcpy(request.handle, &handle, sizeof(handle));
611 trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
613 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
614 req, nbdcmd_to_ascii(type),
615 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
616 result = sock_xmit(nbd, index, 1, &from,
617 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
618 trace_nbd_header_sent(req, handle);
620 if (was_interrupted(result)) {
621 /* If we havne't sent anything we can just return BUSY,
622 * however if we have sent something we need to make
623 * sure we only allow this req to be sent until we are
627 nsock->pending = req;
630 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
631 return BLK_STS_RESOURCE;
633 dev_err_ratelimited(disk_to_dev(nbd->disk),
634 "Send control failed (result %d)\n", result);
638 if (type != NBD_CMD_WRITE)
643 struct bio *next = bio->bi_next;
644 struct bvec_iter iter;
647 bio_for_each_segment(bvec, bio, iter) {
648 bool is_last = !next && bio_iter_last(bvec, iter);
649 int flags = is_last ? 0 : MSG_MORE;
651 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
653 iov_iter_bvec(&from, WRITE, &bvec, 1, bvec.bv_len);
655 if (skip >= iov_iter_count(&from)) {
656 skip -= iov_iter_count(&from);
659 iov_iter_advance(&from, skip);
662 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
664 if (was_interrupted(result)) {
665 /* We've already sent the header, we
666 * have no choice but to set pending and
669 nsock->pending = req;
671 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
672 return BLK_STS_RESOURCE;
674 dev_err(disk_to_dev(nbd->disk),
675 "Send data failed (result %d)\n",
680 * The completion might already have come in,
681 * so break for the last one instead of letting
682 * the iterator do it. This prevents use-after-free
691 trace_nbd_payload_sent(req, handle);
692 nsock->pending = NULL;
697 static int nbd_read_reply(struct nbd_device *nbd, int index,
698 struct nbd_reply *reply)
700 struct kvec iov = {.iov_base = reply, .iov_len = sizeof(*reply)};
705 iov_iter_kvec(&to, READ, &iov, 1, sizeof(*reply));
706 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
708 if (!nbd_disconnected(nbd->config))
709 dev_err(disk_to_dev(nbd->disk),
710 "Receive control failed (result %d)\n", result);
714 if (ntohl(reply->magic) != NBD_REPLY_MAGIC) {
715 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
716 (unsigned long)ntohl(reply->magic));
723 /* NULL returned = something went wrong, inform userspace */
724 static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index,
725 struct nbd_reply *reply)
729 struct request *req = NULL;
735 memcpy(&handle, reply->handle, sizeof(handle));
736 tag = nbd_handle_to_tag(handle);
737 hwq = blk_mq_unique_tag_to_hwq(tag);
738 if (hwq < nbd->tag_set.nr_hw_queues)
739 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
740 blk_mq_unique_tag_to_tag(tag));
741 if (!req || !blk_mq_request_started(req)) {
742 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
744 return ERR_PTR(-ENOENT);
746 trace_nbd_header_received(req, handle);
747 cmd = blk_mq_rq_to_pdu(req);
749 mutex_lock(&cmd->lock);
750 if (!test_bit(NBD_CMD_INFLIGHT, &cmd->flags)) {
751 dev_err(disk_to_dev(nbd->disk), "Suspicious reply %d (status %u flags %lu)",
752 tag, cmd->status, cmd->flags);
756 if (cmd->index != index) {
757 dev_err(disk_to_dev(nbd->disk), "Unexpected reply %d from different sock %d (expected %d)",
758 tag, index, cmd->index);
762 if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
763 dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
764 req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
768 if (cmd->status != BLK_STS_OK) {
769 dev_err(disk_to_dev(nbd->disk), "Command already handled %p\n",
774 if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) {
775 dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n",
780 if (ntohl(reply->error)) {
781 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
782 ntohl(reply->error));
783 cmd->status = BLK_STS_IOERR;
787 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
788 if (rq_data_dir(req) != WRITE) {
789 struct req_iterator iter;
793 rq_for_each_segment(bvec, req, iter) {
794 iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len);
795 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
797 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
800 * If we've disconnected, we need to make sure we
801 * complete this request, otherwise error out
802 * and let the timeout stuff handle resubmitting
803 * this request onto another connection.
805 if (nbd_disconnected(nbd->config)) {
806 cmd->status = BLK_STS_IOERR;
812 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
817 trace_nbd_payload_received(req, handle);
818 mutex_unlock(&cmd->lock);
819 return ret ? ERR_PTR(ret) : cmd;
822 static void recv_work(struct work_struct *work)
824 struct recv_thread_args *args = container_of(work,
825 struct recv_thread_args,
827 struct nbd_device *nbd = args->nbd;
828 struct nbd_config *config = nbd->config;
829 struct request_queue *q = nbd->disk->queue;
830 struct nbd_sock *nsock;
835 struct nbd_reply reply;
837 if (nbd_read_reply(nbd, args->index, &reply))
841 * Grab .q_usage_counter so request pool won't go away, then no
842 * request use-after-free is possible during nbd_handle_reply().
843 * If queue is frozen, there won't be any inflight requests, we
844 * needn't to handle the incoming garbage message.
846 if (!percpu_ref_tryget(&q->q_usage_counter)) {
847 dev_err(disk_to_dev(nbd->disk), "%s: no io inflight\n",
852 cmd = nbd_handle_reply(nbd, args->index, &reply);
854 percpu_ref_put(&q->q_usage_counter);
858 rq = blk_mq_rq_from_pdu(cmd);
859 if (likely(!blk_should_fake_timeout(rq->q))) {
862 mutex_lock(&cmd->lock);
863 complete = __test_and_clear_bit(NBD_CMD_INFLIGHT,
865 mutex_unlock(&cmd->lock);
867 blk_mq_complete_request(rq);
869 percpu_ref_put(&q->q_usage_counter);
872 nsock = config->socks[args->index];
873 mutex_lock(&nsock->tx_lock);
874 nbd_mark_nsock_dead(nbd, nsock, 1);
875 mutex_unlock(&nsock->tx_lock);
878 atomic_dec(&config->recv_threads);
879 wake_up(&config->recv_wq);
883 static bool nbd_clear_req(struct request *req, void *data, bool reserved)
885 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
887 /* don't abort one completed request */
888 if (blk_mq_request_completed(req))
891 mutex_lock(&cmd->lock);
892 if (!__test_and_clear_bit(NBD_CMD_INFLIGHT, &cmd->flags)) {
893 mutex_unlock(&cmd->lock);
896 cmd->status = BLK_STS_IOERR;
897 mutex_unlock(&cmd->lock);
899 blk_mq_complete_request(req);
903 static void nbd_clear_que(struct nbd_device *nbd)
905 blk_mq_quiesce_queue(nbd->disk->queue);
906 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
907 blk_mq_unquiesce_queue(nbd->disk->queue);
908 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
911 static int find_fallback(struct nbd_device *nbd, int index)
913 struct nbd_config *config = nbd->config;
915 struct nbd_sock *nsock = config->socks[index];
916 int fallback = nsock->fallback_index;
918 if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
921 if (config->num_connections <= 1) {
922 dev_err_ratelimited(disk_to_dev(nbd->disk),
923 "Dead connection, failed to find a fallback\n");
927 if (fallback >= 0 && fallback < config->num_connections &&
928 !config->socks[fallback]->dead)
931 if (nsock->fallback_index < 0 ||
932 nsock->fallback_index >= config->num_connections ||
933 config->socks[nsock->fallback_index]->dead) {
935 for (i = 0; i < config->num_connections; i++) {
938 if (!config->socks[i]->dead) {
943 nsock->fallback_index = new_index;
945 dev_err_ratelimited(disk_to_dev(nbd->disk),
946 "Dead connection, failed to find a fallback\n");
950 new_index = nsock->fallback_index;
954 static int wait_for_reconnect(struct nbd_device *nbd)
956 struct nbd_config *config = nbd->config;
957 if (!config->dead_conn_timeout)
960 if (!wait_event_timeout(config->conn_wait,
961 test_bit(NBD_RT_DISCONNECTED,
962 &config->runtime_flags) ||
963 atomic_read(&config->live_connections) > 0,
964 config->dead_conn_timeout))
967 return !test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags);
970 static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
972 struct request *req = blk_mq_rq_from_pdu(cmd);
973 struct nbd_device *nbd = cmd->nbd;
974 struct nbd_config *config;
975 struct nbd_sock *nsock;
978 if (!refcount_inc_not_zero(&nbd->config_refs)) {
979 dev_err_ratelimited(disk_to_dev(nbd->disk),
980 "Socks array is empty\n");
983 config = nbd->config;
985 if (index >= config->num_connections) {
986 dev_err_ratelimited(disk_to_dev(nbd->disk),
987 "Attempted send on invalid socket\n");
991 cmd->status = BLK_STS_OK;
993 nsock = config->socks[index];
994 mutex_lock(&nsock->tx_lock);
996 int old_index = index;
997 index = find_fallback(nbd, index);
998 mutex_unlock(&nsock->tx_lock);
1000 if (wait_for_reconnect(nbd)) {
1004 /* All the sockets should already be down at this point,
1005 * we just want to make sure that DISCONNECTED is set so
1006 * any requests that come in that were queue'ed waiting
1007 * for the reconnect timer don't trigger the timer again
1008 * and instead just error out.
1011 nbd_config_put(nbd);
1017 /* Handle the case that we have a pending request that was partially
1018 * transmitted that _has_ to be serviced first. We need to call requeue
1019 * here so that it gets put _after_ the request that is already on the
1022 blk_mq_start_request(req);
1023 if (unlikely(nsock->pending && nsock->pending != req)) {
1024 nbd_requeue_cmd(cmd);
1029 * Some failures are related to the link going down, so anything that
1030 * returns EAGAIN can be retried on a different socket.
1032 ret = nbd_send_cmd(nbd, cmd, index);
1034 * Access to this flag is protected by cmd->lock, thus it's safe to set
1035 * the flag after nbd_send_cmd() succeed to send request to server.
1038 __set_bit(NBD_CMD_INFLIGHT, &cmd->flags);
1039 else if (ret == -EAGAIN) {
1040 dev_err_ratelimited(disk_to_dev(nbd->disk),
1041 "Request send failed, requeueing\n");
1042 nbd_mark_nsock_dead(nbd, nsock, 1);
1043 nbd_requeue_cmd(cmd);
1047 mutex_unlock(&nsock->tx_lock);
1048 nbd_config_put(nbd);
1052 static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
1053 const struct blk_mq_queue_data *bd)
1055 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
1059 * Since we look at the bio's to send the request over the network we
1060 * need to make sure the completion work doesn't mark this request done
1061 * before we are done doing our send. This keeps us from dereferencing
1062 * freed data if we have particularly fast completions (ie we get the
1063 * completion before we exit sock_xmit on the last bvec) or in the case
1064 * that the server is misbehaving (or there was an error) before we're
1065 * done sending everything over the wire.
1067 mutex_lock(&cmd->lock);
1068 clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
1070 /* We can be called directly from the user space process, which means we
1071 * could possibly have signals pending so our sendmsg will fail. In
1072 * this case we need to return that we are busy, otherwise error out as
1075 ret = nbd_handle_cmd(cmd, hctx->queue_num);
1077 ret = BLK_STS_IOERR;
1080 mutex_unlock(&cmd->lock);
1085 static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
1088 struct socket *sock;
1091 sock = sockfd_lookup(fd, err);
1095 if (sock->ops->shutdown == sock_no_shutdown) {
1096 dev_err(disk_to_dev(nbd->disk), "Unsupported socket: shutdown callout must be supported.\n");
1105 static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
1108 struct nbd_config *config = nbd->config;
1109 struct socket *sock;
1110 struct nbd_sock **socks;
1111 struct nbd_sock *nsock;
1114 sock = nbd_get_socket(nbd, arg, &err);
1119 * We need to make sure we don't get any errant requests while we're
1120 * reallocating the ->socks array.
1122 blk_mq_freeze_queue(nbd->disk->queue);
1124 if (!netlink && !nbd->task_setup &&
1125 !test_bit(NBD_RT_BOUND, &config->runtime_flags))
1126 nbd->task_setup = current;
1129 (nbd->task_setup != current ||
1130 test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
1131 dev_err(disk_to_dev(nbd->disk),
1132 "Device being setup by another task");
1137 nsock = kzalloc(sizeof(*nsock), GFP_KERNEL);
1143 socks = krealloc(config->socks, (config->num_connections + 1) *
1144 sizeof(struct nbd_sock *), GFP_KERNEL);
1151 config->socks = socks;
1153 nsock->fallback_index = -1;
1154 nsock->dead = false;
1155 mutex_init(&nsock->tx_lock);
1157 nsock->pending = NULL;
1160 socks[config->num_connections++] = nsock;
1161 atomic_inc(&config->live_connections);
1162 blk_mq_unfreeze_queue(nbd->disk->queue);
1167 blk_mq_unfreeze_queue(nbd->disk->queue);
1172 static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
1174 struct nbd_config *config = nbd->config;
1175 struct socket *sock, *old;
1176 struct recv_thread_args *args;
1180 sock = nbd_get_socket(nbd, arg, &err);
1184 args = kzalloc(sizeof(*args), GFP_KERNEL);
1190 for (i = 0; i < config->num_connections; i++) {
1191 struct nbd_sock *nsock = config->socks[i];
1196 mutex_lock(&nsock->tx_lock);
1198 mutex_unlock(&nsock->tx_lock);
1201 sk_set_memalloc(sock->sk);
1202 if (nbd->tag_set.timeout)
1203 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
1204 atomic_inc(&config->recv_threads);
1205 refcount_inc(&nbd->config_refs);
1207 nsock->fallback_index = -1;
1209 nsock->dead = false;
1210 INIT_WORK(&args->work, recv_work);
1214 mutex_unlock(&nsock->tx_lock);
1217 clear_bit(NBD_RT_DISCONNECTED, &config->runtime_flags);
1219 /* We take the tx_mutex in an error path in the recv_work, so we
1220 * need to queue_work outside of the tx_mutex.
1222 queue_work(nbd->recv_workq, &args->work);
1224 atomic_inc(&config->live_connections);
1225 wake_up(&config->conn_wait);
1233 static void nbd_bdev_reset(struct nbd_device *nbd)
1235 if (disk_openers(nbd->disk) > 1)
1237 set_capacity(nbd->disk, 0);
1240 static void nbd_parse_flags(struct nbd_device *nbd)
1242 struct nbd_config *config = nbd->config;
1243 if (config->flags & NBD_FLAG_READ_ONLY)
1244 set_disk_ro(nbd->disk, true);
1246 set_disk_ro(nbd->disk, false);
1247 if (config->flags & NBD_FLAG_SEND_FLUSH) {
1248 if (config->flags & NBD_FLAG_SEND_FUA)
1249 blk_queue_write_cache(nbd->disk->queue, true, true);
1251 blk_queue_write_cache(nbd->disk->queue, true, false);
1254 blk_queue_write_cache(nbd->disk->queue, false, false);
1257 static void send_disconnects(struct nbd_device *nbd)
1259 struct nbd_config *config = nbd->config;
1260 struct nbd_request request = {
1261 .magic = htonl(NBD_REQUEST_MAGIC),
1262 .type = htonl(NBD_CMD_DISC),
1264 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1265 struct iov_iter from;
1268 for (i = 0; i < config->num_connections; i++) {
1269 struct nbd_sock *nsock = config->socks[i];
1271 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
1272 mutex_lock(&nsock->tx_lock);
1273 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
1275 dev_err(disk_to_dev(nbd->disk),
1276 "Send disconnect failed %d\n", ret);
1277 mutex_unlock(&nsock->tx_lock);
1281 static int nbd_disconnect(struct nbd_device *nbd)
1283 struct nbd_config *config = nbd->config;
1285 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
1286 set_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
1287 set_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags);
1288 send_disconnects(nbd);
1292 static void nbd_clear_sock(struct nbd_device *nbd)
1296 nbd->task_setup = NULL;
1299 static void nbd_config_put(struct nbd_device *nbd)
1301 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1302 &nbd->config_lock)) {
1303 struct nbd_config *config = nbd->config;
1304 nbd_dev_dbg_close(nbd);
1305 invalidate_disk(nbd->disk);
1306 if (nbd->config->bytesize)
1307 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
1308 if (test_and_clear_bit(NBD_RT_HAS_PID_FILE,
1309 &config->runtime_flags))
1310 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1312 if (test_and_clear_bit(NBD_RT_HAS_BACKEND_FILE,
1313 &config->runtime_flags)) {
1314 device_remove_file(disk_to_dev(nbd->disk), &backend_attr);
1315 kfree(nbd->backend);
1316 nbd->backend = NULL;
1318 nbd_clear_sock(nbd);
1319 if (config->num_connections) {
1321 for (i = 0; i < config->num_connections; i++) {
1322 sockfd_put(config->socks[i]->sock);
1323 kfree(config->socks[i]);
1325 kfree(config->socks);
1330 nbd->tag_set.timeout = 0;
1331 nbd->disk->queue->limits.discard_granularity = 0;
1332 blk_queue_max_discard_sectors(nbd->disk->queue, 0);
1334 mutex_unlock(&nbd->config_lock);
1336 module_put(THIS_MODULE);
1340 static int nbd_start_device(struct nbd_device *nbd)
1342 struct nbd_config *config = nbd->config;
1343 int num_connections = config->num_connections;
1350 if (num_connections > 1 &&
1351 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
1352 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
1356 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
1357 nbd->pid = task_pid_nr(current);
1359 nbd_parse_flags(nbd);
1361 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1363 dev_err(disk_to_dev(nbd->disk), "device_create_file failed for pid!\n");
1366 set_bit(NBD_RT_HAS_PID_FILE, &config->runtime_flags);
1368 nbd_dev_dbg_init(nbd);
1369 for (i = 0; i < num_connections; i++) {
1370 struct recv_thread_args *args;
1372 args = kzalloc(sizeof(*args), GFP_KERNEL);
1376 * If num_connections is m (2 < m),
1377 * and NO.1 ~ NO.n(1 < n < m) kzallocs are successful.
1378 * But NO.(n + 1) failed. We still have n recv threads.
1379 * So, add flush_workqueue here to prevent recv threads
1380 * dropping the last config_refs and trying to destroy
1381 * the workqueue from inside the workqueue.
1384 flush_workqueue(nbd->recv_workq);
1387 sk_set_memalloc(config->socks[i]->sock->sk);
1388 if (nbd->tag_set.timeout)
1389 config->socks[i]->sock->sk->sk_sndtimeo =
1390 nbd->tag_set.timeout;
1391 atomic_inc(&config->recv_threads);
1392 refcount_inc(&nbd->config_refs);
1393 INIT_WORK(&args->work, recv_work);
1396 queue_work(nbd->recv_workq, &args->work);
1398 return nbd_set_size(nbd, config->bytesize, nbd_blksize(config));
1401 static int nbd_start_device_ioctl(struct nbd_device *nbd)
1403 struct nbd_config *config = nbd->config;
1406 ret = nbd_start_device(nbd);
1411 set_bit(GD_NEED_PART_SCAN, &nbd->disk->state);
1412 mutex_unlock(&nbd->config_lock);
1413 ret = wait_event_interruptible(config->recv_wq,
1414 atomic_read(&config->recv_threads) == 0);
1417 flush_workqueue(nbd->recv_workq);
1419 mutex_lock(&nbd->config_lock);
1420 nbd_bdev_reset(nbd);
1421 /* user requested, ignore socket errors */
1422 if (test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags))
1424 if (test_bit(NBD_RT_TIMEDOUT, &config->runtime_flags))
1429 static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1430 struct block_device *bdev)
1432 nbd_clear_sock(nbd);
1433 __invalidate_device(bdev, true);
1434 nbd_bdev_reset(nbd);
1435 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
1436 &nbd->config->runtime_flags))
1437 nbd_config_put(nbd);
1440 static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout)
1442 nbd->tag_set.timeout = timeout * HZ;
1444 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1446 blk_queue_rq_timeout(nbd->disk->queue, 30 * HZ);
1449 /* Must be called with config_lock held */
1450 static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
1451 unsigned int cmd, unsigned long arg)
1453 struct nbd_config *config = nbd->config;
1457 case NBD_DISCONNECT:
1458 return nbd_disconnect(nbd);
1459 case NBD_CLEAR_SOCK:
1460 nbd_clear_sock_ioctl(nbd, bdev);
1463 return nbd_add_socket(nbd, arg, false);
1464 case NBD_SET_BLKSIZE:
1465 return nbd_set_size(nbd, config->bytesize, arg);
1467 return nbd_set_size(nbd, arg, nbd_blksize(config));
1468 case NBD_SET_SIZE_BLOCKS:
1469 if (check_shl_overflow(arg, config->blksize_bits, &bytesize))
1471 return nbd_set_size(nbd, bytesize, nbd_blksize(config));
1472 case NBD_SET_TIMEOUT:
1473 nbd_set_cmd_timeout(nbd, arg);
1477 config->flags = arg;
1480 return nbd_start_device_ioctl(nbd);
1483 * This is for compatibility only. The queue is always cleared
1484 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1487 case NBD_PRINT_DEBUG:
1489 * For compatibility only, we no longer keep a list of
1490 * outstanding requests.
1497 static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1498 unsigned int cmd, unsigned long arg)
1500 struct nbd_device *nbd = bdev->bd_disk->private_data;
1501 struct nbd_config *config = nbd->config;
1502 int error = -EINVAL;
1504 if (!capable(CAP_SYS_ADMIN))
1507 /* The block layer will pass back some non-nbd ioctls in case we have
1508 * special handling for them, but we don't so just return an error.
1510 if (_IOC_TYPE(cmd) != 0xab)
1513 mutex_lock(&nbd->config_lock);
1515 /* Don't allow ioctl operations on a nbd device that was created with
1516 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1518 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
1519 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1520 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1522 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
1523 mutex_unlock(&nbd->config_lock);
1527 static struct nbd_config *nbd_alloc_config(void)
1529 struct nbd_config *config;
1531 if (!try_module_get(THIS_MODULE))
1532 return ERR_PTR(-ENODEV);
1534 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1536 module_put(THIS_MODULE);
1537 return ERR_PTR(-ENOMEM);
1540 atomic_set(&config->recv_threads, 0);
1541 init_waitqueue_head(&config->recv_wq);
1542 init_waitqueue_head(&config->conn_wait);
1543 config->blksize_bits = NBD_DEF_BLKSIZE_BITS;
1544 atomic_set(&config->live_connections, 0);
1548 static int nbd_open(struct block_device *bdev, fmode_t mode)
1550 struct nbd_device *nbd;
1553 mutex_lock(&nbd_index_mutex);
1554 nbd = bdev->bd_disk->private_data;
1559 if (!refcount_inc_not_zero(&nbd->refs)) {
1563 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1564 struct nbd_config *config;
1566 mutex_lock(&nbd->config_lock);
1567 if (refcount_inc_not_zero(&nbd->config_refs)) {
1568 mutex_unlock(&nbd->config_lock);
1571 config = nbd_alloc_config();
1572 if (IS_ERR(config)) {
1573 ret = PTR_ERR(config);
1574 mutex_unlock(&nbd->config_lock);
1577 nbd->config = config;
1578 refcount_set(&nbd->config_refs, 1);
1579 refcount_inc(&nbd->refs);
1580 mutex_unlock(&nbd->config_lock);
1582 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1583 } else if (nbd_disconnected(nbd->config)) {
1585 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1588 mutex_unlock(&nbd_index_mutex);
1592 static void nbd_release(struct gendisk *disk, fmode_t mode)
1594 struct nbd_device *nbd = disk->private_data;
1596 if (test_bit(NBD_RT_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) &&
1597 disk_openers(disk) == 0)
1598 nbd_disconnect_and_put(nbd);
1600 nbd_config_put(nbd);
1604 static const struct block_device_operations nbd_fops =
1606 .owner = THIS_MODULE,
1608 .release = nbd_release,
1610 .compat_ioctl = nbd_ioctl,
1613 #if IS_ENABLED(CONFIG_DEBUG_FS)
1615 static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1617 struct nbd_device *nbd = s->private;
1620 seq_printf(s, "recv: %d\n", nbd->pid);
1625 DEFINE_SHOW_ATTRIBUTE(nbd_dbg_tasks);
1627 static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1629 struct nbd_device *nbd = s->private;
1630 u32 flags = nbd->config->flags;
1632 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1634 seq_puts(s, "Known flags:\n");
1636 if (flags & NBD_FLAG_HAS_FLAGS)
1637 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1638 if (flags & NBD_FLAG_READ_ONLY)
1639 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1640 if (flags & NBD_FLAG_SEND_FLUSH)
1641 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
1642 if (flags & NBD_FLAG_SEND_FUA)
1643 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
1644 if (flags & NBD_FLAG_SEND_TRIM)
1645 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1650 DEFINE_SHOW_ATTRIBUTE(nbd_dbg_flags);
1652 static int nbd_dev_dbg_init(struct nbd_device *nbd)
1655 struct nbd_config *config = nbd->config;
1660 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
1662 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1666 config->dbg_dir = dir;
1668 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_fops);
1669 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
1670 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
1671 debugfs_create_u32("blocksize_bits", 0444, dir, &config->blksize_bits);
1672 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_fops);
1677 static void nbd_dev_dbg_close(struct nbd_device *nbd)
1679 debugfs_remove_recursive(nbd->config->dbg_dir);
1682 static int nbd_dbg_init(void)
1684 struct dentry *dbg_dir;
1686 dbg_dir = debugfs_create_dir("nbd", NULL);
1690 nbd_dbg_dir = dbg_dir;
1695 static void nbd_dbg_close(void)
1697 debugfs_remove_recursive(nbd_dbg_dir);
1700 #else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1702 static int nbd_dev_dbg_init(struct nbd_device *nbd)
1707 static void nbd_dev_dbg_close(struct nbd_device *nbd)
1711 static int nbd_dbg_init(void)
1716 static void nbd_dbg_close(void)
1722 static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1723 unsigned int hctx_idx, unsigned int numa_node)
1725 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
1726 cmd->nbd = set->driver_data;
1728 mutex_init(&cmd->lock);
1732 static const struct blk_mq_ops nbd_mq_ops = {
1733 .queue_rq = nbd_queue_rq,
1734 .complete = nbd_complete_rq,
1735 .init_request = nbd_init_request,
1736 .timeout = nbd_xmit_timeout,
1739 static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
1741 struct nbd_device *nbd;
1742 struct gendisk *disk;
1745 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1749 nbd->tag_set.ops = &nbd_mq_ops;
1750 nbd->tag_set.nr_hw_queues = 1;
1751 nbd->tag_set.queue_depth = 128;
1752 nbd->tag_set.numa_node = NUMA_NO_NODE;
1753 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1754 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1756 nbd->tag_set.driver_data = nbd;
1757 INIT_WORK(&nbd->remove_work, nbd_dev_remove_work);
1758 nbd->backend = NULL;
1760 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1764 mutex_lock(&nbd_index_mutex);
1766 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1771 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1776 mutex_unlock(&nbd_index_mutex);
1780 disk = blk_mq_alloc_disk(&nbd->tag_set, NULL);
1782 err = PTR_ERR(disk);
1787 nbd->recv_workq = alloc_workqueue("nbd%d-recv",
1788 WQ_MEM_RECLAIM | WQ_HIGHPRI |
1789 WQ_UNBOUND, 0, nbd->index);
1790 if (!nbd->recv_workq) {
1791 dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1797 * Tell the block layer that we are not a rotational device
1799 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
1800 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1801 disk->queue->limits.discard_granularity = 0;
1802 blk_queue_max_discard_sectors(disk->queue, 0);
1803 blk_queue_max_segment_size(disk->queue, UINT_MAX);
1804 blk_queue_max_segments(disk->queue, USHRT_MAX);
1805 blk_queue_max_hw_sectors(disk->queue, 65536);
1806 disk->queue->limits.max_sectors = 256;
1808 mutex_init(&nbd->config_lock);
1809 refcount_set(&nbd->config_refs, 0);
1811 * Start out with a zero references to keep other threads from using
1812 * this device until it is fully initialized.
1814 refcount_set(&nbd->refs, 0);
1815 INIT_LIST_HEAD(&nbd->list);
1816 disk->major = NBD_MAJOR;
1817 disk->first_minor = index << part_shift;
1818 disk->minors = 1 << part_shift;
1819 disk->fops = &nbd_fops;
1820 disk->private_data = nbd;
1821 sprintf(disk->disk_name, "nbd%d", index);
1822 err = add_disk(disk);
1827 * Now publish the device.
1829 refcount_set(&nbd->refs, refs);
1830 nbd_total_devices++;
1834 destroy_workqueue(nbd->recv_workq);
1836 blk_cleanup_disk(disk);
1838 mutex_lock(&nbd_index_mutex);
1839 idr_remove(&nbd_index_idr, index);
1840 mutex_unlock(&nbd_index_mutex);
1842 blk_mq_free_tag_set(&nbd->tag_set);
1846 return ERR_PTR(err);
1849 static struct nbd_device *nbd_find_get_unused(void)
1851 struct nbd_device *nbd;
1854 lockdep_assert_held(&nbd_index_mutex);
1856 idr_for_each_entry(&nbd_index_idr, nbd, id) {
1857 if (refcount_read(&nbd->config_refs) ||
1858 test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags))
1860 if (refcount_inc_not_zero(&nbd->refs))
1867 /* Netlink interface. */
1868 static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
1869 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1870 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1871 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1872 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1873 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1874 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1875 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
1876 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
1877 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
1878 [NBD_ATTR_BACKEND_IDENTIFIER] = { .type = NLA_STRING},
1881 static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
1882 [NBD_SOCK_FD] = { .type = NLA_U32 },
1885 /* We don't use this right now since we don't parse the incoming list, but we
1886 * still want it here so userspace knows what to expect.
1888 static const struct nla_policy __attribute__((unused))
1889 nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1890 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1891 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1894 static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
1896 struct nbd_config *config = nbd->config;
1897 u64 bsize = nbd_blksize(config);
1898 u64 bytes = config->bytesize;
1900 if (info->attrs[NBD_ATTR_SIZE_BYTES])
1901 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1903 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES])
1904 bsize = nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1906 if (bytes != config->bytesize || bsize != nbd_blksize(config))
1907 return nbd_set_size(nbd, bytes, bsize);
1911 static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1913 struct nbd_device *nbd;
1914 struct nbd_config *config;
1917 bool put_dev = false;
1919 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1922 if (info->attrs[NBD_ATTR_INDEX]) {
1923 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1926 * Too big first_minor can cause duplicate creation of
1927 * sysfs files/links, since index << part_shift might overflow, or
1928 * MKDEV() expect that the max bits of first_minor is 20.
1930 if (index < 0 || index > MINORMASK >> part_shift) {
1931 pr_err("illegal input index %d\n", index);
1935 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1936 pr_err("must specify at least one socket\n");
1939 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1940 pr_err("must specify a size in bytes for the device\n");
1944 mutex_lock(&nbd_index_mutex);
1946 nbd = nbd_find_get_unused();
1948 nbd = idr_find(&nbd_index_idr, index);
1950 if ((test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
1951 test_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags)) ||
1952 !refcount_inc_not_zero(&nbd->refs)) {
1953 mutex_unlock(&nbd_index_mutex);
1954 pr_err("nbd: device at index %d is going down\n",
1960 mutex_unlock(&nbd_index_mutex);
1963 nbd = nbd_dev_add(index, 2);
1965 pr_err("nbd: failed to add new device\n");
1966 return PTR_ERR(nbd);
1970 mutex_lock(&nbd->config_lock);
1971 if (refcount_read(&nbd->config_refs)) {
1972 mutex_unlock(&nbd->config_lock);
1976 pr_err("nbd%d already in use\n", index);
1979 if (WARN_ON(nbd->config)) {
1980 mutex_unlock(&nbd->config_lock);
1984 config = nbd_alloc_config();
1985 if (IS_ERR(config)) {
1986 mutex_unlock(&nbd->config_lock);
1988 pr_err("couldn't allocate config\n");
1989 return PTR_ERR(config);
1991 nbd->config = config;
1992 refcount_set(&nbd->config_refs, 1);
1993 set_bit(NBD_RT_BOUND, &config->runtime_flags);
1995 ret = nbd_genl_size_set(info, nbd);
1999 if (info->attrs[NBD_ATTR_TIMEOUT])
2000 nbd_set_cmd_timeout(nbd,
2001 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
2002 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
2003 config->dead_conn_timeout =
2004 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
2005 config->dead_conn_timeout *= HZ;
2007 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
2009 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
2010 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
2011 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
2012 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
2014 * We have 1 ref to keep the device around, and then 1
2015 * ref for our current operation here, which will be
2016 * inherited by the config. If we already have
2017 * DESTROY_ON_DISCONNECT set then we know we don't have
2018 * that extra ref already held so we don't need the
2021 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
2025 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
2027 refcount_inc(&nbd->refs);
2029 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
2030 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
2031 &config->runtime_flags);
2035 if (info->attrs[NBD_ATTR_SOCKETS]) {
2036 struct nlattr *attr;
2039 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
2041 struct nlattr *socks[NBD_SOCK_MAX+1];
2043 if (nla_type(attr) != NBD_SOCK_ITEM) {
2044 pr_err("socks must be embedded in a SOCK_ITEM attr\n");
2048 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
2053 pr_err("error processing sock list\n");
2057 if (!socks[NBD_SOCK_FD])
2059 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2060 ret = nbd_add_socket(nbd, fd, true);
2065 ret = nbd_start_device(nbd);
2068 if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) {
2069 nbd->backend = nla_strdup(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER],
2071 if (!nbd->backend) {
2076 ret = device_create_file(disk_to_dev(nbd->disk), &backend_attr);
2078 dev_err(disk_to_dev(nbd->disk),
2079 "device_create_file failed for backend!\n");
2082 set_bit(NBD_RT_HAS_BACKEND_FILE, &config->runtime_flags);
2084 mutex_unlock(&nbd->config_lock);
2086 set_bit(NBD_RT_HAS_CONFIG_REF, &config->runtime_flags);
2087 refcount_inc(&nbd->config_refs);
2088 nbd_connect_reply(info, nbd->index);
2090 nbd_config_put(nbd);
2096 static void nbd_disconnect_and_put(struct nbd_device *nbd)
2098 mutex_lock(&nbd->config_lock);
2099 nbd_disconnect(nbd);
2101 wake_up(&nbd->config->conn_wait);
2103 * Make sure recv thread has finished, we can safely call nbd_clear_que()
2104 * to cancel the inflight I/Os.
2106 flush_workqueue(nbd->recv_workq);
2108 nbd->task_setup = NULL;
2109 mutex_unlock(&nbd->config_lock);
2111 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
2112 &nbd->config->runtime_flags))
2113 nbd_config_put(nbd);
2116 static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
2118 struct nbd_device *nbd;
2121 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2124 if (!info->attrs[NBD_ATTR_INDEX]) {
2125 pr_err("must specify an index to disconnect\n");
2128 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2129 mutex_lock(&nbd_index_mutex);
2130 nbd = idr_find(&nbd_index_idr, index);
2132 mutex_unlock(&nbd_index_mutex);
2133 pr_err("couldn't find device at index %d\n", index);
2136 if (!refcount_inc_not_zero(&nbd->refs)) {
2137 mutex_unlock(&nbd_index_mutex);
2138 pr_err("device at index %d is going down\n", index);
2141 mutex_unlock(&nbd_index_mutex);
2142 if (!refcount_inc_not_zero(&nbd->config_refs))
2144 nbd_disconnect_and_put(nbd);
2145 nbd_config_put(nbd);
2151 static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
2153 struct nbd_device *nbd = NULL;
2154 struct nbd_config *config;
2157 bool put_dev = false;
2159 if (!netlink_capable(skb, CAP_SYS_ADMIN))
2162 if (!info->attrs[NBD_ATTR_INDEX]) {
2163 pr_err("must specify a device to reconfigure\n");
2166 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2167 mutex_lock(&nbd_index_mutex);
2168 nbd = idr_find(&nbd_index_idr, index);
2170 mutex_unlock(&nbd_index_mutex);
2171 pr_err("couldn't find a device at index %d\n", index);
2175 if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) {
2176 if (nla_strcmp(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER],
2178 mutex_unlock(&nbd_index_mutex);
2179 dev_err(nbd_to_dev(nbd),
2180 "backend image doesn't match with %s\n",
2185 mutex_unlock(&nbd_index_mutex);
2186 dev_err(nbd_to_dev(nbd), "must specify backend\n");
2190 if (!refcount_inc_not_zero(&nbd->refs)) {
2191 mutex_unlock(&nbd_index_mutex);
2192 pr_err("device at index %d is going down\n", index);
2195 mutex_unlock(&nbd_index_mutex);
2197 if (!refcount_inc_not_zero(&nbd->config_refs)) {
2198 dev_err(nbd_to_dev(nbd),
2199 "not configured, cannot reconfigure\n");
2204 mutex_lock(&nbd->config_lock);
2205 config = nbd->config;
2206 if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
2208 dev_err(nbd_to_dev(nbd),
2209 "not configured, cannot reconfigure\n");
2214 ret = nbd_genl_size_set(info, nbd);
2218 if (info->attrs[NBD_ATTR_TIMEOUT])
2219 nbd_set_cmd_timeout(nbd,
2220 nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]));
2221 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
2222 config->dead_conn_timeout =
2223 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
2224 config->dead_conn_timeout *= HZ;
2226 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
2227 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
2228 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
2229 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
2233 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
2235 refcount_inc(&nbd->refs);
2238 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
2239 set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
2240 &config->runtime_flags);
2242 clear_bit(NBD_RT_DISCONNECT_ON_CLOSE,
2243 &config->runtime_flags);
2247 if (info->attrs[NBD_ATTR_SOCKETS]) {
2248 struct nlattr *attr;
2251 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
2253 struct nlattr *socks[NBD_SOCK_MAX+1];
2255 if (nla_type(attr) != NBD_SOCK_ITEM) {
2256 pr_err("socks must be embedded in a SOCK_ITEM attr\n");
2260 ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
2265 pr_err("error processing sock list\n");
2269 if (!socks[NBD_SOCK_FD])
2271 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
2272 ret = nbd_reconnect_socket(nbd, fd);
2278 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
2282 mutex_unlock(&nbd->config_lock);
2283 nbd_config_put(nbd);
2290 static const struct genl_small_ops nbd_connect_genl_ops[] = {
2292 .cmd = NBD_CMD_CONNECT,
2293 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2294 .doit = nbd_genl_connect,
2297 .cmd = NBD_CMD_DISCONNECT,
2298 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2299 .doit = nbd_genl_disconnect,
2302 .cmd = NBD_CMD_RECONFIGURE,
2303 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2304 .doit = nbd_genl_reconfigure,
2307 .cmd = NBD_CMD_STATUS,
2308 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2309 .doit = nbd_genl_status,
2313 static const struct genl_multicast_group nbd_mcast_grps[] = {
2314 { .name = NBD_GENL_MCAST_GROUP_NAME, },
2317 static struct genl_family nbd_genl_family __ro_after_init = {
2319 .name = NBD_GENL_FAMILY_NAME,
2320 .version = NBD_GENL_VERSION,
2321 .module = THIS_MODULE,
2322 .small_ops = nbd_connect_genl_ops,
2323 .n_small_ops = ARRAY_SIZE(nbd_connect_genl_ops),
2324 .maxattr = NBD_ATTR_MAX,
2325 .policy = nbd_attr_policy,
2326 .mcgrps = nbd_mcast_grps,
2327 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
2330 static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
2332 struct nlattr *dev_opt;
2336 /* This is a little racey, but for status it's ok. The
2337 * reason we don't take a ref here is because we can't
2338 * take a ref in the index == -1 case as we would need
2339 * to put under the nbd_index_mutex, which could
2340 * deadlock if we are configured to remove ourselves
2341 * once we're disconnected.
2343 if (refcount_read(&nbd->config_refs))
2345 dev_opt = nla_nest_start_noflag(reply, NBD_DEVICE_ITEM);
2348 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
2351 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
2355 nla_nest_end(reply, dev_opt);
2359 static int status_cb(int id, void *ptr, void *data)
2361 struct nbd_device *nbd = ptr;
2362 return populate_nbd_status(nbd, (struct sk_buff *)data);
2365 static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
2367 struct nlattr *dev_list;
2368 struct sk_buff *reply;
2374 if (info->attrs[NBD_ATTR_INDEX])
2375 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2377 mutex_lock(&nbd_index_mutex);
2379 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
2380 nla_attr_size(sizeof(u8)));
2381 msg_size *= (index == -1) ? nbd_total_devices : 1;
2383 reply = genlmsg_new(msg_size, GFP_KERNEL);
2386 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2393 dev_list = nla_nest_start_noflag(reply, NBD_ATTR_DEVICE_LIST);
2395 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2401 struct nbd_device *nbd;
2402 nbd = idr_find(&nbd_index_idr, index);
2404 ret = populate_nbd_status(nbd, reply);
2411 nla_nest_end(reply, dev_list);
2412 genlmsg_end(reply, reply_head);
2413 ret = genlmsg_reply(reply, info);
2415 mutex_unlock(&nbd_index_mutex);
2419 static void nbd_connect_reply(struct genl_info *info, int index)
2421 struct sk_buff *skb;
2425 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2428 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2434 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2439 genlmsg_end(skb, msg_head);
2440 genlmsg_reply(skb, info);
2443 static void nbd_mcast_index(int index)
2445 struct sk_buff *skb;
2449 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2452 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2458 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2463 genlmsg_end(skb, msg_head);
2464 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2467 static void nbd_dead_link_work(struct work_struct *work)
2469 struct link_dead_args *args = container_of(work, struct link_dead_args,
2471 nbd_mcast_index(args->index);
2475 static int __init nbd_init(void)
2479 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
2482 pr_err("max_part must be >= 0\n");
2488 part_shift = fls(max_part);
2491 * Adjust max_part according to part_shift as it is exported
2492 * to user space so that user can know the max number of
2493 * partition kernel should be able to manage.
2495 * Note that -1 is required because partition 0 is reserved
2496 * for the whole disk.
2498 max_part = (1UL << part_shift) - 1;
2501 if ((1UL << part_shift) > DISK_MAX_PARTS)
2504 if (nbds_max > 1UL << (MINORBITS - part_shift))
2507 if (register_blkdev(NBD_MAJOR, "nbd"))
2510 nbd_del_wq = alloc_workqueue("nbd-del", WQ_UNBOUND, 0);
2512 unregister_blkdev(NBD_MAJOR, "nbd");
2516 if (genl_register_family(&nbd_genl_family)) {
2517 destroy_workqueue(nbd_del_wq);
2518 unregister_blkdev(NBD_MAJOR, "nbd");
2523 for (i = 0; i < nbds_max; i++)
2528 static int nbd_exit_cb(int id, void *ptr, void *data)
2530 struct list_head *list = (struct list_head *)data;
2531 struct nbd_device *nbd = ptr;
2533 /* Skip nbd that is being removed asynchronously */
2534 if (refcount_read(&nbd->refs))
2535 list_add_tail(&nbd->list, list);
2540 static void __exit nbd_cleanup(void)
2542 struct nbd_device *nbd;
2543 LIST_HEAD(del_list);
2546 * Unregister netlink interface prior to waiting
2547 * for the completion of netlink commands.
2549 genl_unregister_family(&nbd_genl_family);
2553 mutex_lock(&nbd_index_mutex);
2554 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2555 mutex_unlock(&nbd_index_mutex);
2557 while (!list_empty(&del_list)) {
2558 nbd = list_first_entry(&del_list, struct nbd_device, list);
2559 list_del_init(&nbd->list);
2560 if (refcount_read(&nbd->config_refs))
2561 pr_err("possibly leaking nbd_config (ref %d)\n",
2562 refcount_read(&nbd->config_refs));
2563 if (refcount_read(&nbd->refs) != 1)
2564 pr_err("possibly leaking a device\n");
2568 /* Also wait for nbd_dev_remove_work() completes */
2569 destroy_workqueue(nbd_del_wq);
2571 idr_destroy(&nbd_index_idr);
2572 unregister_blkdev(NBD_MAJOR, "nbd");
2575 module_init(nbd_init);
2576 module_exit(nbd_cleanup);
2578 MODULE_DESCRIPTION("Network Block Device");
2579 MODULE_LICENSE("GPL");
2581 module_param(nbds_max, int, 0444);
2582 MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2583 module_param(max_part, int, 0444);
2584 MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");