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