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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/uaccess.h> |
| 38 | #include <asm/types.h> |
| 39 | |
| 40 | #include <linux/nbd.h> |
| 41 | |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 42 | struct nbd_device { |
| 43 | int flags; |
| 44 | int harderror; /* Code of hard error */ |
| 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 | pid_t pid; /* pid of nbd-client, if attached */ |
| 60 | int xmit_timeout; |
| 61 | int disconnect; /* a disconnect has been requested by user */ |
| 62 | }; |
| 63 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 64 | #define NBD_MAGIC 0x68797548 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | #ifdef NDEBUG |
| 67 | #define dprintk(flags, fmt...) |
| 68 | #else /* NDEBUG */ |
| 69 | #define dprintk(flags, fmt...) do { \ |
| 70 | if (debugflags & (flags)) printk(KERN_DEBUG fmt); \ |
| 71 | } while (0) |
| 72 | #define DBG_IOCTL 0x0004 |
| 73 | #define DBG_INIT 0x0010 |
| 74 | #define DBG_EXIT 0x0020 |
| 75 | #define DBG_BLKDEV 0x0100 |
| 76 | #define DBG_RX 0x0200 |
| 77 | #define DBG_TX 0x0400 |
| 78 | static unsigned int debugflags; |
| 79 | #endif /* NDEBUG */ |
| 80 | |
Ingo van Lil | 9c7a416 | 2006-07-01 04:36:36 -0700 | [diff] [blame] | 81 | static unsigned int nbds_max = 16; |
Paul Clements | 20a8143 | 2008-02-08 04:21:51 -0800 | [diff] [blame] | 82 | static struct nbd_device *nbd_dev; |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 83 | static int max_part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * Use just one lock (or at most 1 per NIC). Two arguments for this: |
| 87 | * 1. Each NIC is essentially a synchronization point for all servers |
| 88 | * accessed through that NIC so there's no need to have more locks |
| 89 | * than NICs anyway. |
| 90 | * 2. More locks lead to more "Dirty cache line bouncing" which will slow |
| 91 | * down each lock to the point where they're actually slower than just |
| 92 | * a single lock. |
| 93 | * Thanks go to Jens Axboe and Al Viro for their LKML emails explaining this! |
| 94 | */ |
| 95 | static DEFINE_SPINLOCK(nbd_lock); |
| 96 | |
| 97 | #ifndef NDEBUG |
| 98 | static const char *ioctl_cmd_to_ascii(int cmd) |
| 99 | { |
| 100 | switch (cmd) { |
| 101 | case NBD_SET_SOCK: return "set-sock"; |
| 102 | case NBD_SET_BLKSIZE: return "set-blksize"; |
| 103 | case NBD_SET_SIZE: return "set-size"; |
Paul Clements | 2f01250 | 2012-10-04 17:16:15 -0700 | [diff] [blame] | 104 | case NBD_SET_TIMEOUT: return "set-timeout"; |
| 105 | case NBD_SET_FLAGS: return "set-flags"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | case NBD_DO_IT: return "do-it"; |
| 107 | case NBD_CLEAR_SOCK: return "clear-sock"; |
| 108 | case NBD_CLEAR_QUE: return "clear-que"; |
| 109 | case NBD_PRINT_DEBUG: return "print-debug"; |
| 110 | case NBD_SET_SIZE_BLOCKS: return "set-size-blocks"; |
| 111 | case NBD_DISCONNECT: return "disconnect"; |
| 112 | case BLKROSET: return "set-read-only"; |
| 113 | case BLKFLSBUF: return "flush-buffer-cache"; |
| 114 | } |
| 115 | return "unknown"; |
| 116 | } |
| 117 | |
| 118 | static const char *nbdcmd_to_ascii(int cmd) |
| 119 | { |
| 120 | switch (cmd) { |
| 121 | case NBD_CMD_READ: return "read"; |
| 122 | case NBD_CMD_WRITE: return "write"; |
| 123 | case NBD_CMD_DISC: return "disconnect"; |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 124 | case NBD_CMD_FLUSH: return "flush"; |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 125 | case NBD_CMD_TRIM: return "trim/discard"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | return "invalid"; |
| 128 | } |
| 129 | #endif /* NDEBUG */ |
| 130 | |
| 131 | static void nbd_end_request(struct request *req) |
| 132 | { |
Kiyoshi Ueda | 097c94a | 2007-12-11 17:44:06 -0500 | [diff] [blame] | 133 | int error = req->errors ? -EIO : 0; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 134 | struct request_queue *q = req->q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | unsigned long flags; |
| 136 | |
| 137 | dprintk(DBG_BLKDEV, "%s: request %p: %s\n", req->rq_disk->disk_name, |
Kiyoshi Ueda | 097c94a | 2007-12-11 17:44:06 -0500 | [diff] [blame] | 138 | req, error ? "failed" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | spin_lock_irqsave(q->queue_lock, flags); |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 141 | __blk_end_request_all(req, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 143 | } |
| 144 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 145 | static void sock_shutdown(struct nbd_device *nbd, int lock) |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 146 | { |
| 147 | /* Forcibly shutdown the socket causing all listeners |
| 148 | * to error |
| 149 | * |
| 150 | * FIXME: This code is duplicated from sys_shutdown, but |
| 151 | * there should be a more generic interface rather than |
| 152 | * calling socket ops directly here */ |
| 153 | if (lock) |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 154 | mutex_lock(&nbd->tx_lock); |
| 155 | if (nbd->sock) { |
| 156 | dev_warn(disk_to_dev(nbd->disk), "shutting down socket\n"); |
| 157 | kernel_sock_shutdown(nbd->sock, SHUT_RDWR); |
| 158 | nbd->sock = NULL; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 159 | } |
| 160 | if (lock) |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 161 | mutex_unlock(&nbd->tx_lock); |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void nbd_xmit_timeout(unsigned long arg) |
| 165 | { |
| 166 | struct task_struct *task = (struct task_struct *)arg; |
| 167 | |
| 168 | printk(KERN_WARNING "nbd: killing hung xmit (%s, pid: %d)\n", |
| 169 | task->comm, task->pid); |
| 170 | force_sig(SIGKILL, task); |
| 171 | } |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* |
| 174 | * Send or receive packet. |
| 175 | */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 176 | 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] | 177 | int msg_flags) |
| 178 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 179 | struct socket *sock = nbd->sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | int result; |
| 181 | struct msghdr msg; |
| 182 | struct kvec iov; |
Oleg Nesterov | be0ef95 | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 183 | sigset_t blocked, oldset; |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 184 | unsigned long pflags = current->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 186 | if (unlikely(!sock)) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 187 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 188 | "Attempted %s on closed socket in sock_xmit\n", |
| 189 | (send ? "send" : "recv")); |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 190 | return -EINVAL; |
| 191 | } |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | /* Allow interception of SIGKILL only |
| 194 | * Don't allow other signals to interrupt the transmission */ |
Oleg Nesterov | be0ef95 | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 195 | siginitsetinv(&blocked, sigmask(SIGKILL)); |
| 196 | sigprocmask(SIG_SETMASK, &blocked, &oldset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 198 | current->flags |= PF_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | do { |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 200 | sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | iov.iov_base = buf; |
| 202 | iov.iov_len = size; |
| 203 | msg.msg_name = NULL; |
| 204 | msg.msg_namelen = 0; |
| 205 | msg.msg_control = NULL; |
| 206 | msg.msg_controllen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | msg.msg_flags = msg_flags | MSG_NOSIGNAL; |
| 208 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 209 | if (send) { |
| 210 | struct timer_list ti; |
| 211 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 212 | if (nbd->xmit_timeout) { |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 213 | init_timer(&ti); |
| 214 | ti.function = nbd_xmit_timeout; |
| 215 | ti.data = (unsigned long)current; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 216 | ti.expires = jiffies + nbd->xmit_timeout; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 217 | add_timer(&ti); |
| 218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | result = kernel_sendmsg(sock, &msg, &iov, 1, size); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 220 | if (nbd->xmit_timeout) |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 221 | del_timer_sync(&ti); |
| 222 | } else |
Namhyung Kim | 35fbf5b | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 223 | result = kernel_recvmsg(sock, &msg, &iov, 1, size, |
| 224 | msg.msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | if (signal_pending(current)) { |
| 227 | siginfo_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | printk(KERN_WARNING "nbd (pid %d: %s) got signal %d\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 229 | task_pid_nr(current), current->comm, |
Oleg Nesterov | be0ef95 | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 230 | dequeue_signal_lock(current, ¤t->blocked, &info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | result = -EINTR; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 232 | sock_shutdown(nbd, !send); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | |
| 236 | if (result <= 0) { |
| 237 | if (result == 0) |
| 238 | result = -EPIPE; /* short read */ |
| 239 | break; |
| 240 | } |
| 241 | size -= result; |
| 242 | buf += result; |
| 243 | } while (size > 0); |
| 244 | |
Oleg Nesterov | be0ef95 | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 245 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 246 | tsk_restore_flags(current, pflags, PF_MEMALLOC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | return result; |
| 249 | } |
| 250 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 251 | 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] | 252 | int flags) |
| 253 | { |
| 254 | int result; |
| 255 | void *kaddr = kmap(bvec->bv_page); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 256 | result = sock_xmit(nbd, 1, kaddr + bvec->bv_offset, |
| 257 | bvec->bv_len, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | kunmap(bvec->bv_page); |
| 259 | return result; |
| 260 | } |
| 261 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 262 | /* always call with the tx_lock held */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 263 | static int nbd_send_req(struct nbd_device *nbd, struct request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 265 | int result, flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | struct nbd_request request; |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 267 | unsigned long size = blk_rq_bytes(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Hani Benhabiles | 04cfac4 | 2014-06-06 14:38:30 -0700 | [diff] [blame] | 269 | memset(&request, 0, sizeof(request)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | request.magic = htonl(NBD_REQUEST_MAGIC); |
| 271 | request.type = htonl(nbd_cmd(req)); |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 272 | |
Hani Benhabiles | 04cfac4 | 2014-06-06 14:38:30 -0700 | [diff] [blame] | 273 | if (nbd_cmd(req) != NBD_CMD_FLUSH && nbd_cmd(req) != NBD_CMD_DISC) { |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 274 | request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9); |
| 275 | request.len = htonl(size); |
| 276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | memcpy(request.handle, &req, sizeof(req)); |
| 278 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 279 | dprintk(DBG_TX, "%s: request %p: sending control (%s@%llu,%uB)\n", |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 280 | nbd->disk->disk_name, req, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | nbdcmd_to_ascii(nbd_cmd(req)), |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 282 | (unsigned long long)blk_rq_pos(req) << 9, |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 283 | blk_rq_bytes(req)); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 284 | result = sock_xmit(nbd, 1, &request, sizeof(request), |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 285 | (nbd_cmd(req) == NBD_CMD_WRITE) ? MSG_MORE : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 287 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 288 | "Send control failed (result %d)\n", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | goto error_out; |
| 290 | } |
| 291 | |
| 292 | if (nbd_cmd(req) == NBD_CMD_WRITE) { |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 293 | struct req_iterator iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 294 | struct bio_vec bvec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* |
| 296 | * we are really probing at internals to determine |
| 297 | * whether to set MSG_MORE or not... |
| 298 | */ |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 299 | rq_for_each_segment(bvec, req, iter) { |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 300 | flags = 0; |
Kent Overstreet | 4550dd6 | 2013-08-07 14:26:21 -0700 | [diff] [blame] | 301 | if (!rq_iter_last(bvec, iter)) |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 302 | flags = MSG_MORE; |
| 303 | dprintk(DBG_TX, "%s: request %p: sending %d bytes data\n", |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 304 | nbd->disk->disk_name, req, bvec.bv_len); |
| 305 | result = sock_send_bvec(nbd, &bvec, flags); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 306 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 307 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 308 | "Send data failed (result %d)\n", |
| 309 | result); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 310 | goto error_out; |
| 311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | |
| 316 | error_out: |
Pavel Machek | 15746fc | 2009-04-02 16:58:42 -0700 | [diff] [blame] | 317 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 320 | static struct request *nbd_find_request(struct nbd_device *nbd, |
Denis Cheng | 0cbc591b | 2007-10-16 23:26:14 -0700 | [diff] [blame] | 321 | struct request *xreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Denis Cheng | d2c9740 | 2007-10-16 23:26:14 -0700 | [diff] [blame] | 323 | struct request *req, *tmp; |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 324 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 326 | err = wait_event_interruptible(nbd->active_wq, nbd->active_req != xreq); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 327 | if (unlikely(err)) |
| 328 | goto out; |
| 329 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 330 | spin_lock(&nbd->queue_lock); |
| 331 | list_for_each_entry_safe(req, tmp, &nbd->queue_head, queuelist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (req != xreq) |
| 333 | continue; |
| 334 | list_del_init(&req->queuelist); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 335 | spin_unlock(&nbd->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | return req; |
| 337 | } |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 338 | spin_unlock(&nbd->queue_lock); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 339 | |
| 340 | err = -ENOENT; |
| 341 | |
| 342 | out: |
| 343 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 346 | 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] | 347 | { |
| 348 | int result; |
| 349 | void *kaddr = kmap(bvec->bv_page); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 350 | result = sock_xmit(nbd, 0, kaddr + bvec->bv_offset, bvec->bv_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | MSG_WAITALL); |
| 352 | kunmap(bvec->bv_page); |
| 353 | return result; |
| 354 | } |
| 355 | |
| 356 | /* NULL returned = something went wrong, inform userspace */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 357 | static struct request *nbd_read_stat(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
| 359 | int result; |
| 360 | struct nbd_reply reply; |
| 361 | struct request *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | reply.magic = 0; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 364 | result = sock_xmit(nbd, 0, &reply, sizeof(reply), MSG_WAITALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 366 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 367 | "Receive control failed (result %d)\n", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | goto harderror; |
| 369 | } |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 370 | |
| 371 | if (ntohl(reply.magic) != NBD_REPLY_MAGIC) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 372 | dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n", |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 373 | (unsigned long)ntohl(reply.magic)); |
| 374 | result = -EPROTO; |
| 375 | goto harderror; |
| 376 | } |
| 377 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 378 | req = nbd_find_request(nbd, *(struct request **)reply.handle); |
Hirofumi Nakagawa | 801678c | 2008-04-29 01:03:09 -0700 | [diff] [blame] | 379 | if (IS_ERR(req)) { |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 380 | result = PTR_ERR(req); |
| 381 | if (result != -ENOENT) |
| 382 | goto harderror; |
| 383 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 384 | dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%p)\n", |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 385 | reply.handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | result = -EBADR; |
| 387 | goto harderror; |
| 388 | } |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | if (ntohl(reply.error)) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 391 | 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] | 392 | ntohl(reply.error)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | req->errors++; |
| 394 | return req; |
| 395 | } |
| 396 | |
| 397 | dprintk(DBG_RX, "%s: request %p: got reply\n", |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 398 | nbd->disk->disk_name, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | if (nbd_cmd(req) == NBD_CMD_READ) { |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 400 | struct req_iterator iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 401 | struct bio_vec bvec; |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 402 | |
| 403 | rq_for_each_segment(bvec, req, iter) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 404 | result = sock_recv_bvec(nbd, &bvec); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 405 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 406 | 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] | 407 | result); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 408 | req->errors++; |
| 409 | return req; |
| 410 | } |
| 411 | dprintk(DBG_RX, "%s: request %p: got %d bytes data\n", |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 412 | nbd->disk->disk_name, req, bvec.bv_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | return req; |
| 416 | harderror: |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 417 | nbd->harderror = result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return NULL; |
| 419 | } |
| 420 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 421 | static ssize_t pid_show(struct device *dev, |
| 422 | struct device_attribute *attr, char *buf) |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 423 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 424 | struct gendisk *disk = dev_to_disk(dev); |
| 425 | |
| 426 | return sprintf(buf, "%ld\n", |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 427 | (long) ((struct nbd_device *)disk->private_data)->pid); |
| 428 | } |
| 429 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 430 | static struct device_attribute pid_attr = { |
Parag Warudkar | 01e8ef1 | 2008-10-18 20:28:50 -0700 | [diff] [blame] | 431 | .attr = { .name = "pid", .mode = S_IRUGO}, |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 432 | .show = pid_show, |
| 433 | }; |
| 434 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 435 | static int nbd_do_it(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
| 437 | struct request *req; |
WANG Cong | 8496304 | 2007-05-09 02:33:36 -0700 | [diff] [blame] | 438 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 440 | BUG_ON(nbd->magic != NBD_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 442 | sk_set_memalloc(nbd->sock->sk); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 443 | nbd->pid = task_pid_nr(current); |
| 444 | ret = device_create_file(disk_to_dev(nbd->disk), &pid_attr); |
WANG Cong | 8496304 | 2007-05-09 02:33:36 -0700 | [diff] [blame] | 445 | if (ret) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 446 | dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n"); |
| 447 | nbd->pid = 0; |
WANG Cong | 8496304 | 2007-05-09 02:33:36 -0700 | [diff] [blame] | 448 | return ret; |
| 449 | } |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 450 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 451 | while ((req = nbd_read_stat(nbd)) != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | nbd_end_request(req); |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 453 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 454 | device_remove_file(disk_to_dev(nbd->disk), &pid_attr); |
| 455 | nbd->pid = 0; |
WANG Cong | 8496304 | 2007-05-09 02:33:36 -0700 | [diff] [blame] | 456 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 459 | static void nbd_clear_que(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
| 461 | struct request *req; |
| 462 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 463 | BUG_ON(nbd->magic != NBD_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 465 | /* |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 466 | * 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] | 467 | * modifications to the list must have completed by now. For |
| 468 | * the same reason, the active_req must be NULL. |
| 469 | * |
| 470 | * As a consequence, we don't need to take the spin lock while |
| 471 | * purging the list here. |
| 472 | */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 473 | BUG_ON(nbd->sock); |
| 474 | BUG_ON(nbd->active_req); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 475 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 476 | while (!list_empty(&nbd->queue_head)) { |
| 477 | req = list_entry(nbd->queue_head.next, struct request, |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 478 | queuelist); |
| 479 | list_del_init(&req->queuelist); |
| 480 | req->errors++; |
| 481 | nbd_end_request(req); |
| 482 | } |
Paul Clements | fded4e0 | 2012-09-17 14:09:02 -0700 | [diff] [blame] | 483 | |
| 484 | while (!list_empty(&nbd->waiting_queue)) { |
| 485 | req = list_entry(nbd->waiting_queue.next, struct request, |
| 486 | queuelist); |
| 487 | list_del_init(&req->queuelist); |
| 488 | req->errors++; |
| 489 | nbd_end_request(req); |
| 490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 493 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 494 | static void nbd_handle_req(struct nbd_device *nbd, struct request *req) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 495 | { |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 496 | if (req->cmd_type != REQ_TYPE_FS) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 497 | goto error_out; |
| 498 | |
| 499 | nbd_cmd(req) = NBD_CMD_READ; |
| 500 | if (rq_data_dir(req) == WRITE) { |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 501 | if ((req->cmd_flags & REQ_DISCARD)) { |
| 502 | WARN_ON(!(nbd->flags & NBD_FLAG_SEND_TRIM)); |
| 503 | nbd_cmd(req) = NBD_CMD_TRIM; |
| 504 | } else |
| 505 | nbd_cmd(req) = NBD_CMD_WRITE; |
Paul Clements | 2f01250 | 2012-10-04 17:16:15 -0700 | [diff] [blame] | 506 | if (nbd->flags & NBD_FLAG_READ_ONLY) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 507 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 508 | "Write on read-only\n"); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 509 | goto error_out; |
| 510 | } |
| 511 | } |
| 512 | |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 513 | if (req->cmd_flags & REQ_FLUSH) { |
| 514 | BUG_ON(unlikely(blk_rq_sectors(req))); |
| 515 | nbd_cmd(req) = NBD_CMD_FLUSH; |
| 516 | } |
| 517 | |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 518 | req->errors = 0; |
| 519 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 520 | mutex_lock(&nbd->tx_lock); |
| 521 | if (unlikely(!nbd->sock)) { |
| 522 | mutex_unlock(&nbd->tx_lock); |
| 523 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 524 | "Attempted send on closed socket\n"); |
Pavel Machek | 15746fc | 2009-04-02 16:58:42 -0700 | [diff] [blame] | 525 | goto error_out; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 528 | nbd->active_req = req; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 529 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 530 | if (nbd_send_req(nbd, req) != 0) { |
| 531 | dev_err(disk_to_dev(nbd->disk), "Request send failed\n"); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 532 | req->errors++; |
| 533 | nbd_end_request(req); |
| 534 | } else { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 535 | spin_lock(&nbd->queue_lock); |
Chetan Loke | 01ff5db | 2012-07-31 08:47:13 +0200 | [diff] [blame] | 536 | list_add_tail(&req->queuelist, &nbd->queue_head); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 537 | spin_unlock(&nbd->queue_lock); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 540 | nbd->active_req = NULL; |
| 541 | mutex_unlock(&nbd->tx_lock); |
| 542 | wake_up_all(&nbd->active_wq); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 543 | |
| 544 | return; |
| 545 | |
| 546 | error_out: |
| 547 | req->errors++; |
| 548 | nbd_end_request(req); |
| 549 | } |
| 550 | |
| 551 | static int nbd_thread(void *data) |
| 552 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 553 | struct nbd_device *nbd = data; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 554 | struct request *req; |
| 555 | |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 556 | set_user_nice(current, MIN_NICE); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 557 | while (!kthread_should_stop() || !list_empty(&nbd->waiting_queue)) { |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 558 | /* wait for something to do */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 559 | wait_event_interruptible(nbd->waiting_wq, |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 560 | kthread_should_stop() || |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 561 | !list_empty(&nbd->waiting_queue)); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 562 | |
| 563 | /* extract request */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 564 | if (list_empty(&nbd->waiting_queue)) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 565 | continue; |
| 566 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 567 | spin_lock_irq(&nbd->queue_lock); |
| 568 | req = list_entry(nbd->waiting_queue.next, struct request, |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 569 | queuelist); |
| 570 | list_del_init(&req->queuelist); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 571 | spin_unlock_irq(&nbd->queue_lock); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 572 | |
| 573 | /* handle request */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 574 | nbd_handle_req(nbd, req); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 575 | } |
| 576 | return 0; |
| 577 | } |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* |
| 580 | * We always wait for result of write, for now. It would be nice to make it optional |
| 581 | * in future |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 582 | * if ((rq_data_dir(req) == WRITE) && (nbd->flags & NBD_WRITE_NOCHK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); } |
| 584 | */ |
| 585 | |
Pavel Machek | 15746fc | 2009-04-02 16:58:42 -0700 | [diff] [blame] | 586 | static void do_nbd_request(struct request_queue *q) |
Alex Elder | 398eb08 | 2013-02-27 17:05:28 -0800 | [diff] [blame] | 587 | __releases(q->queue_lock) __acquires(q->queue_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | { |
| 589 | struct request *req; |
| 590 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 591 | while ((req = blk_fetch_request(q)) != NULL) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 592 | struct nbd_device *nbd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 594 | spin_unlock_irq(q->queue_lock); |
| 595 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 596 | dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%x)\n", |
| 597 | req->rq_disk->disk_name, req, req->cmd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 599 | nbd = req->rq_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 601 | BUG_ON(nbd->magic != NBD_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 603 | if (unlikely(!nbd->sock)) { |
| 604 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 605 | "Attempted send on closed socket\n"); |
Paul Clements | 4d48a54 | 2009-02-11 13:04:45 -0800 | [diff] [blame] | 606 | req->errors++; |
| 607 | nbd_end_request(req); |
| 608 | spin_lock_irq(q->queue_lock); |
| 609 | continue; |
| 610 | } |
| 611 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 612 | spin_lock_irq(&nbd->queue_lock); |
| 613 | list_add_tail(&req->queuelist, &nbd->waiting_queue); |
| 614 | spin_unlock_irq(&nbd->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 616 | wake_up(&nbd->waiting_wq); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | spin_lock_irq(q->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 622 | /* Must be called with tx_lock held */ |
| 623 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 624 | static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 625 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | switch (cmd) { |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 628 | case NBD_DISCONNECT: { |
| 629 | struct request sreq; |
| 630 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 631 | dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n"); |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 632 | if (!nbd->sock) |
| 633 | return -EINVAL; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 634 | |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 635 | mutex_unlock(&nbd->tx_lock); |
| 636 | fsync_bdev(bdev); |
| 637 | mutex_lock(&nbd->tx_lock); |
FUJITA Tomonori | 4f54eec | 2008-04-29 09:54:37 +0200 | [diff] [blame] | 638 | blk_rq_init(NULL, &sreq); |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 639 | sreq.cmd_type = REQ_TYPE_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | nbd_cmd(&sreq) = NBD_CMD_DISC; |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 641 | |
| 642 | /* Check again after getting mutex back. */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 643 | if (!nbd->sock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | return -EINVAL; |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 645 | |
Paul Clements | c378f70 | 2013-07-03 15:09:04 -0700 | [diff] [blame] | 646 | nbd->disconnect = 1; |
| 647 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 648 | nbd_send_req(nbd, &sreq); |
Paul Clements | c378f70 | 2013-07-03 15:09:04 -0700 | [diff] [blame] | 649 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 652 | case NBD_CLEAR_SOCK: { |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 653 | struct socket *sock = nbd->sock; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 654 | nbd->sock = NULL; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 655 | nbd_clear_que(nbd); |
| 656 | BUG_ON(!list_empty(&nbd->queue_head)); |
Paul Clements | fded4e0 | 2012-09-17 14:09:02 -0700 | [diff] [blame] | 657 | BUG_ON(!list_empty(&nbd->waiting_queue)); |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 658 | kill_bdev(bdev); |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 659 | if (sock) |
| 660 | sockfd_put(sock); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | case NBD_SET_SOCK: { |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 665 | struct socket *sock; |
| 666 | int err; |
| 667 | if (nbd->sock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return -EBUSY; |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 669 | sock = sockfd_lookup(arg, &err); |
| 670 | if (sock) { |
| 671 | nbd->sock = sock; |
| 672 | if (max_part > 0) |
| 673 | bdev->bd_invalidated = 1; |
| 674 | nbd->disconnect = 0; /* we're connected now */ |
| 675 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 677 | return -EINVAL; |
| 678 | } |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | case NBD_SET_BLKSIZE: |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 681 | nbd->blksize = arg; |
| 682 | nbd->bytesize &= ~(nbd->blksize-1); |
| 683 | bdev->bd_inode->i_size = nbd->bytesize; |
| 684 | set_blocksize(bdev, nbd->blksize); |
| 685 | set_capacity(nbd->disk, nbd->bytesize >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | case NBD_SET_SIZE: |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 689 | nbd->bytesize = arg & ~(nbd->blksize-1); |
| 690 | bdev->bd_inode->i_size = nbd->bytesize; |
| 691 | set_blocksize(bdev, nbd->blksize); |
| 692 | set_capacity(nbd->disk, nbd->bytesize >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 694 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 695 | case NBD_SET_TIMEOUT: |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 696 | nbd->xmit_timeout = arg * HZ; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 697 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 698 | |
Paul Clements | 2f01250 | 2012-10-04 17:16:15 -0700 | [diff] [blame] | 699 | case NBD_SET_FLAGS: |
| 700 | nbd->flags = arg; |
| 701 | return 0; |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | case NBD_SET_SIZE_BLOCKS: |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 704 | nbd->bytesize = ((u64) arg) * nbd->blksize; |
| 705 | bdev->bd_inode->i_size = nbd->bytesize; |
| 706 | set_blocksize(bdev, nbd->blksize); |
| 707 | set_capacity(nbd->disk, nbd->bytesize >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 709 | |
| 710 | case NBD_DO_IT: { |
| 711 | struct task_struct *thread; |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 712 | struct socket *sock; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 713 | int error; |
| 714 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 715 | if (nbd->pid) |
Pavel Machek | c91192d | 2009-01-15 13:51:03 -0800 | [diff] [blame] | 716 | return -EBUSY; |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 717 | if (!nbd->sock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return -EINVAL; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 719 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 720 | mutex_unlock(&nbd->tx_lock); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 721 | |
Paolo Bonzini | a83e814 | 2013-02-27 17:05:26 -0800 | [diff] [blame] | 722 | if (nbd->flags & NBD_FLAG_READ_ONLY) |
| 723 | set_device_ro(bdev, true); |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 724 | if (nbd->flags & NBD_FLAG_SEND_TRIM) |
| 725 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 726 | nbd->disk->queue); |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 727 | if (nbd->flags & NBD_FLAG_SEND_FLUSH) |
| 728 | blk_queue_flush(nbd->disk->queue, REQ_FLUSH); |
| 729 | else |
| 730 | blk_queue_flush(nbd->disk->queue, 0); |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 731 | |
Markus Pargmann | d06df60 | 2015-04-02 10:11:36 +0200 | [diff] [blame] | 732 | thread = kthread_run(nbd_thread, nbd, "%s", |
| 733 | nbd->disk->disk_name); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 734 | if (IS_ERR(thread)) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 735 | mutex_lock(&nbd->tx_lock); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 736 | return PTR_ERR(thread); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 737 | } |
Markus Pargmann | d06df60 | 2015-04-02 10:11:36 +0200 | [diff] [blame] | 738 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 739 | error = nbd_do_it(nbd); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 740 | kthread_stop(thread); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 741 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 742 | mutex_lock(&nbd->tx_lock); |
WANG Cong | 8496304 | 2007-05-09 02:33:36 -0700 | [diff] [blame] | 743 | if (error) |
| 744 | return error; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 745 | sock_shutdown(nbd, 0); |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 746 | sock = nbd->sock; |
| 747 | nbd->sock = NULL; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 748 | nbd_clear_que(nbd); |
| 749 | dev_warn(disk_to_dev(nbd->disk), "queue cleared\n"); |
Paolo Bonzini | 3a2d63f8 | 2013-02-27 17:05:25 -0800 | [diff] [blame] | 750 | kill_bdev(bdev); |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 751 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); |
Paolo Bonzini | a83e814 | 2013-02-27 17:05:26 -0800 | [diff] [blame] | 752 | set_device_ro(bdev, false); |
Al Viro | e251157 | 2014-03-05 20:41:36 -0500 | [diff] [blame] | 753 | if (sock) |
| 754 | sockfd_put(sock); |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 755 | nbd->flags = 0; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 756 | nbd->bytesize = 0; |
Al Viro | a8cdc30 | 2008-03-02 09:33:33 -0500 | [diff] [blame] | 757 | bdev->bd_inode->i_size = 0; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 758 | set_capacity(nbd->disk, 0); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 759 | if (max_part > 0) |
Al Viro | a8cdc30 | 2008-03-02 09:33:33 -0500 | [diff] [blame] | 760 | ioctl_by_bdev(bdev, BLKRRPART, 0); |
Paul Clements | c378f70 | 2013-07-03 15:09:04 -0700 | [diff] [blame] | 761 | if (nbd->disconnect) /* user requested, ignore socket errors */ |
| 762 | return 0; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 763 | return nbd->harderror; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | case NBD_CLEAR_QUE: |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 767 | /* |
| 768 | * This is for compatibility only. The queue is always cleared |
| 769 | * by NBD_DO_IT or NBD_CLEAR_SOCK. |
| 770 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | case NBD_PRINT_DEBUG: |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 774 | dev_info(disk_to_dev(nbd->disk), |
WANG Cong | 5eedf54 | 2011-08-19 14:48:28 +0200 | [diff] [blame] | 775 | "next = %p, prev = %p, head = %p\n", |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 776 | nbd->queue_head.next, nbd->queue_head.prev, |
| 777 | &nbd->queue_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return 0; |
| 779 | } |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 780 | return -ENOTTY; |
| 781 | } |
| 782 | |
| 783 | static int nbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 784 | unsigned int cmd, unsigned long arg) |
| 785 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 786 | struct nbd_device *nbd = bdev->bd_disk->private_data; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 787 | int error; |
| 788 | |
| 789 | if (!capable(CAP_SYS_ADMIN)) |
| 790 | return -EPERM; |
| 791 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 792 | BUG_ON(nbd->magic != NBD_MAGIC); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 793 | |
| 794 | /* Anyone capable of this syscall can do *real bad* things */ |
| 795 | dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n", |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 796 | nbd->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 797 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 798 | mutex_lock(&nbd->tx_lock); |
| 799 | error = __nbd_ioctl(bdev, nbd, cmd, arg); |
| 800 | mutex_unlock(&nbd->tx_lock); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 801 | |
| 802 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 805 | static const struct block_device_operations nbd_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
| 807 | .owner = THIS_MODULE, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 808 | .ioctl = nbd_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | }; |
| 810 | |
| 811 | /* |
| 812 | * And here should be modules and kernel interface |
| 813 | * (Just smiley confuses emacs :-) |
| 814 | */ |
| 815 | |
| 816 | static int __init nbd_init(void) |
| 817 | { |
| 818 | int err = -ENOMEM; |
| 819 | int i; |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 820 | int part_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
Adrian Bunk | 5b7b18c | 2006-03-25 03:07:04 -0800 | [diff] [blame] | 822 | BUILD_BUG_ON(sizeof(struct nbd_request) != 28); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 824 | if (max_part < 0) { |
WANG Cong | 7742ce4 | 2011-08-19 14:48:28 +0200 | [diff] [blame] | 825 | printk(KERN_ERR "nbd: max_part must be >= 0\n"); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 826 | return -EINVAL; |
| 827 | } |
| 828 | |
| 829 | part_shift = 0; |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 830 | if (max_part > 0) { |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 831 | part_shift = fls(max_part); |
| 832 | |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 833 | /* |
| 834 | * Adjust max_part according to part_shift as it is exported |
| 835 | * to user space so that user can know the max number of |
| 836 | * partition kernel should be able to manage. |
| 837 | * |
| 838 | * Note that -1 is required because partition 0 is reserved |
| 839 | * for the whole disk. |
| 840 | */ |
| 841 | max_part = (1UL << part_shift) - 1; |
| 842 | } |
| 843 | |
Namhyung Kim | 3b27108 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 844 | if ((1UL << part_shift) > DISK_MAX_PARTS) |
| 845 | return -EINVAL; |
| 846 | |
| 847 | if (nbds_max > 1UL << (MINORBITS - part_shift)) |
| 848 | return -EINVAL; |
| 849 | |
Sudip Mukherjee | ff6b809 | 2015-01-27 18:08:22 +0530 | [diff] [blame] | 850 | nbd_dev = kcalloc(nbds_max, sizeof(*nbd_dev), GFP_KERNEL); |
| 851 | if (!nbd_dev) |
| 852 | return -ENOMEM; |
| 853 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 854 | for (i = 0; i < nbds_max; i++) { |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 855 | struct gendisk *disk = alloc_disk(1 << part_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (!disk) |
| 857 | goto out; |
| 858 | nbd_dev[i].disk = disk; |
| 859 | /* |
| 860 | * The new linux 2.5 block layer implementation requires |
| 861 | * every gendisk to have its very own request_queue struct. |
| 862 | * These structs are big so we dynamically allocate them. |
| 863 | */ |
| 864 | disk->queue = blk_init_queue(do_nbd_request, &nbd_lock); |
| 865 | if (!disk->queue) { |
| 866 | put_disk(disk); |
| 867 | goto out; |
| 868 | } |
Jens Axboe | 31dcfab | 2008-10-31 10:06:37 +0100 | [diff] [blame] | 869 | /* |
| 870 | * Tell the block layer that we are not a rotational device |
| 871 | */ |
| 872 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 873 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue); |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 874 | disk->queue->limits.discard_granularity = 512; |
| 875 | disk->queue->limits.max_discard_sectors = UINT_MAX; |
| 876 | disk->queue->limits.discard_zeroes_data = 0; |
Michal Belczyk | 078be02 | 2013-04-30 15:28:28 -0700 | [diff] [blame] | 877 | blk_queue_max_hw_sectors(disk->queue, 65536); |
| 878 | disk->queue->limits.max_sectors = 256; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | if (register_blkdev(NBD_MAJOR, "nbd")) { |
| 882 | err = -EIO; |
| 883 | goto out; |
| 884 | } |
| 885 | |
| 886 | printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR); |
| 887 | dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags); |
| 888 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 889 | for (i = 0; i < nbds_max; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | struct gendisk *disk = nbd_dev[i].disk; |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 891 | nbd_dev[i].magic = NBD_MAGIC; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 892 | INIT_LIST_HEAD(&nbd_dev[i].waiting_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | spin_lock_init(&nbd_dev[i].queue_lock); |
| 894 | INIT_LIST_HEAD(&nbd_dev[i].queue_head); |
Ingo Molnar | 82d4dc5 | 2006-03-23 03:00:38 -0800 | [diff] [blame] | 895 | mutex_init(&nbd_dev[i].tx_lock); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 896 | init_waitqueue_head(&nbd_dev[i].active_wq); |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 897 | init_waitqueue_head(&nbd_dev[i].waiting_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | nbd_dev[i].blksize = 1024; |
Paul Clements | 4b86a87 | 2007-10-16 23:27:36 -0700 | [diff] [blame] | 899 | nbd_dev[i].bytesize = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | disk->major = NBD_MAJOR; |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 901 | disk->first_minor = i << part_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | disk->fops = &nbd_fops; |
| 903 | disk->private_data = &nbd_dev[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | sprintf(disk->disk_name, "nbd%d", i); |
Paul Clements | 4b86a87 | 2007-10-16 23:27:36 -0700 | [diff] [blame] | 905 | set_capacity(disk, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | add_disk(disk); |
| 907 | } |
| 908 | |
| 909 | return 0; |
| 910 | out: |
| 911 | while (i--) { |
| 912 | blk_cleanup_queue(nbd_dev[i].disk->queue); |
| 913 | put_disk(nbd_dev[i].disk); |
| 914 | } |
Sven Wegener | f3944d6 | 2008-08-20 14:09:07 -0700 | [diff] [blame] | 915 | kfree(nbd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | return err; |
| 917 | } |
| 918 | |
| 919 | static void __exit nbd_cleanup(void) |
| 920 | { |
| 921 | int i; |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 922 | for (i = 0; i < nbds_max; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | struct gendisk *disk = nbd_dev[i].disk; |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 924 | nbd_dev[i].magic = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | if (disk) { |
| 926 | del_gendisk(disk); |
| 927 | blk_cleanup_queue(disk->queue); |
| 928 | put_disk(disk); |
| 929 | } |
| 930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | unregister_blkdev(NBD_MAJOR, "nbd"); |
Sven Wegener | f3944d6 | 2008-08-20 14:09:07 -0700 | [diff] [blame] | 932 | kfree(nbd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR); |
| 934 | } |
| 935 | |
| 936 | module_init(nbd_init); |
| 937 | module_exit(nbd_cleanup); |
| 938 | |
| 939 | MODULE_DESCRIPTION("Network Block Device"); |
| 940 | MODULE_LICENSE("GPL"); |
| 941 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 942 | module_param(nbds_max, int, 0444); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 943 | MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)"); |
| 944 | module_param(max_part, int, 0444); |
| 945 | MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | #ifndef NDEBUG |
| 947 | module_param(debugflags, int, 0644); |
| 948 | MODULE_PARM_DESC(debugflags, "flags for controlling debug output"); |
| 949 | #endif |