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