xref: /openbmc/linux/drivers/block/rnbd/rnbd-srv.c (revision dff03381)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * RDMA Network Block Driver
4  *
5  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8  */
9 #undef pr_fmt
10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
11 
12 #include <linux/module.h>
13 #include <linux/blkdev.h>
14 
15 #include "rnbd-srv.h"
16 #include "rnbd-srv-dev.h"
17 
18 MODULE_DESCRIPTION("RDMA Network Block Device Server");
19 MODULE_LICENSE("GPL");
20 
21 static u16 port_nr = RTRS_PORT;
22 
23 module_param_named(port_nr, port_nr, ushort, 0444);
24 MODULE_PARM_DESC(port_nr,
25 		 "The port number the server is listening on (default: "
26 		 __stringify(RTRS_PORT)")");
27 
28 #define DEFAULT_DEV_SEARCH_PATH "/"
29 
30 static char dev_search_path[PATH_MAX] = DEFAULT_DEV_SEARCH_PATH;
31 
32 static int dev_search_path_set(const char *val, const struct kernel_param *kp)
33 {
34 	const char *p = strrchr(val, '\n') ? : val + strlen(val);
35 
36 	if (strlen(val) >= sizeof(dev_search_path))
37 		return -EINVAL;
38 
39 	snprintf(dev_search_path, sizeof(dev_search_path), "%.*s",
40 		 (int)(p - val), val);
41 
42 	pr_info("dev_search_path changed to '%s'\n", dev_search_path);
43 
44 	return 0;
45 }
46 
47 static struct kparam_string dev_search_path_kparam_str = {
48 	.maxlen	= sizeof(dev_search_path),
49 	.string	= dev_search_path
50 };
51 
52 static const struct kernel_param_ops dev_search_path_ops = {
53 	.set	= dev_search_path_set,
54 	.get	= param_get_string,
55 };
56 
57 module_param_cb(dev_search_path, &dev_search_path_ops,
58 		&dev_search_path_kparam_str, 0444);
59 MODULE_PARM_DESC(dev_search_path,
60 		 "Sets the dev_search_path. When a device is mapped this path is prepended to the device path from the map device operation.  If %SESSNAME% is specified in a path, then device will be searched in a session namespace. (default: "
61 		 DEFAULT_DEV_SEARCH_PATH ")");
62 
63 static DEFINE_MUTEX(sess_lock);
64 static DEFINE_SPINLOCK(dev_lock);
65 
66 static LIST_HEAD(sess_list);
67 static LIST_HEAD(dev_list);
68 
69 struct rnbd_io_private {
70 	struct rtrs_srv_op		*id;
71 	struct rnbd_srv_sess_dev	*sess_dev;
72 };
73 
74 static void rnbd_sess_dev_release(struct kref *kref)
75 {
76 	struct rnbd_srv_sess_dev *sess_dev;
77 
78 	sess_dev = container_of(kref, struct rnbd_srv_sess_dev, kref);
79 	complete(sess_dev->destroy_comp);
80 }
81 
82 static inline void rnbd_put_sess_dev(struct rnbd_srv_sess_dev *sess_dev)
83 {
84 	kref_put(&sess_dev->kref, rnbd_sess_dev_release);
85 }
86 
87 void rnbd_endio(void *priv, int error)
88 {
89 	struct rnbd_io_private *rnbd_priv = priv;
90 	struct rnbd_srv_sess_dev *sess_dev = rnbd_priv->sess_dev;
91 
92 	rnbd_put_sess_dev(sess_dev);
93 
94 	rtrs_srv_resp_rdma(rnbd_priv->id, error);
95 
96 	kfree(priv);
97 }
98 
99 static struct rnbd_srv_sess_dev *
100 rnbd_get_sess_dev(int dev_id, struct rnbd_srv_session *srv_sess)
101 {
102 	struct rnbd_srv_sess_dev *sess_dev;
103 	int ret = 0;
104 
105 	rcu_read_lock();
106 	sess_dev = xa_load(&srv_sess->index_idr, dev_id);
107 	if (sess_dev)
108 		ret = kref_get_unless_zero(&sess_dev->kref);
109 	rcu_read_unlock();
110 
111 	if (!sess_dev || !ret)
112 		return ERR_PTR(-ENXIO);
113 
114 	return sess_dev;
115 }
116 
117 static 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 
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)
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 
173 bio_put:
174 	bio_put(bio);
175 	rnbd_put_sess_dev(sess_dev);
176 err:
177 	kfree(priv);
178 	return err;
179 }
180 
181 static 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 
203 static void rnbd_put_srv_dev(struct rnbd_srv_dev *dev)
204 {
205 	kref_put(&dev->kref, destroy_device);
206 }
207 
208 void 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 	mutex_lock(&sess_dev->dev->lock);
228 	list_del(&sess_dev->dev_list);
229 	if (sess_dev->open_flags & FMODE_WRITE)
230 		sess_dev->dev->open_write_cnt--;
231 	mutex_unlock(&sess_dev->dev->lock);
232 
233 	rnbd_put_srv_dev(sess_dev->dev);
234 
235 	rnbd_srv_info(sess_dev, "Device closed\n");
236 	kfree(sess_dev);
237 }
238 
239 static void destroy_sess(struct rnbd_srv_session *srv_sess)
240 {
241 	struct rnbd_srv_sess_dev *sess_dev;
242 	unsigned long index;
243 
244 	if (xa_empty(&srv_sess->index_idr))
245 		goto out;
246 
247 	mutex_lock(&srv_sess->lock);
248 	xa_for_each(&srv_sess->index_idr, index, sess_dev)
249 		rnbd_srv_destroy_dev_session_sysfs(sess_dev);
250 	mutex_unlock(&srv_sess->lock);
251 
252 out:
253 	xa_destroy(&srv_sess->index_idr);
254 
255 	pr_info("RTRS Session %s disconnected\n", srv_sess->sessname);
256 
257 	mutex_lock(&sess_lock);
258 	list_del(&srv_sess->list);
259 	mutex_unlock(&sess_lock);
260 
261 	mutex_destroy(&srv_sess->lock);
262 	kfree(srv_sess);
263 }
264 
265 static int create_sess(struct rtrs_srv_sess *rtrs)
266 {
267 	struct rnbd_srv_session *srv_sess;
268 	char pathname[NAME_MAX];
269 	int err;
270 
271 	err = rtrs_srv_get_path_name(rtrs, pathname, sizeof(pathname));
272 	if (err) {
273 		pr_err("rtrs_srv_get_path_name(%s): %d\n", pathname, err);
274 
275 		return err;
276 	}
277 	srv_sess = kzalloc(sizeof(*srv_sess), GFP_KERNEL);
278 	if (!srv_sess)
279 		return -ENOMEM;
280 
281 	srv_sess->queue_depth = rtrs_srv_get_queue_depth(rtrs);
282 	xa_init_flags(&srv_sess->index_idr, XA_FLAGS_ALLOC);
283 	mutex_init(&srv_sess->lock);
284 	mutex_lock(&sess_lock);
285 	list_add(&srv_sess->list, &sess_list);
286 	mutex_unlock(&sess_lock);
287 
288 	srv_sess->rtrs = rtrs;
289 	strscpy(srv_sess->sessname, pathname, sizeof(srv_sess->sessname));
290 
291 	rtrs_srv_set_sess_priv(rtrs, srv_sess);
292 
293 	return 0;
294 }
295 
296 static int rnbd_srv_link_ev(struct rtrs_srv_sess *rtrs,
297 			     enum rtrs_srv_link_ev ev, void *priv)
298 {
299 	struct rnbd_srv_session *srv_sess = priv;
300 
301 	switch (ev) {
302 	case RTRS_SRV_LINK_EV_CONNECTED:
303 		return create_sess(rtrs);
304 
305 	case RTRS_SRV_LINK_EV_DISCONNECTED:
306 		if (WARN_ON_ONCE(!srv_sess))
307 			return -EINVAL;
308 
309 		destroy_sess(srv_sess);
310 		return 0;
311 
312 	default:
313 		pr_warn("Received unknown RTRS session event %d from session %s\n",
314 			ev, srv_sess->sessname);
315 		return -EINVAL;
316 	}
317 }
318 
319 void rnbd_srv_sess_dev_force_close(struct rnbd_srv_sess_dev *sess_dev,
320 				   struct kobj_attribute *attr)
321 {
322 	struct rnbd_srv_session	*sess = sess_dev->sess;
323 
324 	/* It is already started to close by client's close message. */
325 	if (!mutex_trylock(&sess->lock))
326 		return;
327 
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);
333 }
334 
335 static int process_msg_close(struct rnbd_srv_session *srv_sess,
336 			     void *data, size_t datalen, const void *usr,
337 			     size_t usrlen)
338 {
339 	const struct rnbd_msg_close *close_msg = usr;
340 	struct rnbd_srv_sess_dev *sess_dev;
341 
342 	sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id),
343 				      srv_sess);
344 	if (IS_ERR(sess_dev))
345 		return 0;
346 
347 	rnbd_put_sess_dev(sess_dev);
348 	mutex_lock(&srv_sess->lock);
349 	rnbd_srv_destroy_dev_session_sysfs(sess_dev);
350 	mutex_unlock(&srv_sess->lock);
351 	return 0;
352 }
353 
354 static int process_msg_open(struct rnbd_srv_session *srv_sess,
355 			    const void *msg, size_t len,
356 			    void *data, size_t datalen);
357 
358 static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
359 				 const void *msg, size_t len,
360 				 void *data, size_t datalen);
361 
362 static int rnbd_srv_rdma_ev(void *priv,
363 			    struct rtrs_srv_op *id, int dir,
364 			    void *data, size_t datalen, const void *usr,
365 			    size_t usrlen)
366 {
367 	struct rnbd_srv_session *srv_sess = priv;
368 	const struct rnbd_msg_hdr *hdr = usr;
369 	int ret = 0;
370 	u16 type;
371 
372 	if (WARN_ON_ONCE(!srv_sess))
373 		return -ENODEV;
374 
375 	type = le16_to_cpu(hdr->type);
376 
377 	switch (type) {
378 	case RNBD_MSG_IO:
379 		return process_rdma(srv_sess, id, data, datalen, usr, usrlen);
380 	case RNBD_MSG_CLOSE:
381 		ret = process_msg_close(srv_sess, data, datalen, usr, usrlen);
382 		break;
383 	case RNBD_MSG_OPEN:
384 		ret = process_msg_open(srv_sess, usr, usrlen, data, datalen);
385 		break;
386 	case RNBD_MSG_SESS_INFO:
387 		ret = process_msg_sess_info(srv_sess, usr, usrlen, data,
388 					    datalen);
389 		break;
390 	default:
391 		pr_warn("Received unexpected message type %d with dir %d from session %s\n",
392 			type, dir, srv_sess->sessname);
393 		return -EINVAL;
394 	}
395 
396 	rtrs_srv_resp_rdma(id, ret);
397 	return 0;
398 }
399 
400 static struct rnbd_srv_sess_dev
401 *rnbd_sess_dev_alloc(struct rnbd_srv_session *srv_sess)
402 {
403 	struct rnbd_srv_sess_dev *sess_dev;
404 	int error;
405 
406 	sess_dev = kzalloc(sizeof(*sess_dev), GFP_KERNEL);
407 	if (!sess_dev)
408 		return ERR_PTR(-ENOMEM);
409 
410 	error = xa_alloc(&srv_sess->index_idr, &sess_dev->device_id, sess_dev,
411 			 xa_limit_32b, GFP_NOWAIT);
412 	if (error < 0) {
413 		pr_warn("Allocating idr failed, err: %d\n", error);
414 		kfree(sess_dev);
415 		return ERR_PTR(error);
416 	}
417 
418 	return sess_dev;
419 }
420 
421 static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(struct block_device *bdev)
422 {
423 	struct rnbd_srv_dev *dev;
424 
425 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
426 	if (!dev)
427 		return ERR_PTR(-ENOMEM);
428 
429 	snprintf(dev->id, sizeof(dev->id), "%pg", bdev);
430 	kref_init(&dev->kref);
431 	INIT_LIST_HEAD(&dev->sess_dev_list);
432 	mutex_init(&dev->lock);
433 
434 	return dev;
435 }
436 
437 static struct rnbd_srv_dev *
438 rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev *new_dev)
439 {
440 	struct rnbd_srv_dev *dev;
441 
442 	spin_lock(&dev_lock);
443 	list_for_each_entry(dev, &dev_list, list) {
444 		if (!strncmp(dev->id, new_dev->id, sizeof(dev->id))) {
445 			if (!kref_get_unless_zero(&dev->kref))
446 				/*
447 				 * We lost the race, device is almost dead.
448 				 *  Continue traversing to find a valid one.
449 				 */
450 				continue;
451 			spin_unlock(&dev_lock);
452 			return dev;
453 		}
454 	}
455 	list_add(&new_dev->list, &dev_list);
456 	spin_unlock(&dev_lock);
457 
458 	return new_dev;
459 }
460 
461 static int rnbd_srv_check_update_open_perm(struct rnbd_srv_dev *srv_dev,
462 					    struct rnbd_srv_session *srv_sess,
463 					    enum rnbd_access_mode access_mode)
464 {
465 	int ret = -EPERM;
466 
467 	mutex_lock(&srv_dev->lock);
468 
469 	switch (access_mode) {
470 	case RNBD_ACCESS_RO:
471 		ret = 0;
472 		break;
473 	case RNBD_ACCESS_RW:
474 		if (srv_dev->open_write_cnt == 0)  {
475 			srv_dev->open_write_cnt++;
476 			ret = 0;
477 		} else {
478 			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",
479 			       srv_dev->id, srv_sess->sessname,
480 			       srv_dev->open_write_cnt,
481 			       rnbd_access_mode_str(access_mode));
482 		}
483 		break;
484 	case RNBD_ACCESS_MIGRATION:
485 		if (srv_dev->open_write_cnt < 2) {
486 			srv_dev->open_write_cnt++;
487 			ret = 0;
488 		} else {
489 			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",
490 			       srv_dev->id, srv_sess->sessname,
491 			       srv_dev->open_write_cnt,
492 			       rnbd_access_mode_str(access_mode));
493 		}
494 		break;
495 	default:
496 		pr_err("Received mapping request for device '%s' on session %s with invalid access mode: %d\n",
497 		       srv_dev->id, srv_sess->sessname, access_mode);
498 		ret = -EINVAL;
499 	}
500 
501 	mutex_unlock(&srv_dev->lock);
502 
503 	return ret;
504 }
505 
506 static struct rnbd_srv_dev *
507 rnbd_srv_get_or_create_srv_dev(struct rnbd_dev *rnbd_dev,
508 				struct rnbd_srv_session *srv_sess,
509 				enum rnbd_access_mode access_mode)
510 {
511 	int ret;
512 	struct rnbd_srv_dev *new_dev, *dev;
513 
514 	new_dev = rnbd_srv_init_srv_dev(rnbd_dev->bdev);
515 	if (IS_ERR(new_dev))
516 		return new_dev;
517 
518 	dev = rnbd_srv_find_or_add_srv_dev(new_dev);
519 	if (dev != new_dev)
520 		kfree(new_dev);
521 
522 	ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode);
523 	if (ret) {
524 		rnbd_put_srv_dev(dev);
525 		return ERR_PTR(ret);
526 	}
527 
528 	return dev;
529 }
530 
531 static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp,
532 					struct rnbd_srv_sess_dev *sess_dev)
533 {
534 	struct rnbd_dev *rnbd_dev = sess_dev->rnbd_dev;
535 
536 	rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP);
537 	rsp->device_id =
538 		cpu_to_le32(sess_dev->device_id);
539 	rsp->nsectors =
540 		cpu_to_le64(get_capacity(rnbd_dev->bdev->bd_disk));
541 	rsp->logical_block_size	=
542 		cpu_to_le16(bdev_logical_block_size(rnbd_dev->bdev));
543 	rsp->physical_block_size =
544 		cpu_to_le16(bdev_physical_block_size(rnbd_dev->bdev));
545 	rsp->max_segments =
546 		cpu_to_le16(rnbd_dev_get_max_segs(rnbd_dev));
547 	rsp->max_hw_sectors =
548 		cpu_to_le32(rnbd_dev_get_max_hw_sects(rnbd_dev));
549 	rsp->max_write_same_sectors = 0;
550 	rsp->max_discard_sectors =
551 		cpu_to_le32(rnbd_dev_get_max_discard_sects(rnbd_dev));
552 	rsp->discard_granularity =
553 		cpu_to_le32(rnbd_dev_get_discard_granularity(rnbd_dev));
554 	rsp->discard_alignment =
555 		cpu_to_le32(rnbd_dev_get_discard_alignment(rnbd_dev));
556 	rsp->secure_discard =
557 		cpu_to_le16(rnbd_dev_get_secure_discard(rnbd_dev));
558 	rsp->cache_policy = 0;
559 	if (bdev_write_cache(rnbd_dev->bdev))
560 		rsp->cache_policy |= RNBD_WRITEBACK;
561 	if (bdev_fua(rnbd_dev->bdev))
562 		rsp->cache_policy |= RNBD_FUA;
563 }
564 
565 static struct rnbd_srv_sess_dev *
566 rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess,
567 			      const struct rnbd_msg_open *open_msg,
568 			      struct rnbd_dev *rnbd_dev, fmode_t open_flags,
569 			      struct rnbd_srv_dev *srv_dev)
570 {
571 	struct rnbd_srv_sess_dev *sdev = rnbd_sess_dev_alloc(srv_sess);
572 
573 	if (IS_ERR(sdev))
574 		return sdev;
575 
576 	kref_init(&sdev->kref);
577 
578 	strscpy(sdev->pathname, open_msg->dev_name, sizeof(sdev->pathname));
579 
580 	sdev->rnbd_dev		= rnbd_dev;
581 	sdev->sess		= srv_sess;
582 	sdev->dev		= srv_dev;
583 	sdev->open_flags	= open_flags;
584 	sdev->access_mode	= open_msg->access_mode;
585 
586 	return sdev;
587 }
588 
589 static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess,
590 				     const char *dev_name)
591 {
592 	char *full_path;
593 	char *a, *b;
594 
595 	full_path = kmalloc(PATH_MAX, GFP_KERNEL);
596 	if (!full_path)
597 		return ERR_PTR(-ENOMEM);
598 
599 	/*
600 	 * Replace %SESSNAME% with a real session name in order to
601 	 * create device namespace.
602 	 */
603 	a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path));
604 	if (a) {
605 		int len = a - dev_search_path;
606 
607 		len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len,
608 			       dev_search_path, srv_sess->sessname, dev_name);
609 		if (len >= PATH_MAX) {
610 			pr_err("Too long path: %s, %s, %s\n",
611 			       dev_search_path, srv_sess->sessname, dev_name);
612 			kfree(full_path);
613 			return ERR_PTR(-EINVAL);
614 		}
615 	} else {
616 		snprintf(full_path, PATH_MAX, "%s/%s",
617 			 dev_search_path, dev_name);
618 	}
619 
620 	/* eliminitate duplicated slashes */
621 	a = strchr(full_path, '/');
622 	b = a;
623 	while (*b != '\0') {
624 		if (*b == '/' && *a == '/') {
625 			b++;
626 		} else {
627 			a++;
628 			*a = *b;
629 			b++;
630 		}
631 	}
632 	a++;
633 	*a = '\0';
634 
635 	return full_path;
636 }
637 
638 static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
639 				 const void *msg, size_t len,
640 				 void *data, size_t datalen)
641 {
642 	const struct rnbd_msg_sess_info *sess_info_msg = msg;
643 	struct rnbd_msg_sess_info_rsp *rsp = data;
644 
645 	srv_sess->ver = min_t(u8, sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
646 	pr_debug("Session %s using protocol version %d (client version: %d, server version: %d)\n",
647 		 srv_sess->sessname, srv_sess->ver,
648 		 sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
649 
650 	rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP);
651 	rsp->ver = srv_sess->ver;
652 
653 	return 0;
654 }
655 
656 /**
657  * find_srv_sess_dev() - a dev is already opened by this name
658  * @srv_sess:	the session to search.
659  * @dev_name:	string containing the name of the device.
660  *
661  * Return struct rnbd_srv_sess_dev if srv_sess already opened the dev_name
662  * NULL if the session didn't open the device yet.
663  */
664 static struct rnbd_srv_sess_dev *
665 find_srv_sess_dev(struct rnbd_srv_session *srv_sess, const char *dev_name)
666 {
667 	struct rnbd_srv_sess_dev *sess_dev;
668 	unsigned long index;
669 
670 	if (xa_empty(&srv_sess->index_idr))
671 		return NULL;
672 
673 	xa_for_each(&srv_sess->index_idr, index, sess_dev)
674 		if (!strcmp(sess_dev->pathname, dev_name))
675 			return sess_dev;
676 
677 	return NULL;
678 }
679 
680 static 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 		if (ret) {
763 			mutex_unlock(&srv_dev->lock);
764 			rnbd_srv_err(srv_sess_dev,
765 				      "Opening device failed, failed to create device sysfs files, err: %d\n",
766 				      ret);
767 			goto free_srv_sess_dev;
768 		}
769 	}
770 
771 	ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
772 	if (ret) {
773 		mutex_unlock(&srv_dev->lock);
774 		rnbd_srv_err(srv_sess_dev,
775 			      "Opening device failed, failed to create dev client sysfs files, err: %d\n",
776 			      ret);
777 		goto free_srv_sess_dev;
778 	}
779 
780 	list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
781 	mutex_unlock(&srv_dev->lock);
782 
783 	rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id);
784 
785 	kfree(full_path);
786 
787 fill_response:
788 	rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev);
789 	mutex_unlock(&srv_sess->lock);
790 	return 0;
791 
792 free_srv_sess_dev:
793 	xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id);
794 	synchronize_rcu();
795 	kfree(srv_sess_dev);
796 srv_dev_put:
797 	if (open_msg->access_mode != RNBD_ACCESS_RO) {
798 		mutex_lock(&srv_dev->lock);
799 		srv_dev->open_write_cnt--;
800 		mutex_unlock(&srv_dev->lock);
801 	}
802 	rnbd_put_srv_dev(srv_dev);
803 rnbd_dev_close:
804 	rnbd_dev_close(rnbd_dev);
805 free_path:
806 	kfree(full_path);
807 reject:
808 	mutex_unlock(&srv_sess->lock);
809 	return ret;
810 }
811 
812 static struct rtrs_srv_ctx *rtrs_ctx;
813 
814 static struct rtrs_srv_ops rtrs_ops;
815 static int __init rnbd_srv_init_module(void)
816 {
817 	int err;
818 
819 	BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4);
820 	BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36);
821 	BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36);
822 	BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264);
823 	BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8);
824 	BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56);
825 	rtrs_ops = (struct rtrs_srv_ops) {
826 		.rdma_ev = rnbd_srv_rdma_ev,
827 		.link_ev = rnbd_srv_link_ev,
828 	};
829 	rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr);
830 	if (IS_ERR(rtrs_ctx)) {
831 		err = PTR_ERR(rtrs_ctx);
832 		pr_err("rtrs_srv_open(), err: %d\n", err);
833 		return err;
834 	}
835 
836 	err = rnbd_srv_create_sysfs_files();
837 	if (err) {
838 		pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err);
839 		rtrs_srv_close(rtrs_ctx);
840 		return err;
841 	}
842 
843 	return 0;
844 }
845 
846 static void __exit rnbd_srv_cleanup_module(void)
847 {
848 	rtrs_srv_close(rtrs_ctx);
849 	WARN_ON(!list_empty(&sess_list));
850 	rnbd_srv_destroy_sysfs_files();
851 }
852 
853 module_init(rnbd_srv_init_module);
854 module_exit(rnbd_srv_cleanup_module);
855