Thomas Gleixner | eb1fe3b | 2019-05-24 12:03:47 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Network block device - make block devices work over TCP |
| 4 | * |
| 5 | * Note that you can not swap over this thing, yet. Seems to work but |
| 6 | * deadlocks sometimes - you can not swap over TCP in general. |
| 7 | * |
Pavel Machek | a253129 | 2010-07-18 14:27:13 +0200 | [diff] [blame] | 8 | * Copyright 1997-2000, 2008 Pavel Machek <pavel@ucw.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Parts copyright 2001 Steven Whitehouse <steve@chygwyn.com> |
| 10 | * |
Pavel Machek | dbf492d | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 11 | * (part of code stolen from loop.c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/major.h> |
| 15 | |
| 16 | #include <linux/blkdev.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/sched.h> |
Vlastimil Babka | f108304 | 2017-05-08 15:59:53 -0700 | [diff] [blame] | 20 | #include <linux/sched/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 43 | #include <linux/nbd-netlink.h> |
| 44 | #include <net/genetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Matt Mullins | ea10672 | 2019-04-26 11:49:48 -0700 | [diff] [blame] | 46 | #define CREATE_TRACE_POINTS |
| 47 | #include <trace/events/nbd.h> |
| 48 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 49 | static DEFINE_IDR(nbd_index_idr); |
| 50 | static DEFINE_MUTEX(nbd_index_mutex); |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 51 | static int nbd_total_devices = 0; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 52 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 53 | struct nbd_sock { |
| 54 | struct socket *sock; |
| 55 | struct mutex tx_lock; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 56 | struct request *pending; |
| 57 | int sent; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 58 | bool dead; |
| 59 | int fallback_index; |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 60 | int cookie; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 63 | struct recv_thread_args { |
| 64 | struct work_struct work; |
| 65 | struct nbd_device *nbd; |
| 66 | int index; |
| 67 | }; |
| 68 | |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 69 | struct link_dead_args { |
| 70 | struct work_struct work; |
| 71 | int index; |
| 72 | }; |
| 73 | |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 74 | #define NBD_TIMEDOUT 0 |
| 75 | #define NBD_DISCONNECT_REQUESTED 1 |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 76 | #define NBD_DISCONNECTED 2 |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 77 | #define NBD_HAS_PID_FILE 3 |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 78 | #define NBD_HAS_CONFIG_REF 4 |
| 79 | #define NBD_BOUND 5 |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 80 | #define NBD_DESTROY_ON_DISCONNECT 6 |
Doron Roberts-Kedes | 08ba91e | 2018-06-15 14:05:32 -0700 | [diff] [blame] | 81 | #define NBD_DISCONNECT_ON_CLOSE 7 |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 82 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 83 | struct nbd_config { |
Markus Pargmann | 22d109c | 2015-08-17 08:20:09 +0200 | [diff] [blame] | 84 | u32 flags; |
Josef Bacik | 9b4a6ba | 2016-09-08 12:33:39 -0700 | [diff] [blame] | 85 | unsigned long runtime_flags; |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 86 | u64 dead_conn_timeout; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 87 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 88 | struct nbd_sock **socks; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 89 | int num_connections; |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 90 | atomic_t live_connections; |
| 91 | wait_queue_head_t conn_wait; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 92 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 93 | atomic_t recv_threads; |
| 94 | wait_queue_head_t recv_wq; |
Josef Bacik | ef77b51 | 2016-12-02 16:19:12 -0500 | [diff] [blame] | 95 | loff_t blksize; |
Markus Pargmann | b9c495b | 2015-04-02 10:11:37 +0200 | [diff] [blame] | 96 | loff_t bytesize; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 97 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 98 | struct dentry *dbg_dir; |
| 99 | #endif |
Markus Pargmann | 13e71d6 | 2015-04-02 10:11:35 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 102 | struct nbd_device { |
| 103 | struct blk_mq_tag_set tag_set; |
| 104 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 105 | int index; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 106 | refcount_t config_refs; |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 107 | refcount_t refs; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 108 | struct nbd_config *config; |
| 109 | struct mutex config_lock; |
| 110 | struct gendisk *disk; |
| 111 | |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 112 | struct list_head list; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 113 | struct task_struct *task_recv; |
| 114 | struct task_struct *task_setup; |
| 115 | }; |
| 116 | |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 117 | #define NBD_CMD_REQUEUED 1 |
| 118 | |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 119 | struct nbd_cmd { |
| 120 | struct nbd_device *nbd; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 121 | struct mutex lock; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 122 | int index; |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 123 | int cookie; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 124 | blk_status_t status; |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 125 | unsigned long flags; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 126 | u32 cmd_cookie; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 129 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 130 | static struct dentry *nbd_dbg_dir; |
| 131 | #endif |
| 132 | |
| 133 | #define nbd_name(nbd) ((nbd)->disk->disk_name) |
| 134 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 135 | #define NBD_MAGIC 0x68797548 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Xiubo Li | 553768d | 2019-05-29 15:16:05 -0500 | [diff] [blame] | 137 | #define NBD_DEF_BLKSIZE 1024 |
| 138 | |
Ingo van Lil | 9c7a416 | 2006-07-01 04:36:36 -0700 | [diff] [blame] | 139 | static unsigned int nbds_max = 16; |
Josef Bacik | 7a8362a | 2017-08-14 18:56:16 +0000 | [diff] [blame] | 140 | static int max_part = 16; |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 141 | static struct workqueue_struct *recv_workqueue; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 142 | static int part_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 144 | static int nbd_dev_dbg_init(struct nbd_device *nbd); |
| 145 | static void nbd_dev_dbg_close(struct nbd_device *nbd); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 146 | static void nbd_config_put(struct nbd_device *nbd); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 147 | static void nbd_connect_reply(struct genl_info *info, int index); |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 148 | static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info); |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 149 | static void nbd_dead_link_work(struct work_struct *work); |
Doron Roberts-Kedes | 08ba91e | 2018-06-15 14:05:32 -0700 | [diff] [blame] | 150 | static void nbd_disconnect_and_put(struct nbd_device *nbd); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 151 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 152 | static inline struct device *nbd_to_dev(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 154 | return disk_to_dev(nbd->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 157 | static void nbd_requeue_cmd(struct nbd_cmd *cmd) |
| 158 | { |
| 159 | struct request *req = blk_mq_rq_from_pdu(cmd); |
| 160 | |
| 161 | if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags)) |
| 162 | blk_mq_requeue_request(req, true); |
| 163 | } |
| 164 | |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 165 | #define NBD_COOKIE_BITS 32 |
| 166 | |
| 167 | static u64 nbd_cmd_handle(struct nbd_cmd *cmd) |
| 168 | { |
| 169 | struct request *req = blk_mq_rq_from_pdu(cmd); |
| 170 | u32 tag = blk_mq_unique_tag(req); |
| 171 | u64 cookie = cmd->cmd_cookie; |
| 172 | |
| 173 | return (cookie << NBD_COOKIE_BITS) | tag; |
| 174 | } |
| 175 | |
| 176 | static u32 nbd_handle_to_tag(u64 handle) |
| 177 | { |
| 178 | return (u32)handle; |
| 179 | } |
| 180 | |
| 181 | static u32 nbd_handle_to_cookie(u64 handle) |
| 182 | { |
| 183 | return (u32)(handle >> NBD_COOKIE_BITS); |
| 184 | } |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | static const char *nbdcmd_to_ascii(int cmd) |
| 187 | { |
| 188 | switch (cmd) { |
| 189 | case NBD_CMD_READ: return "read"; |
| 190 | case NBD_CMD_WRITE: return "write"; |
| 191 | case NBD_CMD_DISC: return "disconnect"; |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 192 | case NBD_CMD_FLUSH: return "flush"; |
Paul Clements | a336d29 | 2012-10-04 17:16:18 -0700 | [diff] [blame] | 193 | case NBD_CMD_TRIM: return "trim/discard"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | return "invalid"; |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 198 | static ssize_t pid_show(struct device *dev, |
| 199 | struct device_attribute *attr, char *buf) |
| 200 | { |
| 201 | struct gendisk *disk = dev_to_disk(dev); |
| 202 | struct nbd_device *nbd = (struct nbd_device *)disk->private_data; |
| 203 | |
| 204 | return sprintf(buf, "%d\n", task_pid_nr(nbd->task_recv)); |
| 205 | } |
| 206 | |
Bhumika Goyal | dfbde55 | 2017-08-21 17:13:08 +0530 | [diff] [blame] | 207 | static const struct device_attribute pid_attr = { |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 208 | .attr = { .name = "pid", .mode = 0444}, |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 209 | .show = pid_show, |
| 210 | }; |
| 211 | |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 212 | static void nbd_dev_remove(struct nbd_device *nbd) |
| 213 | { |
| 214 | struct gendisk *disk = nbd->disk; |
Josef Bacik | 8364da4 | 2018-05-16 14:51:17 -0400 | [diff] [blame] | 215 | struct request_queue *q; |
| 216 | |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 217 | if (disk) { |
Josef Bacik | 8364da4 | 2018-05-16 14:51:17 -0400 | [diff] [blame] | 218 | q = disk->queue; |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 219 | del_gendisk(disk); |
Josef Bacik | 8364da4 | 2018-05-16 14:51:17 -0400 | [diff] [blame] | 220 | blk_cleanup_queue(q); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 221 | blk_mq_free_tag_set(&nbd->tag_set); |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 222 | disk->private_data = NULL; |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 223 | put_disk(disk); |
| 224 | } |
| 225 | kfree(nbd); |
| 226 | } |
| 227 | |
| 228 | static void nbd_put(struct nbd_device *nbd) |
| 229 | { |
| 230 | if (refcount_dec_and_mutex_lock(&nbd->refs, |
| 231 | &nbd_index_mutex)) { |
| 232 | idr_remove(&nbd_index_idr, nbd->index); |
| 233 | mutex_unlock(&nbd_index_mutex); |
| 234 | nbd_dev_remove(nbd); |
| 235 | } |
| 236 | } |
| 237 | |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 238 | static int nbd_disconnected(struct nbd_config *config) |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 239 | { |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 240 | return test_bit(NBD_DISCONNECTED, &config->runtime_flags) || |
| 241 | test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags); |
| 242 | } |
| 243 | |
| 244 | static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock, |
| 245 | int notify) |
| 246 | { |
| 247 | if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) { |
| 248 | struct link_dead_args *args; |
| 249 | args = kmalloc(sizeof(struct link_dead_args), GFP_NOIO); |
| 250 | if (args) { |
| 251 | INIT_WORK(&args->work, nbd_dead_link_work); |
| 252 | args->index = nbd->index; |
| 253 | queue_work(system_wq, &args->work); |
| 254 | } |
| 255 | } |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 256 | if (!nsock->dead) { |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 257 | kernel_sock_shutdown(nsock->sock, SHUT_RDWR); |
Kevin Vigor | 5e3c3a7 | 2018-05-30 10:45:11 -0600 | [diff] [blame] | 258 | if (atomic_dec_return(&nbd->config->live_connections) == 0) { |
| 259 | if (test_and_clear_bit(NBD_DISCONNECT_REQUESTED, |
| 260 | &nbd->config->runtime_flags)) { |
| 261 | set_bit(NBD_DISCONNECTED, |
| 262 | &nbd->config->runtime_flags); |
| 263 | dev_info(nbd_to_dev(nbd), |
| 264 | "Disconnected due to user request.\n"); |
| 265 | } |
| 266 | } |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 267 | } |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 268 | nsock->dead = true; |
| 269 | nsock->pending = NULL; |
| 270 | nsock->sent = 0; |
| 271 | } |
| 272 | |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 273 | static void nbd_size_clear(struct nbd_device *nbd) |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 274 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 275 | if (nbd->config->bytesize) { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 276 | set_capacity(nbd->disk, 0); |
| 277 | kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); |
| 278 | } |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 281 | static void nbd_size_update(struct nbd_device *nbd) |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 282 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 283 | struct nbd_config *config = nbd->config; |
Josef Bacik | 9e2b1967 | 2018-05-16 14:51:19 -0400 | [diff] [blame] | 284 | struct block_device *bdev = bdget_disk(nbd->disk, 0); |
| 285 | |
Josef Bacik | 6df133a | 2018-05-23 13:35:59 -0400 | [diff] [blame] | 286 | if (config->flags & NBD_FLAG_SEND_TRIM) { |
| 287 | nbd->disk->queue->limits.discard_granularity = config->blksize; |
Josef Bacik | 07ce213 | 2018-06-05 11:41:23 -0400 | [diff] [blame] | 288 | nbd->disk->queue->limits.discard_alignment = config->blksize; |
Josef Bacik | 6df133a | 2018-05-23 13:35:59 -0400 | [diff] [blame] | 289 | blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX); |
| 290 | } |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 291 | blk_queue_logical_block_size(nbd->disk->queue, config->blksize); |
| 292 | blk_queue_physical_block_size(nbd->disk->queue, config->blksize); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 293 | set_capacity(nbd->disk, config->bytesize >> 9); |
Josef Bacik | 9e2b1967 | 2018-05-16 14:51:19 -0400 | [diff] [blame] | 294 | if (bdev) { |
Jan Kara | c8a83a6 | 2019-01-14 09:48:09 +0100 | [diff] [blame] | 295 | if (bdev->bd_disk) { |
Josef Bacik | 9e2b1967 | 2018-05-16 14:51:19 -0400 | [diff] [blame] | 296 | bd_set_size(bdev, config->bytesize); |
Jan Kara | c8a83a6 | 2019-01-14 09:48:09 +0100 | [diff] [blame] | 297 | set_blocksize(bdev, config->blksize); |
| 298 | } else |
Josef Bacik | 9e2b1967 | 2018-05-16 14:51:19 -0400 | [diff] [blame] | 299 | bdev->bd_invalidated = 1; |
| 300 | bdput(bdev); |
| 301 | } |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 302 | kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); |
| 303 | } |
| 304 | |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 305 | static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize, |
| 306 | loff_t nr_blocks) |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 307 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 308 | struct nbd_config *config = nbd->config; |
| 309 | config->blksize = blocksize; |
| 310 | config->bytesize = blocksize * nr_blocks; |
Josef Bacik | c3f7c93 | 2018-05-16 14:51:18 -0400 | [diff] [blame] | 311 | if (nbd->task_recv != NULL) |
| 312 | nbd_size_update(nbd); |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Christoph Hellwig | 1e388ae | 2017-04-20 16:03:06 +0200 | [diff] [blame] | 315 | static void nbd_complete_rq(struct request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
Christoph Hellwig | 1e388ae | 2017-04-20 16:03:06 +0200 | [diff] [blame] | 317 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Kevin Vigor | ee57a05 | 2018-06-04 10:40:12 -0600 | [diff] [blame] | 319 | dev_dbg(nbd_to_dev(cmd->nbd), "request %p: %s\n", req, |
Christoph Hellwig | 1e388ae | 2017-04-20 16:03:06 +0200 | [diff] [blame] | 320 | cmd->status ? "failed" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Christoph Hellwig | 1e388ae | 2017-04-20 16:03:06 +0200 | [diff] [blame] | 322 | blk_mq_end_request(req, cmd->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Markus Pargmann | e018e75 | 2015-04-02 10:11:39 +0200 | [diff] [blame] | 325 | /* |
| 326 | * Forcibly shutdown the socket causing all listeners to error |
| 327 | */ |
Markus Pargmann | 36e47be | 2015-08-17 08:20:01 +0200 | [diff] [blame] | 328 | static void sock_shutdown(struct nbd_device *nbd) |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 329 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 330 | struct nbd_config *config = nbd->config; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 331 | int i; |
Josef Bacik | c261189 | 2016-09-08 12:33:38 -0700 | [diff] [blame] | 332 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 333 | if (config->num_connections == 0) |
Markus Pargmann | 260bbce | 2015-08-17 08:20:02 +0200 | [diff] [blame] | 334 | return; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 335 | if (test_and_set_bit(NBD_DISCONNECTED, &config->runtime_flags)) |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 336 | return; |
| 337 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 338 | for (i = 0; i < config->num_connections; i++) { |
| 339 | struct nbd_sock *nsock = config->socks[i]; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 340 | mutex_lock(&nsock->tx_lock); |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 341 | nbd_mark_nsock_dead(nbd, nsock, 0); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 342 | mutex_unlock(&nsock->tx_lock); |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 343 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 344 | dev_warn(disk_to_dev(nbd->disk), "shutting down sockets\n"); |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Mike Christie | 0051467 | 2019-08-13 11:39:50 -0500 | [diff] [blame^] | 347 | static u32 req_to_nbd_cmd_type(struct request *req) |
| 348 | { |
| 349 | switch (req_op(req)) { |
| 350 | case REQ_OP_DISCARD: |
| 351 | return NBD_CMD_TRIM; |
| 352 | case REQ_OP_FLUSH: |
| 353 | return NBD_CMD_FLUSH; |
| 354 | case REQ_OP_WRITE: |
| 355 | return NBD_CMD_WRITE; |
| 356 | case REQ_OP_READ: |
| 357 | return NBD_CMD_READ; |
| 358 | default: |
| 359 | return U32_MAX; |
| 360 | } |
| 361 | } |
| 362 | |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 363 | static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req, |
| 364 | bool reserved) |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 365 | { |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 366 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req); |
| 367 | struct nbd_device *nbd = cmd->nbd; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 368 | struct nbd_config *config; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 369 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 370 | if (!refcount_inc_not_zero(&nbd->config_refs)) { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 371 | cmd->status = BLK_STS_TIMEOUT; |
Christoph Hellwig | e5eab01 | 2018-05-29 15:52:31 +0200 | [diff] [blame] | 372 | goto done; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 373 | } |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 374 | config = nbd->config; |
| 375 | |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 376 | if (!mutex_trylock(&cmd->lock)) |
| 377 | return BLK_EH_RESET_TIMER; |
| 378 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 379 | if (config->num_connections > 1) { |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 380 | dev_err_ratelimited(nbd_to_dev(nbd), |
Kevin Vigor | 5e3c3a7 | 2018-05-30 10:45:11 -0600 | [diff] [blame] | 381 | "Connection timed out, retrying (%d/%d alive)\n", |
| 382 | atomic_read(&config->live_connections), |
| 383 | config->num_connections); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 384 | /* |
| 385 | * Hooray we have more connections, requeue this IO, the submit |
| 386 | * path will put it on a real connection. |
| 387 | */ |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 388 | if (config->socks && config->num_connections > 1) { |
| 389 | if (cmd->index < config->num_connections) { |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 390 | struct nbd_sock *nsock = |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 391 | config->socks[cmd->index]; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 392 | mutex_lock(&nsock->tx_lock); |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 393 | /* We can have multiple outstanding requests, so |
| 394 | * we don't want to mark the nsock dead if we've |
| 395 | * already reconnected with a new socket, so |
| 396 | * only mark it dead if its the same socket we |
| 397 | * were sent out on. |
| 398 | */ |
| 399 | if (cmd->cookie == nsock->cookie) |
| 400 | nbd_mark_nsock_dead(nbd, nsock, 1); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 401 | mutex_unlock(&nsock->tx_lock); |
| 402 | } |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 403 | mutex_unlock(&cmd->lock); |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 404 | nbd_requeue_cmd(cmd); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 405 | nbd_config_put(nbd); |
Christoph Hellwig | 6600593 | 2018-05-29 15:52:29 +0200 | [diff] [blame] | 406 | return BLK_EH_DONE; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 407 | } |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 408 | } else { |
| 409 | dev_err_ratelimited(nbd_to_dev(nbd), |
| 410 | "Connection timed out\n"); |
| 411 | } |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 412 | set_bit(NBD_TIMEDOUT, &config->runtime_flags); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 413 | cmd->status = BLK_STS_IOERR; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 414 | mutex_unlock(&cmd->lock); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 415 | sock_shutdown(nbd); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 416 | nbd_config_put(nbd); |
Christoph Hellwig | e5eab01 | 2018-05-29 15:52:31 +0200 | [diff] [blame] | 417 | done: |
| 418 | blk_mq_complete_request(req); |
| 419 | return BLK_EH_DONE; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | /* |
| 423 | * Send or receive packet. |
| 424 | */ |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 425 | static int sock_xmit(struct nbd_device *nbd, int index, int send, |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 426 | struct iov_iter *iter, int msg_flags, int *sent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 428 | struct nbd_config *config = nbd->config; |
| 429 | struct socket *sock = config->socks[index]->sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | int result; |
| 431 | struct msghdr msg; |
Vlastimil Babka | f108304 | 2017-05-08 15:59:53 -0700 | [diff] [blame] | 432 | unsigned int noreclaim_flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 434 | if (unlikely(!sock)) { |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 435 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 436 | "Attempted %s on closed socket in sock_xmit\n", |
| 437 | (send ? "send" : "recv")); |
Mike Snitzer | ffc41cf | 2008-04-02 13:04:47 -0700 | [diff] [blame] | 438 | return -EINVAL; |
| 439 | } |
| 440 | |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 441 | msg.msg_iter = *iter; |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 442 | |
Vlastimil Babka | f108304 | 2017-05-08 15:59:53 -0700 | [diff] [blame] | 443 | noreclaim_flag = memalloc_noreclaim_save(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | do { |
Mel Gorman | 7f338fe | 2012-07-31 16:44:32 -0700 | [diff] [blame] | 445 | sock->sk->sk_allocation = GFP_NOIO | __GFP_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | msg.msg_name = NULL; |
| 447 | msg.msg_namelen = 0; |
| 448 | msg.msg_control = NULL; |
| 449 | msg.msg_controllen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | msg.msg_flags = msg_flags | MSG_NOSIGNAL; |
| 451 | |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 452 | if (send) |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 453 | result = sock_sendmsg(sock, &msg); |
Markus Pargmann | 7e2893a | 2015-08-17 08:20:00 +0200 | [diff] [blame] | 454 | else |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 455 | result = sock_recvmsg(sock, &msg, msg.msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | if (result <= 0) { |
| 458 | if (result == 0) |
| 459 | result = -EPIPE; /* short read */ |
| 460 | break; |
| 461 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 462 | if (sent) |
| 463 | *sent += result; |
Al Viro | c1696ca | 2015-11-12 04:51:19 -0500 | [diff] [blame] | 464 | } while (msg_data_left(&msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Vlastimil Babka | f108304 | 2017-05-08 15:59:53 -0700 | [diff] [blame] | 466 | memalloc_noreclaim_restore(noreclaim_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
| 468 | return result; |
| 469 | } |
| 470 | |
Josef Bacik | 32e67a3 | 2017-10-24 15:57:18 -0400 | [diff] [blame] | 471 | /* |
| 472 | * Different settings for sk->sk_sndtimeo can result in different return values |
| 473 | * if there is a signal pending when we enter sendmsg, because reasons? |
| 474 | */ |
| 475 | static inline int was_interrupted(int result) |
| 476 | { |
| 477 | return result == -ERESTARTSYS || result == -EINTR; |
| 478 | } |
| 479 | |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 480 | /* always call with the tx_lock held */ |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 481 | 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] | 482 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 483 | struct request *req = blk_mq_rq_from_pdu(cmd); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 484 | struct nbd_config *config = nbd->config; |
| 485 | struct nbd_sock *nsock = config->socks[index]; |
Josef Bacik | d61b7f9 | 2017-01-19 16:08:49 -0500 | [diff] [blame] | 486 | int result; |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 487 | struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)}; |
| 488 | struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)}; |
| 489 | struct iov_iter from; |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 490 | unsigned long size = blk_rq_bytes(req); |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 491 | struct bio *bio; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 492 | u64 handle; |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 493 | u32 type; |
Shaun McDowell | 685c9b2 | 2017-05-25 23:55:54 -0400 | [diff] [blame] | 494 | u32 nbd_cmd_flags = 0; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 495 | int sent = nsock->sent, skip = 0; |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 496 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 497 | iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request)); |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 498 | |
Mike Christie | 0051467 | 2019-08-13 11:39:50 -0500 | [diff] [blame^] | 499 | type = req_to_nbd_cmd_type(req); |
| 500 | if (type == U32_MAX) |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 501 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Christoph Hellwig | 09fc54cc | 2017-01-31 16:57:28 +0100 | [diff] [blame] | 503 | if (rq_data_dir(req) == WRITE && |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 504 | (config->flags & NBD_FLAG_READ_ONLY)) { |
Christoph Hellwig | 09fc54cc | 2017-01-31 16:57:28 +0100 | [diff] [blame] | 505 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 506 | "Write on read-only\n"); |
| 507 | return -EIO; |
| 508 | } |
| 509 | |
Shaun McDowell | 685c9b2 | 2017-05-25 23:55:54 -0400 | [diff] [blame] | 510 | if (req->cmd_flags & REQ_FUA) |
| 511 | nbd_cmd_flags |= NBD_CMD_FLAG_FUA; |
| 512 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 513 | /* We did a partial send previously, and we at least sent the whole |
| 514 | * request struct, so just go and send the rest of the pages in the |
| 515 | * request. |
| 516 | */ |
| 517 | if (sent) { |
| 518 | if (sent >= sizeof(request)) { |
| 519 | skip = sent - sizeof(request); |
Andrew Hall | 2abd2de | 2019-04-26 11:49:49 -0700 | [diff] [blame] | 520 | |
| 521 | /* initialize handle for tracing purposes */ |
| 522 | handle = nbd_cmd_handle(cmd); |
| 523 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 524 | goto send_pages; |
| 525 | } |
| 526 | iov_iter_advance(&from, sent); |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 527 | } else { |
| 528 | cmd->cmd_cookie++; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 529 | } |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 530 | cmd->index = index; |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 531 | cmd->cookie = nsock->cookie; |
Shaun McDowell | 685c9b2 | 2017-05-25 23:55:54 -0400 | [diff] [blame] | 532 | request.type = htonl(type | nbd_cmd_flags); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 533 | if (type != NBD_CMD_FLUSH) { |
Alex Bligh | 75f187a | 2013-02-27 17:05:23 -0800 | [diff] [blame] | 534 | request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9); |
| 535 | request.len = htonl(size); |
| 536 | } |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 537 | handle = nbd_cmd_handle(cmd); |
| 538 | memcpy(request.handle, &handle, sizeof(handle)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Matt Mullins | ea10672 | 2019-04-26 11:49:48 -0700 | [diff] [blame] | 540 | trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd)); |
| 541 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 542 | dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n", |
Kevin Vigor | ee57a05 | 2018-06-04 10:40:12 -0600 | [diff] [blame] | 543 | req, nbdcmd_to_ascii(type), |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 544 | (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req)); |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 545 | result = sock_xmit(nbd, index, 1, &from, |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 546 | (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent); |
Andrew Hall | 2abd2de | 2019-04-26 11:49:49 -0700 | [diff] [blame] | 547 | trace_nbd_header_sent(req, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | if (result <= 0) { |
Josef Bacik | 32e67a3 | 2017-10-24 15:57:18 -0400 | [diff] [blame] | 549 | if (was_interrupted(result)) { |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 550 | /* If we havne't sent anything we can just return BUSY, |
| 551 | * however if we have sent something we need to make |
| 552 | * sure we only allow this req to be sent until we are |
| 553 | * completely done. |
| 554 | */ |
| 555 | if (sent) { |
| 556 | nsock->pending = req; |
| 557 | nsock->sent = sent; |
| 558 | } |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 559 | set_bit(NBD_CMD_REQUEUED, &cmd->flags); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 560 | return BLK_STS_RESOURCE; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 561 | } |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 562 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 563 | "Send control failed (result %d)\n", result); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 564 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 566 | send_pages: |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 567 | if (type != NBD_CMD_WRITE) |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 568 | goto out; |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 569 | |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 570 | bio = req->bio; |
| 571 | while (bio) { |
| 572 | struct bio *next = bio->bi_next; |
| 573 | struct bvec_iter iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 574 | struct bio_vec bvec; |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 575 | |
| 576 | bio_for_each_segment(bvec, bio, iter) { |
| 577 | bool is_last = !next && bio_iter_last(bvec, iter); |
Josef Bacik | d61b7f9 | 2017-01-19 16:08:49 -0500 | [diff] [blame] | 578 | int flags = is_last ? 0 : MSG_MORE; |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 579 | |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 580 | dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n", |
Kevin Vigor | ee57a05 | 2018-06-04 10:40:12 -0600 | [diff] [blame] | 581 | req, bvec.bv_len); |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 582 | iov_iter_bvec(&from, WRITE, &bvec, 1, bvec.bv_len); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 583 | if (skip) { |
| 584 | if (skip >= iov_iter_count(&from)) { |
| 585 | skip -= iov_iter_count(&from); |
| 586 | continue; |
| 587 | } |
| 588 | iov_iter_advance(&from, skip); |
| 589 | skip = 0; |
| 590 | } |
| 591 | result = sock_xmit(nbd, index, 1, &from, flags, &sent); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 592 | if (result <= 0) { |
Josef Bacik | 32e67a3 | 2017-10-24 15:57:18 -0400 | [diff] [blame] | 593 | if (was_interrupted(result)) { |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 594 | /* We've already sent the header, we |
| 595 | * have no choice but to set pending and |
| 596 | * return BUSY. |
| 597 | */ |
| 598 | nsock->pending = req; |
| 599 | nsock->sent = sent; |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 600 | set_bit(NBD_CMD_REQUEUED, &cmd->flags); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 601 | return BLK_STS_RESOURCE; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 602 | } |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 603 | dev_err(disk_to_dev(nbd->disk), |
WANG Cong | 7f1b90f | 2011-08-19 14:48:22 +0200 | [diff] [blame] | 604 | "Send data failed (result %d)\n", |
| 605 | result); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 606 | return -EAGAIN; |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 607 | } |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 608 | /* |
| 609 | * The completion might already have come in, |
| 610 | * so break for the last one instead of letting |
| 611 | * the iterator do it. This prevents use-after-free |
| 612 | * of the bio. |
| 613 | */ |
| 614 | if (is_last) |
| 615 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
Jens Axboe | 429a787 | 2016-11-17 12:30:37 -0700 | [diff] [blame] | 617 | bio = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 619 | out: |
Andrew Hall | 2abd2de | 2019-04-26 11:49:49 -0700 | [diff] [blame] | 620 | trace_nbd_payload_sent(req, handle); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 621 | nsock->pending = NULL; |
| 622 | nsock->sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | /* NULL returned = something went wrong, inform userspace */ |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 627 | 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] | 628 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 629 | struct nbd_config *config = nbd->config; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | int result; |
| 631 | struct nbd_reply reply; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 632 | struct nbd_cmd *cmd; |
| 633 | struct request *req = NULL; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 634 | u64 handle; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 635 | u16 hwq; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 636 | u32 tag; |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 637 | struct kvec iov = {.iov_base = &reply, .iov_len = sizeof(reply)}; |
| 638 | struct iov_iter to; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 639 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | reply.magic = 0; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 642 | iov_iter_kvec(&to, READ, &iov, 1, sizeof(reply)); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 643 | result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if (result <= 0) { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 645 | if (!nbd_disconnected(config)) |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 646 | dev_err(disk_to_dev(nbd->disk), |
| 647 | "Receive control failed (result %d)\n", result); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 648 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 650 | |
| 651 | if (ntohl(reply.magic) != NBD_REPLY_MAGIC) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 652 | dev_err(disk_to_dev(nbd->disk), "Wrong magic (0x%lx)\n", |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 653 | (unsigned long)ntohl(reply.magic)); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 654 | return ERR_PTR(-EPROTO); |
Michal Feix | e4b57e0 | 2006-07-30 03:03:31 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 657 | memcpy(&handle, reply.handle, sizeof(handle)); |
| 658 | tag = nbd_handle_to_tag(handle); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 659 | hwq = blk_mq_unique_tag_to_hwq(tag); |
| 660 | if (hwq < nbd->tag_set.nr_hw_queues) |
| 661 | req = blk_mq_tag_to_rq(nbd->tag_set.tags[hwq], |
| 662 | blk_mq_unique_tag_to_tag(tag)); |
| 663 | if (!req || !blk_mq_request_started(req)) { |
| 664 | dev_err(disk_to_dev(nbd->disk), "Unexpected reply (%d) %p\n", |
| 665 | tag, req); |
| 666 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
Andrew Hall | 2abd2de | 2019-04-26 11:49:49 -0700 | [diff] [blame] | 668 | trace_nbd_header_received(req, handle); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 669 | cmd = blk_mq_rq_to_pdu(req); |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 670 | |
| 671 | mutex_lock(&cmd->lock); |
| 672 | if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) { |
| 673 | dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n", |
| 674 | req, cmd->cmd_cookie, nbd_handle_to_cookie(handle)); |
| 675 | ret = -ENOENT; |
| 676 | goto out; |
| 677 | } |
| 678 | if (test_bit(NBD_CMD_REQUEUED, &cmd->flags)) { |
| 679 | dev_err(disk_to_dev(nbd->disk), "Raced with timeout on req %p\n", |
| 680 | req); |
| 681 | ret = -ENOENT; |
| 682 | goto out; |
| 683 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | if (ntohl(reply.error)) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 685 | 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] | 686 | ntohl(reply.error)); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 687 | cmd->status = BLK_STS_IOERR; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 688 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Kevin Vigor | ee57a05 | 2018-06-04 10:40:12 -0600 | [diff] [blame] | 691 | dev_dbg(nbd_to_dev(nbd), "request %p: got reply\n", req); |
Christoph Hellwig | 9dc6c80 | 2015-04-17 22:37:21 +0200 | [diff] [blame] | 692 | if (rq_data_dir(req) != WRITE) { |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 693 | struct req_iterator iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 694 | struct bio_vec bvec; |
NeilBrown | 5705f70 | 2007-09-25 12:35:59 +0200 | [diff] [blame] | 695 | |
| 696 | rq_for_each_segment(bvec, req, iter) { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 697 | iov_iter_bvec(&to, READ, &bvec, 1, bvec.bv_len); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 698 | result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL); |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 699 | if (result <= 0) { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 700 | 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] | 701 | result); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 702 | /* |
| 703 | * If we've disconnected or we only have 1 |
| 704 | * connection then we need to make sure we |
| 705 | * complete this request, otherwise error out |
| 706 | * and let the timeout stuff handle resubmitting |
| 707 | * this request onto another connection. |
| 708 | */ |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 709 | if (nbd_disconnected(config) || |
| 710 | config->num_connections <= 1) { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 711 | cmd->status = BLK_STS_IOERR; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 712 | goto out; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 713 | } |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 714 | ret = -EIO; |
| 715 | goto out; |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 716 | } |
Markus Pargmann | d18509f | 2015-04-02 10:11:38 +0200 | [diff] [blame] | 717 | dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n", |
Kevin Vigor | ee57a05 | 2018-06-04 10:40:12 -0600 | [diff] [blame] | 718 | req, bvec.bv_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 721 | out: |
Andrew Hall | 2abd2de | 2019-04-26 11:49:49 -0700 | [diff] [blame] | 722 | trace_nbd_payload_received(req, handle); |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 723 | mutex_unlock(&cmd->lock); |
| 724 | return ret ? ERR_PTR(ret) : cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 727 | static void recv_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 729 | struct recv_thread_args *args = container_of(work, |
| 730 | struct recv_thread_args, |
| 731 | work); |
| 732 | struct nbd_device *nbd = args->nbd; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 733 | struct nbd_config *config = nbd->config; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 734 | struct nbd_cmd *cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 736 | while (1) { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 737 | cmd = nbd_read_stat(nbd, args->index); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 738 | if (IS_ERR(cmd)) { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 739 | struct nbd_sock *nsock = config->socks[args->index]; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 740 | |
| 741 | mutex_lock(&nsock->tx_lock); |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 742 | nbd_mark_nsock_dead(nbd, nsock, 1); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 743 | mutex_unlock(&nsock->tx_lock); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 744 | break; |
| 745 | } |
| 746 | |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 747 | blk_mq_complete_request(blk_mq_rq_from_pdu(cmd)); |
Markus Pargmann | 1939183 | 2015-08-17 08:20:03 +0200 | [diff] [blame] | 748 | } |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 749 | atomic_dec(&config->recv_threads); |
| 750 | wake_up(&config->recv_wq); |
| 751 | nbd_config_put(nbd); |
| 752 | kfree(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Jens Axboe | 7baa857 | 2018-11-08 10:24:07 -0700 | [diff] [blame] | 755 | static bool nbd_clear_req(struct request *req, void *data, bool reserved) |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 756 | { |
Christoph Hellwig | d250bf4e | 2018-05-30 18:51:00 +0200 | [diff] [blame] | 757 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 758 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 759 | cmd->status = BLK_STS_IOERR; |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 760 | blk_mq_complete_request(req); |
Jens Axboe | 7baa857 | 2018-11-08 10:24:07 -0700 | [diff] [blame] | 761 | return true; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 764 | static void nbd_clear_que(struct nbd_device *nbd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { |
Sagi Grimberg | b52c2e9 | 2017-07-04 09:57:09 +0300 | [diff] [blame] | 766 | blk_mq_quiesce_queue(nbd->disk->queue); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 767 | blk_mq_tagset_busy_iter(&nbd->tag_set, nbd_clear_req, NULL); |
Sagi Grimberg | b52c2e9 | 2017-07-04 09:57:09 +0300 | [diff] [blame] | 768 | blk_mq_unquiesce_queue(nbd->disk->queue); |
Markus Pargmann | e78273c | 2015-08-17 08:20:04 +0200 | [diff] [blame] | 769 | dev_dbg(disk_to_dev(nbd->disk), "queue cleared\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 772 | static int find_fallback(struct nbd_device *nbd, int index) |
| 773 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 774 | struct nbd_config *config = nbd->config; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 775 | int new_index = -1; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 776 | struct nbd_sock *nsock = config->socks[index]; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 777 | int fallback = nsock->fallback_index; |
| 778 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 779 | if (test_bit(NBD_DISCONNECTED, &config->runtime_flags)) |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 780 | return new_index; |
| 781 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 782 | if (config->num_connections <= 1) { |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 783 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 784 | "Attempted send on invalid socket\n"); |
| 785 | return new_index; |
| 786 | } |
| 787 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 788 | if (fallback >= 0 && fallback < config->num_connections && |
| 789 | !config->socks[fallback]->dead) |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 790 | return fallback; |
| 791 | |
| 792 | if (nsock->fallback_index < 0 || |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 793 | nsock->fallback_index >= config->num_connections || |
| 794 | config->socks[nsock->fallback_index]->dead) { |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 795 | int i; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 796 | for (i = 0; i < config->num_connections; i++) { |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 797 | if (i == index) |
| 798 | continue; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 799 | if (!config->socks[i]->dead) { |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 800 | new_index = i; |
| 801 | break; |
| 802 | } |
| 803 | } |
| 804 | nsock->fallback_index = new_index; |
| 805 | if (new_index < 0) { |
| 806 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 807 | "Dead connection, failed to find a fallback\n"); |
| 808 | return new_index; |
| 809 | } |
| 810 | } |
| 811 | new_index = nsock->fallback_index; |
| 812 | return new_index; |
| 813 | } |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 814 | |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 815 | static int wait_for_reconnect(struct nbd_device *nbd) |
| 816 | { |
| 817 | struct nbd_config *config = nbd->config; |
| 818 | if (!config->dead_conn_timeout) |
| 819 | return 0; |
| 820 | if (test_bit(NBD_DISCONNECTED, &config->runtime_flags)) |
| 821 | return 0; |
Kevin Vigor | 5e3c3a7 | 2018-05-30 10:45:11 -0600 | [diff] [blame] | 822 | return wait_event_timeout(config->conn_wait, |
| 823 | atomic_read(&config->live_connections) > 0, |
| 824 | config->dead_conn_timeout) > 0; |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 825 | } |
| 826 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 827 | static int nbd_handle_cmd(struct nbd_cmd *cmd, int index) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 828 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 829 | struct request *req = blk_mq_rq_from_pdu(cmd); |
| 830 | struct nbd_device *nbd = cmd->nbd; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 831 | struct nbd_config *config; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 832 | struct nbd_sock *nsock; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 833 | int ret; |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 834 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 835 | if (!refcount_inc_not_zero(&nbd->config_refs)) { |
| 836 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 837 | "Socks array is empty\n"); |
Josef Bacik | 6a468d5 | 2017-11-06 16:11:58 -0500 | [diff] [blame] | 838 | blk_mq_start_request(req); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 839 | return -EINVAL; |
| 840 | } |
| 841 | config = nbd->config; |
| 842 | |
| 843 | if (index >= config->num_connections) { |
Josef Bacik | a897b66 | 2016-12-05 16:20:29 -0500 | [diff] [blame] | 844 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
| 845 | "Attempted send on invalid socket\n"); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 846 | nbd_config_put(nbd); |
Josef Bacik | 6a468d5 | 2017-11-06 16:11:58 -0500 | [diff] [blame] | 847 | blk_mq_start_request(req); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 848 | return -EINVAL; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 849 | } |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 850 | cmd->status = BLK_STS_OK; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 851 | again: |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 852 | nsock = config->socks[index]; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 853 | mutex_lock(&nsock->tx_lock); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 854 | if (nsock->dead) { |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 855 | int old_index = index; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 856 | index = find_fallback(nbd, index); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 857 | mutex_unlock(&nsock->tx_lock); |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 858 | if (index < 0) { |
| 859 | if (wait_for_reconnect(nbd)) { |
| 860 | index = old_index; |
| 861 | goto again; |
| 862 | } |
| 863 | /* All the sockets should already be down at this point, |
| 864 | * we just want to make sure that DISCONNECTED is set so |
| 865 | * any requests that come in that were queue'ed waiting |
| 866 | * for the reconnect timer don't trigger the timer again |
| 867 | * and instead just error out. |
| 868 | */ |
| 869 | sock_shutdown(nbd); |
| 870 | nbd_config_put(nbd); |
Josef Bacik | 6a468d5 | 2017-11-06 16:11:58 -0500 | [diff] [blame] | 871 | blk_mq_start_request(req); |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 872 | return -EIO; |
| 873 | } |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 874 | goto again; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 877 | /* Handle the case that we have a pending request that was partially |
| 878 | * transmitted that _has_ to be serviced first. We need to call requeue |
| 879 | * here so that it gets put _after_ the request that is already on the |
| 880 | * dispatch list. |
| 881 | */ |
Josef Bacik | 6a468d5 | 2017-11-06 16:11:58 -0500 | [diff] [blame] | 882 | blk_mq_start_request(req); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 883 | if (unlikely(nsock->pending && nsock->pending != req)) { |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 884 | nbd_requeue_cmd(cmd); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 885 | ret = 0; |
| 886 | goto out; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 887 | } |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 888 | /* |
| 889 | * Some failures are related to the link going down, so anything that |
| 890 | * returns EAGAIN can be retried on a different socket. |
| 891 | */ |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 892 | ret = nbd_send_cmd(nbd, cmd, index); |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 893 | if (ret == -EAGAIN) { |
| 894 | dev_err_ratelimited(disk_to_dev(nbd->disk), |
Josef Bacik | 6a468d5 | 2017-11-06 16:11:58 -0500 | [diff] [blame] | 895 | "Request send failed, requeueing\n"); |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 896 | nbd_mark_nsock_dead(nbd, nsock, 1); |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 897 | nbd_requeue_cmd(cmd); |
Josef Bacik | 6a468d5 | 2017-11-06 16:11:58 -0500 | [diff] [blame] | 898 | ret = 0; |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 899 | } |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 900 | out: |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 901 | mutex_unlock(&nsock->tx_lock); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 902 | nbd_config_put(nbd); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 903 | return ret; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 906 | static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 907 | const struct blk_mq_queue_data *bd) |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 908 | { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 909 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 910 | int ret; |
Laurent Vivier | 48cf606 | 2008-04-29 01:02:46 -0700 | [diff] [blame] | 911 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 912 | /* |
| 913 | * Since we look at the bio's to send the request over the network we |
| 914 | * need to make sure the completion work doesn't mark this request done |
| 915 | * before we are done doing our send. This keeps us from dereferencing |
| 916 | * freed data if we have particularly fast completions (ie we get the |
| 917 | * completion before we exit sock_xmit on the last bvec) or in the case |
| 918 | * that the server is misbehaving (or there was an error) before we're |
| 919 | * done sending everything over the wire. |
| 920 | */ |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 921 | mutex_lock(&cmd->lock); |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 922 | clear_bit(NBD_CMD_REQUEUED, &cmd->flags); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 923 | |
| 924 | /* We can be called directly from the user space process, which means we |
| 925 | * could possibly have signals pending so our sendmsg will fail. In |
| 926 | * this case we need to return that we are busy, otherwise error out as |
| 927 | * appropriate. |
| 928 | */ |
| 929 | ret = nbd_handle_cmd(cmd, hctx->queue_num); |
Josef Bacik | 6e60a3b | 2017-10-02 16:22:08 -0400 | [diff] [blame] | 930 | if (ret < 0) |
| 931 | ret = BLK_STS_IOERR; |
| 932 | else if (!ret) |
| 933 | ret = BLK_STS_OK; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 934 | mutex_unlock(&cmd->lock); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 935 | |
Josef Bacik | 6e60a3b | 2017-10-02 16:22:08 -0400 | [diff] [blame] | 936 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 939 | static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg, |
| 940 | bool netlink) |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 941 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 942 | struct nbd_config *config = nbd->config; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 943 | struct socket *sock; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 944 | struct nbd_sock **socks; |
| 945 | struct nbd_sock *nsock; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 946 | int err; |
| 947 | |
| 948 | sock = sockfd_lookup(arg, &err); |
| 949 | if (!sock) |
| 950 | return err; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 951 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 952 | if (!netlink && !nbd->task_setup && |
| 953 | !test_bit(NBD_BOUND, &config->runtime_flags)) |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 954 | nbd->task_setup = current; |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 955 | |
| 956 | if (!netlink && |
| 957 | (nbd->task_setup != current || |
| 958 | test_bit(NBD_BOUND, &config->runtime_flags))) { |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 959 | dev_err(disk_to_dev(nbd->disk), |
| 960 | "Device being setup by another task"); |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 961 | sockfd_put(sock); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 962 | return -EBUSY; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 965 | socks = krealloc(config->socks, (config->num_connections + 1) * |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 966 | sizeof(struct nbd_sock *), GFP_KERNEL); |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 967 | if (!socks) { |
| 968 | sockfd_put(sock); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 969 | return -ENOMEM; |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 970 | } |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 971 | nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL); |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 972 | if (!nsock) { |
| 973 | sockfd_put(sock); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 974 | return -ENOMEM; |
Josef Bacik | 9b1355d | 2017-04-06 17:01:56 -0400 | [diff] [blame] | 975 | } |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 976 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 977 | config->socks = socks; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 978 | |
Josef Bacik | f373324 | 2017-04-06 17:01:57 -0400 | [diff] [blame] | 979 | nsock->fallback_index = -1; |
| 980 | nsock->dead = false; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 981 | mutex_init(&nsock->tx_lock); |
| 982 | nsock->sock = sock; |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 983 | nsock->pending = NULL; |
| 984 | nsock->sent = 0; |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 985 | nsock->cookie = 0; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 986 | socks[config->num_connections++] = nsock; |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 987 | atomic_inc(&config->live_connections); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 988 | |
| 989 | return 0; |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 990 | } |
| 991 | |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 992 | static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg) |
| 993 | { |
| 994 | struct nbd_config *config = nbd->config; |
| 995 | struct socket *sock, *old; |
| 996 | struct recv_thread_args *args; |
| 997 | int i; |
| 998 | int err; |
| 999 | |
| 1000 | sock = sockfd_lookup(arg, &err); |
| 1001 | if (!sock) |
| 1002 | return err; |
| 1003 | |
| 1004 | args = kzalloc(sizeof(*args), GFP_KERNEL); |
| 1005 | if (!args) { |
| 1006 | sockfd_put(sock); |
| 1007 | return -ENOMEM; |
| 1008 | } |
| 1009 | |
| 1010 | for (i = 0; i < config->num_connections; i++) { |
| 1011 | struct nbd_sock *nsock = config->socks[i]; |
| 1012 | |
| 1013 | if (!nsock->dead) |
| 1014 | continue; |
| 1015 | |
| 1016 | mutex_lock(&nsock->tx_lock); |
| 1017 | if (!nsock->dead) { |
| 1018 | mutex_unlock(&nsock->tx_lock); |
| 1019 | continue; |
| 1020 | } |
| 1021 | sk_set_memalloc(sock->sk); |
Josef Bacik | a7ee8cf | 2017-07-21 10:48:15 -0400 | [diff] [blame] | 1022 | if (nbd->tag_set.timeout) |
| 1023 | sock->sk->sk_sndtimeo = nbd->tag_set.timeout; |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1024 | atomic_inc(&config->recv_threads); |
| 1025 | refcount_inc(&nbd->config_refs); |
| 1026 | old = nsock->sock; |
| 1027 | nsock->fallback_index = -1; |
| 1028 | nsock->sock = sock; |
| 1029 | nsock->dead = false; |
| 1030 | INIT_WORK(&args->work, recv_work); |
| 1031 | args->index = i; |
| 1032 | args->nbd = nbd; |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 1033 | nsock->cookie++; |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1034 | mutex_unlock(&nsock->tx_lock); |
| 1035 | sockfd_put(old); |
| 1036 | |
Josef Bacik | 7a362ea | 2017-07-25 13:31:19 -0400 | [diff] [blame] | 1037 | clear_bit(NBD_DISCONNECTED, &config->runtime_flags); |
| 1038 | |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1039 | /* We take the tx_mutex in an error path in the recv_work, so we |
| 1040 | * need to queue_work outside of the tx_mutex. |
| 1041 | */ |
| 1042 | queue_work(recv_workqueue, &args->work); |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 1043 | |
| 1044 | atomic_inc(&config->live_connections); |
| 1045 | wake_up(&config->conn_wait); |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1046 | return 0; |
| 1047 | } |
| 1048 | sockfd_put(sock); |
| 1049 | kfree(args); |
| 1050 | return -ENOSPC; |
| 1051 | } |
| 1052 | |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 1053 | static void nbd_bdev_reset(struct block_device *bdev) |
| 1054 | { |
Ratna Manoj Bolla | abbbdf1 | 2017-03-24 14:08:29 -0400 | [diff] [blame] | 1055 | if (bdev->bd_openers > 1) |
| 1056 | return; |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1057 | bd_set_size(bdev, 0); |
Markus Pargmann | 0e4f0f6 | 2015-10-29 12:04:51 +0100 | [diff] [blame] | 1058 | } |
| 1059 | |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1060 | static void nbd_parse_flags(struct nbd_device *nbd) |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 1061 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1062 | struct nbd_config *config = nbd->config; |
| 1063 | if (config->flags & NBD_FLAG_READ_ONLY) |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1064 | set_disk_ro(nbd->disk, true); |
| 1065 | else |
| 1066 | set_disk_ro(nbd->disk, false); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1067 | if (config->flags & NBD_FLAG_SEND_TRIM) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 1068 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, nbd->disk->queue); |
Shaun McDowell | 685c9b2 | 2017-05-25 23:55:54 -0400 | [diff] [blame] | 1069 | if (config->flags & NBD_FLAG_SEND_FLUSH) { |
| 1070 | if (config->flags & NBD_FLAG_SEND_FUA) |
| 1071 | blk_queue_write_cache(nbd->disk->queue, true, true); |
| 1072 | else |
| 1073 | blk_queue_write_cache(nbd->disk->queue, true, false); |
| 1074 | } |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 1075 | else |
Jens Axboe | aafb1ee | 2016-03-30 10:10:53 -0600 | [diff] [blame] | 1076 | blk_queue_write_cache(nbd->disk->queue, false, false); |
Markus Pargmann | d02cf53 | 2015-10-29 12:06:15 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 1079 | static void send_disconnects(struct nbd_device *nbd) |
| 1080 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1081 | struct nbd_config *config = nbd->config; |
Al Viro | c9f2b6a | 2015-11-12 05:09:35 -0500 | [diff] [blame] | 1082 | struct nbd_request request = { |
| 1083 | .magic = htonl(NBD_REQUEST_MAGIC), |
| 1084 | .type = htonl(NBD_CMD_DISC), |
| 1085 | }; |
| 1086 | struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)}; |
| 1087 | struct iov_iter from; |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 1088 | int i, ret; |
| 1089 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1090 | for (i = 0; i < config->num_connections; i++) { |
Josef Bacik | b4b2aec | 2017-07-21 10:48:14 -0400 | [diff] [blame] | 1091 | struct nbd_sock *nsock = config->socks[i]; |
| 1092 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1093 | iov_iter_kvec(&from, WRITE, &iov, 1, sizeof(request)); |
Josef Bacik | b4b2aec | 2017-07-21 10:48:14 -0400 | [diff] [blame] | 1094 | mutex_lock(&nsock->tx_lock); |
Josef Bacik | 9dd5d3a | 2017-03-24 14:08:26 -0400 | [diff] [blame] | 1095 | ret = sock_xmit(nbd, i, 1, &from, 0, NULL); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 1096 | if (ret <= 0) |
| 1097 | dev_err(disk_to_dev(nbd->disk), |
| 1098 | "Send disconnect failed %d\n", ret); |
Josef Bacik | b4b2aec | 2017-07-21 10:48:14 -0400 | [diff] [blame] | 1099 | mutex_unlock(&nsock->tx_lock); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1103 | static int nbd_disconnect(struct nbd_device *nbd) |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1104 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1105 | struct nbd_config *config = nbd->config; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1106 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1107 | dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n"); |
Josef Bacik | 2e13456 | 2017-07-21 10:48:13 -0400 | [diff] [blame] | 1108 | set_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags); |
| 1109 | send_disconnects(nbd); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1110 | return 0; |
| 1111 | } |
| 1112 | |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1113 | static void nbd_clear_sock(struct nbd_device *nbd) |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1114 | { |
| 1115 | sock_shutdown(nbd); |
| 1116 | nbd_clear_que(nbd); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1117 | nbd->task_setup = NULL; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1118 | } |
| 1119 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1120 | static void nbd_config_put(struct nbd_device *nbd) |
| 1121 | { |
| 1122 | if (refcount_dec_and_mutex_lock(&nbd->config_refs, |
| 1123 | &nbd->config_lock)) { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1124 | struct nbd_config *config = nbd->config; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1125 | nbd_dev_dbg_close(nbd); |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1126 | nbd_size_clear(nbd); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1127 | if (test_and_clear_bit(NBD_HAS_PID_FILE, |
| 1128 | &config->runtime_flags)) |
| 1129 | device_remove_file(disk_to_dev(nbd->disk), &pid_attr); |
| 1130 | nbd->task_recv = NULL; |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1131 | nbd_clear_sock(nbd); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1132 | if (config->num_connections) { |
| 1133 | int i; |
| 1134 | for (i = 0; i < config->num_connections; i++) { |
| 1135 | sockfd_put(config->socks[i]->sock); |
| 1136 | kfree(config->socks[i]); |
| 1137 | } |
| 1138 | kfree(config->socks); |
| 1139 | } |
Ilya Dryomov | fa97653 | 2017-05-23 17:49:55 +0200 | [diff] [blame] | 1140 | kfree(nbd->config); |
Ilya Dryomov | af622b8 | 2017-05-23 17:49:54 +0200 | [diff] [blame] | 1141 | nbd->config = NULL; |
| 1142 | |
| 1143 | nbd->tag_set.timeout = 0; |
Josef Bacik | 6df133a | 2018-05-23 13:35:59 -0400 | [diff] [blame] | 1144 | nbd->disk->queue->limits.discard_granularity = 0; |
Josef Bacik | 07ce213 | 2018-06-05 11:41:23 -0400 | [diff] [blame] | 1145 | nbd->disk->queue->limits.discard_alignment = 0; |
Josef Bacik | 6df133a | 2018-05-23 13:35:59 -0400 | [diff] [blame] | 1146 | blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX); |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 1147 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, nbd->disk->queue); |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 1148 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1149 | mutex_unlock(&nbd->config_lock); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1150 | nbd_put(nbd); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1151 | module_put(THIS_MODULE); |
| 1152 | } |
| 1153 | } |
| 1154 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1155 | static int nbd_start_device(struct nbd_device *nbd) |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1156 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1157 | struct nbd_config *config = nbd->config; |
| 1158 | int num_connections = config->num_connections; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1159 | int error = 0, i; |
| 1160 | |
| 1161 | if (nbd->task_recv) |
| 1162 | return -EBUSY; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1163 | if (!config->socks) |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1164 | return -EINVAL; |
| 1165 | if (num_connections > 1 && |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1166 | !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) { |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1167 | dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n"); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1168 | return -EINVAL; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1169 | } |
| 1170 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1171 | blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1172 | nbd->task_recv = current; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1173 | |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1174 | nbd_parse_flags(nbd); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1175 | |
| 1176 | error = device_create_file(disk_to_dev(nbd->disk), &pid_attr); |
| 1177 | if (error) { |
| 1178 | dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n"); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1179 | return error; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1180 | } |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1181 | set_bit(NBD_HAS_PID_FILE, &config->runtime_flags); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1182 | |
| 1183 | nbd_dev_dbg_init(nbd); |
| 1184 | for (i = 0; i < num_connections; i++) { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1185 | struct recv_thread_args *args; |
| 1186 | |
| 1187 | args = kzalloc(sizeof(*args), GFP_KERNEL); |
| 1188 | if (!args) { |
| 1189 | sock_shutdown(nbd); |
| 1190 | return -ENOMEM; |
| 1191 | } |
| 1192 | sk_set_memalloc(config->socks[i]->sock->sk); |
Josef Bacik | a7ee8cf | 2017-07-21 10:48:15 -0400 | [diff] [blame] | 1193 | if (nbd->tag_set.timeout) |
| 1194 | config->socks[i]->sock->sk->sk_sndtimeo = |
| 1195 | nbd->tag_set.timeout; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1196 | atomic_inc(&config->recv_threads); |
| 1197 | refcount_inc(&nbd->config_refs); |
| 1198 | INIT_WORK(&args->work, recv_work); |
| 1199 | args->nbd = nbd; |
| 1200 | args->index = i; |
| 1201 | queue_work(recv_workqueue, &args->work); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1202 | } |
Josef Bacik | 639812a | 2017-10-09 13:12:10 -0400 | [diff] [blame] | 1203 | nbd_size_update(nbd); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1204 | return error; |
| 1205 | } |
| 1206 | |
| 1207 | static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *bdev) |
| 1208 | { |
| 1209 | struct nbd_config *config = nbd->config; |
| 1210 | int ret; |
| 1211 | |
| 1212 | ret = nbd_start_device(nbd); |
| 1213 | if (ret) |
| 1214 | return ret; |
| 1215 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1216 | if (max_part) |
| 1217 | bdev->bd_invalidated = 1; |
| 1218 | mutex_unlock(&nbd->config_lock); |
| 1219 | ret = wait_event_interruptible(config->recv_wq, |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1220 | atomic_read(&config->recv_threads) == 0); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1221 | if (ret) |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1222 | sock_shutdown(nbd); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1223 | mutex_lock(&nbd->config_lock); |
Josef Bacik | 76aa1d3 | 2018-05-16 14:51:22 -0400 | [diff] [blame] | 1224 | nbd_bdev_reset(bdev); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1225 | /* user requested, ignore socket errors */ |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1226 | if (test_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags)) |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1227 | ret = 0; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1228 | if (test_bit(NBD_TIMEDOUT, &config->runtime_flags)) |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1229 | ret = -ETIMEDOUT; |
| 1230 | return ret; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1231 | } |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1232 | |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1233 | static void nbd_clear_sock_ioctl(struct nbd_device *nbd, |
| 1234 | struct block_device *bdev) |
| 1235 | { |
Josef Bacik | 2516ab1 | 2017-04-06 17:02:03 -0400 | [diff] [blame] | 1236 | sock_shutdown(nbd); |
Munehisa Kamata | 2b5c8f0 | 2019-07-31 20:13:10 +0800 | [diff] [blame] | 1237 | __invalidate_device(bdev, true); |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1238 | nbd_bdev_reset(bdev); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1239 | if (test_and_clear_bit(NBD_HAS_CONFIG_REF, |
| 1240 | &nbd->config->runtime_flags)) |
| 1241 | nbd_config_put(nbd); |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1242 | } |
| 1243 | |
Xiubo Li | 553768d | 2019-05-29 15:16:05 -0500 | [diff] [blame] | 1244 | static bool nbd_is_valid_blksize(unsigned long blksize) |
| 1245 | { |
| 1246 | if (!blksize || !is_power_of_2(blksize) || blksize < 512 || |
| 1247 | blksize > PAGE_SIZE) |
| 1248 | return false; |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
Mike Christie | 55313e9 | 2019-08-13 11:39:49 -0500 | [diff] [blame] | 1252 | static void nbd_set_cmd_timeout(struct nbd_device *nbd, u64 timeout) |
| 1253 | { |
| 1254 | nbd->tag_set.timeout = timeout * HZ; |
| 1255 | blk_queue_rq_timeout(nbd->disk->queue, timeout * HZ); |
| 1256 | } |
| 1257 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 1258 | /* Must be called with config_lock held */ |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 1259 | static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 1260 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1262 | struct nbd_config *config = nbd->config; |
| 1263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | switch (cmd) { |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1265 | case NBD_DISCONNECT: |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1266 | return nbd_disconnect(nbd); |
Markus Pargmann | 23272a67 | 2015-10-29 11:51:16 +0100 | [diff] [blame] | 1267 | case NBD_CLEAR_SOCK: |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1268 | nbd_clear_sock_ioctl(nbd, bdev); |
| 1269 | return 0; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1270 | case NBD_SET_SOCK: |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1271 | return nbd_add_socket(nbd, arg, false); |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1272 | case NBD_SET_BLKSIZE: |
Xiubo Li | 553768d | 2019-05-29 15:16:05 -0500 | [diff] [blame] | 1273 | if (!arg) |
| 1274 | arg = NBD_DEF_BLKSIZE; |
| 1275 | if (!nbd_is_valid_blksize(arg)) |
Jens Axboe | bc811f0 | 2018-09-04 11:52:34 -0600 | [diff] [blame] | 1276 | return -EINVAL; |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1277 | nbd_size_set(nbd, arg, |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1278 | div_s64(config->bytesize, arg)); |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 1279 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | case NBD_SET_SIZE: |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1281 | nbd_size_set(nbd, config->blksize, |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1282 | div_s64(arg, config->blksize)); |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 1283 | return 0; |
Markus Pargmann | 37091fd | 2015-07-27 07:36:49 +0200 | [diff] [blame] | 1284 | case NBD_SET_SIZE_BLOCKS: |
Josef Bacik | 29eaadc | 2017-04-06 17:01:59 -0400 | [diff] [blame] | 1285 | nbd_size_set(nbd, config->blksize, arg); |
Josef Bacik | e544541 | 2017-02-13 10:39:47 -0500 | [diff] [blame] | 1286 | return 0; |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 1287 | case NBD_SET_TIMEOUT: |
Mike Christie | 55313e9 | 2019-08-13 11:39:49 -0500 | [diff] [blame] | 1288 | if (arg) |
| 1289 | nbd_set_cmd_timeout(nbd, arg); |
Paul Clements | 7fdfd40 | 2007-10-16 23:27:37 -0700 | [diff] [blame] | 1290 | return 0; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 1291 | |
Paul Clements | 2f01250 | 2012-10-04 17:16:15 -0700 | [diff] [blame] | 1292 | case NBD_SET_FLAGS: |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1293 | config->flags = arg; |
Paul Clements | 2f01250 | 2012-10-04 17:16:15 -0700 | [diff] [blame] | 1294 | return 0; |
Josef Bacik | 9442b73 | 2017-02-07 17:10:22 -0500 | [diff] [blame] | 1295 | case NBD_DO_IT: |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1296 | return nbd_start_device_ioctl(nbd, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | case NBD_CLEAR_QUE: |
Herbert Xu | 4b2f026 | 2006-01-06 00:09:47 -0800 | [diff] [blame] | 1298 | /* |
| 1299 | * This is for compatibility only. The queue is always cleared |
| 1300 | * by NBD_DO_IT or NBD_CLEAR_SOCK. |
| 1301 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | return 0; |
| 1303 | case NBD_PRINT_DEBUG: |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1304 | /* |
| 1305 | * For compatibility only, we no longer keep a list of |
| 1306 | * outstanding requests. |
| 1307 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 1310 | return -ENOTTY; |
| 1311 | } |
| 1312 | |
| 1313 | static int nbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 1314 | unsigned int cmd, unsigned long arg) |
| 1315 | { |
Wanlong Gao | f450716 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 1316 | struct nbd_device *nbd = bdev->bd_disk->private_data; |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1317 | struct nbd_config *config = nbd->config; |
| 1318 | int error = -EINVAL; |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 1319 | |
| 1320 | if (!capable(CAP_SYS_ADMIN)) |
| 1321 | return -EPERM; |
| 1322 | |
Josef Bacik | 1dae69b | 2017-05-05 22:25:18 -0400 | [diff] [blame] | 1323 | /* The block layer will pass back some non-nbd ioctls in case we have |
| 1324 | * special handling for them, but we don't so just return an error. |
| 1325 | */ |
| 1326 | if (_IOC_TYPE(cmd) != 0xab) |
| 1327 | return -EINVAL; |
| 1328 | |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 1329 | mutex_lock(&nbd->config_lock); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1330 | |
| 1331 | /* Don't allow ioctl operations on a nbd device that was created with |
| 1332 | * netlink, unless it's DISCONNECT or CLEAR_SOCK, which are fine. |
| 1333 | */ |
| 1334 | if (!test_bit(NBD_BOUND, &config->runtime_flags) || |
| 1335 | (cmd == NBD_DISCONNECT || cmd == NBD_CLEAR_SOCK)) |
| 1336 | error = __nbd_ioctl(bdev, nbd, cmd, arg); |
| 1337 | else |
| 1338 | dev_err(nbd_to_dev(nbd), "Cannot use ioctl interface on a netlink controlled device.\n"); |
Josef Bacik | 9561a7a | 2016-11-22 14:04:40 -0500 | [diff] [blame] | 1339 | mutex_unlock(&nbd->config_lock); |
Pavel Machek | 1a2ad21 | 2009-04-02 16:58:41 -0700 | [diff] [blame] | 1340 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1343 | static struct nbd_config *nbd_alloc_config(void) |
| 1344 | { |
| 1345 | struct nbd_config *config; |
| 1346 | |
| 1347 | config = kzalloc(sizeof(struct nbd_config), GFP_NOFS); |
| 1348 | if (!config) |
| 1349 | return NULL; |
| 1350 | atomic_set(&config->recv_threads, 0); |
| 1351 | init_waitqueue_head(&config->recv_wq); |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 1352 | init_waitqueue_head(&config->conn_wait); |
Xiubo Li | 553768d | 2019-05-29 15:16:05 -0500 | [diff] [blame] | 1353 | config->blksize = NBD_DEF_BLKSIZE; |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 1354 | atomic_set(&config->live_connections, 0); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1355 | try_module_get(THIS_MODULE); |
| 1356 | return config; |
| 1357 | } |
| 1358 | |
| 1359 | static int nbd_open(struct block_device *bdev, fmode_t mode) |
| 1360 | { |
| 1361 | struct nbd_device *nbd; |
| 1362 | int ret = 0; |
| 1363 | |
| 1364 | mutex_lock(&nbd_index_mutex); |
| 1365 | nbd = bdev->bd_disk->private_data; |
| 1366 | if (!nbd) { |
| 1367 | ret = -ENXIO; |
| 1368 | goto out; |
| 1369 | } |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1370 | if (!refcount_inc_not_zero(&nbd->refs)) { |
| 1371 | ret = -ENXIO; |
| 1372 | goto out; |
| 1373 | } |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1374 | if (!refcount_inc_not_zero(&nbd->config_refs)) { |
| 1375 | struct nbd_config *config; |
| 1376 | |
| 1377 | mutex_lock(&nbd->config_lock); |
| 1378 | if (refcount_inc_not_zero(&nbd->config_refs)) { |
| 1379 | mutex_unlock(&nbd->config_lock); |
| 1380 | goto out; |
| 1381 | } |
| 1382 | config = nbd->config = nbd_alloc_config(); |
| 1383 | if (!config) { |
| 1384 | ret = -ENOMEM; |
| 1385 | mutex_unlock(&nbd->config_lock); |
| 1386 | goto out; |
| 1387 | } |
| 1388 | refcount_set(&nbd->config_refs, 1); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1389 | refcount_inc(&nbd->refs); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1390 | mutex_unlock(&nbd->config_lock); |
Josef Bacik | fe1f9e6 | 2018-05-16 14:51:21 -0400 | [diff] [blame] | 1391 | bdev->bd_invalidated = 1; |
| 1392 | } else if (nbd_disconnected(nbd->config)) { |
| 1393 | bdev->bd_invalidated = 1; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1394 | } |
| 1395 | out: |
| 1396 | mutex_unlock(&nbd_index_mutex); |
| 1397 | return ret; |
| 1398 | } |
| 1399 | |
| 1400 | static void nbd_release(struct gendisk *disk, fmode_t mode) |
| 1401 | { |
| 1402 | struct nbd_device *nbd = disk->private_data; |
Doron Roberts-Kedes | 08ba91e | 2018-06-15 14:05:32 -0700 | [diff] [blame] | 1403 | struct block_device *bdev = bdget_disk(disk, 0); |
| 1404 | |
| 1405 | if (test_bit(NBD_DISCONNECT_ON_CLOSE, &nbd->config->runtime_flags) && |
| 1406 | bdev->bd_openers == 0) |
| 1407 | nbd_disconnect_and_put(nbd); |
| 1408 | |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1409 | nbd_config_put(nbd); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1410 | nbd_put(nbd); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1411 | } |
| 1412 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1413 | static const struct block_device_operations nbd_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | { |
| 1415 | .owner = THIS_MODULE, |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1416 | .open = nbd_open, |
| 1417 | .release = nbd_release, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1418 | .ioctl = nbd_ioctl, |
Al Viro | 263a3df | 2016-01-07 10:04:37 -0500 | [diff] [blame] | 1419 | .compat_ioctl = nbd_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | }; |
| 1421 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1422 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 1423 | |
| 1424 | static int nbd_dbg_tasks_show(struct seq_file *s, void *unused) |
| 1425 | { |
| 1426 | struct nbd_device *nbd = s->private; |
| 1427 | |
| 1428 | if (nbd->task_recv) |
| 1429 | seq_printf(s, "recv: %d\n", task_pid_nr(nbd->task_recv)); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1430 | |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | static int nbd_dbg_tasks_open(struct inode *inode, struct file *file) |
| 1435 | { |
| 1436 | return single_open(file, nbd_dbg_tasks_show, inode->i_private); |
| 1437 | } |
| 1438 | |
| 1439 | static const struct file_operations nbd_dbg_tasks_ops = { |
| 1440 | .open = nbd_dbg_tasks_open, |
| 1441 | .read = seq_read, |
| 1442 | .llseek = seq_lseek, |
| 1443 | .release = single_release, |
| 1444 | }; |
| 1445 | |
| 1446 | static int nbd_dbg_flags_show(struct seq_file *s, void *unused) |
| 1447 | { |
| 1448 | struct nbd_device *nbd = s->private; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1449 | u32 flags = nbd->config->flags; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1450 | |
| 1451 | seq_printf(s, "Hex: 0x%08x\n\n", flags); |
| 1452 | |
| 1453 | seq_puts(s, "Known flags:\n"); |
| 1454 | |
| 1455 | if (flags & NBD_FLAG_HAS_FLAGS) |
| 1456 | seq_puts(s, "NBD_FLAG_HAS_FLAGS\n"); |
| 1457 | if (flags & NBD_FLAG_READ_ONLY) |
| 1458 | seq_puts(s, "NBD_FLAG_READ_ONLY\n"); |
| 1459 | if (flags & NBD_FLAG_SEND_FLUSH) |
| 1460 | seq_puts(s, "NBD_FLAG_SEND_FLUSH\n"); |
Shaun McDowell | 685c9b2 | 2017-05-25 23:55:54 -0400 | [diff] [blame] | 1461 | if (flags & NBD_FLAG_SEND_FUA) |
| 1462 | seq_puts(s, "NBD_FLAG_SEND_FUA\n"); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1463 | if (flags & NBD_FLAG_SEND_TRIM) |
| 1464 | seq_puts(s, "NBD_FLAG_SEND_TRIM\n"); |
| 1465 | |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | static int nbd_dbg_flags_open(struct inode *inode, struct file *file) |
| 1470 | { |
| 1471 | return single_open(file, nbd_dbg_flags_show, inode->i_private); |
| 1472 | } |
| 1473 | |
| 1474 | static const struct file_operations nbd_dbg_flags_ops = { |
| 1475 | .open = nbd_dbg_flags_open, |
| 1476 | .read = seq_read, |
| 1477 | .llseek = seq_lseek, |
| 1478 | .release = single_release, |
| 1479 | }; |
| 1480 | |
| 1481 | static int nbd_dev_dbg_init(struct nbd_device *nbd) |
| 1482 | { |
| 1483 | struct dentry *dir; |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1484 | struct nbd_config *config = nbd->config; |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 1485 | |
| 1486 | if (!nbd_dbg_dir) |
| 1487 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1488 | |
| 1489 | dir = debugfs_create_dir(nbd_name(nbd), nbd_dbg_dir); |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 1490 | if (!dir) { |
| 1491 | dev_err(nbd_to_dev(nbd), "Failed to create debugfs dir for '%s'\n", |
| 1492 | nbd_name(nbd)); |
| 1493 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1494 | } |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1495 | config->dbg_dir = dir; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1496 | |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 1497 | debugfs_create_file("tasks", 0444, dir, nbd, &nbd_dbg_tasks_ops); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1498 | debugfs_create_u64("size_bytes", 0444, dir, &config->bytesize); |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 1499 | debugfs_create_u32("timeout", 0444, dir, &nbd->tag_set.timeout); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1500 | debugfs_create_u64("blocksize", 0444, dir, &config->blksize); |
Josef Bacik | d366a0f | 2016-06-08 10:32:10 -0400 | [diff] [blame] | 1501 | debugfs_create_file("flags", 0444, dir, nbd, &nbd_dbg_flags_ops); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | static void nbd_dev_dbg_close(struct nbd_device *nbd) |
| 1507 | { |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1508 | debugfs_remove_recursive(nbd->config->dbg_dir); |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | static int nbd_dbg_init(void) |
| 1512 | { |
| 1513 | struct dentry *dbg_dir; |
| 1514 | |
| 1515 | dbg_dir = debugfs_create_dir("nbd", NULL); |
Markus Pargmann | 27ea43f | 2015-10-24 21:15:34 +0200 | [diff] [blame] | 1516 | if (!dbg_dir) |
| 1517 | return -EIO; |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 1518 | |
| 1519 | nbd_dbg_dir = dbg_dir; |
| 1520 | |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | static void nbd_dbg_close(void) |
| 1525 | { |
| 1526 | debugfs_remove_recursive(nbd_dbg_dir); |
| 1527 | } |
| 1528 | |
| 1529 | #else /* IS_ENABLED(CONFIG_DEBUG_FS) */ |
| 1530 | |
| 1531 | static int nbd_dev_dbg_init(struct nbd_device *nbd) |
| 1532 | { |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
| 1536 | static void nbd_dev_dbg_close(struct nbd_device *nbd) |
| 1537 | { |
| 1538 | } |
| 1539 | |
| 1540 | static int nbd_dbg_init(void) |
| 1541 | { |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | static void nbd_dbg_close(void) |
| 1546 | { |
| 1547 | } |
| 1548 | |
| 1549 | #endif |
| 1550 | |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 1551 | static int nbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 1552 | unsigned int hctx_idx, unsigned int numa_node) |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1553 | { |
| 1554 | struct nbd_cmd *cmd = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 1555 | cmd->nbd = set->driver_data; |
Josef Bacik | d7d94d4 | 2018-07-16 12:11:34 -0400 | [diff] [blame] | 1556 | cmd->flags = 0; |
Josef Bacik | 8f3ea35 | 2018-07-16 12:11:35 -0400 | [diff] [blame] | 1557 | mutex_init(&cmd->lock); |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1558 | return 0; |
| 1559 | } |
| 1560 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 1561 | static const struct blk_mq_ops nbd_mq_ops = { |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1562 | .queue_rq = nbd_queue_rq, |
Christoph Hellwig | 1e388ae | 2017-04-20 16:03:06 +0200 | [diff] [blame] | 1563 | .complete = nbd_complete_rq, |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1564 | .init_request = nbd_init_request, |
Josef Bacik | 0eadf37 | 2016-09-08 12:33:40 -0700 | [diff] [blame] | 1565 | .timeout = nbd_xmit_timeout, |
Josef Bacik | fd8383f | 2016-09-08 12:33:37 -0700 | [diff] [blame] | 1566 | }; |
| 1567 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1568 | static int nbd_dev_add(int index) |
| 1569 | { |
| 1570 | struct nbd_device *nbd; |
| 1571 | struct gendisk *disk; |
| 1572 | struct request_queue *q; |
| 1573 | int err = -ENOMEM; |
| 1574 | |
| 1575 | nbd = kzalloc(sizeof(struct nbd_device), GFP_KERNEL); |
| 1576 | if (!nbd) |
| 1577 | goto out; |
| 1578 | |
| 1579 | disk = alloc_disk(1 << part_shift); |
| 1580 | if (!disk) |
| 1581 | goto out_free_nbd; |
| 1582 | |
| 1583 | if (index >= 0) { |
| 1584 | err = idr_alloc(&nbd_index_idr, nbd, index, index + 1, |
| 1585 | GFP_KERNEL); |
| 1586 | if (err == -ENOSPC) |
| 1587 | err = -EEXIST; |
| 1588 | } else { |
| 1589 | err = idr_alloc(&nbd_index_idr, nbd, 0, 0, GFP_KERNEL); |
| 1590 | if (err >= 0) |
| 1591 | index = err; |
| 1592 | } |
| 1593 | if (err < 0) |
| 1594 | goto out_free_disk; |
| 1595 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1596 | nbd->index = index; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1597 | nbd->disk = disk; |
| 1598 | nbd->tag_set.ops = &nbd_mq_ops; |
| 1599 | nbd->tag_set.nr_hw_queues = 1; |
| 1600 | nbd->tag_set.queue_depth = 128; |
| 1601 | nbd->tag_set.numa_node = NUMA_NO_NODE; |
| 1602 | nbd->tag_set.cmd_size = sizeof(struct nbd_cmd); |
| 1603 | nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | |
Ming Lei | 56d18f6 | 2019-02-15 19:13:24 +0800 | [diff] [blame] | 1604 | BLK_MQ_F_BLOCKING; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1605 | nbd->tag_set.driver_data = nbd; |
| 1606 | |
| 1607 | err = blk_mq_alloc_tag_set(&nbd->tag_set); |
| 1608 | if (err) |
| 1609 | goto out_free_idr; |
| 1610 | |
| 1611 | q = blk_mq_init_queue(&nbd->tag_set); |
| 1612 | if (IS_ERR(q)) { |
| 1613 | err = PTR_ERR(q); |
| 1614 | goto out_free_tags; |
| 1615 | } |
| 1616 | disk->queue = q; |
| 1617 | |
| 1618 | /* |
| 1619 | * Tell the block layer that we are not a rotational device |
| 1620 | */ |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 1621 | blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue); |
| 1622 | blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue); |
Josef Bacik | 6df133a | 2018-05-23 13:35:59 -0400 | [diff] [blame] | 1623 | disk->queue->limits.discard_granularity = 0; |
Josef Bacik | 07ce213 | 2018-06-05 11:41:23 -0400 | [diff] [blame] | 1624 | disk->queue->limits.discard_alignment = 0; |
Josef Bacik | 6df133a | 2018-05-23 13:35:59 -0400 | [diff] [blame] | 1625 | blk_queue_max_discard_sectors(disk->queue, 0); |
Josef Bacik | ebb16d0 | 2017-04-18 16:22:51 -0400 | [diff] [blame] | 1626 | blk_queue_max_segment_size(disk->queue, UINT_MAX); |
Josef Bacik | 1cc1f17 | 2017-04-20 15:47:01 -0400 | [diff] [blame] | 1627 | blk_queue_max_segments(disk->queue, USHRT_MAX); |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1628 | blk_queue_max_hw_sectors(disk->queue, 65536); |
| 1629 | disk->queue->limits.max_sectors = 256; |
| 1630 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1631 | mutex_init(&nbd->config_lock); |
Josef Bacik | 5ea8d10 | 2017-04-06 17:01:58 -0400 | [diff] [blame] | 1632 | refcount_set(&nbd->config_refs, 0); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1633 | refcount_set(&nbd->refs, 1); |
| 1634 | INIT_LIST_HEAD(&nbd->list); |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1635 | disk->major = NBD_MAJOR; |
| 1636 | disk->first_minor = index << part_shift; |
| 1637 | disk->fops = &nbd_fops; |
| 1638 | disk->private_data = nbd; |
| 1639 | sprintf(disk->disk_name, "nbd%d", index); |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1640 | add_disk(disk); |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 1641 | nbd_total_devices++; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 1642 | return index; |
| 1643 | |
| 1644 | out_free_tags: |
| 1645 | blk_mq_free_tag_set(&nbd->tag_set); |
| 1646 | out_free_idr: |
| 1647 | idr_remove(&nbd_index_idr, index); |
| 1648 | out_free_disk: |
| 1649 | put_disk(disk); |
| 1650 | out_free_nbd: |
| 1651 | kfree(nbd); |
| 1652 | out: |
| 1653 | return err; |
| 1654 | } |
| 1655 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1656 | static int find_free_cb(int id, void *ptr, void *data) |
| 1657 | { |
| 1658 | struct nbd_device *nbd = ptr; |
| 1659 | struct nbd_device **found = data; |
| 1660 | |
| 1661 | if (!refcount_read(&nbd->config_refs)) { |
| 1662 | *found = nbd; |
| 1663 | return 1; |
| 1664 | } |
| 1665 | return 0; |
| 1666 | } |
| 1667 | |
| 1668 | /* Netlink interface. */ |
Stephen Hemminger | a86c412 | 2018-07-18 09:32:43 -0700 | [diff] [blame] | 1669 | static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = { |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1670 | [NBD_ATTR_INDEX] = { .type = NLA_U32 }, |
| 1671 | [NBD_ATTR_SIZE_BYTES] = { .type = NLA_U64 }, |
| 1672 | [NBD_ATTR_BLOCK_SIZE_BYTES] = { .type = NLA_U64 }, |
| 1673 | [NBD_ATTR_TIMEOUT] = { .type = NLA_U64 }, |
| 1674 | [NBD_ATTR_SERVER_FLAGS] = { .type = NLA_U64 }, |
| 1675 | [NBD_ATTR_CLIENT_FLAGS] = { .type = NLA_U64 }, |
| 1676 | [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED}, |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 1677 | [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 }, |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 1678 | [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED}, |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1679 | }; |
| 1680 | |
Stephen Hemminger | a86c412 | 2018-07-18 09:32:43 -0700 | [diff] [blame] | 1681 | static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = { |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1682 | [NBD_SOCK_FD] = { .type = NLA_U32 }, |
| 1683 | }; |
| 1684 | |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 1685 | /* We don't use this right now since we don't parse the incoming list, but we |
| 1686 | * still want it here so userspace knows what to expect. |
| 1687 | */ |
Stephen Hemminger | a86c412 | 2018-07-18 09:32:43 -0700 | [diff] [blame] | 1688 | static const struct nla_policy __attribute__((unused)) |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 1689 | nbd_device_policy[NBD_DEVICE_ATTR_MAX + 1] = { |
| 1690 | [NBD_DEVICE_INDEX] = { .type = NLA_U32 }, |
| 1691 | [NBD_DEVICE_CONNECTED] = { .type = NLA_U8 }, |
| 1692 | }; |
| 1693 | |
Mike Christie | 4ddeaae8 | 2019-05-29 15:16:06 -0500 | [diff] [blame] | 1694 | static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd) |
| 1695 | { |
| 1696 | struct nbd_config *config = nbd->config; |
| 1697 | u64 bsize = config->blksize; |
| 1698 | u64 bytes = config->bytesize; |
| 1699 | |
| 1700 | if (info->attrs[NBD_ATTR_SIZE_BYTES]) |
| 1701 | bytes = nla_get_u64(info->attrs[NBD_ATTR_SIZE_BYTES]); |
| 1702 | |
| 1703 | if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) { |
| 1704 | bsize = nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]); |
| 1705 | if (!bsize) |
| 1706 | bsize = NBD_DEF_BLKSIZE; |
| 1707 | if (!nbd_is_valid_blksize(bsize)) { |
| 1708 | printk(KERN_ERR "Invalid block size %llu\n", bsize); |
| 1709 | return -EINVAL; |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | if (bytes != config->bytesize || bsize != config->blksize) |
| 1714 | nbd_size_set(nbd, bsize, div64_u64(bytes, bsize)); |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1718 | static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info) |
| 1719 | { |
| 1720 | struct nbd_device *nbd = NULL; |
| 1721 | struct nbd_config *config; |
| 1722 | int index = -1; |
| 1723 | int ret; |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 1724 | bool put_dev = false; |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1725 | |
| 1726 | if (!netlink_capable(skb, CAP_SYS_ADMIN)) |
| 1727 | return -EPERM; |
| 1728 | |
| 1729 | if (info->attrs[NBD_ATTR_INDEX]) |
| 1730 | index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]); |
| 1731 | if (!info->attrs[NBD_ATTR_SOCKETS]) { |
| 1732 | printk(KERN_ERR "nbd: must specify at least one socket\n"); |
| 1733 | return -EINVAL; |
| 1734 | } |
| 1735 | if (!info->attrs[NBD_ATTR_SIZE_BYTES]) { |
| 1736 | printk(KERN_ERR "nbd: must specify a size in bytes for the device\n"); |
| 1737 | return -EINVAL; |
| 1738 | } |
| 1739 | again: |
| 1740 | mutex_lock(&nbd_index_mutex); |
| 1741 | if (index == -1) { |
| 1742 | ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd); |
| 1743 | if (ret == 0) { |
| 1744 | int new_index; |
| 1745 | new_index = nbd_dev_add(-1); |
| 1746 | if (new_index < 0) { |
| 1747 | mutex_unlock(&nbd_index_mutex); |
| 1748 | printk(KERN_ERR "nbd: failed to add new device\n"); |
Gustavo A. R. Silva | 0979962 | 2018-02-12 11:14:55 -0600 | [diff] [blame] | 1749 | return new_index; |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1750 | } |
| 1751 | nbd = idr_find(&nbd_index_idr, new_index); |
| 1752 | } |
| 1753 | } else { |
| 1754 | nbd = idr_find(&nbd_index_idr, index); |
Josef Bacik | e6a7627 | 2017-08-14 18:25:33 +0000 | [diff] [blame] | 1755 | if (!nbd) { |
| 1756 | ret = nbd_dev_add(index); |
| 1757 | if (ret < 0) { |
| 1758 | mutex_unlock(&nbd_index_mutex); |
| 1759 | printk(KERN_ERR "nbd: failed to add new device\n"); |
| 1760 | return ret; |
| 1761 | } |
| 1762 | nbd = idr_find(&nbd_index_idr, index); |
| 1763 | } |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1764 | } |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1765 | if (!nbd) { |
| 1766 | printk(KERN_ERR "nbd: couldn't find device at index %d\n", |
| 1767 | index); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1768 | mutex_unlock(&nbd_index_mutex); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1769 | return -EINVAL; |
| 1770 | } |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1771 | if (!refcount_inc_not_zero(&nbd->refs)) { |
| 1772 | mutex_unlock(&nbd_index_mutex); |
| 1773 | if (index == -1) |
| 1774 | goto again; |
| 1775 | printk(KERN_ERR "nbd: device at index %d is going down\n", |
| 1776 | index); |
| 1777 | return -EINVAL; |
| 1778 | } |
| 1779 | mutex_unlock(&nbd_index_mutex); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1780 | |
| 1781 | mutex_lock(&nbd->config_lock); |
| 1782 | if (refcount_read(&nbd->config_refs)) { |
| 1783 | mutex_unlock(&nbd->config_lock); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1784 | nbd_put(nbd); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1785 | if (index == -1) |
| 1786 | goto again; |
| 1787 | printk(KERN_ERR "nbd: nbd%d already in use\n", index); |
| 1788 | return -EBUSY; |
| 1789 | } |
| 1790 | if (WARN_ON(nbd->config)) { |
| 1791 | mutex_unlock(&nbd->config_lock); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1792 | nbd_put(nbd); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1793 | return -EINVAL; |
| 1794 | } |
| 1795 | config = nbd->config = nbd_alloc_config(); |
| 1796 | if (!nbd->config) { |
| 1797 | mutex_unlock(&nbd->config_lock); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1798 | nbd_put(nbd); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1799 | printk(KERN_ERR "nbd: couldn't allocate config\n"); |
| 1800 | return -ENOMEM; |
| 1801 | } |
| 1802 | refcount_set(&nbd->config_refs, 1); |
| 1803 | set_bit(NBD_BOUND, &config->runtime_flags); |
| 1804 | |
Mike Christie | 4ddeaae8 | 2019-05-29 15:16:06 -0500 | [diff] [blame] | 1805 | ret = nbd_genl_size_set(info, nbd); |
| 1806 | if (ret) |
| 1807 | goto out; |
| 1808 | |
Mike Christie | 55313e9 | 2019-08-13 11:39:49 -0500 | [diff] [blame] | 1809 | if (info->attrs[NBD_ATTR_TIMEOUT]) |
| 1810 | nbd_set_cmd_timeout(nbd, |
| 1811 | nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT])); |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 1812 | if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) { |
| 1813 | config->dead_conn_timeout = |
| 1814 | nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]); |
| 1815 | config->dead_conn_timeout *= HZ; |
| 1816 | } |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1817 | if (info->attrs[NBD_ATTR_SERVER_FLAGS]) |
| 1818 | config->flags = |
| 1819 | nla_get_u64(info->attrs[NBD_ATTR_SERVER_FLAGS]); |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 1820 | if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) { |
| 1821 | u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]); |
| 1822 | if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) { |
| 1823 | set_bit(NBD_DESTROY_ON_DISCONNECT, |
| 1824 | &config->runtime_flags); |
| 1825 | put_dev = true; |
| 1826 | } |
Doron Roberts-Kedes | 08ba91e | 2018-06-15 14:05:32 -0700 | [diff] [blame] | 1827 | if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) { |
| 1828 | set_bit(NBD_DISCONNECT_ON_CLOSE, |
| 1829 | &config->runtime_flags); |
| 1830 | } |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 1831 | } |
| 1832 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1833 | if (info->attrs[NBD_ATTR_SOCKETS]) { |
| 1834 | struct nlattr *attr; |
| 1835 | int rem, fd; |
| 1836 | |
| 1837 | nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS], |
| 1838 | rem) { |
| 1839 | struct nlattr *socks[NBD_SOCK_MAX+1]; |
| 1840 | |
| 1841 | if (nla_type(attr) != NBD_SOCK_ITEM) { |
| 1842 | printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n"); |
| 1843 | ret = -EINVAL; |
| 1844 | goto out; |
| 1845 | } |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1846 | ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX, |
| 1847 | attr, |
| 1848 | nbd_sock_policy, |
| 1849 | info->extack); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1850 | if (ret != 0) { |
| 1851 | printk(KERN_ERR "nbd: error processing sock list\n"); |
| 1852 | ret = -EINVAL; |
| 1853 | goto out; |
| 1854 | } |
| 1855 | if (!socks[NBD_SOCK_FD]) |
| 1856 | continue; |
| 1857 | fd = (int)nla_get_u32(socks[NBD_SOCK_FD]); |
| 1858 | ret = nbd_add_socket(nbd, fd, true); |
| 1859 | if (ret) |
| 1860 | goto out; |
| 1861 | } |
| 1862 | } |
| 1863 | ret = nbd_start_device(nbd); |
| 1864 | out: |
| 1865 | mutex_unlock(&nbd->config_lock); |
| 1866 | if (!ret) { |
| 1867 | set_bit(NBD_HAS_CONFIG_REF, &config->runtime_flags); |
| 1868 | refcount_inc(&nbd->config_refs); |
| 1869 | nbd_connect_reply(info, nbd->index); |
| 1870 | } |
| 1871 | nbd_config_put(nbd); |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 1872 | if (put_dev) |
| 1873 | nbd_put(nbd); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1874 | return ret; |
| 1875 | } |
| 1876 | |
Doron Roberts-Kedes | 08ba91e | 2018-06-15 14:05:32 -0700 | [diff] [blame] | 1877 | static void nbd_disconnect_and_put(struct nbd_device *nbd) |
| 1878 | { |
| 1879 | mutex_lock(&nbd->config_lock); |
| 1880 | nbd_disconnect(nbd); |
| 1881 | nbd_clear_sock(nbd); |
| 1882 | mutex_unlock(&nbd->config_lock); |
| 1883 | if (test_and_clear_bit(NBD_HAS_CONFIG_REF, |
| 1884 | &nbd->config->runtime_flags)) |
| 1885 | nbd_config_put(nbd); |
| 1886 | } |
| 1887 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1888 | static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info) |
| 1889 | { |
| 1890 | struct nbd_device *nbd; |
| 1891 | int index; |
| 1892 | |
| 1893 | if (!netlink_capable(skb, CAP_SYS_ADMIN)) |
| 1894 | return -EPERM; |
| 1895 | |
| 1896 | if (!info->attrs[NBD_ATTR_INDEX]) { |
| 1897 | printk(KERN_ERR "nbd: must specify an index to disconnect\n"); |
| 1898 | return -EINVAL; |
| 1899 | } |
| 1900 | index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]); |
| 1901 | mutex_lock(&nbd_index_mutex); |
| 1902 | nbd = idr_find(&nbd_index_idr, index); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1903 | if (!nbd) { |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1904 | mutex_unlock(&nbd_index_mutex); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1905 | printk(KERN_ERR "nbd: couldn't find device at index %d\n", |
| 1906 | index); |
| 1907 | return -EINVAL; |
| 1908 | } |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1909 | if (!refcount_inc_not_zero(&nbd->refs)) { |
| 1910 | mutex_unlock(&nbd_index_mutex); |
| 1911 | printk(KERN_ERR "nbd: device at index %d is going down\n", |
| 1912 | index); |
| 1913 | return -EINVAL; |
| 1914 | } |
| 1915 | mutex_unlock(&nbd_index_mutex); |
| 1916 | if (!refcount_inc_not_zero(&nbd->config_refs)) { |
| 1917 | nbd_put(nbd); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1918 | return 0; |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1919 | } |
Doron Roberts-Kedes | 08ba91e | 2018-06-15 14:05:32 -0700 | [diff] [blame] | 1920 | nbd_disconnect_and_put(nbd); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1921 | nbd_config_put(nbd); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1922 | nbd_put(nbd); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 1923 | return 0; |
| 1924 | } |
| 1925 | |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1926 | static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info) |
| 1927 | { |
| 1928 | struct nbd_device *nbd = NULL; |
| 1929 | struct nbd_config *config; |
| 1930 | int index; |
Doron Roberts-Kedes | 08ba91e | 2018-06-15 14:05:32 -0700 | [diff] [blame] | 1931 | int ret = 0; |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 1932 | bool put_dev = false; |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1933 | |
| 1934 | if (!netlink_capable(skb, CAP_SYS_ADMIN)) |
| 1935 | return -EPERM; |
| 1936 | |
| 1937 | if (!info->attrs[NBD_ATTR_INDEX]) { |
| 1938 | printk(KERN_ERR "nbd: must specify a device to reconfigure\n"); |
| 1939 | return -EINVAL; |
| 1940 | } |
| 1941 | index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]); |
| 1942 | mutex_lock(&nbd_index_mutex); |
| 1943 | nbd = idr_find(&nbd_index_idr, index); |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1944 | if (!nbd) { |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1945 | mutex_unlock(&nbd_index_mutex); |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1946 | printk(KERN_ERR "nbd: couldn't find a device at index %d\n", |
| 1947 | index); |
| 1948 | return -EINVAL; |
| 1949 | } |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1950 | if (!refcount_inc_not_zero(&nbd->refs)) { |
| 1951 | mutex_unlock(&nbd_index_mutex); |
| 1952 | printk(KERN_ERR "nbd: device at index %d is going down\n", |
| 1953 | index); |
| 1954 | return -EINVAL; |
| 1955 | } |
| 1956 | mutex_unlock(&nbd_index_mutex); |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1957 | |
| 1958 | if (!refcount_inc_not_zero(&nbd->config_refs)) { |
| 1959 | dev_err(nbd_to_dev(nbd), |
| 1960 | "not configured, cannot reconfigure\n"); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 1961 | nbd_put(nbd); |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1962 | return -EINVAL; |
| 1963 | } |
| 1964 | |
| 1965 | mutex_lock(&nbd->config_lock); |
| 1966 | config = nbd->config; |
| 1967 | if (!test_bit(NBD_BOUND, &config->runtime_flags) || |
| 1968 | !nbd->task_recv) { |
| 1969 | dev_err(nbd_to_dev(nbd), |
| 1970 | "not configured, cannot reconfigure\n"); |
Doron Roberts-Kedes | 08ba91e | 2018-06-15 14:05:32 -0700 | [diff] [blame] | 1971 | ret = -EINVAL; |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 1972 | goto out; |
| 1973 | } |
| 1974 | |
Mike Christie | 4ddeaae8 | 2019-05-29 15:16:06 -0500 | [diff] [blame] | 1975 | ret = nbd_genl_size_set(info, nbd); |
| 1976 | if (ret) |
| 1977 | goto out; |
| 1978 | |
Mike Christie | 55313e9 | 2019-08-13 11:39:49 -0500 | [diff] [blame] | 1979 | if (info->attrs[NBD_ATTR_TIMEOUT]) |
| 1980 | nbd_set_cmd_timeout(nbd, |
| 1981 | nla_get_u64(info->attrs[NBD_ATTR_TIMEOUT])); |
Josef Bacik | 560bc4b | 2017-04-06 17:02:04 -0400 | [diff] [blame] | 1982 | if (info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]) { |
| 1983 | config->dead_conn_timeout = |
| 1984 | nla_get_u64(info->attrs[NBD_ATTR_DEAD_CONN_TIMEOUT]); |
| 1985 | config->dead_conn_timeout *= HZ; |
| 1986 | } |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 1987 | if (info->attrs[NBD_ATTR_CLIENT_FLAGS]) { |
| 1988 | u64 flags = nla_get_u64(info->attrs[NBD_ATTR_CLIENT_FLAGS]); |
| 1989 | if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) { |
| 1990 | if (!test_and_set_bit(NBD_DESTROY_ON_DISCONNECT, |
| 1991 | &config->runtime_flags)) |
| 1992 | put_dev = true; |
| 1993 | } else { |
| 1994 | if (test_and_clear_bit(NBD_DESTROY_ON_DISCONNECT, |
| 1995 | &config->runtime_flags)) |
| 1996 | refcount_inc(&nbd->refs); |
| 1997 | } |
Doron Roberts-Kedes | 08ba91e | 2018-06-15 14:05:32 -0700 | [diff] [blame] | 1998 | |
| 1999 | if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) { |
| 2000 | set_bit(NBD_DISCONNECT_ON_CLOSE, |
| 2001 | &config->runtime_flags); |
| 2002 | } else { |
| 2003 | clear_bit(NBD_DISCONNECT_ON_CLOSE, |
| 2004 | &config->runtime_flags); |
| 2005 | } |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 2006 | } |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 2007 | |
| 2008 | if (info->attrs[NBD_ATTR_SOCKETS]) { |
| 2009 | struct nlattr *attr; |
| 2010 | int rem, fd; |
| 2011 | |
| 2012 | nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS], |
| 2013 | rem) { |
| 2014 | struct nlattr *socks[NBD_SOCK_MAX+1]; |
| 2015 | |
| 2016 | if (nla_type(attr) != NBD_SOCK_ITEM) { |
| 2017 | printk(KERN_ERR "nbd: socks must be embedded in a SOCK_ITEM attr\n"); |
| 2018 | ret = -EINVAL; |
| 2019 | goto out; |
| 2020 | } |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2021 | ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX, |
| 2022 | attr, |
| 2023 | nbd_sock_policy, |
| 2024 | info->extack); |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 2025 | if (ret != 0) { |
| 2026 | printk(KERN_ERR "nbd: error processing sock list\n"); |
| 2027 | ret = -EINVAL; |
| 2028 | goto out; |
| 2029 | } |
| 2030 | if (!socks[NBD_SOCK_FD]) |
| 2031 | continue; |
| 2032 | fd = (int)nla_get_u32(socks[NBD_SOCK_FD]); |
| 2033 | ret = nbd_reconnect_socket(nbd, fd); |
| 2034 | if (ret) { |
| 2035 | if (ret == -ENOSPC) |
| 2036 | ret = 0; |
| 2037 | goto out; |
| 2038 | } |
| 2039 | dev_info(nbd_to_dev(nbd), "reconnected socket\n"); |
| 2040 | } |
| 2041 | } |
| 2042 | out: |
| 2043 | mutex_unlock(&nbd->config_lock); |
| 2044 | nbd_config_put(nbd); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 2045 | nbd_put(nbd); |
Josef Bacik | a2c9790 | 2017-04-06 17:02:07 -0400 | [diff] [blame] | 2046 | if (put_dev) |
| 2047 | nbd_put(nbd); |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 2048 | return ret; |
| 2049 | } |
| 2050 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 2051 | static const struct genl_ops nbd_connect_genl_ops[] = { |
| 2052 | { |
| 2053 | .cmd = NBD_CMD_CONNECT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 2054 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 2055 | .doit = nbd_genl_connect, |
| 2056 | }, |
| 2057 | { |
| 2058 | .cmd = NBD_CMD_DISCONNECT, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 2059 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 2060 | .doit = nbd_genl_disconnect, |
| 2061 | }, |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 2062 | { |
| 2063 | .cmd = NBD_CMD_RECONFIGURE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 2064 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Josef Bacik | b7aa3d3 | 2017-04-06 17:02:01 -0400 | [diff] [blame] | 2065 | .doit = nbd_genl_reconfigure, |
| 2066 | }, |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 2067 | { |
| 2068 | .cmd = NBD_CMD_STATUS, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 2069 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 2070 | .doit = nbd_genl_status, |
| 2071 | }, |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 2072 | }; |
| 2073 | |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 2074 | static const struct genl_multicast_group nbd_mcast_grps[] = { |
| 2075 | { .name = NBD_GENL_MCAST_GROUP_NAME, }, |
| 2076 | }; |
| 2077 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 2078 | static struct genl_family nbd_genl_family __ro_after_init = { |
| 2079 | .hdrsize = 0, |
| 2080 | .name = NBD_GENL_FAMILY_NAME, |
| 2081 | .version = NBD_GENL_VERSION, |
| 2082 | .module = THIS_MODULE, |
| 2083 | .ops = nbd_connect_genl_ops, |
| 2084 | .n_ops = ARRAY_SIZE(nbd_connect_genl_ops), |
| 2085 | .maxattr = NBD_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 2086 | .policy = nbd_attr_policy, |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 2087 | .mcgrps = nbd_mcast_grps, |
| 2088 | .n_mcgrps = ARRAY_SIZE(nbd_mcast_grps), |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 2089 | }; |
| 2090 | |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 2091 | static int populate_nbd_status(struct nbd_device *nbd, struct sk_buff *reply) |
| 2092 | { |
| 2093 | struct nlattr *dev_opt; |
| 2094 | u8 connected = 0; |
| 2095 | int ret; |
| 2096 | |
| 2097 | /* This is a little racey, but for status it's ok. The |
| 2098 | * reason we don't take a ref here is because we can't |
| 2099 | * take a ref in the index == -1 case as we would need |
| 2100 | * to put under the nbd_index_mutex, which could |
| 2101 | * deadlock if we are configured to remove ourselves |
| 2102 | * once we're disconnected. |
| 2103 | */ |
| 2104 | if (refcount_read(&nbd->config_refs)) |
| 2105 | connected = 1; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2106 | dev_opt = nla_nest_start_noflag(reply, NBD_DEVICE_ITEM); |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 2107 | if (!dev_opt) |
| 2108 | return -EMSGSIZE; |
| 2109 | ret = nla_put_u32(reply, NBD_DEVICE_INDEX, nbd->index); |
| 2110 | if (ret) |
| 2111 | return -EMSGSIZE; |
| 2112 | ret = nla_put_u8(reply, NBD_DEVICE_CONNECTED, |
| 2113 | connected); |
| 2114 | if (ret) |
| 2115 | return -EMSGSIZE; |
| 2116 | nla_nest_end(reply, dev_opt); |
| 2117 | return 0; |
| 2118 | } |
| 2119 | |
| 2120 | static int status_cb(int id, void *ptr, void *data) |
| 2121 | { |
| 2122 | struct nbd_device *nbd = ptr; |
| 2123 | return populate_nbd_status(nbd, (struct sk_buff *)data); |
| 2124 | } |
| 2125 | |
| 2126 | static int nbd_genl_status(struct sk_buff *skb, struct genl_info *info) |
| 2127 | { |
| 2128 | struct nlattr *dev_list; |
| 2129 | struct sk_buff *reply; |
| 2130 | void *reply_head; |
| 2131 | size_t msg_size; |
| 2132 | int index = -1; |
| 2133 | int ret = -ENOMEM; |
| 2134 | |
| 2135 | if (info->attrs[NBD_ATTR_INDEX]) |
| 2136 | index = nla_get_u32(info->attrs[NBD_ATTR_INDEX]); |
| 2137 | |
| 2138 | mutex_lock(&nbd_index_mutex); |
| 2139 | |
| 2140 | msg_size = nla_total_size(nla_attr_size(sizeof(u32)) + |
| 2141 | nla_attr_size(sizeof(u8))); |
| 2142 | msg_size *= (index == -1) ? nbd_total_devices : 1; |
| 2143 | |
| 2144 | reply = genlmsg_new(msg_size, GFP_KERNEL); |
| 2145 | if (!reply) |
| 2146 | goto out; |
| 2147 | reply_head = genlmsg_put_reply(reply, info, &nbd_genl_family, 0, |
| 2148 | NBD_CMD_STATUS); |
| 2149 | if (!reply_head) { |
| 2150 | nlmsg_free(reply); |
| 2151 | goto out; |
| 2152 | } |
| 2153 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2154 | dev_list = nla_nest_start_noflag(reply, NBD_ATTR_DEVICE_LIST); |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 2155 | if (index == -1) { |
| 2156 | ret = idr_for_each(&nbd_index_idr, &status_cb, reply); |
| 2157 | if (ret) { |
| 2158 | nlmsg_free(reply); |
| 2159 | goto out; |
| 2160 | } |
| 2161 | } else { |
| 2162 | struct nbd_device *nbd; |
| 2163 | nbd = idr_find(&nbd_index_idr, index); |
| 2164 | if (nbd) { |
| 2165 | ret = populate_nbd_status(nbd, reply); |
| 2166 | if (ret) { |
| 2167 | nlmsg_free(reply); |
| 2168 | goto out; |
| 2169 | } |
| 2170 | } |
| 2171 | } |
| 2172 | nla_nest_end(reply, dev_list); |
| 2173 | genlmsg_end(reply, reply_head); |
Li RongQing | cd46eb8 | 2019-02-19 13:14:07 +0800 | [diff] [blame] | 2174 | ret = genlmsg_reply(reply, info); |
Josef Bacik | 47d902b | 2017-04-06 17:02:05 -0400 | [diff] [blame] | 2175 | out: |
| 2176 | mutex_unlock(&nbd_index_mutex); |
| 2177 | return ret; |
| 2178 | } |
| 2179 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 2180 | static void nbd_connect_reply(struct genl_info *info, int index) |
| 2181 | { |
| 2182 | struct sk_buff *skb; |
| 2183 | void *msg_head; |
| 2184 | int ret; |
| 2185 | |
| 2186 | skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL); |
| 2187 | if (!skb) |
| 2188 | return; |
| 2189 | msg_head = genlmsg_put_reply(skb, info, &nbd_genl_family, 0, |
| 2190 | NBD_CMD_CONNECT); |
| 2191 | if (!msg_head) { |
| 2192 | nlmsg_free(skb); |
| 2193 | return; |
| 2194 | } |
| 2195 | ret = nla_put_u32(skb, NBD_ATTR_INDEX, index); |
| 2196 | if (ret) { |
| 2197 | nlmsg_free(skb); |
| 2198 | return; |
| 2199 | } |
| 2200 | genlmsg_end(skb, msg_head); |
| 2201 | genlmsg_reply(skb, info); |
| 2202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | |
Josef Bacik | 799f9a3 | 2017-04-06 17:02:02 -0400 | [diff] [blame] | 2204 | static void nbd_mcast_index(int index) |
| 2205 | { |
| 2206 | struct sk_buff *skb; |
| 2207 | void *msg_head; |
| 2208 | int ret; |
| 2209 | |
| 2210 | skb = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL); |
| 2211 | if (!skb) |
| 2212 | return; |
| 2213 | msg_head = genlmsg_put(skb, 0, 0, &nbd_genl_family, 0, |
| 2214 | NBD_CMD_LINK_DEAD); |
| 2215 | if (!msg_head) { |
| 2216 | nlmsg_free(skb); |
| 2217 | return; |
| 2218 | } |
| 2219 | ret = nla_put_u32(skb, NBD_ATTR_INDEX, index); |
| 2220 | if (ret) { |
| 2221 | nlmsg_free(skb); |
| 2222 | return; |
| 2223 | } |
| 2224 | genlmsg_end(skb, msg_head); |
| 2225 | genlmsg_multicast(&nbd_genl_family, skb, 0, 0, GFP_KERNEL); |
| 2226 | } |
| 2227 | |
| 2228 | static void nbd_dead_link_work(struct work_struct *work) |
| 2229 | { |
| 2230 | struct link_dead_args *args = container_of(work, struct link_dead_args, |
| 2231 | work); |
| 2232 | nbd_mcast_index(args->index); |
| 2233 | kfree(args); |
| 2234 | } |
| 2235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | static int __init nbd_init(void) |
| 2237 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | int i; |
| 2239 | |
Adrian Bunk | 5b7b18c | 2006-03-25 03:07:04 -0800 | [diff] [blame] | 2240 | BUILD_BUG_ON(sizeof(struct nbd_request) != 28); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 2242 | if (max_part < 0) { |
WANG Cong | 7742ce4 | 2011-08-19 14:48:28 +0200 | [diff] [blame] | 2243 | printk(KERN_ERR "nbd: max_part must be >= 0\n"); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 2244 | return -EINVAL; |
| 2245 | } |
| 2246 | |
| 2247 | part_shift = 0; |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 2248 | if (max_part > 0) { |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 2249 | part_shift = fls(max_part); |
| 2250 | |
Namhyung Kim | 5988ce2 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 2251 | /* |
| 2252 | * Adjust max_part according to part_shift as it is exported |
| 2253 | * to user space so that user can know the max number of |
| 2254 | * partition kernel should be able to manage. |
| 2255 | * |
| 2256 | * Note that -1 is required because partition 0 is reserved |
| 2257 | * for the whole disk. |
| 2258 | */ |
| 2259 | max_part = (1UL << part_shift) - 1; |
| 2260 | } |
| 2261 | |
Namhyung Kim | 3b27108 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 2262 | if ((1UL << part_shift) > DISK_MAX_PARTS) |
| 2263 | return -EINVAL; |
| 2264 | |
| 2265 | if (nbds_max > 1UL << (MINORBITS - part_shift)) |
| 2266 | return -EINVAL; |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 2267 | recv_workqueue = alloc_workqueue("knbd-recv", |
Dan Melnic | 2189c97 | 2017-09-18 13:08:51 -0700 | [diff] [blame] | 2268 | WQ_MEM_RECLAIM | WQ_HIGHPRI | |
| 2269 | WQ_UNBOUND, 0); |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 2270 | if (!recv_workqueue) |
| 2271 | return -ENOMEM; |
Namhyung Kim | 3b27108 | 2011-05-28 14:44:46 +0200 | [diff] [blame] | 2272 | |
Josef Bacik | 6330a2d | 2017-02-15 16:49:48 -0500 | [diff] [blame] | 2273 | if (register_blkdev(NBD_MAJOR, "nbd")) { |
| 2274 | destroy_workqueue(recv_workqueue); |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 2275 | return -EIO; |
Josef Bacik | 6330a2d | 2017-02-15 16:49:48 -0500 | [diff] [blame] | 2276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 2278 | if (genl_register_family(&nbd_genl_family)) { |
| 2279 | unregister_blkdev(NBD_MAJOR, "nbd"); |
| 2280 | destroy_workqueue(recv_workqueue); |
| 2281 | return -EINVAL; |
| 2282 | } |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 2283 | nbd_dbg_init(); |
| 2284 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 2285 | mutex_lock(&nbd_index_mutex); |
| 2286 | for (i = 0; i < nbds_max; i++) |
| 2287 | nbd_dev_add(i); |
| 2288 | mutex_unlock(&nbd_index_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | return 0; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | static int nbd_exit_cb(int id, void *ptr, void *data) |
| 2293 | { |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 2294 | struct list_head *list = (struct list_head *)data; |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 2295 | struct nbd_device *nbd = ptr; |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 2296 | |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 2297 | list_add_tail(&nbd->list, list); |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 2298 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | static void __exit nbd_cleanup(void) |
| 2302 | { |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 2303 | struct nbd_device *nbd; |
| 2304 | LIST_HEAD(del_list); |
| 2305 | |
Markus Pargmann | 30d53d9 | 2015-08-17 08:20:06 +0200 | [diff] [blame] | 2306 | nbd_dbg_close(); |
| 2307 | |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 2308 | mutex_lock(&nbd_index_mutex); |
| 2309 | idr_for_each(&nbd_index_idr, &nbd_exit_cb, &del_list); |
| 2310 | mutex_unlock(&nbd_index_mutex); |
| 2311 | |
Josef Bacik | 60ae36a | 2017-04-28 09:49:19 -0400 | [diff] [blame] | 2312 | while (!list_empty(&del_list)) { |
| 2313 | nbd = list_first_entry(&del_list, struct nbd_device, list); |
| 2314 | list_del_init(&nbd->list); |
| 2315 | if (refcount_read(&nbd->refs) != 1) |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 2316 | printk(KERN_ERR "nbd: possibly leaking a device\n"); |
| 2317 | nbd_put(nbd); |
Josef Bacik | c6a4759 | 2017-04-06 17:02:06 -0400 | [diff] [blame] | 2318 | } |
| 2319 | |
Josef Bacik | b0d9111 | 2017-02-01 16:11:40 -0500 | [diff] [blame] | 2320 | idr_destroy(&nbd_index_idr); |
Josef Bacik | e46c728 | 2017-04-06 17:02:00 -0400 | [diff] [blame] | 2321 | genl_unregister_family(&nbd_genl_family); |
Josef Bacik | 124d6db | 2017-02-01 16:11:11 -0500 | [diff] [blame] | 2322 | destroy_workqueue(recv_workqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | unregister_blkdev(NBD_MAJOR, "nbd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | module_init(nbd_init); |
| 2327 | module_exit(nbd_cleanup); |
| 2328 | |
| 2329 | MODULE_DESCRIPTION("Network Block Device"); |
| 2330 | MODULE_LICENSE("GPL"); |
| 2331 | |
Lars Marowsky-Bree | 40be0c2 | 2005-05-01 08:59:07 -0700 | [diff] [blame] | 2332 | module_param(nbds_max, int, 0444); |
Laurent Vivier | d71a6d7 | 2008-04-29 01:02:51 -0700 | [diff] [blame] | 2333 | MODULE_PARM_DESC(nbds_max, "number of network block devices to initialize (default: 16)"); |
| 2334 | module_param(max_part, int, 0444); |
Josef Bacik | 7a8362a | 2017-08-14 18:56:16 +0000 | [diff] [blame] | 2335 | MODULE_PARM_DESC(max_part, "number of partitions per device (default: 16)"); |