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
82 unsigned long runtime_flags
;
83 u64 dead_conn_timeout
;
85 struct nbd_sock
**socks
;
87 atomic_t live_connections
;
88 wait_queue_head_t conn_wait
;
90 atomic_t recv_threads
;
91 wait_queue_head_t recv_wq
;
94 #if IS_ENABLED(CONFIG_DEBUG_FS)
95 struct dentry
*dbg_dir
;
100 struct blk_mq_tag_set tag_set
;
103 refcount_t config_refs
;
105 struct nbd_config
*config
;
106 struct mutex config_lock
;
107 struct gendisk
*disk
;
109 struct list_head list
;
110 struct task_struct
*task_recv
;
111 struct task_struct
*task_setup
;
115 struct nbd_device
*nbd
;
118 struct completion send_complete
;
122 #if IS_ENABLED(CONFIG_DEBUG_FS)
123 static struct dentry
*nbd_dbg_dir
;
126 #define nbd_name(nbd) ((nbd)->disk->disk_name)
128 #define NBD_MAGIC 0x68797548
130 static unsigned int nbds_max
= 16;
131 static int max_part
= 15;
132 static struct workqueue_struct
*recv_workqueue
;
133 static int part_shift
;
135 static int nbd_dev_dbg_init(struct nbd_device
*nbd
);
136 static void nbd_dev_dbg_close(struct nbd_device
*nbd
);
137 static void nbd_config_put(struct nbd_device
*nbd
);
138 static void nbd_connect_reply(struct genl_info
*info
, int index
);
139 static int nbd_genl_status(struct sk_buff
*skb
, struct genl_info
*info
);
140 static void nbd_dead_link_work(struct work_struct
*work
);
142 static inline struct device
*nbd_to_dev(struct nbd_device
*nbd
)
144 return disk_to_dev(nbd
->disk
);
147 static const char *nbdcmd_to_ascii(int cmd
)
150 case NBD_CMD_READ
: return "read";
151 case NBD_CMD_WRITE
: return "write";
152 case NBD_CMD_DISC
: return "disconnect";
153 case NBD_CMD_FLUSH
: return "flush";
154 case NBD_CMD_TRIM
: return "trim/discard";
159 static ssize_t
pid_show(struct device
*dev
,
160 struct device_attribute
*attr
, char *buf
)
162 struct gendisk
*disk
= dev_to_disk(dev
);
163 struct nbd_device
*nbd
= (struct nbd_device
*)disk
->private_data
;
165 return sprintf(buf
, "%d\n", task_pid_nr(nbd
->task_recv
));
168 static struct device_attribute pid_attr
= {
169 .attr
= { .name
= "pid", .mode
= S_IRUGO
},
173 static void nbd_dev_remove(struct nbd_device
*nbd
)
175 struct gendisk
*disk
= nbd
->disk
;
178 blk_cleanup_queue(disk
->queue
);
179 blk_mq_free_tag_set(&nbd
->tag_set
);
180 disk
->private_data
= NULL
;
186 static void nbd_put(struct nbd_device
*nbd
)
188 if (refcount_dec_and_mutex_lock(&nbd
->refs
,
190 idr_remove(&nbd_index_idr
, nbd
->index
);
191 mutex_unlock(&nbd_index_mutex
);
196 static int nbd_disconnected(struct nbd_config
*config
)
198 return test_bit(NBD_DISCONNECTED
, &config
->runtime_flags
) ||
199 test_bit(NBD_DISCONNECT_REQUESTED
, &config
->runtime_flags
);
202 static void nbd_mark_nsock_dead(struct nbd_device
*nbd
, struct nbd_sock
*nsock
,
205 if (!nsock
->dead
&& notify
&& !nbd_disconnected(nbd
->config
)) {
206 struct link_dead_args
*args
;
207 args
= kmalloc(sizeof(struct link_dead_args
), GFP_NOIO
);
209 INIT_WORK(&args
->work
, nbd_dead_link_work
);
210 args
->index
= nbd
->index
;
211 queue_work(system_wq
, &args
->work
);
215 kernel_sock_shutdown(nsock
->sock
, SHUT_RDWR
);
216 atomic_dec(&nbd
->config
->live_connections
);
219 nsock
->pending
= NULL
;
223 static void nbd_size_clear(struct nbd_device
*nbd
)
225 if (nbd
->config
->bytesize
) {
226 set_capacity(nbd
->disk
, 0);
227 kobject_uevent(&nbd_to_dev(nbd
)->kobj
, KOBJ_CHANGE
);
231 static void nbd_size_update(struct nbd_device
*nbd
)
233 struct nbd_config
*config
= nbd
->config
;
234 blk_queue_logical_block_size(nbd
->disk
->queue
, config
->blksize
);
235 blk_queue_physical_block_size(nbd
->disk
->queue
, config
->blksize
);
236 set_capacity(nbd
->disk
, config
->bytesize
>> 9);
237 kobject_uevent(&nbd_to_dev(nbd
)->kobj
, KOBJ_CHANGE
);
240 static void nbd_size_set(struct nbd_device
*nbd
, loff_t blocksize
,
243 struct nbd_config
*config
= nbd
->config
;
244 config
->blksize
= blocksize
;
245 config
->bytesize
= blocksize
* nr_blocks
;
248 static void nbd_complete_rq(struct request
*req
)
250 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
252 dev_dbg(nbd_to_dev(cmd
->nbd
), "request %p: %s\n", cmd
,
253 cmd
->status
? "failed" : "done");
255 blk_mq_end_request(req
, cmd
->status
);
259 * Forcibly shutdown the socket causing all listeners to error
261 static void sock_shutdown(struct nbd_device
*nbd
)
263 struct nbd_config
*config
= nbd
->config
;
266 if (config
->num_connections
== 0)
268 if (test_and_set_bit(NBD_DISCONNECTED
, &config
->runtime_flags
))
271 for (i
= 0; i
< config
->num_connections
; i
++) {
272 struct nbd_sock
*nsock
= config
->socks
[i
];
273 mutex_lock(&nsock
->tx_lock
);
274 nbd_mark_nsock_dead(nbd
, nsock
, 0);
275 mutex_unlock(&nsock
->tx_lock
);
277 dev_warn(disk_to_dev(nbd
->disk
), "shutting down sockets\n");
280 static enum blk_eh_timer_return
nbd_xmit_timeout(struct request
*req
,
283 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
284 struct nbd_device
*nbd
= cmd
->nbd
;
285 struct nbd_config
*config
;
287 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
288 cmd
->status
= BLK_STS_TIMEOUT
;
289 return BLK_EH_HANDLED
;
292 /* If we are waiting on our dead timer then we could get timeout
293 * callbacks for our request. For this we just want to reset the timer
294 * and let the queue side take care of everything.
296 if (!completion_done(&cmd
->send_complete
)) {
298 return BLK_EH_RESET_TIMER
;
300 config
= nbd
->config
;
302 if (config
->num_connections
> 1) {
303 dev_err_ratelimited(nbd_to_dev(nbd
),
304 "Connection timed out, retrying\n");
306 * Hooray we have more connections, requeue this IO, the submit
307 * path will put it on a real connection.
309 if (config
->socks
&& config
->num_connections
> 1) {
310 if (cmd
->index
< config
->num_connections
) {
311 struct nbd_sock
*nsock
=
312 config
->socks
[cmd
->index
];
313 mutex_lock(&nsock
->tx_lock
);
314 /* We can have multiple outstanding requests, so
315 * we don't want to mark the nsock dead if we've
316 * already reconnected with a new socket, so
317 * only mark it dead if its the same socket we
320 if (cmd
->cookie
== nsock
->cookie
)
321 nbd_mark_nsock_dead(nbd
, nsock
, 1);
322 mutex_unlock(&nsock
->tx_lock
);
324 blk_mq_requeue_request(req
, true);
326 return BLK_EH_NOT_HANDLED
;
329 dev_err_ratelimited(nbd_to_dev(nbd
),
330 "Connection timed out\n");
332 set_bit(NBD_TIMEDOUT
, &config
->runtime_flags
);
333 cmd
->status
= BLK_STS_IOERR
;
337 return BLK_EH_HANDLED
;
341 * Send or receive packet.
343 static int sock_xmit(struct nbd_device
*nbd
, int index
, int send
,
344 struct iov_iter
*iter
, int msg_flags
, int *sent
)
346 struct nbd_config
*config
= nbd
->config
;
347 struct socket
*sock
= config
->socks
[index
]->sock
;
350 unsigned int noreclaim_flag
;
352 if (unlikely(!sock
)) {
353 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
354 "Attempted %s on closed socket in sock_xmit\n",
355 (send
? "send" : "recv"));
359 msg
.msg_iter
= *iter
;
361 noreclaim_flag
= memalloc_noreclaim_save();
363 sock
->sk
->sk_allocation
= GFP_NOIO
| __GFP_MEMALLOC
;
366 msg
.msg_control
= NULL
;
367 msg
.msg_controllen
= 0;
368 msg
.msg_flags
= msg_flags
| MSG_NOSIGNAL
;
371 result
= sock_sendmsg(sock
, &msg
);
373 result
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
377 result
= -EPIPE
; /* short read */
382 } while (msg_data_left(&msg
));
384 memalloc_noreclaim_restore(noreclaim_flag
);
390 * Different settings for sk->sk_sndtimeo can result in different return values
391 * if there is a signal pending when we enter sendmsg, because reasons?
393 static inline int was_interrupted(int result
)
395 return result
== -ERESTARTSYS
|| result
== -EINTR
;
398 /* always call with the tx_lock held */
399 static int nbd_send_cmd(struct nbd_device
*nbd
, struct nbd_cmd
*cmd
, int index
)
401 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
402 struct nbd_config
*config
= nbd
->config
;
403 struct nbd_sock
*nsock
= config
->socks
[index
];
405 struct nbd_request request
= {.magic
= htonl(NBD_REQUEST_MAGIC
)};
406 struct kvec iov
= {.iov_base
= &request
, .iov_len
= sizeof(request
)};
407 struct iov_iter from
;
408 unsigned long size
= blk_rq_bytes(req
);
411 u32 nbd_cmd_flags
= 0;
412 u32 tag
= blk_mq_unique_tag(req
);
413 int sent
= nsock
->sent
, skip
= 0;
415 iov_iter_kvec(&from
, WRITE
| ITER_KVEC
, &iov
, 1, sizeof(request
));
417 switch (req_op(req
)) {
422 type
= NBD_CMD_FLUSH
;
425 type
= NBD_CMD_WRITE
;
434 if (rq_data_dir(req
) == WRITE
&&
435 (config
->flags
& NBD_FLAG_READ_ONLY
)) {
436 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
437 "Write on read-only\n");
441 if (req
->cmd_flags
& REQ_FUA
)
442 nbd_cmd_flags
|= NBD_CMD_FLAG_FUA
;
444 /* We did a partial send previously, and we at least sent the whole
445 * request struct, so just go and send the rest of the pages in the
449 if (sent
>= sizeof(request
)) {
450 skip
= sent
- sizeof(request
);
453 iov_iter_advance(&from
, sent
);
456 cmd
->cookie
= nsock
->cookie
;
457 request
.type
= htonl(type
| nbd_cmd_flags
);
458 if (type
!= NBD_CMD_FLUSH
) {
459 request
.from
= cpu_to_be64((u64
)blk_rq_pos(req
) << 9);
460 request
.len
= htonl(size
);
462 memcpy(request
.handle
, &tag
, sizeof(tag
));
464 dev_dbg(nbd_to_dev(nbd
), "request %p: sending control (%s@%llu,%uB)\n",
465 cmd
, nbdcmd_to_ascii(type
),
466 (unsigned long long)blk_rq_pos(req
) << 9, blk_rq_bytes(req
));
467 result
= sock_xmit(nbd
, index
, 1, &from
,
468 (type
== NBD_CMD_WRITE
) ? MSG_MORE
: 0, &sent
);
470 if (was_interrupted(result
)) {
471 /* If we havne't sent anything we can just return BUSY,
472 * however if we have sent something we need to make
473 * sure we only allow this req to be sent until we are
477 nsock
->pending
= req
;
480 return BLK_STS_RESOURCE
;
482 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
483 "Send control failed (result %d)\n", result
);
487 if (type
!= NBD_CMD_WRITE
)
492 struct bio
*next
= bio
->bi_next
;
493 struct bvec_iter iter
;
496 bio_for_each_segment(bvec
, bio
, iter
) {
497 bool is_last
= !next
&& bio_iter_last(bvec
, iter
);
498 int flags
= is_last
? 0 : MSG_MORE
;
500 dev_dbg(nbd_to_dev(nbd
), "request %p: sending %d bytes data\n",
502 iov_iter_bvec(&from
, ITER_BVEC
| WRITE
,
503 &bvec
, 1, bvec
.bv_len
);
505 if (skip
>= iov_iter_count(&from
)) {
506 skip
-= iov_iter_count(&from
);
509 iov_iter_advance(&from
, skip
);
512 result
= sock_xmit(nbd
, index
, 1, &from
, flags
, &sent
);
514 if (was_interrupted(result
)) {
515 /* We've already sent the header, we
516 * have no choice but to set pending and
519 nsock
->pending
= req
;
521 return BLK_STS_RESOURCE
;
523 dev_err(disk_to_dev(nbd
->disk
),
524 "Send data failed (result %d)\n",
529 * The completion might already have come in,
530 * so break for the last one instead of letting
531 * the iterator do it. This prevents use-after-free
540 nsock
->pending
= NULL
;
545 /* NULL returned = something went wrong, inform userspace */
546 static struct nbd_cmd
*nbd_read_stat(struct nbd_device
*nbd
, int index
)
548 struct nbd_config
*config
= nbd
->config
;
550 struct nbd_reply reply
;
552 struct request
*req
= NULL
;
555 struct kvec iov
= {.iov_base
= &reply
, .iov_len
= sizeof(reply
)};
559 iov_iter_kvec(&to
, READ
| ITER_KVEC
, &iov
, 1, sizeof(reply
));
560 result
= sock_xmit(nbd
, index
, 0, &to
, MSG_WAITALL
, NULL
);
562 if (!nbd_disconnected(config
))
563 dev_err(disk_to_dev(nbd
->disk
),
564 "Receive control failed (result %d)\n", result
);
565 return ERR_PTR(result
);
568 if (ntohl(reply
.magic
) != NBD_REPLY_MAGIC
) {
569 dev_err(disk_to_dev(nbd
->disk
), "Wrong magic (0x%lx)\n",
570 (unsigned long)ntohl(reply
.magic
));
571 return ERR_PTR(-EPROTO
);
574 memcpy(&tag
, reply
.handle
, sizeof(u32
));
576 hwq
= blk_mq_unique_tag_to_hwq(tag
);
577 if (hwq
< nbd
->tag_set
.nr_hw_queues
)
578 req
= blk_mq_tag_to_rq(nbd
->tag_set
.tags
[hwq
],
579 blk_mq_unique_tag_to_tag(tag
));
580 if (!req
|| !blk_mq_request_started(req
)) {
581 dev_err(disk_to_dev(nbd
->disk
), "Unexpected reply (%d) %p\n",
583 return ERR_PTR(-ENOENT
);
585 cmd
= blk_mq_rq_to_pdu(req
);
586 if (ntohl(reply
.error
)) {
587 dev_err(disk_to_dev(nbd
->disk
), "Other side returned error (%d)\n",
589 cmd
->status
= BLK_STS_IOERR
;
593 dev_dbg(nbd_to_dev(nbd
), "request %p: got reply\n", cmd
);
594 if (rq_data_dir(req
) != WRITE
) {
595 struct req_iterator iter
;
598 rq_for_each_segment(bvec
, req
, iter
) {
599 iov_iter_bvec(&to
, ITER_BVEC
| READ
,
600 &bvec
, 1, bvec
.bv_len
);
601 result
= sock_xmit(nbd
, index
, 0, &to
, MSG_WAITALL
, NULL
);
603 dev_err(disk_to_dev(nbd
->disk
), "Receive data failed (result %d)\n",
606 * If we've disconnected or we only have 1
607 * connection then we need to make sure we
608 * complete this request, otherwise error out
609 * and let the timeout stuff handle resubmitting
610 * this request onto another connection.
612 if (nbd_disconnected(config
) ||
613 config
->num_connections
<= 1) {
614 cmd
->status
= BLK_STS_IOERR
;
617 return ERR_PTR(-EIO
);
619 dev_dbg(nbd_to_dev(nbd
), "request %p: got %d bytes data\n",
623 /* See the comment in nbd_queue_rq. */
624 wait_for_completion(&cmd
->send_complete
);
629 static void recv_work(struct work_struct
*work
)
631 struct recv_thread_args
*args
= container_of(work
,
632 struct recv_thread_args
,
634 struct nbd_device
*nbd
= args
->nbd
;
635 struct nbd_config
*config
= nbd
->config
;
639 cmd
= nbd_read_stat(nbd
, args
->index
);
641 struct nbd_sock
*nsock
= config
->socks
[args
->index
];
643 mutex_lock(&nsock
->tx_lock
);
644 nbd_mark_nsock_dead(nbd
, nsock
, 1);
645 mutex_unlock(&nsock
->tx_lock
);
649 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd
));
651 atomic_dec(&config
->recv_threads
);
652 wake_up(&config
->recv_wq
);
657 static void nbd_clear_req(struct request
*req
, void *data
, bool reserved
)
661 if (!blk_mq_request_started(req
))
663 cmd
= blk_mq_rq_to_pdu(req
);
664 cmd
->status
= BLK_STS_IOERR
;
665 blk_mq_complete_request(req
);
668 static void nbd_clear_que(struct nbd_device
*nbd
)
670 blk_mq_quiesce_queue(nbd
->disk
->queue
);
671 blk_mq_tagset_busy_iter(&nbd
->tag_set
, nbd_clear_req
, NULL
);
672 blk_mq_unquiesce_queue(nbd
->disk
->queue
);
673 dev_dbg(disk_to_dev(nbd
->disk
), "queue cleared\n");
676 static int find_fallback(struct nbd_device
*nbd
, int index
)
678 struct nbd_config
*config
= nbd
->config
;
680 struct nbd_sock
*nsock
= config
->socks
[index
];
681 int fallback
= nsock
->fallback_index
;
683 if (test_bit(NBD_DISCONNECTED
, &config
->runtime_flags
))
686 if (config
->num_connections
<= 1) {
687 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
688 "Attempted send on invalid socket\n");
692 if (fallback
>= 0 && fallback
< config
->num_connections
&&
693 !config
->socks
[fallback
]->dead
)
696 if (nsock
->fallback_index
< 0 ||
697 nsock
->fallback_index
>= config
->num_connections
||
698 config
->socks
[nsock
->fallback_index
]->dead
) {
700 for (i
= 0; i
< config
->num_connections
; i
++) {
703 if (!config
->socks
[i
]->dead
) {
708 nsock
->fallback_index
= new_index
;
710 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
711 "Dead connection, failed to find a fallback\n");
715 new_index
= nsock
->fallback_index
;
719 static int wait_for_reconnect(struct nbd_device
*nbd
)
721 struct nbd_config
*config
= nbd
->config
;
722 if (!config
->dead_conn_timeout
)
724 if (test_bit(NBD_DISCONNECTED
, &config
->runtime_flags
))
726 wait_event_interruptible_timeout(config
->conn_wait
,
727 atomic_read(&config
->live_connections
),
728 config
->dead_conn_timeout
);
729 return atomic_read(&config
->live_connections
);
732 static int nbd_handle_cmd(struct nbd_cmd
*cmd
, int index
)
734 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
735 struct nbd_device
*nbd
= cmd
->nbd
;
736 struct nbd_config
*config
;
737 struct nbd_sock
*nsock
;
740 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
741 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
742 "Socks array is empty\n");
745 config
= nbd
->config
;
747 if (index
>= config
->num_connections
) {
748 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
749 "Attempted send on invalid socket\n");
753 cmd
->status
= BLK_STS_OK
;
755 nsock
= config
->socks
[index
];
756 mutex_lock(&nsock
->tx_lock
);
758 int old_index
= index
;
759 index
= find_fallback(nbd
, index
);
760 mutex_unlock(&nsock
->tx_lock
);
762 if (wait_for_reconnect(nbd
)) {
766 /* All the sockets should already be down at this point,
767 * we just want to make sure that DISCONNECTED is set so
768 * any requests that come in that were queue'ed waiting
769 * for the reconnect timer don't trigger the timer again
770 * and instead just error out.
779 /* Handle the case that we have a pending request that was partially
780 * transmitted that _has_ to be serviced first. We need to call requeue
781 * here so that it gets put _after_ the request that is already on the
784 if (unlikely(nsock
->pending
&& nsock
->pending
!= req
)) {
785 blk_mq_requeue_request(req
, true);
790 * Some failures are related to the link going down, so anything that
791 * returns EAGAIN can be retried on a different socket.
793 ret
= nbd_send_cmd(nbd
, cmd
, index
);
794 if (ret
== -EAGAIN
) {
795 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
796 "Request send failed trying another connection\n");
797 nbd_mark_nsock_dead(nbd
, nsock
, 1);
798 mutex_unlock(&nsock
->tx_lock
);
802 mutex_unlock(&nsock
->tx_lock
);
807 static blk_status_t
nbd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
808 const struct blk_mq_queue_data
*bd
)
810 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(bd
->rq
);
814 * Since we look at the bio's to send the request over the network we
815 * need to make sure the completion work doesn't mark this request done
816 * before we are done doing our send. This keeps us from dereferencing
817 * freed data if we have particularly fast completions (ie we get the
818 * completion before we exit sock_xmit on the last bvec) or in the case
819 * that the server is misbehaving (or there was an error) before we're
820 * done sending everything over the wire.
822 init_completion(&cmd
->send_complete
);
823 blk_mq_start_request(bd
->rq
);
825 /* We can be called directly from the user space process, which means we
826 * could possibly have signals pending so our sendmsg will fail. In
827 * this case we need to return that we are busy, otherwise error out as
830 ret
= nbd_handle_cmd(cmd
, hctx
->queue_num
);
831 complete(&cmd
->send_complete
);
833 return ret
< 0 ? BLK_STS_IOERR
: BLK_STS_OK
;
836 static int nbd_add_socket(struct nbd_device
*nbd
, unsigned long arg
,
839 struct nbd_config
*config
= nbd
->config
;
841 struct nbd_sock
**socks
;
842 struct nbd_sock
*nsock
;
845 sock
= sockfd_lookup(arg
, &err
);
849 if (!netlink
&& !nbd
->task_setup
&&
850 !test_bit(NBD_BOUND
, &config
->runtime_flags
))
851 nbd
->task_setup
= current
;
854 (nbd
->task_setup
!= current
||
855 test_bit(NBD_BOUND
, &config
->runtime_flags
))) {
856 dev_err(disk_to_dev(nbd
->disk
),
857 "Device being setup by another task");
862 socks
= krealloc(config
->socks
, (config
->num_connections
+ 1) *
863 sizeof(struct nbd_sock
*), GFP_KERNEL
);
868 nsock
= kzalloc(sizeof(struct nbd_sock
), GFP_KERNEL
);
874 config
->socks
= socks
;
876 nsock
->fallback_index
= -1;
878 mutex_init(&nsock
->tx_lock
);
880 nsock
->pending
= NULL
;
883 socks
[config
->num_connections
++] = nsock
;
884 atomic_inc(&config
->live_connections
);
889 static int nbd_reconnect_socket(struct nbd_device
*nbd
, unsigned long arg
)
891 struct nbd_config
*config
= nbd
->config
;
892 struct socket
*sock
, *old
;
893 struct recv_thread_args
*args
;
897 sock
= sockfd_lookup(arg
, &err
);
901 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
907 for (i
= 0; i
< config
->num_connections
; i
++) {
908 struct nbd_sock
*nsock
= config
->socks
[i
];
913 mutex_lock(&nsock
->tx_lock
);
915 mutex_unlock(&nsock
->tx_lock
);
918 sk_set_memalloc(sock
->sk
);
919 if (nbd
->tag_set
.timeout
)
920 sock
->sk
->sk_sndtimeo
= nbd
->tag_set
.timeout
;
921 atomic_inc(&config
->recv_threads
);
922 refcount_inc(&nbd
->config_refs
);
924 nsock
->fallback_index
= -1;
927 INIT_WORK(&args
->work
, recv_work
);
931 mutex_unlock(&nsock
->tx_lock
);
934 clear_bit(NBD_DISCONNECTED
, &config
->runtime_flags
);
936 /* We take the tx_mutex in an error path in the recv_work, so we
937 * need to queue_work outside of the tx_mutex.
939 queue_work(recv_workqueue
, &args
->work
);
941 atomic_inc(&config
->live_connections
);
942 wake_up(&config
->conn_wait
);
950 static void nbd_bdev_reset(struct block_device
*bdev
)
952 if (bdev
->bd_openers
> 1)
954 bd_set_size(bdev
, 0);
956 blkdev_reread_part(bdev
);
957 bdev
->bd_invalidated
= 1;
961 static void nbd_parse_flags(struct nbd_device
*nbd
)
963 struct nbd_config
*config
= nbd
->config
;
964 if (config
->flags
& NBD_FLAG_READ_ONLY
)
965 set_disk_ro(nbd
->disk
, true);
967 set_disk_ro(nbd
->disk
, false);
968 if (config
->flags
& NBD_FLAG_SEND_TRIM
)
969 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
970 if (config
->flags
& NBD_FLAG_SEND_FLUSH
) {
971 if (config
->flags
& NBD_FLAG_SEND_FUA
)
972 blk_queue_write_cache(nbd
->disk
->queue
, true, true);
974 blk_queue_write_cache(nbd
->disk
->queue
, true, false);
977 blk_queue_write_cache(nbd
->disk
->queue
, false, false);
980 static void send_disconnects(struct nbd_device
*nbd
)
982 struct nbd_config
*config
= nbd
->config
;
983 struct nbd_request request
= {
984 .magic
= htonl(NBD_REQUEST_MAGIC
),
985 .type
= htonl(NBD_CMD_DISC
),
987 struct kvec iov
= {.iov_base
= &request
, .iov_len
= sizeof(request
)};
988 struct iov_iter from
;
991 for (i
= 0; i
< config
->num_connections
; i
++) {
992 struct nbd_sock
*nsock
= config
->socks
[i
];
994 iov_iter_kvec(&from
, WRITE
| ITER_KVEC
, &iov
, 1, sizeof(request
));
995 mutex_lock(&nsock
->tx_lock
);
996 ret
= sock_xmit(nbd
, i
, 1, &from
, 0, NULL
);
998 dev_err(disk_to_dev(nbd
->disk
),
999 "Send disconnect failed %d\n", ret
);
1000 mutex_unlock(&nsock
->tx_lock
);
1004 static int nbd_disconnect(struct nbd_device
*nbd
)
1006 struct nbd_config
*config
= nbd
->config
;
1008 dev_info(disk_to_dev(nbd
->disk
), "NBD_DISCONNECT\n");
1009 set_bit(NBD_DISCONNECT_REQUESTED
, &config
->runtime_flags
);
1010 send_disconnects(nbd
);
1014 static void nbd_clear_sock(struct nbd_device
*nbd
)
1018 nbd
->task_setup
= NULL
;
1021 static void nbd_config_put(struct nbd_device
*nbd
)
1023 if (refcount_dec_and_mutex_lock(&nbd
->config_refs
,
1024 &nbd
->config_lock
)) {
1025 struct nbd_config
*config
= nbd
->config
;
1026 nbd_dev_dbg_close(nbd
);
1027 nbd_size_clear(nbd
);
1028 if (test_and_clear_bit(NBD_HAS_PID_FILE
,
1029 &config
->runtime_flags
))
1030 device_remove_file(disk_to_dev(nbd
->disk
), &pid_attr
);
1031 nbd
->task_recv
= NULL
;
1032 nbd_clear_sock(nbd
);
1033 if (config
->num_connections
) {
1035 for (i
= 0; i
< config
->num_connections
; i
++) {
1036 sockfd_put(config
->socks
[i
]->sock
);
1037 kfree(config
->socks
[i
]);
1039 kfree(config
->socks
);
1044 nbd
->tag_set
.timeout
= 0;
1045 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
1047 mutex_unlock(&nbd
->config_lock
);
1049 module_put(THIS_MODULE
);
1053 static int nbd_start_device(struct nbd_device
*nbd
)
1055 struct nbd_config
*config
= nbd
->config
;
1056 int num_connections
= config
->num_connections
;
1063 if (num_connections
> 1 &&
1064 !(config
->flags
& NBD_FLAG_CAN_MULTI_CONN
)) {
1065 dev_err(disk_to_dev(nbd
->disk
), "server does not support multiple connections per device.\n");
1069 blk_mq_update_nr_hw_queues(&nbd
->tag_set
, config
->num_connections
);
1070 nbd
->task_recv
= current
;
1072 nbd_parse_flags(nbd
);
1074 error
= device_create_file(disk_to_dev(nbd
->disk
), &pid_attr
);
1076 dev_err(disk_to_dev(nbd
->disk
), "device_create_file failed!\n");
1079 set_bit(NBD_HAS_PID_FILE
, &config
->runtime_flags
);
1081 nbd_dev_dbg_init(nbd
);
1082 for (i
= 0; i
< num_connections
; i
++) {
1083 struct recv_thread_args
*args
;
1085 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
1090 sk_set_memalloc(config
->socks
[i
]->sock
->sk
);
1091 if (nbd
->tag_set
.timeout
)
1092 config
->socks
[i
]->sock
->sk
->sk_sndtimeo
=
1093 nbd
->tag_set
.timeout
;
1094 atomic_inc(&config
->recv_threads
);
1095 refcount_inc(&nbd
->config_refs
);
1096 INIT_WORK(&args
->work
, recv_work
);
1099 queue_work(recv_workqueue
, &args
->work
);
1101 nbd_size_update(nbd
);
1105 static int nbd_start_device_ioctl(struct nbd_device
*nbd
, struct block_device
*bdev
)
1107 struct nbd_config
*config
= nbd
->config
;
1110 ret
= nbd_start_device(nbd
);
1114 bd_set_size(bdev
, config
->bytesize
);
1116 bdev
->bd_invalidated
= 1;
1117 mutex_unlock(&nbd
->config_lock
);
1118 ret
= wait_event_interruptible(config
->recv_wq
,
1119 atomic_read(&config
->recv_threads
) == 0);
1122 mutex_lock(&nbd
->config_lock
);
1123 bd_set_size(bdev
, 0);
1124 /* user requested, ignore socket errors */
1125 if (test_bit(NBD_DISCONNECT_REQUESTED
, &config
->runtime_flags
))
1127 if (test_bit(NBD_TIMEDOUT
, &config
->runtime_flags
))
1132 static void nbd_clear_sock_ioctl(struct nbd_device
*nbd
,
1133 struct block_device
*bdev
)
1137 nbd_bdev_reset(bdev
);
1138 if (test_and_clear_bit(NBD_HAS_CONFIG_REF
,
1139 &nbd
->config
->runtime_flags
))
1140 nbd_config_put(nbd
);
1143 /* Must be called with config_lock held */
1144 static int __nbd_ioctl(struct block_device
*bdev
, struct nbd_device
*nbd
,
1145 unsigned int cmd
, unsigned long arg
)
1147 struct nbd_config
*config
= nbd
->config
;
1150 case NBD_DISCONNECT
:
1151 return nbd_disconnect(nbd
);
1152 case NBD_CLEAR_SOCK
:
1153 nbd_clear_sock_ioctl(nbd
, bdev
);
1156 return nbd_add_socket(nbd
, arg
, false);
1157 case NBD_SET_BLKSIZE
:
1158 nbd_size_set(nbd
, arg
,
1159 div_s64(config
->bytesize
, arg
));
1162 nbd_size_set(nbd
, config
->blksize
,
1163 div_s64(arg
, config
->blksize
));
1165 case NBD_SET_SIZE_BLOCKS
:
1166 nbd_size_set(nbd
, config
->blksize
, arg
);
1168 case NBD_SET_TIMEOUT
:
1170 nbd
->tag_set
.timeout
= arg
* HZ
;
1171 blk_queue_rq_timeout(nbd
->disk
->queue
, arg
* HZ
);
1176 config
->flags
= arg
;
1179 return nbd_start_device_ioctl(nbd
, bdev
);
1182 * This is for compatibility only. The queue is always cleared
1183 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1186 case NBD_PRINT_DEBUG
:
1188 * For compatibility only, we no longer keep a list of
1189 * outstanding requests.
1196 static int nbd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1197 unsigned int cmd
, unsigned long arg
)
1199 struct nbd_device
*nbd
= bdev
->bd_disk
->private_data
;
1200 struct nbd_config
*config
= nbd
->config
;
1201 int error
= -EINVAL
;
1203 if (!capable(CAP_SYS_ADMIN
))
1206 mutex_lock(&nbd
->config_lock
);
1208 /* Don't allow ioctl operations on a nbd device that was created with
1209 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1211 if (!test_bit(NBD_BOUND
, &config
->runtime_flags
) ||
1212 (cmd
== NBD_DISCONNECT
|| cmd
== NBD_CLEAR_SOCK
))
1213 error
= __nbd_ioctl(bdev
, nbd
, cmd
, arg
);
1215 dev_err(nbd_to_dev(nbd
), "Cannot use ioctl interface on a netlink controlled device.\n");
1216 mutex_unlock(&nbd
->config_lock
);
1220 static struct nbd_config
*nbd_alloc_config(void)
1222 struct nbd_config
*config
;
1224 config
= kzalloc(sizeof(struct nbd_config
), GFP_NOFS
);
1227 atomic_set(&config
->recv_threads
, 0);
1228 init_waitqueue_head(&config
->recv_wq
);
1229 init_waitqueue_head(&config
->conn_wait
);
1230 config
->blksize
= 1024;
1231 atomic_set(&config
->live_connections
, 0);
1232 try_module_get(THIS_MODULE
);
1236 static int nbd_open(struct block_device
*bdev
, fmode_t mode
)
1238 struct nbd_device
*nbd
;
1241 mutex_lock(&nbd_index_mutex
);
1242 nbd
= bdev
->bd_disk
->private_data
;
1247 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1251 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1252 struct nbd_config
*config
;
1254 mutex_lock(&nbd
->config_lock
);
1255 if (refcount_inc_not_zero(&nbd
->config_refs
)) {
1256 mutex_unlock(&nbd
->config_lock
);
1259 config
= nbd
->config
= nbd_alloc_config();
1262 mutex_unlock(&nbd
->config_lock
);
1265 refcount_set(&nbd
->config_refs
, 1);
1266 refcount_inc(&nbd
->refs
);
1267 mutex_unlock(&nbd
->config_lock
);
1270 mutex_unlock(&nbd_index_mutex
);
1274 static void nbd_release(struct gendisk
*disk
, fmode_t mode
)
1276 struct nbd_device
*nbd
= disk
->private_data
;
1277 nbd_config_put(nbd
);
1281 static const struct block_device_operations nbd_fops
=
1283 .owner
= THIS_MODULE
,
1285 .release
= nbd_release
,
1287 .compat_ioctl
= nbd_ioctl
,
1290 #if IS_ENABLED(CONFIG_DEBUG_FS)
1292 static int nbd_dbg_tasks_show(struct seq_file
*s
, void *unused
)
1294 struct nbd_device
*nbd
= s
->private;
1297 seq_printf(s
, "recv: %d\n", task_pid_nr(nbd
->task_recv
));
1302 static int nbd_dbg_tasks_open(struct inode
*inode
, struct file
*file
)
1304 return single_open(file
, nbd_dbg_tasks_show
, inode
->i_private
);
1307 static const struct file_operations nbd_dbg_tasks_ops
= {
1308 .open
= nbd_dbg_tasks_open
,
1310 .llseek
= seq_lseek
,
1311 .release
= single_release
,
1314 static int nbd_dbg_flags_show(struct seq_file
*s
, void *unused
)
1316 struct nbd_device
*nbd
= s
->private;
1317 u32 flags
= nbd
->config
->flags
;
1319 seq_printf(s
, "Hex: 0x%08x\n\n", flags
);
1321 seq_puts(s
, "Known flags:\n");
1323 if (flags
& NBD_FLAG_HAS_FLAGS
)
1324 seq_puts(s
, "NBD_FLAG_HAS_FLAGS\n");
1325 if (flags
& NBD_FLAG_READ_ONLY
)
1326 seq_puts(s
, "NBD_FLAG_READ_ONLY\n");
1327 if (flags
& NBD_FLAG_SEND_FLUSH
)
1328 seq_puts(s
, "NBD_FLAG_SEND_FLUSH\n");
1329 if (flags
& NBD_FLAG_SEND_FUA
)
1330 seq_puts(s
, "NBD_FLAG_SEND_FUA\n");
1331 if (flags
& NBD_FLAG_SEND_TRIM
)
1332 seq_puts(s
, "NBD_FLAG_SEND_TRIM\n");
1337 static int nbd_dbg_flags_open(struct inode
*inode
, struct file
*file
)
1339 return single_open(file
, nbd_dbg_flags_show
, inode
->i_private
);
1342 static const struct file_operations nbd_dbg_flags_ops
= {
1343 .open
= nbd_dbg_flags_open
,
1345 .llseek
= seq_lseek
,
1346 .release
= single_release
,
1349 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
1352 struct nbd_config
*config
= nbd
->config
;
1357 dir
= debugfs_create_dir(nbd_name(nbd
), nbd_dbg_dir
);
1359 dev_err(nbd_to_dev(nbd
), "Failed to create debugfs dir for '%s'\n",
1363 config
->dbg_dir
= dir
;
1365 debugfs_create_file("tasks", 0444, dir
, nbd
, &nbd_dbg_tasks_ops
);
1366 debugfs_create_u64("size_bytes", 0444, dir
, &config
->bytesize
);
1367 debugfs_create_u32("timeout", 0444, dir
, &nbd
->tag_set
.timeout
);
1368 debugfs_create_u64("blocksize", 0444, dir
, &config
->blksize
);
1369 debugfs_create_file("flags", 0444, dir
, nbd
, &nbd_dbg_flags_ops
);
1374 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
1376 debugfs_remove_recursive(nbd
->config
->dbg_dir
);
1379 static int nbd_dbg_init(void)
1381 struct dentry
*dbg_dir
;
1383 dbg_dir
= debugfs_create_dir("nbd", NULL
);
1387 nbd_dbg_dir
= dbg_dir
;
1392 static void nbd_dbg_close(void)
1394 debugfs_remove_recursive(nbd_dbg_dir
);
1397 #else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1399 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
1404 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
1408 static int nbd_dbg_init(void)
1413 static void nbd_dbg_close(void)
1419 static int nbd_init_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
1420 unsigned int hctx_idx
, unsigned int numa_node
)
1422 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
1423 cmd
->nbd
= set
->driver_data
;
1427 static const struct blk_mq_ops nbd_mq_ops
= {
1428 .queue_rq
= nbd_queue_rq
,
1429 .complete
= nbd_complete_rq
,
1430 .init_request
= nbd_init_request
,
1431 .timeout
= nbd_xmit_timeout
,
1434 static int nbd_dev_add(int index
)
1436 struct nbd_device
*nbd
;
1437 struct gendisk
*disk
;
1438 struct request_queue
*q
;
1441 nbd
= kzalloc(sizeof(struct nbd_device
), GFP_KERNEL
);
1445 disk
= alloc_disk(1 << part_shift
);
1450 err
= idr_alloc(&nbd_index_idr
, nbd
, index
, index
+ 1,
1455 err
= idr_alloc(&nbd_index_idr
, nbd
, 0, 0, GFP_KERNEL
);
1464 nbd
->tag_set
.ops
= &nbd_mq_ops
;
1465 nbd
->tag_set
.nr_hw_queues
= 1;
1466 nbd
->tag_set
.queue_depth
= 128;
1467 nbd
->tag_set
.numa_node
= NUMA_NO_NODE
;
1468 nbd
->tag_set
.cmd_size
= sizeof(struct nbd_cmd
);
1469 nbd
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
|
1470 BLK_MQ_F_SG_MERGE
| BLK_MQ_F_BLOCKING
;
1471 nbd
->tag_set
.driver_data
= nbd
;
1473 err
= blk_mq_alloc_tag_set(&nbd
->tag_set
);
1477 q
= blk_mq_init_queue(&nbd
->tag_set
);
1485 * Tell the block layer that we are not a rotational device
1487 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, disk
->queue
);
1488 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, disk
->queue
);
1489 disk
->queue
->limits
.discard_granularity
= 512;
1490 blk_queue_max_discard_sectors(disk
->queue
, UINT_MAX
);
1491 blk_queue_max_segment_size(disk
->queue
, UINT_MAX
);
1492 blk_queue_max_segments(disk
->queue
, USHRT_MAX
);
1493 blk_queue_max_hw_sectors(disk
->queue
, 65536);
1494 disk
->queue
->limits
.max_sectors
= 256;
1496 mutex_init(&nbd
->config_lock
);
1497 refcount_set(&nbd
->config_refs
, 0);
1498 refcount_set(&nbd
->refs
, 1);
1499 INIT_LIST_HEAD(&nbd
->list
);
1500 disk
->major
= NBD_MAJOR
;
1501 disk
->first_minor
= index
<< part_shift
;
1502 disk
->fops
= &nbd_fops
;
1503 disk
->private_data
= nbd
;
1504 sprintf(disk
->disk_name
, "nbd%d", index
);
1506 nbd_total_devices
++;
1510 blk_mq_free_tag_set(&nbd
->tag_set
);
1512 idr_remove(&nbd_index_idr
, index
);
1521 static int find_free_cb(int id
, void *ptr
, void *data
)
1523 struct nbd_device
*nbd
= ptr
;
1524 struct nbd_device
**found
= data
;
1526 if (!refcount_read(&nbd
->config_refs
)) {
1533 /* Netlink interface. */
1534 static struct nla_policy nbd_attr_policy
[NBD_ATTR_MAX
+ 1] = {
1535 [NBD_ATTR_INDEX
] = { .type
= NLA_U32
},
1536 [NBD_ATTR_SIZE_BYTES
] = { .type
= NLA_U64
},
1537 [NBD_ATTR_BLOCK_SIZE_BYTES
] = { .type
= NLA_U64
},
1538 [NBD_ATTR_TIMEOUT
] = { .type
= NLA_U64
},
1539 [NBD_ATTR_SERVER_FLAGS
] = { .type
= NLA_U64
},
1540 [NBD_ATTR_CLIENT_FLAGS
] = { .type
= NLA_U64
},
1541 [NBD_ATTR_SOCKETS
] = { .type
= NLA_NESTED
},
1542 [NBD_ATTR_DEAD_CONN_TIMEOUT
] = { .type
= NLA_U64
},
1543 [NBD_ATTR_DEVICE_LIST
] = { .type
= NLA_NESTED
},
1546 static struct nla_policy nbd_sock_policy
[NBD_SOCK_MAX
+ 1] = {
1547 [NBD_SOCK_FD
] = { .type
= NLA_U32
},
1550 /* We don't use this right now since we don't parse the incoming list, but we
1551 * still want it here so userspace knows what to expect.
1553 static struct nla_policy
__attribute__((unused
))
1554 nbd_device_policy
[NBD_DEVICE_ATTR_MAX
+ 1] = {
1555 [NBD_DEVICE_INDEX
] = { .type
= NLA_U32
},
1556 [NBD_DEVICE_CONNECTED
] = { .type
= NLA_U8
},
1559 static int nbd_genl_connect(struct sk_buff
*skb
, struct genl_info
*info
)
1561 struct nbd_device
*nbd
= NULL
;
1562 struct nbd_config
*config
;
1565 bool put_dev
= false;
1567 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1570 if (info
->attrs
[NBD_ATTR_INDEX
])
1571 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1572 if (!info
->attrs
[NBD_ATTR_SOCKETS
]) {
1573 printk(KERN_ERR
"nbd: must specify at least one socket\n");
1576 if (!info
->attrs
[NBD_ATTR_SIZE_BYTES
]) {
1577 printk(KERN_ERR
"nbd: must specify a size in bytes for the device\n");
1581 mutex_lock(&nbd_index_mutex
);
1583 ret
= idr_for_each(&nbd_index_idr
, &find_free_cb
, &nbd
);
1586 new_index
= nbd_dev_add(-1);
1587 if (new_index
< 0) {
1588 mutex_unlock(&nbd_index_mutex
);
1589 printk(KERN_ERR
"nbd: failed to add new device\n");
1592 nbd
= idr_find(&nbd_index_idr
, new_index
);
1595 nbd
= idr_find(&nbd_index_idr
, index
);
1598 printk(KERN_ERR
"nbd: couldn't find device at index %d\n",
1600 mutex_unlock(&nbd_index_mutex
);
1603 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1604 mutex_unlock(&nbd_index_mutex
);
1607 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1611 mutex_unlock(&nbd_index_mutex
);
1613 mutex_lock(&nbd
->config_lock
);
1614 if (refcount_read(&nbd
->config_refs
)) {
1615 mutex_unlock(&nbd
->config_lock
);
1619 printk(KERN_ERR
"nbd: nbd%d already in use\n", index
);
1622 if (WARN_ON(nbd
->config
)) {
1623 mutex_unlock(&nbd
->config_lock
);
1627 config
= nbd
->config
= nbd_alloc_config();
1629 mutex_unlock(&nbd
->config_lock
);
1631 printk(KERN_ERR
"nbd: couldn't allocate config\n");
1634 refcount_set(&nbd
->config_refs
, 1);
1635 set_bit(NBD_BOUND
, &config
->runtime_flags
);
1637 if (info
->attrs
[NBD_ATTR_SIZE_BYTES
]) {
1638 u64 bytes
= nla_get_u64(info
->attrs
[NBD_ATTR_SIZE_BYTES
]);
1639 nbd_size_set(nbd
, config
->blksize
,
1640 div64_u64(bytes
, config
->blksize
));
1642 if (info
->attrs
[NBD_ATTR_BLOCK_SIZE_BYTES
]) {
1644 nla_get_u64(info
->attrs
[NBD_ATTR_BLOCK_SIZE_BYTES
]);
1645 nbd_size_set(nbd
, bsize
, div64_u64(config
->bytesize
, bsize
));
1647 if (info
->attrs
[NBD_ATTR_TIMEOUT
]) {
1648 u64 timeout
= nla_get_u64(info
->attrs
[NBD_ATTR_TIMEOUT
]);
1649 nbd
->tag_set
.timeout
= timeout
* HZ
;
1650 blk_queue_rq_timeout(nbd
->disk
->queue
, timeout
* HZ
);
1652 if (info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]) {
1653 config
->dead_conn_timeout
=
1654 nla_get_u64(info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]);
1655 config
->dead_conn_timeout
*= HZ
;
1657 if (info
->attrs
[NBD_ATTR_SERVER_FLAGS
])
1659 nla_get_u64(info
->attrs
[NBD_ATTR_SERVER_FLAGS
]);
1660 if (info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]) {
1661 u64 flags
= nla_get_u64(info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]);
1662 if (flags
& NBD_CFLAG_DESTROY_ON_DISCONNECT
) {
1663 set_bit(NBD_DESTROY_ON_DISCONNECT
,
1664 &config
->runtime_flags
);
1669 if (info
->attrs
[NBD_ATTR_SOCKETS
]) {
1670 struct nlattr
*attr
;
1673 nla_for_each_nested(attr
, info
->attrs
[NBD_ATTR_SOCKETS
],
1675 struct nlattr
*socks
[NBD_SOCK_MAX
+1];
1677 if (nla_type(attr
) != NBD_SOCK_ITEM
) {
1678 printk(KERN_ERR
"nbd: socks must be embedded in a SOCK_ITEM attr\n");
1682 ret
= nla_parse_nested(socks
, NBD_SOCK_MAX
, attr
,
1683 nbd_sock_policy
, info
->extack
);
1685 printk(KERN_ERR
"nbd: error processing sock list\n");
1689 if (!socks
[NBD_SOCK_FD
])
1691 fd
= (int)nla_get_u32(socks
[NBD_SOCK_FD
]);
1692 ret
= nbd_add_socket(nbd
, fd
, true);
1697 ret
= nbd_start_device(nbd
);
1699 mutex_unlock(&nbd
->config_lock
);
1701 set_bit(NBD_HAS_CONFIG_REF
, &config
->runtime_flags
);
1702 refcount_inc(&nbd
->config_refs
);
1703 nbd_connect_reply(info
, nbd
->index
);
1705 nbd_config_put(nbd
);
1711 static int nbd_genl_disconnect(struct sk_buff
*skb
, struct genl_info
*info
)
1713 struct nbd_device
*nbd
;
1716 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1719 if (!info
->attrs
[NBD_ATTR_INDEX
]) {
1720 printk(KERN_ERR
"nbd: must specify an index to disconnect\n");
1723 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1724 mutex_lock(&nbd_index_mutex
);
1725 nbd
= idr_find(&nbd_index_idr
, index
);
1727 mutex_unlock(&nbd_index_mutex
);
1728 printk(KERN_ERR
"nbd: couldn't find device at index %d\n",
1732 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1733 mutex_unlock(&nbd_index_mutex
);
1734 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1738 mutex_unlock(&nbd_index_mutex
);
1739 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1743 mutex_lock(&nbd
->config_lock
);
1744 nbd_disconnect(nbd
);
1745 mutex_unlock(&nbd
->config_lock
);
1746 if (test_and_clear_bit(NBD_HAS_CONFIG_REF
,
1747 &nbd
->config
->runtime_flags
))
1748 nbd_config_put(nbd
);
1749 nbd_config_put(nbd
);
1754 static int nbd_genl_reconfigure(struct sk_buff
*skb
, struct genl_info
*info
)
1756 struct nbd_device
*nbd
= NULL
;
1757 struct nbd_config
*config
;
1760 bool put_dev
= false;
1762 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1765 if (!info
->attrs
[NBD_ATTR_INDEX
]) {
1766 printk(KERN_ERR
"nbd: must specify a device to reconfigure\n");
1769 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1770 mutex_lock(&nbd_index_mutex
);
1771 nbd
= idr_find(&nbd_index_idr
, index
);
1773 mutex_unlock(&nbd_index_mutex
);
1774 printk(KERN_ERR
"nbd: couldn't find a device at index %d\n",
1778 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1779 mutex_unlock(&nbd_index_mutex
);
1780 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1784 mutex_unlock(&nbd_index_mutex
);
1786 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1787 dev_err(nbd_to_dev(nbd
),
1788 "not configured, cannot reconfigure\n");
1793 mutex_lock(&nbd
->config_lock
);
1794 config
= nbd
->config
;
1795 if (!test_bit(NBD_BOUND
, &config
->runtime_flags
) ||
1797 dev_err(nbd_to_dev(nbd
),
1798 "not configured, cannot reconfigure\n");
1802 if (info
->attrs
[NBD_ATTR_TIMEOUT
]) {
1803 u64 timeout
= nla_get_u64(info
->attrs
[NBD_ATTR_TIMEOUT
]);
1804 nbd
->tag_set
.timeout
= timeout
* HZ
;
1805 blk_queue_rq_timeout(nbd
->disk
->queue
, timeout
* HZ
);
1807 if (info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]) {
1808 config
->dead_conn_timeout
=
1809 nla_get_u64(info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]);
1810 config
->dead_conn_timeout
*= HZ
;
1812 if (info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]) {
1813 u64 flags
= nla_get_u64(info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]);
1814 if (flags
& NBD_CFLAG_DESTROY_ON_DISCONNECT
) {
1815 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT
,
1816 &config
->runtime_flags
))
1819 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT
,
1820 &config
->runtime_flags
))
1821 refcount_inc(&nbd
->refs
);
1825 if (info
->attrs
[NBD_ATTR_SOCKETS
]) {
1826 struct nlattr
*attr
;
1829 nla_for_each_nested(attr
, info
->attrs
[NBD_ATTR_SOCKETS
],
1831 struct nlattr
*socks
[NBD_SOCK_MAX
+1];
1833 if (nla_type(attr
) != NBD_SOCK_ITEM
) {
1834 printk(KERN_ERR
"nbd: socks must be embedded in a SOCK_ITEM attr\n");
1838 ret
= nla_parse_nested(socks
, NBD_SOCK_MAX
, attr
,
1839 nbd_sock_policy
, info
->extack
);
1841 printk(KERN_ERR
"nbd: error processing sock list\n");
1845 if (!socks
[NBD_SOCK_FD
])
1847 fd
= (int)nla_get_u32(socks
[NBD_SOCK_FD
]);
1848 ret
= nbd_reconnect_socket(nbd
, fd
);
1854 dev_info(nbd_to_dev(nbd
), "reconnected socket\n");
1858 mutex_unlock(&nbd
->config_lock
);
1859 nbd_config_put(nbd
);
1866 static const struct genl_ops nbd_connect_genl_ops
[] = {
1868 .cmd
= NBD_CMD_CONNECT
,
1869 .policy
= nbd_attr_policy
,
1870 .doit
= nbd_genl_connect
,
1873 .cmd
= NBD_CMD_DISCONNECT
,
1874 .policy
= nbd_attr_policy
,
1875 .doit
= nbd_genl_disconnect
,
1878 .cmd
= NBD_CMD_RECONFIGURE
,
1879 .policy
= nbd_attr_policy
,
1880 .doit
= nbd_genl_reconfigure
,
1883 .cmd
= NBD_CMD_STATUS
,
1884 .policy
= nbd_attr_policy
,
1885 .doit
= nbd_genl_status
,
1889 static const struct genl_multicast_group nbd_mcast_grps
[] = {
1890 { .name
= NBD_GENL_MCAST_GROUP_NAME
, },
1893 static struct genl_family nbd_genl_family __ro_after_init
= {
1895 .name
= NBD_GENL_FAMILY_NAME
,
1896 .version
= NBD_GENL_VERSION
,
1897 .module
= THIS_MODULE
,
1898 .ops
= nbd_connect_genl_ops
,
1899 .n_ops
= ARRAY_SIZE(nbd_connect_genl_ops
),
1900 .maxattr
= NBD_ATTR_MAX
,
1901 .mcgrps
= nbd_mcast_grps
,
1902 .n_mcgrps
= ARRAY_SIZE(nbd_mcast_grps
),
1905 static int populate_nbd_status(struct nbd_device
*nbd
, struct sk_buff
*reply
)
1907 struct nlattr
*dev_opt
;
1911 /* This is a little racey, but for status it's ok. The
1912 * reason we don't take a ref here is because we can't
1913 * take a ref in the index == -1 case as we would need
1914 * to put under the nbd_index_mutex, which could
1915 * deadlock if we are configured to remove ourselves
1916 * once we're disconnected.
1918 if (refcount_read(&nbd
->config_refs
))
1920 dev_opt
= nla_nest_start(reply
, NBD_DEVICE_ITEM
);
1923 ret
= nla_put_u32(reply
, NBD_DEVICE_INDEX
, nbd
->index
);
1926 ret
= nla_put_u8(reply
, NBD_DEVICE_CONNECTED
,
1930 nla_nest_end(reply
, dev_opt
);
1934 static int status_cb(int id
, void *ptr
, void *data
)
1936 struct nbd_device
*nbd
= ptr
;
1937 return populate_nbd_status(nbd
, (struct sk_buff
*)data
);
1940 static int nbd_genl_status(struct sk_buff
*skb
, struct genl_info
*info
)
1942 struct nlattr
*dev_list
;
1943 struct sk_buff
*reply
;
1949 if (info
->attrs
[NBD_ATTR_INDEX
])
1950 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1952 mutex_lock(&nbd_index_mutex
);
1954 msg_size
= nla_total_size(nla_attr_size(sizeof(u32
)) +
1955 nla_attr_size(sizeof(u8
)));
1956 msg_size
*= (index
== -1) ? nbd_total_devices
: 1;
1958 reply
= genlmsg_new(msg_size
, GFP_KERNEL
);
1961 reply_head
= genlmsg_put_reply(reply
, info
, &nbd_genl_family
, 0,
1968 dev_list
= nla_nest_start(reply
, NBD_ATTR_DEVICE_LIST
);
1970 ret
= idr_for_each(&nbd_index_idr
, &status_cb
, reply
);
1976 struct nbd_device
*nbd
;
1977 nbd
= idr_find(&nbd_index_idr
, index
);
1979 ret
= populate_nbd_status(nbd
, reply
);
1986 nla_nest_end(reply
, dev_list
);
1987 genlmsg_end(reply
, reply_head
);
1988 genlmsg_reply(reply
, info
);
1991 mutex_unlock(&nbd_index_mutex
);
1995 static void nbd_connect_reply(struct genl_info
*info
, int index
)
1997 struct sk_buff
*skb
;
2001 skb
= genlmsg_new(nla_total_size(sizeof(u32
)), GFP_KERNEL
);
2004 msg_head
= genlmsg_put_reply(skb
, info
, &nbd_genl_family
, 0,
2010 ret
= nla_put_u32(skb
, NBD_ATTR_INDEX
, index
);
2015 genlmsg_end(skb
, msg_head
);
2016 genlmsg_reply(skb
, info
);
2019 static void nbd_mcast_index(int index
)
2021 struct sk_buff
*skb
;
2025 skb
= genlmsg_new(nla_total_size(sizeof(u32
)), GFP_KERNEL
);
2028 msg_head
= genlmsg_put(skb
, 0, 0, &nbd_genl_family
, 0,
2034 ret
= nla_put_u32(skb
, NBD_ATTR_INDEX
, index
);
2039 genlmsg_end(skb
, msg_head
);
2040 genlmsg_multicast(&nbd_genl_family
, skb
, 0, 0, GFP_KERNEL
);
2043 static void nbd_dead_link_work(struct work_struct
*work
)
2045 struct link_dead_args
*args
= container_of(work
, struct link_dead_args
,
2047 nbd_mcast_index(args
->index
);
2051 static int __init
nbd_init(void)
2055 BUILD_BUG_ON(sizeof(struct nbd_request
) != 28);
2058 printk(KERN_ERR
"nbd: max_part must be >= 0\n");
2064 part_shift
= fls(max_part
);
2067 * Adjust max_part according to part_shift as it is exported
2068 * to user space so that user can know the max number of
2069 * partition kernel should be able to manage.
2071 * Note that -1 is required because partition 0 is reserved
2072 * for the whole disk.
2074 max_part
= (1UL << part_shift
) - 1;
2077 if ((1UL << part_shift
) > DISK_MAX_PARTS
)
2080 if (nbds_max
> 1UL << (MINORBITS
- part_shift
))
2082 recv_workqueue
= alloc_workqueue("knbd-recv",
2083 WQ_MEM_RECLAIM
| WQ_HIGHPRI
, 0);
2084 if (!recv_workqueue
)
2087 if (register_blkdev(NBD_MAJOR
, "nbd")) {
2088 destroy_workqueue(recv_workqueue
);
2092 if (genl_register_family(&nbd_genl_family
)) {
2093 unregister_blkdev(NBD_MAJOR
, "nbd");
2094 destroy_workqueue(recv_workqueue
);
2099 mutex_lock(&nbd_index_mutex
);
2100 for (i
= 0; i
< nbds_max
; i
++)
2102 mutex_unlock(&nbd_index_mutex
);
2106 static int nbd_exit_cb(int id
, void *ptr
, void *data
)
2108 struct list_head
*list
= (struct list_head
*)data
;
2109 struct nbd_device
*nbd
= ptr
;
2111 list_add_tail(&nbd
->list
, list
);
2115 static void __exit
nbd_cleanup(void)
2117 struct nbd_device
*nbd
;
2118 LIST_HEAD(del_list
);
2122 mutex_lock(&nbd_index_mutex
);
2123 idr_for_each(&nbd_index_idr
, &nbd_exit_cb
, &del_list
);
2124 mutex_unlock(&nbd_index_mutex
);
2126 while (!list_empty(&del_list
)) {
2127 nbd
= list_first_entry(&del_list
, struct nbd_device
, list
);
2128 list_del_init(&nbd
->list
);
2129 if (refcount_read(&nbd
->refs
) != 1)
2130 printk(KERN_ERR
"nbd: possibly leaking a device\n");
2134 idr_destroy(&nbd_index_idr
);
2135 genl_unregister_family(&nbd_genl_family
);
2136 destroy_workqueue(recv_workqueue
);
2137 unregister_blkdev(NBD_MAJOR
, "nbd");
2140 module_init(nbd_init
);
2141 module_exit(nbd_cleanup
);
2143 MODULE_DESCRIPTION("Network Block Device");
2144 MODULE_LICENSE("GPL");
2146 module_param(nbds_max
, int, 0444);
2147 MODULE_PARM_DESC(nbds_max
, "number of network block devices to initialize (default: 16)");
2148 module_param(max_part
, int, 0444);
2149 MODULE_PARM_DESC(max_part
, "number of partitions per device (default: 0)");