Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Network block device - make block devices work over TCP |
| 3 | * |
| 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. |
| 6 | * |
Pavel Machek | a253129 | 2010-07-18 14:27:13 +0200 | [diff] [blame] | 7 | * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com> |
| 9 | * |
Pavel Machek | dbf492d | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 10 | * This file is released under GPLv2 or later. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Pavel Machek | dbf492d | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 12 | * (part of code stolen from loop.c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/major.h> |
| 16 | |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/fs.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> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 28 | #include <linux/compiler.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/sock.h> |
Trond Myklebust | 91cf45f | 2007-11-12 18:10:39 -0800 | [diff] [blame] | 33 | #include <linux/net.h> |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 34 | #include <linux/kthread.h> |
Markus Pargmann | b9c495b | 2015-04-02 10:11:37 +0200 | [diff] [blame] | 35 | #include <linux/types.h> |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 36 | #include <linux/debugfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/uaccess.h> |
| 39 | #include <asm/types.h> |
| 40 | |
| 41 | #include <linux/nbd.h> |
| 42 | |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 43 | struct nbd_device { |
Markus Pargmann | 22d109c | 2015-08-17 08:20:09 +0200 | [diff] [blame] | 44 | u32 flags; |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 45 | struct socket * sock; /* If == NULL, device is not ready, yet */ |
| 46 | int magic; |
| 47 | |
| 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; |
| 54 | |
| 55 | struct mutex tx_lock; |
| 56 | struct gendisk *disk; |
| 57 | int blksize; |
Markus Pargmann | b9c495b | 2015-04-02 10:11:37 +0200 | [diff] [blame] | 58 | loff_t bytesize; |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 59 | int xmit_timeout; |
Markus Pargmann | 1f7b5cf | 2015-10-29 12:01:34 +0100 | [diff] [blame] | 60 | bool timedout; |
Markus Pargmann | 696697c | 2015-08-17 08:20:07 +0200 | [diff] [blame] | 61 | bool disconnect; /* a disconnect has been requested by user */ |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 62 | |
| 63 | struct timer_list timeout_timer; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 64 | /* protects initialization and shutdown of the socket */ |
| 65 | spinlock_t sock_lock; |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 66 | struct task_struct *task_recv; |
| 67 | struct task_struct *task_send; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 68 | |
| 69 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 70 | struct dentry *dbg_dir; |
| 71 | #endif |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 74 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 75 | static struct dentry *nbd_dbg_dir; |
| 76 | #endif |
| 77 | |
| 78 | #define nbd_name(nbd) ((nbd)->disk->disk_name) |
| 79 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 80 | #define NBD_MAGIC 0x68797548 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Ingo van Lil | 9c7a416 | 2006-07-01 04:36:36 -0700 | [diff] [blame] | 82 | static unsigned int nbds_max = 16; |
Paul Clements | 20a8143 | 2008-02-08 04:21:51 -0800 | [diff] [blame] | 83 | static struct nbd_device *nbd_dev; |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 84 | static int max_part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * Use just one lock (or at most 1 per NIC). Two arguments for this: |
| 88 | * 1. Each NIC is essentially a synchronization point for all servers |
| 89 | * accessed through that NIC so there's no need to have more locks |
| 90 | * than NICs anyway. |
| 91 | * 2. More locks lead to more "Dirty cache line bouncing" which will slow |
| 92 | * down each lock to the point where they're actually slower than just |
| 93 | * a single lock. |
| 94 | * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this! |
| 95 | */ |
| 96 | static DEFINE_SPINLOCK(nbd_lock); |
| 97 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 98 | static inline struct device *nbd_to_dev(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 100 | return disk_to_dev(nbd->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 103 | static bool nbd_is_connected(struct nbd_device *nbd) |
| 104 | { |
| 105 | return !!nbd->task_recv; |
| 106 | } |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | static const char *nbdcmd_to_ascii(int cmd) |
| 109 | { |
| 110 | switch (cmd) { |
| 111 | case NBD_CMD_READ: return "read"; |
| 112 | case NBD_CMD_WRITE: return "write"; |
| 113 | case NBD_CMD_DISC: return "disconnect"; |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 114 | case NBD_CMD_FLUSH: return "flush"; |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 115 | case NBD_CMD_TRIM: return "trim/discard"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | return "invalid"; |
| 118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 120 | static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev) |
| 121 | { |
| 122 | bdev->bd_inode->i_size = 0; |
| 123 | set_capacity(nbd->disk, 0); |
| 124 | kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev) |
| 130 | { |
| 131 | if (!nbd_is_connected(nbd)) |
| 132 | return; |
| 133 | |
| 134 | bdev->bd_inode->i_size = nbd->bytesize; |
| 135 | set_capacity(nbd->disk, nbd->bytesize >> 9); |
| 136 | kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); |
| 137 | } |
| 138 | |
| 139 | static int nbd_size_set(struct nbd_device *nbd, struct block_device *bdev, |
| 140 | int blocksize, int nr_blocks) |
| 141 | { |
| 142 | int ret; |
| 143 | |
| 144 | ret = set_blocksize(bdev, blocksize); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | |
| 148 | nbd->blksize = blocksize; |
| 149 | nbd->bytesize = (loff_t)blocksize * (loff_t)nr_blocks; |
| 150 | |
| 151 | nbd_size_update(nbd, bdev); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 156 | static void nbd_end_request(struct nbd_device *nbd, struct request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Kiyoshi Ueda | 097c94a | 2007-12-11 17:44:06 -0500 | [diff] [blame] | 158 | int error = req->errors ? -EIO : 0; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 159 | struct request_queue *q = req->q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | unsigned long flags; |
| 161 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 162 | dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", req, |
| 163 | error ? "failed" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | spin_lock_irqsave(q->queue_lock, flags); |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 166 | __blk_end_request_all(req, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 168 | } |
| 169 | |
Markus Pargmann | e018e75 | 2015-04-02 10:11:39 +0200 | [diff] [blame] | 170 | /* |
| 171 | * Forcibly shutdown the socket causing all listeners to error |
| 172 | */ |
Markus Pargmann | 36e47be | 2015-08-17 08:20:01 +0200 | [diff] [blame] | 173 | static void sock_shutdown(struct nbd_device *nbd) |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 174 | { |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 175 | spin_lock_irq(&nbd->sock_lock); |
| 176 | |
| 177 | if (!nbd->sock) { |
| 178 | spin_unlock_irq(&nbd->sock_lock); |
Markus Pargmann | 260bbce | 2015-08-17 08:20:02 +0200 | [diff] [blame] | 179 | return; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 180 | } |
Markus Pargmann | 260bbce | 2015-08-17 08:20:02 +0200 | [diff] [blame] | 181 | |
| 182 | dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n"); |
| 183 | kernel_sock_shutdown(nbd->sock, SHUT_RDWR); |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 184 | sockfd_put(nbd->sock); |
Markus Pargmann | 260bbce | 2015-08-17 08:20:02 +0200 | [diff] [blame] | 185 | nbd->sock = NULL; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 186 | spin_unlock_irq(&nbd->sock_lock); |
| 187 | |
| 188 | del_timer(&nbd->timeout_timer); |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void nbd_xmit_timeout(unsigned long arg) |
| 192 | { |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 193 | struct nbd_device *nbd = (struct nbd_device *)arg; |
Markus Pargmann | dcc909d | 2015-10-06 20:03:54 +0200 | [diff] [blame] | 194 | unsigned long flags; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 195 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 196 | if (list_empty(&nbd->queue_head)) |
| 197 | return; |
| 198 | |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 199 | spin_lock_irqsave(&nbd->sock_lock, flags); |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 200 | |
Markus Pargmann | 1f7b5cf | 2015-10-29 12:01:34 +0100 | [diff] [blame] | 201 | nbd->timedout = true; |
Markus Pargmann | dcc909d | 2015-10-06 20:03:54 +0200 | [diff] [blame] | 202 | |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 203 | if (nbd->sock) |
| 204 | kernel_sock_shutdown(nbd->sock, SHUT_RDWR); |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 205 | |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 206 | spin_unlock_irqrestore(&nbd->sock_lock, flags); |
Markus Pargmann | dcc909d | 2015-10-06 20:03:54 +0200 | [diff] [blame] | 207 | |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 208 | dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n"); |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | /* |
| 212 | * Send or receive packet. |
| 213 | */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 214 | static int sock_xmit(struct nbd_device *nbd, int send, void *buf, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | int msg_flags) |
| 216 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 217 | struct socket *sock = nbd->sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | int result; |
| 219 | struct msghdr msg; |
| 220 | struct kvec iov; |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 221 | unsigned long pflags = current->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 223 | if (unlikely(!sock)) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 224 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 225 | "Attempted %s on closed socket in sock_xmit\n", |
| 226 | (send ? "send" : "recv")); |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 227 | return -EINVAL; |
| 228 | } |
| 229 | |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 230 | current->flags |= PF_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | do { |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 232 | sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | iov.iov_base = buf; |
| 234 | iov.iov_len = size; |
| 235 | msg.msg_name = NULL; |
| 236 | msg.msg_namelen = 0; |
| 237 | msg.msg_control = NULL; |
| 238 | msg.msg_controllen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | msg.msg_flags = msg_flags | MSG_NOSIGNAL; |
| 240 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 241 | if (send) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | result = kernel_sendmsg(sock, &msg, &iov, 1, size); |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 243 | else |
Namhyung Kim | 35fbf5b | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 244 | result = kernel_recvmsg(sock, &msg, &iov, 1, size, |
| 245 | msg.msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | if (result <= 0) { |
| 248 | if (result == 0) |
| 249 | result = -EPIPE; /* short read */ |
| 250 | break; |
| 251 | } |
| 252 | size -= result; |
| 253 | buf += result; |
| 254 | } while (size > 0); |
| 255 | |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 256 | tsk_restore_flags(current, pflags, PF_MEMALLOC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 258 | if (!send && nbd->xmit_timeout) |
| 259 | mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout); |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | return result; |
| 262 | } |
| 263 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 264 | static inline int sock_send_bvec(struct nbd_device *nbd, struct bio_vec *bvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | int flags) |
| 266 | { |
| 267 | int result; |
| 268 | void *kaddr = kmap(bvec->bv_page); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 269 | result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset, |
| 270 | bvec->bv_len, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | kunmap(bvec->bv_page); |
| 272 | return result; |
| 273 | } |
| 274 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 275 | /* always call with the tx_lock held */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 276 | static int nbd_send_req(struct nbd_device *nbd, struct request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 278 | int result, flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | struct nbd_request request; |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 280 | unsigned long size = blk_rq_bytes(req); |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 281 | u32 type; |
| 282 | |
| 283 | if (req->cmd_type == REQ_TYPE_DRV_PRIV) |
| 284 | type = NBD_CMD_DISC; |
| 285 | else if (req->cmd_flags & REQ_DISCARD) |
| 286 | type = NBD_CMD_TRIM; |
| 287 | else if (req->cmd_flags & REQ_FLUSH) |
| 288 | type = NBD_CMD_FLUSH; |
| 289 | else if (rq_data_dir(req) == WRITE) |
| 290 | type = NBD_CMD_WRITE; |
| 291 | else |
| 292 | type = NBD_CMD_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Hani Benhabiles | 04cfac4 | 2014-06-06 14:38:30 -0700 | [diff] [blame] | 294 | memset(&request, 0, sizeof(request)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | request.magic = htonl(NBD_REQUEST_MAGIC); |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 296 | request.type = htonl(type); |
| 297 | if (type != NBD_CMD_FLUSH && type != NBD_CMD_DISC) { |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 298 | request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9); |
| 299 | request.len = htonl(size); |
| 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | memcpy(request.handle, &req, sizeof(req)); |
| 302 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 303 | dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n", |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 304 | req, nbdcmd_to_ascii(type), |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 305 | (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req)); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 306 | result = sock_xmit(nbd, 1, &request, sizeof(request), |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 307 | (type == NBD_CMD_WRITE) ? MSG_MORE : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 309 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 310 | "Send control failed (result %d)\n", result); |
Markus Pargmann | dab5313 | 2015-04-02 10:11:40 +0200 | [diff] [blame] | 311 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 314 | if (type == NBD_CMD_WRITE) { |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 315 | struct req_iterator iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 316 | struct bio_vec bvec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | /* |
| 318 | * we are really probing at internals to determine |
| 319 | * whether to set MSG_MORE or not... |
| 320 | */ |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 321 | rq_for_each_segment(bvec, req, iter) { |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 322 | flags = 0; |
Kent Overstreet | 4550dd6 | 2013-08-07 14:26:21 -0700 | [diff] [blame] | 323 | if (!rq_iter_last(bvec, iter)) |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 324 | flags = MSG_MORE; |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 325 | dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n", |
| 326 | req, bvec.bv_len); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 327 | result = sock_send_bvec(nbd, &bvec, flags); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 328 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 329 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 330 | "Send data failed (result %d)\n", |
| 331 | result); |
Markus Pargmann | dab5313 | 2015-04-02 10:11:40 +0200 | [diff] [blame] | 332 | return -EIO; |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 339 | static struct request *nbd_find_request(struct nbd_device *nbd, |
Denis Cheng | 0cbc591b | 2007-10-16 23:26:14 -0700 | [diff] [blame] | 340 | struct request *xreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Denis Cheng | d2c9740 | 2007-10-16 23:26:14 -0700 | [diff] [blame] | 342 | struct request *req, *tmp; |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 343 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 345 | err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 346 | if (unlikely(err)) |
Markus Pargmann | de9ad6d | 2015-04-02 10:11:41 +0200 | [diff] [blame] | 347 | return ERR_PTR(err); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 348 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 349 | spin_lock(&nbd->queue_lock); |
| 350 | list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (req != xreq) |
| 352 | continue; |
| 353 | list_del_init(&req->queuelist); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 354 | spin_unlock(&nbd->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return req; |
| 356 | } |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 357 | spin_unlock(&nbd->queue_lock); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 358 | |
Markus Pargmann | de9ad6d | 2015-04-02 10:11:41 +0200 | [diff] [blame] | 359 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 362 | static inline int sock_recv_bvec(struct nbd_device *nbd, struct bio_vec *bvec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | int result; |
| 365 | void *kaddr = kmap(bvec->bv_page); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 366 | result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | MSG_WAITALL); |
| 368 | kunmap(bvec->bv_page); |
| 369 | return result; |
| 370 | } |
| 371 | |
| 372 | /* NULL returned = something went wrong, inform userspace */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 373 | static struct request *nbd_read_stat(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
| 375 | int result; |
| 376 | struct nbd_reply reply; |
| 377 | struct request *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | reply.magic = 0; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 380 | result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 382 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 383 | "Receive control failed (result %d)\n", result); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 384 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 386 | |
| 387 | if (ntohl(reply.magic) != NBD_REPLY_MAGIC) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 388 | dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n", |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 389 | (unsigned long)ntohl(reply.magic)); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 390 | return ERR_PTR(-EPROTO); |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 393 | req = nbd_find_request(nbd, *(struct request **)reply.handle); |
Hirofumi Nakagawa | 801678c | 2008-04-29 01:03:09 -0700 | [diff] [blame] | 394 | if (IS_ERR(req)) { |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 395 | result = PTR_ERR(req); |
| 396 | if (result != -ENOENT) |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 397 | return ERR_PTR(result); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 398 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 399 | dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n", |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 400 | reply.handle); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 401 | return ERR_PTR(-EBADR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | if (ntohl(reply.error)) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 405 | dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n", |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 406 | ntohl(reply.error)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | req->errors++; |
| 408 | return req; |
| 409 | } |
| 410 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 411 | dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req); |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 412 | if (rq_data_dir(req) != WRITE) { |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 413 | struct req_iterator iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 414 | struct bio_vec bvec; |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 415 | |
| 416 | rq_for_each_segment(bvec, req, iter) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 417 | result = sock_recv_bvec(nbd, &bvec); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 418 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 419 | dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n", |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 420 | result); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 421 | req->errors++; |
| 422 | return req; |
| 423 | } |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 424 | dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n", |
| 425 | req, bvec.bv_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | return req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 431 | static ssize_t pid_show(struct device *dev, |
| 432 | struct device_attribute *attr, char *buf) |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 433 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 434 | struct gendisk *disk = dev_to_disk(dev); |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 435 | struct nbd_device *nbd = (struct nbd_device *)disk->private_data; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 436 | |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 437 | return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv)); |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 440 | static struct device_attribute pid_attr = { |
Parag Warudkar | 01e8ef1 | 2008-10-18 20:28:50 -0700 | [diff] [blame] | 441 | .attr = { .name = "pid", .mode = S_IRUGO}, |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 442 | .show = pid_show, |
| 443 | }; |
| 444 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 445 | static int nbd_thread_recv(struct nbd_device *nbd, struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
| 447 | struct request *req; |
WANG Cong | 8496304 | 2007-05-09 02:33:36 -0700 | [diff] [blame] | 448 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 450 | BUG_ON(nbd->magic != NBD_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 452 | sk_set_memalloc(nbd->sock->sk); |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 453 | |
| 454 | nbd->task_recv = current; |
| 455 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 456 | ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr); |
WANG Cong | 8496304 | 2007-05-09 02:33:36 -0700 | [diff] [blame] | 457 | if (ret) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 458 | dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n"); |
Markus Pargmann | dcc909d | 2015-10-06 20:03:54 +0200 | [diff] [blame] | 459 | |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 460 | nbd->task_recv = NULL; |
Markus Pargmann | dcc909d | 2015-10-06 20:03:54 +0200 | [diff] [blame] | 461 | |
WANG Cong | 8496304 | 2007-05-09 02:33:36 -0700 | [diff] [blame] | 462 | return ret; |
| 463 | } |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 464 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 465 | nbd_size_update(nbd, bdev); |
| 466 | |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 467 | while (1) { |
| 468 | req = nbd_read_stat(nbd); |
| 469 | if (IS_ERR(req)) { |
| 470 | ret = PTR_ERR(req); |
| 471 | break; |
| 472 | } |
| 473 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 474 | nbd_end_request(nbd, req); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 475 | } |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 476 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 477 | nbd_size_clear(nbd, bdev); |
| 478 | |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 479 | device_remove_file(disk_to_dev(nbd->disk), &pid_attr); |
| 480 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 481 | nbd->task_recv = NULL; |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 482 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 483 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 486 | static void nbd_clear_que(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
| 488 | struct request *req; |
| 489 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 490 | BUG_ON(nbd->magic != NBD_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 492 | /* |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 493 | * Because we have set nbd->sock to NULL under the tx_lock, all |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 494 | * modifications to the list must have completed by now. For |
| 495 | * the same reason, the active_req must be NULL. |
| 496 | * |
| 497 | * As a consequence, we don't need to take the spin lock while |
| 498 | * purging the list here. |
| 499 | */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 500 | BUG_ON(nbd->sock); |
| 501 | BUG_ON(nbd->active_req); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 502 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 503 | while (!list_empty(&nbd->queue_head)) { |
| 504 | req = list_entry(nbd->queue_head.next, struct request, |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 505 | queuelist); |
| 506 | list_del_init(&req->queuelist); |
| 507 | req->errors++; |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 508 | nbd_end_request(nbd, req); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 509 | } |
Paul Clements | fded4e0 | 2012-09-17 14:09:02 -0700 | [diff] [blame] | 510 | |
| 511 | while (!list_empty(&nbd->waiting_queue)) { |
| 512 | req = list_entry(nbd->waiting_queue.next, struct request, |
| 513 | queuelist); |
| 514 | list_del_init(&req->queuelist); |
| 515 | req->errors++; |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 516 | nbd_end_request(nbd, req); |
Paul Clements | fded4e0 | 2012-09-17 14:09:02 -0700 | [diff] [blame] | 517 | } |
Markus Pargmann | e78273c | 2015-08-17 08:20:04 +0200 | [diff] [blame] | 518 | dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 521 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 522 | static void nbd_handle_req(struct nbd_device *nbd, struct request *req) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 523 | { |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 524 | if (req->cmd_type != REQ_TYPE_FS) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 525 | goto error_out; |
| 526 | |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 527 | if (rq_data_dir(req) == WRITE && |
| 528 | (nbd->flags & NBD_FLAG_READ_ONLY)) { |
| 529 | dev_err(disk_to_dev(nbd->disk), |
| 530 | "Write on read-only\n"); |
| 531 | goto error_out; |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 532 | } |
| 533 | |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 534 | req->errors = 0; |
| 535 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 536 | mutex_lock(&nbd->tx_lock); |
| 537 | if (unlikely(!nbd->sock)) { |
| 538 | mutex_unlock(&nbd->tx_lock); |
| 539 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 540 | "Attempted send on closed socket\n"); |
Pavel Machek | 15746fc | 2009-04-02 16:58:42 -0700 | [diff] [blame] | 541 | goto error_out; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 544 | nbd->active_req = req; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 545 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 546 | if (nbd->xmit_timeout && list_empty_careful(&nbd->queue_head)) |
| 547 | mod_timer(&nbd->timeout_timer, jiffies + nbd->xmit_timeout); |
| 548 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 549 | if (nbd_send_req(nbd, req) != 0) { |
| 550 | dev_err(disk_to_dev(nbd->disk), "Request send failed\n"); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 551 | req->errors++; |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 552 | nbd_end_request(nbd, req); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 553 | } else { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 554 | spin_lock(&nbd->queue_lock); |
Chetan Loke | 01ff5db | 2012-07-31 08:47:13 +0200 | [diff] [blame] | 555 | list_add_tail(&req->queuelist, &nbd->queue_head); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 556 | spin_unlock(&nbd->queue_lock); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 559 | nbd->active_req = NULL; |
| 560 | mutex_unlock(&nbd->tx_lock); |
| 561 | wake_up_all(&nbd->active_wq); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 562 | |
| 563 | return; |
| 564 | |
| 565 | error_out: |
| 566 | req->errors++; |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 567 | nbd_end_request(nbd, req); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Markus Pargmann | cad73b2 | 2015-08-17 08:20:08 +0200 | [diff] [blame] | 570 | static int nbd_thread_send(void *data) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 571 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 572 | struct nbd_device *nbd = data; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 573 | struct request *req; |
| 574 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 575 | nbd->task_send = current; |
| 576 | |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 577 | set_user_nice(current, MIN_NICE); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 578 | while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) { |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 579 | /* wait for something to do */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 580 | wait_event_interruptible(nbd->waiting_wq, |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 581 | kthread_should_stop() || |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 582 | !list_empty(&nbd->waiting_queue)); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 583 | |
| 584 | /* extract request */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 585 | if (list_empty(&nbd->waiting_queue)) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 586 | continue; |
| 587 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 588 | spin_lock_irq(&nbd->queue_lock); |
| 589 | req = list_entry(nbd->waiting_queue.next, struct request, |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 590 | queuelist); |
| 591 | list_del_init(&req->queuelist); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 592 | spin_unlock_irq(&nbd->queue_lock); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 593 | |
| 594 | /* handle request */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 595 | nbd_handle_req(nbd, req); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 596 | } |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 597 | |
| 598 | nbd->task_send = NULL; |
| 599 | |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | /* |
| 604 | * We always wait for result of write, for now. It would be nice to make it optional |
| 605 | * in future |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 606 | * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); } |
| 608 | */ |
| 609 | |
Markus Pargmann | cad73b2 | 2015-08-17 08:20:08 +0200 | [diff] [blame] | 610 | static void nbd_request_handler(struct request_queue *q) |
Alex Elder | 398eb08 | 2013-02-27 17:05:28 -0800 | [diff] [blame] | 611 | __releases(q->queue_lock) __acquires(q->queue_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | { |
| 613 | struct request *req; |
| 614 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 615 | while ((req = blk_fetch_request(q)) != NULL) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 616 | struct nbd_device *nbd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 618 | spin_unlock_irq(q->queue_lock); |
| 619 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 620 | nbd = req->rq_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 622 | BUG_ON(nbd->magic != NBD_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 624 | dev_dbg(nbd_to_dev(nbd), "request %p: dequeued (flags=%x)\n", |
| 625 | req, req->cmd_type); |
| 626 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 627 | if (unlikely(!nbd->sock)) { |
Dan Streetman | da6ccaa | 2016-01-14 13:42:32 -0500 | [diff] [blame] | 628 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 629 | "Attempted send on closed socket\n"); |
Paul Clements | 4d48a54 | 2009-02-11 13:04:45 -0800 | [diff] [blame] | 630 | req->errors++; |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 631 | nbd_end_request(nbd, req); |
Paul Clements | 4d48a54 | 2009-02-11 13:04:45 -0800 | [diff] [blame] | 632 | spin_lock_irq(q->queue_lock); |
| 633 | continue; |
| 634 | } |
| 635 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 636 | spin_lock_irq(&nbd->queue_lock); |
| 637 | list_add_tail(&req->queuelist, &nbd->waiting_queue); |
| 638 | spin_unlock_irq(&nbd->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 640 | wake_up(&nbd->waiting_wq); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | spin_lock_irq(q->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 646 | static int nbd_set_socket(struct nbd_device *nbd, struct socket *sock) |
| 647 | { |
| 648 | int ret = 0; |
| 649 | |
| 650 | spin_lock_irq(&nbd->sock_lock); |
| 651 | |
| 652 | if (nbd->sock) { |
| 653 | ret = -EBUSY; |
| 654 | goto out; |
| 655 | } |
| 656 | |
| 657 | nbd->sock = sock; |
| 658 | |
| 659 | out: |
| 660 | spin_unlock_irq(&nbd->sock_lock); |
| 661 | |
| 662 | return ret; |
| 663 | } |
| 664 | |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 665 | /* Reset all properties of an NBD device */ |
| 666 | static void nbd_reset(struct nbd_device *nbd) |
| 667 | { |
| 668 | nbd->disconnect = false; |
| 669 | nbd->timedout = false; |
| 670 | nbd->blksize = 1024; |
| 671 | nbd->bytesize = 0; |
| 672 | set_capacity(nbd->disk, 0); |
| 673 | nbd->flags = 0; |
| 674 | nbd->xmit_timeout = 0; |
| 675 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); |
| 676 | del_timer_sync(&nbd->timeout_timer); |
| 677 | } |
| 678 | |
| 679 | static void nbd_bdev_reset(struct block_device *bdev) |
| 680 | { |
| 681 | set_device_ro(bdev, false); |
| 682 | bdev->bd_inode->i_size = 0; |
| 683 | if (max_part > 0) { |
| 684 | blkdev_reread_part(bdev); |
| 685 | bdev->bd_invalidated = 1; |
| 686 | } |
| 687 | } |
| 688 | |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 689 | static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev) |
| 690 | { |
| 691 | if (nbd->flags & NBD_FLAG_READ_ONLY) |
| 692 | set_device_ro(bdev, true); |
| 693 | if (nbd->flags & NBD_FLAG_SEND_TRIM) |
| 694 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); |
| 695 | if (nbd->flags & NBD_FLAG_SEND_FLUSH) |
Jens Axboe | aafb1ee | 2016-03-30 10:10:53 -0600 | [diff] [blame] | 696 | blk_queue_write_cache(nbd->disk->queue, true, false); |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 697 | else |
Jens Axboe | aafb1ee | 2016-03-30 10:10:53 -0600 | [diff] [blame] | 698 | blk_queue_write_cache(nbd->disk->queue, false, false); |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 699 | } |
| 700 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 701 | static int nbd_dev_dbg_init(struct nbd_device *nbd); |
| 702 | static void nbd_dev_dbg_close(struct nbd_device *nbd); |
| 703 | |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 704 | /* Must be called with tx_lock held */ |
| 705 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 706 | static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 707 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | switch (cmd) { |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 710 | case NBD_DISCONNECT: { |
| 711 | struct request sreq; |
| 712 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 713 | dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n"); |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 714 | if (!nbd->sock) |
| 715 | return -EINVAL; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 716 | |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 717 | mutex_unlock(&nbd->tx_lock); |
| 718 | fsync_bdev(bdev); |
| 719 | mutex_lock(&nbd->tx_lock); |
FUJITA Tomonori | 4f54eec | 2008-04-29 09:54:37 +0200 | [diff] [blame] | 720 | blk_rq_init(NULL, &sreq); |
Christoph Hellwig | 4f8c951 | 2015-04-17 22:37:16 +0200 | [diff] [blame] | 721 | sreq.cmd_type = REQ_TYPE_DRV_PRIV; |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 722 | |
| 723 | /* Check again after getting mutex back. */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 724 | if (!nbd->sock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | return -EINVAL; |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 726 | |
Markus Pargmann | 696697c | 2015-08-17 08:20:07 +0200 | [diff] [blame] | 727 | nbd->disconnect = true; |
Paul Clements | c378f70 | 2013-07-03 15:09:04 -0700 | [diff] [blame] | 728 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 729 | nbd_send_req(nbd, &sreq); |
Paul Clements | c378f70 | 2013-07-03 15:09:04 -0700 | [diff] [blame] | 730 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 733 | case NBD_CLEAR_SOCK: |
| 734 | sock_shutdown(nbd); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 735 | nbd_clear_que(nbd); |
| 736 | BUG_ON(!list_empty(&nbd->queue_head)); |
Paul Clements | fded4e0 | 2012-09-17 14:09:02 -0700 | [diff] [blame] | 737 | BUG_ON(!list_empty(&nbd->waiting_queue)); |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 738 | kill_bdev(bdev); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 739 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 740 | |
| 741 | case NBD_SET_SOCK: { |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 742 | int err; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 743 | struct socket *sock = sockfd_lookup(arg, &err); |
| 744 | |
| 745 | if (!sock) |
| 746 | return err; |
| 747 | |
| 748 | err = nbd_set_socket(nbd, sock); |
| 749 | if (!err && max_part) |
| 750 | bdev->bd_invalidated = 1; |
| 751 | |
| 752 | return err; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 755 | case NBD_SET_BLKSIZE: { |
Arnd Bergmann | 5e454c6 | 2016-03-05 00:49:31 +0100 | [diff] [blame] | 756 | loff_t bsize = div_s64(nbd->bytesize, arg); |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 757 | |
| 758 | return nbd_size_set(nbd, bdev, arg, bsize); |
| 759 | } |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | case NBD_SET_SIZE: |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 762 | return nbd_size_set(nbd, bdev, nbd->blksize, |
| 763 | arg / nbd->blksize); |
| 764 | |
| 765 | case NBD_SET_SIZE_BLOCKS: |
| 766 | return nbd_size_set(nbd, bdev, nbd->blksize, arg); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 767 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 768 | case NBD_SET_TIMEOUT: |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 769 | nbd->xmit_timeout = arg * HZ; |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 770 | if (arg) |
| 771 | mod_timer(&nbd->timeout_timer, |
| 772 | jiffies + nbd->xmit_timeout); |
| 773 | else |
| 774 | del_timer_sync(&nbd->timeout_timer); |
| 775 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 776 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 777 | |
Paul Clements | 2f01250 | 2012-10-04 17:16:15 -0700 | [diff] [blame] | 778 | case NBD_SET_FLAGS: |
| 779 | nbd->flags = arg; |
| 780 | return 0; |
| 781 | |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 782 | case NBD_DO_IT: { |
| 783 | struct task_struct *thread; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 784 | int error; |
| 785 | |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 786 | if (nbd->task_recv) |
Pavel Machek | c91192d | 2009-01-15 13:51:03 -0800 | [diff] [blame] | 787 | return -EBUSY; |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 788 | if (!nbd->sock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | return -EINVAL; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 790 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 791 | mutex_unlock(&nbd->tx_lock); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 792 | |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 793 | nbd_parse_flags(nbd, bdev); |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 794 | |
Markus Pargmann | cad73b2 | 2015-08-17 08:20:08 +0200 | [diff] [blame] | 795 | thread = kthread_run(nbd_thread_send, nbd, "%s", |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 796 | nbd_name(nbd)); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 797 | if (IS_ERR(thread)) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 798 | mutex_lock(&nbd->tx_lock); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 799 | return PTR_ERR(thread); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 800 | } |
Markus Pargmann | d06df60 | 2015-04-02 10:11:36 +0200 | [diff] [blame] | 801 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 802 | nbd_dev_dbg_init(nbd); |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 803 | error = nbd_thread_recv(nbd, bdev); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 804 | nbd_dev_dbg_close(nbd); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 805 | kthread_stop(thread); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 806 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 807 | mutex_lock(&nbd->tx_lock); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 808 | |
Markus Pargmann | 36e47be | 2015-08-17 08:20:01 +0200 | [diff] [blame] | 809 | sock_shutdown(nbd); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 810 | nbd_clear_que(nbd); |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 811 | kill_bdev(bdev); |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 812 | nbd_bdev_reset(bdev); |
| 813 | |
Paul Clements | c378f70 | 2013-07-03 15:09:04 -0700 | [diff] [blame] | 814 | if (nbd->disconnect) /* user requested, ignore socket errors */ |
Markus Pargmann | 1f7b5cf | 2015-10-29 12:01:34 +0100 | [diff] [blame] | 815 | error = 0; |
| 816 | if (nbd->timedout) |
| 817 | error = -ETIMEDOUT; |
| 818 | |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 819 | nbd_reset(nbd); |
| 820 | |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 821 | return error; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | case NBD_CLEAR_QUE: |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 825 | /* |
| 826 | * This is for compatibility only. The queue is always cleared |
| 827 | * by NBD_DO_IT or NBD_CLEAR_SOCK. |
| 828 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | case NBD_PRINT_DEBUG: |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 832 | dev_info(disk_to_dev(nbd->disk), |
WANG Cong | 5eedf54 | 2011-08-19 14:48:28 +0200 | [diff] [blame] | 833 | "next = %p, prev = %p, head = %p\n", |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 834 | nbd->queue_head.next, nbd->queue_head.prev, |
| 835 | &nbd->queue_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | return 0; |
| 837 | } |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 838 | return -ENOTTY; |
| 839 | } |
| 840 | |
| 841 | static int nbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 842 | unsigned int cmd, unsigned long arg) |
| 843 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 844 | struct nbd_device *nbd = bdev->bd_disk->private_data; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 845 | int error; |
| 846 | |
| 847 | if (!capable(CAP_SYS_ADMIN)) |
| 848 | return -EPERM; |
| 849 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 850 | BUG_ON(nbd->magic != NBD_MAGIC); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 851 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 852 | mutex_lock(&nbd->tx_lock); |
| 853 | error = __nbd_ioctl(bdev, nbd, cmd, arg); |
| 854 | mutex_unlock(&nbd->tx_lock); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 855 | |
| 856 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 859 | static const struct block_device_operations nbd_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
| 861 | .owner = THIS_MODULE, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 862 | .ioctl = nbd_ioctl, |
Al Viro | 263a3df | 2016-01-07 10:04:37 -0500 | [diff] [blame] | 863 | .compat_ioctl = nbd_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | }; |
| 865 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 866 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 867 | |
| 868 | static int nbd_dbg_tasks_show(struct seq_file *s, void *unused) |
| 869 | { |
| 870 | struct nbd_device *nbd = s->private; |
| 871 | |
| 872 | if (nbd->task_recv) |
| 873 | seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv)); |
| 874 | if (nbd->task_send) |
| 875 | seq_printf(s, "send: %d\n", task_pid_nr(nbd->task_send)); |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static int nbd_dbg_tasks_open(struct inode *inode, struct file *file) |
| 881 | { |
| 882 | return single_open(file, nbd_dbg_tasks_show, inode->i_private); |
| 883 | } |
| 884 | |
| 885 | static const struct file_operations nbd_dbg_tasks_ops = { |
| 886 | .open = nbd_dbg_tasks_open, |
| 887 | .read = seq_read, |
| 888 | .llseek = seq_lseek, |
| 889 | .release = single_release, |
| 890 | }; |
| 891 | |
| 892 | static int nbd_dbg_flags_show(struct seq_file *s, void *unused) |
| 893 | { |
| 894 | struct nbd_device *nbd = s->private; |
| 895 | u32 flags = nbd->flags; |
| 896 | |
| 897 | seq_printf(s, "Hex: 0x%08x\n\n", flags); |
| 898 | |
| 899 | seq_puts(s, "Known flags:\n"); |
| 900 | |
| 901 | if (flags & NBD_FLAG_HAS_FLAGS) |
| 902 | seq_puts(s, "NBD_FLAG_HAS_FLAGS\n"); |
| 903 | if (flags & NBD_FLAG_READ_ONLY) |
| 904 | seq_puts(s, "NBD_FLAG_READ_ONLY\n"); |
| 905 | if (flags & NBD_FLAG_SEND_FLUSH) |
| 906 | seq_puts(s, "NBD_FLAG_SEND_FLUSH\n"); |
| 907 | if (flags & NBD_FLAG_SEND_TRIM) |
| 908 | seq_puts(s, "NBD_FLAG_SEND_TRIM\n"); |
| 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | static int nbd_dbg_flags_open(struct inode *inode, struct file *file) |
| 914 | { |
| 915 | return single_open(file, nbd_dbg_flags_show, inode->i_private); |
| 916 | } |
| 917 | |
| 918 | static const struct file_operations nbd_dbg_flags_ops = { |
| 919 | .open = nbd_dbg_flags_open, |
| 920 | .read = seq_read, |
| 921 | .llseek = seq_lseek, |
| 922 | .release = single_release, |
| 923 | }; |
| 924 | |
| 925 | static int nbd_dev_dbg_init(struct nbd_device *nbd) |
| 926 | { |
| 927 | struct dentry *dir; |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 928 | |
| 929 | if (!nbd_dbg_dir) |
| 930 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 931 | |
| 932 | dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir); |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 933 | if (!dir) { |
| 934 | dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n", |
| 935 | nbd_name(nbd)); |
| 936 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 937 | } |
| 938 | nbd->dbg_dir = dir; |
| 939 | |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 940 | debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops); |
| 941 | debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize); |
| 942 | debugfs_create_u32("timeout", 0444, dir, &nbd->xmit_timeout); |
| 943 | debugfs_create_u32("blocksize", 0444, dir, &nbd->blksize); |
| 944 | debugfs_create_file("flags", 0444, dir, &nbd, &nbd_dbg_flags_ops); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | static void nbd_dev_dbg_close(struct nbd_device *nbd) |
| 950 | { |
| 951 | debugfs_remove_recursive(nbd->dbg_dir); |
| 952 | } |
| 953 | |
| 954 | static int nbd_dbg_init(void) |
| 955 | { |
| 956 | struct dentry *dbg_dir; |
| 957 | |
| 958 | dbg_dir = debugfs_create_dir("nbd", NULL); |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 959 | if (!dbg_dir) |
| 960 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 961 | |
| 962 | nbd_dbg_dir = dbg_dir; |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static void nbd_dbg_close(void) |
| 968 | { |
| 969 | debugfs_remove_recursive(nbd_dbg_dir); |
| 970 | } |
| 971 | |
| 972 | #else /* IS_ENABLED(CONFIG_DEBUG_FS) */ |
| 973 | |
| 974 | static int nbd_dev_dbg_init(struct nbd_device *nbd) |
| 975 | { |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | static void nbd_dev_dbg_close(struct nbd_device *nbd) |
| 980 | { |
| 981 | } |
| 982 | |
| 983 | static int nbd_dbg_init(void) |
| 984 | { |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static void nbd_dbg_close(void) |
| 989 | { |
| 990 | } |
| 991 | |
| 992 | #endif |
| 993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | /* |
| 995 | * And here should be modules and kernel interface |
| 996 | * (Just smiley confuses emacs :-) |
| 997 | */ |
| 998 | |
| 999 | static int __init nbd_init(void) |
| 1000 | { |
| 1001 | int err = -ENOMEM; |
| 1002 | int i; |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1003 | int part_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Adrian Bunk | 5b7b18c | 2006-03-25 03:07:04 -0800 | [diff] [blame] | 1005 | BUILD_BUG_ON(sizeof(struct nbd_request) != 28); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1007 | if (max_part < 0) { |
WANG Cong | 7742ce4 | 2011-08-19 14:48:28 +0200 | [diff] [blame] | 1008 | printk(KERN_ERR "nbd: max_part must be >= 0\n"); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1009 | return -EINVAL; |
| 1010 | } |
| 1011 | |
| 1012 | part_shift = 0; |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1013 | if (max_part > 0) { |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1014 | part_shift = fls(max_part); |
| 1015 | |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1016 | /* |
| 1017 | * Adjust max_part according to part_shift as it is exported |
| 1018 | * to user space so that user can know the max number of |
| 1019 | * partition kernel should be able to manage. |
| 1020 | * |
| 1021 | * Note that -1 is required because partition 0 is reserved |
| 1022 | * for the whole disk. |
| 1023 | */ |
| 1024 | max_part = (1UL << part_shift) - 1; |
| 1025 | } |
| 1026 | |
Namhyung Kim | 3b27108 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1027 | if ((1UL << part_shift) > DISK_MAX_PARTS) |
| 1028 | return -EINVAL; |
| 1029 | |
| 1030 | if (nbds_max > 1UL << (MINORBITS - part_shift)) |
| 1031 | return -EINVAL; |
| 1032 | |
Sudip Mukherjee | ff6b809 | 2015-01-27 18:08:22 +0530 | [diff] [blame] | 1033 | nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL); |
| 1034 | if (!nbd_dev) |
| 1035 | return -ENOMEM; |
| 1036 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 1037 | for (i = 0; i < nbds_max; i++) { |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1038 | struct gendisk *disk = alloc_disk(1 << part_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | if (!disk) |
| 1040 | goto out; |
| 1041 | nbd_dev[i].disk = disk; |
| 1042 | /* |
| 1043 | * The new linux 2.5 block layer implementation requires |
| 1044 | * every gendisk to have its very own request_queue struct. |
| 1045 | * These structs are big so we dynamically allocate them. |
| 1046 | */ |
Markus Pargmann | cad73b2 | 2015-08-17 08:20:08 +0200 | [diff] [blame] | 1047 | disk->queue = blk_init_queue(nbd_request_handler, &nbd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | if (!disk->queue) { |
| 1049 | put_disk(disk); |
| 1050 | goto out; |
| 1051 | } |
Jens Axboe | 31dcfab | 2008-10-31 10:06:37 +0100 | [diff] [blame] | 1052 | /* |
| 1053 | * Tell the block layer that we are not a rotational device |
| 1054 | */ |
| 1055 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 1056 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue); |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 1057 | disk->queue->limits.discard_granularity = 512; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 1058 | blk_queue_max_discard_sectors(disk->queue, UINT_MAX); |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 1059 | disk->queue->limits.discard_zeroes_data = 0; |
Michal Belczyk | 078be02 | 2013-04-30 15:28:28 -0700 | [diff] [blame] | 1060 | blk_queue_max_hw_sectors(disk->queue, 65536); |
| 1061 | disk->queue->limits.max_sectors = 256; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | if (register_blkdev(NBD_MAJOR, "nbd")) { |
| 1065 | err = -EIO; |
| 1066 | goto out; |
| 1067 | } |
| 1068 | |
| 1069 | printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1071 | nbd_dbg_init(); |
| 1072 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 1073 | for (i = 0; i < nbds_max; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | struct gendisk *disk = nbd_dev[i].disk; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 1075 | nbd_dev[i].magic = NBD_MAGIC; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 1076 | INIT_LIST_HEAD(&nbd_dev[i].waiting_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | spin_lock_init(&nbd_dev[i].queue_lock); |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 1078 | spin_lock_init(&nbd_dev[i].sock_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | INIT_LIST_HEAD(&nbd_dev[i].queue_head); |
Ingo Molnar | 82d4dc5 | 2006-03-23 03:00:38 -0800 | [diff] [blame] | 1080 | mutex_init(&nbd_dev[i].tx_lock); |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 1081 | init_timer(&nbd_dev[i].timeout_timer); |
| 1082 | nbd_dev[i].timeout_timer.function = nbd_xmit_timeout; |
| 1083 | nbd_dev[i].timeout_timer.data = (unsigned long)&nbd_dev[i]; |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 1084 | init_waitqueue_head(&nbd_dev[i].active_wq); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 1085 | init_waitqueue_head(&nbd_dev[i].waiting_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | disk->major = NBD_MAJOR; |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1087 | disk->first_minor = i << part_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | disk->fops = &nbd_fops; |
| 1089 | disk->private_data = &nbd_dev[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | sprintf(disk->disk_name, "nbd%d", i); |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 1091 | nbd_reset(&nbd_dev[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | add_disk(disk); |
| 1093 | } |
| 1094 | |
| 1095 | return 0; |
| 1096 | out: |
| 1097 | while (i--) { |
| 1098 | blk_cleanup_queue(nbd_dev[i].disk->queue); |
| 1099 | put_disk(nbd_dev[i].disk); |
| 1100 | } |
Sven Wegener | f3944d6 | 2008-08-20 14:09:07 -0700 | [diff] [blame] | 1101 | kfree(nbd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | return err; |
| 1103 | } |
| 1104 | |
| 1105 | static void __exit nbd_cleanup(void) |
| 1106 | { |
| 1107 | int i; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1108 | |
| 1109 | nbd_dbg_close(); |
| 1110 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 1111 | for (i = 0; i < nbds_max; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | struct gendisk *disk = nbd_dev[i].disk; |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 1113 | nbd_dev[i].magic = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | if (disk) { |
| 1115 | del_gendisk(disk); |
| 1116 | blk_cleanup_queue(disk->queue); |
| 1117 | put_disk(disk); |
| 1118 | } |
| 1119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | unregister_blkdev(NBD_MAJOR, "nbd"); |
Sven Wegener | f3944d6 | 2008-08-20 14:09:07 -0700 | [diff] [blame] | 1121 | kfree(nbd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR); |
| 1123 | } |
| 1124 | |
| 1125 | module_init(nbd_init); |
| 1126 | module_exit(nbd_cleanup); |
| 1127 | |
| 1128 | MODULE_DESCRIPTION("Network Block Device"); |
| 1129 | MODULE_LICENSE("GPL"); |
| 1130 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 1131 | module_param(nbds_max, int, 0444); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1132 | MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)"); |
| 1133 | module_param(max_part, int, 0444); |
| 1134 | MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)"); |