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