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