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;
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
;
246 nbd_size_update(nbd
);
249 static void nbd_complete_rq(struct request
*req
)
251 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
253 dev_dbg(nbd_to_dev(cmd
->nbd
), "request %p: %s\n", cmd
,
254 cmd
->status
? "failed" : "done");
256 blk_mq_end_request(req
, cmd
->status
);
260 * Forcibly shutdown the socket causing all listeners to error
262 static void sock_shutdown(struct nbd_device
*nbd
)
264 struct nbd_config
*config
= nbd
->config
;
267 if (config
->num_connections
== 0)
269 if (test_and_set_bit(NBD_DISCONNECTED
, &config
->runtime_flags
))
272 for (i
= 0; i
< config
->num_connections
; i
++) {
273 struct nbd_sock
*nsock
= config
->socks
[i
];
274 mutex_lock(&nsock
->tx_lock
);
275 nbd_mark_nsock_dead(nbd
, nsock
, 0);
276 mutex_unlock(&nsock
->tx_lock
);
278 dev_warn(disk_to_dev(nbd
->disk
), "shutting down sockets\n");
281 static enum blk_eh_timer_return
nbd_xmit_timeout(struct request
*req
,
284 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
285 struct nbd_device
*nbd
= cmd
->nbd
;
286 struct nbd_config
*config
;
288 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
290 return BLK_EH_HANDLED
;
293 /* If we are waiting on our dead timer then we could get timeout
294 * callbacks for our request. For this we just want to reset the timer
295 * and let the queue side take care of everything.
297 if (!completion_done(&cmd
->send_complete
)) {
299 return BLK_EH_RESET_TIMER
;
301 config
= nbd
->config
;
303 if (config
->num_connections
> 1) {
304 dev_err_ratelimited(nbd_to_dev(nbd
),
305 "Connection timed out, retrying\n");
307 * Hooray we have more connections, requeue this IO, the submit
308 * path will put it on a real connection.
310 if (config
->socks
&& config
->num_connections
> 1) {
311 if (cmd
->index
< config
->num_connections
) {
312 struct nbd_sock
*nsock
=
313 config
->socks
[cmd
->index
];
314 mutex_lock(&nsock
->tx_lock
);
315 /* We can have multiple outstanding requests, so
316 * we don't want to mark the nsock dead if we've
317 * already reconnected with a new socket, so
318 * only mark it dead if its the same socket we
321 if (cmd
->cookie
== nsock
->cookie
)
322 nbd_mark_nsock_dead(nbd
, nsock
, 1);
323 mutex_unlock(&nsock
->tx_lock
);
325 blk_mq_requeue_request(req
, true);
327 return BLK_EH_NOT_HANDLED
;
330 dev_err_ratelimited(nbd_to_dev(nbd
),
331 "Connection timed out\n");
333 set_bit(NBD_TIMEDOUT
, &config
->runtime_flags
);
338 return BLK_EH_HANDLED
;
342 * Send or receive packet.
344 static int sock_xmit(struct nbd_device
*nbd
, int index
, int send
,
345 struct iov_iter
*iter
, int msg_flags
, int *sent
)
347 struct nbd_config
*config
= nbd
->config
;
348 struct socket
*sock
= config
->socks
[index
]->sock
;
351 unsigned int noreclaim_flag
;
353 if (unlikely(!sock
)) {
354 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
355 "Attempted %s on closed socket in sock_xmit\n",
356 (send
? "send" : "recv"));
360 msg
.msg_iter
= *iter
;
362 noreclaim_flag
= memalloc_noreclaim_save();
364 sock
->sk
->sk_allocation
= GFP_NOIO
| __GFP_MEMALLOC
;
367 msg
.msg_control
= NULL
;
368 msg
.msg_controllen
= 0;
369 msg
.msg_flags
= msg_flags
| MSG_NOSIGNAL
;
372 result
= sock_sendmsg(sock
, &msg
);
374 result
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
378 result
= -EPIPE
; /* short read */
383 } while (msg_data_left(&msg
));
385 memalloc_noreclaim_restore(noreclaim_flag
);
390 /* always call with the tx_lock held */
391 static int nbd_send_cmd(struct nbd_device
*nbd
, struct nbd_cmd
*cmd
, int index
)
393 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
394 struct nbd_config
*config
= nbd
->config
;
395 struct nbd_sock
*nsock
= config
->socks
[index
];
397 struct nbd_request request
= {.magic
= htonl(NBD_REQUEST_MAGIC
)};
398 struct kvec iov
= {.iov_base
= &request
, .iov_len
= sizeof(request
)};
399 struct iov_iter from
;
400 unsigned long size
= blk_rq_bytes(req
);
403 u32 tag
= blk_mq_unique_tag(req
);
404 int sent
= nsock
->sent
, skip
= 0;
406 iov_iter_kvec(&from
, WRITE
| ITER_KVEC
, &iov
, 1, sizeof(request
));
408 switch (req_op(req
)) {
413 type
= NBD_CMD_FLUSH
;
416 type
= NBD_CMD_WRITE
;
425 if (rq_data_dir(req
) == WRITE
&&
426 (config
->flags
& NBD_FLAG_READ_ONLY
)) {
427 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
428 "Write on read-only\n");
432 /* We did a partial send previously, and we at least sent the whole
433 * request struct, so just go and send the rest of the pages in the
437 if (sent
>= sizeof(request
)) {
438 skip
= sent
- sizeof(request
);
441 iov_iter_advance(&from
, sent
);
444 cmd
->cookie
= nsock
->cookie
;
445 request
.type
= htonl(type
);
446 if (type
!= NBD_CMD_FLUSH
) {
447 request
.from
= cpu_to_be64((u64
)blk_rq_pos(req
) << 9);
448 request
.len
= htonl(size
);
450 memcpy(request
.handle
, &tag
, sizeof(tag
));
452 dev_dbg(nbd_to_dev(nbd
), "request %p: sending control (%s@%llu,%uB)\n",
453 cmd
, nbdcmd_to_ascii(type
),
454 (unsigned long long)blk_rq_pos(req
) << 9, blk_rq_bytes(req
));
455 result
= sock_xmit(nbd
, index
, 1, &from
,
456 (type
== NBD_CMD_WRITE
) ? MSG_MORE
: 0, &sent
);
458 if (result
== -ERESTARTSYS
) {
459 /* If we havne't sent anything we can just return BUSY,
460 * however if we have sent something we need to make
461 * sure we only allow this req to be sent until we are
465 nsock
->pending
= req
;
468 return BLK_MQ_RQ_QUEUE_BUSY
;
470 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
471 "Send control failed (result %d)\n", result
);
475 if (type
!= NBD_CMD_WRITE
)
480 struct bio
*next
= bio
->bi_next
;
481 struct bvec_iter iter
;
484 bio_for_each_segment(bvec
, bio
, iter
) {
485 bool is_last
= !next
&& bio_iter_last(bvec
, iter
);
486 int flags
= is_last
? 0 : MSG_MORE
;
488 dev_dbg(nbd_to_dev(nbd
), "request %p: sending %d bytes data\n",
490 iov_iter_bvec(&from
, ITER_BVEC
| WRITE
,
491 &bvec
, 1, bvec
.bv_len
);
493 if (skip
>= iov_iter_count(&from
)) {
494 skip
-= iov_iter_count(&from
);
497 iov_iter_advance(&from
, skip
);
500 result
= sock_xmit(nbd
, index
, 1, &from
, flags
, &sent
);
502 if (result
== -ERESTARTSYS
) {
503 /* We've already sent the header, we
504 * have no choice but to set pending and
507 nsock
->pending
= req
;
509 return BLK_MQ_RQ_QUEUE_BUSY
;
511 dev_err(disk_to_dev(nbd
->disk
),
512 "Send data failed (result %d)\n",
517 * The completion might already have come in,
518 * so break for the last one instead of letting
519 * the iterator do it. This prevents use-after-free
528 nsock
->pending
= NULL
;
533 /* NULL returned = something went wrong, inform userspace */
534 static struct nbd_cmd
*nbd_read_stat(struct nbd_device
*nbd
, int index
)
536 struct nbd_config
*config
= nbd
->config
;
538 struct nbd_reply reply
;
540 struct request
*req
= NULL
;
543 struct kvec iov
= {.iov_base
= &reply
, .iov_len
= sizeof(reply
)};
547 iov_iter_kvec(&to
, READ
| ITER_KVEC
, &iov
, 1, sizeof(reply
));
548 result
= sock_xmit(nbd
, index
, 0, &to
, MSG_WAITALL
, NULL
);
550 if (!nbd_disconnected(config
))
551 dev_err(disk_to_dev(nbd
->disk
),
552 "Receive control failed (result %d)\n", result
);
553 return ERR_PTR(result
);
556 if (ntohl(reply
.magic
) != NBD_REPLY_MAGIC
) {
557 dev_err(disk_to_dev(nbd
->disk
), "Wrong magic (0x%lx)\n",
558 (unsigned long)ntohl(reply
.magic
));
559 return ERR_PTR(-EPROTO
);
562 memcpy(&tag
, reply
.handle
, sizeof(u32
));
564 hwq
= blk_mq_unique_tag_to_hwq(tag
);
565 if (hwq
< nbd
->tag_set
.nr_hw_queues
)
566 req
= blk_mq_tag_to_rq(nbd
->tag_set
.tags
[hwq
],
567 blk_mq_unique_tag_to_tag(tag
));
568 if (!req
|| !blk_mq_request_started(req
)) {
569 dev_err(disk_to_dev(nbd
->disk
), "Unexpected reply (%d) %p\n",
571 return ERR_PTR(-ENOENT
);
573 cmd
= blk_mq_rq_to_pdu(req
);
574 if (ntohl(reply
.error
)) {
575 dev_err(disk_to_dev(nbd
->disk
), "Other side returned error (%d)\n",
581 dev_dbg(nbd_to_dev(nbd
), "request %p: got reply\n", cmd
);
582 if (rq_data_dir(req
) != WRITE
) {
583 struct req_iterator iter
;
586 rq_for_each_segment(bvec
, req
, iter
) {
587 iov_iter_bvec(&to
, ITER_BVEC
| READ
,
588 &bvec
, 1, bvec
.bv_len
);
589 result
= sock_xmit(nbd
, index
, 0, &to
, MSG_WAITALL
, NULL
);
591 dev_err(disk_to_dev(nbd
->disk
), "Receive data failed (result %d)\n",
594 * If we've disconnected or we only have 1
595 * connection then we need to make sure we
596 * complete this request, otherwise error out
597 * and let the timeout stuff handle resubmitting
598 * this request onto another connection.
600 if (nbd_disconnected(config
) ||
601 config
->num_connections
<= 1) {
605 return ERR_PTR(-EIO
);
607 dev_dbg(nbd_to_dev(nbd
), "request %p: got %d bytes data\n",
611 /* See the comment in nbd_queue_rq. */
612 wait_for_completion(&cmd
->send_complete
);
617 static void recv_work(struct work_struct
*work
)
619 struct recv_thread_args
*args
= container_of(work
,
620 struct recv_thread_args
,
622 struct nbd_device
*nbd
= args
->nbd
;
623 struct nbd_config
*config
= nbd
->config
;
628 cmd
= nbd_read_stat(nbd
, args
->index
);
630 struct nbd_sock
*nsock
= config
->socks
[args
->index
];
632 mutex_lock(&nsock
->tx_lock
);
633 nbd_mark_nsock_dead(nbd
, nsock
, 1);
634 mutex_unlock(&nsock
->tx_lock
);
639 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd
));
641 atomic_dec(&config
->recv_threads
);
642 wake_up(&config
->recv_wq
);
647 static void nbd_clear_req(struct request
*req
, void *data
, bool reserved
)
651 if (!blk_mq_request_started(req
))
653 cmd
= blk_mq_rq_to_pdu(req
);
655 blk_mq_complete_request(req
);
658 static void nbd_clear_que(struct nbd_device
*nbd
)
660 blk_mq_stop_hw_queues(nbd
->disk
->queue
);
661 blk_mq_tagset_busy_iter(&nbd
->tag_set
, nbd_clear_req
, NULL
);
662 blk_mq_start_hw_queues(nbd
->disk
->queue
);
663 dev_dbg(disk_to_dev(nbd
->disk
), "queue cleared\n");
666 static int find_fallback(struct nbd_device
*nbd
, int index
)
668 struct nbd_config
*config
= nbd
->config
;
670 struct nbd_sock
*nsock
= config
->socks
[index
];
671 int fallback
= nsock
->fallback_index
;
673 if (test_bit(NBD_DISCONNECTED
, &config
->runtime_flags
))
676 if (config
->num_connections
<= 1) {
677 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
678 "Attempted send on invalid socket\n");
682 if (fallback
>= 0 && fallback
< config
->num_connections
&&
683 !config
->socks
[fallback
]->dead
)
686 if (nsock
->fallback_index
< 0 ||
687 nsock
->fallback_index
>= config
->num_connections
||
688 config
->socks
[nsock
->fallback_index
]->dead
) {
690 for (i
= 0; i
< config
->num_connections
; i
++) {
693 if (!config
->socks
[i
]->dead
) {
698 nsock
->fallback_index
= new_index
;
700 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
701 "Dead connection, failed to find a fallback\n");
705 new_index
= nsock
->fallback_index
;
709 static int wait_for_reconnect(struct nbd_device
*nbd
)
711 struct nbd_config
*config
= nbd
->config
;
712 if (!config
->dead_conn_timeout
)
714 if (test_bit(NBD_DISCONNECTED
, &config
->runtime_flags
))
716 wait_event_interruptible_timeout(config
->conn_wait
,
717 atomic_read(&config
->live_connections
),
718 config
->dead_conn_timeout
);
719 return atomic_read(&config
->live_connections
);
722 static int nbd_handle_cmd(struct nbd_cmd
*cmd
, int index
)
724 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
725 struct nbd_device
*nbd
= cmd
->nbd
;
726 struct nbd_config
*config
;
727 struct nbd_sock
*nsock
;
730 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
731 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
732 "Socks array is empty\n");
735 config
= nbd
->config
;
737 if (index
>= config
->num_connections
) {
738 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
739 "Attempted send on invalid socket\n");
745 nsock
= config
->socks
[index
];
746 mutex_lock(&nsock
->tx_lock
);
748 int old_index
= index
;
749 index
= find_fallback(nbd
, index
);
750 mutex_unlock(&nsock
->tx_lock
);
752 if (wait_for_reconnect(nbd
)) {
756 /* All the sockets should already be down at this point,
757 * we just want to make sure that DISCONNECTED is set so
758 * any requests that come in that were queue'ed waiting
759 * for the reconnect timer don't trigger the timer again
760 * and instead just error out.
769 /* Handle the case that we have a pending request that was partially
770 * transmitted that _has_ to be serviced first. We need to call requeue
771 * here so that it gets put _after_ the request that is already on the
774 if (unlikely(nsock
->pending
&& nsock
->pending
!= req
)) {
775 blk_mq_requeue_request(req
, true);
780 * Some failures are related to the link going down, so anything that
781 * returns EAGAIN can be retried on a different socket.
783 ret
= nbd_send_cmd(nbd
, cmd
, index
);
784 if (ret
== -EAGAIN
) {
785 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
786 "Request send failed trying another connection\n");
787 nbd_mark_nsock_dead(nbd
, nsock
, 1);
788 mutex_unlock(&nsock
->tx_lock
);
792 mutex_unlock(&nsock
->tx_lock
);
797 static int nbd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
798 const struct blk_mq_queue_data
*bd
)
800 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(bd
->rq
);
804 * Since we look at the bio's to send the request over the network we
805 * need to make sure the completion work doesn't mark this request done
806 * before we are done doing our send. This keeps us from dereferencing
807 * freed data if we have particularly fast completions (ie we get the
808 * completion before we exit sock_xmit on the last bvec) or in the case
809 * that the server is misbehaving (or there was an error) before we're
810 * done sending everything over the wire.
812 init_completion(&cmd
->send_complete
);
813 blk_mq_start_request(bd
->rq
);
815 /* We can be called directly from the user space process, which means we
816 * could possibly have signals pending so our sendmsg will fail. In
817 * this case we need to return that we are busy, otherwise error out as
820 ret
= nbd_handle_cmd(cmd
, hctx
->queue_num
);
822 ret
= BLK_MQ_RQ_QUEUE_ERROR
;
824 ret
= BLK_MQ_RQ_QUEUE_OK
;
825 complete(&cmd
->send_complete
);
830 static int nbd_add_socket(struct nbd_device
*nbd
, unsigned long arg
,
833 struct nbd_config
*config
= nbd
->config
;
835 struct nbd_sock
**socks
;
836 struct nbd_sock
*nsock
;
839 sock
= sockfd_lookup(arg
, &err
);
843 if (!netlink
&& !nbd
->task_setup
&&
844 !test_bit(NBD_BOUND
, &config
->runtime_flags
))
845 nbd
->task_setup
= current
;
848 (nbd
->task_setup
!= current
||
849 test_bit(NBD_BOUND
, &config
->runtime_flags
))) {
850 dev_err(disk_to_dev(nbd
->disk
),
851 "Device being setup by another task");
856 socks
= krealloc(config
->socks
, (config
->num_connections
+ 1) *
857 sizeof(struct nbd_sock
*), GFP_KERNEL
);
862 nsock
= kzalloc(sizeof(struct nbd_sock
), GFP_KERNEL
);
868 config
->socks
= socks
;
870 nsock
->fallback_index
= -1;
872 mutex_init(&nsock
->tx_lock
);
874 nsock
->pending
= NULL
;
877 socks
[config
->num_connections
++] = nsock
;
878 atomic_inc(&config
->live_connections
);
883 static int nbd_reconnect_socket(struct nbd_device
*nbd
, unsigned long arg
)
885 struct nbd_config
*config
= nbd
->config
;
886 struct socket
*sock
, *old
;
887 struct recv_thread_args
*args
;
891 sock
= sockfd_lookup(arg
, &err
);
895 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
901 for (i
= 0; i
< config
->num_connections
; i
++) {
902 struct nbd_sock
*nsock
= config
->socks
[i
];
907 mutex_lock(&nsock
->tx_lock
);
909 mutex_unlock(&nsock
->tx_lock
);
912 sk_set_memalloc(sock
->sk
);
913 atomic_inc(&config
->recv_threads
);
914 refcount_inc(&nbd
->config_refs
);
916 nsock
->fallback_index
= -1;
919 INIT_WORK(&args
->work
, recv_work
);
923 mutex_unlock(&nsock
->tx_lock
);
926 /* We take the tx_mutex in an error path in the recv_work, so we
927 * need to queue_work outside of the tx_mutex.
929 queue_work(recv_workqueue
, &args
->work
);
931 atomic_inc(&config
->live_connections
);
932 wake_up(&config
->conn_wait
);
940 /* Reset all properties of an NBD device */
941 static void nbd_reset(struct nbd_device
*nbd
)
944 nbd
->tag_set
.timeout
= 0;
945 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
948 static void nbd_bdev_reset(struct block_device
*bdev
)
950 if (bdev
->bd_openers
> 1)
952 bd_set_size(bdev
, 0);
954 blkdev_reread_part(bdev
);
955 bdev
->bd_invalidated
= 1;
959 static void nbd_parse_flags(struct nbd_device
*nbd
)
961 struct nbd_config
*config
= nbd
->config
;
962 if (config
->flags
& NBD_FLAG_READ_ONLY
)
963 set_disk_ro(nbd
->disk
, true);
965 set_disk_ro(nbd
->disk
, false);
966 if (config
->flags
& NBD_FLAG_SEND_TRIM
)
967 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
968 if (config
->flags
& NBD_FLAG_SEND_FLUSH
)
969 blk_queue_write_cache(nbd
->disk
->queue
, true, false);
971 blk_queue_write_cache(nbd
->disk
->queue
, false, false);
974 static void send_disconnects(struct nbd_device
*nbd
)
976 struct nbd_config
*config
= nbd
->config
;
977 struct nbd_request request
= {
978 .magic
= htonl(NBD_REQUEST_MAGIC
),
979 .type
= htonl(NBD_CMD_DISC
),
981 struct kvec iov
= {.iov_base
= &request
, .iov_len
= sizeof(request
)};
982 struct iov_iter from
;
985 for (i
= 0; i
< config
->num_connections
; i
++) {
986 iov_iter_kvec(&from
, WRITE
| ITER_KVEC
, &iov
, 1, sizeof(request
));
987 ret
= sock_xmit(nbd
, i
, 1, &from
, 0, NULL
);
989 dev_err(disk_to_dev(nbd
->disk
),
990 "Send disconnect failed %d\n", ret
);
994 static int nbd_disconnect(struct nbd_device
*nbd
)
996 struct nbd_config
*config
= nbd
->config
;
998 dev_info(disk_to_dev(nbd
->disk
), "NBD_DISCONNECT\n");
999 if (!test_and_set_bit(NBD_DISCONNECT_REQUESTED
,
1000 &config
->runtime_flags
))
1001 send_disconnects(nbd
);
1005 static void nbd_clear_sock(struct nbd_device
*nbd
)
1009 nbd
->task_setup
= NULL
;
1012 static void nbd_config_put(struct nbd_device
*nbd
)
1014 if (refcount_dec_and_mutex_lock(&nbd
->config_refs
,
1015 &nbd
->config_lock
)) {
1016 struct nbd_config
*config
= nbd
->config
;
1017 nbd_dev_dbg_close(nbd
);
1018 nbd_size_clear(nbd
);
1019 if (test_and_clear_bit(NBD_HAS_PID_FILE
,
1020 &config
->runtime_flags
))
1021 device_remove_file(disk_to_dev(nbd
->disk
), &pid_attr
);
1022 nbd
->task_recv
= NULL
;
1023 nbd_clear_sock(nbd
);
1024 if (config
->num_connections
) {
1026 for (i
= 0; i
< config
->num_connections
; i
++) {
1027 sockfd_put(config
->socks
[i
]->sock
);
1028 kfree(config
->socks
[i
]);
1030 kfree(config
->socks
);
1034 mutex_unlock(&nbd
->config_lock
);
1036 module_put(THIS_MODULE
);
1040 static int nbd_start_device(struct nbd_device
*nbd
)
1042 struct nbd_config
*config
= nbd
->config
;
1043 int num_connections
= config
->num_connections
;
1050 if (num_connections
> 1 &&
1051 !(config
->flags
& NBD_FLAG_CAN_MULTI_CONN
)) {
1052 dev_err(disk_to_dev(nbd
->disk
), "server does not support multiple connections per device.\n");
1056 blk_mq_update_nr_hw_queues(&nbd
->tag_set
, config
->num_connections
);
1057 nbd
->task_recv
= current
;
1059 nbd_parse_flags(nbd
);
1061 error
= device_create_file(disk_to_dev(nbd
->disk
), &pid_attr
);
1063 dev_err(disk_to_dev(nbd
->disk
), "device_create_file failed!\n");
1066 set_bit(NBD_HAS_PID_FILE
, &config
->runtime_flags
);
1068 nbd_dev_dbg_init(nbd
);
1069 for (i
= 0; i
< num_connections
; i
++) {
1070 struct recv_thread_args
*args
;
1072 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
1077 sk_set_memalloc(config
->socks
[i
]->sock
->sk
);
1078 atomic_inc(&config
->recv_threads
);
1079 refcount_inc(&nbd
->config_refs
);
1080 INIT_WORK(&args
->work
, recv_work
);
1083 queue_work(recv_workqueue
, &args
->work
);
1088 static int nbd_start_device_ioctl(struct nbd_device
*nbd
, struct block_device
*bdev
)
1090 struct nbd_config
*config
= nbd
->config
;
1093 ret
= nbd_start_device(nbd
);
1097 bd_set_size(bdev
, config
->bytesize
);
1099 bdev
->bd_invalidated
= 1;
1100 mutex_unlock(&nbd
->config_lock
);
1101 ret
= wait_event_interruptible(config
->recv_wq
,
1102 atomic_read(&config
->recv_threads
) == 0);
1105 mutex_lock(&nbd
->config_lock
);
1106 bd_set_size(bdev
, 0);
1107 /* user requested, ignore socket errors */
1108 if (test_bit(NBD_DISCONNECT_REQUESTED
, &config
->runtime_flags
))
1110 if (test_bit(NBD_TIMEDOUT
, &config
->runtime_flags
))
1115 static void nbd_clear_sock_ioctl(struct nbd_device
*nbd
,
1116 struct block_device
*bdev
)
1120 nbd_bdev_reset(bdev
);
1121 if (test_and_clear_bit(NBD_HAS_CONFIG_REF
,
1122 &nbd
->config
->runtime_flags
))
1123 nbd_config_put(nbd
);
1126 /* Must be called with config_lock held */
1127 static int __nbd_ioctl(struct block_device
*bdev
, struct nbd_device
*nbd
,
1128 unsigned int cmd
, unsigned long arg
)
1130 struct nbd_config
*config
= nbd
->config
;
1133 case NBD_DISCONNECT
:
1134 return nbd_disconnect(nbd
);
1135 case NBD_CLEAR_SOCK
:
1136 nbd_clear_sock_ioctl(nbd
, bdev
);
1139 return nbd_add_socket(nbd
, arg
, false);
1140 case NBD_SET_BLKSIZE
:
1141 nbd_size_set(nbd
, arg
,
1142 div_s64(config
->bytesize
, arg
));
1145 nbd_size_set(nbd
, config
->blksize
,
1146 div_s64(arg
, config
->blksize
));
1148 case NBD_SET_SIZE_BLOCKS
:
1149 nbd_size_set(nbd
, config
->blksize
, arg
);
1151 case NBD_SET_TIMEOUT
:
1153 nbd
->tag_set
.timeout
= arg
* HZ
;
1154 blk_queue_rq_timeout(nbd
->disk
->queue
, arg
* HZ
);
1159 config
->flags
= arg
;
1162 return nbd_start_device_ioctl(nbd
, bdev
);
1165 * This is for compatibility only. The queue is always cleared
1166 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1169 case NBD_PRINT_DEBUG
:
1171 * For compatibility only, we no longer keep a list of
1172 * outstanding requests.
1179 static int nbd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1180 unsigned int cmd
, unsigned long arg
)
1182 struct nbd_device
*nbd
= bdev
->bd_disk
->private_data
;
1183 struct nbd_config
*config
= nbd
->config
;
1184 int error
= -EINVAL
;
1186 if (!capable(CAP_SYS_ADMIN
))
1189 mutex_lock(&nbd
->config_lock
);
1191 /* Don't allow ioctl operations on a nbd device that was created with
1192 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1194 if (!test_bit(NBD_BOUND
, &config
->runtime_flags
) ||
1195 (cmd
== NBD_DISCONNECT
|| cmd
== NBD_CLEAR_SOCK
))
1196 error
= __nbd_ioctl(bdev
, nbd
, cmd
, arg
);
1198 dev_err(nbd_to_dev(nbd
), "Cannot use ioctl interface on a netlink controlled device.\n");
1199 mutex_unlock(&nbd
->config_lock
);
1203 static struct nbd_config
*nbd_alloc_config(void)
1205 struct nbd_config
*config
;
1207 config
= kzalloc(sizeof(struct nbd_config
), GFP_NOFS
);
1210 atomic_set(&config
->recv_threads
, 0);
1211 init_waitqueue_head(&config
->recv_wq
);
1212 init_waitqueue_head(&config
->conn_wait
);
1213 config
->blksize
= 1024;
1214 atomic_set(&config
->live_connections
, 0);
1215 try_module_get(THIS_MODULE
);
1219 static int nbd_open(struct block_device
*bdev
, fmode_t mode
)
1221 struct nbd_device
*nbd
;
1224 mutex_lock(&nbd_index_mutex
);
1225 nbd
= bdev
->bd_disk
->private_data
;
1230 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1234 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1235 struct nbd_config
*config
;
1237 mutex_lock(&nbd
->config_lock
);
1238 if (refcount_inc_not_zero(&nbd
->config_refs
)) {
1239 mutex_unlock(&nbd
->config_lock
);
1242 config
= nbd
->config
= nbd_alloc_config();
1245 mutex_unlock(&nbd
->config_lock
);
1248 refcount_set(&nbd
->config_refs
, 1);
1249 refcount_inc(&nbd
->refs
);
1250 mutex_unlock(&nbd
->config_lock
);
1253 mutex_unlock(&nbd_index_mutex
);
1257 static void nbd_release(struct gendisk
*disk
, fmode_t mode
)
1259 struct nbd_device
*nbd
= disk
->private_data
;
1260 nbd_config_put(nbd
);
1264 static const struct block_device_operations nbd_fops
=
1266 .owner
= THIS_MODULE
,
1268 .release
= nbd_release
,
1270 .compat_ioctl
= nbd_ioctl
,
1273 #if IS_ENABLED(CONFIG_DEBUG_FS)
1275 static int nbd_dbg_tasks_show(struct seq_file
*s
, void *unused
)
1277 struct nbd_device
*nbd
= s
->private;
1280 seq_printf(s
, "recv: %d\n", task_pid_nr(nbd
->task_recv
));
1285 static int nbd_dbg_tasks_open(struct inode
*inode
, struct file
*file
)
1287 return single_open(file
, nbd_dbg_tasks_show
, inode
->i_private
);
1290 static const struct file_operations nbd_dbg_tasks_ops
= {
1291 .open
= nbd_dbg_tasks_open
,
1293 .llseek
= seq_lseek
,
1294 .release
= single_release
,
1297 static int nbd_dbg_flags_show(struct seq_file
*s
, void *unused
)
1299 struct nbd_device
*nbd
= s
->private;
1300 u32 flags
= nbd
->config
->flags
;
1302 seq_printf(s
, "Hex: 0x%08x\n\n", flags
);
1304 seq_puts(s
, "Known flags:\n");
1306 if (flags
& NBD_FLAG_HAS_FLAGS
)
1307 seq_puts(s
, "NBD_FLAG_HAS_FLAGS\n");
1308 if (flags
& NBD_FLAG_READ_ONLY
)
1309 seq_puts(s
, "NBD_FLAG_READ_ONLY\n");
1310 if (flags
& NBD_FLAG_SEND_FLUSH
)
1311 seq_puts(s
, "NBD_FLAG_SEND_FLUSH\n");
1312 if (flags
& NBD_FLAG_SEND_TRIM
)
1313 seq_puts(s
, "NBD_FLAG_SEND_TRIM\n");
1318 static int nbd_dbg_flags_open(struct inode
*inode
, struct file
*file
)
1320 return single_open(file
, nbd_dbg_flags_show
, inode
->i_private
);
1323 static const struct file_operations nbd_dbg_flags_ops
= {
1324 .open
= nbd_dbg_flags_open
,
1326 .llseek
= seq_lseek
,
1327 .release
= single_release
,
1330 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
1333 struct nbd_config
*config
= nbd
->config
;
1338 dir
= debugfs_create_dir(nbd_name(nbd
), nbd_dbg_dir
);
1340 dev_err(nbd_to_dev(nbd
), "Failed to create debugfs dir for '%s'\n",
1344 config
->dbg_dir
= dir
;
1346 debugfs_create_file("tasks", 0444, dir
, nbd
, &nbd_dbg_tasks_ops
);
1347 debugfs_create_u64("size_bytes", 0444, dir
, &config
->bytesize
);
1348 debugfs_create_u32("timeout", 0444, dir
, &nbd
->tag_set
.timeout
);
1349 debugfs_create_u64("blocksize", 0444, dir
, &config
->blksize
);
1350 debugfs_create_file("flags", 0444, dir
, nbd
, &nbd_dbg_flags_ops
);
1355 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
1357 debugfs_remove_recursive(nbd
->config
->dbg_dir
);
1360 static int nbd_dbg_init(void)
1362 struct dentry
*dbg_dir
;
1364 dbg_dir
= debugfs_create_dir("nbd", NULL
);
1368 nbd_dbg_dir
= dbg_dir
;
1373 static void nbd_dbg_close(void)
1375 debugfs_remove_recursive(nbd_dbg_dir
);
1378 #else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1380 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
1385 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
1389 static int nbd_dbg_init(void)
1394 static void nbd_dbg_close(void)
1400 static int nbd_init_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
1401 unsigned int hctx_idx
, unsigned int numa_node
)
1403 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
1404 cmd
->nbd
= set
->driver_data
;
1408 static const struct blk_mq_ops nbd_mq_ops
= {
1409 .queue_rq
= nbd_queue_rq
,
1410 .complete
= nbd_complete_rq
,
1411 .init_request
= nbd_init_request
,
1412 .timeout
= nbd_xmit_timeout
,
1415 static int nbd_dev_add(int index
)
1417 struct nbd_device
*nbd
;
1418 struct gendisk
*disk
;
1419 struct request_queue
*q
;
1422 nbd
= kzalloc(sizeof(struct nbd_device
), GFP_KERNEL
);
1426 disk
= alloc_disk(1 << part_shift
);
1431 err
= idr_alloc(&nbd_index_idr
, nbd
, index
, index
+ 1,
1436 err
= idr_alloc(&nbd_index_idr
, nbd
, 0, 0, GFP_KERNEL
);
1445 nbd
->tag_set
.ops
= &nbd_mq_ops
;
1446 nbd
->tag_set
.nr_hw_queues
= 1;
1447 nbd
->tag_set
.queue_depth
= 128;
1448 nbd
->tag_set
.numa_node
= NUMA_NO_NODE
;
1449 nbd
->tag_set
.cmd_size
= sizeof(struct nbd_cmd
);
1450 nbd
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
|
1451 BLK_MQ_F_SG_MERGE
| BLK_MQ_F_BLOCKING
;
1452 nbd
->tag_set
.driver_data
= nbd
;
1454 err
= blk_mq_alloc_tag_set(&nbd
->tag_set
);
1458 q
= blk_mq_init_queue(&nbd
->tag_set
);
1466 * Tell the block layer that we are not a rotational device
1468 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, disk
->queue
);
1469 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, disk
->queue
);
1470 disk
->queue
->limits
.discard_granularity
= 512;
1471 blk_queue_max_discard_sectors(disk
->queue
, UINT_MAX
);
1472 blk_queue_max_segment_size(disk
->queue
, UINT_MAX
);
1473 blk_queue_max_segments(disk
->queue
, USHRT_MAX
);
1474 blk_queue_max_hw_sectors(disk
->queue
, 65536);
1475 disk
->queue
->limits
.max_sectors
= 256;
1477 mutex_init(&nbd
->config_lock
);
1478 refcount_set(&nbd
->config_refs
, 0);
1479 refcount_set(&nbd
->refs
, 1);
1480 INIT_LIST_HEAD(&nbd
->list
);
1481 disk
->major
= NBD_MAJOR
;
1482 disk
->first_minor
= index
<< part_shift
;
1483 disk
->fops
= &nbd_fops
;
1484 disk
->private_data
= nbd
;
1485 sprintf(disk
->disk_name
, "nbd%d", index
);
1488 nbd_total_devices
++;
1492 blk_mq_free_tag_set(&nbd
->tag_set
);
1494 idr_remove(&nbd_index_idr
, index
);
1503 static int find_free_cb(int id
, void *ptr
, void *data
)
1505 struct nbd_device
*nbd
= ptr
;
1506 struct nbd_device
**found
= data
;
1508 if (!refcount_read(&nbd
->config_refs
)) {
1515 /* Netlink interface. */
1516 static struct nla_policy nbd_attr_policy
[NBD_ATTR_MAX
+ 1] = {
1517 [NBD_ATTR_INDEX
] = { .type
= NLA_U32
},
1518 [NBD_ATTR_SIZE_BYTES
] = { .type
= NLA_U64
},
1519 [NBD_ATTR_BLOCK_SIZE_BYTES
] = { .type
= NLA_U64
},
1520 [NBD_ATTR_TIMEOUT
] = { .type
= NLA_U64
},
1521 [NBD_ATTR_SERVER_FLAGS
] = { .type
= NLA_U64
},
1522 [NBD_ATTR_CLIENT_FLAGS
] = { .type
= NLA_U64
},
1523 [NBD_ATTR_SOCKETS
] = { .type
= NLA_NESTED
},
1524 [NBD_ATTR_DEAD_CONN_TIMEOUT
] = { .type
= NLA_U64
},
1525 [NBD_ATTR_DEVICE_LIST
] = { .type
= NLA_NESTED
},
1528 static struct nla_policy nbd_sock_policy
[NBD_SOCK_MAX
+ 1] = {
1529 [NBD_SOCK_FD
] = { .type
= NLA_U32
},
1532 /* We don't use this right now since we don't parse the incoming list, but we
1533 * still want it here so userspace knows what to expect.
1535 static struct nla_policy
__attribute__((unused
))
1536 nbd_device_policy
[NBD_DEVICE_ATTR_MAX
+ 1] = {
1537 [NBD_DEVICE_INDEX
] = { .type
= NLA_U32
},
1538 [NBD_DEVICE_CONNECTED
] = { .type
= NLA_U8
},
1541 static int nbd_genl_connect(struct sk_buff
*skb
, struct genl_info
*info
)
1543 struct nbd_device
*nbd
= NULL
;
1544 struct nbd_config
*config
;
1547 bool put_dev
= false;
1549 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1552 if (info
->attrs
[NBD_ATTR_INDEX
])
1553 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1554 if (!info
->attrs
[NBD_ATTR_SOCKETS
]) {
1555 printk(KERN_ERR
"nbd: must specify at least one socket\n");
1558 if (!info
->attrs
[NBD_ATTR_SIZE_BYTES
]) {
1559 printk(KERN_ERR
"nbd: must specify a size in bytes for the device\n");
1563 mutex_lock(&nbd_index_mutex
);
1565 ret
= idr_for_each(&nbd_index_idr
, &find_free_cb
, &nbd
);
1568 new_index
= nbd_dev_add(-1);
1569 if (new_index
< 0) {
1570 mutex_unlock(&nbd_index_mutex
);
1571 printk(KERN_ERR
"nbd: failed to add new device\n");
1574 nbd
= idr_find(&nbd_index_idr
, new_index
);
1577 nbd
= idr_find(&nbd_index_idr
, index
);
1580 printk(KERN_ERR
"nbd: couldn't find device at index %d\n",
1582 mutex_unlock(&nbd_index_mutex
);
1585 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1586 mutex_unlock(&nbd_index_mutex
);
1589 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1593 mutex_unlock(&nbd_index_mutex
);
1595 mutex_lock(&nbd
->config_lock
);
1596 if (refcount_read(&nbd
->config_refs
)) {
1597 mutex_unlock(&nbd
->config_lock
);
1601 printk(KERN_ERR
"nbd: nbd%d already in use\n", index
);
1604 if (WARN_ON(nbd
->config
)) {
1605 mutex_unlock(&nbd
->config_lock
);
1609 config
= nbd
->config
= nbd_alloc_config();
1611 mutex_unlock(&nbd
->config_lock
);
1613 printk(KERN_ERR
"nbd: couldn't allocate config\n");
1616 refcount_set(&nbd
->config_refs
, 1);
1617 set_bit(NBD_BOUND
, &config
->runtime_flags
);
1619 if (info
->attrs
[NBD_ATTR_SIZE_BYTES
]) {
1620 u64 bytes
= nla_get_u64(info
->attrs
[NBD_ATTR_SIZE_BYTES
]);
1621 nbd_size_set(nbd
, config
->blksize
,
1622 div64_u64(bytes
, config
->blksize
));
1624 if (info
->attrs
[NBD_ATTR_BLOCK_SIZE_BYTES
]) {
1626 nla_get_u64(info
->attrs
[NBD_ATTR_BLOCK_SIZE_BYTES
]);
1627 nbd_size_set(nbd
, bsize
, div64_u64(config
->bytesize
, bsize
));
1629 if (info
->attrs
[NBD_ATTR_TIMEOUT
]) {
1630 u64 timeout
= nla_get_u64(info
->attrs
[NBD_ATTR_TIMEOUT
]);
1631 nbd
->tag_set
.timeout
= timeout
* HZ
;
1632 blk_queue_rq_timeout(nbd
->disk
->queue
, timeout
* HZ
);
1634 if (info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]) {
1635 config
->dead_conn_timeout
=
1636 nla_get_u64(info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]);
1637 config
->dead_conn_timeout
*= HZ
;
1639 if (info
->attrs
[NBD_ATTR_SERVER_FLAGS
])
1641 nla_get_u64(info
->attrs
[NBD_ATTR_SERVER_FLAGS
]);
1642 if (info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]) {
1643 u64 flags
= nla_get_u64(info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]);
1644 if (flags
& NBD_CFLAG_DESTROY_ON_DISCONNECT
) {
1645 set_bit(NBD_DESTROY_ON_DISCONNECT
,
1646 &config
->runtime_flags
);
1651 if (info
->attrs
[NBD_ATTR_SOCKETS
]) {
1652 struct nlattr
*attr
;
1655 nla_for_each_nested(attr
, info
->attrs
[NBD_ATTR_SOCKETS
],
1657 struct nlattr
*socks
[NBD_SOCK_MAX
+1];
1659 if (nla_type(attr
) != NBD_SOCK_ITEM
) {
1660 printk(KERN_ERR
"nbd: socks must be embedded in a SOCK_ITEM attr\n");
1664 ret
= nla_parse_nested(socks
, NBD_SOCK_MAX
, attr
,
1665 nbd_sock_policy
, info
->extack
);
1667 printk(KERN_ERR
"nbd: error processing sock list\n");
1671 if (!socks
[NBD_SOCK_FD
])
1673 fd
= (int)nla_get_u32(socks
[NBD_SOCK_FD
]);
1674 ret
= nbd_add_socket(nbd
, fd
, true);
1679 ret
= nbd_start_device(nbd
);
1681 mutex_unlock(&nbd
->config_lock
);
1683 set_bit(NBD_HAS_CONFIG_REF
, &config
->runtime_flags
);
1684 refcount_inc(&nbd
->config_refs
);
1685 nbd_connect_reply(info
, nbd
->index
);
1687 nbd_config_put(nbd
);
1693 static int nbd_genl_disconnect(struct sk_buff
*skb
, struct genl_info
*info
)
1695 struct nbd_device
*nbd
;
1698 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1701 if (!info
->attrs
[NBD_ATTR_INDEX
]) {
1702 printk(KERN_ERR
"nbd: must specify an index to disconnect\n");
1705 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1706 mutex_lock(&nbd_index_mutex
);
1707 nbd
= idr_find(&nbd_index_idr
, index
);
1709 mutex_unlock(&nbd_index_mutex
);
1710 printk(KERN_ERR
"nbd: couldn't find device at index %d\n",
1714 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1715 mutex_unlock(&nbd_index_mutex
);
1716 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1720 mutex_unlock(&nbd_index_mutex
);
1721 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1725 mutex_lock(&nbd
->config_lock
);
1726 nbd_disconnect(nbd
);
1727 mutex_unlock(&nbd
->config_lock
);
1728 if (test_and_clear_bit(NBD_HAS_CONFIG_REF
,
1729 &nbd
->config
->runtime_flags
))
1730 nbd_config_put(nbd
);
1731 nbd_config_put(nbd
);
1736 static int nbd_genl_reconfigure(struct sk_buff
*skb
, struct genl_info
*info
)
1738 struct nbd_device
*nbd
= NULL
;
1739 struct nbd_config
*config
;
1742 bool put_dev
= false;
1744 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1747 if (!info
->attrs
[NBD_ATTR_INDEX
]) {
1748 printk(KERN_ERR
"nbd: must specify a device to reconfigure\n");
1751 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1752 mutex_lock(&nbd_index_mutex
);
1753 nbd
= idr_find(&nbd_index_idr
, index
);
1755 mutex_unlock(&nbd_index_mutex
);
1756 printk(KERN_ERR
"nbd: couldn't find a device at index %d\n",
1760 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1761 mutex_unlock(&nbd_index_mutex
);
1762 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1766 mutex_unlock(&nbd_index_mutex
);
1768 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1769 dev_err(nbd_to_dev(nbd
),
1770 "not configured, cannot reconfigure\n");
1775 mutex_lock(&nbd
->config_lock
);
1776 config
= nbd
->config
;
1777 if (!test_bit(NBD_BOUND
, &config
->runtime_flags
) ||
1779 dev_err(nbd_to_dev(nbd
),
1780 "not configured, cannot reconfigure\n");
1784 if (info
->attrs
[NBD_ATTR_TIMEOUT
]) {
1785 u64 timeout
= nla_get_u64(info
->attrs
[NBD_ATTR_TIMEOUT
]);
1786 nbd
->tag_set
.timeout
= timeout
* HZ
;
1787 blk_queue_rq_timeout(nbd
->disk
->queue
, timeout
* HZ
);
1789 if (info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]) {
1790 config
->dead_conn_timeout
=
1791 nla_get_u64(info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]);
1792 config
->dead_conn_timeout
*= HZ
;
1794 if (info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]) {
1795 u64 flags
= nla_get_u64(info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]);
1796 if (flags
& NBD_CFLAG_DESTROY_ON_DISCONNECT
) {
1797 if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT
,
1798 &config
->runtime_flags
))
1801 if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT
,
1802 &config
->runtime_flags
))
1803 refcount_inc(&nbd
->refs
);
1807 if (info
->attrs
[NBD_ATTR_SOCKETS
]) {
1808 struct nlattr
*attr
;
1811 nla_for_each_nested(attr
, info
->attrs
[NBD_ATTR_SOCKETS
],
1813 struct nlattr
*socks
[NBD_SOCK_MAX
+1];
1815 if (nla_type(attr
) != NBD_SOCK_ITEM
) {
1816 printk(KERN_ERR
"nbd: socks must be embedded in a SOCK_ITEM attr\n");
1820 ret
= nla_parse_nested(socks
, NBD_SOCK_MAX
, attr
,
1821 nbd_sock_policy
, info
->extack
);
1823 printk(KERN_ERR
"nbd: error processing sock list\n");
1827 if (!socks
[NBD_SOCK_FD
])
1829 fd
= (int)nla_get_u32(socks
[NBD_SOCK_FD
]);
1830 ret
= nbd_reconnect_socket(nbd
, fd
);
1836 dev_info(nbd_to_dev(nbd
), "reconnected socket\n");
1840 mutex_unlock(&nbd
->config_lock
);
1841 nbd_config_put(nbd
);
1848 static const struct genl_ops nbd_connect_genl_ops
[] = {
1850 .cmd
= NBD_CMD_CONNECT
,
1851 .policy
= nbd_attr_policy
,
1852 .doit
= nbd_genl_connect
,
1855 .cmd
= NBD_CMD_DISCONNECT
,
1856 .policy
= nbd_attr_policy
,
1857 .doit
= nbd_genl_disconnect
,
1860 .cmd
= NBD_CMD_RECONFIGURE
,
1861 .policy
= nbd_attr_policy
,
1862 .doit
= nbd_genl_reconfigure
,
1865 .cmd
= NBD_CMD_STATUS
,
1866 .policy
= nbd_attr_policy
,
1867 .doit
= nbd_genl_status
,
1871 static const struct genl_multicast_group nbd_mcast_grps
[] = {
1872 { .name
= NBD_GENL_MCAST_GROUP_NAME
, },
1875 static struct genl_family nbd_genl_family __ro_after_init
= {
1877 .name
= NBD_GENL_FAMILY_NAME
,
1878 .version
= NBD_GENL_VERSION
,
1879 .module
= THIS_MODULE
,
1880 .ops
= nbd_connect_genl_ops
,
1881 .n_ops
= ARRAY_SIZE(nbd_connect_genl_ops
),
1882 .maxattr
= NBD_ATTR_MAX
,
1883 .mcgrps
= nbd_mcast_grps
,
1884 .n_mcgrps
= ARRAY_SIZE(nbd_mcast_grps
),
1887 static int populate_nbd_status(struct nbd_device
*nbd
, struct sk_buff
*reply
)
1889 struct nlattr
*dev_opt
;
1893 /* This is a little racey, but for status it's ok. The
1894 * reason we don't take a ref here is because we can't
1895 * take a ref in the index == -1 case as we would need
1896 * to put under the nbd_index_mutex, which could
1897 * deadlock if we are configured to remove ourselves
1898 * once we're disconnected.
1900 if (refcount_read(&nbd
->config_refs
))
1902 dev_opt
= nla_nest_start(reply
, NBD_DEVICE_ITEM
);
1905 ret
= nla_put_u32(reply
, NBD_DEVICE_INDEX
, nbd
->index
);
1908 ret
= nla_put_u8(reply
, NBD_DEVICE_CONNECTED
,
1912 nla_nest_end(reply
, dev_opt
);
1916 static int status_cb(int id
, void *ptr
, void *data
)
1918 struct nbd_device
*nbd
= ptr
;
1919 return populate_nbd_status(nbd
, (struct sk_buff
*)data
);
1922 static int nbd_genl_status(struct sk_buff
*skb
, struct genl_info
*info
)
1924 struct nlattr
*dev_list
;
1925 struct sk_buff
*reply
;
1931 if (info
->attrs
[NBD_ATTR_INDEX
])
1932 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1934 mutex_lock(&nbd_index_mutex
);
1936 msg_size
= nla_total_size(nla_attr_size(sizeof(u32
)) +
1937 nla_attr_size(sizeof(u8
)));
1938 msg_size
*= (index
== -1) ? nbd_total_devices
: 1;
1940 reply
= genlmsg_new(msg_size
, GFP_KERNEL
);
1943 reply_head
= genlmsg_put_reply(reply
, info
, &nbd_genl_family
, 0,
1950 dev_list
= nla_nest_start(reply
, NBD_ATTR_DEVICE_LIST
);
1952 ret
= idr_for_each(&nbd_index_idr
, &status_cb
, reply
);
1958 struct nbd_device
*nbd
;
1959 nbd
= idr_find(&nbd_index_idr
, index
);
1961 ret
= populate_nbd_status(nbd
, reply
);
1968 nla_nest_end(reply
, dev_list
);
1969 genlmsg_end(reply
, reply_head
);
1970 genlmsg_reply(reply
, info
);
1973 mutex_unlock(&nbd_index_mutex
);
1977 static void nbd_connect_reply(struct genl_info
*info
, int index
)
1979 struct sk_buff
*skb
;
1983 skb
= genlmsg_new(nla_total_size(sizeof(u32
)), GFP_KERNEL
);
1986 msg_head
= genlmsg_put_reply(skb
, info
, &nbd_genl_family
, 0,
1992 ret
= nla_put_u32(skb
, NBD_ATTR_INDEX
, index
);
1997 genlmsg_end(skb
, msg_head
);
1998 genlmsg_reply(skb
, info
);
2001 static void nbd_mcast_index(int index
)
2003 struct sk_buff
*skb
;
2007 skb
= genlmsg_new(nla_total_size(sizeof(u32
)), GFP_KERNEL
);
2010 msg_head
= genlmsg_put(skb
, 0, 0, &nbd_genl_family
, 0,
2016 ret
= nla_put_u32(skb
, NBD_ATTR_INDEX
, index
);
2021 genlmsg_end(skb
, msg_head
);
2022 genlmsg_multicast(&nbd_genl_family
, skb
, 0, 0, GFP_KERNEL
);
2025 static void nbd_dead_link_work(struct work_struct
*work
)
2027 struct link_dead_args
*args
= container_of(work
, struct link_dead_args
,
2029 nbd_mcast_index(args
->index
);
2033 static int __init
nbd_init(void)
2037 BUILD_BUG_ON(sizeof(struct nbd_request
) != 28);
2040 printk(KERN_ERR
"nbd: max_part must be >= 0\n");
2046 part_shift
= fls(max_part
);
2049 * Adjust max_part according to part_shift as it is exported
2050 * to user space so that user can know the max number of
2051 * partition kernel should be able to manage.
2053 * Note that -1 is required because partition 0 is reserved
2054 * for the whole disk.
2056 max_part
= (1UL << part_shift
) - 1;
2059 if ((1UL << part_shift
) > DISK_MAX_PARTS
)
2062 if (nbds_max
> 1UL << (MINORBITS
- part_shift
))
2064 recv_workqueue
= alloc_workqueue("knbd-recv",
2065 WQ_MEM_RECLAIM
| WQ_HIGHPRI
, 0);
2066 if (!recv_workqueue
)
2069 if (register_blkdev(NBD_MAJOR
, "nbd")) {
2070 destroy_workqueue(recv_workqueue
);
2074 if (genl_register_family(&nbd_genl_family
)) {
2075 unregister_blkdev(NBD_MAJOR
, "nbd");
2076 destroy_workqueue(recv_workqueue
);
2081 mutex_lock(&nbd_index_mutex
);
2082 for (i
= 0; i
< nbds_max
; i
++)
2084 mutex_unlock(&nbd_index_mutex
);
2088 static int nbd_exit_cb(int id
, void *ptr
, void *data
)
2090 struct list_head
*list
= (struct list_head
*)data
;
2091 struct nbd_device
*nbd
= ptr
;
2093 list_add_tail(&nbd
->list
, list
);
2097 static void __exit
nbd_cleanup(void)
2099 struct nbd_device
*nbd
;
2100 LIST_HEAD(del_list
);
2104 mutex_lock(&nbd_index_mutex
);
2105 idr_for_each(&nbd_index_idr
, &nbd_exit_cb
, &del_list
);
2106 mutex_unlock(&nbd_index_mutex
);
2108 while (!list_empty(&del_list
)) {
2109 nbd
= list_first_entry(&del_list
, struct nbd_device
, list
);
2110 list_del_init(&nbd
->list
);
2111 if (refcount_read(&nbd
->refs
) != 1)
2112 printk(KERN_ERR
"nbd: possibly leaking a device\n");
2116 idr_destroy(&nbd_index_idr
);
2117 genl_unregister_family(&nbd_genl_family
);
2118 destroy_workqueue(recv_workqueue
);
2119 unregister_blkdev(NBD_MAJOR
, "nbd");
2122 module_init(nbd_init
);
2123 module_exit(nbd_cleanup
);
2125 MODULE_DESCRIPTION("Network Block Device");
2126 MODULE_LICENSE("GPL");
2128 module_param(nbds_max
, int, 0444);
2129 MODULE_PARM_DESC(nbds_max
, "number of network block devices to initialize (default: 16)");
2130 module_param(max_part
, int, 0444);
2131 MODULE_PARM_DESC(max_part
, "number of partitions per device (default: 0)");