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