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