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