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