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