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
;
116 struct nbd_device
*nbd
;
119 struct completion send_complete
;
123 #if IS_ENABLED(CONFIG_DEBUG_FS)
124 static struct dentry
*nbd_dbg_dir
;
127 #define nbd_name(nbd) ((nbd)->disk->disk_name)
129 #define NBD_MAGIC 0x68797548
131 static unsigned int nbds_max
= 16;
132 static int max_part
= 16;
133 static struct workqueue_struct
*recv_workqueue
;
134 static int part_shift
;
136 static int nbd_dev_dbg_init(struct nbd_device
*nbd
);
137 static void nbd_dev_dbg_close(struct nbd_device
*nbd
);
138 static void nbd_config_put(struct nbd_device
*nbd
);
139 static void nbd_connect_reply(struct genl_info
*info
, int index
);
140 static int nbd_genl_status(struct sk_buff
*skb
, struct genl_info
*info
);
141 static void nbd_dead_link_work(struct work_struct
*work
);
142 static void nbd_disconnect_and_put(struct nbd_device
*nbd
);
144 static inline struct device
*nbd_to_dev(struct nbd_device
*nbd
)
146 return disk_to_dev(nbd
->disk
);
149 static const char *nbdcmd_to_ascii(int cmd
)
152 case NBD_CMD_READ
: return "read";
153 case NBD_CMD_WRITE
: return "write";
154 case NBD_CMD_DISC
: return "disconnect";
155 case NBD_CMD_FLUSH
: return "flush";
156 case NBD_CMD_TRIM
: return "trim/discard";
161 static ssize_t
pid_show(struct device
*dev
,
162 struct device_attribute
*attr
, char *buf
)
164 struct gendisk
*disk
= dev_to_disk(dev
);
165 struct nbd_device
*nbd
= (struct nbd_device
*)disk
->private_data
;
167 return sprintf(buf
, "%d\n", task_pid_nr(nbd
->task_recv
));
170 static const struct device_attribute pid_attr
= {
171 .attr
= { .name
= "pid", .mode
= 0444},
175 static void nbd_dev_remove(struct nbd_device
*nbd
)
177 struct gendisk
*disk
= nbd
->disk
;
178 struct request_queue
*q
;
183 blk_cleanup_queue(q
);
184 blk_mq_free_tag_set(&nbd
->tag_set
);
185 disk
->private_data
= NULL
;
191 static void nbd_put(struct nbd_device
*nbd
)
193 if (refcount_dec_and_mutex_lock(&nbd
->refs
,
195 idr_remove(&nbd_index_idr
, nbd
->index
);
196 mutex_unlock(&nbd_index_mutex
);
201 static int nbd_disconnected(struct nbd_config
*config
)
203 return test_bit(NBD_DISCONNECTED
, &config
->runtime_flags
) ||
204 test_bit(NBD_DISCONNECT_REQUESTED
, &config
->runtime_flags
);
207 static void nbd_mark_nsock_dead(struct nbd_device
*nbd
, struct nbd_sock
*nsock
,
210 if (!nsock
->dead
&& notify
&& !nbd_disconnected(nbd
->config
)) {
211 struct link_dead_args
*args
;
212 args
= kmalloc(sizeof(struct link_dead_args
), GFP_NOIO
);
214 INIT_WORK(&args
->work
, nbd_dead_link_work
);
215 args
->index
= nbd
->index
;
216 queue_work(system_wq
, &args
->work
);
220 kernel_sock_shutdown(nsock
->sock
, SHUT_RDWR
);
221 if (atomic_dec_return(&nbd
->config
->live_connections
) == 0) {
222 if (test_and_clear_bit(NBD_DISCONNECT_REQUESTED
,
223 &nbd
->config
->runtime_flags
)) {
224 set_bit(NBD_DISCONNECTED
,
225 &nbd
->config
->runtime_flags
);
226 dev_info(nbd_to_dev(nbd
),
227 "Disconnected due to user request.\n");
232 nsock
->pending
= NULL
;
236 static void nbd_size_clear(struct nbd_device
*nbd
)
238 if (nbd
->config
->bytesize
) {
239 set_capacity(nbd
->disk
, 0);
240 kobject_uevent(&nbd_to_dev(nbd
)->kobj
, KOBJ_CHANGE
);
244 static void nbd_size_update(struct nbd_device
*nbd
)
246 struct nbd_config
*config
= nbd
->config
;
247 struct block_device
*bdev
= bdget_disk(nbd
->disk
, 0);
249 if (config
->flags
& NBD_FLAG_SEND_TRIM
) {
250 nbd
->disk
->queue
->limits
.discard_granularity
= config
->blksize
;
251 nbd
->disk
->queue
->limits
.discard_alignment
= config
->blksize
;
252 blk_queue_max_discard_sectors(nbd
->disk
->queue
, UINT_MAX
);
254 blk_queue_logical_block_size(nbd
->disk
->queue
, config
->blksize
);
255 blk_queue_physical_block_size(nbd
->disk
->queue
, config
->blksize
);
256 set_capacity(nbd
->disk
, config
->bytesize
>> 9);
259 bd_set_size(bdev
, config
->bytesize
);
261 bdev
->bd_invalidated
= 1;
264 kobject_uevent(&nbd_to_dev(nbd
)->kobj
, KOBJ_CHANGE
);
267 static void nbd_size_set(struct nbd_device
*nbd
, loff_t blocksize
,
270 struct nbd_config
*config
= nbd
->config
;
271 config
->blksize
= blocksize
;
272 config
->bytesize
= blocksize
* nr_blocks
;
273 if (nbd
->task_recv
!= NULL
)
274 nbd_size_update(nbd
);
277 static void nbd_complete_rq(struct request
*req
)
279 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
281 dev_dbg(nbd_to_dev(cmd
->nbd
), "request %p: %s\n", req
,
282 cmd
->status
? "failed" : "done");
284 blk_mq_end_request(req
, cmd
->status
);
288 * Forcibly shutdown the socket causing all listeners to error
290 static void sock_shutdown(struct nbd_device
*nbd
)
292 struct nbd_config
*config
= nbd
->config
;
295 if (config
->num_connections
== 0)
297 if (test_and_set_bit(NBD_DISCONNECTED
, &config
->runtime_flags
))
300 for (i
= 0; i
< config
->num_connections
; i
++) {
301 struct nbd_sock
*nsock
= config
->socks
[i
];
302 mutex_lock(&nsock
->tx_lock
);
303 nbd_mark_nsock_dead(nbd
, nsock
, 0);
304 mutex_unlock(&nsock
->tx_lock
);
306 dev_warn(disk_to_dev(nbd
->disk
), "shutting down sockets\n");
309 static enum blk_eh_timer_return
nbd_xmit_timeout(struct request
*req
,
312 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
313 struct nbd_device
*nbd
= cmd
->nbd
;
314 struct nbd_config
*config
;
316 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
317 cmd
->status
= BLK_STS_TIMEOUT
;
320 config
= nbd
->config
;
322 if (config
->num_connections
> 1) {
323 dev_err_ratelimited(nbd_to_dev(nbd
),
324 "Connection timed out, retrying (%d/%d alive)\n",
325 atomic_read(&config
->live_connections
),
326 config
->num_connections
);
328 * Hooray we have more connections, requeue this IO, the submit
329 * path will put it on a real connection.
331 if (config
->socks
&& config
->num_connections
> 1) {
332 if (cmd
->index
< config
->num_connections
) {
333 struct nbd_sock
*nsock
=
334 config
->socks
[cmd
->index
];
335 mutex_lock(&nsock
->tx_lock
);
336 /* We can have multiple outstanding requests, so
337 * we don't want to mark the nsock dead if we've
338 * already reconnected with a new socket, so
339 * only mark it dead if its the same socket we
342 if (cmd
->cookie
== nsock
->cookie
)
343 nbd_mark_nsock_dead(nbd
, nsock
, 1);
344 mutex_unlock(&nsock
->tx_lock
);
346 blk_mq_requeue_request(req
, true);
351 dev_err_ratelimited(nbd_to_dev(nbd
),
352 "Connection timed out\n");
354 set_bit(NBD_TIMEDOUT
, &config
->runtime_flags
);
355 cmd
->status
= BLK_STS_IOERR
;
359 blk_mq_complete_request(req
);
364 * Send or receive packet.
366 static int sock_xmit(struct nbd_device
*nbd
, int index
, int send
,
367 struct iov_iter
*iter
, int msg_flags
, int *sent
)
369 struct nbd_config
*config
= nbd
->config
;
370 struct socket
*sock
= config
->socks
[index
]->sock
;
373 unsigned int noreclaim_flag
;
375 if (unlikely(!sock
)) {
376 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
377 "Attempted %s on closed socket in sock_xmit\n",
378 (send
? "send" : "recv"));
382 msg
.msg_iter
= *iter
;
384 noreclaim_flag
= memalloc_noreclaim_save();
386 sock
->sk
->sk_allocation
= GFP_NOIO
| __GFP_MEMALLOC
;
389 msg
.msg_control
= NULL
;
390 msg
.msg_controllen
= 0;
391 msg
.msg_flags
= msg_flags
| MSG_NOSIGNAL
;
394 result
= sock_sendmsg(sock
, &msg
);
396 result
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
400 result
= -EPIPE
; /* short read */
405 } while (msg_data_left(&msg
));
407 memalloc_noreclaim_restore(noreclaim_flag
);
413 * Different settings for sk->sk_sndtimeo can result in different return values
414 * if there is a signal pending when we enter sendmsg, because reasons?
416 static inline int was_interrupted(int result
)
418 return result
== -ERESTARTSYS
|| result
== -EINTR
;
421 /* always call with the tx_lock held */
422 static int nbd_send_cmd(struct nbd_device
*nbd
, struct nbd_cmd
*cmd
, int index
)
424 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
425 struct nbd_config
*config
= nbd
->config
;
426 struct nbd_sock
*nsock
= config
->socks
[index
];
428 struct nbd_request request
= {.magic
= htonl(NBD_REQUEST_MAGIC
)};
429 struct kvec iov
= {.iov_base
= &request
, .iov_len
= sizeof(request
)};
430 struct iov_iter from
;
431 unsigned long size
= blk_rq_bytes(req
);
434 u32 nbd_cmd_flags
= 0;
435 u32 tag
= blk_mq_unique_tag(req
);
436 int sent
= nsock
->sent
, skip
= 0;
438 iov_iter_kvec(&from
, WRITE
| ITER_KVEC
, &iov
, 1, sizeof(request
));
440 switch (req_op(req
)) {
445 type
= NBD_CMD_FLUSH
;
448 type
= NBD_CMD_WRITE
;
457 if (rq_data_dir(req
) == WRITE
&&
458 (config
->flags
& NBD_FLAG_READ_ONLY
)) {
459 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
460 "Write on read-only\n");
464 if (req
->cmd_flags
& REQ_FUA
)
465 nbd_cmd_flags
|= NBD_CMD_FLAG_FUA
;
467 /* We did a partial send previously, and we at least sent the whole
468 * request struct, so just go and send the rest of the pages in the
472 if (sent
>= sizeof(request
)) {
473 skip
= sent
- sizeof(request
);
476 iov_iter_advance(&from
, sent
);
479 cmd
->cookie
= nsock
->cookie
;
480 request
.type
= htonl(type
| nbd_cmd_flags
);
481 if (type
!= NBD_CMD_FLUSH
) {
482 request
.from
= cpu_to_be64((u64
)blk_rq_pos(req
) << 9);
483 request
.len
= htonl(size
);
485 memcpy(request
.handle
, &tag
, sizeof(tag
));
487 dev_dbg(nbd_to_dev(nbd
), "request %p: sending control (%s@%llu,%uB)\n",
488 req
, nbdcmd_to_ascii(type
),
489 (unsigned long long)blk_rq_pos(req
) << 9, blk_rq_bytes(req
));
490 result
= sock_xmit(nbd
, index
, 1, &from
,
491 (type
== NBD_CMD_WRITE
) ? MSG_MORE
: 0, &sent
);
493 if (was_interrupted(result
)) {
494 /* If we havne't sent anything we can just return BUSY,
495 * however if we have sent something we need to make
496 * sure we only allow this req to be sent until we are
500 nsock
->pending
= req
;
503 return BLK_STS_RESOURCE
;
505 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
506 "Send control failed (result %d)\n", result
);
510 if (type
!= NBD_CMD_WRITE
)
515 struct bio
*next
= bio
->bi_next
;
516 struct bvec_iter iter
;
519 bio_for_each_segment(bvec
, bio
, iter
) {
520 bool is_last
= !next
&& bio_iter_last(bvec
, iter
);
521 int flags
= is_last
? 0 : MSG_MORE
;
523 dev_dbg(nbd_to_dev(nbd
), "request %p: sending %d bytes data\n",
525 iov_iter_bvec(&from
, ITER_BVEC
| WRITE
,
526 &bvec
, 1, bvec
.bv_len
);
528 if (skip
>= iov_iter_count(&from
)) {
529 skip
-= iov_iter_count(&from
);
532 iov_iter_advance(&from
, skip
);
535 result
= sock_xmit(nbd
, index
, 1, &from
, flags
, &sent
);
537 if (was_interrupted(result
)) {
538 /* We've already sent the header, we
539 * have no choice but to set pending and
542 nsock
->pending
= req
;
544 return BLK_STS_RESOURCE
;
546 dev_err(disk_to_dev(nbd
->disk
),
547 "Send data failed (result %d)\n",
552 * The completion might already have come in,
553 * so break for the last one instead of letting
554 * the iterator do it. This prevents use-after-free
563 nsock
->pending
= NULL
;
568 /* NULL returned = something went wrong, inform userspace */
569 static struct nbd_cmd
*nbd_read_stat(struct nbd_device
*nbd
, int index
)
571 struct nbd_config
*config
= nbd
->config
;
573 struct nbd_reply reply
;
575 struct request
*req
= NULL
;
578 struct kvec iov
= {.iov_base
= &reply
, .iov_len
= sizeof(reply
)};
582 iov_iter_kvec(&to
, READ
| ITER_KVEC
, &iov
, 1, sizeof(reply
));
583 result
= sock_xmit(nbd
, index
, 0, &to
, MSG_WAITALL
, NULL
);
585 if (!nbd_disconnected(config
))
586 dev_err(disk_to_dev(nbd
->disk
),
587 "Receive control failed (result %d)\n", result
);
588 return ERR_PTR(result
);
591 if (ntohl(reply
.magic
) != NBD_REPLY_MAGIC
) {
592 dev_err(disk_to_dev(nbd
->disk
), "Wrong magic (0x%lx)\n",
593 (unsigned long)ntohl(reply
.magic
));
594 return ERR_PTR(-EPROTO
);
597 memcpy(&tag
, reply
.handle
, sizeof(u32
));
599 hwq
= blk_mq_unique_tag_to_hwq(tag
);
600 if (hwq
< nbd
->tag_set
.nr_hw_queues
)
601 req
= blk_mq_tag_to_rq(nbd
->tag_set
.tags
[hwq
],
602 blk_mq_unique_tag_to_tag(tag
));
603 if (!req
|| !blk_mq_request_started(req
)) {
604 dev_err(disk_to_dev(nbd
->disk
), "Unexpected reply (%d) %p\n",
606 return ERR_PTR(-ENOENT
);
608 cmd
= blk_mq_rq_to_pdu(req
);
609 if (ntohl(reply
.error
)) {
610 dev_err(disk_to_dev(nbd
->disk
), "Other side returned error (%d)\n",
612 cmd
->status
= BLK_STS_IOERR
;
616 dev_dbg(nbd_to_dev(nbd
), "request %p: got reply\n", req
);
617 if (rq_data_dir(req
) != WRITE
) {
618 struct req_iterator iter
;
621 rq_for_each_segment(bvec
, req
, iter
) {
622 iov_iter_bvec(&to
, ITER_BVEC
| READ
,
623 &bvec
, 1, bvec
.bv_len
);
624 result
= sock_xmit(nbd
, index
, 0, &to
, MSG_WAITALL
, NULL
);
626 dev_err(disk_to_dev(nbd
->disk
), "Receive data failed (result %d)\n",
629 * If we've disconnected or we only have 1
630 * connection then we need to make sure we
631 * complete this request, otherwise error out
632 * and let the timeout stuff handle resubmitting
633 * this request onto another connection.
635 if (nbd_disconnected(config
) ||
636 config
->num_connections
<= 1) {
637 cmd
->status
= BLK_STS_IOERR
;
640 return ERR_PTR(-EIO
);
642 dev_dbg(nbd_to_dev(nbd
), "request %p: got %d bytes data\n",
646 /* See the comment in nbd_queue_rq. */
647 wait_for_completion(&cmd
->send_complete
);
652 static void recv_work(struct work_struct
*work
)
654 struct recv_thread_args
*args
= container_of(work
,
655 struct recv_thread_args
,
657 struct nbd_device
*nbd
= args
->nbd
;
658 struct nbd_config
*config
= nbd
->config
;
662 cmd
= nbd_read_stat(nbd
, args
->index
);
664 struct nbd_sock
*nsock
= config
->socks
[args
->index
];
666 mutex_lock(&nsock
->tx_lock
);
667 nbd_mark_nsock_dead(nbd
, nsock
, 1);
668 mutex_unlock(&nsock
->tx_lock
);
672 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd
));
674 atomic_dec(&config
->recv_threads
);
675 wake_up(&config
->recv_wq
);
680 static void nbd_clear_req(struct request
*req
, void *data
, bool reserved
)
682 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
684 cmd
->status
= BLK_STS_IOERR
;
685 blk_mq_complete_request(req
);
688 static void nbd_clear_que(struct nbd_device
*nbd
)
690 blk_mq_quiesce_queue(nbd
->disk
->queue
);
691 blk_mq_tagset_busy_iter(&nbd
->tag_set
, nbd_clear_req
, NULL
);
692 blk_mq_unquiesce_queue(nbd
->disk
->queue
);
693 dev_dbg(disk_to_dev(nbd
->disk
), "queue cleared\n");
696 static int find_fallback(struct nbd_device
*nbd
, int index
)
698 struct nbd_config
*config
= nbd
->config
;
700 struct nbd_sock
*nsock
= config
->socks
[index
];
701 int fallback
= nsock
->fallback_index
;
703 if (test_bit(NBD_DISCONNECTED
, &config
->runtime_flags
))
706 if (config
->num_connections
<= 1) {
707 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
708 "Attempted send on invalid socket\n");
712 if (fallback
>= 0 && fallback
< config
->num_connections
&&
713 !config
->socks
[fallback
]->dead
)
716 if (nsock
->fallback_index
< 0 ||
717 nsock
->fallback_index
>= config
->num_connections
||
718 config
->socks
[nsock
->fallback_index
]->dead
) {
720 for (i
= 0; i
< config
->num_connections
; i
++) {
723 if (!config
->socks
[i
]->dead
) {
728 nsock
->fallback_index
= new_index
;
730 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
731 "Dead connection, failed to find a fallback\n");
735 new_index
= nsock
->fallback_index
;
739 static int wait_for_reconnect(struct nbd_device
*nbd
)
741 struct nbd_config
*config
= nbd
->config
;
742 if (!config
->dead_conn_timeout
)
744 if (test_bit(NBD_DISCONNECTED
, &config
->runtime_flags
))
746 return wait_event_timeout(config
->conn_wait
,
747 atomic_read(&config
->live_connections
) > 0,
748 config
->dead_conn_timeout
) > 0;
751 static int nbd_handle_cmd(struct nbd_cmd
*cmd
, int index
)
753 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
754 struct nbd_device
*nbd
= cmd
->nbd
;
755 struct nbd_config
*config
;
756 struct nbd_sock
*nsock
;
759 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
760 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
761 "Socks array is empty\n");
762 blk_mq_start_request(req
);
765 config
= nbd
->config
;
767 if (index
>= config
->num_connections
) {
768 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
769 "Attempted send on invalid socket\n");
771 blk_mq_start_request(req
);
774 cmd
->status
= BLK_STS_OK
;
776 nsock
= config
->socks
[index
];
777 mutex_lock(&nsock
->tx_lock
);
779 int old_index
= index
;
780 index
= find_fallback(nbd
, index
);
781 mutex_unlock(&nsock
->tx_lock
);
783 if (wait_for_reconnect(nbd
)) {
787 /* All the sockets should already be down at this point,
788 * we just want to make sure that DISCONNECTED is set so
789 * any requests that come in that were queue'ed waiting
790 * for the reconnect timer don't trigger the timer again
791 * and instead just error out.
795 blk_mq_start_request(req
);
801 /* Handle the case that we have a pending request that was partially
802 * transmitted that _has_ to be serviced first. We need to call requeue
803 * here so that it gets put _after_ the request that is already on the
806 blk_mq_start_request(req
);
807 if (unlikely(nsock
->pending
&& nsock
->pending
!= req
)) {
808 blk_mq_requeue_request(req
, true);
813 * Some failures are related to the link going down, so anything that
814 * returns EAGAIN can be retried on a different socket.
816 ret
= nbd_send_cmd(nbd
, cmd
, index
);
817 if (ret
== -EAGAIN
) {
818 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
819 "Request send failed, requeueing\n");
820 nbd_mark_nsock_dead(nbd
, nsock
, 1);
821 blk_mq_requeue_request(req
, true);
825 mutex_unlock(&nsock
->tx_lock
);
830 static blk_status_t
nbd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
831 const struct blk_mq_queue_data
*bd
)
833 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(bd
->rq
);
837 * Since we look at the bio's to send the request over the network we
838 * need to make sure the completion work doesn't mark this request done
839 * before we are done doing our send. This keeps us from dereferencing
840 * freed data if we have particularly fast completions (ie we get the
841 * completion before we exit sock_xmit on the last bvec) or in the case
842 * that the server is misbehaving (or there was an error) before we're
843 * done sending everything over the wire.
845 init_completion(&cmd
->send_complete
);
847 /* We can be called directly from the user space process, which means we
848 * could possibly have signals pending so our sendmsg will fail. In
849 * this case we need to return that we are busy, otherwise error out as
852 ret
= nbd_handle_cmd(cmd
, hctx
->queue_num
);
857 complete(&cmd
->send_complete
);
862 static int nbd_add_socket(struct nbd_device
*nbd
, unsigned long arg
,
865 struct nbd_config
*config
= nbd
->config
;
867 struct nbd_sock
**socks
;
868 struct nbd_sock
*nsock
;
871 sock
= sockfd_lookup(arg
, &err
);
875 if (!netlink
&& !nbd
->task_setup
&&
876 !test_bit(NBD_BOUND
, &config
->runtime_flags
))
877 nbd
->task_setup
= current
;
880 (nbd
->task_setup
!= current
||
881 test_bit(NBD_BOUND
, &config
->runtime_flags
))) {
882 dev_err(disk_to_dev(nbd
->disk
),
883 "Device being setup by another task");
888 socks
= krealloc(config
->socks
, (config
->num_connections
+ 1) *
889 sizeof(struct nbd_sock
*), GFP_KERNEL
);
894 nsock
= kzalloc(sizeof(struct nbd_sock
), GFP_KERNEL
);
900 config
->socks
= socks
;
902 nsock
->fallback_index
= -1;
904 mutex_init(&nsock
->tx_lock
);
906 nsock
->pending
= NULL
;
909 socks
[config
->num_connections
++] = nsock
;
910 atomic_inc(&config
->live_connections
);
915 static int nbd_reconnect_socket(struct nbd_device
*nbd
, unsigned long arg
)
917 struct nbd_config
*config
= nbd
->config
;
918 struct socket
*sock
, *old
;
919 struct recv_thread_args
*args
;
923 sock
= sockfd_lookup(arg
, &err
);
927 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
933 for (i
= 0; i
< config
->num_connections
; i
++) {
934 struct nbd_sock
*nsock
= config
->socks
[i
];
939 mutex_lock(&nsock
->tx_lock
);
941 mutex_unlock(&nsock
->tx_lock
);
944 sk_set_memalloc(sock
->sk
);
945 if (nbd
->tag_set
.timeout
)
946 sock
->sk
->sk_sndtimeo
= nbd
->tag_set
.timeout
;
947 atomic_inc(&config
->recv_threads
);
948 refcount_inc(&nbd
->config_refs
);
950 nsock
->fallback_index
= -1;
953 INIT_WORK(&args
->work
, recv_work
);
957 mutex_unlock(&nsock
->tx_lock
);
960 clear_bit(NBD_DISCONNECTED
, &config
->runtime_flags
);
962 /* We take the tx_mutex in an error path in the recv_work, so we
963 * need to queue_work outside of the tx_mutex.
965 queue_work(recv_workqueue
, &args
->work
);
967 atomic_inc(&config
->live_connections
);
968 wake_up(&config
->conn_wait
);
976 static void nbd_bdev_reset(struct block_device
*bdev
)
978 if (bdev
->bd_openers
> 1)
980 bd_set_size(bdev
, 0);
983 static void nbd_parse_flags(struct nbd_device
*nbd
)
985 struct nbd_config
*config
= nbd
->config
;
986 if (config
->flags
& NBD_FLAG_READ_ONLY
)
987 set_disk_ro(nbd
->disk
, true);
989 set_disk_ro(nbd
->disk
, false);
990 if (config
->flags
& NBD_FLAG_SEND_TRIM
)
991 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
992 if (config
->flags
& NBD_FLAG_SEND_FLUSH
) {
993 if (config
->flags
& NBD_FLAG_SEND_FUA
)
994 blk_queue_write_cache(nbd
->disk
->queue
, true, true);
996 blk_queue_write_cache(nbd
->disk
->queue
, true, false);
999 blk_queue_write_cache(nbd
->disk
->queue
, false, false);
1002 static void send_disconnects(struct nbd_device
*nbd
)
1004 struct nbd_config
*config
= nbd
->config
;
1005 struct nbd_request request
= {
1006 .magic
= htonl(NBD_REQUEST_MAGIC
),
1007 .type
= htonl(NBD_CMD_DISC
),
1009 struct kvec iov
= {.iov_base
= &request
, .iov_len
= sizeof(request
)};
1010 struct iov_iter from
;
1013 for (i
= 0; i
< config
->num_connections
; i
++) {
1014 struct nbd_sock
*nsock
= config
->socks
[i
];
1016 iov_iter_kvec(&from
, WRITE
| ITER_KVEC
, &iov
, 1, sizeof(request
));
1017 mutex_lock(&nsock
->tx_lock
);
1018 ret
= sock_xmit(nbd
, i
, 1, &from
, 0, NULL
);
1020 dev_err(disk_to_dev(nbd
->disk
),
1021 "Send disconnect failed %d\n", ret
);
1022 mutex_unlock(&nsock
->tx_lock
);
1026 static int nbd_disconnect(struct nbd_device
*nbd
)
1028 struct nbd_config
*config
= nbd
->config
;
1030 dev_info(disk_to_dev(nbd
->disk
), "NBD_DISCONNECT\n");
1031 set_bit(NBD_DISCONNECT_REQUESTED
, &config
->runtime_flags
);
1032 send_disconnects(nbd
);
1036 static void nbd_clear_sock(struct nbd_device
*nbd
)
1040 nbd
->task_setup
= NULL
;
1043 static void nbd_config_put(struct nbd_device
*nbd
)
1045 if (refcount_dec_and_mutex_lock(&nbd
->config_refs
,
1046 &nbd
->config_lock
)) {
1047 struct nbd_config
*config
= nbd
->config
;
1048 nbd_dev_dbg_close(nbd
);
1049 nbd_size_clear(nbd
);
1050 if (test_and_clear_bit(NBD_HAS_PID_FILE
,
1051 &config
->runtime_flags
))
1052 device_remove_file(disk_to_dev(nbd
->disk
), &pid_attr
);
1053 nbd
->task_recv
= NULL
;
1054 nbd_clear_sock(nbd
);
1055 if (config
->num_connections
) {
1057 for (i
= 0; i
< config
->num_connections
; i
++) {
1058 sockfd_put(config
->socks
[i
]->sock
);
1059 kfree(config
->socks
[i
]);
1061 kfree(config
->socks
);
1066 nbd
->tag_set
.timeout
= 0;
1067 nbd
->disk
->queue
->limits
.discard_granularity
= 0;
1068 nbd
->disk
->queue
->limits
.discard_alignment
= 0;
1069 blk_queue_max_discard_sectors(nbd
->disk
->queue
, UINT_MAX
);
1070 blk_queue_flag_clear(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
1072 mutex_unlock(&nbd
->config_lock
);
1074 module_put(THIS_MODULE
);
1078 static int nbd_start_device(struct nbd_device
*nbd
)
1080 struct nbd_config
*config
= nbd
->config
;
1081 int num_connections
= config
->num_connections
;
1088 if (num_connections
> 1 &&
1089 !(config
->flags
& NBD_FLAG_CAN_MULTI_CONN
)) {
1090 dev_err(disk_to_dev(nbd
->disk
), "server does not support multiple connections per device.\n");
1094 blk_mq_update_nr_hw_queues(&nbd
->tag_set
, config
->num_connections
);
1095 nbd
->task_recv
= current
;
1097 nbd_parse_flags(nbd
);
1099 error
= device_create_file(disk_to_dev(nbd
->disk
), &pid_attr
);
1101 dev_err(disk_to_dev(nbd
->disk
), "device_create_file failed!\n");
1104 set_bit(NBD_HAS_PID_FILE
, &config
->runtime_flags
);
1106 nbd_dev_dbg_init(nbd
);
1107 for (i
= 0; i
< num_connections
; i
++) {
1108 struct recv_thread_args
*args
;
1110 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
1115 sk_set_memalloc(config
->socks
[i
]->sock
->sk
);
1116 if (nbd
->tag_set
.timeout
)
1117 config
->socks
[i
]->sock
->sk
->sk_sndtimeo
=
1118 nbd
->tag_set
.timeout
;
1119 atomic_inc(&config
->recv_threads
);
1120 refcount_inc(&nbd
->config_refs
);
1121 INIT_WORK(&args
->work
, recv_work
);
1124 queue_work(recv_workqueue
, &args
->work
);
1126 nbd_size_update(nbd
);
1130 static int nbd_start_device_ioctl(struct nbd_device
*nbd
, struct block_device
*bdev
)
1132 struct nbd_config
*config
= nbd
->config
;
1135 ret
= nbd_start_device(nbd
);
1140 bdev
->bd_invalidated
= 1;
1141 mutex_unlock(&nbd
->config_lock
);
1142 ret
= wait_event_interruptible(config
->recv_wq
,
1143 atomic_read(&config
->recv_threads
) == 0);
1146 mutex_lock(&nbd
->config_lock
);
1147 nbd_bdev_reset(bdev
);
1148 /* user requested, ignore socket errors */
1149 if (test_bit(NBD_DISCONNECT_REQUESTED
, &config
->runtime_flags
))
1151 if (test_bit(NBD_TIMEDOUT
, &config
->runtime_flags
))
1156 static void nbd_clear_sock_ioctl(struct nbd_device
*nbd
,
1157 struct block_device
*bdev
)
1161 nbd_bdev_reset(bdev
);
1162 if (test_and_clear_bit(NBD_HAS_CONFIG_REF
,
1163 &nbd
->config
->runtime_flags
))
1164 nbd_config_put(nbd
);
1167 /* Must be called with config_lock held */
1168 static int __nbd_ioctl(struct block_device
*bdev
, struct nbd_device
*nbd
,
1169 unsigned int cmd
, unsigned long arg
)
1171 struct nbd_config
*config
= nbd
->config
;
1174 case NBD_DISCONNECT
:
1175 return nbd_disconnect(nbd
);
1176 case NBD_CLEAR_SOCK
:
1177 nbd_clear_sock_ioctl(nbd
, bdev
);
1180 return nbd_add_socket(nbd
, arg
, false);
1181 case NBD_SET_BLKSIZE
:
1182 nbd_size_set(nbd
, arg
,
1183 div_s64(config
->bytesize
, arg
));
1186 nbd_size_set(nbd
, config
->blksize
,
1187 div_s64(arg
, config
->blksize
));
1189 case NBD_SET_SIZE_BLOCKS
:
1190 nbd_size_set(nbd
, config
->blksize
, arg
);
1192 case NBD_SET_TIMEOUT
:
1194 nbd
->tag_set
.timeout
= arg
* HZ
;
1195 blk_queue_rq_timeout(nbd
->disk
->queue
, arg
* HZ
);
1200 config
->flags
= arg
;
1203 return nbd_start_device_ioctl(nbd
, bdev
);
1206 * This is for compatibility only. The queue is always cleared
1207 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1210 case NBD_PRINT_DEBUG
:
1212 * For compatibility only, we no longer keep a list of
1213 * outstanding requests.
1220 static int nbd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1221 unsigned int cmd
, unsigned long arg
)
1223 struct nbd_device
*nbd
= bdev
->bd_disk
->private_data
;
1224 struct nbd_config
*config
= nbd
->config
;
1225 int error
= -EINVAL
;
1227 if (!capable(CAP_SYS_ADMIN
))
1230 /* The block layer will pass back some non-nbd ioctls in case we have
1231 * special handling for them, but we don't so just return an error.
1233 if (_IOC_TYPE(cmd
) != 0xab)
1236 mutex_lock(&nbd
->config_lock
);
1238 /* Don't allow ioctl operations on a nbd device that was created with
1239 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1241 if (!test_bit(NBD_BOUND
, &config
->runtime_flags
) ||
1242 (cmd
== NBD_DISCONNECT
|| cmd
== NBD_CLEAR_SOCK
))
1243 error
= __nbd_ioctl(bdev
, nbd
, cmd
, arg
);
1245 dev_err(nbd_to_dev(nbd
), "Cannot use ioctl interface on a netlink controlled device.\n");
1246 mutex_unlock(&nbd
->config_lock
);
1250 static struct nbd_config
*nbd_alloc_config(void)
1252 struct nbd_config
*config
;
1254 config
= kzalloc(sizeof(struct nbd_config
), GFP_NOFS
);
1257 atomic_set(&config
->recv_threads
, 0);
1258 init_waitqueue_head(&config
->recv_wq
);
1259 init_waitqueue_head(&config
->conn_wait
);
1260 config
->blksize
= 1024;
1261 atomic_set(&config
->live_connections
, 0);
1262 try_module_get(THIS_MODULE
);
1266 static int nbd_open(struct block_device
*bdev
, fmode_t mode
)
1268 struct nbd_device
*nbd
;
1271 mutex_lock(&nbd_index_mutex
);
1272 nbd
= bdev
->bd_disk
->private_data
;
1277 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1281 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1282 struct nbd_config
*config
;
1284 mutex_lock(&nbd
->config_lock
);
1285 if (refcount_inc_not_zero(&nbd
->config_refs
)) {
1286 mutex_unlock(&nbd
->config_lock
);
1289 config
= nbd
->config
= nbd_alloc_config();
1292 mutex_unlock(&nbd
->config_lock
);
1295 refcount_set(&nbd
->config_refs
, 1);
1296 refcount_inc(&nbd
->refs
);
1297 mutex_unlock(&nbd
->config_lock
);
1298 bdev
->bd_invalidated
= 1;
1299 } else if (nbd_disconnected(nbd
->config
)) {
1300 bdev
->bd_invalidated
= 1;
1303 mutex_unlock(&nbd_index_mutex
);
1307 static void nbd_release(struct gendisk
*disk
, fmode_t mode
)
1309 struct nbd_device
*nbd
= disk
->private_data
;
1310 struct block_device
*bdev
= bdget_disk(disk
, 0);
1312 if (test_bit(NBD_DISCONNECT_ON_CLOSE
, &nbd
->config
->runtime_flags
) &&
1313 bdev
->bd_openers
== 0)
1314 nbd_disconnect_and_put(nbd
);
1316 nbd_config_put(nbd
);
1320 static const struct block_device_operations nbd_fops
=
1322 .owner
= THIS_MODULE
,
1324 .release
= nbd_release
,
1326 .compat_ioctl
= nbd_ioctl
,
1329 #if IS_ENABLED(CONFIG_DEBUG_FS)
1331 static int nbd_dbg_tasks_show(struct seq_file
*s
, void *unused
)
1333 struct nbd_device
*nbd
= s
->private;
1336 seq_printf(s
, "recv: %d\n", task_pid_nr(nbd
->task_recv
));
1341 static int nbd_dbg_tasks_open(struct inode
*inode
, struct file
*file
)
1343 return single_open(file
, nbd_dbg_tasks_show
, inode
->i_private
);
1346 static const struct file_operations nbd_dbg_tasks_ops
= {
1347 .open
= nbd_dbg_tasks_open
,
1349 .llseek
= seq_lseek
,
1350 .release
= single_release
,
1353 static int nbd_dbg_flags_show(struct seq_file
*s
, void *unused
)
1355 struct nbd_device
*nbd
= s
->private;
1356 u32 flags
= nbd
->config
->flags
;
1358 seq_printf(s
, "Hex: 0x%08x\n\n", flags
);
1360 seq_puts(s
, "Known flags:\n");
1362 if (flags
& NBD_FLAG_HAS_FLAGS
)
1363 seq_puts(s
, "NBD_FLAG_HAS_FLAGS\n");
1364 if (flags
& NBD_FLAG_READ_ONLY
)
1365 seq_puts(s
, "NBD_FLAG_READ_ONLY\n");
1366 if (flags
& NBD_FLAG_SEND_FLUSH
)
1367 seq_puts(s
, "NBD_FLAG_SEND_FLUSH\n");
1368 if (flags
& NBD_FLAG_SEND_FUA
)
1369 seq_puts(s
, "NBD_FLAG_SEND_FUA\n");
1370 if (flags
& NBD_FLAG_SEND_TRIM
)
1371 seq_puts(s
, "NBD_FLAG_SEND_TRIM\n");
1376 static int nbd_dbg_flags_open(struct inode
*inode
, struct file
*file
)
1378 return single_open(file
, nbd_dbg_flags_show
, inode
->i_private
);
1381 static const struct file_operations nbd_dbg_flags_ops
= {
1382 .open
= nbd_dbg_flags_open
,
1384 .llseek
= seq_lseek
,
1385 .release
= single_release
,
1388 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
1391 struct nbd_config
*config
= nbd
->config
;
1396 dir
= debugfs_create_dir(nbd_name(nbd
), nbd_dbg_dir
);
1398 dev_err(nbd_to_dev(nbd
), "Failed to create debugfs dir for '%s'\n",
1402 config
->dbg_dir
= dir
;
1404 debugfs_create_file("tasks", 0444, dir
, nbd
, &nbd_dbg_tasks_ops
);
1405 debugfs_create_u64("size_bytes", 0444, dir
, &config
->bytesize
);
1406 debugfs_create_u32("timeout", 0444, dir
, &nbd
->tag_set
.timeout
);
1407 debugfs_create_u64("blocksize", 0444, dir
, &config
->blksize
);
1408 debugfs_create_file("flags", 0444, dir
, nbd
, &nbd_dbg_flags_ops
);
1413 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
1415 debugfs_remove_recursive(nbd
->config
->dbg_dir
);
1418 static int nbd_dbg_init(void)
1420 struct dentry
*dbg_dir
;
1422 dbg_dir
= debugfs_create_dir("nbd", NULL
);
1426 nbd_dbg_dir
= dbg_dir
;
1431 static void nbd_dbg_close(void)
1433 debugfs_remove_recursive(nbd_dbg_dir
);
1436 #else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1438 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
1443 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
1447 static int nbd_dbg_init(void)
1452 static void nbd_dbg_close(void)
1458 static int nbd_init_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
1459 unsigned int hctx_idx
, unsigned int numa_node
)
1461 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
1462 cmd
->nbd
= set
->driver_data
;
1466 static const struct blk_mq_ops nbd_mq_ops
= {
1467 .queue_rq
= nbd_queue_rq
,
1468 .complete
= nbd_complete_rq
,
1469 .init_request
= nbd_init_request
,
1470 .timeout
= nbd_xmit_timeout
,
1473 static int nbd_dev_add(int index
)
1475 struct nbd_device
*nbd
;
1476 struct gendisk
*disk
;
1477 struct request_queue
*q
;
1480 nbd
= kzalloc(sizeof(struct nbd_device
), GFP_KERNEL
);
1484 disk
= alloc_disk(1 << part_shift
);
1489 err
= idr_alloc(&nbd_index_idr
, nbd
, index
, index
+ 1,
1494 err
= idr_alloc(&nbd_index_idr
, nbd
, 0, 0, GFP_KERNEL
);
1503 nbd
->tag_set
.ops
= &nbd_mq_ops
;
1504 nbd
->tag_set
.nr_hw_queues
= 1;
1505 nbd
->tag_set
.queue_depth
= 128;
1506 nbd
->tag_set
.numa_node
= NUMA_NO_NODE
;
1507 nbd
->tag_set
.cmd_size
= sizeof(struct nbd_cmd
);
1508 nbd
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
|
1509 BLK_MQ_F_SG_MERGE
| BLK_MQ_F_BLOCKING
;
1510 nbd
->tag_set
.driver_data
= nbd
;
1512 err
= blk_mq_alloc_tag_set(&nbd
->tag_set
);
1516 q
= blk_mq_init_queue(&nbd
->tag_set
);
1524 * Tell the block layer that we are not a rotational device
1526 blk_queue_flag_set(QUEUE_FLAG_NONROT
, disk
->queue
);
1527 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, disk
->queue
);
1528 disk
->queue
->limits
.discard_granularity
= 0;
1529 disk
->queue
->limits
.discard_alignment
= 0;
1530 blk_queue_max_discard_sectors(disk
->queue
, 0);
1531 blk_queue_max_segment_size(disk
->queue
, UINT_MAX
);
1532 blk_queue_max_segments(disk
->queue
, USHRT_MAX
);
1533 blk_queue_max_hw_sectors(disk
->queue
, 65536);
1534 disk
->queue
->limits
.max_sectors
= 256;
1536 mutex_init(&nbd
->config_lock
);
1537 refcount_set(&nbd
->config_refs
, 0);
1538 refcount_set(&nbd
->refs
, 1);
1539 INIT_LIST_HEAD(&nbd
->list
);
1540 disk
->major
= NBD_MAJOR
;
1541 disk
->first_minor
= index
<< part_shift
;
1542 disk
->fops
= &nbd_fops
;
1543 disk
->private_data
= nbd
;
1544 sprintf(disk
->disk_name
, "nbd%d", index
);
1546 nbd_total_devices
++;
1550 blk_mq_free_tag_set(&nbd
->tag_set
);
1552 idr_remove(&nbd_index_idr
, index
);
1561 static int find_free_cb(int id
, void *ptr
, void *data
)
1563 struct nbd_device
*nbd
= ptr
;
1564 struct nbd_device
**found
= data
;
1566 if (!refcount_read(&nbd
->config_refs
)) {
1573 /* Netlink interface. */
1574 static struct nla_policy nbd_attr_policy
[NBD_ATTR_MAX
+ 1] = {
1575 [NBD_ATTR_INDEX
] = { .type
= NLA_U32
},
1576 [NBD_ATTR_SIZE_BYTES
] = { .type
= NLA_U64
},
1577 [NBD_ATTR_BLOCK_SIZE_BYTES
] = { .type
= NLA_U64
},
1578 [NBD_ATTR_TIMEOUT
] = { .type
= NLA_U64
},
1579 [NBD_ATTR_SERVER_FLAGS
] = { .type
= NLA_U64
},
1580 [NBD_ATTR_CLIENT_FLAGS
] = { .type
= NLA_U64
},
1581 [NBD_ATTR_SOCKETS
] = { .type
= NLA_NESTED
},
1582 [NBD_ATTR_DEAD_CONN_TIMEOUT
] = { .type
= NLA_U64
},
1583 [NBD_ATTR_DEVICE_LIST
] = { .type
= NLA_NESTED
},
1586 static struct nla_policy nbd_sock_policy
[NBD_SOCK_MAX
+ 1] = {
1587 [NBD_SOCK_FD
] = { .type
= NLA_U32
},
1590 /* We don't use this right now since we don't parse the incoming list, but we
1591 * still want it here so userspace knows what to expect.
1593 static struct nla_policy
__attribute__((unused
))
1594 nbd_device_policy
[NBD_DEVICE_ATTR_MAX
+ 1] = {
1595 [NBD_DEVICE_INDEX
] = { .type
= NLA_U32
},
1596 [NBD_DEVICE_CONNECTED
] = { .type
= NLA_U8
},
1599 static int nbd_genl_connect(struct sk_buff
*skb
, struct genl_info
*info
)
1601 struct nbd_device
*nbd
= NULL
;
1602 struct nbd_config
*config
;
1605 bool put_dev
= false;
1607 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1610 if (info
->attrs
[NBD_ATTR_INDEX
])
1611 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1612 if (!info
->attrs
[NBD_ATTR_SOCKETS
]) {
1613 printk(KERN_ERR
"nbd: must specify at least one socket\n");
1616 if (!info
->attrs
[NBD_ATTR_SIZE_BYTES
]) {
1617 printk(KERN_ERR
"nbd: must specify a size in bytes for the device\n");
1621 mutex_lock(&nbd_index_mutex
);
1623 ret
= idr_for_each(&nbd_index_idr
, &find_free_cb
, &nbd
);
1626 new_index
= nbd_dev_add(-1);
1627 if (new_index
< 0) {
1628 mutex_unlock(&nbd_index_mutex
);
1629 printk(KERN_ERR
"nbd: failed to add new device\n");
1632 nbd
= idr_find(&nbd_index_idr
, new_index
);
1635 nbd
= idr_find(&nbd_index_idr
, index
);
1637 ret
= nbd_dev_add(index
);
1639 mutex_unlock(&nbd_index_mutex
);
1640 printk(KERN_ERR
"nbd: failed to add new device\n");
1643 nbd
= idr_find(&nbd_index_idr
, index
);
1647 printk(KERN_ERR
"nbd: couldn't find device at index %d\n",
1649 mutex_unlock(&nbd_index_mutex
);
1652 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1653 mutex_unlock(&nbd_index_mutex
);
1656 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1660 mutex_unlock(&nbd_index_mutex
);
1662 mutex_lock(&nbd
->config_lock
);
1663 if (refcount_read(&nbd
->config_refs
)) {
1664 mutex_unlock(&nbd
->config_lock
);
1668 printk(KERN_ERR
"nbd: nbd%d already in use\n", index
);
1671 if (WARN_ON(nbd
->config
)) {
1672 mutex_unlock(&nbd
->config_lock
);
1676 config
= nbd
->config
= nbd_alloc_config();
1678 mutex_unlock(&nbd
->config_lock
);
1680 printk(KERN_ERR
"nbd: couldn't allocate config\n");
1683 refcount_set(&nbd
->config_refs
, 1);
1684 set_bit(NBD_BOUND
, &config
->runtime_flags
);
1686 if (info
->attrs
[NBD_ATTR_SIZE_BYTES
]) {
1687 u64 bytes
= nla_get_u64(info
->attrs
[NBD_ATTR_SIZE_BYTES
]);
1688 nbd_size_set(nbd
, config
->blksize
,
1689 div64_u64(bytes
, config
->blksize
));
1691 if (info
->attrs
[NBD_ATTR_BLOCK_SIZE_BYTES
]) {
1693 nla_get_u64(info
->attrs
[NBD_ATTR_BLOCK_SIZE_BYTES
]);
1694 nbd_size_set(nbd
, bsize
, div64_u64(config
->bytesize
, bsize
));
1696 if (info
->attrs
[NBD_ATTR_TIMEOUT
]) {
1697 u64 timeout
= nla_get_u64(info
->attrs
[NBD_ATTR_TIMEOUT
]);
1698 nbd
->tag_set
.timeout
= timeout
* HZ
;
1699 blk_queue_rq_timeout(nbd
->disk
->queue
, timeout
* HZ
);
1701 if (info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]) {
1702 config
->dead_conn_timeout
=
1703 nla_get_u64(info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]);
1704 config
->dead_conn_timeout
*= HZ
;
1706 if (info
->attrs
[NBD_ATTR_SERVER_FLAGS
])
1708 nla_get_u64(info
->attrs
[NBD_ATTR_SERVER_FLAGS
]);
1709 if (info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]) {
1710 u64 flags
= nla_get_u64(info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]);
1711 if (flags
& NBD_CFLAG_DESTROY_ON_DISCONNECT
) {
1712 set_bit(NBD_DESTROY_ON_DISCONNECT
,
1713 &config
->runtime_flags
);
1716 if (flags
& NBD_CFLAG_DISCONNECT_ON_CLOSE
) {
1717 set_bit(NBD_DISCONNECT_ON_CLOSE
,
1718 &config
->runtime_flags
);
1722 if (info
->attrs
[NBD_ATTR_SOCKETS
]) {
1723 struct nlattr
*attr
;
1726 nla_for_each_nested(attr
, info
->attrs
[NBD_ATTR_SOCKETS
],
1728 struct nlattr
*socks
[NBD_SOCK_MAX
+1];
1730 if (nla_type(attr
) != NBD_SOCK_ITEM
) {
1731 printk(KERN_ERR
"nbd: socks must be embedded in a SOCK_ITEM attr\n");
1735 ret
= nla_parse_nested(socks
, NBD_SOCK_MAX
, attr
,
1736 nbd_sock_policy
, info
->extack
);
1738 printk(KERN_ERR
"nbd: error processing sock list\n");
1742 if (!socks
[NBD_SOCK_FD
])
1744 fd
= (int)nla_get_u32(socks
[NBD_SOCK_FD
]);
1745 ret
= nbd_add_socket(nbd
, fd
, true);
1750 ret
= nbd_start_device(nbd
);
1752 mutex_unlock(&nbd
->config_lock
);
1754 set_bit(NBD_HAS_CONFIG_REF
, &config
->runtime_flags
);
1755 refcount_inc(&nbd
->config_refs
);
1756 nbd_connect_reply(info
, nbd
->index
);
1758 nbd_config_put(nbd
);
1764 static void nbd_disconnect_and_put(struct nbd_device
*nbd
)
1766 mutex_lock(&nbd
->config_lock
);
1767 nbd_disconnect(nbd
);
1768 nbd_clear_sock(nbd
);
1769 mutex_unlock(&nbd
->config_lock
);
1770 if (test_and_clear_bit(NBD_HAS_CONFIG_REF
,
1771 &nbd
->config
->runtime_flags
))
1772 nbd_config_put(nbd
);
1775 static int nbd_genl_disconnect(struct sk_buff
*skb
, struct genl_info
*info
)
1777 struct nbd_device
*nbd
;
1780 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1783 if (!info
->attrs
[NBD_ATTR_INDEX
]) {
1784 printk(KERN_ERR
"nbd: must specify an index to disconnect\n");
1787 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1788 mutex_lock(&nbd_index_mutex
);
1789 nbd
= idr_find(&nbd_index_idr
, index
);
1791 mutex_unlock(&nbd_index_mutex
);
1792 printk(KERN_ERR
"nbd: couldn't find device at index %d\n",
1796 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1797 mutex_unlock(&nbd_index_mutex
);
1798 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1802 mutex_unlock(&nbd_index_mutex
);
1803 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1807 nbd_disconnect_and_put(nbd
);
1808 nbd_config_put(nbd
);
1813 static int nbd_genl_reconfigure(struct sk_buff
*skb
, struct genl_info
*info
)
1815 struct nbd_device
*nbd
= NULL
;
1816 struct nbd_config
*config
;
1819 bool put_dev
= false;
1821 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1824 if (!info
->attrs
[NBD_ATTR_INDEX
]) {
1825 printk(KERN_ERR
"nbd: must specify a device to reconfigure\n");
1828 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1829 mutex_lock(&nbd_index_mutex
);
1830 nbd
= idr_find(&nbd_index_idr
, index
);
1832 mutex_unlock(&nbd_index_mutex
);
1833 printk(KERN_ERR
"nbd: couldn't find a device at index %d\n",
1837 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1838 mutex_unlock(&nbd_index_mutex
);
1839 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1843 mutex_unlock(&nbd_index_mutex
);
1845 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1846 dev_err(nbd_to_dev(nbd
),
1847 "not configured, cannot reconfigure\n");
1852 mutex_lock(&nbd
->config_lock
);
1853 config
= nbd
->config
;
1854 if (!test_bit(NBD_BOUND
, &config
->runtime_flags
) ||
1856 dev_err(nbd_to_dev(nbd
),
1857 "not configured, cannot reconfigure\n");
1862 if (info
->attrs
[NBD_ATTR_TIMEOUT
]) {
1863 u64 timeout
= nla_get_u64(info
->attrs
[NBD_ATTR_TIMEOUT
]);
1864 nbd
->tag_set
.timeout
= timeout
* HZ
;
1865 blk_queue_rq_timeout(nbd
->disk
->queue
, timeout
* HZ
);
1867 if (info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]) {
1868 config
->dead_conn_timeout
=
1869 nla_get_u64(info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]);
1870 config
->dead_conn_timeout
*= HZ
;
1872 if (info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]) {
1873 u64 flags
= nla_get_u64(info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]);
1874 if (flags
& NBD_CFLAG_DESTROY_ON_DISCONNECT
) {
1875 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT
,
1876 &config
->runtime_flags
))
1879 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT
,
1880 &config
->runtime_flags
))
1881 refcount_inc(&nbd
->refs
);
1884 if (flags
& NBD_CFLAG_DISCONNECT_ON_CLOSE
) {
1885 set_bit(NBD_DISCONNECT_ON_CLOSE
,
1886 &config
->runtime_flags
);
1888 clear_bit(NBD_DISCONNECT_ON_CLOSE
,
1889 &config
->runtime_flags
);
1893 if (info
->attrs
[NBD_ATTR_SOCKETS
]) {
1894 struct nlattr
*attr
;
1897 nla_for_each_nested(attr
, info
->attrs
[NBD_ATTR_SOCKETS
],
1899 struct nlattr
*socks
[NBD_SOCK_MAX
+1];
1901 if (nla_type(attr
) != NBD_SOCK_ITEM
) {
1902 printk(KERN_ERR
"nbd: socks must be embedded in a SOCK_ITEM attr\n");
1906 ret
= nla_parse_nested(socks
, NBD_SOCK_MAX
, attr
,
1907 nbd_sock_policy
, info
->extack
);
1909 printk(KERN_ERR
"nbd: error processing sock list\n");
1913 if (!socks
[NBD_SOCK_FD
])
1915 fd
= (int)nla_get_u32(socks
[NBD_SOCK_FD
]);
1916 ret
= nbd_reconnect_socket(nbd
, fd
);
1922 dev_info(nbd_to_dev(nbd
), "reconnected socket\n");
1926 mutex_unlock(&nbd
->config_lock
);
1927 nbd_config_put(nbd
);
1934 static const struct genl_ops nbd_connect_genl_ops
[] = {
1936 .cmd
= NBD_CMD_CONNECT
,
1937 .policy
= nbd_attr_policy
,
1938 .doit
= nbd_genl_connect
,
1941 .cmd
= NBD_CMD_DISCONNECT
,
1942 .policy
= nbd_attr_policy
,
1943 .doit
= nbd_genl_disconnect
,
1946 .cmd
= NBD_CMD_RECONFIGURE
,
1947 .policy
= nbd_attr_policy
,
1948 .doit
= nbd_genl_reconfigure
,
1951 .cmd
= NBD_CMD_STATUS
,
1952 .policy
= nbd_attr_policy
,
1953 .doit
= nbd_genl_status
,
1957 static const struct genl_multicast_group nbd_mcast_grps
[] = {
1958 { .name
= NBD_GENL_MCAST_GROUP_NAME
, },
1961 static struct genl_family nbd_genl_family __ro_after_init
= {
1963 .name
= NBD_GENL_FAMILY_NAME
,
1964 .version
= NBD_GENL_VERSION
,
1965 .module
= THIS_MODULE
,
1966 .ops
= nbd_connect_genl_ops
,
1967 .n_ops
= ARRAY_SIZE(nbd_connect_genl_ops
),
1968 .maxattr
= NBD_ATTR_MAX
,
1969 .mcgrps
= nbd_mcast_grps
,
1970 .n_mcgrps
= ARRAY_SIZE(nbd_mcast_grps
),
1973 static int populate_nbd_status(struct nbd_device
*nbd
, struct sk_buff
*reply
)
1975 struct nlattr
*dev_opt
;
1979 /* This is a little racey, but for status it's ok. The
1980 * reason we don't take a ref here is because we can't
1981 * take a ref in the index == -1 case as we would need
1982 * to put under the nbd_index_mutex, which could
1983 * deadlock if we are configured to remove ourselves
1984 * once we're disconnected.
1986 if (refcount_read(&nbd
->config_refs
))
1988 dev_opt
= nla_nest_start(reply
, NBD_DEVICE_ITEM
);
1991 ret
= nla_put_u32(reply
, NBD_DEVICE_INDEX
, nbd
->index
);
1994 ret
= nla_put_u8(reply
, NBD_DEVICE_CONNECTED
,
1998 nla_nest_end(reply
, dev_opt
);
2002 static int status_cb(int id
, void *ptr
, void *data
)
2004 struct nbd_device
*nbd
= ptr
;
2005 return populate_nbd_status(nbd
, (struct sk_buff
*)data
);
2008 static int nbd_genl_status(struct sk_buff
*skb
, struct genl_info
*info
)
2010 struct nlattr
*dev_list
;
2011 struct sk_buff
*reply
;
2017 if (info
->attrs
[NBD_ATTR_INDEX
])
2018 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
2020 mutex_lock(&nbd_index_mutex
);
2022 msg_size
= nla_total_size(nla_attr_size(sizeof(u32
)) +
2023 nla_attr_size(sizeof(u8
)));
2024 msg_size
*= (index
== -1) ? nbd_total_devices
: 1;
2026 reply
= genlmsg_new(msg_size
, GFP_KERNEL
);
2029 reply_head
= genlmsg_put_reply(reply
, info
, &nbd_genl_family
, 0,
2036 dev_list
= nla_nest_start(reply
, NBD_ATTR_DEVICE_LIST
);
2038 ret
= idr_for_each(&nbd_index_idr
, &status_cb
, reply
);
2044 struct nbd_device
*nbd
;
2045 nbd
= idr_find(&nbd_index_idr
, index
);
2047 ret
= populate_nbd_status(nbd
, reply
);
2054 nla_nest_end(reply
, dev_list
);
2055 genlmsg_end(reply
, reply_head
);
2056 genlmsg_reply(reply
, info
);
2059 mutex_unlock(&nbd_index_mutex
);
2063 static void nbd_connect_reply(struct genl_info
*info
, int index
)
2065 struct sk_buff
*skb
;
2069 skb
= genlmsg_new(nla_total_size(sizeof(u32
)), GFP_KERNEL
);
2072 msg_head
= genlmsg_put_reply(skb
, info
, &nbd_genl_family
, 0,
2078 ret
= nla_put_u32(skb
, NBD_ATTR_INDEX
, index
);
2083 genlmsg_end(skb
, msg_head
);
2084 genlmsg_reply(skb
, info
);
2087 static void nbd_mcast_index(int index
)
2089 struct sk_buff
*skb
;
2093 skb
= genlmsg_new(nla_total_size(sizeof(u32
)), GFP_KERNEL
);
2096 msg_head
= genlmsg_put(skb
, 0, 0, &nbd_genl_family
, 0,
2102 ret
= nla_put_u32(skb
, NBD_ATTR_INDEX
, index
);
2107 genlmsg_end(skb
, msg_head
);
2108 genlmsg_multicast(&nbd_genl_family
, skb
, 0, 0, GFP_KERNEL
);
2111 static void nbd_dead_link_work(struct work_struct
*work
)
2113 struct link_dead_args
*args
= container_of(work
, struct link_dead_args
,
2115 nbd_mcast_index(args
->index
);
2119 static int __init
nbd_init(void)
2123 BUILD_BUG_ON(sizeof(struct nbd_request
) != 28);
2126 printk(KERN_ERR
"nbd: max_part must be >= 0\n");
2132 part_shift
= fls(max_part
);
2135 * Adjust max_part according to part_shift as it is exported
2136 * to user space so that user can know the max number of
2137 * partition kernel should be able to manage.
2139 * Note that -1 is required because partition 0 is reserved
2140 * for the whole disk.
2142 max_part
= (1UL << part_shift
) - 1;
2145 if ((1UL << part_shift
) > DISK_MAX_PARTS
)
2148 if (nbds_max
> 1UL << (MINORBITS
- part_shift
))
2150 recv_workqueue
= alloc_workqueue("knbd-recv",
2151 WQ_MEM_RECLAIM
| WQ_HIGHPRI
|
2153 if (!recv_workqueue
)
2156 if (register_blkdev(NBD_MAJOR
, "nbd")) {
2157 destroy_workqueue(recv_workqueue
);
2161 if (genl_register_family(&nbd_genl_family
)) {
2162 unregister_blkdev(NBD_MAJOR
, "nbd");
2163 destroy_workqueue(recv_workqueue
);
2168 mutex_lock(&nbd_index_mutex
);
2169 for (i
= 0; i
< nbds_max
; i
++)
2171 mutex_unlock(&nbd_index_mutex
);
2175 static int nbd_exit_cb(int id
, void *ptr
, void *data
)
2177 struct list_head
*list
= (struct list_head
*)data
;
2178 struct nbd_device
*nbd
= ptr
;
2180 list_add_tail(&nbd
->list
, list
);
2184 static void __exit
nbd_cleanup(void)
2186 struct nbd_device
*nbd
;
2187 LIST_HEAD(del_list
);
2191 mutex_lock(&nbd_index_mutex
);
2192 idr_for_each(&nbd_index_idr
, &nbd_exit_cb
, &del_list
);
2193 mutex_unlock(&nbd_index_mutex
);
2195 while (!list_empty(&del_list
)) {
2196 nbd
= list_first_entry(&del_list
, struct nbd_device
, list
);
2197 list_del_init(&nbd
->list
);
2198 if (refcount_read(&nbd
->refs
) != 1)
2199 printk(KERN_ERR
"nbd: possibly leaking a device\n");
2203 idr_destroy(&nbd_index_idr
);
2204 genl_unregister_family(&nbd_genl_family
);
2205 destroy_workqueue(recv_workqueue
);
2206 unregister_blkdev(NBD_MAJOR
, "nbd");
2209 module_init(nbd_init
);
2210 module_exit(nbd_cleanup
);
2212 MODULE_DESCRIPTION("Network Block Device");
2213 MODULE_LICENSE("GPL");
2215 module_param(nbds_max
, int, 0444);
2216 MODULE_PARM_DESC(nbds_max
, "number of network block devices to initialize (default: 16)");
2217 module_param(max_part
, int, 0444);
2218 MODULE_PARM_DESC(max_part
, "number of partitions per device (default: 16)");