cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

rnbd-srv.c (22669B)


      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
     18MODULE_DESCRIPTION("RDMA Network Block Device Server");
     19MODULE_LICENSE("GPL");
     20
     21static u16 port_nr = RTRS_PORT;
     22
     23module_param_named(port_nr, port_nr, ushort, 0444);
     24MODULE_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
     30static char dev_search_path[PATH_MAX] = DEFAULT_DEV_SEARCH_PATH;
     31
     32static 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
     47static struct kparam_string dev_search_path_kparam_str = {
     48	.maxlen	= sizeof(dev_search_path),
     49	.string	= dev_search_path
     50};
     51
     52static const struct kernel_param_ops dev_search_path_ops = {
     53	.set	= dev_search_path_set,
     54	.get	= param_get_string,
     55};
     56
     57module_param_cb(dev_search_path, &dev_search_path_ops,
     58		&dev_search_path_kparam_str, 0444);
     59MODULE_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
     63static DEFINE_MUTEX(sess_lock);
     64static DEFINE_SPINLOCK(dev_lock);
     65
     66static LIST_HEAD(sess_list);
     67static LIST_HEAD(dev_list);
     68
     69struct rnbd_io_private {
     70	struct rtrs_srv_op		*id;
     71	struct rnbd_srv_sess_dev	*sess_dev;
     72};
     73
     74static 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
     82static 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
     87void 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
     99static struct rnbd_srv_sess_dev *
    100rnbd_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 (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
    117static void rnbd_dev_bi_end_io(struct bio *bio)
    118{
    119	rnbd_endio(bio->bi_private, blk_status_to_errno(bio->bi_status));
    120	bio_put(bio);
    121}
    122
    123static 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)
    126{
    127	const struct rnbd_msg_io *msg = usr;
    128	struct rnbd_io_private *priv;
    129	struct rnbd_srv_sess_dev *sess_dev;
    130	u32 dev_id;
    131	int err;
    132	struct bio *bio;
    133	short prio;
    134
    135	priv = kmalloc(sizeof(*priv), GFP_KERNEL);
    136	if (!priv)
    137		return -ENOMEM;
    138
    139	dev_id = le32_to_cpu(msg->device_id);
    140
    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);
    145		err = -ENOTCONN;
    146		goto err;
    147	}
    148
    149	priv->sess_dev = sess_dev;
    150	priv->id = id;
    151
    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");
    157		err = -EINVAL;
    158		goto bio_put;
    159	}
    160
    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);
    168
    169	submit_bio(bio);
    170
    171	return 0;
    172
    173bio_put:
    174	bio_put(bio);
    175	rnbd_put_sess_dev(sess_dev);
    176err:
    177	kfree(priv);
    178	return err;
    179}
    180
    181static void destroy_device(struct kref *kref)
    182{
    183	struct rnbd_srv_dev *dev = container_of(kref, struct rnbd_srv_dev, kref);
    184
    185	WARN_ONCE(!list_empty(&dev->sess_dev_list),
    186		  "Device %s is being destroyed but still in use!\n",
    187		  dev->id);
    188
    189	spin_lock(&dev_lock);
    190	list_del(&dev->list);
    191	spin_unlock(&dev_lock);
    192
    193	mutex_destroy(&dev->lock);
    194	if (dev->dev_kobj.state_in_sysfs)
    195		/*
    196		 * Destroy kobj only if it was really created.
    197		 */
    198		rnbd_srv_destroy_dev_sysfs(dev);
    199	else
    200		kfree(dev);
    201}
    202
    203static void rnbd_put_srv_dev(struct rnbd_srv_dev *dev)
    204{
    205	kref_put(&dev->kref, destroy_device);
    206}
    207
    208void rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev *sess_dev, bool keep_id)
    209{
    210	DECLARE_COMPLETION_ONSTACK(dc);
    211
    212	if (keep_id)
    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,
    217			   sess_dev, NULL, 0);
    218	else
    219		xa_erase(&sess_dev->sess->index_idr, sess_dev->device_id);
    220	synchronize_rcu();
    221
    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 */
    225
    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);
    233
    234	rnbd_put_srv_dev(sess_dev->dev);
    235
    236	rnbd_srv_info(sess_dev, "Device closed\n");
    237	kfree(sess_dev);
    238}
    239
    240static void destroy_sess(struct rnbd_srv_session *srv_sess)
    241{
    242	struct rnbd_srv_sess_dev *sess_dev, *tmp;
    243
    244	if (list_empty(&srv_sess->sess_dev_list))
    245		goto out;
    246
    247	mutex_lock(&srv_sess->lock);
    248	list_for_each_entry_safe(sess_dev, tmp, &srv_sess->sess_dev_list,
    249				 sess_list)
    250		rnbd_srv_destroy_dev_session_sysfs(sess_dev);
    251	mutex_unlock(&srv_sess->lock);
    252
    253out:
    254	xa_destroy(&srv_sess->index_idr);
    255
    256	pr_info("RTRS Session %s disconnected\n", srv_sess->sessname);
    257
    258	mutex_lock(&sess_lock);
    259	list_del(&srv_sess->list);
    260	mutex_unlock(&sess_lock);
    261
    262	mutex_destroy(&srv_sess->lock);
    263	kfree(srv_sess);
    264}
    265
    266static int create_sess(struct rtrs_srv_sess *rtrs)
    267{
    268	struct rnbd_srv_session *srv_sess;
    269	char pathname[NAME_MAX];
    270	int err;
    271
    272	err = rtrs_srv_get_path_name(rtrs, pathname, sizeof(pathname));
    273	if (err) {
    274		pr_err("rtrs_srv_get_path_name(%s): %d\n", pathname, err);
    275
    276		return err;
    277	}
    278	srv_sess = kzalloc(sizeof(*srv_sess), GFP_KERNEL);
    279	if (!srv_sess)
    280		return -ENOMEM;
    281
    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);
    289
    290	srv_sess->rtrs = rtrs;
    291	strscpy(srv_sess->sessname, pathname, sizeof(srv_sess->sessname));
    292
    293	rtrs_srv_set_sess_priv(rtrs, srv_sess);
    294
    295	return 0;
    296}
    297
    298static int rnbd_srv_link_ev(struct rtrs_srv_sess *rtrs,
    299			     enum rtrs_srv_link_ev ev, void *priv)
    300{
    301	struct rnbd_srv_session *srv_sess = priv;
    302
    303	switch (ev) {
    304	case RTRS_SRV_LINK_EV_CONNECTED:
    305		return create_sess(rtrs);
    306
    307	case RTRS_SRV_LINK_EV_DISCONNECTED:
    308		if (WARN_ON_ONCE(!srv_sess))
    309			return -EINVAL;
    310
    311		destroy_sess(srv_sess);
    312		return 0;
    313
    314	default:
    315		pr_warn("Received unknown RTRS session event %d from session %s\n",
    316			ev, srv_sess->sessname);
    317		return -EINVAL;
    318	}
    319}
    320
    321void rnbd_srv_sess_dev_force_close(struct rnbd_srv_sess_dev *sess_dev,
    322				   struct kobj_attribute *attr)
    323{
    324	struct rnbd_srv_session	*sess = sess_dev->sess;
    325
    326	sess_dev->keep_id = true;
    327	/* It is already started to close by client's close message. */
    328	if (!mutex_trylock(&sess->lock))
    329		return;
    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);
    334}
    335
    336static int process_msg_close(struct rnbd_srv_session *srv_sess,
    337			     void *data, size_t datalen, const void *usr,
    338			     size_t usrlen)
    339{
    340	const struct rnbd_msg_close *close_msg = usr;
    341	struct rnbd_srv_sess_dev *sess_dev;
    342
    343	sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id),
    344				      srv_sess);
    345	if (IS_ERR(sess_dev))
    346		return 0;
    347
    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);
    352	return 0;
    353}
    354
    355static int process_msg_open(struct rnbd_srv_session *srv_sess,
    356			    const void *msg, size_t len,
    357			    void *data, size_t datalen);
    358
    359static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
    360				 const void *msg, size_t len,
    361				 void *data, size_t datalen);
    362
    363static int rnbd_srv_rdma_ev(void *priv,
    364			    struct rtrs_srv_op *id, int dir,
    365			    void *data, size_t datalen, const void *usr,
    366			    size_t usrlen)
    367{
    368	struct rnbd_srv_session *srv_sess = priv;
    369	const struct rnbd_msg_hdr *hdr = usr;
    370	int ret = 0;
    371	u16 type;
    372
    373	if (WARN_ON_ONCE(!srv_sess))
    374		return -ENODEV;
    375
    376	type = le16_to_cpu(hdr->type);
    377
    378	switch (type) {
    379	case RNBD_MSG_IO:
    380		return process_rdma(srv_sess, id, data, datalen, usr, usrlen);
    381	case RNBD_MSG_CLOSE:
    382		ret = process_msg_close(srv_sess, data, datalen, usr, usrlen);
    383		break;
    384	case RNBD_MSG_OPEN:
    385		ret = process_msg_open(srv_sess, usr, usrlen, data, datalen);
    386		break;
    387	case RNBD_MSG_SESS_INFO:
    388		ret = process_msg_sess_info(srv_sess, usr, usrlen, data,
    389					    datalen);
    390		break;
    391	default:
    392		pr_warn("Received unexpected message type %d with dir %d from session %s\n",
    393			type, dir, srv_sess->sessname);
    394		return -EINVAL;
    395	}
    396
    397	rtrs_srv_resp_rdma(id, ret);
    398	return 0;
    399}
    400
    401static struct rnbd_srv_sess_dev
    402*rnbd_sess_dev_alloc(struct rnbd_srv_session *srv_sess)
    403{
    404	struct rnbd_srv_sess_dev *sess_dev;
    405	int error;
    406
    407	sess_dev = kzalloc(sizeof(*sess_dev), GFP_KERNEL);
    408	if (!sess_dev)
    409		return ERR_PTR(-ENOMEM);
    410
    411	error = xa_alloc(&srv_sess->index_idr, &sess_dev->device_id, sess_dev,
    412			 xa_limit_32b, GFP_NOWAIT);
    413	if (error < 0) {
    414		pr_warn("Allocating idr failed, err: %d\n", error);
    415		kfree(sess_dev);
    416		return ERR_PTR(error);
    417	}
    418
    419	return sess_dev;
    420}
    421
    422static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(const char *id)
    423{
    424	struct rnbd_srv_dev *dev;
    425
    426	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
    427	if (!dev)
    428		return ERR_PTR(-ENOMEM);
    429
    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);
    434
    435	return dev;
    436}
    437
    438static struct rnbd_srv_dev *
    439rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev *new_dev)
    440{
    441	struct rnbd_srv_dev *dev;
    442
    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))
    447				/*
    448				 * We lost the race, device is almost dead.
    449				 *  Continue traversing to find a valid one.
    450				 */
    451				continue;
    452			spin_unlock(&dev_lock);
    453			return dev;
    454		}
    455	}
    456	list_add(&new_dev->list, &dev_list);
    457	spin_unlock(&dev_lock);
    458
    459	return new_dev;
    460}
    461
    462static 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)
    465{
    466	int ret = -EPERM;
    467
    468	mutex_lock(&srv_dev->lock);
    469
    470	switch (access_mode) {
    471	case RNBD_ACCESS_RO:
    472		ret = 0;
    473		break;
    474	case RNBD_ACCESS_RW:
    475		if (srv_dev->open_write_cnt == 0)  {
    476			srv_dev->open_write_cnt++;
    477			ret = 0;
    478		} else {
    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));
    483		}
    484		break;
    485	case RNBD_ACCESS_MIGRATION:
    486		if (srv_dev->open_write_cnt < 2) {
    487			srv_dev->open_write_cnt++;
    488			ret = 0;
    489		} else {
    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));
    494		}
    495		break;
    496	default:
    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);
    499		ret = -EINVAL;
    500	}
    501
    502	mutex_unlock(&srv_dev->lock);
    503
    504	return ret;
    505}
    506
    507static struct rnbd_srv_dev *
    508rnbd_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)
    511{
    512	int ret;
    513	struct rnbd_srv_dev *new_dev, *dev;
    514
    515	new_dev = rnbd_srv_init_srv_dev(rnbd_dev->name);
    516	if (IS_ERR(new_dev))
    517		return new_dev;
    518
    519	dev = rnbd_srv_find_or_add_srv_dev(new_dev);
    520	if (dev != new_dev)
    521		kfree(new_dev);
    522
    523	ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode);
    524	if (ret) {
    525		rnbd_put_srv_dev(dev);
    526		return ERR_PTR(ret);
    527	}
    528
    529	return dev;
    530}
    531
    532static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp,
    533					struct rnbd_srv_sess_dev *sess_dev)
    534{
    535	struct rnbd_dev *rnbd_dev = sess_dev->rnbd_dev;
    536
    537	rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP);
    538	rsp->device_id =
    539		cpu_to_le32(sess_dev->device_id);
    540	rsp->nsectors =
    541		cpu_to_le64(get_capacity(rnbd_dev->bdev->bd_disk));
    542	rsp->logical_block_size	=
    543		cpu_to_le16(bdev_logical_block_size(rnbd_dev->bdev));
    544	rsp->physical_block_size =
    545		cpu_to_le16(bdev_physical_block_size(rnbd_dev->bdev));
    546	rsp->max_segments =
    547		cpu_to_le16(rnbd_dev_get_max_segs(rnbd_dev));
    548	rsp->max_hw_sectors =
    549		cpu_to_le32(rnbd_dev_get_max_hw_sects(rnbd_dev));
    550	rsp->max_write_same_sectors = 0;
    551	rsp->max_discard_sectors =
    552		cpu_to_le32(rnbd_dev_get_max_discard_sects(rnbd_dev));
    553	rsp->discard_granularity =
    554		cpu_to_le32(rnbd_dev_get_discard_granularity(rnbd_dev));
    555	rsp->discard_alignment =
    556		cpu_to_le32(rnbd_dev_get_discard_alignment(rnbd_dev));
    557	rsp->secure_discard =
    558		cpu_to_le16(rnbd_dev_get_secure_discard(rnbd_dev));
    559	rsp->cache_policy = 0;
    560	if (bdev_write_cache(rnbd_dev->bdev))
    561		rsp->cache_policy |= RNBD_WRITEBACK;
    562	if (bdev_fua(rnbd_dev->bdev))
    563		rsp->cache_policy |= RNBD_FUA;
    564}
    565
    566static struct rnbd_srv_sess_dev *
    567rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess,
    568			      const struct rnbd_msg_open *open_msg,
    569			      struct rnbd_dev *rnbd_dev, fmode_t open_flags,
    570			      struct rnbd_srv_dev *srv_dev)
    571{
    572	struct rnbd_srv_sess_dev *sdev = rnbd_sess_dev_alloc(srv_sess);
    573
    574	if (IS_ERR(sdev))
    575		return sdev;
    576
    577	kref_init(&sdev->kref);
    578
    579	strscpy(sdev->pathname, open_msg->dev_name, sizeof(sdev->pathname));
    580
    581	sdev->rnbd_dev		= rnbd_dev;
    582	sdev->sess		= srv_sess;
    583	sdev->dev		= srv_dev;
    584	sdev->open_flags	= open_flags;
    585	sdev->access_mode	= open_msg->access_mode;
    586
    587	return sdev;
    588}
    589
    590static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess,
    591				     const char *dev_name)
    592{
    593	char *full_path;
    594	char *a, *b;
    595
    596	full_path = kmalloc(PATH_MAX, GFP_KERNEL);
    597	if (!full_path)
    598		return ERR_PTR(-ENOMEM);
    599
    600	/*
    601	 * Replace %SESSNAME% with a real session name in order to
    602	 * create device namespace.
    603	 */
    604	a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path));
    605	if (a) {
    606		int len = a - dev_search_path;
    607
    608		len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len,
    609			       dev_search_path, srv_sess->sessname, dev_name);
    610		if (len >= PATH_MAX) {
    611			pr_err("Too long path: %s, %s, %s\n",
    612			       dev_search_path, srv_sess->sessname, dev_name);
    613			kfree(full_path);
    614			return ERR_PTR(-EINVAL);
    615		}
    616	} else {
    617		snprintf(full_path, PATH_MAX, "%s/%s",
    618			 dev_search_path, dev_name);
    619	}
    620
    621	/* eliminitate duplicated slashes */
    622	a = strchr(full_path, '/');
    623	b = a;
    624	while (*b != '\0') {
    625		if (*b == '/' && *a == '/') {
    626			b++;
    627		} else {
    628			a++;
    629			*a = *b;
    630			b++;
    631		}
    632	}
    633	a++;
    634	*a = '\0';
    635
    636	return full_path;
    637}
    638
    639static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
    640				 const void *msg, size_t len,
    641				 void *data, size_t datalen)
    642{
    643	const struct rnbd_msg_sess_info *sess_info_msg = msg;
    644	struct rnbd_msg_sess_info_rsp *rsp = data;
    645
    646	srv_sess->ver = min_t(u8, sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
    647	pr_debug("Session %s using protocol version %d (client version: %d, server version: %d)\n",
    648		 srv_sess->sessname, srv_sess->ver,
    649		 sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
    650
    651	rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP);
    652	rsp->ver = srv_sess->ver;
    653
    654	return 0;
    655}
    656
    657/**
    658 * find_srv_sess_dev() - a dev is already opened by this name
    659 * @srv_sess:	the session to search.
    660 * @dev_name:	string containing the name of the device.
    661 *
    662 * Return struct rnbd_srv_sess_dev if srv_sess already opened the dev_name
    663 * NULL if the session didn't open the device yet.
    664 */
    665static struct rnbd_srv_sess_dev *
    666find_srv_sess_dev(struct rnbd_srv_session *srv_sess, const char *dev_name)
    667{
    668	struct rnbd_srv_sess_dev *sess_dev;
    669
    670	if (list_empty(&srv_sess->sess_dev_list))
    671		return NULL;
    672
    673	list_for_each_entry(sess_dev, &srv_sess->sess_dev_list, sess_list)
    674		if (!strcmp(sess_dev->pathname, dev_name))
    675			return sess_dev;
    676
    677	return NULL;
    678}
    679
    680static int process_msg_open(struct rnbd_srv_session *srv_sess,
    681			    const void *msg, size_t len,
    682			    void *data, size_t datalen)
    683{
    684	int ret;
    685	struct rnbd_srv_dev *srv_dev;
    686	struct rnbd_srv_sess_dev *srv_sess_dev;
    687	const struct rnbd_msg_open *open_msg = msg;
    688	fmode_t open_flags;
    689	char *full_path;
    690	struct rnbd_dev *rnbd_dev;
    691	struct rnbd_msg_open_rsp *rsp = data;
    692
    693	pr_debug("Open message received: session='%s' path='%s' access_mode=%d\n",
    694		 srv_sess->sessname, open_msg->dev_name,
    695		 open_msg->access_mode);
    696	open_flags = FMODE_READ;
    697	if (open_msg->access_mode != RNBD_ACCESS_RO)
    698		open_flags |= FMODE_WRITE;
    699
    700	mutex_lock(&srv_sess->lock);
    701
    702	srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name);
    703	if (srv_sess_dev)
    704		goto fill_response;
    705
    706	if ((strlen(dev_search_path) + strlen(open_msg->dev_name))
    707	    >= PATH_MAX) {
    708		pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n",
    709		       srv_sess->sessname, dev_search_path, open_msg->dev_name,
    710		       PATH_MAX);
    711		ret = -EINVAL;
    712		goto reject;
    713	}
    714	if (strstr(open_msg->dev_name, "..")) {
    715		pr_err("Opening device for session %s failed, device path %s contains relative path ..\n",
    716		       srv_sess->sessname, open_msg->dev_name);
    717		ret = -EINVAL;
    718		goto reject;
    719	}
    720	full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name);
    721	if (IS_ERR(full_path)) {
    722		ret = PTR_ERR(full_path);
    723		pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %d\n",
    724		       open_msg->dev_name, srv_sess->sessname, ret);
    725		goto reject;
    726	}
    727
    728	rnbd_dev = rnbd_dev_open(full_path, open_flags);
    729	if (IS_ERR(rnbd_dev)) {
    730		pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %ld\n",
    731		       full_path, srv_sess->sessname, PTR_ERR(rnbd_dev));
    732		ret = PTR_ERR(rnbd_dev);
    733		goto free_path;
    734	}
    735
    736	srv_dev = rnbd_srv_get_or_create_srv_dev(rnbd_dev, srv_sess,
    737						  open_msg->access_mode);
    738	if (IS_ERR(srv_dev)) {
    739		pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %ld\n",
    740		       full_path, srv_sess->sessname, PTR_ERR(srv_dev));
    741		ret = PTR_ERR(srv_dev);
    742		goto rnbd_dev_close;
    743	}
    744
    745	srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg,
    746						     rnbd_dev, open_flags,
    747						     srv_dev);
    748	if (IS_ERR(srv_sess_dev)) {
    749		pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %ld\n",
    750		       full_path, srv_sess->sessname, PTR_ERR(srv_sess_dev));
    751		ret = PTR_ERR(srv_sess_dev);
    752		goto srv_dev_put;
    753	}
    754
    755	/* Create the srv_dev sysfs files if they haven't been created yet. The
    756	 * reason to delay the creation is not to create the sysfs files before
    757	 * we are sure the device can be opened.
    758	 */
    759	mutex_lock(&srv_dev->lock);
    760	if (!srv_dev->dev_kobj.state_in_sysfs) {
    761		ret = rnbd_srv_create_dev_sysfs(srv_dev, rnbd_dev->bdev,
    762						 rnbd_dev->name);
    763		if (ret) {
    764			mutex_unlock(&srv_dev->lock);
    765			rnbd_srv_err(srv_sess_dev,
    766				      "Opening device failed, failed to create device sysfs files, err: %d\n",
    767				      ret);
    768			goto free_srv_sess_dev;
    769		}
    770	}
    771
    772	ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
    773	if (ret) {
    774		mutex_unlock(&srv_dev->lock);
    775		rnbd_srv_err(srv_sess_dev,
    776			      "Opening device failed, failed to create dev client sysfs files, err: %d\n",
    777			      ret);
    778		goto free_srv_sess_dev;
    779	}
    780
    781	list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
    782	mutex_unlock(&srv_dev->lock);
    783
    784	list_add(&srv_sess_dev->sess_list, &srv_sess->sess_dev_list);
    785
    786	rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id);
    787
    788	kfree(full_path);
    789
    790fill_response:
    791	rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev);
    792	mutex_unlock(&srv_sess->lock);
    793	return 0;
    794
    795free_srv_sess_dev:
    796	xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id);
    797	synchronize_rcu();
    798	kfree(srv_sess_dev);
    799srv_dev_put:
    800	if (open_msg->access_mode != RNBD_ACCESS_RO) {
    801		mutex_lock(&srv_dev->lock);
    802		srv_dev->open_write_cnt--;
    803		mutex_unlock(&srv_dev->lock);
    804	}
    805	rnbd_put_srv_dev(srv_dev);
    806rnbd_dev_close:
    807	rnbd_dev_close(rnbd_dev);
    808free_path:
    809	kfree(full_path);
    810reject:
    811	mutex_unlock(&srv_sess->lock);
    812	return ret;
    813}
    814
    815static struct rtrs_srv_ctx *rtrs_ctx;
    816
    817static struct rtrs_srv_ops rtrs_ops;
    818static int __init rnbd_srv_init_module(void)
    819{
    820	int err;
    821
    822	BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4);
    823	BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36);
    824	BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36);
    825	BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264);
    826	BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8);
    827	BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56);
    828	rtrs_ops = (struct rtrs_srv_ops) {
    829		.rdma_ev = rnbd_srv_rdma_ev,
    830		.link_ev = rnbd_srv_link_ev,
    831	};
    832	rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr);
    833	if (IS_ERR(rtrs_ctx)) {
    834		err = PTR_ERR(rtrs_ctx);
    835		pr_err("rtrs_srv_open(), err: %d\n", err);
    836		return err;
    837	}
    838
    839	err = rnbd_srv_create_sysfs_files();
    840	if (err) {
    841		pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err);
    842		rtrs_srv_close(rtrs_ctx);
    843		return err;
    844	}
    845
    846	return 0;
    847}
    848
    849static void __exit rnbd_srv_cleanup_module(void)
    850{
    851	rtrs_srv_close(rtrs_ctx);
    852	WARN_ON(!list_empty(&sess_list));
    853	rnbd_srv_destroy_sysfs_files();
    854}
    855
    856module_init(rnbd_srv_init_module);
    857module_exit(rnbd_srv_cleanup_module);