2 * Network block device - make block devices work over TCP
4 * Note that you can not swap over this thing, yet. Seems to work but
5 * deadlocks sometimes - you can not swap over TCP in general.
7 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
8 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
10 * This file is released under GPLv2 or later.
12 * (part of code stolen from loop.c)
15 #include <linux/major.h>
17 #include <linux/blkdev.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/sched.h>
21 #include <linux/sched/mm.h>
23 #include <linux/bio.h>
24 #include <linux/stat.h>
25 #include <linux/errno.h>
26 #include <linux/file.h>
27 #include <linux/ioctl.h>
28 #include <linux/mutex.h>
29 #include <linux/compiler.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 static DEFINE_IDR(nbd_index_idr);
48 static DEFINE_MUTEX(nbd_index_mutex);
49 static int nbd_total_devices = 0;
54 struct request *pending;
61 struct recv_thread_args {
62 struct work_struct work;
63 struct nbd_device *nbd;
67 struct link_dead_args {
68 struct work_struct work;
72 #define NBD_TIMEDOUT 0
73 #define NBD_DISCONNECT_REQUESTED 1
74 #define NBD_DISCONNECTED 2
75 #define NBD_HAS_PID_FILE 3
76 #define NBD_HAS_CONFIG_REF 4
78 #define NBD_DESTROY_ON_DISCONNECT 6
79 #define NBD_DISCONNECT_ON_CLOSE 7
83 unsigned long runtime_flags;
84 u64 dead_conn_timeout;
86 struct nbd_sock **socks;
88 atomic_t live_connections;
89 wait_queue_head_t conn_wait;
91 atomic_t recv_threads;
92 wait_queue_head_t recv_wq;
95 #if IS_ENABLED(CONFIG_DEBUG_FS)
96 struct dentry *dbg_dir;
101 struct blk_mq_tag_set tag_set;
104 refcount_t config_refs;
106 struct nbd_config *config;
107 struct mutex config_lock;
108 struct gendisk *disk;
110 struct list_head list;
111 struct task_struct *task_recv;
112 struct task_struct *task_setup;
115 #define NBD_CMD_REQUEUED 1
118 struct nbd_device *nbd;
127 #if IS_ENABLED(CONFIG_DEBUG_FS)
128 static struct dentry *nbd_dbg_dir;
131 #define nbd_name(nbd) ((nbd)->disk->disk_name)
133 #define NBD_MAGIC 0x68797548
135 static unsigned int nbds_max = 16;
136 static int max_part = 16;
137 static struct workqueue_struct *recv_workqueue;
138 static int part_shift;
140 static int nbd_dev_dbg_init(struct nbd_device *nbd);
141 static void nbd_dev_dbg_close(struct nbd_device *nbd);
142 static void nbd_config_put(struct nbd_device *nbd);
143 static void nbd_connect_reply(struct genl_info *info, int index);
144 static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info);
145 static void nbd_dead_link_work(struct work_struct *work);
146 static void nbd_disconnect_and_put(struct nbd_device *nbd);
148 static inline struct device *nbd_to_dev(struct nbd_device *nbd)
150 return disk_to_dev(nbd->disk);
153 static void nbd_requeue_cmd(struct nbd_cmd *cmd)
155 struct request *req = blk_mq_rq_from_pdu(cmd);
157 if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
158 blk_mq_requeue_request(req, true);
161 #define NBD_COOKIE_BITS 32
163 static u64 nbd_cmd_handle(struct nbd_cmd *cmd)
165 struct request *req = blk_mq_rq_from_pdu(cmd);
166 u32 tag = blk_mq_unique_tag(req);
167 u64 cookie = cmd->cmd_cookie;
169 return (cookie << NBD_COOKIE_BITS) | tag;
172 static u32 nbd_handle_to_tag(u64 handle)
177 static u32 nbd_handle_to_cookie(u64 handle)
179 return (u32)(handle >> NBD_COOKIE_BITS);
182 static const char *nbdcmd_to_ascii(int cmd)
185 case NBD_CMD_READ: return "read";
186 case NBD_CMD_WRITE: return "write";
187 case NBD_CMD_DISC: return "disconnect";
188 case NBD_CMD_FLUSH: return "flush";
189 case NBD_CMD_TRIM: return "trim/discard";
194 static ssize_t pid_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
197 struct gendisk *disk = dev_to_disk(dev);
198 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
200 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
203 static const struct device_attribute pid_attr = {
204 .attr = { .name = "pid", .mode = 0444},
208 static void nbd_dev_remove(struct nbd_device *nbd)
210 struct gendisk *disk = nbd->disk;
211 struct request_queue *q;
216 blk_cleanup_queue(q);
217 blk_mq_free_tag_set(&nbd->tag_set);
218 disk->private_data = NULL;
224 static void nbd_put(struct nbd_device *nbd)
226 if (refcount_dec_and_mutex_lock(&nbd->refs,
228 idr_remove(&nbd_index_idr, nbd->index);
229 mutex_unlock(&nbd_index_mutex);
234 static int nbd_disconnected(struct nbd_config *config)
236 return test_bit(NBD_DISCONNECTED, &config->runtime_flags) ||
237 test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
240 static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
243 if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
244 struct link_dead_args *args;
245 args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO);
247 INIT_WORK(&args->work, nbd_dead_link_work);
248 args->index = nbd->index;
249 queue_work(system_wq, &args->work);
253 kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
254 if (atomic_dec_return(&nbd->config->live_connections) == 0) {
255 if (test_and_clear_bit(NBD_DISCONNECT_REQUESTED,
256 &nbd->config->runtime_flags)) {
257 set_bit(NBD_DISCONNECTED,
258 &nbd->config->runtime_flags);
259 dev_info(nbd_to_dev(nbd),
260 "Disconnected due to user request.\n");
265 nsock->pending = NULL;
269 static void nbd_size_clear(struct nbd_device *nbd)
271 if (nbd->config->bytesize) {
272 set_capacity(nbd->disk, 0);
273 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
277 static void nbd_size_update(struct nbd_device *nbd)
279 struct nbd_config *config = nbd->config;
280 struct block_device *bdev = bdget_disk(nbd->disk, 0);
282 if (config->flags & NBD_FLAG_SEND_TRIM) {
283 nbd->disk->queue->limits.discard_granularity = config->blksize;
284 nbd->disk->queue->limits.discard_alignment = config->blksize;
285 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
287 blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
288 blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
289 set_capacity(nbd->disk, config->bytesize >> 9);
292 bd_set_size(bdev, config->bytesize);
294 bdev->bd_invalidated = 1;
297 kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
300 static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
303 struct nbd_config *config = nbd->config;
304 config->blksize = blocksize;
305 config->bytesize = blocksize * nr_blocks;
306 if (nbd->task_recv != NULL)
307 nbd_size_update(nbd);
310 static void nbd_complete_rq(struct request *req)
312 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
314 dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", req,
315 cmd->status ? "failed" : "done");
317 blk_mq_end_request(req, cmd->status);
321 * Forcibly shutdown the socket causing all listeners to error
323 static void sock_shutdown(struct nbd_device *nbd)
325 struct nbd_config *config = nbd->config;
328 if (config->num_connections == 0)
330 if (test_and_set_bit(NBD_DISCONNECTED, &config->runtime_flags))
333 for (i = 0; i < config->num_connections; i++) {
334 struct nbd_sock *nsock = config->socks[i];
335 mutex_lock(&nsock->tx_lock);
336 nbd_mark_nsock_dead(nbd, nsock, 0);
337 mutex_unlock(&nsock->tx_lock);
339 dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n");
342 static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
345 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
346 struct nbd_device *nbd = cmd->nbd;
347 struct nbd_config *config;
349 if (!refcount_inc_not_zero(&nbd->config_refs)) {
350 cmd->status = BLK_STS_TIMEOUT;
353 config = nbd->config;
355 if (!mutex_trylock(&cmd->lock))
356 return BLK_EH_RESET_TIMER;
358 if (config->num_connections > 1) {
359 dev_err_ratelimited(nbd_to_dev(nbd),
360 "Connection timed out, retrying (%d/%d alive)\n",
361 atomic_read(&config->live_connections),
362 config->num_connections);
364 * Hooray we have more connections, requeue this IO, the submit
365 * path will put it on a real connection.
367 if (config->socks && config->num_connections > 1) {
368 if (cmd->index < config->num_connections) {
369 struct nbd_sock *nsock =
370 config->socks[cmd->index];
371 mutex_lock(&nsock->tx_lock);
372 /* We can have multiple outstanding requests, so
373 * we don't want to mark the nsock dead if we've
374 * already reconnected with a new socket, so
375 * only mark it dead if its the same socket we
378 if (cmd->cookie == nsock->cookie)
379 nbd_mark_nsock_dead(nbd, nsock, 1);
380 mutex_unlock(&nsock->tx_lock);
382 mutex_unlock(&cmd->lock);
383 nbd_requeue_cmd(cmd);
388 dev_err_ratelimited(nbd_to_dev(nbd),
389 "Connection timed out\n");
391 set_bit(NBD_TIMEDOUT, &config->runtime_flags);
392 cmd->status = BLK_STS_IOERR;
393 mutex_unlock(&cmd->lock);
397 blk_mq_complete_request(req);
402 * Send or receive packet.
404 static int sock_xmit(struct nbd_device *nbd, int index, int send,
405 struct iov_iter *iter, int msg_flags, int *sent)
407 struct nbd_config *config = nbd->config;
408 struct socket *sock = config->socks[index]->sock;
411 unsigned int noreclaim_flag;
413 if (unlikely(!sock)) {
414 dev_err_ratelimited(disk_to_dev(nbd->disk),
415 "Attempted %s on closed socket in sock_xmit\n",
416 (send ? "send" : "recv"));
420 msg.msg_iter = *iter;
422 noreclaim_flag = memalloc_noreclaim_save();
424 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
427 msg.msg_control = NULL;
428 msg.msg_controllen = 0;
429 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
432 result = sock_sendmsg(sock, &msg);
434 result = sock_recvmsg(sock, &msg, msg.msg_flags);
438 result = -EPIPE; /* short read */
443 } while (msg_data_left(&msg));
445 memalloc_noreclaim_restore(noreclaim_flag);
451 * Different settings for sk->sk_sndtimeo can result in different return values
452 * if there is a signal pending when we enter sendmsg, because reasons?
454 static inline int was_interrupted(int result)
456 return result == -ERESTARTSYS || result == -EINTR;
459 /* always call with the tx_lock held */
460 static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
462 struct request *req = blk_mq_rq_from_pdu(cmd);
463 struct nbd_config *config = nbd->config;
464 struct nbd_sock *nsock = config->socks[index];
466 struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
467 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
468 struct iov_iter from;
469 unsigned long size = blk_rq_bytes(req);
473 u32 nbd_cmd_flags = 0;
474 int sent = nsock->sent, skip = 0;
476 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
478 switch (req_op(req)) {
483 type = NBD_CMD_FLUSH;
486 type = NBD_CMD_WRITE;
495 if (rq_data_dir(req) == WRITE &&
496 (config->flags & NBD_FLAG_READ_ONLY)) {
497 dev_err_ratelimited(disk_to_dev(nbd->disk),
498 "Write on read-only\n");
502 if (req->cmd_flags & REQ_FUA)
503 nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
505 /* We did a partial send previously, and we at least sent the whole
506 * request struct, so just go and send the rest of the pages in the
510 if (sent >= sizeof(request)) {
511 skip = sent - sizeof(request);
514 iov_iter_advance(&from, sent);
519 cmd->cookie = nsock->cookie;
520 request.type = htonl(type | nbd_cmd_flags);
521 if (type != NBD_CMD_FLUSH) {
522 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
523 request.len = htonl(size);
525 handle = nbd_cmd_handle(cmd);
526 memcpy(request.handle, &handle, sizeof(handle));
528 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
529 req, nbdcmd_to_ascii(type),
530 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
531 result = sock_xmit(nbd, index, 1, &from,
532 (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
534 if (was_interrupted(result)) {
535 /* If we havne't sent anything we can just return BUSY,
536 * however if we have sent something we need to make
537 * sure we only allow this req to be sent until we are
541 nsock->pending = req;
544 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
545 return BLK_STS_RESOURCE;
547 dev_err_ratelimited(disk_to_dev(nbd->disk),
548 "Send control failed (result %d)\n", result);
552 if (type != NBD_CMD_WRITE)
557 struct bio *next = bio->bi_next;
558 struct bvec_iter iter;
561 bio_for_each_segment(bvec, bio, iter) {
562 bool is_last = !next && bio_iter_last(bvec, iter);
563 int flags = is_last ? 0 : MSG_MORE;
565 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
567 iov_iter_bvec(&from, WRITE, &bvec, 1, bvec.bv_len);
569 if (skip >= iov_iter_count(&from)) {
570 skip -= iov_iter_count(&from);
573 iov_iter_advance(&from, skip);
576 result = sock_xmit(nbd, index, 1, &from, flags, &sent);
578 if (was_interrupted(result)) {
579 /* We've already sent the header, we
580 * have no choice but to set pending and
583 nsock->pending = req;
585 set_bit(NBD_CMD_REQUEUED, &cmd->flags);
586 return BLK_STS_RESOURCE;
588 dev_err(disk_to_dev(nbd->disk),
589 "Send data failed (result %d)\n",
594 * The completion might already have come in,
595 * so break for the last one instead of letting
596 * the iterator do it. This prevents use-after-free
605 nsock->pending = NULL;
610 /* NULL returned = something went wrong, inform userspace */
611 static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
613 struct nbd_config *config = nbd->config;
615 struct nbd_reply reply;
617 struct request *req = NULL;
621 struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)};
626 iov_iter_kvec(&to, READ, &iov, 1, sizeof(reply));
627 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
629 if (!nbd_disconnected(config))
630 dev_err(disk_to_dev(nbd->disk),
631 "Receive control failed (result %d)\n", result);
632 return ERR_PTR(result);
635 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
636 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
637 (unsigned long)ntohl(reply.magic));
638 return ERR_PTR(-EPROTO);
641 memcpy(&handle, reply.handle, sizeof(handle));
642 tag = nbd_handle_to_tag(handle);
643 hwq = blk_mq_unique_tag_to_hwq(tag);
644 if (hwq < nbd->tag_set.nr_hw_queues)
645 req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq],
646 blk_mq_unique_tag_to_tag(tag));
647 if (!req || !blk_mq_request_started(req)) {
648 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n",
650 return ERR_PTR(-ENOENT);
652 cmd = blk_mq_rq_to_pdu(req);
654 mutex_lock(&cmd->lock);
655 if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
656 dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
657 req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
661 if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) {
662 dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n",
667 if (ntohl(reply.error)) {
668 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
670 cmd->status = BLK_STS_IOERR;
674 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
675 if (rq_data_dir(req) != WRITE) {
676 struct req_iterator iter;
679 rq_for_each_segment(bvec, req, iter) {
680 iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len);
681 result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
683 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
686 * If we've disconnected or we only have 1
687 * connection then we need to make sure we
688 * complete this request, otherwise error out
689 * and let the timeout stuff handle resubmitting
690 * this request onto another connection.
692 if (nbd_disconnected(config) ||
693 config->num_connections <= 1) {
694 cmd->status = BLK_STS_IOERR;
700 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
705 mutex_unlock(&cmd->lock);
706 return ret ? ERR_PTR(ret) : cmd;
709 static void recv_work(struct work_struct *work)
711 struct recv_thread_args *args = container_of(work,
712 struct recv_thread_args,
714 struct nbd_device *nbd = args->nbd;
715 struct nbd_config *config = nbd->config;
719 cmd = nbd_read_stat(nbd, args->index);
721 struct nbd_sock *nsock = config->socks[args->index];
723 mutex_lock(&nsock->tx_lock);
724 nbd_mark_nsock_dead(nbd, nsock, 1);
725 mutex_unlock(&nsock->tx_lock);
729 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd));
731 atomic_dec(&config->recv_threads);
732 wake_up(&config->recv_wq);
737 static void nbd_clear_req(struct request *req, void *data, bool reserved)
739 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
741 cmd->status = BLK_STS_IOERR;
742 blk_mq_complete_request(req);
745 static void nbd_clear_que(struct nbd_device *nbd)
747 blk_mq_quiesce_queue(nbd->disk->queue);
748 blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL);
749 blk_mq_unquiesce_queue(nbd->disk->queue);
750 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
753 static int find_fallback(struct nbd_device *nbd, int index)
755 struct nbd_config *config = nbd->config;
757 struct nbd_sock *nsock = config->socks[index];
758 int fallback = nsock->fallback_index;
760 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
763 if (config->num_connections <= 1) {
764 dev_err_ratelimited(disk_to_dev(nbd->disk),
765 "Attempted send on invalid socket\n");
769 if (fallback >= 0 && fallback < config->num_connections &&
770 !config->socks[fallback]->dead)
773 if (nsock->fallback_index < 0 ||
774 nsock->fallback_index >= config->num_connections ||
775 config->socks[nsock->fallback_index]->dead) {
777 for (i = 0; i < config->num_connections; i++) {
780 if (!config->socks[i]->dead) {
785 nsock->fallback_index = new_index;
787 dev_err_ratelimited(disk_to_dev(nbd->disk),
788 "Dead connection, failed to find a fallback\n");
792 new_index = nsock->fallback_index;
796 static int wait_for_reconnect(struct nbd_device *nbd)
798 struct nbd_config *config = nbd->config;
799 if (!config->dead_conn_timeout)
801 if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
803 return wait_event_timeout(config->conn_wait,
804 atomic_read(&config->live_connections) > 0,
805 config->dead_conn_timeout) > 0;
808 static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
810 struct request *req = blk_mq_rq_from_pdu(cmd);
811 struct nbd_device *nbd = cmd->nbd;
812 struct nbd_config *config;
813 struct nbd_sock *nsock;
816 if (!refcount_inc_not_zero(&nbd->config_refs)) {
817 dev_err_ratelimited(disk_to_dev(nbd->disk),
818 "Socks array is empty\n");
819 blk_mq_start_request(req);
822 config = nbd->config;
824 if (index >= config->num_connections) {
825 dev_err_ratelimited(disk_to_dev(nbd->disk),
826 "Attempted send on invalid socket\n");
828 blk_mq_start_request(req);
831 cmd->status = BLK_STS_OK;
833 nsock = config->socks[index];
834 mutex_lock(&nsock->tx_lock);
836 int old_index = index;
837 index = find_fallback(nbd, index);
838 mutex_unlock(&nsock->tx_lock);
840 if (wait_for_reconnect(nbd)) {
844 /* All the sockets should already be down at this point,
845 * we just want to make sure that DISCONNECTED is set so
846 * any requests that come in that were queue'ed waiting
847 * for the reconnect timer don't trigger the timer again
848 * and instead just error out.
852 blk_mq_start_request(req);
858 /* Handle the case that we have a pending request that was partially
859 * transmitted that _has_ to be serviced first. We need to call requeue
860 * here so that it gets put _after_ the request that is already on the
863 blk_mq_start_request(req);
864 if (unlikely(nsock->pending && nsock->pending != req)) {
865 nbd_requeue_cmd(cmd);
870 * Some failures are related to the link going down, so anything that
871 * returns EAGAIN can be retried on a different socket.
873 ret = nbd_send_cmd(nbd, cmd, index);
874 if (ret == -EAGAIN) {
875 dev_err_ratelimited(disk_to_dev(nbd->disk),
876 "Request send failed, requeueing\n");
877 nbd_mark_nsock_dead(nbd, nsock, 1);
878 nbd_requeue_cmd(cmd);
882 mutex_unlock(&nsock->tx_lock);
887 static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
888 const struct blk_mq_queue_data *bd)
890 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
894 * Since we look at the bio's to send the request over the network we
895 * need to make sure the completion work doesn't mark this request done
896 * before we are done doing our send. This keeps us from dereferencing
897 * freed data if we have particularly fast completions (ie we get the
898 * completion before we exit sock_xmit on the last bvec) or in the case
899 * that the server is misbehaving (or there was an error) before we're
900 * done sending everything over the wire.
902 mutex_lock(&cmd->lock);
903 clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
905 /* We can be called directly from the user space process, which means we
906 * could possibly have signals pending so our sendmsg will fail. In
907 * this case we need to return that we are busy, otherwise error out as
910 ret = nbd_handle_cmd(cmd, hctx->queue_num);
915 mutex_unlock(&cmd->lock);
920 static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
923 struct nbd_config *config = nbd->config;
925 struct nbd_sock **socks;
926 struct nbd_sock *nsock;
929 sock = sockfd_lookup(arg, &err);
933 if (!netlink && !nbd->task_setup &&
934 !test_bit(NBD_BOUND, &config->runtime_flags))
935 nbd->task_setup = current;
938 (nbd->task_setup != current ||
939 test_bit(NBD_BOUND, &config->runtime_flags))) {
940 dev_err(disk_to_dev(nbd->disk),
941 "Device being setup by another task");
946 socks = krealloc(config->socks, (config->num_connections + 1) *
947 sizeof(struct nbd_sock *), GFP_KERNEL);
952 nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
958 config->socks = socks;
960 nsock->fallback_index = -1;
962 mutex_init(&nsock->tx_lock);
964 nsock->pending = NULL;
967 socks[config->num_connections++] = nsock;
968 atomic_inc(&config->live_connections);
973 static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
975 struct nbd_config *config = nbd->config;
976 struct socket *sock, *old;
977 struct recv_thread_args *args;
981 sock = sockfd_lookup(arg, &err);
985 args = kzalloc(sizeof(*args), GFP_KERNEL);
991 for (i = 0; i < config->num_connections; i++) {
992 struct nbd_sock *nsock = config->socks[i];
997 mutex_lock(&nsock->tx_lock);
999 mutex_unlock(&nsock->tx_lock);
1002 sk_set_memalloc(sock->sk);
1003 if (nbd->tag_set.timeout)
1004 sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
1005 atomic_inc(&config->recv_threads);
1006 refcount_inc(&nbd->config_refs);
1008 nsock->fallback_index = -1;
1010 nsock->dead = false;
1011 INIT_WORK(&args->work, recv_work);
1015 mutex_unlock(&nsock->tx_lock);
1018 clear_bit(NBD_DISCONNECTED, &config->runtime_flags);
1020 /* We take the tx_mutex in an error path in the recv_work, so we
1021 * need to queue_work outside of the tx_mutex.
1023 queue_work(recv_workqueue, &args->work);
1025 atomic_inc(&config->live_connections);
1026 wake_up(&config->conn_wait);
1034 static void nbd_bdev_reset(struct block_device *bdev)
1036 if (bdev->bd_openers > 1)
1038 bd_set_size(bdev, 0);
1041 static void nbd_parse_flags(struct nbd_device *nbd)
1043 struct nbd_config *config = nbd->config;
1044 if (config->flags & NBD_FLAG_READ_ONLY)
1045 set_disk_ro(nbd->disk, true);
1047 set_disk_ro(nbd->disk, false);
1048 if (config->flags & NBD_FLAG_SEND_TRIM)
1049 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nbd->disk->queue);
1050 if (config->flags & NBD_FLAG_SEND_FLUSH) {
1051 if (config->flags & NBD_FLAG_SEND_FUA)
1052 blk_queue_write_cache(nbd->disk->queue, true, true);
1054 blk_queue_write_cache(nbd->disk->queue, true, false);
1057 blk_queue_write_cache(nbd->disk->queue, false, false);
1060 static void send_disconnects(struct nbd_device *nbd)
1062 struct nbd_config *config = nbd->config;
1063 struct nbd_request request = {
1064 .magic = htonl(NBD_REQUEST_MAGIC),
1065 .type = htonl(NBD_CMD_DISC),
1067 struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
1068 struct iov_iter from;
1071 for (i = 0; i < config->num_connections; i++) {
1072 struct nbd_sock *nsock = config->socks[i];
1074 iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request));
1075 mutex_lock(&nsock->tx_lock);
1076 ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
1078 dev_err(disk_to_dev(nbd->disk),
1079 "Send disconnect failed %d\n", ret);
1080 mutex_unlock(&nsock->tx_lock);
1084 static int nbd_disconnect(struct nbd_device *nbd)
1086 struct nbd_config *config = nbd->config;
1088 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
1089 set_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags);
1090 send_disconnects(nbd);
1094 static void nbd_clear_sock(struct nbd_device *nbd)
1098 nbd->task_setup = NULL;
1101 static void nbd_config_put(struct nbd_device *nbd)
1103 if (refcount_dec_and_mutex_lock(&nbd->config_refs,
1104 &nbd->config_lock)) {
1105 struct nbd_config *config = nbd->config;
1106 nbd_dev_dbg_close(nbd);
1107 nbd_size_clear(nbd);
1108 if (test_and_clear_bit(NBD_HAS_PID_FILE,
1109 &config->runtime_flags))
1110 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
1111 nbd->task_recv = NULL;
1112 nbd_clear_sock(nbd);
1113 if (config->num_connections) {
1115 for (i = 0; i < config->num_connections; i++) {
1116 sockfd_put(config->socks[i]->sock);
1117 kfree(config->socks[i]);
1119 kfree(config->socks);
1124 nbd->tag_set.timeout = 0;
1125 nbd->disk->queue->limits.discard_granularity = 0;
1126 nbd->disk->queue->limits.discard_alignment = 0;
1127 blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
1128 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue);
1130 mutex_unlock(&nbd->config_lock);
1132 module_put(THIS_MODULE);
1136 static int nbd_start_device(struct nbd_device *nbd)
1138 struct nbd_config *config = nbd->config;
1139 int num_connections = config->num_connections;
1146 if (num_connections > 1 &&
1147 !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
1148 dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
1152 blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
1153 nbd->task_recv = current;
1155 nbd_parse_flags(nbd);
1157 error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
1159 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
1162 set_bit(NBD_HAS_PID_FILE, &config->runtime_flags);
1164 nbd_dev_dbg_init(nbd);
1165 for (i = 0; i < num_connections; i++) {
1166 struct recv_thread_args *args;
1168 args = kzalloc(sizeof(*args), GFP_KERNEL);
1173 sk_set_memalloc(config->socks[i]->sock->sk);
1174 if (nbd->tag_set.timeout)
1175 config->socks[i]->sock->sk->sk_sndtimeo =
1176 nbd->tag_set.timeout;
1177 atomic_inc(&config->recv_threads);
1178 refcount_inc(&nbd->config_refs);
1179 INIT_WORK(&args->work, recv_work);
1182 queue_work(recv_workqueue, &args->work);
1184 nbd_size_update(nbd);
1188 static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev)
1190 struct nbd_config *config = nbd->config;
1193 ret = nbd_start_device(nbd);
1198 bdev->bd_invalidated = 1;
1199 mutex_unlock(&nbd->config_lock);
1200 ret = wait_event_interruptible(config->recv_wq,
1201 atomic_read(&config->recv_threads) == 0);
1204 mutex_lock(&nbd->config_lock);
1205 nbd_bdev_reset(bdev);
1206 /* user requested, ignore socket errors */
1207 if (test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags))
1209 if (test_bit(NBD_TIMEDOUT, &config->runtime_flags))
1214 static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
1215 struct block_device *bdev)
1219 nbd_bdev_reset(bdev);
1220 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1221 &nbd->config->runtime_flags))
1222 nbd_config_put(nbd);
1225 /* Must be called with config_lock held */
1226 static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
1227 unsigned int cmd, unsigned long arg)
1229 struct nbd_config *config = nbd->config;
1232 case NBD_DISCONNECT:
1233 return nbd_disconnect(nbd);
1234 case NBD_CLEAR_SOCK:
1235 nbd_clear_sock_ioctl(nbd, bdev);
1238 return nbd_add_socket(nbd, arg, false);
1239 case NBD_SET_BLKSIZE:
1240 if (!arg || !is_power_of_2(arg) || arg < 512 ||
1243 nbd_size_set(nbd, arg,
1244 div_s64(config->bytesize, arg));
1247 nbd_size_set(nbd, config->blksize,
1248 div_s64(arg, config->blksize));
1250 case NBD_SET_SIZE_BLOCKS:
1251 nbd_size_set(nbd, config->blksize, arg);
1253 case NBD_SET_TIMEOUT:
1255 nbd->tag_set.timeout = arg * HZ;
1256 blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
1261 config->flags = arg;
1264 return nbd_start_device_ioctl(nbd, bdev);
1267 * This is for compatibility only. The queue is always cleared
1268 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1271 case NBD_PRINT_DEBUG:
1273 * For compatibility only, we no longer keep a list of
1274 * outstanding requests.
1281 static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
1282 unsigned int cmd, unsigned long arg)
1284 struct nbd_device *nbd = bdev->bd_disk->private_data;
1285 struct nbd_config *config = nbd->config;
1286 int error = -EINVAL;
1288 if (!capable(CAP_SYS_ADMIN))
1291 /* The block layer will pass back some non-nbd ioctls in case we have
1292 * special handling for them, but we don't so just return an error.
1294 if (_IOC_TYPE(cmd) != 0xab)
1297 mutex_lock(&nbd->config_lock);
1299 /* Don't allow ioctl operations on a nbd device that was created with
1300 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1302 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1303 (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK))
1304 error = __nbd_ioctl(bdev, nbd, cmd, arg);
1306 dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n");
1307 mutex_unlock(&nbd->config_lock);
1311 static struct nbd_config *nbd_alloc_config(void)
1313 struct nbd_config *config;
1315 config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
1318 atomic_set(&config->recv_threads, 0);
1319 init_waitqueue_head(&config->recv_wq);
1320 init_waitqueue_head(&config->conn_wait);
1321 config->blksize = 1024;
1322 atomic_set(&config->live_connections, 0);
1323 try_module_get(THIS_MODULE);
1327 static int nbd_open(struct block_device *bdev, fmode_t mode)
1329 struct nbd_device *nbd;
1332 mutex_lock(&nbd_index_mutex);
1333 nbd = bdev->bd_disk->private_data;
1338 if (!refcount_inc_not_zero(&nbd->refs)) {
1342 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1343 struct nbd_config *config;
1345 mutex_lock(&nbd->config_lock);
1346 if (refcount_inc_not_zero(&nbd->config_refs)) {
1347 mutex_unlock(&nbd->config_lock);
1350 config = nbd->config = nbd_alloc_config();
1353 mutex_unlock(&nbd->config_lock);
1356 refcount_set(&nbd->config_refs, 1);
1357 refcount_inc(&nbd->refs);
1358 mutex_unlock(&nbd->config_lock);
1359 bdev->bd_invalidated = 1;
1360 } else if (nbd_disconnected(nbd->config)) {
1361 bdev->bd_invalidated = 1;
1364 mutex_unlock(&nbd_index_mutex);
1368 static void nbd_release(struct gendisk *disk, fmode_t mode)
1370 struct nbd_device *nbd = disk->private_data;
1371 struct block_device *bdev = bdget_disk(disk, 0);
1373 if (test_bit(NBD_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) &&
1374 bdev->bd_openers == 0)
1375 nbd_disconnect_and_put(nbd);
1377 nbd_config_put(nbd);
1381 static const struct block_device_operations nbd_fops =
1383 .owner = THIS_MODULE,
1385 .release = nbd_release,
1387 .compat_ioctl = nbd_ioctl,
1390 #if IS_ENABLED(CONFIG_DEBUG_FS)
1392 static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
1394 struct nbd_device *nbd = s->private;
1397 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
1402 static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
1404 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
1407 static const struct file_operations nbd_dbg_tasks_ops = {
1408 .open = nbd_dbg_tasks_open,
1410 .llseek = seq_lseek,
1411 .release = single_release,
1414 static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
1416 struct nbd_device *nbd = s->private;
1417 u32 flags = nbd->config->flags;
1419 seq_printf(s, "Hex: 0x%08x\n\n", flags);
1421 seq_puts(s, "Known flags:\n");
1423 if (flags & NBD_FLAG_HAS_FLAGS)
1424 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
1425 if (flags & NBD_FLAG_READ_ONLY)
1426 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
1427 if (flags & NBD_FLAG_SEND_FLUSH)
1428 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
1429 if (flags & NBD_FLAG_SEND_FUA)
1430 seq_puts(s, "NBD_FLAG_SEND_FUA\n");
1431 if (flags & NBD_FLAG_SEND_TRIM)
1432 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
1437 static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
1439 return single_open(file, nbd_dbg_flags_show, inode->i_private);
1442 static const struct file_operations nbd_dbg_flags_ops = {
1443 .open = nbd_dbg_flags_open,
1445 .llseek = seq_lseek,
1446 .release = single_release,
1449 static int nbd_dev_dbg_init(struct nbd_device *nbd)
1452 struct nbd_config *config = nbd->config;
1457 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
1459 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n",
1463 config->dbg_dir = dir;
1465 debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
1466 debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize);
1467 debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout);
1468 debugfs_create_u64("blocksize", 0444, dir, &config->blksize);
1469 debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops);
1474 static void nbd_dev_dbg_close(struct nbd_device *nbd)
1476 debugfs_remove_recursive(nbd->config->dbg_dir);
1479 static int nbd_dbg_init(void)
1481 struct dentry *dbg_dir;
1483 dbg_dir = debugfs_create_dir("nbd", NULL);
1487 nbd_dbg_dir = dbg_dir;
1492 static void nbd_dbg_close(void)
1494 debugfs_remove_recursive(nbd_dbg_dir);
1497 #else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1499 static int nbd_dev_dbg_init(struct nbd_device *nbd)
1504 static void nbd_dev_dbg_close(struct nbd_device *nbd)
1508 static int nbd_dbg_init(void)
1513 static void nbd_dbg_close(void)
1519 static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
1520 unsigned int hctx_idx, unsigned int numa_node)
1522 struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq);
1523 cmd->nbd = set->driver_data;
1525 mutex_init(&cmd->lock);
1529 static const struct blk_mq_ops nbd_mq_ops = {
1530 .queue_rq = nbd_queue_rq,
1531 .complete = nbd_complete_rq,
1532 .init_request = nbd_init_request,
1533 .timeout = nbd_xmit_timeout,
1536 static int nbd_dev_add(int index)
1538 struct nbd_device *nbd;
1539 struct gendisk *disk;
1540 struct request_queue *q;
1543 nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL);
1547 disk = alloc_disk(1 << part_shift);
1552 err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
1557 err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL);
1566 nbd->tag_set.ops = &nbd_mq_ops;
1567 nbd->tag_set.nr_hw_queues = 1;
1568 nbd->tag_set.queue_depth = 128;
1569 nbd->tag_set.numa_node = NUMA_NO_NODE;
1570 nbd->tag_set.cmd_size = sizeof(struct nbd_cmd);
1571 nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
1572 BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING;
1573 nbd->tag_set.driver_data = nbd;
1575 err = blk_mq_alloc_tag_set(&nbd->tag_set);
1579 q = blk_mq_init_queue(&nbd->tag_set);
1587 * Tell the block layer that we are not a rotational device
1589 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
1590 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1591 disk->queue->limits.discard_granularity = 0;
1592 disk->queue->limits.discard_alignment = 0;
1593 blk_queue_max_discard_sectors(disk->queue, 0);
1594 blk_queue_max_segment_size(disk->queue, UINT_MAX);
1595 blk_queue_max_segments(disk->queue, USHRT_MAX);
1596 blk_queue_max_hw_sectors(disk->queue, 65536);
1597 disk->queue->limits.max_sectors = 256;
1599 mutex_init(&nbd->config_lock);
1600 refcount_set(&nbd->config_refs, 0);
1601 refcount_set(&nbd->refs, 1);
1602 INIT_LIST_HEAD(&nbd->list);
1603 disk->major = NBD_MAJOR;
1604 disk->first_minor = index << part_shift;
1605 disk->fops = &nbd_fops;
1606 disk->private_data = nbd;
1607 sprintf(disk->disk_name, "nbd%d", index);
1609 nbd_total_devices++;
1613 blk_mq_free_tag_set(&nbd->tag_set);
1615 idr_remove(&nbd_index_idr, index);
1624 static int find_free_cb(int id, void *ptr, void *data)
1626 struct nbd_device *nbd = ptr;
1627 struct nbd_device **found = data;
1629 if (!refcount_read(&nbd->config_refs)) {
1636 /* Netlink interface. */
1637 static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
1638 [NBD_ATTR_INDEX] = { .type = NLA_U32 },
1639 [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 },
1640 [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 },
1641 [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 },
1642 [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 },
1643 [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 },
1644 [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
1645 [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
1646 [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
1649 static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
1650 [NBD_SOCK_FD] = { .type = NLA_U32 },
1653 /* We don't use this right now since we don't parse the incoming list, but we
1654 * still want it here so userspace knows what to expect.
1656 static const struct nla_policy __attribute__((unused))
1657 nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = {
1658 [NBD_DEVICE_INDEX] = { .type = NLA_U32 },
1659 [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 },
1662 static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1664 struct nbd_device *nbd = NULL;
1665 struct nbd_config *config;
1668 bool put_dev = false;
1670 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1673 if (info->attrs[NBD_ATTR_INDEX])
1674 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1675 if (!info->attrs[NBD_ATTR_SOCKETS]) {
1676 printk(KERN_ERR "nbd: must specify at least one socket\n");
1679 if (!info->attrs[NBD_ATTR_SIZE_BYTES]) {
1680 printk(KERN_ERR "nbd: must specify a size in bytes for the device\n");
1684 mutex_lock(&nbd_index_mutex);
1686 ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
1689 new_index = nbd_dev_add(-1);
1690 if (new_index < 0) {
1691 mutex_unlock(&nbd_index_mutex);
1692 printk(KERN_ERR "nbd: failed to add new device\n");
1695 nbd = idr_find(&nbd_index_idr, new_index);
1698 nbd = idr_find(&nbd_index_idr, index);
1700 ret = nbd_dev_add(index);
1702 mutex_unlock(&nbd_index_mutex);
1703 printk(KERN_ERR "nbd: failed to add new device\n");
1706 nbd = idr_find(&nbd_index_idr, index);
1710 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1712 mutex_unlock(&nbd_index_mutex);
1715 if (!refcount_inc_not_zero(&nbd->refs)) {
1716 mutex_unlock(&nbd_index_mutex);
1719 printk(KERN_ERR "nbd: device at index %d is going down\n",
1723 mutex_unlock(&nbd_index_mutex);
1725 mutex_lock(&nbd->config_lock);
1726 if (refcount_read(&nbd->config_refs)) {
1727 mutex_unlock(&nbd->config_lock);
1731 printk(KERN_ERR "nbd: nbd%d already in use\n", index);
1734 if (WARN_ON(nbd->config)) {
1735 mutex_unlock(&nbd->config_lock);
1739 config = nbd->config = nbd_alloc_config();
1741 mutex_unlock(&nbd->config_lock);
1743 printk(KERN_ERR "nbd: couldn't allocate config\n");
1746 refcount_set(&nbd->config_refs, 1);
1747 set_bit(NBD_BOUND, &config->runtime_flags);
1749 if (info->attrs[NBD_ATTR_SIZE_BYTES]) {
1750 u64 bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]);
1751 nbd_size_set(nbd, config->blksize,
1752 div64_u64(bytes, config->blksize));
1754 if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) {
1756 nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]);
1757 nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize));
1759 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1760 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1761 nbd->tag_set.timeout = timeout * HZ;
1762 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1764 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1765 config->dead_conn_timeout =
1766 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1767 config->dead_conn_timeout *= HZ;
1769 if (info->attrs[NBD_ATTR_SERVER_FLAGS])
1771 nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]);
1772 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1773 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1774 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1775 set_bit(NBD_DESTROY_ON_DISCONNECT,
1776 &config->runtime_flags);
1779 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
1780 set_bit(NBD_DISCONNECT_ON_CLOSE,
1781 &config->runtime_flags);
1785 if (info->attrs[NBD_ATTR_SOCKETS]) {
1786 struct nlattr *attr;
1789 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1791 struct nlattr *socks[NBD_SOCK_MAX+1];
1793 if (nla_type(attr) != NBD_SOCK_ITEM) {
1794 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1798 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
1799 nbd_sock_policy, info->extack);
1801 printk(KERN_ERR "nbd: error processing sock list\n");
1805 if (!socks[NBD_SOCK_FD])
1807 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1808 ret = nbd_add_socket(nbd, fd, true);
1813 ret = nbd_start_device(nbd);
1815 mutex_unlock(&nbd->config_lock);
1817 set_bit(NBD_HAS_CONFIG_REF, &config->runtime_flags);
1818 refcount_inc(&nbd->config_refs);
1819 nbd_connect_reply(info, nbd->index);
1821 nbd_config_put(nbd);
1827 static void nbd_disconnect_and_put(struct nbd_device *nbd)
1829 mutex_lock(&nbd->config_lock);
1830 nbd_disconnect(nbd);
1831 nbd_clear_sock(nbd);
1832 mutex_unlock(&nbd->config_lock);
1833 if (test_and_clear_bit(NBD_HAS_CONFIG_REF,
1834 &nbd->config->runtime_flags))
1835 nbd_config_put(nbd);
1838 static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
1840 struct nbd_device *nbd;
1843 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1846 if (!info->attrs[NBD_ATTR_INDEX]) {
1847 printk(KERN_ERR "nbd: must specify an index to disconnect\n");
1850 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1851 mutex_lock(&nbd_index_mutex);
1852 nbd = idr_find(&nbd_index_idr, index);
1854 mutex_unlock(&nbd_index_mutex);
1855 printk(KERN_ERR "nbd: couldn't find device at index %d\n",
1859 if (!refcount_inc_not_zero(&nbd->refs)) {
1860 mutex_unlock(&nbd_index_mutex);
1861 printk(KERN_ERR "nbd: device at index %d is going down\n",
1865 mutex_unlock(&nbd_index_mutex);
1866 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1870 nbd_disconnect_and_put(nbd);
1871 nbd_config_put(nbd);
1876 static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
1878 struct nbd_device *nbd = NULL;
1879 struct nbd_config *config;
1882 bool put_dev = false;
1884 if (!netlink_capable(skb, CAP_SYS_ADMIN))
1887 if (!info->attrs[NBD_ATTR_INDEX]) {
1888 printk(KERN_ERR "nbd: must specify a device to reconfigure\n");
1891 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
1892 mutex_lock(&nbd_index_mutex);
1893 nbd = idr_find(&nbd_index_idr, index);
1895 mutex_unlock(&nbd_index_mutex);
1896 printk(KERN_ERR "nbd: couldn't find a device at index %d\n",
1900 if (!refcount_inc_not_zero(&nbd->refs)) {
1901 mutex_unlock(&nbd_index_mutex);
1902 printk(KERN_ERR "nbd: device at index %d is going down\n",
1906 mutex_unlock(&nbd_index_mutex);
1908 if (!refcount_inc_not_zero(&nbd->config_refs)) {
1909 dev_err(nbd_to_dev(nbd),
1910 "not configured, cannot reconfigure\n");
1915 mutex_lock(&nbd->config_lock);
1916 config = nbd->config;
1917 if (!test_bit(NBD_BOUND, &config->runtime_flags) ||
1919 dev_err(nbd_to_dev(nbd),
1920 "not configured, cannot reconfigure\n");
1925 if (info->attrs[NBD_ATTR_TIMEOUT]) {
1926 u64 timeout = nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT]);
1927 nbd->tag_set.timeout = timeout * HZ;
1928 blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ);
1930 if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) {
1931 config->dead_conn_timeout =
1932 nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]);
1933 config->dead_conn_timeout *= HZ;
1935 if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) {
1936 u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]);
1937 if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
1938 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT,
1939 &config->runtime_flags))
1942 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT,
1943 &config->runtime_flags))
1944 refcount_inc(&nbd->refs);
1947 if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
1948 set_bit(NBD_DISCONNECT_ON_CLOSE,
1949 &config->runtime_flags);
1951 clear_bit(NBD_DISCONNECT_ON_CLOSE,
1952 &config->runtime_flags);
1956 if (info->attrs[NBD_ATTR_SOCKETS]) {
1957 struct nlattr *attr;
1960 nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
1962 struct nlattr *socks[NBD_SOCK_MAX+1];
1964 if (nla_type(attr) != NBD_SOCK_ITEM) {
1965 printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n");
1969 ret = nla_parse_nested(socks, NBD_SOCK_MAX, attr,
1970 nbd_sock_policy, info->extack);
1972 printk(KERN_ERR "nbd: error processing sock list\n");
1976 if (!socks[NBD_SOCK_FD])
1978 fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
1979 ret = nbd_reconnect_socket(nbd, fd);
1985 dev_info(nbd_to_dev(nbd), "reconnected socket\n");
1989 mutex_unlock(&nbd->config_lock);
1990 nbd_config_put(nbd);
1997 static const struct genl_ops nbd_connect_genl_ops[] = {
1999 .cmd = NBD_CMD_CONNECT,
2000 .policy = nbd_attr_policy,
2001 .doit = nbd_genl_connect,
2004 .cmd = NBD_CMD_DISCONNECT,
2005 .policy = nbd_attr_policy,
2006 .doit = nbd_genl_disconnect,
2009 .cmd = NBD_CMD_RECONFIGURE,
2010 .policy = nbd_attr_policy,
2011 .doit = nbd_genl_reconfigure,
2014 .cmd = NBD_CMD_STATUS,
2015 .policy = nbd_attr_policy,
2016 .doit = nbd_genl_status,
2020 static const struct genl_multicast_group nbd_mcast_grps[] = {
2021 { .name = NBD_GENL_MCAST_GROUP_NAME, },
2024 static struct genl_family nbd_genl_family __ro_after_init = {
2026 .name = NBD_GENL_FAMILY_NAME,
2027 .version = NBD_GENL_VERSION,
2028 .module = THIS_MODULE,
2029 .ops = nbd_connect_genl_ops,
2030 .n_ops = ARRAY_SIZE(nbd_connect_genl_ops),
2031 .maxattr = NBD_ATTR_MAX,
2032 .mcgrps = nbd_mcast_grps,
2033 .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps),
2036 static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply)
2038 struct nlattr *dev_opt;
2042 /* This is a little racey, but for status it's ok. The
2043 * reason we don't take a ref here is because we can't
2044 * take a ref in the index == -1 case as we would need
2045 * to put under the nbd_index_mutex, which could
2046 * deadlock if we are configured to remove ourselves
2047 * once we're disconnected.
2049 if (refcount_read(&nbd->config_refs))
2051 dev_opt = nla_nest_start(reply, NBD_DEVICE_ITEM);
2054 ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index);
2057 ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED,
2061 nla_nest_end(reply, dev_opt);
2065 static int status_cb(int id, void *ptr, void *data)
2067 struct nbd_device *nbd = ptr;
2068 return populate_nbd_status(nbd, (struct sk_buff *)data);
2071 static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info)
2073 struct nlattr *dev_list;
2074 struct sk_buff *reply;
2080 if (info->attrs[NBD_ATTR_INDEX])
2081 index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]);
2083 mutex_lock(&nbd_index_mutex);
2085 msg_size = nla_total_size(nla_attr_size(sizeof(u32)) +
2086 nla_attr_size(sizeof(u8)));
2087 msg_size *= (index == -1) ? nbd_total_devices : 1;
2089 reply = genlmsg_new(msg_size, GFP_KERNEL);
2092 reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0,
2099 dev_list = nla_nest_start(reply, NBD_ATTR_DEVICE_LIST);
2101 ret = idr_for_each(&nbd_index_idr, &status_cb, reply);
2107 struct nbd_device *nbd;
2108 nbd = idr_find(&nbd_index_idr, index);
2110 ret = populate_nbd_status(nbd, reply);
2117 nla_nest_end(reply, dev_list);
2118 genlmsg_end(reply, reply_head);
2119 genlmsg_reply(reply, info);
2122 mutex_unlock(&nbd_index_mutex);
2126 static void nbd_connect_reply(struct genl_info *info, int index)
2128 struct sk_buff *skb;
2132 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2135 msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0,
2141 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2146 genlmsg_end(skb, msg_head);
2147 genlmsg_reply(skb, info);
2150 static void nbd_mcast_index(int index)
2152 struct sk_buff *skb;
2156 skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL);
2159 msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0,
2165 ret = nla_put_u32(skb, NBD_ATTR_INDEX, index);
2170 genlmsg_end(skb, msg_head);
2171 genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL);
2174 static void nbd_dead_link_work(struct work_struct *work)
2176 struct link_dead_args *args = container_of(work, struct link_dead_args,
2178 nbd_mcast_index(args->index);
2182 static int __init nbd_init(void)
2186 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
2189 printk(KERN_ERR "nbd: max_part must be >= 0\n");
2195 part_shift = fls(max_part);
2198 * Adjust max_part according to part_shift as it is exported
2199 * to user space so that user can know the max number of
2200 * partition kernel should be able to manage.
2202 * Note that -1 is required because partition 0 is reserved
2203 * for the whole disk.
2205 max_part = (1UL << part_shift) - 1;
2208 if ((1UL << part_shift) > DISK_MAX_PARTS)
2211 if (nbds_max > 1UL << (MINORBITS - part_shift))
2213 recv_workqueue = alloc_workqueue("knbd-recv",
2214 WQ_MEM_RECLAIM | WQ_HIGHPRI |
2216 if (!recv_workqueue)
2219 if (register_blkdev(NBD_MAJOR, "nbd")) {
2220 destroy_workqueue(recv_workqueue);
2224 if (genl_register_family(&nbd_genl_family)) {
2225 unregister_blkdev(NBD_MAJOR, "nbd");
2226 destroy_workqueue(recv_workqueue);
2231 mutex_lock(&nbd_index_mutex);
2232 for (i = 0; i < nbds_max; i++)
2234 mutex_unlock(&nbd_index_mutex);
2238 static int nbd_exit_cb(int id, void *ptr, void *data)
2240 struct list_head *list = (struct list_head *)data;
2241 struct nbd_device *nbd = ptr;
2243 list_add_tail(&nbd->list, list);
2247 static void __exit nbd_cleanup(void)
2249 struct nbd_device *nbd;
2250 LIST_HEAD(del_list);
2254 mutex_lock(&nbd_index_mutex);
2255 idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list);
2256 mutex_unlock(&nbd_index_mutex);
2258 while (!list_empty(&del_list)) {
2259 nbd = list_first_entry(&del_list, struct nbd_device, list);
2260 list_del_init(&nbd->list);
2261 if (refcount_read(&nbd->refs) != 1)
2262 printk(KERN_ERR "nbd: possibly leaking a device\n");
2266 idr_destroy(&nbd_index_idr);
2267 genl_unregister_family(&nbd_genl_family);
2268 destroy_workqueue(recv_workqueue);
2269 unregister_blkdev(NBD_MAJOR, "nbd");
2272 module_init(nbd_init);
2273 module_exit(nbd_cleanup);
2275 MODULE_DESCRIPTION("Network Block Device");
2276 MODULE_LICENSE("GPL");
2278 module_param(nbds_max, int, 0444);
2279 MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
2280 module_param(max_part, int, 0444);
2281 MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)");