1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * RDMA Network Block Driver 4 * 5 * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved. 6 * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved. 7 * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved. 8 */ 9 #undef pr_fmt 10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt 11 12 #include <linux/module.h> 13 #include <linux/blkdev.h> 14 15 #include "rnbd-srv.h" 16 #include "rnbd-srv-dev.h" 17 18 MODULE_DESCRIPTION("RDMA Network Block Device Server"); 19 MODULE_LICENSE("GPL"); 20 21 static u16 port_nr = RTRS_PORT; 22 23 module_param_named(port_nr, port_nr, ushort, 0444); 24 MODULE_PARM_DESC(port_nr, 25 "The port number the server is listening on (default: " 26 __stringify(RTRS_PORT)")"); 27 28 #define DEFAULT_DEV_SEARCH_PATH "/" 29 30 static char dev_search_path[PATH_MAX] = DEFAULT_DEV_SEARCH_PATH; 31 32 static int dev_search_path_set(const char *val, const struct kernel_param *kp) 33 { 34 const char *p = strrchr(val, '\n') ? : val + strlen(val); 35 36 if (strlen(val) >= sizeof(dev_search_path)) 37 return -EINVAL; 38 39 snprintf(dev_search_path, sizeof(dev_search_path), "%.*s", 40 (int)(p - val), val); 41 42 pr_info("dev_search_path changed to '%s'\n", dev_search_path); 43 44 return 0; 45 } 46 47 static struct kparam_string dev_search_path_kparam_str = { 48 .maxlen = sizeof(dev_search_path), 49 .string = dev_search_path 50 }; 51 52 static const struct kernel_param_ops dev_search_path_ops = { 53 .set = dev_search_path_set, 54 .get = param_get_string, 55 }; 56 57 module_param_cb(dev_search_path, &dev_search_path_ops, 58 &dev_search_path_kparam_str, 0444); 59 MODULE_PARM_DESC(dev_search_path, 60 "Sets the dev_search_path. When a device is mapped this path is prepended to the device path from the map device operation. If %SESSNAME% is specified in a path, then device will be searched in a session namespace. (default: " 61 DEFAULT_DEV_SEARCH_PATH ")"); 62 63 static DEFINE_MUTEX(sess_lock); 64 static DEFINE_SPINLOCK(dev_lock); 65 66 static LIST_HEAD(sess_list); 67 static LIST_HEAD(dev_list); 68 69 struct rnbd_io_private { 70 struct rtrs_srv_op *id; 71 struct rnbd_srv_sess_dev *sess_dev; 72 }; 73 74 static void rnbd_sess_dev_release(struct kref *kref) 75 { 76 struct rnbd_srv_sess_dev *sess_dev; 77 78 sess_dev = container_of(kref, struct rnbd_srv_sess_dev, kref); 79 complete(sess_dev->destroy_comp); 80 } 81 82 static inline void rnbd_put_sess_dev(struct rnbd_srv_sess_dev *sess_dev) 83 { 84 kref_put(&sess_dev->kref, rnbd_sess_dev_release); 85 } 86 87 void rnbd_endio(void *priv, int error) 88 { 89 struct rnbd_io_private *rnbd_priv = priv; 90 struct rnbd_srv_sess_dev *sess_dev = rnbd_priv->sess_dev; 91 92 rnbd_put_sess_dev(sess_dev); 93 94 rtrs_srv_resp_rdma(rnbd_priv->id, error); 95 96 kfree(priv); 97 } 98 99 static struct rnbd_srv_sess_dev * 100 rnbd_get_sess_dev(int dev_id, struct rnbd_srv_session *srv_sess) 101 { 102 struct rnbd_srv_sess_dev *sess_dev; 103 int ret = 0; 104 105 rcu_read_lock(); 106 sess_dev = xa_load(&srv_sess->index_idr, dev_id); 107 if (likely(sess_dev)) 108 ret = kref_get_unless_zero(&sess_dev->kref); 109 rcu_read_unlock(); 110 111 if (!sess_dev || !ret) 112 return ERR_PTR(-ENXIO); 113 114 return sess_dev; 115 } 116 117 static int process_rdma(struct rtrs_srv *sess, 118 struct rnbd_srv_session *srv_sess, 119 struct rtrs_srv_op *id, void *data, u32 datalen, 120 const void *usr, size_t usrlen) 121 { 122 const struct rnbd_msg_io *msg = usr; 123 struct rnbd_io_private *priv; 124 struct rnbd_srv_sess_dev *sess_dev; 125 u32 dev_id; 126 int err; 127 struct rnbd_dev_blk_io *io; 128 struct bio *bio; 129 short prio; 130 131 priv = kmalloc(sizeof(*priv), GFP_KERNEL); 132 if (!priv) 133 return -ENOMEM; 134 135 dev_id = le32_to_cpu(msg->device_id); 136 137 sess_dev = rnbd_get_sess_dev(dev_id, srv_sess); 138 if (IS_ERR(sess_dev)) { 139 pr_err_ratelimited("Got I/O request on session %s for unknown device id %d\n", 140 srv_sess->sessname, dev_id); 141 err = -ENOTCONN; 142 goto err; 143 } 144 145 priv->sess_dev = sess_dev; 146 priv->id = id; 147 148 /* Generate bio with pages pointing to the rdma buffer */ 149 bio = rnbd_bio_map_kern(data, sess_dev->rnbd_dev->ibd_bio_set, datalen, GFP_KERNEL); 150 if (IS_ERR(bio)) { 151 err = PTR_ERR(bio); 152 rnbd_srv_err(sess_dev, "Failed to generate bio, err: %d\n", err); 153 goto sess_dev_put; 154 } 155 156 io = container_of(bio, struct rnbd_dev_blk_io, bio); 157 io->dev = sess_dev->rnbd_dev; 158 io->priv = priv; 159 160 bio->bi_end_io = rnbd_dev_bi_end_io; 161 bio->bi_private = io; 162 bio->bi_opf = rnbd_to_bio_flags(le32_to_cpu(msg->rw)); 163 bio->bi_iter.bi_sector = le64_to_cpu(msg->sector); 164 bio->bi_iter.bi_size = le32_to_cpu(msg->bi_size); 165 prio = srv_sess->ver < RNBD_PROTO_VER_MAJOR || 166 usrlen < sizeof(*msg) ? 0 : le16_to_cpu(msg->prio); 167 bio_set_prio(bio, prio); 168 bio_set_dev(bio, sess_dev->rnbd_dev->bdev); 169 170 submit_bio(bio); 171 172 return 0; 173 174 sess_dev_put: 175 rnbd_put_sess_dev(sess_dev); 176 err: 177 kfree(priv); 178 return err; 179 } 180 181 static void destroy_device(struct rnbd_srv_dev *dev) 182 { 183 WARN_ONCE(!list_empty(&dev->sess_dev_list), 184 "Device %s is being destroyed but still in use!\n", 185 dev->id); 186 187 spin_lock(&dev_lock); 188 list_del(&dev->list); 189 spin_unlock(&dev_lock); 190 191 mutex_destroy(&dev->lock); 192 if (dev->dev_kobj.state_in_sysfs) 193 /* 194 * Destroy kobj only if it was really created. 195 */ 196 rnbd_srv_destroy_dev_sysfs(dev); 197 else 198 kfree(dev); 199 } 200 201 static void destroy_device_cb(struct kref *kref) 202 { 203 struct rnbd_srv_dev *dev; 204 205 dev = container_of(kref, struct rnbd_srv_dev, kref); 206 207 destroy_device(dev); 208 } 209 210 static void rnbd_put_srv_dev(struct rnbd_srv_dev *dev) 211 { 212 kref_put(&dev->kref, destroy_device_cb); 213 } 214 215 void rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev *sess_dev) 216 { 217 DECLARE_COMPLETION_ONSTACK(dc); 218 219 xa_erase(&sess_dev->sess->index_idr, sess_dev->device_id); 220 synchronize_rcu(); 221 sess_dev->destroy_comp = &dc; 222 rnbd_put_sess_dev(sess_dev); 223 wait_for_completion(&dc); /* wait for inflights to drop to zero */ 224 225 rnbd_dev_close(sess_dev->rnbd_dev); 226 list_del(&sess_dev->sess_list); 227 mutex_lock(&sess_dev->dev->lock); 228 list_del(&sess_dev->dev_list); 229 if (sess_dev->open_flags & FMODE_WRITE) 230 sess_dev->dev->open_write_cnt--; 231 mutex_unlock(&sess_dev->dev->lock); 232 233 rnbd_put_srv_dev(sess_dev->dev); 234 235 rnbd_srv_info(sess_dev, "Device closed\n"); 236 kfree(sess_dev); 237 } 238 239 static void destroy_sess(struct rnbd_srv_session *srv_sess) 240 { 241 struct rnbd_srv_sess_dev *sess_dev, *tmp; 242 243 if (list_empty(&srv_sess->sess_dev_list)) 244 goto out; 245 246 mutex_lock(&srv_sess->lock); 247 list_for_each_entry_safe(sess_dev, tmp, &srv_sess->sess_dev_list, 248 sess_list) 249 rnbd_srv_destroy_dev_session_sysfs(sess_dev); 250 mutex_unlock(&srv_sess->lock); 251 252 out: 253 xa_destroy(&srv_sess->index_idr); 254 bioset_exit(&srv_sess->sess_bio_set); 255 256 pr_info("RTRS Session %s disconnected\n", srv_sess->sessname); 257 258 mutex_lock(&sess_lock); 259 list_del(&srv_sess->list); 260 mutex_unlock(&sess_lock); 261 262 mutex_destroy(&srv_sess->lock); 263 kfree(srv_sess); 264 } 265 266 static int create_sess(struct rtrs_srv *rtrs) 267 { 268 struct rnbd_srv_session *srv_sess; 269 char sessname[NAME_MAX]; 270 int err; 271 272 err = rtrs_srv_get_sess_name(rtrs, sessname, sizeof(sessname)); 273 if (err) { 274 pr_err("rtrs_srv_get_sess_name(%s): %d\n", sessname, err); 275 276 return err; 277 } 278 srv_sess = kzalloc(sizeof(*srv_sess), GFP_KERNEL); 279 if (!srv_sess) 280 return -ENOMEM; 281 282 srv_sess->queue_depth = rtrs_srv_get_queue_depth(rtrs); 283 err = bioset_init(&srv_sess->sess_bio_set, srv_sess->queue_depth, 284 offsetof(struct rnbd_dev_blk_io, bio), 285 BIOSET_NEED_BVECS); 286 if (err) { 287 pr_err("Allocating srv_session for session %s failed\n", 288 sessname); 289 kfree(srv_sess); 290 return err; 291 } 292 293 xa_init_flags(&srv_sess->index_idr, XA_FLAGS_ALLOC); 294 INIT_LIST_HEAD(&srv_sess->sess_dev_list); 295 mutex_init(&srv_sess->lock); 296 mutex_lock(&sess_lock); 297 list_add(&srv_sess->list, &sess_list); 298 mutex_unlock(&sess_lock); 299 300 srv_sess->rtrs = rtrs; 301 strlcpy(srv_sess->sessname, sessname, sizeof(srv_sess->sessname)); 302 303 rtrs_srv_set_sess_priv(rtrs, srv_sess); 304 305 return 0; 306 } 307 308 static int rnbd_srv_link_ev(struct rtrs_srv *rtrs, 309 enum rtrs_srv_link_ev ev, void *priv) 310 { 311 struct rnbd_srv_session *srv_sess = priv; 312 313 switch (ev) { 314 case RTRS_SRV_LINK_EV_CONNECTED: 315 return create_sess(rtrs); 316 317 case RTRS_SRV_LINK_EV_DISCONNECTED: 318 if (WARN_ON_ONCE(!srv_sess)) 319 return -EINVAL; 320 321 destroy_sess(srv_sess); 322 return 0; 323 324 default: 325 pr_warn("Received unknown RTRS session event %d from session %s\n", 326 ev, srv_sess->sessname); 327 return -EINVAL; 328 } 329 } 330 331 static int process_msg_close(struct rtrs_srv *rtrs, 332 struct rnbd_srv_session *srv_sess, 333 void *data, size_t datalen, const void *usr, 334 size_t usrlen) 335 { 336 const struct rnbd_msg_close *close_msg = usr; 337 struct rnbd_srv_sess_dev *sess_dev; 338 339 sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id), 340 srv_sess); 341 if (IS_ERR(sess_dev)) 342 return 0; 343 344 rnbd_put_sess_dev(sess_dev); 345 mutex_lock(&srv_sess->lock); 346 rnbd_srv_destroy_dev_session_sysfs(sess_dev); 347 mutex_unlock(&srv_sess->lock); 348 return 0; 349 } 350 351 static int process_msg_open(struct rtrs_srv *rtrs, 352 struct rnbd_srv_session *srv_sess, 353 const void *msg, size_t len, 354 void *data, size_t datalen); 355 356 static int process_msg_sess_info(struct rtrs_srv *rtrs, 357 struct rnbd_srv_session *srv_sess, 358 const void *msg, size_t len, 359 void *data, size_t datalen); 360 361 static int rnbd_srv_rdma_ev(struct rtrs_srv *rtrs, void *priv, 362 struct rtrs_srv_op *id, int dir, 363 void *data, size_t datalen, const void *usr, 364 size_t usrlen) 365 { 366 struct rnbd_srv_session *srv_sess = priv; 367 const struct rnbd_msg_hdr *hdr = usr; 368 int ret = 0; 369 u16 type; 370 371 if (WARN_ON_ONCE(!srv_sess)) 372 return -ENODEV; 373 374 type = le16_to_cpu(hdr->type); 375 376 switch (type) { 377 case RNBD_MSG_IO: 378 return process_rdma(rtrs, srv_sess, id, data, datalen, usr, 379 usrlen); 380 case RNBD_MSG_CLOSE: 381 ret = process_msg_close(rtrs, srv_sess, data, datalen, 382 usr, usrlen); 383 break; 384 case RNBD_MSG_OPEN: 385 ret = process_msg_open(rtrs, srv_sess, usr, usrlen, 386 data, datalen); 387 break; 388 case RNBD_MSG_SESS_INFO: 389 ret = process_msg_sess_info(rtrs, srv_sess, usr, usrlen, 390 data, datalen); 391 break; 392 default: 393 pr_warn("Received unexpected message type %d with dir %d from session %s\n", 394 type, dir, srv_sess->sessname); 395 return -EINVAL; 396 } 397 398 rtrs_srv_resp_rdma(id, ret); 399 return 0; 400 } 401 402 static struct rnbd_srv_sess_dev 403 *rnbd_sess_dev_alloc(struct rnbd_srv_session *srv_sess) 404 { 405 struct rnbd_srv_sess_dev *sess_dev; 406 int error; 407 408 sess_dev = kzalloc(sizeof(*sess_dev), GFP_KERNEL); 409 if (!sess_dev) 410 return ERR_PTR(-ENOMEM); 411 412 error = xa_alloc(&srv_sess->index_idr, &sess_dev->device_id, sess_dev, 413 xa_limit_32b, GFP_NOWAIT); 414 if (error < 0) { 415 pr_warn("Allocating idr failed, err: %d\n", error); 416 kfree(sess_dev); 417 return ERR_PTR(error); 418 } 419 420 return sess_dev; 421 } 422 423 static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(const char *id) 424 { 425 struct rnbd_srv_dev *dev; 426 427 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 428 if (!dev) 429 return ERR_PTR(-ENOMEM); 430 431 strlcpy(dev->id, id, sizeof(dev->id)); 432 kref_init(&dev->kref); 433 INIT_LIST_HEAD(&dev->sess_dev_list); 434 mutex_init(&dev->lock); 435 436 return dev; 437 } 438 439 static struct rnbd_srv_dev * 440 rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev *new_dev) 441 { 442 struct rnbd_srv_dev *dev; 443 444 spin_lock(&dev_lock); 445 list_for_each_entry(dev, &dev_list, list) { 446 if (!strncmp(dev->id, new_dev->id, sizeof(dev->id))) { 447 if (!kref_get_unless_zero(&dev->kref)) 448 /* 449 * We lost the race, device is almost dead. 450 * Continue traversing to find a valid one. 451 */ 452 continue; 453 spin_unlock(&dev_lock); 454 return dev; 455 } 456 } 457 list_add(&new_dev->list, &dev_list); 458 spin_unlock(&dev_lock); 459 460 return new_dev; 461 } 462 463 static int rnbd_srv_check_update_open_perm(struct rnbd_srv_dev *srv_dev, 464 struct rnbd_srv_session *srv_sess, 465 enum rnbd_access_mode access_mode) 466 { 467 int ret = -EPERM; 468 469 mutex_lock(&srv_dev->lock); 470 471 switch (access_mode) { 472 case RNBD_ACCESS_RO: 473 ret = 0; 474 break; 475 case RNBD_ACCESS_RW: 476 if (srv_dev->open_write_cnt == 0) { 477 srv_dev->open_write_cnt++; 478 ret = 0; 479 } else { 480 pr_err("Mapping device '%s' for session %s with RW permissions failed. Device already opened as 'RW' by %d client(s), access mode %s.\n", 481 srv_dev->id, srv_sess->sessname, 482 srv_dev->open_write_cnt, 483 rnbd_access_mode_str(access_mode)); 484 } 485 break; 486 case RNBD_ACCESS_MIGRATION: 487 if (srv_dev->open_write_cnt < 2) { 488 srv_dev->open_write_cnt++; 489 ret = 0; 490 } else { 491 pr_err("Mapping device '%s' for session %s with migration permissions failed. Device already opened as 'RW' by %d client(s), access mode %s.\n", 492 srv_dev->id, srv_sess->sessname, 493 srv_dev->open_write_cnt, 494 rnbd_access_mode_str(access_mode)); 495 } 496 break; 497 default: 498 pr_err("Received mapping request for device '%s' on session %s with invalid access mode: %d\n", 499 srv_dev->id, srv_sess->sessname, access_mode); 500 ret = -EINVAL; 501 } 502 503 mutex_unlock(&srv_dev->lock); 504 505 return ret; 506 } 507 508 static struct rnbd_srv_dev * 509 rnbd_srv_get_or_create_srv_dev(struct rnbd_dev *rnbd_dev, 510 struct rnbd_srv_session *srv_sess, 511 enum rnbd_access_mode access_mode) 512 { 513 int ret; 514 struct rnbd_srv_dev *new_dev, *dev; 515 516 new_dev = rnbd_srv_init_srv_dev(rnbd_dev->name); 517 if (IS_ERR(new_dev)) 518 return new_dev; 519 520 dev = rnbd_srv_find_or_add_srv_dev(new_dev); 521 if (dev != new_dev) 522 kfree(new_dev); 523 524 ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode); 525 if (ret) { 526 rnbd_put_srv_dev(dev); 527 return ERR_PTR(ret); 528 } 529 530 return dev; 531 } 532 533 static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp, 534 struct rnbd_srv_sess_dev *sess_dev) 535 { 536 struct rnbd_dev *rnbd_dev = sess_dev->rnbd_dev; 537 538 rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP); 539 rsp->device_id = 540 cpu_to_le32(sess_dev->device_id); 541 rsp->nsectors = 542 cpu_to_le64(get_capacity(rnbd_dev->bdev->bd_disk)); 543 rsp->logical_block_size = 544 cpu_to_le16(bdev_logical_block_size(rnbd_dev->bdev)); 545 rsp->physical_block_size = 546 cpu_to_le16(bdev_physical_block_size(rnbd_dev->bdev)); 547 rsp->max_segments = 548 cpu_to_le16(rnbd_dev_get_max_segs(rnbd_dev)); 549 rsp->max_hw_sectors = 550 cpu_to_le32(rnbd_dev_get_max_hw_sects(rnbd_dev)); 551 rsp->max_write_same_sectors = 552 cpu_to_le32(bdev_write_same(rnbd_dev->bdev)); 553 rsp->max_discard_sectors = 554 cpu_to_le32(rnbd_dev_get_max_discard_sects(rnbd_dev)); 555 rsp->discard_granularity = 556 cpu_to_le32(rnbd_dev_get_discard_granularity(rnbd_dev)); 557 rsp->discard_alignment = 558 cpu_to_le32(rnbd_dev_get_discard_alignment(rnbd_dev)); 559 rsp->secure_discard = 560 cpu_to_le16(rnbd_dev_get_secure_discard(rnbd_dev)); 561 rsp->rotational = 562 !blk_queue_nonrot(bdev_get_queue(rnbd_dev->bdev)); 563 } 564 565 static struct rnbd_srv_sess_dev * 566 rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess, 567 const struct rnbd_msg_open *open_msg, 568 struct rnbd_dev *rnbd_dev, fmode_t open_flags, 569 struct rnbd_srv_dev *srv_dev) 570 { 571 struct rnbd_srv_sess_dev *sdev = rnbd_sess_dev_alloc(srv_sess); 572 573 if (IS_ERR(sdev)) 574 return sdev; 575 576 kref_init(&sdev->kref); 577 578 strlcpy(sdev->pathname, open_msg->dev_name, sizeof(sdev->pathname)); 579 580 sdev->rnbd_dev = rnbd_dev; 581 sdev->sess = srv_sess; 582 sdev->dev = srv_dev; 583 sdev->open_flags = open_flags; 584 sdev->access_mode = open_msg->access_mode; 585 586 return sdev; 587 } 588 589 static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess, 590 const char *dev_name) 591 { 592 char *full_path; 593 char *a, *b; 594 595 full_path = kmalloc(PATH_MAX, GFP_KERNEL); 596 if (!full_path) 597 return ERR_PTR(-ENOMEM); 598 599 /* 600 * Replace %SESSNAME% with a real session name in order to 601 * create device namespace. 602 */ 603 a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path)); 604 if (a) { 605 int len = a - dev_search_path; 606 607 len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len, 608 dev_search_path, srv_sess->sessname, dev_name); 609 if (len >= PATH_MAX) { 610 pr_err("Too long path: %s, %s, %s\n", 611 dev_search_path, srv_sess->sessname, dev_name); 612 kfree(full_path); 613 return ERR_PTR(-EINVAL); 614 } 615 } else { 616 snprintf(full_path, PATH_MAX, "%s/%s", 617 dev_search_path, dev_name); 618 } 619 620 /* eliminitate duplicated slashes */ 621 a = strchr(full_path, '/'); 622 b = a; 623 while (*b != '\0') { 624 if (*b == '/' && *a == '/') { 625 b++; 626 } else { 627 a++; 628 *a = *b; 629 b++; 630 } 631 } 632 a++; 633 *a = '\0'; 634 635 return full_path; 636 } 637 638 static int process_msg_sess_info(struct rtrs_srv *rtrs, 639 struct rnbd_srv_session *srv_sess, 640 const void *msg, size_t len, 641 void *data, size_t datalen) 642 { 643 const struct rnbd_msg_sess_info *sess_info_msg = msg; 644 struct rnbd_msg_sess_info_rsp *rsp = data; 645 646 srv_sess->ver = min_t(u8, sess_info_msg->ver, RNBD_PROTO_VER_MAJOR); 647 pr_debug("Session %s using protocol version %d (client version: %d, server version: %d)\n", 648 srv_sess->sessname, srv_sess->ver, 649 sess_info_msg->ver, RNBD_PROTO_VER_MAJOR); 650 651 rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP); 652 rsp->ver = srv_sess->ver; 653 654 return 0; 655 } 656 657 /** 658 * find_srv_sess_dev() - a dev is already opened by this name 659 * @srv_sess: the session to search. 660 * @dev_name: string containing the name of the device. 661 * 662 * Return struct rnbd_srv_sess_dev if srv_sess already opened the dev_name 663 * NULL if the session didn't open the device yet. 664 */ 665 static struct rnbd_srv_sess_dev * 666 find_srv_sess_dev(struct rnbd_srv_session *srv_sess, const char *dev_name) 667 { 668 struct rnbd_srv_sess_dev *sess_dev; 669 670 if (list_empty(&srv_sess->sess_dev_list)) 671 return NULL; 672 673 list_for_each_entry(sess_dev, &srv_sess->sess_dev_list, sess_list) 674 if (!strcmp(sess_dev->pathname, dev_name)) 675 return sess_dev; 676 677 return NULL; 678 } 679 680 static int process_msg_open(struct rtrs_srv *rtrs, 681 struct rnbd_srv_session *srv_sess, 682 const void *msg, size_t len, 683 void *data, size_t datalen) 684 { 685 int ret; 686 struct rnbd_srv_dev *srv_dev; 687 struct rnbd_srv_sess_dev *srv_sess_dev; 688 const struct rnbd_msg_open *open_msg = msg; 689 fmode_t open_flags; 690 char *full_path; 691 struct rnbd_dev *rnbd_dev; 692 struct rnbd_msg_open_rsp *rsp = data; 693 694 pr_debug("Open message received: session='%s' path='%s' access_mode=%d\n", 695 srv_sess->sessname, open_msg->dev_name, 696 open_msg->access_mode); 697 open_flags = FMODE_READ; 698 if (open_msg->access_mode != RNBD_ACCESS_RO) 699 open_flags |= FMODE_WRITE; 700 701 mutex_lock(&srv_sess->lock); 702 703 srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name); 704 if (srv_sess_dev) 705 goto fill_response; 706 707 if ((strlen(dev_search_path) + strlen(open_msg->dev_name)) 708 >= PATH_MAX) { 709 pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n", 710 srv_sess->sessname, dev_search_path, open_msg->dev_name, 711 PATH_MAX); 712 ret = -EINVAL; 713 goto reject; 714 } 715 if (strstr(open_msg->dev_name, "..")) { 716 pr_err("Opening device for session %s failed, device path %s contains relative path ..\n", 717 srv_sess->sessname, open_msg->dev_name); 718 ret = -EINVAL; 719 goto reject; 720 } 721 full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name); 722 if (IS_ERR(full_path)) { 723 ret = PTR_ERR(full_path); 724 pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %d\n", 725 open_msg->dev_name, srv_sess->sessname, ret); 726 goto reject; 727 } 728 729 rnbd_dev = rnbd_dev_open(full_path, open_flags, 730 &srv_sess->sess_bio_set); 731 if (IS_ERR(rnbd_dev)) { 732 pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %ld\n", 733 full_path, srv_sess->sessname, PTR_ERR(rnbd_dev)); 734 ret = PTR_ERR(rnbd_dev); 735 goto free_path; 736 } 737 738 srv_dev = rnbd_srv_get_or_create_srv_dev(rnbd_dev, srv_sess, 739 open_msg->access_mode); 740 if (IS_ERR(srv_dev)) { 741 pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %ld\n", 742 full_path, srv_sess->sessname, PTR_ERR(srv_dev)); 743 ret = PTR_ERR(srv_dev); 744 goto rnbd_dev_close; 745 } 746 747 srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg, 748 rnbd_dev, open_flags, 749 srv_dev); 750 if (IS_ERR(srv_sess_dev)) { 751 pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %ld\n", 752 full_path, srv_sess->sessname, PTR_ERR(srv_sess_dev)); 753 ret = PTR_ERR(srv_sess_dev); 754 goto srv_dev_put; 755 } 756 757 /* Create the srv_dev sysfs files if they haven't been created yet. The 758 * reason to delay the creation is not to create the sysfs files before 759 * we are sure the device can be opened. 760 */ 761 mutex_lock(&srv_dev->lock); 762 if (!srv_dev->dev_kobj.state_in_sysfs) { 763 ret = rnbd_srv_create_dev_sysfs(srv_dev, rnbd_dev->bdev, 764 rnbd_dev->name); 765 if (ret) { 766 mutex_unlock(&srv_dev->lock); 767 rnbd_srv_err(srv_sess_dev, 768 "Opening device failed, failed to create device sysfs files, err: %d\n", 769 ret); 770 goto free_srv_sess_dev; 771 } 772 } 773 774 ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev); 775 if (ret) { 776 mutex_unlock(&srv_dev->lock); 777 rnbd_srv_err(srv_sess_dev, 778 "Opening device failed, failed to create dev client sysfs files, err: %d\n", 779 ret); 780 goto free_srv_sess_dev; 781 } 782 783 list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list); 784 mutex_unlock(&srv_dev->lock); 785 786 list_add(&srv_sess_dev->sess_list, &srv_sess->sess_dev_list); 787 788 rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id); 789 790 kfree(full_path); 791 792 fill_response: 793 rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev); 794 mutex_unlock(&srv_sess->lock); 795 return 0; 796 797 free_srv_sess_dev: 798 xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id); 799 synchronize_rcu(); 800 kfree(srv_sess_dev); 801 srv_dev_put: 802 if (open_msg->access_mode != RNBD_ACCESS_RO) { 803 mutex_lock(&srv_dev->lock); 804 srv_dev->open_write_cnt--; 805 mutex_unlock(&srv_dev->lock); 806 } 807 rnbd_put_srv_dev(srv_dev); 808 rnbd_dev_close: 809 rnbd_dev_close(rnbd_dev); 810 free_path: 811 kfree(full_path); 812 reject: 813 mutex_unlock(&srv_sess->lock); 814 return ret; 815 } 816 817 static struct rtrs_srv_ctx *rtrs_ctx; 818 819 static struct rtrs_srv_ops rtrs_ops; 820 static int __init rnbd_srv_init_module(void) 821 { 822 int err; 823 824 BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4); 825 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36); 826 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36); 827 BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264); 828 BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8); 829 BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56); 830 rtrs_ops = (struct rtrs_srv_ops) { 831 .rdma_ev = rnbd_srv_rdma_ev, 832 .link_ev = rnbd_srv_link_ev, 833 }; 834 rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr); 835 if (IS_ERR(rtrs_ctx)) { 836 err = PTR_ERR(rtrs_ctx); 837 pr_err("rtrs_srv_open(), err: %d\n", err); 838 return err; 839 } 840 841 err = rnbd_srv_create_sysfs_files(); 842 if (err) { 843 pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err); 844 rtrs_srv_close(rtrs_ctx); 845 return err; 846 } 847 848 return 0; 849 } 850 851 static void __exit rnbd_srv_cleanup_module(void) 852 { 853 rtrs_srv_close(rtrs_ctx); 854 WARN_ON(!list_empty(&sess_list)); 855 rnbd_srv_destroy_sysfs_files(); 856 } 857 858 module_init(rnbd_srv_init_module); 859 module_exit(rnbd_srv_cleanup_module); 860