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