1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Network block device - make block devices work over TCP
5 * Note that you can not swap over this thing, yet. Seems to work but
6 * deadlocks sometimes - you can not swap over TCP in general.
8 * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz>
9 * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com>
11 * (part of code stolen from loop.c)
14 #include <linux/major.h>
16 #include <linux/blkdev.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/sched/mm.h>
22 #include <linux/bio.h>
23 #include <linux/stat.h>
24 #include <linux/errno.h>
25 #include <linux/file.h>
26 #include <linux/ioctl.h>
27 #include <linux/mutex.h>
28 #include <linux/compiler.h>
29 #include <linux/completion.h>
30 #include <linux/err.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
34 #include <linux/net.h>
35 #include <linux/kthread.h>
36 #include <linux/types.h>
37 #include <linux/debugfs.h>
38 #include <linux/blk-mq.h>
40 #include <linux/uaccess.h>
41 #include <asm/types.h>
43 #include <linux/nbd.h>
44 #include <linux/nbd-netlink.h>
45 #include <net/genetlink.h>
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/nbd.h>
50 static DEFINE_IDR(nbd_index_idr
);
51 static DEFINE_MUTEX(nbd_index_mutex
);
52 static int nbd_total_devices
= 0;
57 struct request
*pending
;
64 struct recv_thread_args
{
65 struct work_struct work
;
66 struct nbd_device
*nbd
;
70 struct link_dead_args
{
71 struct work_struct work
;
75 #define NBD_RT_TIMEDOUT 0
76 #define NBD_RT_DISCONNECT_REQUESTED 1
77 #define NBD_RT_DISCONNECTED 2
78 #define NBD_RT_HAS_PID_FILE 3
79 #define NBD_RT_HAS_CONFIG_REF 4
80 #define NBD_RT_BOUND 5
81 #define NBD_RT_DESTROY_ON_DISCONNECT 6
82 #define NBD_RT_DISCONNECT_ON_CLOSE 7
84 #define NBD_DESTROY_ON_DISCONNECT 0
85 #define NBD_DISCONNECT_REQUESTED 1
89 unsigned long runtime_flags
;
90 u64 dead_conn_timeout
;
92 struct nbd_sock
**socks
;
94 atomic_t live_connections
;
95 wait_queue_head_t conn_wait
;
97 atomic_t recv_threads
;
98 wait_queue_head_t recv_wq
;
101 #if IS_ENABLED(CONFIG_DEBUG_FS)
102 struct dentry
*dbg_dir
;
107 struct blk_mq_tag_set tag_set
;
110 refcount_t config_refs
;
112 struct nbd_config
*config
;
113 struct mutex config_lock
;
114 struct gendisk
*disk
;
115 struct workqueue_struct
*recv_workq
;
117 struct list_head list
;
118 struct task_struct
*task_recv
;
119 struct task_struct
*task_setup
;
121 struct completion
*destroy_complete
;
125 #define NBD_CMD_REQUEUED 1
128 struct nbd_device
*nbd
;
138 #if IS_ENABLED(CONFIG_DEBUG_FS)
139 static struct dentry
*nbd_dbg_dir
;
142 #define nbd_name(nbd) ((nbd)->disk->disk_name)
144 #define NBD_MAGIC 0x68797548
146 #define NBD_DEF_BLKSIZE 1024
148 static unsigned int nbds_max
= 16;
149 static int max_part
= 16;
150 static int part_shift
;
152 static int nbd_dev_dbg_init(struct nbd_device
*nbd
);
153 static void nbd_dev_dbg_close(struct nbd_device
*nbd
);
154 static void nbd_config_put(struct nbd_device
*nbd
);
155 static void nbd_connect_reply(struct genl_info
*info
, int index
);
156 static int nbd_genl_status(struct sk_buff
*skb
, struct genl_info
*info
);
157 static void nbd_dead_link_work(struct work_struct
*work
);
158 static void nbd_disconnect_and_put(struct nbd_device
*nbd
);
160 static inline struct device
*nbd_to_dev(struct nbd_device
*nbd
)
162 return disk_to_dev(nbd
->disk
);
165 static void nbd_requeue_cmd(struct nbd_cmd
*cmd
)
167 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
169 if (!test_and_set_bit(NBD_CMD_REQUEUED
, &cmd
->flags
))
170 blk_mq_requeue_request(req
, true);
173 #define NBD_COOKIE_BITS 32
175 static u64
nbd_cmd_handle(struct nbd_cmd
*cmd
)
177 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
178 u32 tag
= blk_mq_unique_tag(req
);
179 u64 cookie
= cmd
->cmd_cookie
;
181 return (cookie
<< NBD_COOKIE_BITS
) | tag
;
184 static u32
nbd_handle_to_tag(u64 handle
)
189 static u32
nbd_handle_to_cookie(u64 handle
)
191 return (u32
)(handle
>> NBD_COOKIE_BITS
);
194 static const char *nbdcmd_to_ascii(int cmd
)
197 case NBD_CMD_READ
: return "read";
198 case NBD_CMD_WRITE
: return "write";
199 case NBD_CMD_DISC
: return "disconnect";
200 case NBD_CMD_FLUSH
: return "flush";
201 case NBD_CMD_TRIM
: return "trim/discard";
206 static ssize_t
pid_show(struct device
*dev
,
207 struct device_attribute
*attr
, char *buf
)
209 struct gendisk
*disk
= dev_to_disk(dev
);
210 struct nbd_device
*nbd
= (struct nbd_device
*)disk
->private_data
;
212 return sprintf(buf
, "%d\n", task_pid_nr(nbd
->task_recv
));
215 static const struct device_attribute pid_attr
= {
216 .attr
= { .name
= "pid", .mode
= 0444},
220 static void nbd_dev_remove(struct nbd_device
*nbd
)
222 struct gendisk
*disk
= nbd
->disk
;
223 struct request_queue
*q
;
228 blk_cleanup_queue(q
);
229 blk_mq_free_tag_set(&nbd
->tag_set
);
230 disk
->private_data
= NULL
;
235 * Place this in the last just before the nbd is freed to
236 * make sure that the disk and the related kobject are also
237 * totally removed to avoid duplicate creation of the same
240 if (test_bit(NBD_DESTROY_ON_DISCONNECT
, &nbd
->flags
) && nbd
->destroy_complete
)
241 complete(nbd
->destroy_complete
);
246 static void nbd_put(struct nbd_device
*nbd
)
248 if (refcount_dec_and_mutex_lock(&nbd
->refs
,
250 idr_remove(&nbd_index_idr
, nbd
->index
);
252 mutex_unlock(&nbd_index_mutex
);
256 static int nbd_disconnected(struct nbd_config
*config
)
258 return test_bit(NBD_RT_DISCONNECTED
, &config
->runtime_flags
) ||
259 test_bit(NBD_RT_DISCONNECT_REQUESTED
, &config
->runtime_flags
);
262 static void nbd_mark_nsock_dead(struct nbd_device
*nbd
, struct nbd_sock
*nsock
,
265 if (!nsock
->dead
&& notify
&& !nbd_disconnected(nbd
->config
)) {
266 struct link_dead_args
*args
;
267 args
= kmalloc(sizeof(struct link_dead_args
), GFP_NOIO
);
269 INIT_WORK(&args
->work
, nbd_dead_link_work
);
270 args
->index
= nbd
->index
;
271 queue_work(system_wq
, &args
->work
);
275 kernel_sock_shutdown(nsock
->sock
, SHUT_RDWR
);
276 if (atomic_dec_return(&nbd
->config
->live_connections
) == 0) {
277 if (test_and_clear_bit(NBD_RT_DISCONNECT_REQUESTED
,
278 &nbd
->config
->runtime_flags
)) {
279 set_bit(NBD_RT_DISCONNECTED
,
280 &nbd
->config
->runtime_flags
);
281 dev_info(nbd_to_dev(nbd
),
282 "Disconnected due to user request.\n");
287 nsock
->pending
= NULL
;
291 static void nbd_size_clear(struct nbd_device
*nbd
)
293 if (nbd
->config
->bytesize
) {
294 set_capacity(nbd
->disk
, 0);
295 kobject_uevent(&nbd_to_dev(nbd
)->kobj
, KOBJ_CHANGE
);
299 static void nbd_size_update(struct nbd_device
*nbd
)
301 struct nbd_config
*config
= nbd
->config
;
302 struct block_device
*bdev
= bdget_disk(nbd
->disk
, 0);
304 if (config
->flags
& NBD_FLAG_SEND_TRIM
) {
305 nbd
->disk
->queue
->limits
.discard_granularity
= config
->blksize
;
306 nbd
->disk
->queue
->limits
.discard_alignment
= config
->blksize
;
307 blk_queue_max_discard_sectors(nbd
->disk
->queue
, UINT_MAX
);
309 blk_queue_logical_block_size(nbd
->disk
->queue
, config
->blksize
);
310 blk_queue_physical_block_size(nbd
->disk
->queue
, config
->blksize
);
311 set_capacity(nbd
->disk
, config
->bytesize
>> 9);
314 bd_set_size(bdev
, config
->bytesize
);
315 set_blocksize(bdev
, config
->blksize
);
317 bdev
->bd_invalidated
= 1;
320 kobject_uevent(&nbd_to_dev(nbd
)->kobj
, KOBJ_CHANGE
);
323 static void nbd_size_set(struct nbd_device
*nbd
, loff_t blocksize
,
326 struct nbd_config
*config
= nbd
->config
;
327 config
->blksize
= blocksize
;
328 config
->bytesize
= blocksize
* nr_blocks
;
329 if (nbd
->task_recv
!= NULL
)
330 nbd_size_update(nbd
);
333 static void nbd_complete_rq(struct request
*req
)
335 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
337 dev_dbg(nbd_to_dev(cmd
->nbd
), "request %p: %s\n", req
,
338 cmd
->status
? "failed" : "done");
340 blk_mq_end_request(req
, cmd
->status
);
344 * Forcibly shutdown the socket causing all listeners to error
346 static void sock_shutdown(struct nbd_device
*nbd
)
348 struct nbd_config
*config
= nbd
->config
;
351 if (config
->num_connections
== 0)
353 if (test_and_set_bit(NBD_RT_DISCONNECTED
, &config
->runtime_flags
))
356 for (i
= 0; i
< config
->num_connections
; i
++) {
357 struct nbd_sock
*nsock
= config
->socks
[i
];
358 mutex_lock(&nsock
->tx_lock
);
359 nbd_mark_nsock_dead(nbd
, nsock
, 0);
360 mutex_unlock(&nsock
->tx_lock
);
362 dev_warn(disk_to_dev(nbd
->disk
), "shutting down sockets\n");
365 static u32
req_to_nbd_cmd_type(struct request
*req
)
367 switch (req_op(req
)) {
371 return NBD_CMD_FLUSH
;
373 return NBD_CMD_WRITE
;
381 static enum blk_eh_timer_return
nbd_xmit_timeout(struct request
*req
,
384 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
385 struct nbd_device
*nbd
= cmd
->nbd
;
386 struct nbd_config
*config
;
388 if (!mutex_trylock(&cmd
->lock
))
389 return BLK_EH_RESET_TIMER
;
391 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
392 cmd
->status
= BLK_STS_TIMEOUT
;
393 mutex_unlock(&cmd
->lock
);
396 config
= nbd
->config
;
398 if (config
->num_connections
> 1 ||
399 (config
->num_connections
== 1 && nbd
->tag_set
.timeout
)) {
400 dev_err_ratelimited(nbd_to_dev(nbd
),
401 "Connection timed out, retrying (%d/%d alive)\n",
402 atomic_read(&config
->live_connections
),
403 config
->num_connections
);
405 * Hooray we have more connections, requeue this IO, the submit
406 * path will put it on a real connection. Or if only one
407 * connection is configured, the submit path will wait util
408 * a new connection is reconfigured or util dead timeout.
411 if (cmd
->index
< config
->num_connections
) {
412 struct nbd_sock
*nsock
=
413 config
->socks
[cmd
->index
];
414 mutex_lock(&nsock
->tx_lock
);
415 /* We can have multiple outstanding requests, so
416 * we don't want to mark the nsock dead if we've
417 * already reconnected with a new socket, so
418 * only mark it dead if its the same socket we
421 if (cmd
->cookie
== nsock
->cookie
)
422 nbd_mark_nsock_dead(nbd
, nsock
, 1);
423 mutex_unlock(&nsock
->tx_lock
);
425 mutex_unlock(&cmd
->lock
);
426 nbd_requeue_cmd(cmd
);
432 if (!nbd
->tag_set
.timeout
) {
434 * Userspace sets timeout=0 to disable socket disconnection,
435 * so just warn and reset the timer.
437 struct nbd_sock
*nsock
= config
->socks
[cmd
->index
];
439 dev_info(nbd_to_dev(nbd
), "Possible stuck request %p: control (%s@%llu,%uB). Runtime %u seconds\n",
440 req
, nbdcmd_to_ascii(req_to_nbd_cmd_type(req
)),
441 (unsigned long long)blk_rq_pos(req
) << 9,
442 blk_rq_bytes(req
), (req
->timeout
/ HZ
) * cmd
->retries
);
444 mutex_lock(&nsock
->tx_lock
);
445 if (cmd
->cookie
!= nsock
->cookie
) {
446 nbd_requeue_cmd(cmd
);
447 mutex_unlock(&nsock
->tx_lock
);
448 mutex_unlock(&cmd
->lock
);
452 mutex_unlock(&nsock
->tx_lock
);
453 mutex_unlock(&cmd
->lock
);
455 return BLK_EH_RESET_TIMER
;
458 dev_err_ratelimited(nbd_to_dev(nbd
), "Connection timed out\n");
459 set_bit(NBD_RT_TIMEDOUT
, &config
->runtime_flags
);
460 cmd
->status
= BLK_STS_IOERR
;
461 mutex_unlock(&cmd
->lock
);
465 blk_mq_complete_request(req
);
470 * Send or receive packet.
472 static int sock_xmit(struct nbd_device
*nbd
, int index
, int send
,
473 struct iov_iter
*iter
, int msg_flags
, int *sent
)
475 struct nbd_config
*config
= nbd
->config
;
476 struct socket
*sock
= config
->socks
[index
]->sock
;
479 unsigned int noreclaim_flag
;
481 if (unlikely(!sock
)) {
482 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
483 "Attempted %s on closed socket in sock_xmit\n",
484 (send
? "send" : "recv"));
488 msg
.msg_iter
= *iter
;
490 noreclaim_flag
= memalloc_noreclaim_save();
492 sock
->sk
->sk_allocation
= GFP_NOIO
| __GFP_MEMALLOC
;
495 msg
.msg_control
= NULL
;
496 msg
.msg_controllen
= 0;
497 msg
.msg_flags
= msg_flags
| MSG_NOSIGNAL
;
500 result
= sock_sendmsg(sock
, &msg
);
502 result
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
506 result
= -EPIPE
; /* short read */
511 } while (msg_data_left(&msg
));
513 memalloc_noreclaim_restore(noreclaim_flag
);
519 * Different settings for sk->sk_sndtimeo can result in different return values
520 * if there is a signal pending when we enter sendmsg, because reasons?
522 static inline int was_interrupted(int result
)
524 return result
== -ERESTARTSYS
|| result
== -EINTR
;
527 /* always call with the tx_lock held */
528 static int nbd_send_cmd(struct nbd_device
*nbd
, struct nbd_cmd
*cmd
, int index
)
530 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
531 struct nbd_config
*config
= nbd
->config
;
532 struct nbd_sock
*nsock
= config
->socks
[index
];
534 struct nbd_request request
= {.magic
= htonl(NBD_REQUEST_MAGIC
)};
535 struct kvec iov
= {.iov_base
= &request
, .iov_len
= sizeof(request
)};
536 struct iov_iter from
;
537 unsigned long size
= blk_rq_bytes(req
);
541 u32 nbd_cmd_flags
= 0;
542 int sent
= nsock
->sent
, skip
= 0;
544 iov_iter_kvec(&from
, WRITE
, &iov
, 1, sizeof(request
));
546 type
= req_to_nbd_cmd_type(req
);
550 if (rq_data_dir(req
) == WRITE
&&
551 (config
->flags
& NBD_FLAG_READ_ONLY
)) {
552 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
553 "Write on read-only\n");
557 if (req
->cmd_flags
& REQ_FUA
)
558 nbd_cmd_flags
|= NBD_CMD_FLAG_FUA
;
560 /* We did a partial send previously, and we at least sent the whole
561 * request struct, so just go and send the rest of the pages in the
565 if (sent
>= sizeof(request
)) {
566 skip
= sent
- sizeof(request
);
568 /* initialize handle for tracing purposes */
569 handle
= nbd_cmd_handle(cmd
);
573 iov_iter_advance(&from
, sent
);
578 cmd
->cookie
= nsock
->cookie
;
580 request
.type
= htonl(type
| nbd_cmd_flags
);
581 if (type
!= NBD_CMD_FLUSH
) {
582 request
.from
= cpu_to_be64((u64
)blk_rq_pos(req
) << 9);
583 request
.len
= htonl(size
);
585 handle
= nbd_cmd_handle(cmd
);
586 memcpy(request
.handle
, &handle
, sizeof(handle
));
588 trace_nbd_send_request(&request
, nbd
->index
, blk_mq_rq_from_pdu(cmd
));
590 dev_dbg(nbd_to_dev(nbd
), "request %p: sending control (%s@%llu,%uB)\n",
591 req
, nbdcmd_to_ascii(type
),
592 (unsigned long long)blk_rq_pos(req
) << 9, blk_rq_bytes(req
));
593 result
= sock_xmit(nbd
, index
, 1, &from
,
594 (type
== NBD_CMD_WRITE
) ? MSG_MORE
: 0, &sent
);
595 trace_nbd_header_sent(req
, handle
);
597 if (was_interrupted(result
)) {
598 /* If we havne't sent anything we can just return BUSY,
599 * however if we have sent something we need to make
600 * sure we only allow this req to be sent until we are
604 nsock
->pending
= req
;
607 set_bit(NBD_CMD_REQUEUED
, &cmd
->flags
);
608 return BLK_STS_RESOURCE
;
610 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
611 "Send control failed (result %d)\n", result
);
615 if (type
!= NBD_CMD_WRITE
)
620 struct bio
*next
= bio
->bi_next
;
621 struct bvec_iter iter
;
624 bio_for_each_segment(bvec
, bio
, iter
) {
625 bool is_last
= !next
&& bio_iter_last(bvec
, iter
);
626 int flags
= is_last
? 0 : MSG_MORE
;
628 dev_dbg(nbd_to_dev(nbd
), "request %p: sending %d bytes data\n",
630 iov_iter_bvec(&from
, WRITE
, &bvec
, 1, bvec
.bv_len
);
632 if (skip
>= iov_iter_count(&from
)) {
633 skip
-= iov_iter_count(&from
);
636 iov_iter_advance(&from
, skip
);
639 result
= sock_xmit(nbd
, index
, 1, &from
, flags
, &sent
);
641 if (was_interrupted(result
)) {
642 /* We've already sent the header, we
643 * have no choice but to set pending and
646 nsock
->pending
= req
;
648 set_bit(NBD_CMD_REQUEUED
, &cmd
->flags
);
649 return BLK_STS_RESOURCE
;
651 dev_err(disk_to_dev(nbd
->disk
),
652 "Send data failed (result %d)\n",
657 * The completion might already have come in,
658 * so break for the last one instead of letting
659 * the iterator do it. This prevents use-after-free
668 trace_nbd_payload_sent(req
, handle
);
669 nsock
->pending
= NULL
;
674 /* NULL returned = something went wrong, inform userspace */
675 static struct nbd_cmd
*nbd_read_stat(struct nbd_device
*nbd
, int index
)
677 struct nbd_config
*config
= nbd
->config
;
679 struct nbd_reply reply
;
681 struct request
*req
= NULL
;
685 struct kvec iov
= {.iov_base
= &reply
, .iov_len
= sizeof(reply
)};
690 iov_iter_kvec(&to
, READ
, &iov
, 1, sizeof(reply
));
691 result
= sock_xmit(nbd
, index
, 0, &to
, MSG_WAITALL
, NULL
);
693 if (!nbd_disconnected(config
))
694 dev_err(disk_to_dev(nbd
->disk
),
695 "Receive control failed (result %d)\n", result
);
696 return ERR_PTR(result
);
699 if (ntohl(reply
.magic
) != NBD_REPLY_MAGIC
) {
700 dev_err(disk_to_dev(nbd
->disk
), "Wrong magic (0x%lx)\n",
701 (unsigned long)ntohl(reply
.magic
));
702 return ERR_PTR(-EPROTO
);
705 memcpy(&handle
, reply
.handle
, sizeof(handle
));
706 tag
= nbd_handle_to_tag(handle
);
707 hwq
= blk_mq_unique_tag_to_hwq(tag
);
708 if (hwq
< nbd
->tag_set
.nr_hw_queues
)
709 req
= blk_mq_tag_to_rq(nbd
->tag_set
.tags
[hwq
],
710 blk_mq_unique_tag_to_tag(tag
));
711 if (!req
|| !blk_mq_request_started(req
)) {
712 dev_err(disk_to_dev(nbd
->disk
), "Unexpected reply (%d) %p\n",
714 return ERR_PTR(-ENOENT
);
716 trace_nbd_header_received(req
, handle
);
717 cmd
= blk_mq_rq_to_pdu(req
);
719 mutex_lock(&cmd
->lock
);
720 if (cmd
->cmd_cookie
!= nbd_handle_to_cookie(handle
)) {
721 dev_err(disk_to_dev(nbd
->disk
), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
722 req
, cmd
->cmd_cookie
, nbd_handle_to_cookie(handle
));
726 if (cmd
->status
!= BLK_STS_OK
) {
727 dev_err(disk_to_dev(nbd
->disk
), "Command already handled %p\n",
732 if (test_bit(NBD_CMD_REQUEUED
, &cmd
->flags
)) {
733 dev_err(disk_to_dev(nbd
->disk
), "Raced with timeout on req %p\n",
738 if (ntohl(reply
.error
)) {
739 dev_err(disk_to_dev(nbd
->disk
), "Other side returned error (%d)\n",
741 cmd
->status
= BLK_STS_IOERR
;
745 dev_dbg(nbd_to_dev(nbd
), "request %p: got reply\n", req
);
746 if (rq_data_dir(req
) != WRITE
) {
747 struct req_iterator iter
;
750 rq_for_each_segment(bvec
, req
, iter
) {
751 iov_iter_bvec(&to
, READ
, &bvec
, 1, bvec
.bv_len
);
752 result
= sock_xmit(nbd
, index
, 0, &to
, MSG_WAITALL
, NULL
);
754 dev_err(disk_to_dev(nbd
->disk
), "Receive data failed (result %d)\n",
757 * If we've disconnected, we need to make sure we
758 * complete this request, otherwise error out
759 * and let the timeout stuff handle resubmitting
760 * this request onto another connection.
762 if (nbd_disconnected(config
)) {
763 cmd
->status
= BLK_STS_IOERR
;
769 dev_dbg(nbd_to_dev(nbd
), "request %p: got %d bytes data\n",
774 trace_nbd_payload_received(req
, handle
);
775 mutex_unlock(&cmd
->lock
);
776 return ret
? ERR_PTR(ret
) : cmd
;
779 static void recv_work(struct work_struct
*work
)
781 struct recv_thread_args
*args
= container_of(work
,
782 struct recv_thread_args
,
784 struct nbd_device
*nbd
= args
->nbd
;
785 struct nbd_config
*config
= nbd
->config
;
789 cmd
= nbd_read_stat(nbd
, args
->index
);
791 struct nbd_sock
*nsock
= config
->socks
[args
->index
];
793 mutex_lock(&nsock
->tx_lock
);
794 nbd_mark_nsock_dead(nbd
, nsock
, 1);
795 mutex_unlock(&nsock
->tx_lock
);
799 blk_mq_complete_request(blk_mq_rq_from_pdu(cmd
));
801 atomic_dec(&config
->recv_threads
);
802 wake_up(&config
->recv_wq
);
807 static bool nbd_clear_req(struct request
*req
, void *data
, bool reserved
)
809 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
811 mutex_lock(&cmd
->lock
);
812 cmd
->status
= BLK_STS_IOERR
;
813 mutex_unlock(&cmd
->lock
);
815 blk_mq_complete_request(req
);
819 static void nbd_clear_que(struct nbd_device
*nbd
)
821 blk_mq_quiesce_queue(nbd
->disk
->queue
);
822 blk_mq_tagset_busy_iter(&nbd
->tag_set
, nbd_clear_req
, NULL
);
823 blk_mq_unquiesce_queue(nbd
->disk
->queue
);
824 dev_dbg(disk_to_dev(nbd
->disk
), "queue cleared\n");
827 static int find_fallback(struct nbd_device
*nbd
, int index
)
829 struct nbd_config
*config
= nbd
->config
;
831 struct nbd_sock
*nsock
= config
->socks
[index
];
832 int fallback
= nsock
->fallback_index
;
834 if (test_bit(NBD_RT_DISCONNECTED
, &config
->runtime_flags
))
837 if (config
->num_connections
<= 1) {
838 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
839 "Dead connection, failed to find a fallback\n");
843 if (fallback
>= 0 && fallback
< config
->num_connections
&&
844 !config
->socks
[fallback
]->dead
)
847 if (nsock
->fallback_index
< 0 ||
848 nsock
->fallback_index
>= config
->num_connections
||
849 config
->socks
[nsock
->fallback_index
]->dead
) {
851 for (i
= 0; i
< config
->num_connections
; i
++) {
854 if (!config
->socks
[i
]->dead
) {
859 nsock
->fallback_index
= new_index
;
861 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
862 "Dead connection, failed to find a fallback\n");
866 new_index
= nsock
->fallback_index
;
870 static int wait_for_reconnect(struct nbd_device
*nbd
)
872 struct nbd_config
*config
= nbd
->config
;
873 if (!config
->dead_conn_timeout
)
875 if (test_bit(NBD_RT_DISCONNECTED
, &config
->runtime_flags
))
877 return wait_event_timeout(config
->conn_wait
,
878 atomic_read(&config
->live_connections
) > 0,
879 config
->dead_conn_timeout
) > 0;
882 static int nbd_handle_cmd(struct nbd_cmd
*cmd
, int index
)
884 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
885 struct nbd_device
*nbd
= cmd
->nbd
;
886 struct nbd_config
*config
;
887 struct nbd_sock
*nsock
;
890 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
891 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
892 "Socks array is empty\n");
893 blk_mq_start_request(req
);
896 config
= nbd
->config
;
898 if (index
>= config
->num_connections
) {
899 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
900 "Attempted send on invalid socket\n");
902 blk_mq_start_request(req
);
905 cmd
->status
= BLK_STS_OK
;
907 nsock
= config
->socks
[index
];
908 mutex_lock(&nsock
->tx_lock
);
910 int old_index
= index
;
911 index
= find_fallback(nbd
, index
);
912 mutex_unlock(&nsock
->tx_lock
);
914 if (wait_for_reconnect(nbd
)) {
918 /* All the sockets should already be down at this point,
919 * we just want to make sure that DISCONNECTED is set so
920 * any requests that come in that were queue'ed waiting
921 * for the reconnect timer don't trigger the timer again
922 * and instead just error out.
926 blk_mq_start_request(req
);
932 /* Handle the case that we have a pending request that was partially
933 * transmitted that _has_ to be serviced first. We need to call requeue
934 * here so that it gets put _after_ the request that is already on the
937 blk_mq_start_request(req
);
938 if (unlikely(nsock
->pending
&& nsock
->pending
!= req
)) {
939 nbd_requeue_cmd(cmd
);
944 * Some failures are related to the link going down, so anything that
945 * returns EAGAIN can be retried on a different socket.
947 ret
= nbd_send_cmd(nbd
, cmd
, index
);
948 if (ret
== -EAGAIN
) {
949 dev_err_ratelimited(disk_to_dev(nbd
->disk
),
950 "Request send failed, requeueing\n");
951 nbd_mark_nsock_dead(nbd
, nsock
, 1);
952 nbd_requeue_cmd(cmd
);
956 mutex_unlock(&nsock
->tx_lock
);
961 static blk_status_t
nbd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
962 const struct blk_mq_queue_data
*bd
)
964 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(bd
->rq
);
968 * Since we look at the bio's to send the request over the network we
969 * need to make sure the completion work doesn't mark this request done
970 * before we are done doing our send. This keeps us from dereferencing
971 * freed data if we have particularly fast completions (ie we get the
972 * completion before we exit sock_xmit on the last bvec) or in the case
973 * that the server is misbehaving (or there was an error) before we're
974 * done sending everything over the wire.
976 mutex_lock(&cmd
->lock
);
977 clear_bit(NBD_CMD_REQUEUED
, &cmd
->flags
);
979 /* We can be called directly from the user space process, which means we
980 * could possibly have signals pending so our sendmsg will fail. In
981 * this case we need to return that we are busy, otherwise error out as
984 ret
= nbd_handle_cmd(cmd
, hctx
->queue_num
);
989 mutex_unlock(&cmd
->lock
);
994 static struct socket
*nbd_get_socket(struct nbd_device
*nbd
, unsigned long fd
,
1000 sock
= sockfd_lookup(fd
, err
);
1004 if (sock
->ops
->shutdown
== sock_no_shutdown
) {
1005 dev_err(disk_to_dev(nbd
->disk
), "Unsupported socket: shutdown callout must be supported.\n");
1014 static int nbd_add_socket(struct nbd_device
*nbd
, unsigned long arg
,
1017 struct nbd_config
*config
= nbd
->config
;
1018 struct socket
*sock
;
1019 struct nbd_sock
**socks
;
1020 struct nbd_sock
*nsock
;
1023 sock
= nbd_get_socket(nbd
, arg
, &err
);
1027 if (!netlink
&& !nbd
->task_setup
&&
1028 !test_bit(NBD_RT_BOUND
, &config
->runtime_flags
))
1029 nbd
->task_setup
= current
;
1032 (nbd
->task_setup
!= current
||
1033 test_bit(NBD_RT_BOUND
, &config
->runtime_flags
))) {
1034 dev_err(disk_to_dev(nbd
->disk
),
1035 "Device being setup by another task");
1040 socks
= krealloc(config
->socks
, (config
->num_connections
+ 1) *
1041 sizeof(struct nbd_sock
*), GFP_KERNEL
);
1047 config
->socks
= socks
;
1049 nsock
= kzalloc(sizeof(struct nbd_sock
), GFP_KERNEL
);
1055 nsock
->fallback_index
= -1;
1056 nsock
->dead
= false;
1057 mutex_init(&nsock
->tx_lock
);
1059 nsock
->pending
= NULL
;
1062 socks
[config
->num_connections
++] = nsock
;
1063 atomic_inc(&config
->live_connections
);
1068 static int nbd_reconnect_socket(struct nbd_device
*nbd
, unsigned long arg
)
1070 struct nbd_config
*config
= nbd
->config
;
1071 struct socket
*sock
, *old
;
1072 struct recv_thread_args
*args
;
1076 sock
= nbd_get_socket(nbd
, arg
, &err
);
1080 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
1086 for (i
= 0; i
< config
->num_connections
; i
++) {
1087 struct nbd_sock
*nsock
= config
->socks
[i
];
1092 mutex_lock(&nsock
->tx_lock
);
1094 mutex_unlock(&nsock
->tx_lock
);
1097 sk_set_memalloc(sock
->sk
);
1098 if (nbd
->tag_set
.timeout
)
1099 sock
->sk
->sk_sndtimeo
= nbd
->tag_set
.timeout
;
1100 atomic_inc(&config
->recv_threads
);
1101 refcount_inc(&nbd
->config_refs
);
1103 nsock
->fallback_index
= -1;
1105 nsock
->dead
= false;
1106 INIT_WORK(&args
->work
, recv_work
);
1110 mutex_unlock(&nsock
->tx_lock
);
1113 clear_bit(NBD_RT_DISCONNECTED
, &config
->runtime_flags
);
1115 /* We take the tx_mutex in an error path in the recv_work, so we
1116 * need to queue_work outside of the tx_mutex.
1118 queue_work(nbd
->recv_workq
, &args
->work
);
1120 atomic_inc(&config
->live_connections
);
1121 wake_up(&config
->conn_wait
);
1129 static void nbd_bdev_reset(struct block_device
*bdev
)
1131 if (bdev
->bd_openers
> 1)
1133 bd_set_size(bdev
, 0);
1136 static void nbd_parse_flags(struct nbd_device
*nbd
)
1138 struct nbd_config
*config
= nbd
->config
;
1139 if (config
->flags
& NBD_FLAG_READ_ONLY
)
1140 set_disk_ro(nbd
->disk
, true);
1142 set_disk_ro(nbd
->disk
, false);
1143 if (config
->flags
& NBD_FLAG_SEND_TRIM
)
1144 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
1145 if (config
->flags
& NBD_FLAG_SEND_FLUSH
) {
1146 if (config
->flags
& NBD_FLAG_SEND_FUA
)
1147 blk_queue_write_cache(nbd
->disk
->queue
, true, true);
1149 blk_queue_write_cache(nbd
->disk
->queue
, true, false);
1152 blk_queue_write_cache(nbd
->disk
->queue
, false, false);
1155 static void send_disconnects(struct nbd_device
*nbd
)
1157 struct nbd_config
*config
= nbd
->config
;
1158 struct nbd_request request
= {
1159 .magic
= htonl(NBD_REQUEST_MAGIC
),
1160 .type
= htonl(NBD_CMD_DISC
),
1162 struct kvec iov
= {.iov_base
= &request
, .iov_len
= sizeof(request
)};
1163 struct iov_iter from
;
1166 for (i
= 0; i
< config
->num_connections
; i
++) {
1167 struct nbd_sock
*nsock
= config
->socks
[i
];
1169 iov_iter_kvec(&from
, WRITE
, &iov
, 1, sizeof(request
));
1170 mutex_lock(&nsock
->tx_lock
);
1171 ret
= sock_xmit(nbd
, i
, 1, &from
, 0, NULL
);
1173 dev_err(disk_to_dev(nbd
->disk
),
1174 "Send disconnect failed %d\n", ret
);
1175 mutex_unlock(&nsock
->tx_lock
);
1179 static int nbd_disconnect(struct nbd_device
*nbd
)
1181 struct nbd_config
*config
= nbd
->config
;
1183 dev_info(disk_to_dev(nbd
->disk
), "NBD_DISCONNECT\n");
1184 set_bit(NBD_RT_DISCONNECT_REQUESTED
, &config
->runtime_flags
);
1185 set_bit(NBD_DISCONNECT_REQUESTED
, &nbd
->flags
);
1186 send_disconnects(nbd
);
1190 static void nbd_clear_sock(struct nbd_device
*nbd
)
1194 nbd
->task_setup
= NULL
;
1197 static void nbd_config_put(struct nbd_device
*nbd
)
1199 if (refcount_dec_and_mutex_lock(&nbd
->config_refs
,
1200 &nbd
->config_lock
)) {
1201 struct nbd_config
*config
= nbd
->config
;
1202 nbd_dev_dbg_close(nbd
);
1203 nbd_size_clear(nbd
);
1204 if (test_and_clear_bit(NBD_RT_HAS_PID_FILE
,
1205 &config
->runtime_flags
))
1206 device_remove_file(disk_to_dev(nbd
->disk
), &pid_attr
);
1207 nbd
->task_recv
= NULL
;
1208 nbd_clear_sock(nbd
);
1209 if (config
->num_connections
) {
1211 for (i
= 0; i
< config
->num_connections
; i
++) {
1212 sockfd_put(config
->socks
[i
]->sock
);
1213 kfree(config
->socks
[i
]);
1215 kfree(config
->socks
);
1220 if (nbd
->recv_workq
)
1221 destroy_workqueue(nbd
->recv_workq
);
1222 nbd
->recv_workq
= NULL
;
1224 nbd
->tag_set
.timeout
= 0;
1225 nbd
->disk
->queue
->limits
.discard_granularity
= 0;
1226 nbd
->disk
->queue
->limits
.discard_alignment
= 0;
1227 blk_queue_max_discard_sectors(nbd
->disk
->queue
, UINT_MAX
);
1228 blk_queue_flag_clear(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
1230 mutex_unlock(&nbd
->config_lock
);
1232 module_put(THIS_MODULE
);
1236 static int nbd_start_device(struct nbd_device
*nbd
)
1238 struct nbd_config
*config
= nbd
->config
;
1239 int num_connections
= config
->num_connections
;
1246 if (num_connections
> 1 &&
1247 !(config
->flags
& NBD_FLAG_CAN_MULTI_CONN
)) {
1248 dev_err(disk_to_dev(nbd
->disk
), "server does not support multiple connections per device.\n");
1252 nbd
->recv_workq
= alloc_workqueue("knbd%d-recv",
1253 WQ_MEM_RECLAIM
| WQ_HIGHPRI
|
1254 WQ_UNBOUND
, 0, nbd
->index
);
1255 if (!nbd
->recv_workq
) {
1256 dev_err(disk_to_dev(nbd
->disk
), "Could not allocate knbd recv work queue.\n");
1260 blk_mq_update_nr_hw_queues(&nbd
->tag_set
, config
->num_connections
);
1261 nbd
->task_recv
= current
;
1263 nbd_parse_flags(nbd
);
1265 error
= device_create_file(disk_to_dev(nbd
->disk
), &pid_attr
);
1267 dev_err(disk_to_dev(nbd
->disk
), "device_create_file failed!\n");
1270 set_bit(NBD_RT_HAS_PID_FILE
, &config
->runtime_flags
);
1272 nbd_dev_dbg_init(nbd
);
1273 for (i
= 0; i
< num_connections
; i
++) {
1274 struct recv_thread_args
*args
;
1276 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
1280 * If num_connections is m (2 < m),
1281 * and NO.1 ~ NO.n(1 < n < m) kzallocs are successful.
1282 * But NO.(n + 1) failed. We still have n recv threads.
1283 * So, add flush_workqueue here to prevent recv threads
1284 * dropping the last config_refs and trying to destroy
1285 * the workqueue from inside the workqueue.
1288 flush_workqueue(nbd
->recv_workq
);
1291 sk_set_memalloc(config
->socks
[i
]->sock
->sk
);
1292 if (nbd
->tag_set
.timeout
)
1293 config
->socks
[i
]->sock
->sk
->sk_sndtimeo
=
1294 nbd
->tag_set
.timeout
;
1295 atomic_inc(&config
->recv_threads
);
1296 refcount_inc(&nbd
->config_refs
);
1297 INIT_WORK(&args
->work
, recv_work
);
1300 queue_work(nbd
->recv_workq
, &args
->work
);
1302 nbd_size_update(nbd
);
1306 static int nbd_start_device_ioctl(struct nbd_device
*nbd
, struct block_device
*bdev
)
1308 struct nbd_config
*config
= nbd
->config
;
1311 ret
= nbd_start_device(nbd
);
1316 bdev
->bd_invalidated
= 1;
1317 mutex_unlock(&nbd
->config_lock
);
1318 ret
= wait_event_interruptible(config
->recv_wq
,
1319 atomic_read(&config
->recv_threads
) == 0);
1322 flush_workqueue(nbd
->recv_workq
);
1324 mutex_lock(&nbd
->config_lock
);
1325 nbd_bdev_reset(bdev
);
1326 /* user requested, ignore socket errors */
1327 if (test_bit(NBD_RT_DISCONNECT_REQUESTED
, &config
->runtime_flags
))
1329 if (test_bit(NBD_RT_TIMEDOUT
, &config
->runtime_flags
))
1334 static void nbd_clear_sock_ioctl(struct nbd_device
*nbd
,
1335 struct block_device
*bdev
)
1338 __invalidate_device(bdev
, true);
1339 nbd_bdev_reset(bdev
);
1340 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF
,
1341 &nbd
->config
->runtime_flags
))
1342 nbd_config_put(nbd
);
1345 static bool nbd_is_valid_blksize(unsigned long blksize
)
1347 if (!blksize
|| !is_power_of_2(blksize
) || blksize
< 512 ||
1348 blksize
> PAGE_SIZE
)
1353 static void nbd_set_cmd_timeout(struct nbd_device
*nbd
, u64 timeout
)
1355 nbd
->tag_set
.timeout
= timeout
* HZ
;
1357 blk_queue_rq_timeout(nbd
->disk
->queue
, timeout
* HZ
);
1360 /* Must be called with config_lock held */
1361 static int __nbd_ioctl(struct block_device
*bdev
, struct nbd_device
*nbd
,
1362 unsigned int cmd
, unsigned long arg
)
1364 struct nbd_config
*config
= nbd
->config
;
1367 case NBD_DISCONNECT
:
1368 return nbd_disconnect(nbd
);
1369 case NBD_CLEAR_SOCK
:
1370 nbd_clear_sock_ioctl(nbd
, bdev
);
1373 return nbd_add_socket(nbd
, arg
, false);
1374 case NBD_SET_BLKSIZE
:
1376 arg
= NBD_DEF_BLKSIZE
;
1377 if (!nbd_is_valid_blksize(arg
))
1379 nbd_size_set(nbd
, arg
,
1380 div_s64(config
->bytesize
, arg
));
1383 nbd_size_set(nbd
, config
->blksize
,
1384 div_s64(arg
, config
->blksize
));
1386 case NBD_SET_SIZE_BLOCKS
:
1387 nbd_size_set(nbd
, config
->blksize
, arg
);
1389 case NBD_SET_TIMEOUT
:
1390 nbd_set_cmd_timeout(nbd
, arg
);
1394 config
->flags
= arg
;
1397 return nbd_start_device_ioctl(nbd
, bdev
);
1400 * This is for compatibility only. The queue is always cleared
1401 * by NBD_DO_IT or NBD_CLEAR_SOCK.
1404 case NBD_PRINT_DEBUG
:
1406 * For compatibility only, we no longer keep a list of
1407 * outstanding requests.
1414 static int nbd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1415 unsigned int cmd
, unsigned long arg
)
1417 struct nbd_device
*nbd
= bdev
->bd_disk
->private_data
;
1418 struct nbd_config
*config
= nbd
->config
;
1419 int error
= -EINVAL
;
1421 if (!capable(CAP_SYS_ADMIN
))
1424 /* The block layer will pass back some non-nbd ioctls in case we have
1425 * special handling for them, but we don't so just return an error.
1427 if (_IOC_TYPE(cmd
) != 0xab)
1430 mutex_lock(&nbd
->config_lock
);
1432 /* Don't allow ioctl operations on a nbd device that was created with
1433 * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine.
1435 if (!test_bit(NBD_RT_BOUND
, &config
->runtime_flags
) ||
1436 (cmd
== NBD_DISCONNECT
|| cmd
== NBD_CLEAR_SOCK
))
1437 error
= __nbd_ioctl(bdev
, nbd
, cmd
, arg
);
1439 dev_err(nbd_to_dev(nbd
), "Cannot use ioctl interface on a netlink controlled device.\n");
1440 mutex_unlock(&nbd
->config_lock
);
1444 static struct nbd_config
*nbd_alloc_config(void)
1446 struct nbd_config
*config
;
1448 config
= kzalloc(sizeof(struct nbd_config
), GFP_NOFS
);
1451 atomic_set(&config
->recv_threads
, 0);
1452 init_waitqueue_head(&config
->recv_wq
);
1453 init_waitqueue_head(&config
->conn_wait
);
1454 config
->blksize
= NBD_DEF_BLKSIZE
;
1455 atomic_set(&config
->live_connections
, 0);
1456 try_module_get(THIS_MODULE
);
1460 static int nbd_open(struct block_device
*bdev
, fmode_t mode
)
1462 struct nbd_device
*nbd
;
1465 mutex_lock(&nbd_index_mutex
);
1466 nbd
= bdev
->bd_disk
->private_data
;
1471 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1475 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
1476 struct nbd_config
*config
;
1478 mutex_lock(&nbd
->config_lock
);
1479 if (refcount_inc_not_zero(&nbd
->config_refs
)) {
1480 mutex_unlock(&nbd
->config_lock
);
1483 config
= nbd
->config
= nbd_alloc_config();
1486 mutex_unlock(&nbd
->config_lock
);
1489 refcount_set(&nbd
->config_refs
, 1);
1490 refcount_inc(&nbd
->refs
);
1491 mutex_unlock(&nbd
->config_lock
);
1492 bdev
->bd_invalidated
= 1;
1493 } else if (nbd_disconnected(nbd
->config
)) {
1494 bdev
->bd_invalidated
= 1;
1497 mutex_unlock(&nbd_index_mutex
);
1501 static void nbd_release(struct gendisk
*disk
, fmode_t mode
)
1503 struct nbd_device
*nbd
= disk
->private_data
;
1504 struct block_device
*bdev
= bdget_disk(disk
, 0);
1506 if (test_bit(NBD_RT_DISCONNECT_ON_CLOSE
, &nbd
->config
->runtime_flags
) &&
1507 bdev
->bd_openers
== 0)
1508 nbd_disconnect_and_put(nbd
);
1510 nbd_config_put(nbd
);
1514 static const struct block_device_operations nbd_fops
=
1516 .owner
= THIS_MODULE
,
1518 .release
= nbd_release
,
1520 .compat_ioctl
= nbd_ioctl
,
1523 #if IS_ENABLED(CONFIG_DEBUG_FS)
1525 static int nbd_dbg_tasks_show(struct seq_file
*s
, void *unused
)
1527 struct nbd_device
*nbd
= s
->private;
1530 seq_printf(s
, "recv: %d\n", task_pid_nr(nbd
->task_recv
));
1535 static int nbd_dbg_tasks_open(struct inode
*inode
, struct file
*file
)
1537 return single_open(file
, nbd_dbg_tasks_show
, inode
->i_private
);
1540 static const struct file_operations nbd_dbg_tasks_ops
= {
1541 .open
= nbd_dbg_tasks_open
,
1543 .llseek
= seq_lseek
,
1544 .release
= single_release
,
1547 static int nbd_dbg_flags_show(struct seq_file
*s
, void *unused
)
1549 struct nbd_device
*nbd
= s
->private;
1550 u32 flags
= nbd
->config
->flags
;
1552 seq_printf(s
, "Hex: 0x%08x\n\n", flags
);
1554 seq_puts(s
, "Known flags:\n");
1556 if (flags
& NBD_FLAG_HAS_FLAGS
)
1557 seq_puts(s
, "NBD_FLAG_HAS_FLAGS\n");
1558 if (flags
& NBD_FLAG_READ_ONLY
)
1559 seq_puts(s
, "NBD_FLAG_READ_ONLY\n");
1560 if (flags
& NBD_FLAG_SEND_FLUSH
)
1561 seq_puts(s
, "NBD_FLAG_SEND_FLUSH\n");
1562 if (flags
& NBD_FLAG_SEND_FUA
)
1563 seq_puts(s
, "NBD_FLAG_SEND_FUA\n");
1564 if (flags
& NBD_FLAG_SEND_TRIM
)
1565 seq_puts(s
, "NBD_FLAG_SEND_TRIM\n");
1570 static int nbd_dbg_flags_open(struct inode
*inode
, struct file
*file
)
1572 return single_open(file
, nbd_dbg_flags_show
, inode
->i_private
);
1575 static const struct file_operations nbd_dbg_flags_ops
= {
1576 .open
= nbd_dbg_flags_open
,
1578 .llseek
= seq_lseek
,
1579 .release
= single_release
,
1582 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
1585 struct nbd_config
*config
= nbd
->config
;
1590 dir
= debugfs_create_dir(nbd_name(nbd
), nbd_dbg_dir
);
1592 dev_err(nbd_to_dev(nbd
), "Failed to create debugfs dir for '%s'\n",
1596 config
->dbg_dir
= dir
;
1598 debugfs_create_file("tasks", 0444, dir
, nbd
, &nbd_dbg_tasks_ops
);
1599 debugfs_create_u64("size_bytes", 0444, dir
, &config
->bytesize
);
1600 debugfs_create_u32("timeout", 0444, dir
, &nbd
->tag_set
.timeout
);
1601 debugfs_create_u64("blocksize", 0444, dir
, &config
->blksize
);
1602 debugfs_create_file("flags", 0444, dir
, nbd
, &nbd_dbg_flags_ops
);
1607 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
1609 debugfs_remove_recursive(nbd
->config
->dbg_dir
);
1612 static int nbd_dbg_init(void)
1614 struct dentry
*dbg_dir
;
1616 dbg_dir
= debugfs_create_dir("nbd", NULL
);
1620 nbd_dbg_dir
= dbg_dir
;
1625 static void nbd_dbg_close(void)
1627 debugfs_remove_recursive(nbd_dbg_dir
);
1630 #else /* IS_ENABLED(CONFIG_DEBUG_FS) */
1632 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
1637 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
1641 static int nbd_dbg_init(void)
1646 static void nbd_dbg_close(void)
1652 static int nbd_init_request(struct blk_mq_tag_set
*set
, struct request
*rq
,
1653 unsigned int hctx_idx
, unsigned int numa_node
)
1655 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
1656 cmd
->nbd
= set
->driver_data
;
1658 mutex_init(&cmd
->lock
);
1662 static const struct blk_mq_ops nbd_mq_ops
= {
1663 .queue_rq
= nbd_queue_rq
,
1664 .complete
= nbd_complete_rq
,
1665 .init_request
= nbd_init_request
,
1666 .timeout
= nbd_xmit_timeout
,
1669 static int nbd_dev_add(int index
)
1671 struct nbd_device
*nbd
;
1672 struct gendisk
*disk
;
1673 struct request_queue
*q
;
1676 nbd
= kzalloc(sizeof(struct nbd_device
), GFP_KERNEL
);
1680 disk
= alloc_disk(1 << part_shift
);
1685 err
= idr_alloc(&nbd_index_idr
, nbd
, index
, index
+ 1,
1690 err
= idr_alloc(&nbd_index_idr
, nbd
, 0, 0, GFP_KERNEL
);
1699 nbd
->tag_set
.ops
= &nbd_mq_ops
;
1700 nbd
->tag_set
.nr_hw_queues
= 1;
1701 nbd
->tag_set
.queue_depth
= 128;
1702 nbd
->tag_set
.numa_node
= NUMA_NO_NODE
;
1703 nbd
->tag_set
.cmd_size
= sizeof(struct nbd_cmd
);
1704 nbd
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
|
1706 nbd
->tag_set
.driver_data
= nbd
;
1707 nbd
->destroy_complete
= NULL
;
1709 err
= blk_mq_alloc_tag_set(&nbd
->tag_set
);
1713 q
= blk_mq_init_queue(&nbd
->tag_set
);
1721 * Tell the block layer that we are not a rotational device
1723 blk_queue_flag_set(QUEUE_FLAG_NONROT
, disk
->queue
);
1724 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, disk
->queue
);
1725 disk
->queue
->limits
.discard_granularity
= 0;
1726 disk
->queue
->limits
.discard_alignment
= 0;
1727 blk_queue_max_discard_sectors(disk
->queue
, 0);
1728 blk_queue_max_segment_size(disk
->queue
, UINT_MAX
);
1729 blk_queue_max_segments(disk
->queue
, USHRT_MAX
);
1730 blk_queue_max_hw_sectors(disk
->queue
, 65536);
1731 disk
->queue
->limits
.max_sectors
= 256;
1733 mutex_init(&nbd
->config_lock
);
1734 refcount_set(&nbd
->config_refs
, 0);
1735 refcount_set(&nbd
->refs
, 1);
1736 INIT_LIST_HEAD(&nbd
->list
);
1737 disk
->major
= NBD_MAJOR
;
1738 disk
->first_minor
= index
<< part_shift
;
1739 disk
->fops
= &nbd_fops
;
1740 disk
->private_data
= nbd
;
1741 sprintf(disk
->disk_name
, "nbd%d", index
);
1743 nbd_total_devices
++;
1747 blk_mq_free_tag_set(&nbd
->tag_set
);
1749 idr_remove(&nbd_index_idr
, index
);
1758 static int find_free_cb(int id
, void *ptr
, void *data
)
1760 struct nbd_device
*nbd
= ptr
;
1761 struct nbd_device
**found
= data
;
1763 if (!refcount_read(&nbd
->config_refs
)) {
1770 /* Netlink interface. */
1771 static const struct nla_policy nbd_attr_policy
[NBD_ATTR_MAX
+ 1] = {
1772 [NBD_ATTR_INDEX
] = { .type
= NLA_U32
},
1773 [NBD_ATTR_SIZE_BYTES
] = { .type
= NLA_U64
},
1774 [NBD_ATTR_BLOCK_SIZE_BYTES
] = { .type
= NLA_U64
},
1775 [NBD_ATTR_TIMEOUT
] = { .type
= NLA_U64
},
1776 [NBD_ATTR_SERVER_FLAGS
] = { .type
= NLA_U64
},
1777 [NBD_ATTR_CLIENT_FLAGS
] = { .type
= NLA_U64
},
1778 [NBD_ATTR_SOCKETS
] = { .type
= NLA_NESTED
},
1779 [NBD_ATTR_DEAD_CONN_TIMEOUT
] = { .type
= NLA_U64
},
1780 [NBD_ATTR_DEVICE_LIST
] = { .type
= NLA_NESTED
},
1783 static const struct nla_policy nbd_sock_policy
[NBD_SOCK_MAX
+ 1] = {
1784 [NBD_SOCK_FD
] = { .type
= NLA_U32
},
1787 /* We don't use this right now since we don't parse the incoming list, but we
1788 * still want it here so userspace knows what to expect.
1790 static const struct nla_policy
__attribute__((unused
))
1791 nbd_device_policy
[NBD_DEVICE_ATTR_MAX
+ 1] = {
1792 [NBD_DEVICE_INDEX
] = { .type
= NLA_U32
},
1793 [NBD_DEVICE_CONNECTED
] = { .type
= NLA_U8
},
1796 static int nbd_genl_size_set(struct genl_info
*info
, struct nbd_device
*nbd
)
1798 struct nbd_config
*config
= nbd
->config
;
1799 u64 bsize
= config
->blksize
;
1800 u64 bytes
= config
->bytesize
;
1802 if (info
->attrs
[NBD_ATTR_SIZE_BYTES
])
1803 bytes
= nla_get_u64(info
->attrs
[NBD_ATTR_SIZE_BYTES
]);
1805 if (info
->attrs
[NBD_ATTR_BLOCK_SIZE_BYTES
]) {
1806 bsize
= nla_get_u64(info
->attrs
[NBD_ATTR_BLOCK_SIZE_BYTES
]);
1808 bsize
= NBD_DEF_BLKSIZE
;
1809 if (!nbd_is_valid_blksize(bsize
)) {
1810 printk(KERN_ERR
"Invalid block size %llu\n", bsize
);
1815 if (bytes
!= config
->bytesize
|| bsize
!= config
->blksize
)
1816 nbd_size_set(nbd
, bsize
, div64_u64(bytes
, bsize
));
1820 static int nbd_genl_connect(struct sk_buff
*skb
, struct genl_info
*info
)
1822 DECLARE_COMPLETION_ONSTACK(destroy_complete
);
1823 struct nbd_device
*nbd
= NULL
;
1824 struct nbd_config
*config
;
1827 bool put_dev
= false;
1829 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
1832 if (info
->attrs
[NBD_ATTR_INDEX
])
1833 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
1834 if (!info
->attrs
[NBD_ATTR_SOCKETS
]) {
1835 printk(KERN_ERR
"nbd: must specify at least one socket\n");
1838 if (!info
->attrs
[NBD_ATTR_SIZE_BYTES
]) {
1839 printk(KERN_ERR
"nbd: must specify a size in bytes for the device\n");
1843 mutex_lock(&nbd_index_mutex
);
1845 ret
= idr_for_each(&nbd_index_idr
, &find_free_cb
, &nbd
);
1848 new_index
= nbd_dev_add(-1);
1849 if (new_index
< 0) {
1850 mutex_unlock(&nbd_index_mutex
);
1851 printk(KERN_ERR
"nbd: failed to add new device\n");
1854 nbd
= idr_find(&nbd_index_idr
, new_index
);
1857 nbd
= idr_find(&nbd_index_idr
, index
);
1859 ret
= nbd_dev_add(index
);
1861 mutex_unlock(&nbd_index_mutex
);
1862 printk(KERN_ERR
"nbd: failed to add new device\n");
1865 nbd
= idr_find(&nbd_index_idr
, index
);
1869 printk(KERN_ERR
"nbd: couldn't find device at index %d\n",
1871 mutex_unlock(&nbd_index_mutex
);
1875 if (test_bit(NBD_DESTROY_ON_DISCONNECT
, &nbd
->flags
) &&
1876 test_bit(NBD_DISCONNECT_REQUESTED
, &nbd
->flags
)) {
1877 nbd
->destroy_complete
= &destroy_complete
;
1878 mutex_unlock(&nbd_index_mutex
);
1880 /* Wait untill the the nbd stuff is totally destroyed */
1881 wait_for_completion(&destroy_complete
);
1885 if (!refcount_inc_not_zero(&nbd
->refs
)) {
1886 mutex_unlock(&nbd_index_mutex
);
1889 printk(KERN_ERR
"nbd: device at index %d is going down\n",
1893 mutex_unlock(&nbd_index_mutex
);
1895 mutex_lock(&nbd
->config_lock
);
1896 if (refcount_read(&nbd
->config_refs
)) {
1897 mutex_unlock(&nbd
->config_lock
);
1901 printk(KERN_ERR
"nbd: nbd%d already in use\n", index
);
1904 if (WARN_ON(nbd
->config
)) {
1905 mutex_unlock(&nbd
->config_lock
);
1909 config
= nbd
->config
= nbd_alloc_config();
1911 mutex_unlock(&nbd
->config_lock
);
1913 printk(KERN_ERR
"nbd: couldn't allocate config\n");
1916 refcount_set(&nbd
->config_refs
, 1);
1917 set_bit(NBD_RT_BOUND
, &config
->runtime_flags
);
1919 ret
= nbd_genl_size_set(info
, nbd
);
1923 if (info
->attrs
[NBD_ATTR_TIMEOUT
])
1924 nbd_set_cmd_timeout(nbd
,
1925 nla_get_u64(info
->attrs
[NBD_ATTR_TIMEOUT
]));
1926 if (info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]) {
1927 config
->dead_conn_timeout
=
1928 nla_get_u64(info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]);
1929 config
->dead_conn_timeout
*= HZ
;
1931 if (info
->attrs
[NBD_ATTR_SERVER_FLAGS
])
1933 nla_get_u64(info
->attrs
[NBD_ATTR_SERVER_FLAGS
]);
1934 if (info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]) {
1935 u64 flags
= nla_get_u64(info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]);
1936 if (flags
& NBD_CFLAG_DESTROY_ON_DISCONNECT
) {
1937 set_bit(NBD_RT_DESTROY_ON_DISCONNECT
,
1938 &config
->runtime_flags
);
1939 set_bit(NBD_DESTROY_ON_DISCONNECT
, &nbd
->flags
);
1942 clear_bit(NBD_DESTROY_ON_DISCONNECT
, &nbd
->flags
);
1944 if (flags
& NBD_CFLAG_DISCONNECT_ON_CLOSE
) {
1945 set_bit(NBD_RT_DISCONNECT_ON_CLOSE
,
1946 &config
->runtime_flags
);
1950 if (info
->attrs
[NBD_ATTR_SOCKETS
]) {
1951 struct nlattr
*attr
;
1954 nla_for_each_nested(attr
, info
->attrs
[NBD_ATTR_SOCKETS
],
1956 struct nlattr
*socks
[NBD_SOCK_MAX
+1];
1958 if (nla_type(attr
) != NBD_SOCK_ITEM
) {
1959 printk(KERN_ERR
"nbd: socks must be embedded in a SOCK_ITEM attr\n");
1963 ret
= nla_parse_nested_deprecated(socks
, NBD_SOCK_MAX
,
1968 printk(KERN_ERR
"nbd: error processing sock list\n");
1972 if (!socks
[NBD_SOCK_FD
])
1974 fd
= (int)nla_get_u32(socks
[NBD_SOCK_FD
]);
1975 ret
= nbd_add_socket(nbd
, fd
, true);
1980 ret
= nbd_start_device(nbd
);
1982 mutex_unlock(&nbd
->config_lock
);
1984 set_bit(NBD_RT_HAS_CONFIG_REF
, &config
->runtime_flags
);
1985 refcount_inc(&nbd
->config_refs
);
1986 nbd_connect_reply(info
, nbd
->index
);
1988 nbd_config_put(nbd
);
1994 static void nbd_disconnect_and_put(struct nbd_device
*nbd
)
1996 mutex_lock(&nbd
->config_lock
);
1997 nbd_disconnect(nbd
);
1998 nbd_clear_sock(nbd
);
1999 mutex_unlock(&nbd
->config_lock
);
2001 * Make sure recv thread has finished, so it does not drop the last
2002 * config ref and try to destroy the workqueue from inside the work
2005 flush_workqueue(nbd
->recv_workq
);
2006 if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF
,
2007 &nbd
->config
->runtime_flags
))
2008 nbd_config_put(nbd
);
2011 static int nbd_genl_disconnect(struct sk_buff
*skb
, struct genl_info
*info
)
2013 struct nbd_device
*nbd
;
2016 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
2019 if (!info
->attrs
[NBD_ATTR_INDEX
]) {
2020 printk(KERN_ERR
"nbd: must specify an index to disconnect\n");
2023 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
2024 mutex_lock(&nbd_index_mutex
);
2025 nbd
= idr_find(&nbd_index_idr
, index
);
2027 mutex_unlock(&nbd_index_mutex
);
2028 printk(KERN_ERR
"nbd: couldn't find device at index %d\n",
2032 if (!refcount_inc_not_zero(&nbd
->refs
)) {
2033 mutex_unlock(&nbd_index_mutex
);
2034 printk(KERN_ERR
"nbd: device at index %d is going down\n",
2038 mutex_unlock(&nbd_index_mutex
);
2039 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
2043 nbd_disconnect_and_put(nbd
);
2044 nbd_config_put(nbd
);
2049 static int nbd_genl_reconfigure(struct sk_buff
*skb
, struct genl_info
*info
)
2051 struct nbd_device
*nbd
= NULL
;
2052 struct nbd_config
*config
;
2055 bool put_dev
= false;
2057 if (!netlink_capable(skb
, CAP_SYS_ADMIN
))
2060 if (!info
->attrs
[NBD_ATTR_INDEX
]) {
2061 printk(KERN_ERR
"nbd: must specify a device to reconfigure\n");
2064 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
2065 mutex_lock(&nbd_index_mutex
);
2066 nbd
= idr_find(&nbd_index_idr
, index
);
2068 mutex_unlock(&nbd_index_mutex
);
2069 printk(KERN_ERR
"nbd: couldn't find a device at index %d\n",
2073 if (!refcount_inc_not_zero(&nbd
->refs
)) {
2074 mutex_unlock(&nbd_index_mutex
);
2075 printk(KERN_ERR
"nbd: device at index %d is going down\n",
2079 mutex_unlock(&nbd_index_mutex
);
2081 if (!refcount_inc_not_zero(&nbd
->config_refs
)) {
2082 dev_err(nbd_to_dev(nbd
),
2083 "not configured, cannot reconfigure\n");
2088 mutex_lock(&nbd
->config_lock
);
2089 config
= nbd
->config
;
2090 if (!test_bit(NBD_RT_BOUND
, &config
->runtime_flags
) ||
2092 dev_err(nbd_to_dev(nbd
),
2093 "not configured, cannot reconfigure\n");
2098 ret
= nbd_genl_size_set(info
, nbd
);
2102 if (info
->attrs
[NBD_ATTR_TIMEOUT
])
2103 nbd_set_cmd_timeout(nbd
,
2104 nla_get_u64(info
->attrs
[NBD_ATTR_TIMEOUT
]));
2105 if (info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]) {
2106 config
->dead_conn_timeout
=
2107 nla_get_u64(info
->attrs
[NBD_ATTR_DEAD_CONN_TIMEOUT
]);
2108 config
->dead_conn_timeout
*= HZ
;
2110 if (info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]) {
2111 u64 flags
= nla_get_u64(info
->attrs
[NBD_ATTR_CLIENT_FLAGS
]);
2112 if (flags
& NBD_CFLAG_DESTROY_ON_DISCONNECT
) {
2113 if (!test_and_set_bit(NBD_RT_DESTROY_ON_DISCONNECT
,
2114 &config
->runtime_flags
))
2116 set_bit(NBD_DESTROY_ON_DISCONNECT
, &nbd
->flags
);
2118 if (test_and_clear_bit(NBD_RT_DESTROY_ON_DISCONNECT
,
2119 &config
->runtime_flags
))
2120 refcount_inc(&nbd
->refs
);
2121 clear_bit(NBD_DESTROY_ON_DISCONNECT
, &nbd
->flags
);
2124 if (flags
& NBD_CFLAG_DISCONNECT_ON_CLOSE
) {
2125 set_bit(NBD_RT_DISCONNECT_ON_CLOSE
,
2126 &config
->runtime_flags
);
2128 clear_bit(NBD_RT_DISCONNECT_ON_CLOSE
,
2129 &config
->runtime_flags
);
2133 if (info
->attrs
[NBD_ATTR_SOCKETS
]) {
2134 struct nlattr
*attr
;
2137 nla_for_each_nested(attr
, info
->attrs
[NBD_ATTR_SOCKETS
],
2139 struct nlattr
*socks
[NBD_SOCK_MAX
+1];
2141 if (nla_type(attr
) != NBD_SOCK_ITEM
) {
2142 printk(KERN_ERR
"nbd: socks must be embedded in a SOCK_ITEM attr\n");
2146 ret
= nla_parse_nested_deprecated(socks
, NBD_SOCK_MAX
,
2151 printk(KERN_ERR
"nbd: error processing sock list\n");
2155 if (!socks
[NBD_SOCK_FD
])
2157 fd
= (int)nla_get_u32(socks
[NBD_SOCK_FD
]);
2158 ret
= nbd_reconnect_socket(nbd
, fd
);
2164 dev_info(nbd_to_dev(nbd
), "reconnected socket\n");
2168 mutex_unlock(&nbd
->config_lock
);
2169 nbd_config_put(nbd
);
2176 static const struct genl_ops nbd_connect_genl_ops
[] = {
2178 .cmd
= NBD_CMD_CONNECT
,
2179 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2180 .doit
= nbd_genl_connect
,
2183 .cmd
= NBD_CMD_DISCONNECT
,
2184 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2185 .doit
= nbd_genl_disconnect
,
2188 .cmd
= NBD_CMD_RECONFIGURE
,
2189 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2190 .doit
= nbd_genl_reconfigure
,
2193 .cmd
= NBD_CMD_STATUS
,
2194 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2195 .doit
= nbd_genl_status
,
2199 static const struct genl_multicast_group nbd_mcast_grps
[] = {
2200 { .name
= NBD_GENL_MCAST_GROUP_NAME
, },
2203 static struct genl_family nbd_genl_family __ro_after_init
= {
2205 .name
= NBD_GENL_FAMILY_NAME
,
2206 .version
= NBD_GENL_VERSION
,
2207 .module
= THIS_MODULE
,
2208 .ops
= nbd_connect_genl_ops
,
2209 .n_ops
= ARRAY_SIZE(nbd_connect_genl_ops
),
2210 .maxattr
= NBD_ATTR_MAX
,
2211 .policy
= nbd_attr_policy
,
2212 .mcgrps
= nbd_mcast_grps
,
2213 .n_mcgrps
= ARRAY_SIZE(nbd_mcast_grps
),
2216 static int populate_nbd_status(struct nbd_device
*nbd
, struct sk_buff
*reply
)
2218 struct nlattr
*dev_opt
;
2222 /* This is a little racey, but for status it's ok. The
2223 * reason we don't take a ref here is because we can't
2224 * take a ref in the index == -1 case as we would need
2225 * to put under the nbd_index_mutex, which could
2226 * deadlock if we are configured to remove ourselves
2227 * once we're disconnected.
2229 if (refcount_read(&nbd
->config_refs
))
2231 dev_opt
= nla_nest_start_noflag(reply
, NBD_DEVICE_ITEM
);
2234 ret
= nla_put_u32(reply
, NBD_DEVICE_INDEX
, nbd
->index
);
2237 ret
= nla_put_u8(reply
, NBD_DEVICE_CONNECTED
,
2241 nla_nest_end(reply
, dev_opt
);
2245 static int status_cb(int id
, void *ptr
, void *data
)
2247 struct nbd_device
*nbd
= ptr
;
2248 return populate_nbd_status(nbd
, (struct sk_buff
*)data
);
2251 static int nbd_genl_status(struct sk_buff
*skb
, struct genl_info
*info
)
2253 struct nlattr
*dev_list
;
2254 struct sk_buff
*reply
;
2260 if (info
->attrs
[NBD_ATTR_INDEX
])
2261 index
= nla_get_u32(info
->attrs
[NBD_ATTR_INDEX
]);
2263 mutex_lock(&nbd_index_mutex
);
2265 msg_size
= nla_total_size(nla_attr_size(sizeof(u32
)) +
2266 nla_attr_size(sizeof(u8
)));
2267 msg_size
*= (index
== -1) ? nbd_total_devices
: 1;
2269 reply
= genlmsg_new(msg_size
, GFP_KERNEL
);
2272 reply_head
= genlmsg_put_reply(reply
, info
, &nbd_genl_family
, 0,
2279 dev_list
= nla_nest_start_noflag(reply
, NBD_ATTR_DEVICE_LIST
);
2281 ret
= idr_for_each(&nbd_index_idr
, &status_cb
, reply
);
2287 struct nbd_device
*nbd
;
2288 nbd
= idr_find(&nbd_index_idr
, index
);
2290 ret
= populate_nbd_status(nbd
, reply
);
2297 nla_nest_end(reply
, dev_list
);
2298 genlmsg_end(reply
, reply_head
);
2299 ret
= genlmsg_reply(reply
, info
);
2301 mutex_unlock(&nbd_index_mutex
);
2305 static void nbd_connect_reply(struct genl_info
*info
, int index
)
2307 struct sk_buff
*skb
;
2311 skb
= genlmsg_new(nla_total_size(sizeof(u32
)), GFP_KERNEL
);
2314 msg_head
= genlmsg_put_reply(skb
, info
, &nbd_genl_family
, 0,
2320 ret
= nla_put_u32(skb
, NBD_ATTR_INDEX
, index
);
2325 genlmsg_end(skb
, msg_head
);
2326 genlmsg_reply(skb
, info
);
2329 static void nbd_mcast_index(int index
)
2331 struct sk_buff
*skb
;
2335 skb
= genlmsg_new(nla_total_size(sizeof(u32
)), GFP_KERNEL
);
2338 msg_head
= genlmsg_put(skb
, 0, 0, &nbd_genl_family
, 0,
2344 ret
= nla_put_u32(skb
, NBD_ATTR_INDEX
, index
);
2349 genlmsg_end(skb
, msg_head
);
2350 genlmsg_multicast(&nbd_genl_family
, skb
, 0, 0, GFP_KERNEL
);
2353 static void nbd_dead_link_work(struct work_struct
*work
)
2355 struct link_dead_args
*args
= container_of(work
, struct link_dead_args
,
2357 nbd_mcast_index(args
->index
);
2361 static int __init
nbd_init(void)
2365 BUILD_BUG_ON(sizeof(struct nbd_request
) != 28);
2368 printk(KERN_ERR
"nbd: max_part must be >= 0\n");
2374 part_shift
= fls(max_part
);
2377 * Adjust max_part according to part_shift as it is exported
2378 * to user space so that user can know the max number of
2379 * partition kernel should be able to manage.
2381 * Note that -1 is required because partition 0 is reserved
2382 * for the whole disk.
2384 max_part
= (1UL << part_shift
) - 1;
2387 if ((1UL << part_shift
) > DISK_MAX_PARTS
)
2390 if (nbds_max
> 1UL << (MINORBITS
- part_shift
))
2393 if (register_blkdev(NBD_MAJOR
, "nbd"))
2396 if (genl_register_family(&nbd_genl_family
)) {
2397 unregister_blkdev(NBD_MAJOR
, "nbd");
2402 mutex_lock(&nbd_index_mutex
);
2403 for (i
= 0; i
< nbds_max
; i
++)
2405 mutex_unlock(&nbd_index_mutex
);
2409 static int nbd_exit_cb(int id
, void *ptr
, void *data
)
2411 struct list_head
*list
= (struct list_head
*)data
;
2412 struct nbd_device
*nbd
= ptr
;
2414 list_add_tail(&nbd
->list
, list
);
2418 static void __exit
nbd_cleanup(void)
2420 struct nbd_device
*nbd
;
2421 LIST_HEAD(del_list
);
2425 mutex_lock(&nbd_index_mutex
);
2426 idr_for_each(&nbd_index_idr
, &nbd_exit_cb
, &del_list
);
2427 mutex_unlock(&nbd_index_mutex
);
2429 while (!list_empty(&del_list
)) {
2430 nbd
= list_first_entry(&del_list
, struct nbd_device
, list
);
2431 list_del_init(&nbd
->list
);
2432 if (refcount_read(&nbd
->refs
) != 1)
2433 printk(KERN_ERR
"nbd: possibly leaking a device\n");
2437 idr_destroy(&nbd_index_idr
);
2438 genl_unregister_family(&nbd_genl_family
);
2439 unregister_blkdev(NBD_MAJOR
, "nbd");
2442 module_init(nbd_init
);
2443 module_exit(nbd_cleanup
);
2445 MODULE_DESCRIPTION("Network Block Device");
2446 MODULE_LICENSE("GPL");
2448 module_param(nbds_max
, int, 0444);
2449 MODULE_PARM_DESC(nbds_max
, "number of network block devices to initialize (default: 16)");
2450 module_param(max_part
, int, 0444);
2451 MODULE_PARM_DESC(max_part
, "number of partitions per device (default: 16)");