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