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-trace.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 static struct rnbd_srv_sess_dev *
88 rnbd_get_sess_dev(int dev_id, struct rnbd_srv_session *srv_sess)
90 struct rnbd_srv_sess_dev *sess_dev;
94 sess_dev = xa_load(&srv_sess->index_idr, dev_id);
96 ret = kref_get_unless_zero(&sess_dev->kref);
100 return ERR_PTR(-ENXIO);
105 static void rnbd_dev_bi_end_io(struct bio *bio)
107 struct rnbd_io_private *rnbd_priv = bio->bi_private;
108 struct rnbd_srv_sess_dev *sess_dev = rnbd_priv->sess_dev;
110 rnbd_put_sess_dev(sess_dev);
111 rtrs_srv_resp_rdma(rnbd_priv->id, blk_status_to_errno(bio->bi_status));
117 static int process_rdma(struct rnbd_srv_session *srv_sess,
118 struct rtrs_srv_op *id, void *data, u32 datalen,
119 const void *usr, size_t usrlen)
121 const struct rnbd_msg_io *msg = usr;
122 struct rnbd_io_private *priv;
123 struct rnbd_srv_sess_dev *sess_dev;
129 trace_process_rdma(srv_sess, msg, id, datalen, usrlen);
131 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
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);
145 priv->sess_dev = sess_dev;
148 bio = bio_alloc(sess_dev->bdev, 1,
149 rnbd_to_bio_flags(le32_to_cpu(msg->rw)), GFP_KERNEL);
150 if (bio_add_page(bio, virt_to_page(data), datalen,
151 offset_in_page(data)) != datalen) {
152 rnbd_srv_err(sess_dev, "Failed to map data to bio\n");
157 bio->bi_end_io = rnbd_dev_bi_end_io;
158 bio->bi_private = priv;
159 bio->bi_iter.bi_sector = le64_to_cpu(msg->sector);
160 bio->bi_iter.bi_size = le32_to_cpu(msg->bi_size);
161 prio = srv_sess->ver < RNBD_PROTO_VER_MAJOR ||
162 usrlen < sizeof(*msg) ? 0 : le16_to_cpu(msg->prio);
163 bio_set_prio(bio, prio);
171 rnbd_put_sess_dev(sess_dev);
177 static void destroy_device(struct kref *kref)
179 struct rnbd_srv_dev *dev = container_of(kref, struct rnbd_srv_dev, kref);
181 WARN_ONCE(!list_empty(&dev->sess_dev_list),
182 "Device %s is being destroyed but still in use!\n",
185 spin_lock(&dev_lock);
186 list_del(&dev->list);
187 spin_unlock(&dev_lock);
189 mutex_destroy(&dev->lock);
190 if (dev->dev_kobj.state_in_sysfs)
192 * Destroy kobj only if it was really created.
194 rnbd_srv_destroy_dev_sysfs(dev);
199 static void rnbd_put_srv_dev(struct rnbd_srv_dev *dev)
201 kref_put(&dev->kref, destroy_device);
204 void rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev *sess_dev, bool keep_id)
206 DECLARE_COMPLETION_ONSTACK(dc);
209 /* free the resources for the id but don't */
210 /* allow to re-use the id itself because it */
211 /* is still used by the client */
212 xa_cmpxchg(&sess_dev->sess->index_idr, sess_dev->device_id,
215 xa_erase(&sess_dev->sess->index_idr, sess_dev->device_id);
218 sess_dev->destroy_comp = &dc;
219 rnbd_put_sess_dev(sess_dev);
220 wait_for_completion(&dc); /* wait for inflights to drop to zero */
222 blkdev_put(sess_dev->bdev, sess_dev->open_flags);
223 mutex_lock(&sess_dev->dev->lock);
224 list_del(&sess_dev->dev_list);
225 if (sess_dev->open_flags & FMODE_WRITE)
226 sess_dev->dev->open_write_cnt--;
227 mutex_unlock(&sess_dev->dev->lock);
229 rnbd_put_srv_dev(sess_dev->dev);
231 rnbd_srv_info(sess_dev, "Device closed\n");
235 static void destroy_sess(struct rnbd_srv_session *srv_sess)
237 struct rnbd_srv_sess_dev *sess_dev;
240 if (xa_empty(&srv_sess->index_idr))
243 trace_destroy_sess(srv_sess);
245 mutex_lock(&srv_sess->lock);
246 xa_for_each(&srv_sess->index_idr, index, sess_dev)
247 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
248 mutex_unlock(&srv_sess->lock);
251 xa_destroy(&srv_sess->index_idr);
253 pr_info("RTRS Session %s disconnected\n", srv_sess->sessname);
255 mutex_lock(&sess_lock);
256 list_del(&srv_sess->list);
257 mutex_unlock(&sess_lock);
259 mutex_destroy(&srv_sess->lock);
263 static int create_sess(struct rtrs_srv_sess *rtrs)
265 struct rnbd_srv_session *srv_sess;
266 char pathname[NAME_MAX];
269 err = rtrs_srv_get_path_name(rtrs, pathname, sizeof(pathname));
271 pr_err("rtrs_srv_get_path_name(%s): %d\n", pathname, err);
275 srv_sess = kzalloc(sizeof(*srv_sess), GFP_KERNEL);
279 srv_sess->queue_depth = rtrs_srv_get_queue_depth(rtrs);
280 xa_init_flags(&srv_sess->index_idr, XA_FLAGS_ALLOC);
281 mutex_init(&srv_sess->lock);
282 mutex_lock(&sess_lock);
283 list_add(&srv_sess->list, &sess_list);
284 mutex_unlock(&sess_lock);
286 srv_sess->rtrs = rtrs;
287 strscpy(srv_sess->sessname, pathname, sizeof(srv_sess->sessname));
289 rtrs_srv_set_sess_priv(rtrs, srv_sess);
291 trace_create_sess(srv_sess);
296 static int rnbd_srv_link_ev(struct rtrs_srv_sess *rtrs,
297 enum rtrs_srv_link_ev ev, void *priv)
299 struct rnbd_srv_session *srv_sess = priv;
302 case RTRS_SRV_LINK_EV_CONNECTED:
303 return create_sess(rtrs);
305 case RTRS_SRV_LINK_EV_DISCONNECTED:
306 if (WARN_ON_ONCE(!srv_sess))
309 destroy_sess(srv_sess);
313 pr_warn("Received unknown RTRS session event %d from session %s\n",
314 ev, srv_sess->sessname);
319 void rnbd_srv_sess_dev_force_close(struct rnbd_srv_sess_dev *sess_dev,
320 struct kobj_attribute *attr)
322 struct rnbd_srv_session *sess = sess_dev->sess;
324 /* It is already started to close by client's close message. */
325 if (!mutex_trylock(&sess->lock))
328 sess_dev->keep_id = true;
329 /* first remove sysfs itself to avoid deadlock */
330 sysfs_remove_file_self(&sess_dev->kobj, &attr->attr);
331 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
332 mutex_unlock(&sess->lock);
335 static void process_msg_close(struct rnbd_srv_session *srv_sess,
336 void *data, size_t datalen, const void *usr,
339 const struct rnbd_msg_close *close_msg = usr;
340 struct rnbd_srv_sess_dev *sess_dev;
342 trace_process_msg_close(srv_sess, close_msg);
344 sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id),
346 if (IS_ERR(sess_dev))
349 rnbd_put_sess_dev(sess_dev);
350 mutex_lock(&srv_sess->lock);
351 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
352 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, struct rtrs_srv_op *id,
364 void *data, size_t datalen,
365 const void *usr, size_t usrlen)
367 struct rnbd_srv_session *srv_sess = priv;
368 const struct rnbd_msg_hdr *hdr = usr;
372 if (WARN_ON_ONCE(!srv_sess))
375 type = le16_to_cpu(hdr->type);
379 return process_rdma(srv_sess, id, data, datalen, usr, usrlen);
381 process_msg_close(srv_sess, data, datalen, usr, usrlen);
384 ret = process_msg_open(srv_sess, usr, usrlen, data, datalen);
386 case RNBD_MSG_SESS_INFO:
387 ret = process_msg_sess_info(srv_sess, usr, usrlen, data,
391 pr_warn("Received unexpected message type %d from session %s\n",
392 type, srv_sess->sessname);
397 * Since ret is passed to rtrs to handle the failure case, we
398 * just return 0 at the end otherwise callers in rtrs would call
399 * send_io_resp_imm again to print redundant err message.
401 rtrs_srv_resp_rdma(id, ret);
405 static struct rnbd_srv_sess_dev
406 *rnbd_sess_dev_alloc(struct rnbd_srv_session *srv_sess)
408 struct rnbd_srv_sess_dev *sess_dev;
411 sess_dev = kzalloc(sizeof(*sess_dev), GFP_KERNEL);
413 return ERR_PTR(-ENOMEM);
415 error = xa_alloc(&srv_sess->index_idr, &sess_dev->device_id, sess_dev,
416 xa_limit_32b, GFP_NOWAIT);
418 pr_warn("Allocating idr failed, err: %d\n", error);
420 return ERR_PTR(error);
426 static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(struct block_device *bdev)
428 struct rnbd_srv_dev *dev;
430 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
432 return ERR_PTR(-ENOMEM);
434 snprintf(dev->id, sizeof(dev->id), "%pg", bdev);
435 kref_init(&dev->kref);
436 INIT_LIST_HEAD(&dev->sess_dev_list);
437 mutex_init(&dev->lock);
442 static struct rnbd_srv_dev *
443 rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev *new_dev)
445 struct rnbd_srv_dev *dev;
447 spin_lock(&dev_lock);
448 list_for_each_entry(dev, &dev_list, list) {
449 if (!strncmp(dev->id, new_dev->id, sizeof(dev->id))) {
450 if (!kref_get_unless_zero(&dev->kref))
452 * We lost the race, device is almost dead.
453 * Continue traversing to find a valid one.
456 spin_unlock(&dev_lock);
460 list_add(&new_dev->list, &dev_list);
461 spin_unlock(&dev_lock);
466 static int rnbd_srv_check_update_open_perm(struct rnbd_srv_dev *srv_dev,
467 struct rnbd_srv_session *srv_sess,
468 enum rnbd_access_mode access_mode)
472 mutex_lock(&srv_dev->lock);
474 switch (access_mode) {
479 if (srv_dev->open_write_cnt == 0) {
480 srv_dev->open_write_cnt++;
483 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",
484 srv_dev->id, srv_sess->sessname,
485 srv_dev->open_write_cnt,
486 rnbd_access_modes[access_mode].str);
489 case RNBD_ACCESS_MIGRATION:
490 if (srv_dev->open_write_cnt < 2) {
491 srv_dev->open_write_cnt++;
494 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",
495 srv_dev->id, srv_sess->sessname,
496 srv_dev->open_write_cnt,
497 rnbd_access_modes[access_mode].str);
501 pr_err("Received mapping request for device '%s' on session %s with invalid access mode: %d\n",
502 srv_dev->id, srv_sess->sessname, access_mode);
506 mutex_unlock(&srv_dev->lock);
511 static struct rnbd_srv_dev *
512 rnbd_srv_get_or_create_srv_dev(struct block_device *bdev,
513 struct rnbd_srv_session *srv_sess,
514 enum rnbd_access_mode access_mode)
517 struct rnbd_srv_dev *new_dev, *dev;
519 new_dev = rnbd_srv_init_srv_dev(bdev);
523 dev = rnbd_srv_find_or_add_srv_dev(new_dev);
527 ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode);
529 rnbd_put_srv_dev(dev);
536 static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp,
537 struct rnbd_srv_sess_dev *sess_dev)
539 struct block_device *bdev = sess_dev->bdev;
541 rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP);
542 rsp->device_id = cpu_to_le32(sess_dev->device_id);
543 rsp->nsectors = cpu_to_le64(bdev_nr_sectors(bdev));
544 rsp->logical_block_size = cpu_to_le16(bdev_logical_block_size(bdev));
545 rsp->physical_block_size = cpu_to_le16(bdev_physical_block_size(bdev));
546 rsp->max_segments = cpu_to_le16(bdev_max_segments(bdev));
547 rsp->max_hw_sectors =
548 cpu_to_le32(queue_max_hw_sectors(bdev_get_queue(bdev)));
549 rsp->max_write_same_sectors = 0;
550 rsp->max_discard_sectors = cpu_to_le32(bdev_max_discard_sectors(bdev));
551 rsp->discard_granularity = cpu_to_le32(bdev_discard_granularity(bdev));
552 rsp->discard_alignment = cpu_to_le32(bdev_discard_alignment(bdev));
553 rsp->secure_discard = cpu_to_le16(bdev_max_secure_erase_sectors(bdev));
554 rsp->cache_policy = 0;
555 if (bdev_write_cache(bdev))
556 rsp->cache_policy |= RNBD_WRITEBACK;
558 rsp->cache_policy |= RNBD_FUA;
561 static struct rnbd_srv_sess_dev *
562 rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess,
563 const struct rnbd_msg_open *open_msg,
564 struct block_device *bdev, fmode_t open_flags,
565 struct rnbd_srv_dev *srv_dev)
567 struct rnbd_srv_sess_dev *sdev = rnbd_sess_dev_alloc(srv_sess);
572 kref_init(&sdev->kref);
574 strscpy(sdev->pathname, open_msg->dev_name, sizeof(sdev->pathname));
577 sdev->sess = srv_sess;
579 sdev->open_flags = open_flags;
580 sdev->access_mode = open_msg->access_mode;
585 static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess,
586 const char *dev_name)
591 full_path = kmalloc(PATH_MAX, GFP_KERNEL);
593 return ERR_PTR(-ENOMEM);
596 * Replace %SESSNAME% with a real session name in order to
597 * create device namespace.
599 a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path));
601 int len = a - dev_search_path;
603 len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len,
604 dev_search_path, srv_sess->sessname, dev_name);
605 if (len >= PATH_MAX) {
606 pr_err("Too long path: %s, %s, %s\n",
607 dev_search_path, srv_sess->sessname, dev_name);
609 return ERR_PTR(-EINVAL);
612 snprintf(full_path, PATH_MAX, "%s/%s",
613 dev_search_path, dev_name);
616 /* eliminitate duplicated slashes */
617 a = strchr(full_path, '/');
620 if (*b == '/' && *a == '/') {
634 static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
635 const void *msg, size_t len,
636 void *data, size_t datalen)
638 const struct rnbd_msg_sess_info *sess_info_msg = msg;
639 struct rnbd_msg_sess_info_rsp *rsp = data;
641 srv_sess->ver = min_t(u8, sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
643 trace_process_msg_sess_info(srv_sess, sess_info_msg);
645 rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP);
646 rsp->ver = srv_sess->ver;
652 * find_srv_sess_dev() - a dev is already opened by this name
653 * @srv_sess: the session to search.
654 * @dev_name: string containing the name of the device.
656 * Return struct rnbd_srv_sess_dev if srv_sess already opened the dev_name
657 * NULL if the session didn't open the device yet.
659 static struct rnbd_srv_sess_dev *
660 find_srv_sess_dev(struct rnbd_srv_session *srv_sess, const char *dev_name)
662 struct rnbd_srv_sess_dev *sess_dev;
665 if (xa_empty(&srv_sess->index_idr))
668 xa_for_each(&srv_sess->index_idr, index, sess_dev)
669 if (!strcmp(sess_dev->pathname, dev_name))
675 static int process_msg_open(struct rnbd_srv_session *srv_sess,
676 const void *msg, size_t len,
677 void *data, size_t datalen)
680 struct rnbd_srv_dev *srv_dev;
681 struct rnbd_srv_sess_dev *srv_sess_dev;
682 const struct rnbd_msg_open *open_msg = msg;
683 struct block_device *bdev;
686 struct rnbd_msg_open_rsp *rsp = data;
688 trace_process_msg_open(srv_sess, open_msg);
690 open_flags = FMODE_READ;
691 if (open_msg->access_mode != RNBD_ACCESS_RO)
692 open_flags |= FMODE_WRITE;
694 mutex_lock(&srv_sess->lock);
696 srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name);
700 if ((strlen(dev_search_path) + strlen(open_msg->dev_name))
702 pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n",
703 srv_sess->sessname, dev_search_path, open_msg->dev_name,
708 if (strstr(open_msg->dev_name, "..")) {
709 pr_err("Opening device for session %s failed, device path %s contains relative path ..\n",
710 srv_sess->sessname, open_msg->dev_name);
714 full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name);
715 if (IS_ERR(full_path)) {
716 ret = PTR_ERR(full_path);
717 pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %d\n",
718 open_msg->dev_name, srv_sess->sessname, ret);
722 bdev = blkdev_get_by_path(full_path, open_flags, THIS_MODULE, NULL);
725 pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %d\n",
726 full_path, srv_sess->sessname, ret);
730 srv_dev = rnbd_srv_get_or_create_srv_dev(bdev, srv_sess,
731 open_msg->access_mode);
732 if (IS_ERR(srv_dev)) {
733 pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %ld\n",
734 full_path, srv_sess->sessname, PTR_ERR(srv_dev));
735 ret = PTR_ERR(srv_dev);
739 srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg,
742 if (IS_ERR(srv_sess_dev)) {
743 pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %ld\n",
744 full_path, srv_sess->sessname, PTR_ERR(srv_sess_dev));
745 ret = PTR_ERR(srv_sess_dev);
749 /* Create the srv_dev sysfs files if they haven't been created yet. The
750 * reason to delay the creation is not to create the sysfs files before
751 * we are sure the device can be opened.
753 mutex_lock(&srv_dev->lock);
754 if (!srv_dev->dev_kobj.state_in_sysfs) {
755 ret = rnbd_srv_create_dev_sysfs(srv_dev, bdev);
757 mutex_unlock(&srv_dev->lock);
758 rnbd_srv_err(srv_sess_dev,
759 "Opening device failed, failed to create device sysfs files, err: %d\n",
761 goto free_srv_sess_dev;
765 ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
767 mutex_unlock(&srv_dev->lock);
768 rnbd_srv_err(srv_sess_dev,
769 "Opening device failed, failed to create dev client sysfs files, err: %d\n",
771 goto free_srv_sess_dev;
774 list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
775 mutex_unlock(&srv_dev->lock);
777 rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id);
782 rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev);
783 mutex_unlock(&srv_sess->lock);
787 xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id);
791 if (open_msg->access_mode != RNBD_ACCESS_RO) {
792 mutex_lock(&srv_dev->lock);
793 srv_dev->open_write_cnt--;
794 mutex_unlock(&srv_dev->lock);
796 rnbd_put_srv_dev(srv_dev);
798 blkdev_put(bdev, open_flags);
802 mutex_unlock(&srv_sess->lock);
806 static struct rtrs_srv_ctx *rtrs_ctx;
808 static struct rtrs_srv_ops rtrs_ops;
809 static int __init rnbd_srv_init_module(void)
813 BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4);
814 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36);
815 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36);
816 BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264);
817 BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8);
818 BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56);
819 rtrs_ops = (struct rtrs_srv_ops) {
820 .rdma_ev = rnbd_srv_rdma_ev,
821 .link_ev = rnbd_srv_link_ev,
823 rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr);
824 if (IS_ERR(rtrs_ctx)) {
825 err = PTR_ERR(rtrs_ctx);
826 pr_err("rtrs_srv_open(), err: %d\n", err);
830 err = rnbd_srv_create_sysfs_files();
832 pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err);
833 rtrs_srv_close(rtrs_ctx);
840 static void __exit rnbd_srv_cleanup_module(void)
842 rtrs_srv_close(rtrs_ctx);
843 WARN_ON(!list_empty(&sess_list));
844 rnbd_srv_destroy_sysfs_files();
847 module_init(rnbd_srv_init_module);
848 module_exit(rnbd_srv_cleanup_module);