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