1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
12 #include <linux/module.h>
13 #include <linux/blkdev.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))
39 snprintf(dev_search_path, sizeof(dev_search_path), "%.*s",
42 pr_info("dev_search_path changed to '%s'\n", dev_search_path);
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);
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;
106 sess_dev = xa_load(&srv_sess->index_idr, dev_id);
108 ret = kref_get_unless_zero(&sess_dev->kref);
111 if (!sess_dev || !ret)
112 return ERR_PTR(-ENXIO);
117 static void rnbd_dev_bi_end_io(struct bio *bio)
119 rnbd_endio(bio->bi_private, blk_status_to_errno(bio->bi_status));
123 static int process_rdma(struct rnbd_srv_session *srv_sess,
124 struct rtrs_srv_op *id, void *data, u32 datalen,
125 const void *usr, size_t usrlen)
127 const struct rnbd_msg_io *msg = usr;
128 struct rnbd_io_private *priv;
129 struct rnbd_srv_sess_dev *sess_dev;
135 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
139 dev_id = le32_to_cpu(msg->device_id);
141 sess_dev = rnbd_get_sess_dev(dev_id, srv_sess);
142 if (IS_ERR(sess_dev)) {
143 pr_err_ratelimited("Got I/O request on session %s for unknown device id %d\n",
144 srv_sess->sessname, dev_id);
149 priv->sess_dev = sess_dev;
152 bio = bio_alloc(sess_dev->rnbd_dev->bdev, 1,
153 rnbd_to_bio_flags(le32_to_cpu(msg->rw)), GFP_KERNEL);
154 if (bio_add_page(bio, virt_to_page(data), datalen,
155 offset_in_page(data)) != datalen) {
156 rnbd_srv_err(sess_dev, "Failed to map data to bio\n");
161 bio->bi_end_io = rnbd_dev_bi_end_io;
162 bio->bi_private = priv;
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);
175 rnbd_put_sess_dev(sess_dev);
181 static void destroy_device(struct kref *kref)
183 struct rnbd_srv_dev *dev = container_of(kref, struct rnbd_srv_dev, kref);
185 WARN_ONCE(!list_empty(&dev->sess_dev_list),
186 "Device %s is being destroyed but still in use!\n",
189 spin_lock(&dev_lock);
190 list_del(&dev->list);
191 spin_unlock(&dev_lock);
193 mutex_destroy(&dev->lock);
194 if (dev->dev_kobj.state_in_sysfs)
196 * Destroy kobj only if it was really created.
198 rnbd_srv_destroy_dev_sysfs(dev);
203 static void rnbd_put_srv_dev(struct rnbd_srv_dev *dev)
205 kref_put(&dev->kref, destroy_device);
208 void rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev *sess_dev, bool keep_id)
210 DECLARE_COMPLETION_ONSTACK(dc);
213 /* free the resources for the id but don't */
214 /* allow to re-use the id itself because it */
215 /* is still used by the client */
216 xa_cmpxchg(&sess_dev->sess->index_idr, sess_dev->device_id,
219 xa_erase(&sess_dev->sess->index_idr, sess_dev->device_id);
222 sess_dev->destroy_comp = &dc;
223 rnbd_put_sess_dev(sess_dev);
224 wait_for_completion(&dc); /* wait for inflights to drop to zero */
226 rnbd_dev_close(sess_dev->rnbd_dev);
227 list_del(&sess_dev->sess_list);
228 mutex_lock(&sess_dev->dev->lock);
229 list_del(&sess_dev->dev_list);
230 if (sess_dev->open_flags & FMODE_WRITE)
231 sess_dev->dev->open_write_cnt--;
232 mutex_unlock(&sess_dev->dev->lock);
234 rnbd_put_srv_dev(sess_dev->dev);
236 rnbd_srv_info(sess_dev, "Device closed\n");
240 static void destroy_sess(struct rnbd_srv_session *srv_sess)
242 struct rnbd_srv_sess_dev *sess_dev, *tmp;
244 if (list_empty(&srv_sess->sess_dev_list))
247 mutex_lock(&srv_sess->lock);
248 list_for_each_entry_safe(sess_dev, tmp, &srv_sess->sess_dev_list,
250 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
251 mutex_unlock(&srv_sess->lock);
254 xa_destroy(&srv_sess->index_idr);
256 pr_info("RTRS Session %s disconnected\n", srv_sess->sessname);
258 mutex_lock(&sess_lock);
259 list_del(&srv_sess->list);
260 mutex_unlock(&sess_lock);
262 mutex_destroy(&srv_sess->lock);
266 static int create_sess(struct rtrs_srv_sess *rtrs)
268 struct rnbd_srv_session *srv_sess;
269 char pathname[NAME_MAX];
272 err = rtrs_srv_get_path_name(rtrs, pathname, sizeof(pathname));
274 pr_err("rtrs_srv_get_path_name(%s): %d\n", pathname, err);
278 srv_sess = kzalloc(sizeof(*srv_sess), GFP_KERNEL);
282 srv_sess->queue_depth = rtrs_srv_get_queue_depth(rtrs);
283 xa_init_flags(&srv_sess->index_idr, XA_FLAGS_ALLOC);
284 INIT_LIST_HEAD(&srv_sess->sess_dev_list);
285 mutex_init(&srv_sess->lock);
286 mutex_lock(&sess_lock);
287 list_add(&srv_sess->list, &sess_list);
288 mutex_unlock(&sess_lock);
290 srv_sess->rtrs = rtrs;
291 strscpy(srv_sess->sessname, pathname, sizeof(srv_sess->sessname));
293 rtrs_srv_set_sess_priv(rtrs, srv_sess);
298 static int rnbd_srv_link_ev(struct rtrs_srv_sess *rtrs,
299 enum rtrs_srv_link_ev ev, void *priv)
301 struct rnbd_srv_session *srv_sess = priv;
304 case RTRS_SRV_LINK_EV_CONNECTED:
305 return create_sess(rtrs);
307 case RTRS_SRV_LINK_EV_DISCONNECTED:
308 if (WARN_ON_ONCE(!srv_sess))
311 destroy_sess(srv_sess);
315 pr_warn("Received unknown RTRS session event %d from session %s\n",
316 ev, srv_sess->sessname);
321 void rnbd_srv_sess_dev_force_close(struct rnbd_srv_sess_dev *sess_dev,
322 struct kobj_attribute *attr)
324 struct rnbd_srv_session *sess = sess_dev->sess;
326 sess_dev->keep_id = true;
327 /* It is already started to close by client's close message. */
328 if (!mutex_trylock(&sess->lock))
330 /* first remove sysfs itself to avoid deadlock */
331 sysfs_remove_file_self(&sess_dev->kobj, &attr->attr);
332 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
333 mutex_unlock(&sess->lock);
336 static int process_msg_close(struct rnbd_srv_session *srv_sess,
337 void *data, size_t datalen, const void *usr,
340 const struct rnbd_msg_close *close_msg = usr;
341 struct rnbd_srv_sess_dev *sess_dev;
343 sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id),
345 if (IS_ERR(sess_dev))
348 rnbd_put_sess_dev(sess_dev);
349 mutex_lock(&srv_sess->lock);
350 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
351 mutex_unlock(&srv_sess->lock);
355 static int process_msg_open(struct rnbd_srv_session *srv_sess,
356 const void *msg, size_t len,
357 void *data, size_t datalen);
359 static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
360 const void *msg, size_t len,
361 void *data, size_t datalen);
363 static int rnbd_srv_rdma_ev(void *priv,
364 struct rtrs_srv_op *id, int dir,
365 void *data, size_t datalen, const void *usr,
368 struct rnbd_srv_session *srv_sess = priv;
369 const struct rnbd_msg_hdr *hdr = usr;
373 if (WARN_ON_ONCE(!srv_sess))
376 type = le16_to_cpu(hdr->type);
380 return process_rdma(srv_sess, id, data, datalen, usr, usrlen);
382 ret = process_msg_close(srv_sess, data, datalen, usr, usrlen);
385 ret = process_msg_open(srv_sess, usr, usrlen, data, datalen);
387 case RNBD_MSG_SESS_INFO:
388 ret = process_msg_sess_info(srv_sess, usr, usrlen, data,
392 pr_warn("Received unexpected message type %d with dir %d from session %s\n",
393 type, dir, srv_sess->sessname);
397 rtrs_srv_resp_rdma(id, ret);
401 static struct rnbd_srv_sess_dev
402 *rnbd_sess_dev_alloc(struct rnbd_srv_session *srv_sess)
404 struct rnbd_srv_sess_dev *sess_dev;
407 sess_dev = kzalloc(sizeof(*sess_dev), GFP_KERNEL);
409 return ERR_PTR(-ENOMEM);
411 error = xa_alloc(&srv_sess->index_idr, &sess_dev->device_id, sess_dev,
412 xa_limit_32b, GFP_NOWAIT);
414 pr_warn("Allocating idr failed, err: %d\n", error);
416 return ERR_PTR(error);
422 static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(const char *id)
424 struct rnbd_srv_dev *dev;
426 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
428 return ERR_PTR(-ENOMEM);
430 strscpy(dev->id, id, sizeof(dev->id));
431 kref_init(&dev->kref);
432 INIT_LIST_HEAD(&dev->sess_dev_list);
433 mutex_init(&dev->lock);
438 static struct rnbd_srv_dev *
439 rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev *new_dev)
441 struct rnbd_srv_dev *dev;
443 spin_lock(&dev_lock);
444 list_for_each_entry(dev, &dev_list, list) {
445 if (!strncmp(dev->id, new_dev->id, sizeof(dev->id))) {
446 if (!kref_get_unless_zero(&dev->kref))
448 * We lost the race, device is almost dead.
449 * Continue traversing to find a valid one.
452 spin_unlock(&dev_lock);
456 list_add(&new_dev->list, &dev_list);
457 spin_unlock(&dev_lock);
462 static int rnbd_srv_check_update_open_perm(struct rnbd_srv_dev *srv_dev,
463 struct rnbd_srv_session *srv_sess,
464 enum rnbd_access_mode access_mode)
468 mutex_lock(&srv_dev->lock);
470 switch (access_mode) {
475 if (srv_dev->open_write_cnt == 0) {
476 srv_dev->open_write_cnt++;
479 pr_err("Mapping device '%s' for session %s with RW permissions failed. Device already opened as 'RW' by %d client(s), access mode %s.\n",
480 srv_dev->id, srv_sess->sessname,
481 srv_dev->open_write_cnt,
482 rnbd_access_mode_str(access_mode));
485 case RNBD_ACCESS_MIGRATION:
486 if (srv_dev->open_write_cnt < 2) {
487 srv_dev->open_write_cnt++;
490 pr_err("Mapping device '%s' for session %s with migration permissions failed. Device already opened as 'RW' by %d client(s), access mode %s.\n",
491 srv_dev->id, srv_sess->sessname,
492 srv_dev->open_write_cnt,
493 rnbd_access_mode_str(access_mode));
497 pr_err("Received mapping request for device '%s' on session %s with invalid access mode: %d\n",
498 srv_dev->id, srv_sess->sessname, access_mode);
502 mutex_unlock(&srv_dev->lock);
507 static struct rnbd_srv_dev *
508 rnbd_srv_get_or_create_srv_dev(struct rnbd_dev *rnbd_dev,
509 struct rnbd_srv_session *srv_sess,
510 enum rnbd_access_mode access_mode)
513 struct rnbd_srv_dev *new_dev, *dev;
515 new_dev = rnbd_srv_init_srv_dev(rnbd_dev->name);
519 dev = rnbd_srv_find_or_add_srv_dev(new_dev);
523 ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode);
525 rnbd_put_srv_dev(dev);
532 static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp,
533 struct rnbd_srv_sess_dev *sess_dev)
535 struct rnbd_dev *rnbd_dev = sess_dev->rnbd_dev;
536 struct request_queue *q = bdev_get_queue(rnbd_dev->bdev);
538 rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP);
540 cpu_to_le32(sess_dev->device_id);
542 cpu_to_le64(get_capacity(rnbd_dev->bdev->bd_disk));
543 rsp->logical_block_size =
544 cpu_to_le16(bdev_logical_block_size(rnbd_dev->bdev));
545 rsp->physical_block_size =
546 cpu_to_le16(bdev_physical_block_size(rnbd_dev->bdev));
548 cpu_to_le16(rnbd_dev_get_max_segs(rnbd_dev));
549 rsp->max_hw_sectors =
550 cpu_to_le32(rnbd_dev_get_max_hw_sects(rnbd_dev));
551 rsp->max_write_same_sectors = 0;
552 rsp->max_discard_sectors =
553 cpu_to_le32(rnbd_dev_get_max_discard_sects(rnbd_dev));
554 rsp->discard_granularity =
555 cpu_to_le32(rnbd_dev_get_discard_granularity(rnbd_dev));
556 rsp->discard_alignment =
557 cpu_to_le32(rnbd_dev_get_discard_alignment(rnbd_dev));
558 rsp->secure_discard =
559 cpu_to_le16(rnbd_dev_get_secure_discard(rnbd_dev));
560 rsp->cache_policy = 0;
561 if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
562 rsp->cache_policy |= RNBD_WRITEBACK;
563 if (blk_queue_fua(q))
564 rsp->cache_policy |= RNBD_FUA;
567 static struct rnbd_srv_sess_dev *
568 rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess,
569 const struct rnbd_msg_open *open_msg,
570 struct rnbd_dev *rnbd_dev, fmode_t open_flags,
571 struct rnbd_srv_dev *srv_dev)
573 struct rnbd_srv_sess_dev *sdev = rnbd_sess_dev_alloc(srv_sess);
578 kref_init(&sdev->kref);
580 strscpy(sdev->pathname, open_msg->dev_name, sizeof(sdev->pathname));
582 sdev->rnbd_dev = rnbd_dev;
583 sdev->sess = srv_sess;
585 sdev->open_flags = open_flags;
586 sdev->access_mode = open_msg->access_mode;
591 static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess,
592 const char *dev_name)
597 full_path = kmalloc(PATH_MAX, GFP_KERNEL);
599 return ERR_PTR(-ENOMEM);
602 * Replace %SESSNAME% with a real session name in order to
603 * create device namespace.
605 a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path));
607 int len = a - dev_search_path;
609 len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len,
610 dev_search_path, srv_sess->sessname, dev_name);
611 if (len >= PATH_MAX) {
612 pr_err("Too long path: %s, %s, %s\n",
613 dev_search_path, srv_sess->sessname, dev_name);
615 return ERR_PTR(-EINVAL);
618 snprintf(full_path, PATH_MAX, "%s/%s",
619 dev_search_path, dev_name);
622 /* eliminitate duplicated slashes */
623 a = strchr(full_path, '/');
626 if (*b == '/' && *a == '/') {
640 static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
641 const void *msg, size_t len,
642 void *data, size_t datalen)
644 const struct rnbd_msg_sess_info *sess_info_msg = msg;
645 struct rnbd_msg_sess_info_rsp *rsp = data;
647 srv_sess->ver = min_t(u8, sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
648 pr_debug("Session %s using protocol version %d (client version: %d, server version: %d)\n",
649 srv_sess->sessname, srv_sess->ver,
650 sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
652 rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP);
653 rsp->ver = srv_sess->ver;
659 * find_srv_sess_dev() - a dev is already opened by this name
660 * @srv_sess: the session to search.
661 * @dev_name: string containing the name of the device.
663 * Return struct rnbd_srv_sess_dev if srv_sess already opened the dev_name
664 * NULL if the session didn't open the device yet.
666 static struct rnbd_srv_sess_dev *
667 find_srv_sess_dev(struct rnbd_srv_session *srv_sess, const char *dev_name)
669 struct rnbd_srv_sess_dev *sess_dev;
671 if (list_empty(&srv_sess->sess_dev_list))
674 list_for_each_entry(sess_dev, &srv_sess->sess_dev_list, sess_list)
675 if (!strcmp(sess_dev->pathname, dev_name))
681 static int process_msg_open(struct rnbd_srv_session *srv_sess,
682 const void *msg, size_t len,
683 void *data, size_t datalen)
686 struct rnbd_srv_dev *srv_dev;
687 struct rnbd_srv_sess_dev *srv_sess_dev;
688 const struct rnbd_msg_open *open_msg = msg;
691 struct rnbd_dev *rnbd_dev;
692 struct rnbd_msg_open_rsp *rsp = data;
694 pr_debug("Open message received: session='%s' path='%s' access_mode=%d\n",
695 srv_sess->sessname, open_msg->dev_name,
696 open_msg->access_mode);
697 open_flags = FMODE_READ;
698 if (open_msg->access_mode != RNBD_ACCESS_RO)
699 open_flags |= FMODE_WRITE;
701 mutex_lock(&srv_sess->lock);
703 srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name);
707 if ((strlen(dev_search_path) + strlen(open_msg->dev_name))
709 pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n",
710 srv_sess->sessname, dev_search_path, open_msg->dev_name,
715 if (strstr(open_msg->dev_name, "..")) {
716 pr_err("Opening device for session %s failed, device path %s contains relative path ..\n",
717 srv_sess->sessname, open_msg->dev_name);
721 full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name);
722 if (IS_ERR(full_path)) {
723 ret = PTR_ERR(full_path);
724 pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %d\n",
725 open_msg->dev_name, srv_sess->sessname, ret);
729 rnbd_dev = rnbd_dev_open(full_path, open_flags);
730 if (IS_ERR(rnbd_dev)) {
731 pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %ld\n",
732 full_path, srv_sess->sessname, PTR_ERR(rnbd_dev));
733 ret = PTR_ERR(rnbd_dev);
737 srv_dev = rnbd_srv_get_or_create_srv_dev(rnbd_dev, srv_sess,
738 open_msg->access_mode);
739 if (IS_ERR(srv_dev)) {
740 pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %ld\n",
741 full_path, srv_sess->sessname, PTR_ERR(srv_dev));
742 ret = PTR_ERR(srv_dev);
746 srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg,
747 rnbd_dev, open_flags,
749 if (IS_ERR(srv_sess_dev)) {
750 pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %ld\n",
751 full_path, srv_sess->sessname, PTR_ERR(srv_sess_dev));
752 ret = PTR_ERR(srv_sess_dev);
756 /* Create the srv_dev sysfs files if they haven't been created yet. The
757 * reason to delay the creation is not to create the sysfs files before
758 * we are sure the device can be opened.
760 mutex_lock(&srv_dev->lock);
761 if (!srv_dev->dev_kobj.state_in_sysfs) {
762 ret = rnbd_srv_create_dev_sysfs(srv_dev, rnbd_dev->bdev,
765 mutex_unlock(&srv_dev->lock);
766 rnbd_srv_err(srv_sess_dev,
767 "Opening device failed, failed to create device sysfs files, err: %d\n",
769 goto free_srv_sess_dev;
773 ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
775 mutex_unlock(&srv_dev->lock);
776 rnbd_srv_err(srv_sess_dev,
777 "Opening device failed, failed to create dev client sysfs files, err: %d\n",
779 goto free_srv_sess_dev;
782 list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
783 mutex_unlock(&srv_dev->lock);
785 list_add(&srv_sess_dev->sess_list, &srv_sess->sess_dev_list);
787 rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id);
792 rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev);
793 mutex_unlock(&srv_sess->lock);
797 xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id);
801 if (open_msg->access_mode != RNBD_ACCESS_RO) {
802 mutex_lock(&srv_dev->lock);
803 srv_dev->open_write_cnt--;
804 mutex_unlock(&srv_dev->lock);
806 rnbd_put_srv_dev(srv_dev);
808 rnbd_dev_close(rnbd_dev);
812 mutex_unlock(&srv_sess->lock);
816 static struct rtrs_srv_ctx *rtrs_ctx;
818 static struct rtrs_srv_ops rtrs_ops;
819 static int __init rnbd_srv_init_module(void)
823 BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4);
824 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36);
825 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36);
826 BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264);
827 BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8);
828 BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56);
829 rtrs_ops = (struct rtrs_srv_ops) {
830 .rdma_ev = rnbd_srv_rdma_ev,
831 .link_ev = rnbd_srv_link_ev,
833 rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr);
834 if (IS_ERR(rtrs_ctx)) {
835 err = PTR_ERR(rtrs_ctx);
836 pr_err("rtrs_srv_open(), err: %d\n", err);
840 err = rnbd_srv_create_sysfs_files();
842 pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err);
843 rtrs_srv_close(rtrs_ctx);
850 static void __exit rnbd_srv_cleanup_module(void)
852 rtrs_srv_close(rtrs_ctx);
853 WARN_ON(!list_empty(&sess_list));
854 rnbd_srv_destroy_sysfs_files();
857 module_init(rnbd_srv_init_module);
858 module_exit(rnbd_srv_cleanup_module);