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