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-trace.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
87 static struct rnbd_srv_sess_dev *
rnbd_get_sess_dev(int dev_id,struct rnbd_srv_session * srv_sess)88 rnbd_get_sess_dev(int dev_id, struct rnbd_srv_session *srv_sess)
89 {
90 struct rnbd_srv_sess_dev *sess_dev;
91 int ret = 0;
92
93 rcu_read_lock();
94 sess_dev = xa_load(&srv_sess->index_idr, dev_id);
95 if (sess_dev)
96 ret = kref_get_unless_zero(&sess_dev->kref);
97 rcu_read_unlock();
98
99 if (!ret)
100 return ERR_PTR(-ENXIO);
101
102 return sess_dev;
103 }
104
rnbd_dev_bi_end_io(struct bio * bio)105 static void rnbd_dev_bi_end_io(struct bio *bio)
106 {
107 struct rnbd_io_private *rnbd_priv = bio->bi_private;
108 struct rnbd_srv_sess_dev *sess_dev = rnbd_priv->sess_dev;
109
110 rnbd_put_sess_dev(sess_dev);
111 rtrs_srv_resp_rdma(rnbd_priv->id, blk_status_to_errno(bio->bi_status));
112
113 kfree(rnbd_priv);
114 bio_put(bio);
115 }
116
process_rdma(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 rnbd_srv_session *srv_sess,
118 struct rtrs_srv_op *id, void *data, u32 datalen,
119 const void *usr, size_t usrlen)
120 {
121 const struct rnbd_msg_io *msg = usr;
122 struct rnbd_io_private *priv;
123 struct rnbd_srv_sess_dev *sess_dev;
124 u32 dev_id;
125 int err;
126 struct bio *bio;
127 short prio;
128
129 trace_process_rdma(srv_sess, msg, id, datalen, usrlen);
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: %pe\n",
140 srv_sess->sessname, dev_id, sess_dev);
141 err = -ENOTCONN;
142 goto err;
143 }
144
145 priv->sess_dev = sess_dev;
146 priv->id = id;
147
148 bio = bio_alloc(file_bdev(sess_dev->bdev_file), 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_rl(sess_dev, "Failed to map data to bio\n");
153 err = -EINVAL;
154 goto bio_put;
155 }
156
157 bio->bi_opf = rnbd_to_bio_flags(le32_to_cpu(msg->rw));
158 if (bio_has_data(bio) &&
159 bio->bi_iter.bi_size != le32_to_cpu(msg->bi_size)) {
160 rnbd_srv_err_rl(sess_dev, "Datalen mismatch: bio bi_size (%u), bi_size (%u)\n",
161 bio->bi_iter.bi_size, msg->bi_size);
162 err = -EINVAL;
163 goto bio_put;
164 }
165 bio->bi_end_io = rnbd_dev_bi_end_io;
166 bio->bi_private = priv;
167 bio->bi_iter.bi_sector = le64_to_cpu(msg->sector);
168 prio = srv_sess->ver < RNBD_PROTO_VER_MAJOR ||
169 usrlen < sizeof(*msg) ? 0 : le16_to_cpu(msg->prio);
170 bio_set_prio(bio, prio);
171
172 submit_bio(bio);
173
174 return 0;
175
176 bio_put:
177 bio_put(bio);
178 rnbd_put_sess_dev(sess_dev);
179 err:
180 kfree(priv);
181 return err;
182 }
183
destroy_device(struct kref * kref)184 static void destroy_device(struct kref *kref)
185 {
186 struct rnbd_srv_dev *dev = container_of(kref, struct rnbd_srv_dev, kref);
187
188 WARN_ONCE(!list_empty(&dev->sess_dev_list),
189 "Device %s is being destroyed but still in use!\n",
190 dev->name);
191
192 spin_lock(&dev_lock);
193 list_del(&dev->list);
194 spin_unlock(&dev_lock);
195
196 mutex_destroy(&dev->lock);
197 if (dev->dev_kobj.state_in_sysfs)
198 /*
199 * Destroy kobj only if it was really created.
200 */
201 rnbd_srv_destroy_dev_sysfs(dev);
202 else
203 kfree(dev);
204 }
205
rnbd_put_srv_dev(struct rnbd_srv_dev * dev)206 static void rnbd_put_srv_dev(struct rnbd_srv_dev *dev)
207 {
208 kref_put(&dev->kref, destroy_device);
209 }
210
rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev * sess_dev,bool keep_id)211 void rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev *sess_dev, bool keep_id)
212 {
213 DECLARE_COMPLETION_ONSTACK(dc);
214
215 if (keep_id)
216 /* free the resources for the id but don't */
217 /* allow to re-use the id itself because it */
218 /* is still used by the client */
219 xa_cmpxchg(&sess_dev->sess->index_idr, sess_dev->device_id,
220 sess_dev, NULL, 0);
221 else
222 xa_erase(&sess_dev->sess->index_idr, sess_dev->device_id);
223 synchronize_rcu();
224
225 sess_dev->destroy_comp = &dc;
226 rnbd_put_sess_dev(sess_dev);
227 wait_for_completion(&dc); /* wait for inflights to drop to zero */
228
229 fput(sess_dev->bdev_file);
230 mutex_lock(&sess_dev->dev->lock);
231 list_del(&sess_dev->dev_list);
232 if (!sess_dev->readonly)
233 sess_dev->dev->open_write_cnt--;
234 mutex_unlock(&sess_dev->dev->lock);
235
236 rnbd_put_srv_dev(sess_dev->dev);
237
238 rnbd_srv_info(sess_dev, "Device closed\n");
239 kfree(sess_dev);
240 }
241
destroy_sess(struct rnbd_srv_session * srv_sess)242 static void destroy_sess(struct rnbd_srv_session *srv_sess)
243 {
244 struct rnbd_srv_sess_dev *sess_dev;
245 unsigned long index;
246
247 if (xa_empty(&srv_sess->index_idr))
248 goto out;
249
250 trace_destroy_sess(srv_sess);
251
252 mutex_lock(&srv_sess->lock);
253 xa_for_each(&srv_sess->index_idr, index, sess_dev)
254 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
255 mutex_unlock(&srv_sess->lock);
256
257 out:
258 xa_destroy(&srv_sess->index_idr);
259
260 pr_info("RTRS Session %s disconnected\n", srv_sess->sessname);
261
262 mutex_lock(&sess_lock);
263 list_del(&srv_sess->list);
264 mutex_unlock(&sess_lock);
265
266 mutex_destroy(&srv_sess->lock);
267 kfree(srv_sess);
268 }
269
create_sess(struct rtrs_srv_sess * rtrs)270 static int create_sess(struct rtrs_srv_sess *rtrs)
271 {
272 struct rnbd_srv_session *srv_sess;
273 char pathname[NAME_MAX];
274 int err;
275
276 err = rtrs_srv_get_path_name(rtrs, pathname, sizeof(pathname));
277 if (err) {
278 pr_err("rtrs_srv_get_path_name(%s): %d\n", pathname, err);
279
280 return err;
281 }
282 srv_sess = kzalloc(sizeof(*srv_sess), GFP_KERNEL);
283 if (!srv_sess)
284 return -ENOMEM;
285
286 srv_sess->queue_depth = rtrs_srv_get_queue_depth(rtrs);
287 xa_init_flags(&srv_sess->index_idr, XA_FLAGS_ALLOC);
288 mutex_init(&srv_sess->lock);
289 mutex_lock(&sess_lock);
290 list_add(&srv_sess->list, &sess_list);
291 mutex_unlock(&sess_lock);
292
293 srv_sess->rtrs = rtrs;
294 strscpy(srv_sess->sessname, pathname, sizeof(srv_sess->sessname));
295
296 rtrs_srv_set_sess_priv(rtrs, srv_sess);
297
298 trace_create_sess(srv_sess);
299
300 return 0;
301 }
302
rnbd_srv_link_ev(struct rtrs_srv_sess * rtrs,enum rtrs_srv_link_ev ev,void * priv)303 static int rnbd_srv_link_ev(struct rtrs_srv_sess *rtrs,
304 enum rtrs_srv_link_ev ev, void *priv)
305 {
306 struct rnbd_srv_session *srv_sess = priv;
307
308 switch (ev) {
309 case RTRS_SRV_LINK_EV_CONNECTED:
310 return create_sess(rtrs);
311
312 case RTRS_SRV_LINK_EV_DISCONNECTED:
313 if (WARN_ON_ONCE(!srv_sess))
314 return -EINVAL;
315
316 destroy_sess(srv_sess);
317 return 0;
318
319 default:
320 pr_warn("Received unknown RTRS session event %d from session %s\n",
321 ev, srv_sess->sessname);
322 return -EINVAL;
323 }
324 }
325
rnbd_srv_sess_dev_force_close(struct rnbd_srv_sess_dev * sess_dev,struct kobj_attribute * attr)326 void rnbd_srv_sess_dev_force_close(struct rnbd_srv_sess_dev *sess_dev,
327 struct kobj_attribute *attr)
328 {
329 struct rnbd_srv_session *sess = sess_dev->sess;
330
331 /* It is already started to close by client's close message. */
332 if (!mutex_trylock(&sess->lock))
333 return;
334
335 sess_dev->keep_id = true;
336 /* first remove sysfs itself to avoid deadlock */
337 sysfs_remove_file_self(&sess_dev->kobj, &attr->attr);
338 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
339 mutex_unlock(&sess->lock);
340 }
341
process_msg_close(struct rnbd_srv_session * srv_sess,void * data,size_t datalen,const void * usr,size_t usrlen)342 static void process_msg_close(struct rnbd_srv_session *srv_sess,
343 void *data, size_t datalen, const void *usr,
344 size_t usrlen)
345 {
346 const struct rnbd_msg_close *close_msg = usr;
347 struct rnbd_srv_sess_dev *sess_dev;
348
349 trace_process_msg_close(srv_sess, close_msg);
350
351 sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id),
352 srv_sess);
353 if (IS_ERR(sess_dev))
354 return;
355
356 rnbd_put_sess_dev(sess_dev);
357 mutex_lock(&srv_sess->lock);
358 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
359 mutex_unlock(&srv_sess->lock);
360 }
361
362 static int process_msg_open(struct rnbd_srv_session *srv_sess,
363 const void *msg, size_t len,
364 void *data, size_t datalen);
365
366 static void process_msg_sess_info(struct rnbd_srv_session *srv_sess,
367 const void *msg, size_t len,
368 void *data, size_t datalen);
369
rnbd_srv_rdma_ev(void * priv,struct rtrs_srv_op * id,void * data,size_t datalen,const void * usr,size_t usrlen)370 static int rnbd_srv_rdma_ev(void *priv, struct rtrs_srv_op *id,
371 void *data, size_t datalen,
372 const void *usr, size_t usrlen)
373 {
374 struct rnbd_srv_session *srv_sess = priv;
375 const struct rnbd_msg_hdr *hdr = usr;
376 int ret = 0;
377 u16 type;
378
379 if (WARN_ON_ONCE(!srv_sess))
380 return -ENODEV;
381
382 type = le16_to_cpu(hdr->type);
383
384 switch (type) {
385 case RNBD_MSG_IO:
386 return process_rdma(srv_sess, id, data, datalen, usr, usrlen);
387 case RNBD_MSG_CLOSE:
388 process_msg_close(srv_sess, data, datalen, usr, usrlen);
389 break;
390 case RNBD_MSG_OPEN:
391 ret = process_msg_open(srv_sess, usr, usrlen, data, datalen);
392 break;
393 case RNBD_MSG_SESS_INFO:
394 process_msg_sess_info(srv_sess, usr, usrlen, data, datalen);
395 break;
396 default:
397 pr_warn("Received unexpected message type %d from session %s\n",
398 type, srv_sess->sessname);
399 return -EINVAL;
400 }
401
402 /*
403 * Since ret is passed to rtrs to handle the failure case, we
404 * just return 0 at the end otherwise callers in rtrs would call
405 * send_io_resp_imm again to print redundant err message.
406 */
407 rtrs_srv_resp_rdma(id, ret);
408 return 0;
409 }
410
411 static struct rnbd_srv_sess_dev
rnbd_sess_dev_alloc(struct rnbd_srv_session * srv_sess)412 *rnbd_sess_dev_alloc(struct rnbd_srv_session *srv_sess)
413 {
414 struct rnbd_srv_sess_dev *sess_dev;
415 int error;
416
417 sess_dev = kzalloc(sizeof(*sess_dev), GFP_KERNEL);
418 if (!sess_dev)
419 return ERR_PTR(-ENOMEM);
420
421 error = xa_alloc(&srv_sess->index_idr, &sess_dev->device_id, sess_dev,
422 xa_limit_32b, GFP_NOWAIT);
423 if (error < 0) {
424 pr_warn("Allocating idr failed, err: %d\n", error);
425 kfree(sess_dev);
426 return ERR_PTR(error);
427 }
428
429 return sess_dev;
430 }
431
rnbd_srv_init_srv_dev(struct block_device * bdev)432 static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(struct block_device *bdev)
433 {
434 struct rnbd_srv_dev *dev;
435
436 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
437 if (!dev)
438 return ERR_PTR(-ENOMEM);
439
440 snprintf(dev->name, sizeof(dev->name), "%pg", bdev);
441 kref_init(&dev->kref);
442 INIT_LIST_HEAD(&dev->sess_dev_list);
443 mutex_init(&dev->lock);
444
445 return dev;
446 }
447
448 static struct rnbd_srv_dev *
rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev * new_dev)449 rnbd_srv_find_or_add_srv_dev(struct rnbd_srv_dev *new_dev)
450 {
451 struct rnbd_srv_dev *dev;
452
453 spin_lock(&dev_lock);
454 list_for_each_entry(dev, &dev_list, list) {
455 if (!strncmp(dev->name, new_dev->name, sizeof(dev->name))) {
456 if (!kref_get_unless_zero(&dev->kref))
457 /*
458 * We lost the race, device is almost dead.
459 * Continue traversing to find a valid one.
460 */
461 continue;
462 spin_unlock(&dev_lock);
463 return dev;
464 }
465 }
466 list_add(&new_dev->list, &dev_list);
467 spin_unlock(&dev_lock);
468
469 return new_dev;
470 }
471
rnbd_srv_check_update_open_perm(struct rnbd_srv_dev * srv_dev,struct rnbd_srv_session * srv_sess,enum rnbd_access_mode access_mode)472 static int rnbd_srv_check_update_open_perm(struct rnbd_srv_dev *srv_dev,
473 struct rnbd_srv_session *srv_sess,
474 enum rnbd_access_mode access_mode)
475 {
476 int ret = 0;
477
478 mutex_lock(&srv_dev->lock);
479
480 switch (access_mode) {
481 case RNBD_ACCESS_RO:
482 break;
483 case RNBD_ACCESS_RW:
484 if (srv_dev->open_write_cnt == 0) {
485 srv_dev->open_write_cnt++;
486 } else {
487 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",
488 srv_dev->name, srv_sess->sessname,
489 srv_dev->open_write_cnt,
490 rnbd_access_modes[access_mode].str);
491 ret = -EPERM;
492 }
493 break;
494 case RNBD_ACCESS_MIGRATION:
495 if (srv_dev->open_write_cnt < 2) {
496 srv_dev->open_write_cnt++;
497 } else {
498 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",
499 srv_dev->name, srv_sess->sessname,
500 srv_dev->open_write_cnt,
501 rnbd_access_modes[access_mode].str);
502 ret = -EPERM;
503 }
504 break;
505 default:
506 pr_err("Received mapping request for device '%s' on session %s with invalid access mode: %d\n",
507 srv_dev->name, srv_sess->sessname, access_mode);
508 ret = -EINVAL;
509 }
510
511 mutex_unlock(&srv_dev->lock);
512
513 return ret;
514 }
515
516 static struct rnbd_srv_dev *
rnbd_srv_get_or_create_srv_dev(struct block_device * bdev,struct rnbd_srv_session * srv_sess,enum rnbd_access_mode access_mode)517 rnbd_srv_get_or_create_srv_dev(struct block_device *bdev,
518 struct rnbd_srv_session *srv_sess,
519 enum rnbd_access_mode access_mode)
520 {
521 int ret;
522 struct rnbd_srv_dev *new_dev, *dev;
523
524 new_dev = rnbd_srv_init_srv_dev(bdev);
525 if (IS_ERR(new_dev))
526 return new_dev;
527
528 dev = rnbd_srv_find_or_add_srv_dev(new_dev);
529 if (dev != new_dev)
530 kfree(new_dev);
531
532 ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode);
533 if (ret) {
534 rnbd_put_srv_dev(dev);
535 return ERR_PTR(ret);
536 }
537
538 return dev;
539 }
540
rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp * rsp,struct rnbd_srv_sess_dev * sess_dev)541 static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp,
542 struct rnbd_srv_sess_dev *sess_dev)
543 {
544 struct block_device *bdev = file_bdev(sess_dev->bdev_file);
545
546 rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP);
547 rsp->device_id = cpu_to_le32(sess_dev->device_id);
548 rsp->nsectors = cpu_to_le64(bdev_nr_sectors(bdev));
549 rsp->logical_block_size = cpu_to_le16(bdev_logical_block_size(bdev));
550 rsp->physical_block_size = cpu_to_le16(bdev_physical_block_size(bdev));
551 rsp->max_segments = cpu_to_le16(bdev_max_segments(bdev));
552 rsp->max_hw_sectors =
553 cpu_to_le32(queue_max_hw_sectors(bdev_get_queue(bdev)));
554 rsp->max_write_zeroes_sectors =
555 cpu_to_le32(bdev_write_zeroes_sectors(bdev));
556 rsp->max_discard_sectors = cpu_to_le32(bdev_max_discard_sectors(bdev));
557 rsp->discard_granularity = cpu_to_le32(bdev_discard_granularity(bdev));
558 rsp->discard_alignment = cpu_to_le32(bdev_discard_alignment(bdev));
559 rsp->secure_discard = cpu_to_le16(bdev_max_secure_erase_sectors(bdev));
560 rsp->cache_policy = 0;
561 if (bdev_write_cache(bdev))
562 rsp->cache_policy |= RNBD_WRITEBACK;
563 if (bdev_fua(bdev))
564 rsp->cache_policy |= RNBD_FUA;
565 }
566
567 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 file * bdev_file,bool readonly,struct rnbd_srv_dev * srv_dev)568 rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess,
569 const struct rnbd_msg_open *open_msg,
570 struct file *bdev_file, bool readonly,
571 struct rnbd_srv_dev *srv_dev)
572 {
573 struct rnbd_srv_sess_dev *sdev = rnbd_sess_dev_alloc(srv_sess);
574
575 if (IS_ERR(sdev))
576 return sdev;
577
578 kref_init(&sdev->kref);
579
580 strscpy(sdev->pathname, open_msg->dev_name, sizeof(sdev->pathname));
581
582 sdev->bdev_file = bdev_file;
583 sdev->sess = srv_sess;
584 sdev->dev = srv_dev;
585 sdev->readonly = readonly;
586 sdev->access_mode = open_msg->access_mode;
587
588 return sdev;
589 }
590
rnbd_srv_get_full_path(struct rnbd_srv_session * srv_sess,const char * dev_name)591 static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess,
592 const char *dev_name)
593 {
594 char *full_path;
595 char *a, *b;
596 int len;
597
598 full_path = kmalloc(PATH_MAX, GFP_KERNEL);
599 if (!full_path)
600 return ERR_PTR(-ENOMEM);
601
602 /*
603 * Replace %SESSNAME% with a real session name in order to
604 * create device namespace.
605 */
606 a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path));
607 if (a) {
608 len = a - dev_search_path;
609
610 len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len,
611 dev_search_path, srv_sess->sessname, dev_name);
612 } else {
613 len = snprintf(full_path, PATH_MAX, "%s/%s",
614 dev_search_path, dev_name);
615 }
616 if (len >= PATH_MAX) {
617 pr_err("Too long path: %s, %s, %s\n",
618 dev_search_path, srv_sess->sessname, dev_name);
619 kfree(full_path);
620 return ERR_PTR(-EINVAL);
621 }
622
623 /* eliminitate duplicated slashes */
624 a = strchr(full_path, '/');
625 b = a;
626 while (*b != '\0') {
627 if (*b == '/' && *a == '/') {
628 b++;
629 } else {
630 a++;
631 *a = *b;
632 b++;
633 }
634 }
635 a++;
636 *a = '\0';
637
638 return full_path;
639 }
640
process_msg_sess_info(struct rnbd_srv_session * srv_sess,const void * msg,size_t len,void * data,size_t datalen)641 static void process_msg_sess_info(struct rnbd_srv_session *srv_sess,
642 const void *msg, size_t len,
643 void *data, size_t datalen)
644 {
645 const struct rnbd_msg_sess_info *sess_info_msg = msg;
646 struct rnbd_msg_sess_info_rsp *rsp = data;
647
648 srv_sess->ver = min_t(u8, sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
649
650 trace_process_msg_sess_info(srv_sess, sess_info_msg);
651
652 rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP);
653 rsp->ver = srv_sess->ver;
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 *
find_srv_sess_dev(struct rnbd_srv_session * srv_sess,const char * dev_name)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
process_msg_open(struct rnbd_srv_session * srv_sess,const void * msg,size_t len,void * data,size_t datalen)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 struct file *bdev_file;
689 blk_mode_t open_flags = BLK_OPEN_READ;
690 char *full_path;
691 struct rnbd_msg_open_rsp *rsp = data;
692
693 trace_process_msg_open(srv_sess, open_msg);
694
695 if (open_msg->access_mode != RNBD_ACCESS_RO)
696 open_flags |= BLK_OPEN_WRITE;
697
698 mutex_lock(&srv_sess->lock);
699
700 srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name);
701 if (srv_sess_dev)
702 goto fill_response;
703
704 if ((strlen(dev_search_path) + strlen(open_msg->dev_name))
705 >= PATH_MAX) {
706 pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n",
707 srv_sess->sessname, dev_search_path, open_msg->dev_name,
708 PATH_MAX);
709 ret = -EINVAL;
710 goto reject;
711 }
712 if (strstr(open_msg->dev_name, "..")) {
713 pr_err("Opening device for session %s failed, device path %s contains relative path ..\n",
714 srv_sess->sessname, open_msg->dev_name);
715 ret = -EINVAL;
716 goto reject;
717 }
718 full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name);
719 if (IS_ERR(full_path)) {
720 ret = PTR_ERR(full_path);
721 pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %pe\n",
722 open_msg->dev_name, srv_sess->sessname, full_path);
723 goto reject;
724 }
725
726 bdev_file = bdev_file_open_by_path(full_path, open_flags, NULL, NULL);
727 if (IS_ERR(bdev_file)) {
728 ret = PTR_ERR(bdev_file);
729 pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %pe\n",
730 full_path, srv_sess->sessname, bdev_file);
731 goto free_path;
732 }
733
734 srv_dev = rnbd_srv_get_or_create_srv_dev(file_bdev(bdev_file), srv_sess,
735 open_msg->access_mode);
736 if (IS_ERR(srv_dev)) {
737 pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %pe\n",
738 full_path, srv_sess->sessname, srv_dev);
739 ret = PTR_ERR(srv_dev);
740 goto blkdev_put;
741 }
742
743 srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg,
744 bdev_file,
745 open_msg->access_mode == RNBD_ACCESS_RO,
746 srv_dev);
747 if (IS_ERR(srv_sess_dev)) {
748 pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %pe\n",
749 full_path, srv_sess->sessname, srv_sess_dev);
750 ret = PTR_ERR(srv_sess_dev);
751 goto srv_dev_put;
752 }
753
754 /* Create the srv_dev sysfs files if they haven't been created yet. The
755 * reason to delay the creation is not to create the sysfs files before
756 * we are sure the device can be opened.
757 */
758 mutex_lock(&srv_dev->lock);
759 if (!srv_dev->dev_kobj.state_in_sysfs) {
760 ret = rnbd_srv_create_dev_sysfs(srv_dev, file_bdev(bdev_file));
761 if (ret) {
762 mutex_unlock(&srv_dev->lock);
763 rnbd_srv_err(srv_sess_dev,
764 "Opening device failed, failed to create device sysfs files, err: %d\n",
765 ret);
766 goto free_srv_sess_dev;
767 }
768 }
769
770 ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
771 if (ret) {
772 mutex_unlock(&srv_dev->lock);
773 rnbd_srv_err(srv_sess_dev,
774 "Opening device failed, failed to create dev client sysfs files, err: %d\n",
775 ret);
776 goto free_srv_sess_dev;
777 }
778
779 list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
780 mutex_unlock(&srv_dev->lock);
781
782 rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->name);
783
784 kfree(full_path);
785
786 fill_response:
787 rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev);
788 mutex_unlock(&srv_sess->lock);
789 return 0;
790
791 free_srv_sess_dev:
792 xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id);
793 synchronize_rcu();
794 kfree(srv_sess_dev);
795 srv_dev_put:
796 if (open_msg->access_mode != RNBD_ACCESS_RO) {
797 mutex_lock(&srv_dev->lock);
798 srv_dev->open_write_cnt--;
799 mutex_unlock(&srv_dev->lock);
800 }
801 rnbd_put_srv_dev(srv_dev);
802 blkdev_put:
803 fput(bdev_file);
804 free_path:
805 kfree(full_path);
806 reject:
807 mutex_unlock(&srv_sess->lock);
808 return ret;
809 }
810
811 static struct rtrs_srv_ctx *rtrs_ctx;
812
813 static struct rtrs_srv_ops rtrs_ops;
rnbd_srv_init_module(void)814 static int __init rnbd_srv_init_module(void)
815 {
816 int err = 0;
817
818 BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4);
819 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36);
820 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36);
821 BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264);
822 BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8);
823 BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56);
824 rtrs_ops = (struct rtrs_srv_ops) {
825 .rdma_ev = rnbd_srv_rdma_ev,
826 .link_ev = rnbd_srv_link_ev,
827 };
828 rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr);
829 if (IS_ERR(rtrs_ctx)) {
830 pr_err("rtrs_srv_open(), err: %pe\n", rtrs_ctx);
831 return PTR_ERR(rtrs_ctx);
832 }
833
834 err = rnbd_srv_create_sysfs_files();
835 if (err) {
836 pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err);
837 rtrs_srv_close(rtrs_ctx);
838 }
839
840 return err;
841 }
842
rnbd_srv_cleanup_module(void)843 static void __exit rnbd_srv_cleanup_module(void)
844 {
845 rtrs_srv_close(rtrs_ctx);
846 WARN_ON(!list_empty(&sess_list));
847 rnbd_srv_destroy_sysfs_files();
848 }
849
850 module_init(rnbd_srv_init_module);
851 module_exit(rnbd_srv_cleanup_module);
852