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