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