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>
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/err.h>
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
33 #include <linux/net.h>
34 #include <linux/kthread.h>
35 #include <linux/types.h>
36 #include <linux/debugfs.h>
37 #include <linux/blk-mq.h>
39 #include <asm/uaccess.h>
40 #include <asm/types.h>
42 #include <linux/nbd.h>
44 #define NBD_TIMEDOUT 0
45 #define NBD_DISCONNECT_REQUESTED 1
49 unsigned long runtime_flags
;
50 struct socket
* sock
; /* If == NULL, device is not ready, yet */
53 struct blk_mq_tag_set tag_set
;
60 /* protects initialization and shutdown of the socket */
62 struct task_struct
*task_recv
;
63 struct task_struct
*task_send
;
65 #if IS_ENABLED(CONFIG_DEBUG_FS)
66 struct dentry
*dbg_dir
;
71 struct nbd_device
*nbd
;
72 struct list_head list
;
75 #if IS_ENABLED(CONFIG_DEBUG_FS)
76 static struct dentry
*nbd_dbg_dir
;
79 #define nbd_name(nbd) ((nbd)->disk->disk_name)
81 #define NBD_MAGIC 0x68797548
83 static unsigned int nbds_max
= 16;
84 static struct nbd_device
*nbd_dev
;
87 static inline struct device
*nbd_to_dev(struct nbd_device
*nbd
)
89 return disk_to_dev(nbd
->disk
);
92 static bool nbd_is_connected(struct nbd_device
*nbd
)
94 return !!nbd
->task_recv
;
97 static const char *nbdcmd_to_ascii(int cmd
)
100 case NBD_CMD_READ
: return "read";
101 case NBD_CMD_WRITE
: return "write";
102 case NBD_CMD_DISC
: return "disconnect";
103 case NBD_CMD_FLUSH
: return "flush";
104 case NBD_CMD_TRIM
: return "trim/discard";
109 static int nbd_size_clear(struct nbd_device
*nbd
, struct block_device
*bdev
)
111 bdev
->bd_inode
->i_size
= 0;
112 set_capacity(nbd
->disk
, 0);
113 kobject_uevent(&nbd_to_dev(nbd
)->kobj
, KOBJ_CHANGE
);
118 static void nbd_size_update(struct nbd_device
*nbd
, struct block_device
*bdev
)
120 if (!nbd_is_connected(nbd
))
123 bdev
->bd_inode
->i_size
= nbd
->bytesize
;
124 set_capacity(nbd
->disk
, nbd
->bytesize
>> 9);
125 kobject_uevent(&nbd_to_dev(nbd
)->kobj
, KOBJ_CHANGE
);
128 static int nbd_size_set(struct nbd_device
*nbd
, struct block_device
*bdev
,
129 int blocksize
, int nr_blocks
)
133 ret
= set_blocksize(bdev
, blocksize
);
137 nbd
->blksize
= blocksize
;
138 nbd
->bytesize
= (loff_t
)blocksize
* (loff_t
)nr_blocks
;
140 nbd_size_update(nbd
, bdev
);
145 static void nbd_end_request(struct nbd_cmd
*cmd
)
147 struct nbd_device
*nbd
= cmd
->nbd
;
148 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
149 int error
= req
->errors
? -EIO
: 0;
151 dev_dbg(nbd_to_dev(nbd
), "request %p: %s\n", cmd
,
152 error
? "failed" : "done");
154 blk_mq_complete_request(req
, error
);
158 * Forcibly shutdown the socket causing all listeners to error
160 static void sock_shutdown(struct nbd_device
*nbd
)
164 spin_lock(&nbd
->sock_lock
);
167 spin_unlock_irq(&nbd
->sock_lock
);
172 dev_warn(disk_to_dev(nbd
->disk
), "shutting down socket\n");
174 spin_unlock(&nbd
->sock_lock
);
176 kernel_sock_shutdown(sock
, SHUT_RDWR
);
180 static enum blk_eh_timer_return
nbd_xmit_timeout(struct request
*req
,
183 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
184 struct nbd_device
*nbd
= cmd
->nbd
;
185 struct socket
*sock
= NULL
;
187 spin_lock(&nbd
->sock_lock
);
189 set_bit(NBD_TIMEDOUT
, &nbd
->runtime_flags
);
193 get_file(sock
->file
);
196 spin_unlock(&nbd
->sock_lock
);
198 kernel_sock_shutdown(sock
, SHUT_RDWR
);
203 dev_err(nbd_to_dev(nbd
), "Connection timed out, shutting down connection\n");
204 return BLK_EH_HANDLED
;
208 * Send or receive packet.
210 static int sock_xmit(struct nbd_device
*nbd
, int send
, void *buf
, int size
,
213 struct socket
*sock
= nbd
->sock
;
217 unsigned long pflags
= current
->flags
;
219 if (unlikely(!sock
)) {
220 dev_err(disk_to_dev(nbd
->disk
),
221 "Attempted %s on closed socket in sock_xmit\n",
222 (send
? "send" : "recv"));
226 current
->flags
|= PF_MEMALLOC
;
228 sock
->sk
->sk_allocation
= GFP_NOIO
| __GFP_MEMALLOC
;
233 msg
.msg_control
= NULL
;
234 msg
.msg_controllen
= 0;
235 msg
.msg_flags
= msg_flags
| MSG_NOSIGNAL
;
238 result
= kernel_sendmsg(sock
, &msg
, &iov
, 1, size
);
240 result
= kernel_recvmsg(sock
, &msg
, &iov
, 1, size
,
245 result
= -EPIPE
; /* short read */
252 tsk_restore_flags(current
, pflags
, PF_MEMALLOC
);
257 static inline int sock_send_bvec(struct nbd_device
*nbd
, struct bio_vec
*bvec
,
261 void *kaddr
= kmap(bvec
->bv_page
);
262 result
= sock_xmit(nbd
, 1, kaddr
+ bvec
->bv_offset
,
263 bvec
->bv_len
, flags
);
264 kunmap(bvec
->bv_page
);
268 /* always call with the tx_lock held */
269 static int nbd_send_cmd(struct nbd_device
*nbd
, struct nbd_cmd
*cmd
)
271 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
273 struct nbd_request request
;
274 unsigned long size
= blk_rq_bytes(req
);
277 if (req
->cmd_type
== REQ_TYPE_DRV_PRIV
)
279 else if (req_op(req
) == REQ_OP_DISCARD
)
281 else if (req_op(req
) == REQ_OP_FLUSH
)
282 type
= NBD_CMD_FLUSH
;
283 else if (rq_data_dir(req
) == WRITE
)
284 type
= NBD_CMD_WRITE
;
288 memset(&request
, 0, sizeof(request
));
289 request
.magic
= htonl(NBD_REQUEST_MAGIC
);
290 request
.type
= htonl(type
);
291 if (type
!= NBD_CMD_FLUSH
&& type
!= NBD_CMD_DISC
) {
292 request
.from
= cpu_to_be64((u64
)blk_rq_pos(req
) << 9);
293 request
.len
= htonl(size
);
295 memcpy(request
.handle
, &req
->tag
, sizeof(req
->tag
));
297 dev_dbg(nbd_to_dev(nbd
), "request %p: sending control (%s@%llu,%uB)\n",
298 cmd
, nbdcmd_to_ascii(type
),
299 (unsigned long long)blk_rq_pos(req
) << 9, blk_rq_bytes(req
));
300 result
= sock_xmit(nbd
, 1, &request
, sizeof(request
),
301 (type
== NBD_CMD_WRITE
) ? MSG_MORE
: 0);
303 dev_err(disk_to_dev(nbd
->disk
),
304 "Send control failed (result %d)\n", result
);
308 if (type
== NBD_CMD_WRITE
) {
309 struct req_iterator iter
;
312 * we are really probing at internals to determine
313 * whether to set MSG_MORE or not...
315 rq_for_each_segment(bvec
, req
, iter
) {
317 if (!rq_iter_last(bvec
, iter
))
319 dev_dbg(nbd_to_dev(nbd
), "request %p: sending %d bytes data\n",
321 result
= sock_send_bvec(nbd
, &bvec
, flags
);
323 dev_err(disk_to_dev(nbd
->disk
),
324 "Send data failed (result %d)\n",
333 static inline int sock_recv_bvec(struct nbd_device
*nbd
, struct bio_vec
*bvec
)
336 void *kaddr
= kmap(bvec
->bv_page
);
337 result
= sock_xmit(nbd
, 0, kaddr
+ bvec
->bv_offset
, bvec
->bv_len
,
339 kunmap(bvec
->bv_page
);
343 /* NULL returned = something went wrong, inform userspace */
344 static struct nbd_cmd
*nbd_read_stat(struct nbd_device
*nbd
)
347 struct nbd_reply reply
;
349 struct request
*req
= NULL
;
354 result
= sock_xmit(nbd
, 0, &reply
, sizeof(reply
), MSG_WAITALL
);
356 dev_err(disk_to_dev(nbd
->disk
),
357 "Receive control failed (result %d)\n", result
);
358 return ERR_PTR(result
);
361 if (ntohl(reply
.magic
) != NBD_REPLY_MAGIC
) {
362 dev_err(disk_to_dev(nbd
->disk
), "Wrong magic (0x%lx)\n",
363 (unsigned long)ntohl(reply
.magic
));
364 return ERR_PTR(-EPROTO
);
367 memcpy(&tag
, reply
.handle
, sizeof(int));
369 hwq
= blk_mq_unique_tag_to_hwq(tag
);
370 if (hwq
< nbd
->tag_set
.nr_hw_queues
)
371 req
= blk_mq_tag_to_rq(nbd
->tag_set
.tags
[hwq
],
372 blk_mq_unique_tag_to_tag(tag
));
373 if (!req
|| !blk_mq_request_started(req
)) {
374 dev_err(disk_to_dev(nbd
->disk
), "Unexpected reply (%d) %p\n",
376 return ERR_PTR(-ENOENT
);
378 cmd
= blk_mq_rq_to_pdu(req
);
380 if (ntohl(reply
.error
)) {
381 dev_err(disk_to_dev(nbd
->disk
), "Other side returned error (%d)\n",
387 dev_dbg(nbd_to_dev(nbd
), "request %p: got reply\n", cmd
);
388 if (rq_data_dir(req
) != WRITE
) {
389 struct req_iterator iter
;
392 rq_for_each_segment(bvec
, req
, iter
) {
393 result
= sock_recv_bvec(nbd
, &bvec
);
395 dev_err(disk_to_dev(nbd
->disk
), "Receive data failed (result %d)\n",
400 dev_dbg(nbd_to_dev(nbd
), "request %p: got %d bytes data\n",
407 static ssize_t
pid_show(struct device
*dev
,
408 struct device_attribute
*attr
, char *buf
)
410 struct gendisk
*disk
= dev_to_disk(dev
);
411 struct nbd_device
*nbd
= (struct nbd_device
*)disk
->private_data
;
413 return sprintf(buf
, "%d\n", task_pid_nr(nbd
->task_recv
));
416 static struct device_attribute pid_attr
= {
417 .attr
= { .name
= "pid", .mode
= S_IRUGO
},
421 static int nbd_thread_recv(struct nbd_device
*nbd
, struct block_device
*bdev
)
426 BUG_ON(nbd
->magic
!= NBD_MAGIC
);
428 sk_set_memalloc(nbd
->sock
->sk
);
430 ret
= device_create_file(disk_to_dev(nbd
->disk
), &pid_attr
);
432 dev_err(disk_to_dev(nbd
->disk
), "device_create_file failed!\n");
436 nbd_size_update(nbd
, bdev
);
439 cmd
= nbd_read_stat(nbd
);
445 nbd_end_request(cmd
);
448 nbd_size_clear(nbd
, bdev
);
450 device_remove_file(disk_to_dev(nbd
->disk
), &pid_attr
);
454 static void nbd_clear_req(struct request
*req
, void *data
, bool reserved
)
458 if (!blk_mq_request_started(req
))
460 cmd
= blk_mq_rq_to_pdu(req
);
462 nbd_end_request(cmd
);
465 static void nbd_clear_que(struct nbd_device
*nbd
)
467 BUG_ON(nbd
->magic
!= NBD_MAGIC
);
470 * Because we have set nbd->sock to NULL under the tx_lock, all
471 * modifications to the list must have completed by now.
475 blk_mq_tagset_busy_iter(&nbd
->tag_set
, nbd_clear_req
, NULL
);
476 dev_dbg(disk_to_dev(nbd
->disk
), "queue cleared\n");
480 static void nbd_handle_cmd(struct nbd_cmd
*cmd
)
482 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
483 struct nbd_device
*nbd
= cmd
->nbd
;
485 if (req
->cmd_type
!= REQ_TYPE_FS
)
488 if (rq_data_dir(req
) == WRITE
&&
489 (nbd
->flags
& NBD_FLAG_READ_ONLY
)) {
490 dev_err(disk_to_dev(nbd
->disk
),
491 "Write on read-only\n");
497 mutex_lock(&nbd
->tx_lock
);
498 nbd
->task_send
= current
;
499 if (unlikely(!nbd
->sock
)) {
500 mutex_unlock(&nbd
->tx_lock
);
501 dev_err(disk_to_dev(nbd
->disk
),
502 "Attempted send on closed socket\n");
506 if (nbd_send_cmd(nbd
, cmd
) != 0) {
507 dev_err(disk_to_dev(nbd
->disk
), "Request send failed\n");
509 nbd_end_request(cmd
);
512 nbd
->task_send
= NULL
;
513 mutex_unlock(&nbd
->tx_lock
);
519 nbd_end_request(cmd
);
522 static int nbd_queue_rq(struct blk_mq_hw_ctx
*hctx
,
523 const struct blk_mq_queue_data
*bd
)
525 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(bd
->rq
);
527 blk_mq_start_request(bd
->rq
);
529 return BLK_MQ_RQ_QUEUE_OK
;
532 static int nbd_set_socket(struct nbd_device
*nbd
, struct socket
*sock
)
536 spin_lock_irq(&nbd
->sock_lock
);
546 spin_unlock_irq(&nbd
->sock_lock
);
551 /* Reset all properties of an NBD device */
552 static void nbd_reset(struct nbd_device
*nbd
)
554 nbd
->runtime_flags
= 0;
557 set_capacity(nbd
->disk
, 0);
559 nbd
->tag_set
.timeout
= 0;
560 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
563 static void nbd_bdev_reset(struct block_device
*bdev
)
565 set_device_ro(bdev
, false);
566 bdev
->bd_inode
->i_size
= 0;
568 blkdev_reread_part(bdev
);
569 bdev
->bd_invalidated
= 1;
573 static void nbd_parse_flags(struct nbd_device
*nbd
, struct block_device
*bdev
)
575 if (nbd
->flags
& NBD_FLAG_READ_ONLY
)
576 set_device_ro(bdev
, true);
577 if (nbd
->flags
& NBD_FLAG_SEND_TRIM
)
578 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, nbd
->disk
->queue
);
579 if (nbd
->flags
& NBD_FLAG_SEND_FLUSH
)
580 blk_queue_write_cache(nbd
->disk
->queue
, true, false);
582 blk_queue_write_cache(nbd
->disk
->queue
, false, false);
585 static int nbd_dev_dbg_init(struct nbd_device
*nbd
);
586 static void nbd_dev_dbg_close(struct nbd_device
*nbd
);
588 /* Must be called with tx_lock held */
590 static int __nbd_ioctl(struct block_device
*bdev
, struct nbd_device
*nbd
,
591 unsigned int cmd
, unsigned long arg
)
594 case NBD_DISCONNECT
: {
595 struct request
*sreq
;
597 dev_info(disk_to_dev(nbd
->disk
), "NBD_DISCONNECT\n");
601 sreq
= blk_mq_alloc_request(bdev_get_queue(bdev
), WRITE
, 0);
605 mutex_unlock(&nbd
->tx_lock
);
607 mutex_lock(&nbd
->tx_lock
);
608 sreq
->cmd_type
= REQ_TYPE_DRV_PRIV
;
610 /* Check again after getting mutex back. */
612 blk_mq_free_request(sreq
);
616 set_bit(NBD_DISCONNECT_REQUESTED
, &nbd
->runtime_flags
);
618 nbd_send_cmd(nbd
, blk_mq_rq_to_pdu(sreq
));
619 blk_mq_free_request(sreq
);
631 struct socket
*sock
= sockfd_lookup(arg
, &err
);
636 err
= nbd_set_socket(nbd
, sock
);
637 if (!err
&& max_part
)
638 bdev
->bd_invalidated
= 1;
643 case NBD_SET_BLKSIZE
: {
644 loff_t bsize
= div_s64(nbd
->bytesize
, arg
);
646 return nbd_size_set(nbd
, bdev
, arg
, bsize
);
650 return nbd_size_set(nbd
, bdev
, nbd
->blksize
,
653 case NBD_SET_SIZE_BLOCKS
:
654 return nbd_size_set(nbd
, bdev
, nbd
->blksize
, arg
);
656 case NBD_SET_TIMEOUT
:
657 nbd
->tag_set
.timeout
= arg
* HZ
;
672 /* We have to claim the device under the lock */
673 nbd
->task_recv
= current
;
674 mutex_unlock(&nbd
->tx_lock
);
676 nbd_parse_flags(nbd
, bdev
);
678 nbd_dev_dbg_init(nbd
);
679 error
= nbd_thread_recv(nbd
, bdev
);
680 nbd_dev_dbg_close(nbd
);
682 mutex_lock(&nbd
->tx_lock
);
683 nbd
->task_recv
= NULL
;
688 nbd_bdev_reset(bdev
);
690 /* user requested, ignore socket errors */
691 if (test_bit(NBD_DISCONNECT_REQUESTED
, &nbd
->runtime_flags
))
693 if (test_bit(NBD_TIMEDOUT
, &nbd
->runtime_flags
))
703 * This is for compatibility only. The queue is always cleared
704 * by NBD_DO_IT or NBD_CLEAR_SOCK.
708 case NBD_PRINT_DEBUG
:
710 * For compatibility only, we no longer keep a list of
711 * outstanding requests.
718 static int nbd_ioctl(struct block_device
*bdev
, fmode_t mode
,
719 unsigned int cmd
, unsigned long arg
)
721 struct nbd_device
*nbd
= bdev
->bd_disk
->private_data
;
724 if (!capable(CAP_SYS_ADMIN
))
727 BUG_ON(nbd
->magic
!= NBD_MAGIC
);
729 mutex_lock(&nbd
->tx_lock
);
730 error
= __nbd_ioctl(bdev
, nbd
, cmd
, arg
);
731 mutex_unlock(&nbd
->tx_lock
);
736 static const struct block_device_operations nbd_fops
=
738 .owner
= THIS_MODULE
,
740 .compat_ioctl
= nbd_ioctl
,
743 #if IS_ENABLED(CONFIG_DEBUG_FS)
745 static int nbd_dbg_tasks_show(struct seq_file
*s
, void *unused
)
747 struct nbd_device
*nbd
= s
->private;
750 seq_printf(s
, "recv: %d\n", task_pid_nr(nbd
->task_recv
));
752 seq_printf(s
, "send: %d\n", task_pid_nr(nbd
->task_send
));
757 static int nbd_dbg_tasks_open(struct inode
*inode
, struct file
*file
)
759 return single_open(file
, nbd_dbg_tasks_show
, inode
->i_private
);
762 static const struct file_operations nbd_dbg_tasks_ops
= {
763 .open
= nbd_dbg_tasks_open
,
766 .release
= single_release
,
769 static int nbd_dbg_flags_show(struct seq_file
*s
, void *unused
)
771 struct nbd_device
*nbd
= s
->private;
772 u32 flags
= nbd
->flags
;
774 seq_printf(s
, "Hex: 0x%08x\n\n", flags
);
776 seq_puts(s
, "Known flags:\n");
778 if (flags
& NBD_FLAG_HAS_FLAGS
)
779 seq_puts(s
, "NBD_FLAG_HAS_FLAGS\n");
780 if (flags
& NBD_FLAG_READ_ONLY
)
781 seq_puts(s
, "NBD_FLAG_READ_ONLY\n");
782 if (flags
& NBD_FLAG_SEND_FLUSH
)
783 seq_puts(s
, "NBD_FLAG_SEND_FLUSH\n");
784 if (flags
& NBD_FLAG_SEND_TRIM
)
785 seq_puts(s
, "NBD_FLAG_SEND_TRIM\n");
790 static int nbd_dbg_flags_open(struct inode
*inode
, struct file
*file
)
792 return single_open(file
, nbd_dbg_flags_show
, inode
->i_private
);
795 static const struct file_operations nbd_dbg_flags_ops
= {
796 .open
= nbd_dbg_flags_open
,
799 .release
= single_release
,
802 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
809 dir
= debugfs_create_dir(nbd_name(nbd
), nbd_dbg_dir
);
811 dev_err(nbd_to_dev(nbd
), "Failed to create debugfs dir for '%s'\n",
817 debugfs_create_file("tasks", 0444, dir
, nbd
, &nbd_dbg_tasks_ops
);
818 debugfs_create_u64("size_bytes", 0444, dir
, &nbd
->bytesize
);
819 debugfs_create_u32("timeout", 0444, dir
, &nbd
->tag_set
.timeout
);
820 debugfs_create_u32("blocksize", 0444, dir
, &nbd
->blksize
);
821 debugfs_create_file("flags", 0444, dir
, nbd
, &nbd_dbg_flags_ops
);
826 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
828 debugfs_remove_recursive(nbd
->dbg_dir
);
831 static int nbd_dbg_init(void)
833 struct dentry
*dbg_dir
;
835 dbg_dir
= debugfs_create_dir("nbd", NULL
);
839 nbd_dbg_dir
= dbg_dir
;
844 static void nbd_dbg_close(void)
846 debugfs_remove_recursive(nbd_dbg_dir
);
849 #else /* IS_ENABLED(CONFIG_DEBUG_FS) */
851 static int nbd_dev_dbg_init(struct nbd_device
*nbd
)
856 static void nbd_dev_dbg_close(struct nbd_device
*nbd
)
860 static int nbd_dbg_init(void)
865 static void nbd_dbg_close(void)
871 static int nbd_init_request(void *data
, struct request
*rq
,
872 unsigned int hctx_idx
, unsigned int request_idx
,
873 unsigned int numa_node
)
875 struct nbd_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
878 INIT_LIST_HEAD(&cmd
->list
);
882 static struct blk_mq_ops nbd_mq_ops
= {
883 .queue_rq
= nbd_queue_rq
,
884 .init_request
= nbd_init_request
,
885 .timeout
= nbd_xmit_timeout
,
889 * And here should be modules and kernel interface
890 * (Just smiley confuses emacs :-)
893 static int __init
nbd_init(void)
899 BUILD_BUG_ON(sizeof(struct nbd_request
) != 28);
902 printk(KERN_ERR
"nbd: max_part must be >= 0\n");
908 part_shift
= fls(max_part
);
911 * Adjust max_part according to part_shift as it is exported
912 * to user space so that user can know the max number of
913 * partition kernel should be able to manage.
915 * Note that -1 is required because partition 0 is reserved
916 * for the whole disk.
918 max_part
= (1UL << part_shift
) - 1;
921 if ((1UL << part_shift
) > DISK_MAX_PARTS
)
924 if (nbds_max
> 1UL << (MINORBITS
- part_shift
))
927 nbd_dev
= kcalloc(nbds_max
, sizeof(*nbd_dev
), GFP_KERNEL
);
931 for (i
= 0; i
< nbds_max
; i
++) {
932 struct gendisk
*disk
= alloc_disk(1 << part_shift
);
935 nbd_dev
[i
].disk
= disk
;
937 nbd_dev
[i
].tag_set
.ops
= &nbd_mq_ops
;
938 nbd_dev
[i
].tag_set
.nr_hw_queues
= 1;
939 nbd_dev
[i
].tag_set
.queue_depth
= 128;
940 nbd_dev
[i
].tag_set
.numa_node
= NUMA_NO_NODE
;
941 nbd_dev
[i
].tag_set
.cmd_size
= sizeof(struct nbd_cmd
);
942 nbd_dev
[i
].tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
|
943 BLK_MQ_F_SG_MERGE
| BLK_MQ_F_BLOCKING
;
944 nbd_dev
[i
].tag_set
.driver_data
= &nbd_dev
[i
];
946 err
= blk_mq_alloc_tag_set(&nbd_dev
[i
].tag_set
);
953 * The new linux 2.5 block layer implementation requires
954 * every gendisk to have its very own request_queue struct.
955 * These structs are big so we dynamically allocate them.
957 disk
->queue
= blk_mq_init_queue(&nbd_dev
[i
].tag_set
);
959 blk_mq_free_tag_set(&nbd_dev
[i
].tag_set
);
965 * Tell the block layer that we are not a rotational device
967 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, disk
->queue
);
968 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, disk
->queue
);
969 disk
->queue
->limits
.discard_granularity
= 512;
970 blk_queue_max_discard_sectors(disk
->queue
, UINT_MAX
);
971 disk
->queue
->limits
.discard_zeroes_data
= 0;
972 blk_queue_max_hw_sectors(disk
->queue
, 65536);
973 disk
->queue
->limits
.max_sectors
= 256;
976 if (register_blkdev(NBD_MAJOR
, "nbd")) {
981 printk(KERN_INFO
"nbd: registered device at major %d\n", NBD_MAJOR
);
985 for (i
= 0; i
< nbds_max
; i
++) {
986 struct gendisk
*disk
= nbd_dev
[i
].disk
;
987 nbd_dev
[i
].magic
= NBD_MAGIC
;
988 spin_lock_init(&nbd_dev
[i
].sock_lock
);
989 mutex_init(&nbd_dev
[i
].tx_lock
);
990 disk
->major
= NBD_MAJOR
;
991 disk
->first_minor
= i
<< part_shift
;
992 disk
->fops
= &nbd_fops
;
993 disk
->private_data
= &nbd_dev
[i
];
994 sprintf(disk
->disk_name
, "nbd%d", i
);
995 nbd_reset(&nbd_dev
[i
]);
1002 blk_mq_free_tag_set(&nbd_dev
[i
].tag_set
);
1003 blk_cleanup_queue(nbd_dev
[i
].disk
->queue
);
1004 put_disk(nbd_dev
[i
].disk
);
1010 static void __exit
nbd_cleanup(void)
1016 for (i
= 0; i
< nbds_max
; i
++) {
1017 struct gendisk
*disk
= nbd_dev
[i
].disk
;
1018 nbd_dev
[i
].magic
= 0;
1021 blk_cleanup_queue(disk
->queue
);
1022 blk_mq_free_tag_set(&nbd_dev
[i
].tag_set
);
1026 unregister_blkdev(NBD_MAJOR
, "nbd");
1028 printk(KERN_INFO
"nbd: unregistered device at major %d\n", NBD_MAJOR
);
1031 module_init(nbd_init
);
1032 module_exit(nbd_cleanup
);
1034 MODULE_DESCRIPTION("Network Block Device");
1035 MODULE_LICENSE("GPL");
1037 module_param(nbds_max
, int, 0444);
1038 MODULE_PARM_DESC(nbds_max
, "number of network block devices to initialize (default: 16)");
1039 module_param(max_part
, int, 0444);
1040 MODULE_PARM_DESC(max_part
, "number of partitions per device (default: 0)");