Jack Wang | 2de6c8d | 2020-05-11 15:51:26 +0200 | [diff] [blame] | 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; |
Guoqing Jiang | 735d77d | 2020-07-30 11:13:57 +0200 | [diff] [blame] | 127 | struct rnbd_dev_blk_io *io; |
| 128 | struct bio *bio; |
| 129 | short prio; |
Jack Wang | 2de6c8d | 2020-05-11 15:51:26 +0200 | [diff] [blame] | 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 | |
Guoqing Jiang | 735d77d | 2020-07-30 11:13:57 +0200 | [diff] [blame] | 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)) { |
Nathan Chancellor | 17bc103 | 2020-08-17 23:49:25 -0700 | [diff] [blame] | 151 | err = PTR_ERR(bio); |
| 152 | rnbd_srv_err(sess_dev, "Failed to generate bio, err: %d\n", err); |
Jack Wang | 2de6c8d | 2020-05-11 15:51:26 +0200 | [diff] [blame] | 153 | goto sess_dev_put; |
| 154 | } |
| 155 | |
Guoqing Jiang | 735d77d | 2020-07-30 11:13:57 +0200 | [diff] [blame] | 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 | |
Jack Wang | 2de6c8d | 2020-05-11 15:51:26 +0200 | [diff] [blame] | 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 | |
Lutz Pogrell | 78699805 | 2020-11-26 11:47:20 +0100 | [diff] [blame^] | 215 | void rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev *sess_dev, bool keep_id) |
Jack Wang | 2de6c8d | 2020-05-11 15:51:26 +0200 | [diff] [blame] | 216 | { |
| 217 | DECLARE_COMPLETION_ONSTACK(dc); |
| 218 | |
Lutz Pogrell | 78699805 | 2020-11-26 11:47:20 +0100 | [diff] [blame^] | 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); |
Jack Wang | 2de6c8d | 2020-05-11 15:51:26 +0200 | [diff] [blame] | 227 | synchronize_rcu(); |
Lutz Pogrell | 78699805 | 2020-11-26 11:47:20 +0100 | [diff] [blame^] | 228 | |
Jack Wang | 2de6c8d | 2020-05-11 15:51:26 +0200 | [diff] [blame] | 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 | |
Lutz Pogrell | 78699805 | 2020-11-26 11:47:20 +0100 | [diff] [blame^] | 339 | void rnbd_srv_sess_dev_force_close(struct rnbd_srv_sess_dev *sess_dev) |
| 340 | { |
| 341 | rnbd_srv_destroy_dev_session_sysfs(sess_dev); |
| 342 | sess_dev->keep_id = true; |
| 343 | |
| 344 | } |
| 345 | |
Jack Wang | 2de6c8d | 2020-05-11 15:51:26 +0200 | [diff] [blame] | 346 | static int process_msg_close(struct rtrs_srv *rtrs, |
| 347 | struct rnbd_srv_session *srv_sess, |
| 348 | void *data, size_t datalen, const void *usr, |
| 349 | size_t usrlen) |
| 350 | { |
| 351 | const struct rnbd_msg_close *close_msg = usr; |
| 352 | struct rnbd_srv_sess_dev *sess_dev; |
| 353 | |
| 354 | sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id), |
| 355 | srv_sess); |
| 356 | if (IS_ERR(sess_dev)) |
| 357 | return 0; |
| 358 | |
| 359 | rnbd_put_sess_dev(sess_dev); |
| 360 | mutex_lock(&srv_sess->lock); |
| 361 | rnbd_srv_destroy_dev_session_sysfs(sess_dev); |
| 362 | mutex_unlock(&srv_sess->lock); |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int process_msg_open(struct rtrs_srv *rtrs, |
| 367 | struct rnbd_srv_session *srv_sess, |
| 368 | const void *msg, size_t len, |
| 369 | void *data, size_t datalen); |
| 370 | |
| 371 | static int process_msg_sess_info(struct rtrs_srv *rtrs, |
| 372 | struct rnbd_srv_session *srv_sess, |
| 373 | const void *msg, size_t len, |
| 374 | void *data, size_t datalen); |
| 375 | |
| 376 | static int rnbd_srv_rdma_ev(struct rtrs_srv *rtrs, void *priv, |
| 377 | struct rtrs_srv_op *id, int dir, |
| 378 | void *data, size_t datalen, const void *usr, |
| 379 | size_t usrlen) |
| 380 | { |
| 381 | struct rnbd_srv_session *srv_sess = priv; |
| 382 | const struct rnbd_msg_hdr *hdr = usr; |
| 383 | int ret = 0; |
| 384 | u16 type; |
| 385 | |
| 386 | if (WARN_ON_ONCE(!srv_sess)) |
| 387 | return -ENODEV; |
| 388 | |
| 389 | type = le16_to_cpu(hdr->type); |
| 390 | |
| 391 | switch (type) { |
| 392 | case RNBD_MSG_IO: |
| 393 | return process_rdma(rtrs, srv_sess, id, data, datalen, usr, |
| 394 | usrlen); |
| 395 | case RNBD_MSG_CLOSE: |
| 396 | ret = process_msg_close(rtrs, srv_sess, data, datalen, |
| 397 | usr, usrlen); |
| 398 | break; |
| 399 | case RNBD_MSG_OPEN: |
| 400 | ret = process_msg_open(rtrs, srv_sess, usr, usrlen, |
| 401 | data, datalen); |
| 402 | break; |
| 403 | case RNBD_MSG_SESS_INFO: |
| 404 | ret = process_msg_sess_info(rtrs, srv_sess, usr, usrlen, |
| 405 | data, datalen); |
| 406 | break; |
| 407 | default: |
| 408 | pr_warn("Received unexpected message type %d with dir %d from session %s\n", |
| 409 | type, dir, srv_sess->sessname); |
| 410 | return -EINVAL; |
| 411 | } |
| 412 | |
| 413 | rtrs_srv_resp_rdma(id, ret); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static struct rnbd_srv_sess_dev |
| 418 | *rnbd_sess_dev_alloc(struct rnbd_srv_session *srv_sess) |
| 419 | { |
| 420 | struct rnbd_srv_sess_dev *sess_dev; |
| 421 | int error; |
| 422 | |
| 423 | sess_dev = kzalloc(sizeof(*sess_dev), GFP_KERNEL); |
| 424 | if (!sess_dev) |
| 425 | return ERR_PTR(-ENOMEM); |
| 426 | |
| 427 | error = xa_alloc(&srv_sess->index_idr, &sess_dev->device_id, sess_dev, |
| 428 | xa_limit_32b, GFP_NOWAIT); |
| 429 | if (error < 0) { |
| 430 | pr_warn("Allocating idr failed, err: %d\n", error); |
| 431 | kfree(sess_dev); |
| 432 | return ERR_PTR(error); |
| 433 | } |
| 434 | |
| 435 | return sess_dev; |
| 436 | } |
| 437 | |
| 438 | static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(const char *id) |
| 439 | { |
| 440 | struct rnbd_srv_dev *dev; |
| 441 | |
| 442 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 443 | if (!dev) |
| 444 | return ERR_PTR(-ENOMEM); |
| 445 | |
| 446 | strlcpy(dev->id, id, sizeof(dev->id)); |
| 447 | kref_init(&dev->kref); |
| 448 | INIT_LIST_HEAD(&dev->sess_dev_list); |
| 449 | mutex_init(&dev->lock); |
| 450 | |
| 451 | return dev; |
| 452 | } |
| 453 | |
| 454 | static struct rnbd_srv_dev * |
| 455 | rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev *new_dev) |
| 456 | { |
| 457 | struct rnbd_srv_dev *dev; |
| 458 | |
| 459 | spin_lock(&dev_lock); |
| 460 | list_for_each_entry(dev, &dev_list, list) { |
| 461 | if (!strncmp(dev->id, new_dev->id, sizeof(dev->id))) { |
| 462 | if (!kref_get_unless_zero(&dev->kref)) |
| 463 | /* |
| 464 | * We lost the race, device is almost dead. |
| 465 | * Continue traversing to find a valid one. |
| 466 | */ |
| 467 | continue; |
| 468 | spin_unlock(&dev_lock); |
| 469 | return dev; |
| 470 | } |
| 471 | } |
| 472 | list_add(&new_dev->list, &dev_list); |
| 473 | spin_unlock(&dev_lock); |
| 474 | |
| 475 | return new_dev; |
| 476 | } |
| 477 | |
| 478 | static int rnbd_srv_check_update_open_perm(struct rnbd_srv_dev *srv_dev, |
| 479 | struct rnbd_srv_session *srv_sess, |
| 480 | enum rnbd_access_mode access_mode) |
| 481 | { |
| 482 | int ret = -EPERM; |
| 483 | |
| 484 | mutex_lock(&srv_dev->lock); |
| 485 | |
| 486 | switch (access_mode) { |
| 487 | case RNBD_ACCESS_RO: |
| 488 | ret = 0; |
| 489 | break; |
| 490 | case RNBD_ACCESS_RW: |
| 491 | if (srv_dev->open_write_cnt == 0) { |
| 492 | srv_dev->open_write_cnt++; |
| 493 | ret = 0; |
| 494 | } else { |
| 495 | 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", |
| 496 | srv_dev->id, srv_sess->sessname, |
| 497 | srv_dev->open_write_cnt, |
| 498 | rnbd_access_mode_str(access_mode)); |
| 499 | } |
| 500 | break; |
| 501 | case RNBD_ACCESS_MIGRATION: |
| 502 | if (srv_dev->open_write_cnt < 2) { |
| 503 | srv_dev->open_write_cnt++; |
| 504 | ret = 0; |
| 505 | } else { |
| 506 | 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", |
| 507 | srv_dev->id, srv_sess->sessname, |
| 508 | srv_dev->open_write_cnt, |
| 509 | rnbd_access_mode_str(access_mode)); |
| 510 | } |
| 511 | break; |
| 512 | default: |
| 513 | pr_err("Received mapping request for device '%s' on session %s with invalid access mode: %d\n", |
| 514 | srv_dev->id, srv_sess->sessname, access_mode); |
| 515 | ret = -EINVAL; |
| 516 | } |
| 517 | |
| 518 | mutex_unlock(&srv_dev->lock); |
| 519 | |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | static struct rnbd_srv_dev * |
| 524 | rnbd_srv_get_or_create_srv_dev(struct rnbd_dev *rnbd_dev, |
| 525 | struct rnbd_srv_session *srv_sess, |
| 526 | enum rnbd_access_mode access_mode) |
| 527 | { |
| 528 | int ret; |
| 529 | struct rnbd_srv_dev *new_dev, *dev; |
| 530 | |
| 531 | new_dev = rnbd_srv_init_srv_dev(rnbd_dev->name); |
| 532 | if (IS_ERR(new_dev)) |
| 533 | return new_dev; |
| 534 | |
| 535 | dev = rnbd_srv_find_or_add_srv_dev(new_dev); |
| 536 | if (dev != new_dev) |
| 537 | kfree(new_dev); |
| 538 | |
| 539 | ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode); |
| 540 | if (ret) { |
| 541 | rnbd_put_srv_dev(dev); |
| 542 | return ERR_PTR(ret); |
| 543 | } |
| 544 | |
| 545 | return dev; |
| 546 | } |
| 547 | |
| 548 | static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp, |
| 549 | struct rnbd_srv_sess_dev *sess_dev) |
| 550 | { |
| 551 | struct rnbd_dev *rnbd_dev = sess_dev->rnbd_dev; |
| 552 | |
| 553 | rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP); |
| 554 | rsp->device_id = |
| 555 | cpu_to_le32(sess_dev->device_id); |
| 556 | rsp->nsectors = |
| 557 | cpu_to_le64(get_capacity(rnbd_dev->bdev->bd_disk)); |
| 558 | rsp->logical_block_size = |
| 559 | cpu_to_le16(bdev_logical_block_size(rnbd_dev->bdev)); |
| 560 | rsp->physical_block_size = |
| 561 | cpu_to_le16(bdev_physical_block_size(rnbd_dev->bdev)); |
| 562 | rsp->max_segments = |
| 563 | cpu_to_le16(rnbd_dev_get_max_segs(rnbd_dev)); |
| 564 | rsp->max_hw_sectors = |
| 565 | cpu_to_le32(rnbd_dev_get_max_hw_sects(rnbd_dev)); |
| 566 | rsp->max_write_same_sectors = |
| 567 | cpu_to_le32(bdev_write_same(rnbd_dev->bdev)); |
| 568 | rsp->max_discard_sectors = |
| 569 | cpu_to_le32(rnbd_dev_get_max_discard_sects(rnbd_dev)); |
| 570 | rsp->discard_granularity = |
| 571 | cpu_to_le32(rnbd_dev_get_discard_granularity(rnbd_dev)); |
| 572 | rsp->discard_alignment = |
| 573 | cpu_to_le32(rnbd_dev_get_discard_alignment(rnbd_dev)); |
| 574 | rsp->secure_discard = |
| 575 | cpu_to_le16(rnbd_dev_get_secure_discard(rnbd_dev)); |
| 576 | rsp->rotational = |
| 577 | !blk_queue_nonrot(bdev_get_queue(rnbd_dev->bdev)); |
| 578 | } |
| 579 | |
| 580 | static struct rnbd_srv_sess_dev * |
| 581 | rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess, |
| 582 | const struct rnbd_msg_open *open_msg, |
| 583 | struct rnbd_dev *rnbd_dev, fmode_t open_flags, |
| 584 | struct rnbd_srv_dev *srv_dev) |
| 585 | { |
| 586 | struct rnbd_srv_sess_dev *sdev = rnbd_sess_dev_alloc(srv_sess); |
| 587 | |
| 588 | if (IS_ERR(sdev)) |
| 589 | return sdev; |
| 590 | |
| 591 | kref_init(&sdev->kref); |
| 592 | |
| 593 | strlcpy(sdev->pathname, open_msg->dev_name, sizeof(sdev->pathname)); |
| 594 | |
| 595 | sdev->rnbd_dev = rnbd_dev; |
| 596 | sdev->sess = srv_sess; |
| 597 | sdev->dev = srv_dev; |
| 598 | sdev->open_flags = open_flags; |
| 599 | sdev->access_mode = open_msg->access_mode; |
| 600 | |
| 601 | return sdev; |
| 602 | } |
| 603 | |
| 604 | static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess, |
| 605 | const char *dev_name) |
| 606 | { |
| 607 | char *full_path; |
| 608 | char *a, *b; |
| 609 | |
| 610 | full_path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 611 | if (!full_path) |
| 612 | return ERR_PTR(-ENOMEM); |
| 613 | |
| 614 | /* |
| 615 | * Replace %SESSNAME% with a real session name in order to |
| 616 | * create device namespace. |
| 617 | */ |
| 618 | a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path)); |
| 619 | if (a) { |
| 620 | int len = a - dev_search_path; |
| 621 | |
| 622 | len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len, |
| 623 | dev_search_path, srv_sess->sessname, dev_name); |
| 624 | if (len >= PATH_MAX) { |
| 625 | pr_err("Too long path: %s, %s, %s\n", |
| 626 | dev_search_path, srv_sess->sessname, dev_name); |
| 627 | kfree(full_path); |
| 628 | return ERR_PTR(-EINVAL); |
| 629 | } |
| 630 | } else { |
| 631 | snprintf(full_path, PATH_MAX, "%s/%s", |
| 632 | dev_search_path, dev_name); |
| 633 | } |
| 634 | |
| 635 | /* eliminitate duplicated slashes */ |
| 636 | a = strchr(full_path, '/'); |
| 637 | b = a; |
| 638 | while (*b != '\0') { |
| 639 | if (*b == '/' && *a == '/') { |
| 640 | b++; |
| 641 | } else { |
| 642 | a++; |
| 643 | *a = *b; |
| 644 | b++; |
| 645 | } |
| 646 | } |
| 647 | a++; |
| 648 | *a = '\0'; |
| 649 | |
| 650 | return full_path; |
| 651 | } |
| 652 | |
| 653 | static int process_msg_sess_info(struct rtrs_srv *rtrs, |
| 654 | struct rnbd_srv_session *srv_sess, |
| 655 | const void *msg, size_t len, |
| 656 | void *data, size_t datalen) |
| 657 | { |
| 658 | const struct rnbd_msg_sess_info *sess_info_msg = msg; |
| 659 | struct rnbd_msg_sess_info_rsp *rsp = data; |
| 660 | |
| 661 | srv_sess->ver = min_t(u8, sess_info_msg->ver, RNBD_PROTO_VER_MAJOR); |
| 662 | pr_debug("Session %s using protocol version %d (client version: %d, server version: %d)\n", |
| 663 | srv_sess->sessname, srv_sess->ver, |
| 664 | sess_info_msg->ver, RNBD_PROTO_VER_MAJOR); |
| 665 | |
| 666 | rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP); |
| 667 | rsp->ver = srv_sess->ver; |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * find_srv_sess_dev() - a dev is already opened by this name |
| 674 | * @srv_sess: the session to search. |
| 675 | * @dev_name: string containing the name of the device. |
| 676 | * |
| 677 | * Return struct rnbd_srv_sess_dev if srv_sess already opened the dev_name |
| 678 | * NULL if the session didn't open the device yet. |
| 679 | */ |
| 680 | static struct rnbd_srv_sess_dev * |
| 681 | find_srv_sess_dev(struct rnbd_srv_session *srv_sess, const char *dev_name) |
| 682 | { |
| 683 | struct rnbd_srv_sess_dev *sess_dev; |
| 684 | |
| 685 | if (list_empty(&srv_sess->sess_dev_list)) |
| 686 | return NULL; |
| 687 | |
| 688 | list_for_each_entry(sess_dev, &srv_sess->sess_dev_list, sess_list) |
| 689 | if (!strcmp(sess_dev->pathname, dev_name)) |
| 690 | return sess_dev; |
| 691 | |
| 692 | return NULL; |
| 693 | } |
| 694 | |
| 695 | static int process_msg_open(struct rtrs_srv *rtrs, |
| 696 | struct rnbd_srv_session *srv_sess, |
| 697 | const void *msg, size_t len, |
| 698 | void *data, size_t datalen) |
| 699 | { |
| 700 | int ret; |
| 701 | struct rnbd_srv_dev *srv_dev; |
| 702 | struct rnbd_srv_sess_dev *srv_sess_dev; |
| 703 | const struct rnbd_msg_open *open_msg = msg; |
| 704 | fmode_t open_flags; |
| 705 | char *full_path; |
| 706 | struct rnbd_dev *rnbd_dev; |
| 707 | struct rnbd_msg_open_rsp *rsp = data; |
| 708 | |
| 709 | pr_debug("Open message received: session='%s' path='%s' access_mode=%d\n", |
| 710 | srv_sess->sessname, open_msg->dev_name, |
| 711 | open_msg->access_mode); |
| 712 | open_flags = FMODE_READ; |
| 713 | if (open_msg->access_mode != RNBD_ACCESS_RO) |
| 714 | open_flags |= FMODE_WRITE; |
| 715 | |
| 716 | mutex_lock(&srv_sess->lock); |
| 717 | |
| 718 | srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name); |
| 719 | if (srv_sess_dev) |
| 720 | goto fill_response; |
| 721 | |
| 722 | if ((strlen(dev_search_path) + strlen(open_msg->dev_name)) |
| 723 | >= PATH_MAX) { |
| 724 | pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n", |
| 725 | srv_sess->sessname, dev_search_path, open_msg->dev_name, |
| 726 | PATH_MAX); |
| 727 | ret = -EINVAL; |
| 728 | goto reject; |
| 729 | } |
| 730 | if (strstr(open_msg->dev_name, "..")) { |
| 731 | pr_err("Opening device for session %s failed, device path %s contains relative path ..\n", |
| 732 | srv_sess->sessname, open_msg->dev_name); |
| 733 | ret = -EINVAL; |
| 734 | goto reject; |
| 735 | } |
| 736 | full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name); |
| 737 | if (IS_ERR(full_path)) { |
| 738 | ret = PTR_ERR(full_path); |
| 739 | pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %d\n", |
| 740 | open_msg->dev_name, srv_sess->sessname, ret); |
| 741 | goto reject; |
| 742 | } |
| 743 | |
| 744 | rnbd_dev = rnbd_dev_open(full_path, open_flags, |
| 745 | &srv_sess->sess_bio_set); |
| 746 | if (IS_ERR(rnbd_dev)) { |
| 747 | pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %ld\n", |
| 748 | full_path, srv_sess->sessname, PTR_ERR(rnbd_dev)); |
| 749 | ret = PTR_ERR(rnbd_dev); |
| 750 | goto free_path; |
| 751 | } |
| 752 | |
| 753 | srv_dev = rnbd_srv_get_or_create_srv_dev(rnbd_dev, srv_sess, |
| 754 | open_msg->access_mode); |
| 755 | if (IS_ERR(srv_dev)) { |
| 756 | pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %ld\n", |
| 757 | full_path, srv_sess->sessname, PTR_ERR(srv_dev)); |
| 758 | ret = PTR_ERR(srv_dev); |
| 759 | goto rnbd_dev_close; |
| 760 | } |
| 761 | |
| 762 | srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg, |
| 763 | rnbd_dev, open_flags, |
| 764 | srv_dev); |
| 765 | if (IS_ERR(srv_sess_dev)) { |
| 766 | pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %ld\n", |
| 767 | full_path, srv_sess->sessname, PTR_ERR(srv_sess_dev)); |
| 768 | ret = PTR_ERR(srv_sess_dev); |
| 769 | goto srv_dev_put; |
| 770 | } |
| 771 | |
| 772 | /* Create the srv_dev sysfs files if they haven't been created yet. The |
| 773 | * reason to delay the creation is not to create the sysfs files before |
| 774 | * we are sure the device can be opened. |
| 775 | */ |
| 776 | mutex_lock(&srv_dev->lock); |
| 777 | if (!srv_dev->dev_kobj.state_in_sysfs) { |
| 778 | ret = rnbd_srv_create_dev_sysfs(srv_dev, rnbd_dev->bdev, |
| 779 | rnbd_dev->name); |
| 780 | if (ret) { |
| 781 | mutex_unlock(&srv_dev->lock); |
| 782 | rnbd_srv_err(srv_sess_dev, |
| 783 | "Opening device failed, failed to create device sysfs files, err: %d\n", |
| 784 | ret); |
| 785 | goto free_srv_sess_dev; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev); |
| 790 | if (ret) { |
| 791 | mutex_unlock(&srv_dev->lock); |
| 792 | rnbd_srv_err(srv_sess_dev, |
| 793 | "Opening device failed, failed to create dev client sysfs files, err: %d\n", |
| 794 | ret); |
| 795 | goto free_srv_sess_dev; |
| 796 | } |
| 797 | |
| 798 | list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list); |
| 799 | mutex_unlock(&srv_dev->lock); |
| 800 | |
| 801 | list_add(&srv_sess_dev->sess_list, &srv_sess->sess_dev_list); |
| 802 | |
| 803 | rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id); |
| 804 | |
| 805 | kfree(full_path); |
| 806 | |
| 807 | fill_response: |
| 808 | rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev); |
| 809 | mutex_unlock(&srv_sess->lock); |
| 810 | return 0; |
| 811 | |
| 812 | free_srv_sess_dev: |
| 813 | xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id); |
| 814 | synchronize_rcu(); |
| 815 | kfree(srv_sess_dev); |
| 816 | srv_dev_put: |
| 817 | if (open_msg->access_mode != RNBD_ACCESS_RO) { |
| 818 | mutex_lock(&srv_dev->lock); |
| 819 | srv_dev->open_write_cnt--; |
| 820 | mutex_unlock(&srv_dev->lock); |
| 821 | } |
| 822 | rnbd_put_srv_dev(srv_dev); |
| 823 | rnbd_dev_close: |
| 824 | rnbd_dev_close(rnbd_dev); |
| 825 | free_path: |
| 826 | kfree(full_path); |
| 827 | reject: |
| 828 | mutex_unlock(&srv_sess->lock); |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | static struct rtrs_srv_ctx *rtrs_ctx; |
| 833 | |
| 834 | static struct rtrs_srv_ops rtrs_ops; |
| 835 | static int __init rnbd_srv_init_module(void) |
| 836 | { |
| 837 | int err; |
| 838 | |
| 839 | BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4); |
| 840 | BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36); |
| 841 | BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36); |
| 842 | BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264); |
| 843 | BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8); |
| 844 | BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56); |
| 845 | rtrs_ops = (struct rtrs_srv_ops) { |
| 846 | .rdma_ev = rnbd_srv_rdma_ev, |
| 847 | .link_ev = rnbd_srv_link_ev, |
| 848 | }; |
| 849 | rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr); |
| 850 | if (IS_ERR(rtrs_ctx)) { |
| 851 | err = PTR_ERR(rtrs_ctx); |
| 852 | pr_err("rtrs_srv_open(), err: %d\n", err); |
| 853 | return err; |
| 854 | } |
| 855 | |
| 856 | err = rnbd_srv_create_sysfs_files(); |
| 857 | if (err) { |
| 858 | pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err); |
| 859 | rtrs_srv_close(rtrs_ctx); |
| 860 | return err; |
| 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | static void __exit rnbd_srv_cleanup_module(void) |
| 867 | { |
| 868 | rtrs_srv_close(rtrs_ctx); |
| 869 | WARN_ON(!list_empty(&sess_list)); |
| 870 | rnbd_srv_destroy_sysfs_files(); |
| 871 | } |
| 872 | |
| 873 | module_init(rnbd_srv_init_module); |
| 874 | module_exit(rnbd_srv_cleanup_module); |