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