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> |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 37 | #include <linux/blk-mq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 39 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/types.h> |
| 41 | |
| 42 | #include <linux/nbd.h> |
| 43 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 44 | static DEFINE_IDR(nbd_index_idr); |
| 45 | static DEFINE_MUTEX(nbd_index_mutex); |
| 46 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 47 | struct nbd_sock { |
| 48 | struct socket *sock; |
| 49 | struct mutex tx_lock; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 50 | struct request *pending; |
| 51 | int sent; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 52 | bool dead; |
| 53 | int fallback_index; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 54 | }; |
| 55 | |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 56 | #define NBD_TIMEDOUT 0 |
| 57 | #define NBD_DISCONNECT_REQUESTED 1 |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 58 | #define NBD_DISCONNECTED 2 |
| 59 | #define NBD_RUNNING 3 |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 60 | |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 61 | struct nbd_device { |
Markus Pargmann | 22d109c | 2015-08-17 08:20:09 +0200 | [diff] [blame] | 62 | u32 flags; |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 63 | unsigned long runtime_flags; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 64 | struct nbd_sock **socks; |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 65 | int magic; |
| 66 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 67 | struct blk_mq_tag_set tag_set; |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 68 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 69 | struct mutex config_lock; |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 70 | struct gendisk *disk; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 71 | int num_connections; |
| 72 | atomic_t recv_threads; |
| 73 | wait_queue_head_t recv_wq; |
Josef Bacik | ef77b51 | 2016-12-02 16:19:12 -0500 | [diff] [blame] | 74 | loff_t blksize; |
Markus Pargmann | b9c495b | 2015-04-02 10:11:37 +0200 | [diff] [blame] | 75 | loff_t bytesize; |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 76 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 77 | struct task_struct *task_recv; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 78 | struct task_struct *task_setup; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 79 | |
| 80 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 81 | struct dentry *dbg_dir; |
| 82 | #endif |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 85 | struct nbd_cmd { |
| 86 | struct nbd_device *nbd; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 87 | int index; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 88 | struct completion send_complete; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 91 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 92 | static struct dentry *nbd_dbg_dir; |
| 93 | #endif |
| 94 | |
| 95 | #define nbd_name(nbd) ((nbd)->disk->disk_name) |
| 96 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 97 | #define NBD_MAGIC 0x68797548 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Ingo van Lil | 9c7a416 | 2006-07-01 04:36:36 -0700 | [diff] [blame] | 99 | static unsigned int nbds_max = 16; |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 100 | static int max_part; |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 101 | static struct workqueue_struct *recv_workqueue; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 102 | static int part_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 104 | static int nbd_dev_dbg_init(struct nbd_device *nbd); |
| 105 | static void nbd_dev_dbg_close(struct nbd_device *nbd); |
| 106 | |
| 107 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 108 | static inline struct device *nbd_to_dev(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 110 | return disk_to_dev(nbd->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 113 | static bool nbd_is_connected(struct nbd_device *nbd) |
| 114 | { |
| 115 | return !!nbd->task_recv; |
| 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | static const char *nbdcmd_to_ascii(int cmd) |
| 119 | { |
| 120 | switch (cmd) { |
| 121 | case NBD_CMD_READ: return "read"; |
| 122 | case NBD_CMD_WRITE: return "write"; |
| 123 | case NBD_CMD_DISC: return "disconnect"; |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 124 | case NBD_CMD_FLUSH: return "flush"; |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 125 | case NBD_CMD_TRIM: return "trim/discard"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | return "invalid"; |
| 128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 130 | static void nbd_mark_nsock_dead(struct nbd_sock *nsock) |
| 131 | { |
| 132 | if (!nsock->dead) |
| 133 | kernel_sock_shutdown(nsock->sock, SHUT_RDWR); |
| 134 | nsock->dead = true; |
| 135 | nsock->pending = NULL; |
| 136 | nsock->sent = 0; |
| 137 | } |
| 138 | |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 139 | static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev) |
| 140 | { |
Ratna Manoj Bolla | abbbdf1 | 2017-03-24 14:08:29 -0400 | [diff] [blame] | 141 | if (bdev->bd_openers <= 1) |
| 142 | bd_set_size(bdev, 0); |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 143 | set_capacity(nbd->disk, 0); |
| 144 | kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev) |
| 150 | { |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 151 | blk_queue_logical_block_size(nbd->disk->queue, nbd->blksize); |
| 152 | blk_queue_physical_block_size(nbd->disk->queue, nbd->blksize); |
| 153 | bd_set_size(bdev, nbd->bytesize); |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 154 | set_capacity(nbd->disk, nbd->bytesize >> 9); |
| 155 | kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); |
| 156 | } |
| 157 | |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 158 | static void nbd_size_set(struct nbd_device *nbd, struct block_device *bdev, |
Josef Bacik | ef77b51 | 2016-12-02 16:19:12 -0500 | [diff] [blame] | 159 | loff_t blocksize, loff_t nr_blocks) |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 160 | { |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 161 | nbd->blksize = blocksize; |
Josef Bacik | ef77b51 | 2016-12-02 16:19:12 -0500 | [diff] [blame] | 162 | nbd->bytesize = blocksize * nr_blocks; |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 163 | if (nbd_is_connected(nbd)) |
| 164 | nbd_size_update(nbd, bdev); |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 167 | static void nbd_end_request(struct nbd_cmd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 169 | struct nbd_device *nbd = cmd->nbd; |
| 170 | struct request *req = blk_mq_rq_from_pdu(cmd); |
Kiyoshi Ueda | 097c94a | 2007-12-11 17:44:06 -0500 | [diff] [blame] | 171 | int error = req->errors ? -EIO : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 173 | dev_dbg(nbd_to_dev(nbd), "request %p: %s\n", cmd, |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 174 | error ? "failed" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 176 | blk_mq_complete_request(req, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Markus Pargmann | e018e75 | 2015-04-02 10:11:39 +0200 | [diff] [blame] | 179 | /* |
| 180 | * Forcibly shutdown the socket causing all listeners to error |
| 181 | */ |
Markus Pargmann | 36e47be | 2015-08-17 08:20:01 +0200 | [diff] [blame] | 182 | static void sock_shutdown(struct nbd_device *nbd) |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 183 | { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 184 | int i; |
Josef Bacik | c261189 | 2016-09-08 12:33:38 -0700 | [diff] [blame] | 185 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 186 | if (nbd->num_connections == 0) |
Markus Pargmann | 260bbce | 2015-08-17 08:20:02 +0200 | [diff] [blame] | 187 | return; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 188 | if (test_and_set_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) |
| 189 | return; |
| 190 | |
| 191 | for (i = 0; i < nbd->num_connections; i++) { |
| 192 | struct nbd_sock *nsock = nbd->socks[i]; |
| 193 | mutex_lock(&nsock->tx_lock); |
| 194 | kernel_sock_shutdown(nsock->sock, SHUT_RDWR); |
| 195 | mutex_unlock(&nsock->tx_lock); |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 196 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 197 | dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n"); |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 200 | static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req, |
| 201 | bool reserved) |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 202 | { |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 203 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req); |
| 204 | struct nbd_device *nbd = cmd->nbd; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 205 | |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 206 | if (nbd->num_connections > 1) { |
| 207 | dev_err_ratelimited(nbd_to_dev(nbd), |
| 208 | "Connection timed out, retrying\n"); |
| 209 | mutex_lock(&nbd->config_lock); |
| 210 | /* |
| 211 | * Hooray we have more connections, requeue this IO, the submit |
| 212 | * path will put it on a real connection. |
| 213 | */ |
| 214 | if (nbd->socks && nbd->num_connections > 1) { |
| 215 | if (cmd->index < nbd->num_connections) { |
| 216 | struct nbd_sock *nsock = |
| 217 | nbd->socks[cmd->index]; |
| 218 | mutex_lock(&nsock->tx_lock); |
| 219 | nbd_mark_nsock_dead(nsock); |
| 220 | mutex_unlock(&nsock->tx_lock); |
| 221 | } |
| 222 | mutex_unlock(&nbd->config_lock); |
| 223 | blk_mq_requeue_request(req, true); |
| 224 | return BLK_EH_NOT_HANDLED; |
| 225 | } |
| 226 | mutex_unlock(&nbd->config_lock); |
| 227 | } else { |
| 228 | dev_err_ratelimited(nbd_to_dev(nbd), |
| 229 | "Connection timed out\n"); |
| 230 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 231 | set_bit(NBD_TIMEDOUT, &nbd->runtime_flags); |
Josef Bacik | c103b4d | 2017-03-24 14:08:27 -0400 | [diff] [blame] | 232 | req->errors = -EIO; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 233 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 234 | mutex_lock(&nbd->config_lock); |
| 235 | sock_shutdown(nbd); |
| 236 | mutex_unlock(&nbd->config_lock); |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 237 | return BLK_EH_HANDLED; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | /* |
| 241 | * Send or receive packet. |
| 242 | */ |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 243 | static int sock_xmit(struct nbd_device *nbd, int index, int send, |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 244 | struct iov_iter *iter, int msg_flags, int *sent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 246 | struct socket *sock = nbd->socks[index]->sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | int result; |
| 248 | struct msghdr msg; |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 249 | unsigned long pflags = current->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 251 | if (unlikely(!sock)) { |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 252 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 253 | "Attempted %s on closed socket in sock_xmit\n", |
| 254 | (send ? "send" : "recv")); |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 255 | return -EINVAL; |
| 256 | } |
| 257 | |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 258 | msg.msg_iter = *iter; |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 259 | |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 260 | current->flags |= PF_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | do { |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 262 | sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | msg.msg_name = NULL; |
| 264 | msg.msg_namelen = 0; |
| 265 | msg.msg_control = NULL; |
| 266 | msg.msg_controllen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | msg.msg_flags = msg_flags | MSG_NOSIGNAL; |
| 268 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 269 | if (send) |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 270 | result = sock_sendmsg(sock, &msg); |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 271 | else |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 272 | result = sock_recvmsg(sock, &msg, msg.msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | if (result <= 0) { |
| 275 | if (result == 0) |
| 276 | result = -EPIPE; /* short read */ |
| 277 | break; |
| 278 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 279 | if (sent) |
| 280 | *sent += result; |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 281 | } while (msg_data_left(&msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 283 | tsk_restore_flags(current, pflags, PF_MEMALLOC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | return result; |
| 286 | } |
| 287 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 288 | /* always call with the tx_lock held */ |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 289 | static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 291 | struct request *req = blk_mq_rq_from_pdu(cmd); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 292 | struct nbd_sock *nsock = nbd->socks[index]; |
Josef Bacik | d61b7f9 | 2017-01-19 16:08:49 -0500 | [diff] [blame] | 293 | int result; |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 294 | struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)}; |
| 295 | struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)}; |
| 296 | struct iov_iter from; |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 297 | unsigned long size = blk_rq_bytes(req); |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 298 | struct bio *bio; |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 299 | u32 type; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 300 | u32 tag = blk_mq_unique_tag(req); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 301 | int sent = nsock->sent, skip = 0; |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 302 | |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 303 | iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request)); |
| 304 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 305 | switch (req_op(req)) { |
| 306 | case REQ_OP_DISCARD: |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 307 | type = NBD_CMD_TRIM; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 308 | break; |
| 309 | case REQ_OP_FLUSH: |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 310 | type = NBD_CMD_FLUSH; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 311 | break; |
| 312 | case REQ_OP_WRITE: |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 313 | type = NBD_CMD_WRITE; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 314 | break; |
| 315 | case REQ_OP_READ: |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 316 | type = NBD_CMD_READ; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 317 | break; |
| 318 | default: |
| 319 | return -EIO; |
| 320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Christoph Hellwig | 09fc54cc | 2017-01-31 16:57:28 +0100 | [diff] [blame] | 322 | if (rq_data_dir(req) == WRITE && |
| 323 | (nbd->flags & NBD_FLAG_READ_ONLY)) { |
| 324 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 325 | "Write on read-only\n"); |
| 326 | return -EIO; |
| 327 | } |
| 328 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 329 | /* We did a partial send previously, and we at least sent the whole |
| 330 | * request struct, so just go and send the rest of the pages in the |
| 331 | * request. |
| 332 | */ |
| 333 | if (sent) { |
| 334 | if (sent >= sizeof(request)) { |
| 335 | skip = sent - sizeof(request); |
| 336 | goto send_pages; |
| 337 | } |
| 338 | iov_iter_advance(&from, sent); |
| 339 | } |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 340 | cmd->index = index; |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 341 | request.type = htonl(type); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 342 | if (type != NBD_CMD_FLUSH) { |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 343 | request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9); |
| 344 | request.len = htonl(size); |
| 345 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 346 | memcpy(request.handle, &tag, sizeof(tag)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 348 | dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n", |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 349 | cmd, nbdcmd_to_ascii(type), |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 350 | (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req)); |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 351 | result = sock_xmit(nbd, index, 1, &from, |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 352 | (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | if (result <= 0) { |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 354 | if (result == -ERESTARTSYS) { |
| 355 | /* If we havne't sent anything we can just return BUSY, |
| 356 | * however if we have sent something we need to make |
| 357 | * sure we only allow this req to be sent until we are |
| 358 | * completely done. |
| 359 | */ |
| 360 | if (sent) { |
| 361 | nsock->pending = req; |
| 362 | nsock->sent = sent; |
| 363 | } |
| 364 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 365 | } |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 366 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 367 | "Send control failed (result %d)\n", result); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 368 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 370 | send_pages: |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 371 | if (type != NBD_CMD_WRITE) |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 372 | goto out; |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 373 | |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 374 | bio = req->bio; |
| 375 | while (bio) { |
| 376 | struct bio *next = bio->bi_next; |
| 377 | struct bvec_iter iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 378 | struct bio_vec bvec; |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 379 | |
| 380 | bio_for_each_segment(bvec, bio, iter) { |
| 381 | bool is_last = !next && bio_iter_last(bvec, iter); |
Josef Bacik | d61b7f9 | 2017-01-19 16:08:49 -0500 | [diff] [blame] | 382 | int flags = is_last ? 0 : MSG_MORE; |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 383 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 384 | dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n", |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 385 | cmd, bvec.bv_len); |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 386 | iov_iter_bvec(&from, ITER_BVEC | WRITE, |
| 387 | &bvec, 1, bvec.bv_len); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 388 | if (skip) { |
| 389 | if (skip >= iov_iter_count(&from)) { |
| 390 | skip -= iov_iter_count(&from); |
| 391 | continue; |
| 392 | } |
| 393 | iov_iter_advance(&from, skip); |
| 394 | skip = 0; |
| 395 | } |
| 396 | result = sock_xmit(nbd, index, 1, &from, flags, &sent); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 397 | if (result <= 0) { |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 398 | if (result == -ERESTARTSYS) { |
| 399 | /* We've already sent the header, we |
| 400 | * have no choice but to set pending and |
| 401 | * return BUSY. |
| 402 | */ |
| 403 | nsock->pending = req; |
| 404 | nsock->sent = sent; |
| 405 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 406 | } |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 407 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 408 | "Send data failed (result %d)\n", |
| 409 | result); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 410 | return -EAGAIN; |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 411 | } |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 412 | /* |
| 413 | * The completion might already have come in, |
| 414 | * so break for the last one instead of letting |
| 415 | * the iterator do it. This prevents use-after-free |
| 416 | * of the bio. |
| 417 | */ |
| 418 | if (is_last) |
| 419 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 421 | bio = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 423 | out: |
| 424 | nsock->pending = NULL; |
| 425 | nsock->sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 429 | static int nbd_disconnected(struct nbd_device *nbd) |
| 430 | { |
| 431 | return test_bit(NBD_DISCONNECTED, &nbd->runtime_flags) || |
| 432 | test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags); |
| 433 | } |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | /* NULL returned = something went wrong, inform userspace */ |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 436 | static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
| 438 | int result; |
| 439 | struct nbd_reply reply; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 440 | struct nbd_cmd *cmd; |
| 441 | struct request *req = NULL; |
| 442 | u16 hwq; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 443 | u32 tag; |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 444 | struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)}; |
| 445 | struct iov_iter to; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | reply.magic = 0; |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 448 | iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply)); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 449 | result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | if (result <= 0) { |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 451 | if (!nbd_disconnected(nbd)) |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 452 | dev_err(disk_to_dev(nbd->disk), |
| 453 | "Receive control failed (result %d)\n", result); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 454 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 456 | |
| 457 | if (ntohl(reply.magic) != NBD_REPLY_MAGIC) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 458 | dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n", |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 459 | (unsigned long)ntohl(reply.magic)); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 460 | return ERR_PTR(-EPROTO); |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 463 | memcpy(&tag, reply.handle, sizeof(u32)); |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 464 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 465 | hwq = blk_mq_unique_tag_to_hwq(tag); |
| 466 | if (hwq < nbd->tag_set.nr_hw_queues) |
| 467 | req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq], |
| 468 | blk_mq_unique_tag_to_tag(tag)); |
| 469 | if (!req || !blk_mq_request_started(req)) { |
| 470 | dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n", |
| 471 | tag, req); |
| 472 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 474 | cmd = blk_mq_rq_to_pdu(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | if (ntohl(reply.error)) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 476 | 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] | 477 | ntohl(reply.error)); |
Josef Bacik | c103b4d | 2017-03-24 14:08:27 -0400 | [diff] [blame] | 478 | req->errors = -EIO; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 479 | return cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 482 | dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", cmd); |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 483 | if (rq_data_dir(req) != WRITE) { |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 484 | struct req_iterator iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 485 | struct bio_vec bvec; |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 486 | |
| 487 | rq_for_each_segment(bvec, req, iter) { |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 488 | iov_iter_bvec(&to, ITER_BVEC | READ, |
| 489 | &bvec, 1, bvec.bv_len); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 490 | result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 491 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 492 | 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] | 493 | result); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 494 | /* |
| 495 | * If we've disconnected or we only have 1 |
| 496 | * connection then we need to make sure we |
| 497 | * complete this request, otherwise error out |
| 498 | * and let the timeout stuff handle resubmitting |
| 499 | * this request onto another connection. |
| 500 | */ |
| 501 | if (nbd_disconnected(nbd) || |
| 502 | nbd->num_connections <= 1) { |
| 503 | req->errors = -EIO; |
| 504 | return cmd; |
| 505 | } |
| 506 | return ERR_PTR(-EIO); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 507 | } |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 508 | dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n", |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 509 | cmd, bvec.bv_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 511 | } else { |
| 512 | /* See the comment in nbd_queue_rq. */ |
| 513 | wait_for_completion(&cmd->send_complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 515 | return cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 518 | static ssize_t pid_show(struct device *dev, |
| 519 | struct device_attribute *attr, char *buf) |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 520 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 521 | struct gendisk *disk = dev_to_disk(dev); |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 522 | struct nbd_device *nbd = (struct nbd_device *)disk->private_data; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 523 | |
Markus Pargmann | 6521d39 | 2015-08-17 08:20:05 +0200 | [diff] [blame] | 524 | return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv)); |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 527 | static struct device_attribute pid_attr = { |
Parag Warudkar | 01e8ef1 | 2008-10-18 20:28:50 -0700 | [diff] [blame] | 528 | .attr = { .name = "pid", .mode = S_IRUGO}, |
Paul Clements | 6b39bb6 | 2006-12-06 20:40:53 -0800 | [diff] [blame] | 529 | .show = pid_show, |
| 530 | }; |
| 531 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 532 | struct recv_thread_args { |
| 533 | struct work_struct work; |
| 534 | struct nbd_device *nbd; |
| 535 | int index; |
| 536 | }; |
| 537 | |
| 538 | static void recv_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 540 | struct recv_thread_args *args = container_of(work, |
| 541 | struct recv_thread_args, |
| 542 | work); |
| 543 | struct nbd_device *nbd = args->nbd; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 544 | struct nbd_cmd *cmd; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 545 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 547 | BUG_ON(nbd->magic != NBD_MAGIC); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 548 | while (1) { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 549 | cmd = nbd_read_stat(nbd, args->index); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 550 | if (IS_ERR(cmd)) { |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 551 | struct nbd_sock *nsock = nbd->socks[args->index]; |
| 552 | |
| 553 | mutex_lock(&nsock->tx_lock); |
| 554 | nbd_mark_nsock_dead(nsock); |
| 555 | mutex_unlock(&nsock->tx_lock); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 556 | ret = PTR_ERR(cmd); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 557 | break; |
| 558 | } |
| 559 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 560 | nbd_end_request(cmd); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 561 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 562 | atomic_dec(&nbd->recv_threads); |
| 563 | wake_up(&nbd->recv_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 566 | static void nbd_clear_req(struct request *req, void *data, bool reserved) |
| 567 | { |
| 568 | struct nbd_cmd *cmd; |
| 569 | |
| 570 | if (!blk_mq_request_started(req)) |
| 571 | return; |
| 572 | cmd = blk_mq_rq_to_pdu(req); |
Josef Bacik | c103b4d | 2017-03-24 14:08:27 -0400 | [diff] [blame] | 573 | req->errors = -EIO; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 574 | nbd_end_request(cmd); |
| 575 | } |
| 576 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 577 | static void nbd_clear_que(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 579 | BUG_ON(nbd->magic != NBD_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 581 | blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL); |
Markus Pargmann | e78273c | 2015-08-17 08:20:04 +0200 | [diff] [blame] | 582 | dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 585 | static int find_fallback(struct nbd_device *nbd, int index) |
| 586 | { |
| 587 | int new_index = -1; |
| 588 | struct nbd_sock *nsock = nbd->socks[index]; |
| 589 | int fallback = nsock->fallback_index; |
| 590 | |
| 591 | if (test_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) |
| 592 | return new_index; |
| 593 | |
| 594 | if (nbd->num_connections <= 1) { |
| 595 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 596 | "Attempted send on invalid socket\n"); |
| 597 | return new_index; |
| 598 | } |
| 599 | |
| 600 | if (fallback >= 0 && fallback < nbd->num_connections && |
| 601 | !nbd->socks[fallback]->dead) |
| 602 | return fallback; |
| 603 | |
| 604 | if (nsock->fallback_index < 0 || |
| 605 | nsock->fallback_index >= nbd->num_connections || |
| 606 | nbd->socks[nsock->fallback_index]->dead) { |
| 607 | int i; |
| 608 | for (i = 0; i < nbd->num_connections; i++) { |
| 609 | if (i == index) |
| 610 | continue; |
| 611 | if (!nbd->socks[i]->dead) { |
| 612 | new_index = i; |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | nsock->fallback_index = new_index; |
| 617 | if (new_index < 0) { |
| 618 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 619 | "Dead connection, failed to find a fallback\n"); |
| 620 | return new_index; |
| 621 | } |
| 622 | } |
| 623 | new_index = nsock->fallback_index; |
| 624 | return new_index; |
| 625 | } |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 626 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 627 | static int nbd_handle_cmd(struct nbd_cmd *cmd, int index) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 628 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 629 | struct request *req = blk_mq_rq_from_pdu(cmd); |
| 630 | struct nbd_device *nbd = cmd->nbd; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 631 | struct nbd_sock *nsock; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 632 | int ret; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 633 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 634 | if (index >= nbd->num_connections) { |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 635 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 636 | "Attempted send on invalid socket\n"); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 637 | return -EINVAL; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 638 | } |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 639 | req->errors = 0; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 640 | again: |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 641 | nsock = nbd->socks[index]; |
| 642 | mutex_lock(&nsock->tx_lock); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 643 | if (nsock->dead) { |
| 644 | index = find_fallback(nbd, index); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 645 | mutex_unlock(&nsock->tx_lock); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 646 | if (index < 0) |
| 647 | return -EIO; |
| 648 | goto again; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 651 | /* Handle the case that we have a pending request that was partially |
| 652 | * transmitted that _has_ to be serviced first. We need to call requeue |
| 653 | * here so that it gets put _after_ the request that is already on the |
| 654 | * dispatch list. |
| 655 | */ |
| 656 | if (unlikely(nsock->pending && nsock->pending != req)) { |
| 657 | blk_mq_requeue_request(req, true); |
| 658 | ret = 0; |
| 659 | goto out; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 660 | } |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 661 | /* |
| 662 | * Some failures are related to the link going down, so anything that |
| 663 | * returns EAGAIN can be retried on a different socket. |
| 664 | */ |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 665 | ret = nbd_send_cmd(nbd, cmd, index); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 666 | if (ret == -EAGAIN) { |
| 667 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 668 | "Request send failed trying another connection\n"); |
| 669 | nbd_mark_nsock_dead(nsock); |
| 670 | mutex_unlock(&nsock->tx_lock); |
| 671 | goto again; |
| 672 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 673 | out: |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 674 | mutex_unlock(&nsock->tx_lock); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 675 | return ret; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 678 | static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 679 | const struct blk_mq_queue_data *bd) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 680 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 681 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 682 | int ret; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 683 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 684 | /* |
| 685 | * Since we look at the bio's to send the request over the network we |
| 686 | * need to make sure the completion work doesn't mark this request done |
| 687 | * before we are done doing our send. This keeps us from dereferencing |
| 688 | * freed data if we have particularly fast completions (ie we get the |
| 689 | * completion before we exit sock_xmit on the last bvec) or in the case |
| 690 | * that the server is misbehaving (or there was an error) before we're |
| 691 | * done sending everything over the wire. |
| 692 | */ |
| 693 | init_completion(&cmd->send_complete); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 694 | blk_mq_start_request(bd->rq); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 695 | |
| 696 | /* We can be called directly from the user space process, which means we |
| 697 | * could possibly have signals pending so our sendmsg will fail. In |
| 698 | * this case we need to return that we are busy, otherwise error out as |
| 699 | * appropriate. |
| 700 | */ |
| 701 | ret = nbd_handle_cmd(cmd, hctx->queue_num); |
| 702 | if (ret < 0) |
| 703 | ret = BLK_MQ_RQ_QUEUE_ERROR; |
| 704 | if (!ret) |
| 705 | ret = BLK_MQ_RQ_QUEUE_OK; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 706 | complete(&cmd->send_complete); |
| 707 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 708 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 711 | static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev, |
| 712 | unsigned long arg) |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 713 | { |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 714 | struct socket *sock; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 715 | struct nbd_sock **socks; |
| 716 | struct nbd_sock *nsock; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 717 | int err; |
| 718 | |
| 719 | sock = sockfd_lookup(arg, &err); |
| 720 | if (!sock) |
| 721 | return err; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 722 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 723 | if (!nbd->task_setup) |
| 724 | nbd->task_setup = current; |
| 725 | if (nbd->task_setup != current) { |
| 726 | dev_err(disk_to_dev(nbd->disk), |
| 727 | "Device being setup by another task"); |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 728 | sockfd_put(sock); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 729 | return -EINVAL; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 732 | socks = krealloc(nbd->socks, (nbd->num_connections + 1) * |
| 733 | sizeof(struct nbd_sock *), GFP_KERNEL); |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 734 | if (!socks) { |
| 735 | sockfd_put(sock); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 736 | return -ENOMEM; |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 737 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 738 | nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL); |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 739 | if (!nsock) { |
| 740 | sockfd_put(sock); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 741 | return -ENOMEM; |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 742 | } |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 743 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 744 | nbd->socks = socks; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 745 | |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame^] | 746 | nsock->fallback_index = -1; |
| 747 | nsock->dead = false; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 748 | mutex_init(&nsock->tx_lock); |
| 749 | nsock->sock = sock; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 750 | nsock->pending = NULL; |
| 751 | nsock->sent = 0; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 752 | socks[nbd->num_connections++] = nsock; |
| 753 | |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 754 | if (max_part) |
| 755 | bdev->bd_invalidated = 1; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 756 | return 0; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 757 | } |
| 758 | |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 759 | /* Reset all properties of an NBD device */ |
| 760 | static void nbd_reset(struct nbd_device *nbd) |
| 761 | { |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 762 | nbd->runtime_flags = 0; |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 763 | nbd->blksize = 1024; |
| 764 | nbd->bytesize = 0; |
| 765 | set_capacity(nbd->disk, 0); |
| 766 | nbd->flags = 0; |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 767 | nbd->tag_set.timeout = 0; |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 768 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | static void nbd_bdev_reset(struct block_device *bdev) |
| 772 | { |
Ratna Manoj Bolla | abbbdf1 | 2017-03-24 14:08:29 -0400 | [diff] [blame] | 773 | if (bdev->bd_openers > 1) |
| 774 | return; |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 775 | set_device_ro(bdev, false); |
| 776 | bdev->bd_inode->i_size = 0; |
| 777 | if (max_part > 0) { |
| 778 | blkdev_reread_part(bdev); |
| 779 | bdev->bd_invalidated = 1; |
| 780 | } |
| 781 | } |
| 782 | |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 783 | static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev) |
| 784 | { |
| 785 | if (nbd->flags & NBD_FLAG_READ_ONLY) |
| 786 | set_device_ro(bdev, true); |
| 787 | if (nbd->flags & NBD_FLAG_SEND_TRIM) |
| 788 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); |
| 789 | if (nbd->flags & NBD_FLAG_SEND_FLUSH) |
Jens Axboe | aafb1ee | 2016-03-30 10:10:53 -0600 | [diff] [blame] | 790 | blk_queue_write_cache(nbd->disk->queue, true, false); |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 791 | else |
Jens Axboe | aafb1ee | 2016-03-30 10:10:53 -0600 | [diff] [blame] | 792 | blk_queue_write_cache(nbd->disk->queue, false, false); |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 793 | } |
| 794 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 795 | static void send_disconnects(struct nbd_device *nbd) |
| 796 | { |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 797 | struct nbd_request request = { |
| 798 | .magic = htonl(NBD_REQUEST_MAGIC), |
| 799 | .type = htonl(NBD_CMD_DISC), |
| 800 | }; |
| 801 | struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)}; |
| 802 | struct iov_iter from; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 803 | int i, ret; |
| 804 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 805 | for (i = 0; i < nbd->num_connections; i++) { |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 806 | iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request)); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 807 | ret = sock_xmit(nbd, i, 1, &from, 0, NULL); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 808 | if (ret <= 0) |
| 809 | dev_err(disk_to_dev(nbd->disk), |
| 810 | "Send disconnect failed %d\n", ret); |
| 811 | } |
| 812 | } |
| 813 | |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 814 | static int nbd_disconnect(struct nbd_device *nbd, struct block_device *bdev) |
| 815 | { |
| 816 | dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n"); |
| 817 | if (!nbd->socks) |
| 818 | return -EINVAL; |
| 819 | |
| 820 | mutex_unlock(&nbd->config_lock); |
| 821 | fsync_bdev(bdev); |
| 822 | mutex_lock(&nbd->config_lock); |
| 823 | |
| 824 | /* Check again after getting mutex back. */ |
| 825 | if (!nbd->socks) |
| 826 | return -EINVAL; |
| 827 | |
| 828 | if (!test_and_set_bit(NBD_DISCONNECT_REQUESTED, |
| 829 | &nbd->runtime_flags)) |
| 830 | send_disconnects(nbd); |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev) |
| 835 | { |
| 836 | sock_shutdown(nbd); |
| 837 | nbd_clear_que(nbd); |
Ratna Manoj Bolla | abbbdf1 | 2017-03-24 14:08:29 -0400 | [diff] [blame] | 838 | |
| 839 | __invalidate_device(bdev, true); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 840 | nbd_bdev_reset(bdev); |
| 841 | /* |
| 842 | * We want to give the run thread a chance to wait for everybody |
| 843 | * to clean up and then do it's own cleanup. |
| 844 | */ |
| 845 | if (!test_bit(NBD_RUNNING, &nbd->runtime_flags) && |
| 846 | nbd->num_connections) { |
| 847 | int i; |
| 848 | |
Josef Bacik | 6a8a215 | 2017-03-01 11:47:22 -0500 | [diff] [blame] | 849 | for (i = 0; i < nbd->num_connections; i++) { |
| 850 | sockfd_put(nbd->socks[i]->sock); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 851 | kfree(nbd->socks[i]); |
Josef Bacik | 6a8a215 | 2017-03-01 11:47:22 -0500 | [diff] [blame] | 852 | } |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 853 | kfree(nbd->socks); |
| 854 | nbd->socks = NULL; |
| 855 | nbd->num_connections = 0; |
| 856 | } |
| 857 | nbd->task_setup = NULL; |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static int nbd_start_device(struct nbd_device *nbd, struct block_device *bdev) |
| 863 | { |
| 864 | struct recv_thread_args *args; |
| 865 | int num_connections = nbd->num_connections; |
| 866 | int error = 0, i; |
| 867 | |
| 868 | if (nbd->task_recv) |
| 869 | return -EBUSY; |
| 870 | if (!nbd->socks) |
| 871 | return -EINVAL; |
| 872 | if (num_connections > 1 && |
| 873 | !(nbd->flags & NBD_FLAG_CAN_MULTI_CONN)) { |
| 874 | dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n"); |
| 875 | error = -EINVAL; |
| 876 | goto out_err; |
| 877 | } |
| 878 | |
| 879 | set_bit(NBD_RUNNING, &nbd->runtime_flags); |
| 880 | blk_mq_update_nr_hw_queues(&nbd->tag_set, nbd->num_connections); |
| 881 | args = kcalloc(num_connections, sizeof(*args), GFP_KERNEL); |
| 882 | if (!args) { |
| 883 | error = -ENOMEM; |
| 884 | goto out_err; |
| 885 | } |
| 886 | nbd->task_recv = current; |
| 887 | mutex_unlock(&nbd->config_lock); |
| 888 | |
| 889 | nbd_parse_flags(nbd, bdev); |
| 890 | |
| 891 | error = device_create_file(disk_to_dev(nbd->disk), &pid_attr); |
| 892 | if (error) { |
| 893 | dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n"); |
| 894 | goto out_recv; |
| 895 | } |
| 896 | |
| 897 | nbd_size_update(nbd, bdev); |
| 898 | |
| 899 | nbd_dev_dbg_init(nbd); |
| 900 | for (i = 0; i < num_connections; i++) { |
| 901 | sk_set_memalloc(nbd->socks[i]->sock->sk); |
| 902 | atomic_inc(&nbd->recv_threads); |
| 903 | INIT_WORK(&args[i].work, recv_work); |
| 904 | args[i].nbd = nbd; |
| 905 | args[i].index = i; |
| 906 | queue_work(recv_workqueue, &args[i].work); |
| 907 | } |
| 908 | wait_event_interruptible(nbd->recv_wq, |
| 909 | atomic_read(&nbd->recv_threads) == 0); |
| 910 | for (i = 0; i < num_connections; i++) |
| 911 | flush_work(&args[i].work); |
| 912 | nbd_dev_dbg_close(nbd); |
| 913 | nbd_size_clear(nbd, bdev); |
| 914 | device_remove_file(disk_to_dev(nbd->disk), &pid_attr); |
| 915 | out_recv: |
| 916 | mutex_lock(&nbd->config_lock); |
| 917 | nbd->task_recv = NULL; |
| 918 | out_err: |
| 919 | clear_bit(NBD_RUNNING, &nbd->runtime_flags); |
| 920 | nbd_clear_sock(nbd, bdev); |
| 921 | |
| 922 | /* user requested, ignore socket errors */ |
| 923 | if (test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags)) |
| 924 | error = 0; |
| 925 | if (test_bit(NBD_TIMEDOUT, &nbd->runtime_flags)) |
| 926 | error = -ETIMEDOUT; |
| 927 | |
| 928 | nbd_reset(nbd); |
| 929 | return error; |
| 930 | } |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 931 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 932 | /* Must be called with config_lock held */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 933 | static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 934 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | switch (cmd) { |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 937 | case NBD_DISCONNECT: |
| 938 | return nbd_disconnect(nbd, bdev); |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 939 | case NBD_CLEAR_SOCK: |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 940 | return nbd_clear_sock(nbd, bdev); |
| 941 | case NBD_SET_SOCK: |
| 942 | return nbd_add_socket(nbd, bdev, arg); |
| 943 | case NBD_SET_BLKSIZE: |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 944 | nbd_size_set(nbd, bdev, arg, |
| 945 | div_s64(nbd->bytesize, arg)); |
| 946 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | case NBD_SET_SIZE: |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 948 | nbd_size_set(nbd, bdev, nbd->blksize, |
| 949 | div_s64(arg, nbd->blksize)); |
| 950 | return 0; |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 951 | case NBD_SET_SIZE_BLOCKS: |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 952 | nbd_size_set(nbd, bdev, nbd->blksize, arg); |
| 953 | return 0; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 954 | case NBD_SET_TIMEOUT: |
Josef Bacik | f858685 | 2017-03-24 14:08:28 -0400 | [diff] [blame] | 955 | if (arg) { |
| 956 | nbd->tag_set.timeout = arg * HZ; |
| 957 | blk_queue_rq_timeout(nbd->disk->queue, arg * HZ); |
| 958 | } |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 959 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 960 | |
Paul Clements | 2f01250 | 2012-10-04 17:16:15 -0700 | [diff] [blame] | 961 | case NBD_SET_FLAGS: |
| 962 | nbd->flags = arg; |
| 963 | return 0; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 964 | case NBD_DO_IT: |
| 965 | return nbd_start_device(nbd, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | case NBD_CLEAR_QUE: |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 967 | /* |
| 968 | * This is for compatibility only. The queue is always cleared |
| 969 | * by NBD_DO_IT or NBD_CLEAR_SOCK. |
| 970 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | return 0; |
| 972 | case NBD_PRINT_DEBUG: |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 973 | /* |
| 974 | * For compatibility only, we no longer keep a list of |
| 975 | * outstanding requests. |
| 976 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | return 0; |
| 978 | } |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 979 | return -ENOTTY; |
| 980 | } |
| 981 | |
| 982 | static int nbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 983 | unsigned int cmd, unsigned long arg) |
| 984 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 985 | struct nbd_device *nbd = bdev->bd_disk->private_data; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 986 | int error; |
| 987 | |
| 988 | if (!capable(CAP_SYS_ADMIN)) |
| 989 | return -EPERM; |
| 990 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 991 | BUG_ON(nbd->magic != NBD_MAGIC); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 992 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 993 | mutex_lock(&nbd->config_lock); |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 994 | error = __nbd_ioctl(bdev, nbd, cmd, arg); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 995 | mutex_unlock(&nbd->config_lock); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 996 | |
| 997 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1000 | static const struct block_device_operations nbd_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | { |
| 1002 | .owner = THIS_MODULE, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1003 | .ioctl = nbd_ioctl, |
Al Viro | 263a3df | 2016-01-07 10:04:37 -0500 | [diff] [blame] | 1004 | .compat_ioctl = nbd_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | }; |
| 1006 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1007 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 1008 | |
| 1009 | static int nbd_dbg_tasks_show(struct seq_file *s, void *unused) |
| 1010 | { |
| 1011 | struct nbd_device *nbd = s->private; |
| 1012 | |
| 1013 | if (nbd->task_recv) |
| 1014 | seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv)); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | static int nbd_dbg_tasks_open(struct inode *inode, struct file *file) |
| 1020 | { |
| 1021 | return single_open(file, nbd_dbg_tasks_show, inode->i_private); |
| 1022 | } |
| 1023 | |
| 1024 | static const struct file_operations nbd_dbg_tasks_ops = { |
| 1025 | .open = nbd_dbg_tasks_open, |
| 1026 | .read = seq_read, |
| 1027 | .llseek = seq_lseek, |
| 1028 | .release = single_release, |
| 1029 | }; |
| 1030 | |
| 1031 | static int nbd_dbg_flags_show(struct seq_file *s, void *unused) |
| 1032 | { |
| 1033 | struct nbd_device *nbd = s->private; |
| 1034 | u32 flags = nbd->flags; |
| 1035 | |
| 1036 | seq_printf(s, "Hex: 0x%08x\n\n", flags); |
| 1037 | |
| 1038 | seq_puts(s, "Known flags:\n"); |
| 1039 | |
| 1040 | if (flags & NBD_FLAG_HAS_FLAGS) |
| 1041 | seq_puts(s, "NBD_FLAG_HAS_FLAGS\n"); |
| 1042 | if (flags & NBD_FLAG_READ_ONLY) |
| 1043 | seq_puts(s, "NBD_FLAG_READ_ONLY\n"); |
| 1044 | if (flags & NBD_FLAG_SEND_FLUSH) |
| 1045 | seq_puts(s, "NBD_FLAG_SEND_FLUSH\n"); |
| 1046 | if (flags & NBD_FLAG_SEND_TRIM) |
| 1047 | seq_puts(s, "NBD_FLAG_SEND_TRIM\n"); |
| 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | static int nbd_dbg_flags_open(struct inode *inode, struct file *file) |
| 1053 | { |
| 1054 | return single_open(file, nbd_dbg_flags_show, inode->i_private); |
| 1055 | } |
| 1056 | |
| 1057 | static const struct file_operations nbd_dbg_flags_ops = { |
| 1058 | .open = nbd_dbg_flags_open, |
| 1059 | .read = seq_read, |
| 1060 | .llseek = seq_lseek, |
| 1061 | .release = single_release, |
| 1062 | }; |
| 1063 | |
| 1064 | static int nbd_dev_dbg_init(struct nbd_device *nbd) |
| 1065 | { |
| 1066 | struct dentry *dir; |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 1067 | |
| 1068 | if (!nbd_dbg_dir) |
| 1069 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1070 | |
| 1071 | dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir); |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 1072 | if (!dir) { |
| 1073 | dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n", |
| 1074 | nbd_name(nbd)); |
| 1075 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1076 | } |
| 1077 | nbd->dbg_dir = dir; |
| 1078 | |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 1079 | debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops); |
| 1080 | debugfs_create_u64("size_bytes", 0444, dir, &nbd->bytesize); |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 1081 | debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout); |
Josef Bacik | ef77b51 | 2016-12-02 16:19:12 -0500 | [diff] [blame] | 1082 | debugfs_create_u64("blocksize", 0444, dir, &nbd->blksize); |
Josef Bacik | d366a0f | 2016-06-08 10:32:10 -0400 | [diff] [blame] | 1083 | debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1084 | |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | static void nbd_dev_dbg_close(struct nbd_device *nbd) |
| 1089 | { |
| 1090 | debugfs_remove_recursive(nbd->dbg_dir); |
| 1091 | } |
| 1092 | |
| 1093 | static int nbd_dbg_init(void) |
| 1094 | { |
| 1095 | struct dentry *dbg_dir; |
| 1096 | |
| 1097 | dbg_dir = debugfs_create_dir("nbd", NULL); |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 1098 | if (!dbg_dir) |
| 1099 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1100 | |
| 1101 | nbd_dbg_dir = dbg_dir; |
| 1102 | |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | static void nbd_dbg_close(void) |
| 1107 | { |
| 1108 | debugfs_remove_recursive(nbd_dbg_dir); |
| 1109 | } |
| 1110 | |
| 1111 | #else /* IS_ENABLED(CONFIG_DEBUG_FS) */ |
| 1112 | |
| 1113 | static int nbd_dev_dbg_init(struct nbd_device *nbd) |
| 1114 | { |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | static void nbd_dev_dbg_close(struct nbd_device *nbd) |
| 1119 | { |
| 1120 | } |
| 1121 | |
| 1122 | static int nbd_dbg_init(void) |
| 1123 | { |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static void nbd_dbg_close(void) |
| 1128 | { |
| 1129 | } |
| 1130 | |
| 1131 | #endif |
| 1132 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1133 | static int nbd_init_request(void *data, struct request *rq, |
| 1134 | unsigned int hctx_idx, unsigned int request_idx, |
| 1135 | unsigned int numa_node) |
| 1136 | { |
| 1137 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1138 | cmd->nbd = data; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
| 1141 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 1142 | static const struct blk_mq_ops nbd_mq_ops = { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1143 | .queue_rq = nbd_queue_rq, |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1144 | .init_request = nbd_init_request, |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 1145 | .timeout = nbd_xmit_timeout, |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1146 | }; |
| 1147 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1148 | static void nbd_dev_remove(struct nbd_device *nbd) |
| 1149 | { |
| 1150 | struct gendisk *disk = nbd->disk; |
| 1151 | nbd->magic = 0; |
| 1152 | if (disk) { |
| 1153 | del_gendisk(disk); |
| 1154 | blk_cleanup_queue(disk->queue); |
| 1155 | blk_mq_free_tag_set(&nbd->tag_set); |
| 1156 | put_disk(disk); |
| 1157 | } |
| 1158 | kfree(nbd); |
| 1159 | } |
| 1160 | |
| 1161 | static int nbd_dev_add(int index) |
| 1162 | { |
| 1163 | struct nbd_device *nbd; |
| 1164 | struct gendisk *disk; |
| 1165 | struct request_queue *q; |
| 1166 | int err = -ENOMEM; |
| 1167 | |
| 1168 | nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL); |
| 1169 | if (!nbd) |
| 1170 | goto out; |
| 1171 | |
| 1172 | disk = alloc_disk(1 << part_shift); |
| 1173 | if (!disk) |
| 1174 | goto out_free_nbd; |
| 1175 | |
| 1176 | if (index >= 0) { |
| 1177 | err = idr_alloc(&nbd_index_idr, nbd, index, index + 1, |
| 1178 | GFP_KERNEL); |
| 1179 | if (err == -ENOSPC) |
| 1180 | err = -EEXIST; |
| 1181 | } else { |
| 1182 | err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL); |
| 1183 | if (err >= 0) |
| 1184 | index = err; |
| 1185 | } |
| 1186 | if (err < 0) |
| 1187 | goto out_free_disk; |
| 1188 | |
| 1189 | nbd->disk = disk; |
| 1190 | nbd->tag_set.ops = &nbd_mq_ops; |
| 1191 | nbd->tag_set.nr_hw_queues = 1; |
| 1192 | nbd->tag_set.queue_depth = 128; |
| 1193 | nbd->tag_set.numa_node = NUMA_NO_NODE; |
| 1194 | nbd->tag_set.cmd_size = sizeof(struct nbd_cmd); |
| 1195 | nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | |
| 1196 | BLK_MQ_F_SG_MERGE | BLK_MQ_F_BLOCKING; |
| 1197 | nbd->tag_set.driver_data = nbd; |
| 1198 | |
| 1199 | err = blk_mq_alloc_tag_set(&nbd->tag_set); |
| 1200 | if (err) |
| 1201 | goto out_free_idr; |
| 1202 | |
| 1203 | q = blk_mq_init_queue(&nbd->tag_set); |
| 1204 | if (IS_ERR(q)) { |
| 1205 | err = PTR_ERR(q); |
| 1206 | goto out_free_tags; |
| 1207 | } |
| 1208 | disk->queue = q; |
| 1209 | |
| 1210 | /* |
| 1211 | * Tell the block layer that we are not a rotational device |
| 1212 | */ |
| 1213 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue); |
| 1214 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, disk->queue); |
| 1215 | disk->queue->limits.discard_granularity = 512; |
| 1216 | blk_queue_max_discard_sectors(disk->queue, UINT_MAX); |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1217 | blk_queue_max_hw_sectors(disk->queue, 65536); |
| 1218 | disk->queue->limits.max_sectors = 256; |
| 1219 | |
| 1220 | nbd->magic = NBD_MAGIC; |
| 1221 | mutex_init(&nbd->config_lock); |
| 1222 | disk->major = NBD_MAJOR; |
| 1223 | disk->first_minor = index << part_shift; |
| 1224 | disk->fops = &nbd_fops; |
| 1225 | disk->private_data = nbd; |
| 1226 | sprintf(disk->disk_name, "nbd%d", index); |
| 1227 | init_waitqueue_head(&nbd->recv_wq); |
| 1228 | nbd_reset(nbd); |
| 1229 | add_disk(disk); |
| 1230 | return index; |
| 1231 | |
| 1232 | out_free_tags: |
| 1233 | blk_mq_free_tag_set(&nbd->tag_set); |
| 1234 | out_free_idr: |
| 1235 | idr_remove(&nbd_index_idr, index); |
| 1236 | out_free_disk: |
| 1237 | put_disk(disk); |
| 1238 | out_free_nbd: |
| 1239 | kfree(nbd); |
| 1240 | out: |
| 1241 | return err; |
| 1242 | } |
| 1243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | /* |
| 1245 | * And here should be modules and kernel interface |
| 1246 | * (Just smiley confuses emacs :-) |
| 1247 | */ |
| 1248 | |
| 1249 | static int __init nbd_init(void) |
| 1250 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | int i; |
| 1252 | |
Adrian Bunk | 5b7b18c | 2006-03-25 03:07:04 -0800 | [diff] [blame] | 1253 | BUILD_BUG_ON(sizeof(struct nbd_request) != 28); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1255 | if (max_part < 0) { |
WANG Cong | 7742ce4 | 2011-08-19 14:48:28 +0200 | [diff] [blame] | 1256 | printk(KERN_ERR "nbd: max_part must be >= 0\n"); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1257 | return -EINVAL; |
| 1258 | } |
| 1259 | |
| 1260 | part_shift = 0; |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1261 | if (max_part > 0) { |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1262 | part_shift = fls(max_part); |
| 1263 | |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1264 | /* |
| 1265 | * Adjust max_part according to part_shift as it is exported |
| 1266 | * to user space so that user can know the max number of |
| 1267 | * partition kernel should be able to manage. |
| 1268 | * |
| 1269 | * Note that -1 is required because partition 0 is reserved |
| 1270 | * for the whole disk. |
| 1271 | */ |
| 1272 | max_part = (1UL << part_shift) - 1; |
| 1273 | } |
| 1274 | |
Namhyung Kim | 3b27108 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1275 | if ((1UL << part_shift) > DISK_MAX_PARTS) |
| 1276 | return -EINVAL; |
| 1277 | |
| 1278 | if (nbds_max > 1UL << (MINORBITS - part_shift)) |
| 1279 | return -EINVAL; |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 1280 | recv_workqueue = alloc_workqueue("knbd-recv", |
| 1281 | WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); |
| 1282 | if (!recv_workqueue) |
| 1283 | return -ENOMEM; |
Namhyung Kim | 3b27108 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 1284 | |
Josef Bacik | 6330a2d | 2017-02-15 16:49:48 -0500 | [diff] [blame] | 1285 | if (register_blkdev(NBD_MAJOR, "nbd")) { |
| 1286 | destroy_workqueue(recv_workqueue); |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1287 | return -EIO; |
Josef Bacik | 6330a2d | 2017-02-15 16:49:48 -0500 | [diff] [blame] | 1288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1290 | nbd_dbg_init(); |
| 1291 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1292 | mutex_lock(&nbd_index_mutex); |
| 1293 | for (i = 0; i < nbds_max; i++) |
| 1294 | nbd_dev_add(i); |
| 1295 | mutex_unlock(&nbd_index_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | return 0; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | static int nbd_exit_cb(int id, void *ptr, void *data) |
| 1300 | { |
| 1301 | struct nbd_device *nbd = ptr; |
| 1302 | nbd_dev_remove(nbd); |
| 1303 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | static void __exit nbd_cleanup(void) |
| 1307 | { |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1308 | nbd_dbg_close(); |
| 1309 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1310 | idr_for_each(&nbd_index_idr, &nbd_exit_cb, NULL); |
| 1311 | idr_destroy(&nbd_index_idr); |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 1312 | destroy_workqueue(recv_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | unregister_blkdev(NBD_MAJOR, "nbd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | module_init(nbd_init); |
| 1317 | module_exit(nbd_cleanup); |
| 1318 | |
| 1319 | MODULE_DESCRIPTION("Network Block Device"); |
| 1320 | MODULE_LICENSE("GPL"); |
| 1321 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 1322 | module_param(nbds_max, int, 0444); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 1323 | MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)"); |
| 1324 | module_param(max_part, int, 0444); |
| 1325 | MODULE_PARM_DESC(max_part, "number of partitions per device (default: 0)"); |