WIP FPC-III support
[linux/fpc-iii.git] / drivers / block / rnbd / rnbd-srv.c
bloba6a68d44f517ceae67b1069f9e54df013551bc28
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * RDMA Network Block Driver
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
12 #include <linux/module.h>
13 #include <linux/blkdev.h>
15 #include "rnbd-srv.h"
16 #include "rnbd-srv-dev.h"
18 MODULE_DESCRIPTION("RDMA Network Block Device Server");
19 MODULE_LICENSE("GPL");
21 static u16 port_nr = RTRS_PORT;
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)")");
28 #define DEFAULT_DEV_SEARCH_PATH "/"
30 static char dev_search_path[PATH_MAX] = DEFAULT_DEV_SEARCH_PATH;
32 static int dev_search_path_set(const char *val, const struct kernel_param *kp)
34 const char *p = strrchr(val, '\n') ? : val + strlen(val);
36 if (strlen(val) >= sizeof(dev_search_path))
37 return -EINVAL;
39 snprintf(dev_search_path, sizeof(dev_search_path), "%.*s",
40 (int)(p - val), val);
42 pr_info("dev_search_path changed to '%s'\n", dev_search_path);
44 return 0;
47 static struct kparam_string dev_search_path_kparam_str = {
48 .maxlen = sizeof(dev_search_path),
49 .string = dev_search_path
52 static const struct kernel_param_ops dev_search_path_ops = {
53 .set = dev_search_path_set,
54 .get = param_get_string,
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 ")");
63 static DEFINE_MUTEX(sess_lock);
64 static DEFINE_SPINLOCK(dev_lock);
66 static LIST_HEAD(sess_list);
67 static LIST_HEAD(dev_list);
69 struct rnbd_io_private {
70 struct rtrs_srv_op *id;
71 struct rnbd_srv_sess_dev *sess_dev;
74 static void rnbd_sess_dev_release(struct kref *kref)
76 struct rnbd_srv_sess_dev *sess_dev;
78 sess_dev = container_of(kref, struct rnbd_srv_sess_dev, kref);
79 complete(sess_dev->destroy_comp);
82 static inline void rnbd_put_sess_dev(struct rnbd_srv_sess_dev *sess_dev)
84 kref_put(&sess_dev->kref, rnbd_sess_dev_release);
87 void rnbd_endio(void *priv, int error)
89 struct rnbd_io_private *rnbd_priv = priv;
90 struct rnbd_srv_sess_dev *sess_dev = rnbd_priv->sess_dev;
92 rnbd_put_sess_dev(sess_dev);
94 rtrs_srv_resp_rdma(rnbd_priv->id, error);
96 kfree(priv);
99 static struct rnbd_srv_sess_dev *
100 rnbd_get_sess_dev(int dev_id, struct rnbd_srv_session *srv_sess)
102 struct rnbd_srv_sess_dev *sess_dev;
103 int ret = 0;
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();
111 if (!sess_dev || !ret)
112 return ERR_PTR(-ENXIO);
114 return sess_dev;
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)
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;
131 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
132 if (!priv)
133 return -ENOMEM;
135 dev_id = le32_to_cpu(msg->device_id);
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;
145 priv->sess_dev = sess_dev;
146 priv->id = id;
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;
156 io = container_of(bio, struct rnbd_dev_blk_io, bio);
157 io->dev = sess_dev->rnbd_dev;
158 io->priv = priv;
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);
170 submit_bio(bio);
172 return 0;
174 sess_dev_put:
175 rnbd_put_sess_dev(sess_dev);
176 err:
177 kfree(priv);
178 return err;
181 static void destroy_device(struct rnbd_srv_dev *dev)
183 WARN_ONCE(!list_empty(&dev->sess_dev_list),
184 "Device %s is being destroyed but still in use!\n",
185 dev->id);
187 spin_lock(&dev_lock);
188 list_del(&dev->list);
189 spin_unlock(&dev_lock);
191 mutex_destroy(&dev->lock);
192 if (dev->dev_kobj.state_in_sysfs)
194 * Destroy kobj only if it was really created.
196 rnbd_srv_destroy_dev_sysfs(dev);
197 else
198 kfree(dev);
201 static void destroy_device_cb(struct kref *kref)
203 struct rnbd_srv_dev *dev;
205 dev = container_of(kref, struct rnbd_srv_dev, kref);
207 destroy_device(dev);
210 static void rnbd_put_srv_dev(struct rnbd_srv_dev *dev)
212 kref_put(&dev->kref, destroy_device_cb);
215 void rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev *sess_dev, bool keep_id)
217 DECLARE_COMPLETION_ONSTACK(dc);
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();
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 */
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);
241 rnbd_put_srv_dev(sess_dev->dev);
243 rnbd_srv_info(sess_dev, "Device closed\n");
244 kfree(sess_dev);
247 static void destroy_sess(struct rnbd_srv_session *srv_sess)
249 struct rnbd_srv_sess_dev *sess_dev, *tmp;
251 if (list_empty(&srv_sess->sess_dev_list))
252 goto out;
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);
260 out:
261 xa_destroy(&srv_sess->index_idr);
262 bioset_exit(&srv_sess->sess_bio_set);
264 pr_info("RTRS Session %s disconnected\n", srv_sess->sessname);
266 mutex_lock(&sess_lock);
267 list_del(&srv_sess->list);
268 mutex_unlock(&sess_lock);
270 mutex_destroy(&srv_sess->lock);
271 kfree(srv_sess);
274 static int create_sess(struct rtrs_srv *rtrs)
276 struct rnbd_srv_session *srv_sess;
277 char sessname[NAME_MAX];
278 int err;
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);
284 return err;
286 srv_sess = kzalloc(sizeof(*srv_sess), GFP_KERNEL);
287 if (!srv_sess)
288 return -ENOMEM;
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;
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);
308 srv_sess->rtrs = rtrs;
309 strlcpy(srv_sess->sessname, sessname, sizeof(srv_sess->sessname));
311 rtrs_srv_set_sess_priv(rtrs, srv_sess);
313 return 0;
316 static int rnbd_srv_link_ev(struct rtrs_srv *rtrs,
317 enum rtrs_srv_link_ev ev, void *priv)
319 struct rnbd_srv_session *srv_sess = priv;
321 switch (ev) {
322 case RTRS_SRV_LINK_EV_CONNECTED:
323 return create_sess(rtrs);
325 case RTRS_SRV_LINK_EV_DISCONNECTED:
326 if (WARN_ON_ONCE(!srv_sess))
327 return -EINVAL;
329 destroy_sess(srv_sess);
330 return 0;
332 default:
333 pr_warn("Received unknown RTRS session event %d from session %s\n",
334 ev, srv_sess->sessname);
335 return -EINVAL;
339 void rnbd_srv_sess_dev_force_close(struct rnbd_srv_sess_dev *sess_dev)
341 struct rnbd_srv_session *sess = sess_dev->sess;
343 sess_dev->keep_id = true;
344 mutex_lock(&sess->lock);
345 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
346 mutex_unlock(&sess->lock);
349 static int process_msg_close(struct rtrs_srv *rtrs,
350 struct rnbd_srv_session *srv_sess,
351 void *data, size_t datalen, const void *usr,
352 size_t usrlen)
354 const struct rnbd_msg_close *close_msg = usr;
355 struct rnbd_srv_sess_dev *sess_dev;
357 sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id),
358 srv_sess);
359 if (IS_ERR(sess_dev))
360 return 0;
362 rnbd_put_sess_dev(sess_dev);
363 mutex_lock(&srv_sess->lock);
364 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
365 mutex_unlock(&srv_sess->lock);
366 return 0;
369 static int process_msg_open(struct rtrs_srv *rtrs,
370 struct rnbd_srv_session *srv_sess,
371 const void *msg, size_t len,
372 void *data, size_t datalen);
374 static int process_msg_sess_info(struct rtrs_srv *rtrs,
375 struct rnbd_srv_session *srv_sess,
376 const void *msg, size_t len,
377 void *data, size_t datalen);
379 static int rnbd_srv_rdma_ev(struct rtrs_srv *rtrs, void *priv,
380 struct rtrs_srv_op *id, int dir,
381 void *data, size_t datalen, const void *usr,
382 size_t usrlen)
384 struct rnbd_srv_session *srv_sess = priv;
385 const struct rnbd_msg_hdr *hdr = usr;
386 int ret = 0;
387 u16 type;
389 if (WARN_ON_ONCE(!srv_sess))
390 return -ENODEV;
392 type = le16_to_cpu(hdr->type);
394 switch (type) {
395 case RNBD_MSG_IO:
396 return process_rdma(rtrs, srv_sess, id, data, datalen, usr,
397 usrlen);
398 case RNBD_MSG_CLOSE:
399 ret = process_msg_close(rtrs, srv_sess, data, datalen,
400 usr, usrlen);
401 break;
402 case RNBD_MSG_OPEN:
403 ret = process_msg_open(rtrs, srv_sess, usr, usrlen,
404 data, datalen);
405 break;
406 case RNBD_MSG_SESS_INFO:
407 ret = process_msg_sess_info(rtrs, srv_sess, usr, usrlen,
408 data, datalen);
409 break;
410 default:
411 pr_warn("Received unexpected message type %d with dir %d from session %s\n",
412 type, dir, srv_sess->sessname);
413 return -EINVAL;
416 rtrs_srv_resp_rdma(id, ret);
417 return 0;
420 static struct rnbd_srv_sess_dev
421 *rnbd_sess_dev_alloc(struct rnbd_srv_session *srv_sess)
423 struct rnbd_srv_sess_dev *sess_dev;
424 int error;
426 sess_dev = kzalloc(sizeof(*sess_dev), GFP_KERNEL);
427 if (!sess_dev)
428 return ERR_PTR(-ENOMEM);
430 error = xa_alloc(&srv_sess->index_idr, &sess_dev->device_id, sess_dev,
431 xa_limit_32b, GFP_NOWAIT);
432 if (error < 0) {
433 pr_warn("Allocating idr failed, err: %d\n", error);
434 kfree(sess_dev);
435 return ERR_PTR(error);
438 return sess_dev;
441 static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(const char *id)
443 struct rnbd_srv_dev *dev;
445 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
446 if (!dev)
447 return ERR_PTR(-ENOMEM);
449 strlcpy(dev->id, id, sizeof(dev->id));
450 kref_init(&dev->kref);
451 INIT_LIST_HEAD(&dev->sess_dev_list);
452 mutex_init(&dev->lock);
454 return dev;
457 static struct rnbd_srv_dev *
458 rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev *new_dev)
460 struct rnbd_srv_dev *dev;
462 spin_lock(&dev_lock);
463 list_for_each_entry(dev, &dev_list, list) {
464 if (!strncmp(dev->id, new_dev->id, sizeof(dev->id))) {
465 if (!kref_get_unless_zero(&dev->kref))
467 * We lost the race, device is almost dead.
468 * Continue traversing to find a valid one.
470 continue;
471 spin_unlock(&dev_lock);
472 return dev;
475 list_add(&new_dev->list, &dev_list);
476 spin_unlock(&dev_lock);
478 return new_dev;
481 static int rnbd_srv_check_update_open_perm(struct rnbd_srv_dev *srv_dev,
482 struct rnbd_srv_session *srv_sess,
483 enum rnbd_access_mode access_mode)
485 int ret = -EPERM;
487 mutex_lock(&srv_dev->lock);
489 switch (access_mode) {
490 case RNBD_ACCESS_RO:
491 ret = 0;
492 break;
493 case RNBD_ACCESS_RW:
494 if (srv_dev->open_write_cnt == 0) {
495 srv_dev->open_write_cnt++;
496 ret = 0;
497 } else {
498 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",
499 srv_dev->id, srv_sess->sessname,
500 srv_dev->open_write_cnt,
501 rnbd_access_mode_str(access_mode));
503 break;
504 case RNBD_ACCESS_MIGRATION:
505 if (srv_dev->open_write_cnt < 2) {
506 srv_dev->open_write_cnt++;
507 ret = 0;
508 } else {
509 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",
510 srv_dev->id, srv_sess->sessname,
511 srv_dev->open_write_cnt,
512 rnbd_access_mode_str(access_mode));
514 break;
515 default:
516 pr_err("Received mapping request for device '%s' on session %s with invalid access mode: %d\n",
517 srv_dev->id, srv_sess->sessname, access_mode);
518 ret = -EINVAL;
521 mutex_unlock(&srv_dev->lock);
523 return ret;
526 static struct rnbd_srv_dev *
527 rnbd_srv_get_or_create_srv_dev(struct rnbd_dev *rnbd_dev,
528 struct rnbd_srv_session *srv_sess,
529 enum rnbd_access_mode access_mode)
531 int ret;
532 struct rnbd_srv_dev *new_dev, *dev;
534 new_dev = rnbd_srv_init_srv_dev(rnbd_dev->name);
535 if (IS_ERR(new_dev))
536 return new_dev;
538 dev = rnbd_srv_find_or_add_srv_dev(new_dev);
539 if (dev != new_dev)
540 kfree(new_dev);
542 ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode);
543 if (ret) {
544 rnbd_put_srv_dev(dev);
545 return ERR_PTR(ret);
548 return dev;
551 static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp,
552 struct rnbd_srv_sess_dev *sess_dev)
554 struct rnbd_dev *rnbd_dev = sess_dev->rnbd_dev;
555 struct request_queue *q = bdev_get_queue(rnbd_dev->bdev);
557 rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP);
558 rsp->device_id =
559 cpu_to_le32(sess_dev->device_id);
560 rsp->nsectors =
561 cpu_to_le64(get_capacity(rnbd_dev->bdev->bd_disk));
562 rsp->logical_block_size =
563 cpu_to_le16(bdev_logical_block_size(rnbd_dev->bdev));
564 rsp->physical_block_size =
565 cpu_to_le16(bdev_physical_block_size(rnbd_dev->bdev));
566 rsp->max_segments =
567 cpu_to_le16(rnbd_dev_get_max_segs(rnbd_dev));
568 rsp->max_hw_sectors =
569 cpu_to_le32(rnbd_dev_get_max_hw_sects(rnbd_dev));
570 rsp->max_write_same_sectors =
571 cpu_to_le32(bdev_write_same(rnbd_dev->bdev));
572 rsp->max_discard_sectors =
573 cpu_to_le32(rnbd_dev_get_max_discard_sects(rnbd_dev));
574 rsp->discard_granularity =
575 cpu_to_le32(rnbd_dev_get_discard_granularity(rnbd_dev));
576 rsp->discard_alignment =
577 cpu_to_le32(rnbd_dev_get_discard_alignment(rnbd_dev));
578 rsp->secure_discard =
579 cpu_to_le16(rnbd_dev_get_secure_discard(rnbd_dev));
580 rsp->rotational = !blk_queue_nonrot(q);
581 rsp->cache_policy = 0;
582 if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
583 rsp->cache_policy |= RNBD_WRITEBACK;
584 if (blk_queue_fua(q))
585 rsp->cache_policy |= RNBD_FUA;
588 static struct rnbd_srv_sess_dev *
589 rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess,
590 const struct rnbd_msg_open *open_msg,
591 struct rnbd_dev *rnbd_dev, fmode_t open_flags,
592 struct rnbd_srv_dev *srv_dev)
594 struct rnbd_srv_sess_dev *sdev = rnbd_sess_dev_alloc(srv_sess);
596 if (IS_ERR(sdev))
597 return sdev;
599 kref_init(&sdev->kref);
601 strlcpy(sdev->pathname, open_msg->dev_name, sizeof(sdev->pathname));
603 sdev->rnbd_dev = rnbd_dev;
604 sdev->sess = srv_sess;
605 sdev->dev = srv_dev;
606 sdev->open_flags = open_flags;
607 sdev->access_mode = open_msg->access_mode;
609 return sdev;
612 static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess,
613 const char *dev_name)
615 char *full_path;
616 char *a, *b;
618 full_path = kmalloc(PATH_MAX, GFP_KERNEL);
619 if (!full_path)
620 return ERR_PTR(-ENOMEM);
623 * Replace %SESSNAME% with a real session name in order to
624 * create device namespace.
626 a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path));
627 if (a) {
628 int len = a - dev_search_path;
630 len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len,
631 dev_search_path, srv_sess->sessname, dev_name);
632 if (len >= PATH_MAX) {
633 pr_err("Too long path: %s, %s, %s\n",
634 dev_search_path, srv_sess->sessname, dev_name);
635 kfree(full_path);
636 return ERR_PTR(-EINVAL);
638 } else {
639 snprintf(full_path, PATH_MAX, "%s/%s",
640 dev_search_path, dev_name);
643 /* eliminitate duplicated slashes */
644 a = strchr(full_path, '/');
645 b = a;
646 while (*b != '\0') {
647 if (*b == '/' && *a == '/') {
648 b++;
649 } else {
650 a++;
651 *a = *b;
652 b++;
655 a++;
656 *a = '\0';
658 return full_path;
661 static int process_msg_sess_info(struct rtrs_srv *rtrs,
662 struct rnbd_srv_session *srv_sess,
663 const void *msg, size_t len,
664 void *data, size_t datalen)
666 const struct rnbd_msg_sess_info *sess_info_msg = msg;
667 struct rnbd_msg_sess_info_rsp *rsp = data;
669 srv_sess->ver = min_t(u8, sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
670 pr_debug("Session %s using protocol version %d (client version: %d, server version: %d)\n",
671 srv_sess->sessname, srv_sess->ver,
672 sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
674 rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP);
675 rsp->ver = srv_sess->ver;
677 return 0;
681 * find_srv_sess_dev() - a dev is already opened by this name
682 * @srv_sess: the session to search.
683 * @dev_name: string containing the name of the device.
685 * Return struct rnbd_srv_sess_dev if srv_sess already opened the dev_name
686 * NULL if the session didn't open the device yet.
688 static struct rnbd_srv_sess_dev *
689 find_srv_sess_dev(struct rnbd_srv_session *srv_sess, const char *dev_name)
691 struct rnbd_srv_sess_dev *sess_dev;
693 if (list_empty(&srv_sess->sess_dev_list))
694 return NULL;
696 list_for_each_entry(sess_dev, &srv_sess->sess_dev_list, sess_list)
697 if (!strcmp(sess_dev->pathname, dev_name))
698 return sess_dev;
700 return NULL;
703 static int process_msg_open(struct rtrs_srv *rtrs,
704 struct rnbd_srv_session *srv_sess,
705 const void *msg, size_t len,
706 void *data, size_t datalen)
708 int ret;
709 struct rnbd_srv_dev *srv_dev;
710 struct rnbd_srv_sess_dev *srv_sess_dev;
711 const struct rnbd_msg_open *open_msg = msg;
712 fmode_t open_flags;
713 char *full_path;
714 struct rnbd_dev *rnbd_dev;
715 struct rnbd_msg_open_rsp *rsp = data;
717 pr_debug("Open message received: session='%s' path='%s' access_mode=%d\n",
718 srv_sess->sessname, open_msg->dev_name,
719 open_msg->access_mode);
720 open_flags = FMODE_READ;
721 if (open_msg->access_mode != RNBD_ACCESS_RO)
722 open_flags |= FMODE_WRITE;
724 mutex_lock(&srv_sess->lock);
726 srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name);
727 if (srv_sess_dev)
728 goto fill_response;
730 if ((strlen(dev_search_path) + strlen(open_msg->dev_name))
731 >= PATH_MAX) {
732 pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n",
733 srv_sess->sessname, dev_search_path, open_msg->dev_name,
734 PATH_MAX);
735 ret = -EINVAL;
736 goto reject;
738 if (strstr(open_msg->dev_name, "..")) {
739 pr_err("Opening device for session %s failed, device path %s contains relative path ..\n",
740 srv_sess->sessname, open_msg->dev_name);
741 ret = -EINVAL;
742 goto reject;
744 full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name);
745 if (IS_ERR(full_path)) {
746 ret = PTR_ERR(full_path);
747 pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %d\n",
748 open_msg->dev_name, srv_sess->sessname, ret);
749 goto reject;
752 rnbd_dev = rnbd_dev_open(full_path, open_flags,
753 &srv_sess->sess_bio_set);
754 if (IS_ERR(rnbd_dev)) {
755 pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %ld\n",
756 full_path, srv_sess->sessname, PTR_ERR(rnbd_dev));
757 ret = PTR_ERR(rnbd_dev);
758 goto free_path;
761 srv_dev = rnbd_srv_get_or_create_srv_dev(rnbd_dev, srv_sess,
762 open_msg->access_mode);
763 if (IS_ERR(srv_dev)) {
764 pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %ld\n",
765 full_path, srv_sess->sessname, PTR_ERR(srv_dev));
766 ret = PTR_ERR(srv_dev);
767 goto rnbd_dev_close;
770 srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg,
771 rnbd_dev, open_flags,
772 srv_dev);
773 if (IS_ERR(srv_sess_dev)) {
774 pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %ld\n",
775 full_path, srv_sess->sessname, PTR_ERR(srv_sess_dev));
776 ret = PTR_ERR(srv_sess_dev);
777 goto srv_dev_put;
780 /* Create the srv_dev sysfs files if they haven't been created yet. The
781 * reason to delay the creation is not to create the sysfs files before
782 * we are sure the device can be opened.
784 mutex_lock(&srv_dev->lock);
785 if (!srv_dev->dev_kobj.state_in_sysfs) {
786 ret = rnbd_srv_create_dev_sysfs(srv_dev, rnbd_dev->bdev,
787 rnbd_dev->name);
788 if (ret) {
789 mutex_unlock(&srv_dev->lock);
790 rnbd_srv_err(srv_sess_dev,
791 "Opening device failed, failed to create device sysfs files, err: %d\n",
792 ret);
793 goto free_srv_sess_dev;
797 ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
798 if (ret) {
799 mutex_unlock(&srv_dev->lock);
800 rnbd_srv_err(srv_sess_dev,
801 "Opening device failed, failed to create dev client sysfs files, err: %d\n",
802 ret);
803 goto free_srv_sess_dev;
806 list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
807 mutex_unlock(&srv_dev->lock);
809 list_add(&srv_sess_dev->sess_list, &srv_sess->sess_dev_list);
811 rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id);
813 kfree(full_path);
815 fill_response:
816 rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev);
817 mutex_unlock(&srv_sess->lock);
818 return 0;
820 free_srv_sess_dev:
821 xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id);
822 synchronize_rcu();
823 kfree(srv_sess_dev);
824 srv_dev_put:
825 if (open_msg->access_mode != RNBD_ACCESS_RO) {
826 mutex_lock(&srv_dev->lock);
827 srv_dev->open_write_cnt--;
828 mutex_unlock(&srv_dev->lock);
830 rnbd_put_srv_dev(srv_dev);
831 rnbd_dev_close:
832 rnbd_dev_close(rnbd_dev);
833 free_path:
834 kfree(full_path);
835 reject:
836 mutex_unlock(&srv_sess->lock);
837 return ret;
840 static struct rtrs_srv_ctx *rtrs_ctx;
842 static struct rtrs_srv_ops rtrs_ops;
843 static int __init rnbd_srv_init_module(void)
845 int err;
847 BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4);
848 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36);
849 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36);
850 BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264);
851 BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8);
852 BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56);
853 rtrs_ops = (struct rtrs_srv_ops) {
854 .rdma_ev = rnbd_srv_rdma_ev,
855 .link_ev = rnbd_srv_link_ev,
857 rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr);
858 if (IS_ERR(rtrs_ctx)) {
859 err = PTR_ERR(rtrs_ctx);
860 pr_err("rtrs_srv_open(), err: %d\n", err);
861 return err;
864 err = rnbd_srv_create_sysfs_files();
865 if (err) {
866 pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err);
867 rtrs_srv_close(rtrs_ctx);
868 return err;
871 return 0;
874 static void __exit rnbd_srv_cleanup_module(void)
876 rtrs_srv_close(rtrs_ctx);
877 WARN_ON(!list_empty(&sess_list));
878 rnbd_srv_destroy_sysfs_files();
881 module_init(rnbd_srv_init_module);
882 module_exit(rnbd_srv_cleanup_module);