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