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>
38 #include <asm/uaccess.h>
39 #include <asm/types.h>
41 #include <linux/nbd.h>
45 struct socket * sock; /* If == NULL, device is not ready, yet */
48 spinlock_t queue_lock;
49 struct list_head queue_head; /* Requests waiting result */
50 struct request *active_req;
51 wait_queue_head_t active_wq;
52 struct list_head waiting_queue; /* Requests to be sent */
53 wait_queue_head_t waiting_wq;
60 bool disconnect; /* a disconnect has been requested by user */
62 struct timer_list timeout_timer;
63 struct task_struct *task_recv;
64 struct task_struct *task_send;
66 #if IS_ENABLED(CONFIG_DEBUG_FS)
67 struct dentry *dbg_dir;
71 #if IS_ENABLED(CONFIG_DEBUG_FS)
72 static struct dentry *nbd_dbg_dir;
75 #define nbd_name(nbd) ((nbd)->disk->disk_name)
77 #define NBD_MAGIC 0x68797548
79 static unsigned int nbds_max = 16;
80 static struct nbd_device *nbd_dev;
84 * Use just one lock (or at most 1 per NIC). Two arguments for this:
85 * 1. Each NIC is essentially a synchronization point for all servers
86 * accessed through that NIC so there's no need to have more locks
88 * 2. More locks lead to more "Dirty cache line bouncing" which will slow
89 * down each lock to the point where they're actually slower than just
91 * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this!
93 static DEFINE_SPINLOCK(nbd_lock);
95 static inline struct device *nbd_to_dev(struct nbd_device *nbd)
97 return disk_to_dev(nbd->disk);
100 static const char *nbdcmd_to_ascii(int cmd)
103 case NBD_CMD_READ: return "read";
104 case NBD_CMD_WRITE: return "write";
105 case NBD_CMD_DISC: return "disconnect";
106 case NBD_CMD_FLUSH: return "flush";
107 case NBD_CMD_TRIM: return "trim/discard";
112 static void nbd_end_request(struct nbd_device *nbd, struct request *req)
114 int error = req->errors ? -EIO : 0;
115 struct request_queue *q = req->q;
118 dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", req,
119 error ? "failed" : "done");
121 spin_lock_irqsave(q->queue_lock, flags);
122 __blk_end_request_all(req, error);
123 spin_unlock_irqrestore(q->queue_lock, flags);
127 * Forcibly shutdown the socket causing all listeners to error
129 static void sock_shutdown(struct nbd_device *nbd)
134 dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n");
135 kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
137 del_timer_sync(&nbd->timeout_timer);
140 static void nbd_xmit_timeout(unsigned long arg)
142 struct nbd_device *nbd = (struct nbd_device *)arg;
143 struct task_struct *task;
145 if (list_empty(&nbd->queue_head))
148 nbd->disconnect = true;
150 task = READ_ONCE(nbd->task_recv);
152 force_sig(SIGKILL, task);
154 task = READ_ONCE(nbd->task_send);
156 force_sig(SIGKILL, nbd->task_send);
158 dev_err(nbd_to_dev(nbd), "Connection timed out, killed receiver and sender, shutting down connection\n");
162 * Send or receive packet.
164 static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size,
167 struct socket *sock = nbd->sock;
171 sigset_t blocked, oldset;
172 unsigned long pflags = current->flags;
174 if (unlikely(!sock)) {
175 dev_err(disk_to_dev(nbd->disk),
176 "Attempted %s on closed socket in sock_xmit\n",
177 (send ? "send" : "recv"));
181 /* Allow interception of SIGKILL only
182 * Don't allow other signals to interrupt the transmission */
183 siginitsetinv(&blocked, sigmask(SIGKILL));
184 sigprocmask(SIG_SETMASK, &blocked, &oldset);
186 current->flags |= PF_MEMALLOC;
188 sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC;
193 msg.msg_control = NULL;
194 msg.msg_controllen = 0;
195 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
198 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
200 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
205 result = -EPIPE; /* short read */
212 sigprocmask(SIG_SETMASK, &oldset, NULL);
213 tsk_restore_flags(current, pflags, PF_MEMALLOC);
215 if (!send && nbd->xmit_timeout)
216 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
221 static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec,
225 void *kaddr = kmap(bvec->bv_page);
226 result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset,
227 bvec->bv_len, flags);
228 kunmap(bvec->bv_page);
232 /* always call with the tx_lock held */
233 static int nbd_send_req(struct nbd_device *nbd, struct request *req)
236 struct nbd_request request;
237 unsigned long size = blk_rq_bytes(req);
240 if (req->cmd_type == REQ_TYPE_DRV_PRIV)
242 else if (req->cmd_flags & REQ_DISCARD)
244 else if (req->cmd_flags & REQ_FLUSH)
245 type = NBD_CMD_FLUSH;
246 else if (rq_data_dir(req) == WRITE)
247 type = NBD_CMD_WRITE;
251 memset(&request, 0, sizeof(request));
252 request.magic = htonl(NBD_REQUEST_MAGIC);
253 request.type = htonl(type);
254 if (type != NBD_CMD_FLUSH && type != NBD_CMD_DISC) {
255 request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
256 request.len = htonl(size);
258 memcpy(request.handle, &req, sizeof(req));
260 dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
261 req, nbdcmd_to_ascii(type),
262 (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
263 result = sock_xmit(nbd, 1, &request, sizeof(request),
264 (type == NBD_CMD_WRITE) ? MSG_MORE : 0);
266 dev_err(disk_to_dev(nbd->disk),
267 "Send control failed (result %d)\n", result);
271 if (type == NBD_CMD_WRITE) {
272 struct req_iterator iter;
275 * we are really probing at internals to determine
276 * whether to set MSG_MORE or not...
278 rq_for_each_segment(bvec, req, iter) {
280 if (!rq_iter_last(bvec, iter))
282 dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
284 result = sock_send_bvec(nbd, &bvec, flags);
286 dev_err(disk_to_dev(nbd->disk),
287 "Send data failed (result %d)\n",
296 static struct request *nbd_find_request(struct nbd_device *nbd,
297 struct request *xreq)
299 struct request *req, *tmp;
302 err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq);
306 spin_lock(&nbd->queue_lock);
307 list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) {
310 list_del_init(&req->queuelist);
311 spin_unlock(&nbd->queue_lock);
314 spin_unlock(&nbd->queue_lock);
316 return ERR_PTR(-ENOENT);
319 static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec)
322 void *kaddr = kmap(bvec->bv_page);
323 result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len,
325 kunmap(bvec->bv_page);
329 /* NULL returned = something went wrong, inform userspace */
330 static struct request *nbd_read_stat(struct nbd_device *nbd)
333 struct nbd_reply reply;
337 result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL);
339 dev_err(disk_to_dev(nbd->disk),
340 "Receive control failed (result %d)\n", result);
341 return ERR_PTR(result);
344 if (ntohl(reply.magic) != NBD_REPLY_MAGIC) {
345 dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n",
346 (unsigned long)ntohl(reply.magic));
347 return ERR_PTR(-EPROTO);
350 req = nbd_find_request(nbd, *(struct request **)reply.handle);
352 result = PTR_ERR(req);
353 if (result != -ENOENT)
354 return ERR_PTR(result);
356 dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n",
358 return ERR_PTR(-EBADR);
361 if (ntohl(reply.error)) {
362 dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
368 dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req);
369 if (rq_data_dir(req) != WRITE) {
370 struct req_iterator iter;
373 rq_for_each_segment(bvec, req, iter) {
374 result = sock_recv_bvec(nbd, &bvec);
376 dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
381 dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
388 static ssize_t pid_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct gendisk *disk = dev_to_disk(dev);
392 struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
394 return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv));
397 static struct device_attribute pid_attr = {
398 .attr = { .name = "pid", .mode = S_IRUGO},
402 static int nbd_thread_recv(struct nbd_device *nbd)
407 BUG_ON(nbd->magic != NBD_MAGIC);
409 sk_set_memalloc(nbd->sock->sk);
411 nbd->task_recv = current;
413 ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
415 dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
416 nbd->task_recv = NULL;
421 req = nbd_read_stat(nbd);
427 nbd_end_request(nbd, req);
430 device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
432 nbd->task_recv = NULL;
434 if (signal_pending(current)) {
437 ret = dequeue_signal_lock(current, ¤t->blocked, &info);
438 dev_warn(nbd_to_dev(nbd), "pid %d, %s, got signal %d\n",
439 task_pid_nr(current), current->comm, ret);
440 mutex_lock(&nbd->tx_lock);
442 mutex_unlock(&nbd->tx_lock);
449 static void nbd_clear_que(struct nbd_device *nbd)
453 BUG_ON(nbd->magic != NBD_MAGIC);
456 * Because we have set nbd->sock to NULL under the tx_lock, all
457 * modifications to the list must have completed by now. For
458 * the same reason, the active_req must be NULL.
460 * As a consequence, we don't need to take the spin lock while
461 * purging the list here.
464 BUG_ON(nbd->active_req);
466 while (!list_empty(&nbd->queue_head)) {
467 req = list_entry(nbd->queue_head.next, struct request,
469 list_del_init(&req->queuelist);
471 nbd_end_request(nbd, req);
474 while (!list_empty(&nbd->waiting_queue)) {
475 req = list_entry(nbd->waiting_queue.next, struct request,
477 list_del_init(&req->queuelist);
479 nbd_end_request(nbd, req);
481 dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n");
485 static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
487 if (req->cmd_type != REQ_TYPE_FS)
490 if (rq_data_dir(req) == WRITE &&
491 (nbd->flags & NBD_FLAG_READ_ONLY)) {
492 dev_err(disk_to_dev(nbd->disk),
493 "Write on read-only\n");
499 mutex_lock(&nbd->tx_lock);
500 if (unlikely(!nbd->sock)) {
501 mutex_unlock(&nbd->tx_lock);
502 dev_err(disk_to_dev(nbd->disk),
503 "Attempted send on closed socket\n");
507 nbd->active_req = req;
509 if (nbd->xmit_timeout && list_empty_careful(&nbd->queue_head))
510 mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout);
512 if (nbd_send_req(nbd, req) != 0) {
513 dev_err(disk_to_dev(nbd->disk), "Request send failed\n");
515 nbd_end_request(nbd, req);
517 spin_lock(&nbd->queue_lock);
518 list_add_tail(&req->queuelist, &nbd->queue_head);
519 spin_unlock(&nbd->queue_lock);
522 nbd->active_req = NULL;
523 mutex_unlock(&nbd->tx_lock);
524 wake_up_all(&nbd->active_wq);
530 nbd_end_request(nbd, req);
533 static int nbd_thread_send(void *data)
535 struct nbd_device *nbd = data;
538 nbd->task_send = current;
540 set_user_nice(current, MIN_NICE);
541 while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) {
542 /* wait for something to do */
543 wait_event_interruptible(nbd->waiting_wq,
544 kthread_should_stop() ||
545 !list_empty(&nbd->waiting_queue));
547 if (signal_pending(current)) {
551 ret = dequeue_signal_lock(current, ¤t->blocked,
553 dev_warn(nbd_to_dev(nbd), "pid %d, %s, got signal %d\n",
554 task_pid_nr(current), current->comm, ret);
555 mutex_lock(&nbd->tx_lock);
557 mutex_unlock(&nbd->tx_lock);
561 /* extract request */
562 if (list_empty(&nbd->waiting_queue))
565 spin_lock_irq(&nbd->queue_lock);
566 req = list_entry(nbd->waiting_queue.next, struct request,
568 list_del_init(&req->queuelist);
569 spin_unlock_irq(&nbd->queue_lock);
572 nbd_handle_req(nbd, req);
575 nbd->task_send = NULL;
581 * We always wait for result of write, for now. It would be nice to make it optional
583 * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK))
584 * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
587 static void nbd_request_handler(struct request_queue *q)
588 __releases(q->queue_lock) __acquires(q->queue_lock)
592 while ((req = blk_fetch_request(q)) != NULL) {
593 struct nbd_device *nbd;
595 spin_unlock_irq(q->queue_lock);
597 nbd = req->rq_disk->private_data;
599 BUG_ON(nbd->magic != NBD_MAGIC);
601 dev_dbg(nbd_to_dev(nbd), "request %p: dequeued (flags=%x)\n",
604 if (unlikely(!nbd->sock)) {
605 dev_err(disk_to_dev(nbd->disk),
606 "Attempted send on closed socket\n");
608 nbd_end_request(nbd, req);
609 spin_lock_irq(q->queue_lock);
613 spin_lock_irq(&nbd->queue_lock);
614 list_add_tail(&req->queuelist, &nbd->waiting_queue);
615 spin_unlock_irq(&nbd->queue_lock);
617 wake_up(&nbd->waiting_wq);
619 spin_lock_irq(q->queue_lock);
623 static int nbd_dev_dbg_init(struct nbd_device *nbd);
624 static void nbd_dev_dbg_close(struct nbd_device *nbd);
626 /* Must be called with tx_lock held */
628 static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
629 unsigned int cmd, unsigned long arg)
632 case NBD_DISCONNECT: {
635 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
639 mutex_unlock(&nbd->tx_lock);
641 mutex_lock(&nbd->tx_lock);
642 blk_rq_init(NULL, &sreq);
643 sreq.cmd_type = REQ_TYPE_DRV_PRIV;
645 /* Check again after getting mutex back. */
649 nbd->disconnect = true;
651 nbd_send_req(nbd, &sreq);
655 case NBD_CLEAR_SOCK: {
656 struct socket *sock = nbd->sock;
659 BUG_ON(!list_empty(&nbd->queue_head));
660 BUG_ON(!list_empty(&nbd->waiting_queue));
672 sock = sockfd_lookup(arg, &err);
676 bdev->bd_invalidated = 1;
677 nbd->disconnect = false; /* we're connected now */
683 case NBD_SET_BLKSIZE:
685 nbd->bytesize &= ~(nbd->blksize-1);
686 bdev->bd_inode->i_size = nbd->bytesize;
687 set_blocksize(bdev, nbd->blksize);
688 set_capacity(nbd->disk, nbd->bytesize >> 9);
692 nbd->bytesize = arg & ~(nbd->blksize-1);
693 bdev->bd_inode->i_size = nbd->bytesize;
694 set_blocksize(bdev, nbd->blksize);
695 set_capacity(nbd->disk, nbd->bytesize >> 9);
698 case NBD_SET_TIMEOUT:
699 nbd->xmit_timeout = arg * HZ;
701 mod_timer(&nbd->timeout_timer,
702 jiffies + nbd->xmit_timeout);
704 del_timer_sync(&nbd->timeout_timer);
712 case NBD_SET_SIZE_BLOCKS:
713 nbd->bytesize = ((u64) arg) * nbd->blksize;
714 bdev->bd_inode->i_size = nbd->bytesize;
715 set_blocksize(bdev, nbd->blksize);
716 set_capacity(nbd->disk, nbd->bytesize >> 9);
720 struct task_struct *thread;
729 mutex_unlock(&nbd->tx_lock);
731 if (nbd->flags & NBD_FLAG_READ_ONLY)
732 set_device_ro(bdev, true);
733 if (nbd->flags & NBD_FLAG_SEND_TRIM)
734 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
736 if (nbd->flags & NBD_FLAG_SEND_FLUSH)
737 blk_queue_flush(nbd->disk->queue, REQ_FLUSH);
739 blk_queue_flush(nbd->disk->queue, 0);
741 thread = kthread_run(nbd_thread_send, nbd, "%s",
743 if (IS_ERR(thread)) {
744 mutex_lock(&nbd->tx_lock);
745 return PTR_ERR(thread);
748 nbd_dev_dbg_init(nbd);
749 error = nbd_thread_recv(nbd);
750 nbd_dev_dbg_close(nbd);
751 kthread_stop(thread);
753 mutex_lock(&nbd->tx_lock);
760 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
761 set_device_ro(bdev, false);
766 bdev->bd_inode->i_size = 0;
767 set_capacity(nbd->disk, 0);
769 blkdev_reread_part(bdev);
770 if (nbd->disconnect) /* user requested, ignore socket errors */
777 * This is for compatibility only. The queue is always cleared
778 * by NBD_DO_IT or NBD_CLEAR_SOCK.
782 case NBD_PRINT_DEBUG:
783 dev_info(disk_to_dev(nbd->disk),
784 "next = %p, prev = %p, head = %p\n",
785 nbd->queue_head.next, nbd->queue_head.prev,
792 static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
793 unsigned int cmd, unsigned long arg)
795 struct nbd_device *nbd = bdev->bd_disk->private_data;
798 if (!capable(CAP_SYS_ADMIN))
801 BUG_ON(nbd->magic != NBD_MAGIC);
803 mutex_lock(&nbd->tx_lock);
804 error = __nbd_ioctl(bdev, nbd, cmd, arg);
805 mutex_unlock(&nbd->tx_lock);
810 static const struct block_device_operations nbd_fops =
812 .owner = THIS_MODULE,
816 #if IS_ENABLED(CONFIG_DEBUG_FS)
818 static int nbd_dbg_tasks_show(struct seq_file *s, void *unused)
820 struct nbd_device *nbd = s->private;
823 seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv));
825 seq_printf(s, "send: %d\n", task_pid_nr(nbd->task_send));
830 static int nbd_dbg_tasks_open(struct inode *inode, struct file *file)
832 return single_open(file, nbd_dbg_tasks_show, inode->i_private);
835 static const struct file_operations nbd_dbg_tasks_ops = {
836 .open = nbd_dbg_tasks_open,
839 .release = single_release,
842 static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
844 struct nbd_device *nbd = s->private;
845 u32 flags = nbd->flags;
847 seq_printf(s, "Hex: 0x%08x\n\n", flags);
849 seq_puts(s, "Known flags:\n");
851 if (flags & NBD_FLAG_HAS_FLAGS)
852 seq_puts(s, "NBD_FLAG_HAS_FLAGS\n");
853 if (flags & NBD_FLAG_READ_ONLY)
854 seq_puts(s, "NBD_FLAG_READ_ONLY\n");
855 if (flags & NBD_FLAG_SEND_FLUSH)
856 seq_puts(s, "NBD_FLAG_SEND_FLUSH\n");
857 if (flags & NBD_FLAG_SEND_TRIM)
858 seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
863 static int nbd_dbg_flags_open(struct inode *inode, struct file *file)
865 return single_open(file, nbd_dbg_flags_show, inode->i_private);
868 static const struct file_operations nbd_dbg_flags_ops = {
869 .open = nbd_dbg_flags_open,
872 .release = single_release,
875 static int nbd_dev_dbg_init(struct nbd_device *nbd)
880 dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir);
881 if (IS_ERR_OR_NULL(dir)) {
882 dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s' (%ld)\n",
883 nbd_name(nbd), PTR_ERR(dir));
888 f = debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops);
889 if (IS_ERR_OR_NULL(f)) {
890 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'tasks', %ld\n",
895 f = debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize);
896 if (IS_ERR_OR_NULL(f)) {
897 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'size_bytes', %ld\n",
902 f = debugfs_create_u32("timeout", 0444, dir, &nbd->xmit_timeout);
903 if (IS_ERR_OR_NULL(f)) {
904 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'timeout', %ld\n",
909 f = debugfs_create_u32("blocksize", 0444, dir, &nbd->blksize);
910 if (IS_ERR_OR_NULL(f)) {
911 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'blocksize', %ld\n",
916 f = debugfs_create_file("flags", 0444, dir, &nbd, &nbd_dbg_flags_ops);
917 if (IS_ERR_OR_NULL(f)) {
918 dev_err(nbd_to_dev(nbd), "Failed to create debugfs file 'flags', %ld\n",
926 static void nbd_dev_dbg_close(struct nbd_device *nbd)
928 debugfs_remove_recursive(nbd->dbg_dir);
931 static int nbd_dbg_init(void)
933 struct dentry *dbg_dir;
935 dbg_dir = debugfs_create_dir("nbd", NULL);
937 return PTR_ERR(dbg_dir);
939 nbd_dbg_dir = dbg_dir;
944 static void nbd_dbg_close(void)
946 debugfs_remove_recursive(nbd_dbg_dir);
949 #else /* IS_ENABLED(CONFIG_DEBUG_FS) */
951 static int nbd_dev_dbg_init(struct nbd_device *nbd)
956 static void nbd_dev_dbg_close(struct nbd_device *nbd)
960 static int nbd_dbg_init(void)
965 static void nbd_dbg_close(void)
972 * And here should be modules and kernel interface
973 * (Just smiley confuses emacs :-)
976 static int __init nbd_init(void)
982 BUILD_BUG_ON(sizeof(struct nbd_request) != 28);
985 printk(KERN_ERR "nbd: max_part must be >= 0\n");
991 part_shift = fls(max_part);
994 * Adjust max_part according to part_shift as it is exported
995 * to user space so that user can know the max number of
996 * partition kernel should be able to manage.
998 * Note that -1 is required because partition 0 is reserved
999 * for the whole disk.
1001 max_part = (1UL << part_shift) - 1;
1004 if ((1UL << part_shift) > DISK_MAX_PARTS)
1007 if (nbds_max > 1UL << (MINORBITS - part_shift))
1010 nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL);
1014 for (i = 0; i < nbds_max; i++) {
1015 struct gendisk *disk = alloc_disk(1 << part_shift);
1018 nbd_dev[i].disk = disk;
1020 * The new linux 2.5 block layer implementation requires
1021 * every gendisk to have its very own request_queue struct.
1022 * These structs are big so we dynamically allocate them.
1024 disk->queue = blk_init_queue(nbd_request_handler, &nbd_lock);
1030 * Tell the block layer that we are not a rotational device
1032 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
1033 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue);
1034 disk->queue->limits.discard_granularity = 512;
1035 blk_queue_max_discard_sectors(disk->queue, UINT_MAX);
1036 disk->queue->limits.discard_zeroes_data = 0;
1037 blk_queue_max_hw_sectors(disk->queue, 65536);
1038 disk->queue->limits.max_sectors = 256;
1041 if (register_blkdev(NBD_MAJOR, "nbd")) {
1046 printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR);
1050 for (i = 0; i < nbds_max; i++) {
1051 struct gendisk *disk = nbd_dev[i].disk;
1052 nbd_dev[i].magic = NBD_MAGIC;
1053 INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
1054 spin_lock_init(&nbd_dev[i].queue_lock);
1055 INIT_LIST_HEAD(&nbd_dev[i].queue_head);
1056 mutex_init(&nbd_dev[i].tx_lock);
1057 init_timer(&nbd_dev[i].timeout_timer);
1058 nbd_dev[i].timeout_timer.function = nbd_xmit_timeout;
1059 nbd_dev[i].timeout_timer.data = (unsigned long)&nbd_dev[i];
1060 init_waitqueue_head(&nbd_dev[i].active_wq);
1061 init_waitqueue_head(&nbd_dev[i].waiting_wq);
1062 nbd_dev[i].blksize = 1024;
1063 nbd_dev[i].bytesize = 0;
1064 disk->major = NBD_MAJOR;
1065 disk->first_minor = i << part_shift;
1066 disk->fops = &nbd_fops;
1067 disk->private_data = &nbd_dev[i];
1068 sprintf(disk->disk_name, "nbd%d", i);
1069 set_capacity(disk, 0);
1076 blk_cleanup_queue(nbd_dev[i].disk->queue);
1077 put_disk(nbd_dev[i].disk);
1083 static void __exit nbd_cleanup(void)
1089 for (i = 0; i < nbds_max; i++) {
1090 struct gendisk *disk = nbd_dev[i].disk;
1091 nbd_dev[i].magic = 0;
1094 blk_cleanup_queue(disk->queue);
1098 unregister_blkdev(NBD_MAJOR, "nbd");
1100 printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);
1103 module_init(nbd_init);
1104 module_exit(nbd_cleanup);
1106 MODULE_DESCRIPTION("Network Block Device");
1107 MODULE_LICENSE("GPL");
1109 module_param(nbds_max, int, 0444);
1110 MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)");
1111 module_param(max_part, int, 0444);
1112 MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)");