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