• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
dev_search_path_set(const char * val,const struct kernel_param * kp)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 
rnbd_sess_dev_release(struct kref * kref)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 
rnbd_put_sess_dev(struct rnbd_srv_sess_dev * sess_dev)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 
rnbd_endio(void * priv,int error)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 *
rnbd_get_sess_dev(int dev_id,struct rnbd_srv_session * srv_sess)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 (likely(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 
process_rdma(struct rtrs_srv * sess,struct rnbd_srv_session * srv_sess,struct rtrs_srv_op * id,void * data,u32 datalen,const void * usr,size_t usrlen)117 static int process_rdma(struct rtrs_srv *sess,
118 			struct rnbd_srv_session *srv_sess,
119 			struct rtrs_srv_op *id, void *data, u32 datalen,
120 			const void *usr, size_t usrlen)
121 {
122 	const struct rnbd_msg_io *msg = usr;
123 	struct rnbd_io_private *priv;
124 	struct rnbd_srv_sess_dev *sess_dev;
125 	u32 dev_id;
126 	int err;
127 	struct rnbd_dev_blk_io *io;
128 	struct bio *bio;
129 	short prio;
130 
131 	priv = kmalloc(sizeof(*priv), GFP_KERNEL);
132 	if (!priv)
133 		return -ENOMEM;
134 
135 	dev_id = le32_to_cpu(msg->device_id);
136 
137 	sess_dev = rnbd_get_sess_dev(dev_id, srv_sess);
138 	if (IS_ERR(sess_dev)) {
139 		pr_err_ratelimited("Got I/O request on session %s for unknown device id %d\n",
140 				   srv_sess->sessname, dev_id);
141 		err = -ENOTCONN;
142 		goto err;
143 	}
144 
145 	priv->sess_dev = sess_dev;
146 	priv->id = id;
147 
148 	/* Generate bio with pages pointing to the rdma buffer */
149 	bio = rnbd_bio_map_kern(data, sess_dev->rnbd_dev->ibd_bio_set, datalen, GFP_KERNEL);
150 	if (IS_ERR(bio)) {
151 		err = PTR_ERR(bio);
152 		rnbd_srv_err(sess_dev, "Failed to generate bio, err: %d\n", err);
153 		goto sess_dev_put;
154 	}
155 
156 	io = container_of(bio, struct rnbd_dev_blk_io, bio);
157 	io->dev = sess_dev->rnbd_dev;
158 	io->priv = priv;
159 
160 	bio->bi_end_io = rnbd_dev_bi_end_io;
161 	bio->bi_private = io;
162 	bio->bi_opf = rnbd_to_bio_flags(le32_to_cpu(msg->rw));
163 	bio->bi_iter.bi_sector = le64_to_cpu(msg->sector);
164 	bio->bi_iter.bi_size = le32_to_cpu(msg->bi_size);
165 	prio = srv_sess->ver < RNBD_PROTO_VER_MAJOR ||
166 	       usrlen < sizeof(*msg) ? 0 : le16_to_cpu(msg->prio);
167 	bio_set_prio(bio, prio);
168 	bio_set_dev(bio, sess_dev->rnbd_dev->bdev);
169 
170 	submit_bio(bio);
171 
172 	return 0;
173 
174 sess_dev_put:
175 	rnbd_put_sess_dev(sess_dev);
176 err:
177 	kfree(priv);
178 	return err;
179 }
180 
destroy_device(struct rnbd_srv_dev * dev)181 static void destroy_device(struct rnbd_srv_dev *dev)
182 {
183 	WARN_ONCE(!list_empty(&dev->sess_dev_list),
184 		  "Device %s is being destroyed but still in use!\n",
185 		  dev->id);
186 
187 	spin_lock(&dev_lock);
188 	list_del(&dev->list);
189 	spin_unlock(&dev_lock);
190 
191 	mutex_destroy(&dev->lock);
192 	if (dev->dev_kobj.state_in_sysfs)
193 		/*
194 		 * Destroy kobj only if it was really created.
195 		 */
196 		rnbd_srv_destroy_dev_sysfs(dev);
197 	else
198 		kfree(dev);
199 }
200 
destroy_device_cb(struct kref * kref)201 static void destroy_device_cb(struct kref *kref)
202 {
203 	struct rnbd_srv_dev *dev;
204 
205 	dev = container_of(kref, struct rnbd_srv_dev, kref);
206 
207 	destroy_device(dev);
208 }
209 
rnbd_put_srv_dev(struct rnbd_srv_dev * dev)210 static void rnbd_put_srv_dev(struct rnbd_srv_dev *dev)
211 {
212 	kref_put(&dev->kref, destroy_device_cb);
213 }
214 
rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev * sess_dev)215 void rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev *sess_dev)
216 {
217 	DECLARE_COMPLETION_ONSTACK(dc);
218 
219 	xa_erase(&sess_dev->sess->index_idr, sess_dev->device_id);
220 	synchronize_rcu();
221 	sess_dev->destroy_comp = &dc;
222 	rnbd_put_sess_dev(sess_dev);
223 	wait_for_completion(&dc); /* wait for inflights to drop to zero */
224 
225 	rnbd_dev_close(sess_dev->rnbd_dev);
226 	list_del(&sess_dev->sess_list);
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 
destroy_sess(struct rnbd_srv_session * srv_sess)239 static void destroy_sess(struct rnbd_srv_session *srv_sess)
240 {
241 	struct rnbd_srv_sess_dev *sess_dev, *tmp;
242 
243 	if (list_empty(&srv_sess->sess_dev_list))
244 		goto out;
245 
246 	mutex_lock(&srv_sess->lock);
247 	list_for_each_entry_safe(sess_dev, tmp, &srv_sess->sess_dev_list,
248 				 sess_list)
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 	bioset_exit(&srv_sess->sess_bio_set);
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 
create_sess(struct rtrs_srv * rtrs)266 static int create_sess(struct rtrs_srv *rtrs)
267 {
268 	struct rnbd_srv_session *srv_sess;
269 	char sessname[NAME_MAX];
270 	int err;
271 
272 	err = rtrs_srv_get_sess_name(rtrs, sessname, sizeof(sessname));
273 	if (err) {
274 		pr_err("rtrs_srv_get_sess_name(%s): %d\n", sessname, 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 	err = bioset_init(&srv_sess->sess_bio_set, srv_sess->queue_depth,
284 			  offsetof(struct rnbd_dev_blk_io, bio),
285 			  BIOSET_NEED_BVECS);
286 	if (err) {
287 		pr_err("Allocating srv_session for session %s failed\n",
288 		       sessname);
289 		kfree(srv_sess);
290 		return err;
291 	}
292 
293 	xa_init_flags(&srv_sess->index_idr, XA_FLAGS_ALLOC);
294 	INIT_LIST_HEAD(&srv_sess->sess_dev_list);
295 	mutex_init(&srv_sess->lock);
296 	mutex_lock(&sess_lock);
297 	list_add(&srv_sess->list, &sess_list);
298 	mutex_unlock(&sess_lock);
299 
300 	srv_sess->rtrs = rtrs;
301 	strlcpy(srv_sess->sessname, sessname, sizeof(srv_sess->sessname));
302 
303 	rtrs_srv_set_sess_priv(rtrs, srv_sess);
304 
305 	return 0;
306 }
307 
rnbd_srv_link_ev(struct rtrs_srv * rtrs,enum rtrs_srv_link_ev ev,void * priv)308 static int rnbd_srv_link_ev(struct rtrs_srv *rtrs,
309 			     enum rtrs_srv_link_ev ev, void *priv)
310 {
311 	struct rnbd_srv_session *srv_sess = priv;
312 
313 	switch (ev) {
314 	case RTRS_SRV_LINK_EV_CONNECTED:
315 		return create_sess(rtrs);
316 
317 	case RTRS_SRV_LINK_EV_DISCONNECTED:
318 		if (WARN_ON_ONCE(!srv_sess))
319 			return -EINVAL;
320 
321 		destroy_sess(srv_sess);
322 		return 0;
323 
324 	default:
325 		pr_warn("Received unknown RTRS session event %d from session %s\n",
326 			ev, srv_sess->sessname);
327 		return -EINVAL;
328 	}
329 }
330 
process_msg_close(struct rtrs_srv * rtrs,struct rnbd_srv_session * srv_sess,void * data,size_t datalen,const void * usr,size_t usrlen)331 static int process_msg_close(struct rtrs_srv *rtrs,
332 			     struct rnbd_srv_session *srv_sess,
333 			     void *data, size_t datalen, const void *usr,
334 			     size_t usrlen)
335 {
336 	const struct rnbd_msg_close *close_msg = usr;
337 	struct rnbd_srv_sess_dev *sess_dev;
338 
339 	sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id),
340 				      srv_sess);
341 	if (IS_ERR(sess_dev))
342 		return 0;
343 
344 	rnbd_put_sess_dev(sess_dev);
345 	mutex_lock(&srv_sess->lock);
346 	rnbd_srv_destroy_dev_session_sysfs(sess_dev);
347 	mutex_unlock(&srv_sess->lock);
348 	return 0;
349 }
350 
351 static int process_msg_open(struct rtrs_srv *rtrs,
352 			    struct rnbd_srv_session *srv_sess,
353 			    const void *msg, size_t len,
354 			    void *data, size_t datalen);
355 
356 static int process_msg_sess_info(struct rtrs_srv *rtrs,
357 				 struct rnbd_srv_session *srv_sess,
358 				 const void *msg, size_t len,
359 				 void *data, size_t datalen);
360 
rnbd_srv_rdma_ev(struct rtrs_srv * rtrs,void * priv,struct rtrs_srv_op * id,int dir,void * data,size_t datalen,const void * usr,size_t usrlen)361 static int rnbd_srv_rdma_ev(struct rtrs_srv *rtrs, void *priv,
362 			     struct rtrs_srv_op *id, int dir,
363 			     void *data, size_t datalen, const void *usr,
364 			     size_t usrlen)
365 {
366 	struct rnbd_srv_session *srv_sess = priv;
367 	const struct rnbd_msg_hdr *hdr = usr;
368 	int ret = 0;
369 	u16 type;
370 
371 	if (WARN_ON_ONCE(!srv_sess))
372 		return -ENODEV;
373 
374 	type = le16_to_cpu(hdr->type);
375 
376 	switch (type) {
377 	case RNBD_MSG_IO:
378 		return process_rdma(rtrs, srv_sess, id, data, datalen, usr,
379 				    usrlen);
380 	case RNBD_MSG_CLOSE:
381 		ret = process_msg_close(rtrs, srv_sess, data, datalen,
382 					usr, usrlen);
383 		break;
384 	case RNBD_MSG_OPEN:
385 		ret = process_msg_open(rtrs, srv_sess, usr, usrlen,
386 				       data, datalen);
387 		break;
388 	case RNBD_MSG_SESS_INFO:
389 		ret = process_msg_sess_info(rtrs, srv_sess, usr, usrlen,
390 					    data, datalen);
391 		break;
392 	default:
393 		pr_warn("Received unexpected message type %d with dir %d from session %s\n",
394 			type, dir, srv_sess->sessname);
395 		return -EINVAL;
396 	}
397 
398 	rtrs_srv_resp_rdma(id, ret);
399 	return 0;
400 }
401 
402 static struct rnbd_srv_sess_dev
rnbd_sess_dev_alloc(struct rnbd_srv_session * srv_sess)403 *rnbd_sess_dev_alloc(struct rnbd_srv_session *srv_sess)
404 {
405 	struct rnbd_srv_sess_dev *sess_dev;
406 	int error;
407 
408 	sess_dev = kzalloc(sizeof(*sess_dev), GFP_KERNEL);
409 	if (!sess_dev)
410 		return ERR_PTR(-ENOMEM);
411 
412 	error = xa_alloc(&srv_sess->index_idr, &sess_dev->device_id, sess_dev,
413 			 xa_limit_32b, GFP_NOWAIT);
414 	if (error < 0) {
415 		pr_warn("Allocating idr failed, err: %d\n", error);
416 		kfree(sess_dev);
417 		return ERR_PTR(error);
418 	}
419 
420 	return sess_dev;
421 }
422 
rnbd_srv_init_srv_dev(const char * id)423 static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(const char *id)
424 {
425 	struct rnbd_srv_dev *dev;
426 
427 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
428 	if (!dev)
429 		return ERR_PTR(-ENOMEM);
430 
431 	strlcpy(dev->id, id, sizeof(dev->id));
432 	kref_init(&dev->kref);
433 	INIT_LIST_HEAD(&dev->sess_dev_list);
434 	mutex_init(&dev->lock);
435 
436 	return dev;
437 }
438 
439 static struct rnbd_srv_dev *
rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev * new_dev)440 rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev *new_dev)
441 {
442 	struct rnbd_srv_dev *dev;
443 
444 	spin_lock(&dev_lock);
445 	list_for_each_entry(dev, &dev_list, list) {
446 		if (!strncmp(dev->id, new_dev->id, sizeof(dev->id))) {
447 			if (!kref_get_unless_zero(&dev->kref))
448 				/*
449 				 * We lost the race, device is almost dead.
450 				 *  Continue traversing to find a valid one.
451 				 */
452 				continue;
453 			spin_unlock(&dev_lock);
454 			return dev;
455 		}
456 	}
457 	list_add(&new_dev->list, &dev_list);
458 	spin_unlock(&dev_lock);
459 
460 	return new_dev;
461 }
462 
rnbd_srv_check_update_open_perm(struct rnbd_srv_dev * srv_dev,struct rnbd_srv_session * srv_sess,enum rnbd_access_mode access_mode)463 static int rnbd_srv_check_update_open_perm(struct rnbd_srv_dev *srv_dev,
464 					    struct rnbd_srv_session *srv_sess,
465 					    enum rnbd_access_mode access_mode)
466 {
467 	int ret = -EPERM;
468 
469 	mutex_lock(&srv_dev->lock);
470 
471 	switch (access_mode) {
472 	case RNBD_ACCESS_RO:
473 		ret = 0;
474 		break;
475 	case RNBD_ACCESS_RW:
476 		if (srv_dev->open_write_cnt == 0)  {
477 			srv_dev->open_write_cnt++;
478 			ret = 0;
479 		} else {
480 			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",
481 			       srv_dev->id, srv_sess->sessname,
482 			       srv_dev->open_write_cnt,
483 			       rnbd_access_mode_str(access_mode));
484 		}
485 		break;
486 	case RNBD_ACCESS_MIGRATION:
487 		if (srv_dev->open_write_cnt < 2) {
488 			srv_dev->open_write_cnt++;
489 			ret = 0;
490 		} else {
491 			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",
492 			       srv_dev->id, srv_sess->sessname,
493 			       srv_dev->open_write_cnt,
494 			       rnbd_access_mode_str(access_mode));
495 		}
496 		break;
497 	default:
498 		pr_err("Received mapping request for device '%s' on session %s with invalid access mode: %d\n",
499 		       srv_dev->id, srv_sess->sessname, access_mode);
500 		ret = -EINVAL;
501 	}
502 
503 	mutex_unlock(&srv_dev->lock);
504 
505 	return ret;
506 }
507 
508 static struct rnbd_srv_dev *
rnbd_srv_get_or_create_srv_dev(struct rnbd_dev * rnbd_dev,struct rnbd_srv_session * srv_sess,enum rnbd_access_mode access_mode)509 rnbd_srv_get_or_create_srv_dev(struct rnbd_dev *rnbd_dev,
510 				struct rnbd_srv_session *srv_sess,
511 				enum rnbd_access_mode access_mode)
512 {
513 	int ret;
514 	struct rnbd_srv_dev *new_dev, *dev;
515 
516 	new_dev = rnbd_srv_init_srv_dev(rnbd_dev->name);
517 	if (IS_ERR(new_dev))
518 		return new_dev;
519 
520 	dev = rnbd_srv_find_or_add_srv_dev(new_dev);
521 	if (dev != new_dev)
522 		kfree(new_dev);
523 
524 	ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode);
525 	if (ret) {
526 		rnbd_put_srv_dev(dev);
527 		return ERR_PTR(ret);
528 	}
529 
530 	return dev;
531 }
532 
rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp * rsp,struct rnbd_srv_sess_dev * sess_dev)533 static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp,
534 					struct rnbd_srv_sess_dev *sess_dev)
535 {
536 	struct rnbd_dev *rnbd_dev = sess_dev->rnbd_dev;
537 
538 	rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP);
539 	rsp->device_id =
540 		cpu_to_le32(sess_dev->device_id);
541 	rsp->nsectors =
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));
547 	rsp->max_segments =
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 =
552 		cpu_to_le32(bdev_write_same(rnbd_dev->bdev));
553 	rsp->max_discard_sectors =
554 		cpu_to_le32(rnbd_dev_get_max_discard_sects(rnbd_dev));
555 	rsp->discard_granularity =
556 		cpu_to_le32(rnbd_dev_get_discard_granularity(rnbd_dev));
557 	rsp->discard_alignment =
558 		cpu_to_le32(rnbd_dev_get_discard_alignment(rnbd_dev));
559 	rsp->secure_discard =
560 		cpu_to_le16(rnbd_dev_get_secure_discard(rnbd_dev));
561 	rsp->rotational =
562 		!blk_queue_nonrot(bdev_get_queue(rnbd_dev->bdev));
563 }
564 
565 static struct rnbd_srv_sess_dev *
rnbd_srv_create_set_sess_dev(struct rnbd_srv_session * srv_sess,const struct rnbd_msg_open * open_msg,struct rnbd_dev * rnbd_dev,fmode_t open_flags,struct rnbd_srv_dev * srv_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 	strlcpy(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 
rnbd_srv_get_full_path(struct rnbd_srv_session * srv_sess,const char * dev_name)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 	int len;
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 		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 	} else {
611 		len = snprintf(full_path, PATH_MAX, "%s/%s",
612 			       dev_search_path, dev_name);
613 	}
614 	if (len >= PATH_MAX) {
615 		pr_err("Too long path: %s, %s, %s\n",
616 		       dev_search_path, srv_sess->sessname, dev_name);
617 		kfree(full_path);
618 		return ERR_PTR(-EINVAL);
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 
process_msg_sess_info(struct rtrs_srv * rtrs,struct rnbd_srv_session * srv_sess,const void * msg,size_t len,void * data,size_t datalen)639 static int process_msg_sess_info(struct rtrs_srv *rtrs,
640 				 struct rnbd_srv_session *srv_sess,
641 				 const void *msg, size_t len,
642 				 void *data, size_t datalen)
643 {
644 	const struct rnbd_msg_sess_info *sess_info_msg = msg;
645 	struct rnbd_msg_sess_info_rsp *rsp = data;
646 
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);
651 
652 	rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP);
653 	rsp->ver = srv_sess->ver;
654 
655 	return 0;
656 }
657 
658 /**
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.
662  *
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.
665  */
666 static struct rnbd_srv_sess_dev *
find_srv_sess_dev(struct rnbd_srv_session * srv_sess,const char * dev_name)667 find_srv_sess_dev(struct rnbd_srv_session *srv_sess, const char *dev_name)
668 {
669 	struct rnbd_srv_sess_dev *sess_dev;
670 
671 	if (list_empty(&srv_sess->sess_dev_list))
672 		return NULL;
673 
674 	list_for_each_entry(sess_dev, &srv_sess->sess_dev_list, sess_list)
675 		if (!strcmp(sess_dev->pathname, dev_name))
676 			return sess_dev;
677 
678 	return NULL;
679 }
680 
process_msg_open(struct rtrs_srv * rtrs,struct rnbd_srv_session * srv_sess,const void * msg,size_t len,void * data,size_t datalen)681 static int process_msg_open(struct rtrs_srv *rtrs,
682 			    struct rnbd_srv_session *srv_sess,
683 			    const void *msg, size_t len,
684 			    void *data, size_t datalen)
685 {
686 	int ret;
687 	struct rnbd_srv_dev *srv_dev;
688 	struct rnbd_srv_sess_dev *srv_sess_dev;
689 	const struct rnbd_msg_open *open_msg = msg;
690 	fmode_t open_flags;
691 	char *full_path;
692 	struct rnbd_dev *rnbd_dev;
693 	struct rnbd_msg_open_rsp *rsp = data;
694 
695 	pr_debug("Open message received: session='%s' path='%s' access_mode=%d\n",
696 		 srv_sess->sessname, open_msg->dev_name,
697 		 open_msg->access_mode);
698 	open_flags = FMODE_READ;
699 	if (open_msg->access_mode != RNBD_ACCESS_RO)
700 		open_flags |= FMODE_WRITE;
701 
702 	mutex_lock(&srv_sess->lock);
703 
704 	srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name);
705 	if (srv_sess_dev)
706 		goto fill_response;
707 
708 	if ((strlen(dev_search_path) + strlen(open_msg->dev_name))
709 	    >= PATH_MAX) {
710 		pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n",
711 		       srv_sess->sessname, dev_search_path, open_msg->dev_name,
712 		       PATH_MAX);
713 		ret = -EINVAL;
714 		goto reject;
715 	}
716 	if (strstr(open_msg->dev_name, "..")) {
717 		pr_err("Opening device for session %s failed, device path %s contains relative path ..\n",
718 		       srv_sess->sessname, open_msg->dev_name);
719 		ret = -EINVAL;
720 		goto reject;
721 	}
722 	full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name);
723 	if (IS_ERR(full_path)) {
724 		ret = PTR_ERR(full_path);
725 		pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %d\n",
726 		       open_msg->dev_name, srv_sess->sessname, ret);
727 		goto reject;
728 	}
729 
730 	rnbd_dev = rnbd_dev_open(full_path, open_flags,
731 				 &srv_sess->sess_bio_set);
732 	if (IS_ERR(rnbd_dev)) {
733 		pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %ld\n",
734 		       full_path, srv_sess->sessname, PTR_ERR(rnbd_dev));
735 		ret = PTR_ERR(rnbd_dev);
736 		goto free_path;
737 	}
738 
739 	srv_dev = rnbd_srv_get_or_create_srv_dev(rnbd_dev, srv_sess,
740 						  open_msg->access_mode);
741 	if (IS_ERR(srv_dev)) {
742 		pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %ld\n",
743 		       full_path, srv_sess->sessname, PTR_ERR(srv_dev));
744 		ret = PTR_ERR(srv_dev);
745 		goto rnbd_dev_close;
746 	}
747 
748 	srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg,
749 						     rnbd_dev, open_flags,
750 						     srv_dev);
751 	if (IS_ERR(srv_sess_dev)) {
752 		pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %ld\n",
753 		       full_path, srv_sess->sessname, PTR_ERR(srv_sess_dev));
754 		ret = PTR_ERR(srv_sess_dev);
755 		goto srv_dev_put;
756 	}
757 
758 	/* Create the srv_dev sysfs files if they haven't been created yet. The
759 	 * reason to delay the creation is not to create the sysfs files before
760 	 * we are sure the device can be opened.
761 	 */
762 	mutex_lock(&srv_dev->lock);
763 	if (!srv_dev->dev_kobj.state_in_sysfs) {
764 		ret = rnbd_srv_create_dev_sysfs(srv_dev, rnbd_dev->bdev,
765 						 rnbd_dev->name);
766 		if (ret) {
767 			mutex_unlock(&srv_dev->lock);
768 			rnbd_srv_err(srv_sess_dev,
769 				      "Opening device failed, failed to create device sysfs files, err: %d\n",
770 				      ret);
771 			goto free_srv_sess_dev;
772 		}
773 	}
774 
775 	ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
776 	if (ret) {
777 		mutex_unlock(&srv_dev->lock);
778 		rnbd_srv_err(srv_sess_dev,
779 			      "Opening device failed, failed to create dev client sysfs files, err: %d\n",
780 			      ret);
781 		goto free_srv_sess_dev;
782 	}
783 
784 	list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
785 	mutex_unlock(&srv_dev->lock);
786 
787 	list_add(&srv_sess_dev->sess_list, &srv_sess->sess_dev_list);
788 
789 	rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id);
790 
791 	kfree(full_path);
792 
793 fill_response:
794 	rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev);
795 	mutex_unlock(&srv_sess->lock);
796 	return 0;
797 
798 free_srv_sess_dev:
799 	xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id);
800 	synchronize_rcu();
801 	kfree(srv_sess_dev);
802 srv_dev_put:
803 	if (open_msg->access_mode != RNBD_ACCESS_RO) {
804 		mutex_lock(&srv_dev->lock);
805 		srv_dev->open_write_cnt--;
806 		mutex_unlock(&srv_dev->lock);
807 	}
808 	rnbd_put_srv_dev(srv_dev);
809 rnbd_dev_close:
810 	rnbd_dev_close(rnbd_dev);
811 free_path:
812 	kfree(full_path);
813 reject:
814 	mutex_unlock(&srv_sess->lock);
815 	return ret;
816 }
817 
818 static struct rtrs_srv_ctx *rtrs_ctx;
819 
820 static struct rtrs_srv_ops rtrs_ops;
rnbd_srv_init_module(void)821 static int __init rnbd_srv_init_module(void)
822 {
823 	int err;
824 
825 	BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4);
826 	BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36);
827 	BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36);
828 	BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264);
829 	BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8);
830 	BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56);
831 	rtrs_ops = (struct rtrs_srv_ops) {
832 		.rdma_ev = rnbd_srv_rdma_ev,
833 		.link_ev = rnbd_srv_link_ev,
834 	};
835 	rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr);
836 	if (IS_ERR(rtrs_ctx)) {
837 		err = PTR_ERR(rtrs_ctx);
838 		pr_err("rtrs_srv_open(), err: %d\n", err);
839 		return err;
840 	}
841 
842 	err = rnbd_srv_create_sysfs_files();
843 	if (err) {
844 		pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err);
845 		rtrs_srv_close(rtrs_ctx);
846 		return err;
847 	}
848 
849 	return 0;
850 }
851 
rnbd_srv_cleanup_module(void)852 static void __exit rnbd_srv_cleanup_module(void)
853 {
854 	rtrs_srv_close(rtrs_ctx);
855 	WARN_ON(!list_empty(&sess_list));
856 	rnbd_srv_destroy_sysfs_files();
857 }
858 
859 module_init(rnbd_srv_init_module);
860 module_exit(rnbd_srv_cleanup_module);
861