Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[platform/kernel/linux-starfive.git] / drivers / block / rnbd / rnbd-srv.c
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         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
240 static 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
253 out:
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
266 static 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
298 static 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
321 void 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
336 static 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
355 static int process_msg_open(struct rnbd_srv_session *srv_sess,
356                             const void *msg, size_t len,
357                             void *data, size_t datalen);
358
359 static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
360                                  const void *msg, size_t len,
361                                  void *data, size_t datalen);
362
363 static 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
401 static 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
422 static 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
438 static struct rnbd_srv_dev *
439 rnbd_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
462 static 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
507 static struct rnbd_srv_dev *
508 rnbd_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
532 static 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         struct request_queue *q = bdev_get_queue(rnbd_dev->bdev);
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 = 0;
552         rsp->max_discard_sectors =
553                 cpu_to_le32(rnbd_dev_get_max_discard_sects(rnbd_dev));
554         rsp->discard_granularity =
555                 cpu_to_le32(rnbd_dev_get_discard_granularity(rnbd_dev));
556         rsp->discard_alignment =
557                 cpu_to_le32(rnbd_dev_get_discard_alignment(rnbd_dev));
558         rsp->secure_discard =
559                 cpu_to_le16(rnbd_dev_get_secure_discard(rnbd_dev));
560         rsp->cache_policy = 0;
561         if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
562                 rsp->cache_policy |= RNBD_WRITEBACK;
563         if (blk_queue_fua(q))
564                 rsp->cache_policy |= RNBD_FUA;
565 }
566
567 static struct rnbd_srv_sess_dev *
568 rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess,
569                               const struct rnbd_msg_open *open_msg,
570                               struct rnbd_dev *rnbd_dev, fmode_t open_flags,
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->rnbd_dev          = rnbd_dev;
583         sdev->sess              = srv_sess;
584         sdev->dev               = srv_dev;
585         sdev->open_flags        = open_flags;
586         sdev->access_mode       = open_msg->access_mode;
587
588         return sdev;
589 }
590
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
597         full_path = kmalloc(PATH_MAX, GFP_KERNEL);
598         if (!full_path)
599                 return ERR_PTR(-ENOMEM);
600
601         /*
602          * Replace %SESSNAME% with a real session name in order to
603          * create device namespace.
604          */
605         a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path));
606         if (a) {
607                 int len = a - dev_search_path;
608
609                 len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len,
610                                dev_search_path, srv_sess->sessname, dev_name);
611                 if (len >= PATH_MAX) {
612                         pr_err("Too long path: %s, %s, %s\n",
613                                dev_search_path, srv_sess->sessname, dev_name);
614                         kfree(full_path);
615                         return ERR_PTR(-EINVAL);
616                 }
617         } else {
618                 snprintf(full_path, PATH_MAX, "%s/%s",
619                          dev_search_path, dev_name);
620         }
621
622         /* eliminitate duplicated slashes */
623         a = strchr(full_path, '/');
624         b = a;
625         while (*b != '\0') {
626                 if (*b == '/' && *a == '/') {
627                         b++;
628                 } else {
629                         a++;
630                         *a = *b;
631                         b++;
632                 }
633         }
634         a++;
635         *a = '\0';
636
637         return full_path;
638 }
639
640 static int process_msg_sess_info(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 *
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
681 static int process_msg_open(struct rnbd_srv_session *srv_sess,
682                             const void *msg, size_t len,
683                             void *data, size_t datalen)
684 {
685         int ret;
686         struct rnbd_srv_dev *srv_dev;
687         struct rnbd_srv_sess_dev *srv_sess_dev;
688         const struct rnbd_msg_open *open_msg = msg;
689         fmode_t open_flags;
690         char *full_path;
691         struct rnbd_dev *rnbd_dev;
692         struct rnbd_msg_open_rsp *rsp = data;
693
694         pr_debug("Open message received: session='%s' path='%s' access_mode=%d\n",
695                  srv_sess->sessname, open_msg->dev_name,
696                  open_msg->access_mode);
697         open_flags = FMODE_READ;
698         if (open_msg->access_mode != RNBD_ACCESS_RO)
699                 open_flags |= FMODE_WRITE;
700
701         mutex_lock(&srv_sess->lock);
702
703         srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name);
704         if (srv_sess_dev)
705                 goto fill_response;
706
707         if ((strlen(dev_search_path) + strlen(open_msg->dev_name))
708             >= PATH_MAX) {
709                 pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n",
710                        srv_sess->sessname, dev_search_path, open_msg->dev_name,
711                        PATH_MAX);
712                 ret = -EINVAL;
713                 goto reject;
714         }
715         if (strstr(open_msg->dev_name, "..")) {
716                 pr_err("Opening device for session %s failed, device path %s contains relative path ..\n",
717                        srv_sess->sessname, open_msg->dev_name);
718                 ret = -EINVAL;
719                 goto reject;
720         }
721         full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name);
722         if (IS_ERR(full_path)) {
723                 ret = PTR_ERR(full_path);
724                 pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %d\n",
725                        open_msg->dev_name, srv_sess->sessname, ret);
726                 goto reject;
727         }
728
729         rnbd_dev = rnbd_dev_open(full_path, open_flags);
730         if (IS_ERR(rnbd_dev)) {
731                 pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %ld\n",
732                        full_path, srv_sess->sessname, PTR_ERR(rnbd_dev));
733                 ret = PTR_ERR(rnbd_dev);
734                 goto free_path;
735         }
736
737         srv_dev = rnbd_srv_get_or_create_srv_dev(rnbd_dev, srv_sess,
738                                                   open_msg->access_mode);
739         if (IS_ERR(srv_dev)) {
740                 pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %ld\n",
741                        full_path, srv_sess->sessname, PTR_ERR(srv_dev));
742                 ret = PTR_ERR(srv_dev);
743                 goto rnbd_dev_close;
744         }
745
746         srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg,
747                                                      rnbd_dev, open_flags,
748                                                      srv_dev);
749         if (IS_ERR(srv_sess_dev)) {
750                 pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %ld\n",
751                        full_path, srv_sess->sessname, PTR_ERR(srv_sess_dev));
752                 ret = PTR_ERR(srv_sess_dev);
753                 goto srv_dev_put;
754         }
755
756         /* Create the srv_dev sysfs files if they haven't been created yet. The
757          * reason to delay the creation is not to create the sysfs files before
758          * we are sure the device can be opened.
759          */
760         mutex_lock(&srv_dev->lock);
761         if (!srv_dev->dev_kobj.state_in_sysfs) {
762                 ret = rnbd_srv_create_dev_sysfs(srv_dev, rnbd_dev->bdev,
763                                                  rnbd_dev->name);
764                 if (ret) {
765                         mutex_unlock(&srv_dev->lock);
766                         rnbd_srv_err(srv_sess_dev,
767                                       "Opening device failed, failed to create device sysfs files, err: %d\n",
768                                       ret);
769                         goto free_srv_sess_dev;
770                 }
771         }
772
773         ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
774         if (ret) {
775                 mutex_unlock(&srv_dev->lock);
776                 rnbd_srv_err(srv_sess_dev,
777                               "Opening device failed, failed to create dev client sysfs files, err: %d\n",
778                               ret);
779                 goto free_srv_sess_dev;
780         }
781
782         list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
783         mutex_unlock(&srv_dev->lock);
784
785         list_add(&srv_sess_dev->sess_list, &srv_sess->sess_dev_list);
786
787         rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id);
788
789         kfree(full_path);
790
791 fill_response:
792         rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev);
793         mutex_unlock(&srv_sess->lock);
794         return 0;
795
796 free_srv_sess_dev:
797         xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id);
798         synchronize_rcu();
799         kfree(srv_sess_dev);
800 srv_dev_put:
801         if (open_msg->access_mode != RNBD_ACCESS_RO) {
802                 mutex_lock(&srv_dev->lock);
803                 srv_dev->open_write_cnt--;
804                 mutex_unlock(&srv_dev->lock);
805         }
806         rnbd_put_srv_dev(srv_dev);
807 rnbd_dev_close:
808         rnbd_dev_close(rnbd_dev);
809 free_path:
810         kfree(full_path);
811 reject:
812         mutex_unlock(&srv_sess->lock);
813         return ret;
814 }
815
816 static struct rtrs_srv_ctx *rtrs_ctx;
817
818 static struct rtrs_srv_ops rtrs_ops;
819 static int __init rnbd_srv_init_module(void)
820 {
821         int err;
822
823         BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4);
824         BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36);
825         BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36);
826         BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264);
827         BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8);
828         BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56);
829         rtrs_ops = (struct rtrs_srv_ops) {
830                 .rdma_ev = rnbd_srv_rdma_ev,
831                 .link_ev = rnbd_srv_link_ev,
832         };
833         rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr);
834         if (IS_ERR(rtrs_ctx)) {
835                 err = PTR_ERR(rtrs_ctx);
836                 pr_err("rtrs_srv_open(), err: %d\n", err);
837                 return err;
838         }
839
840         err = rnbd_srv_create_sysfs_files();
841         if (err) {
842                 pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err);
843                 rtrs_srv_close(rtrs_ctx);
844                 return err;
845         }
846
847         return 0;
848 }
849
850 static void __exit rnbd_srv_cleanup_module(void)
851 {
852         rtrs_srv_close(rtrs_ctx);
853         WARN_ON(!list_empty(&sess_list));
854         rnbd_srv_destroy_sysfs_files();
855 }
856
857 module_init(rnbd_srv_init_module);
858 module_exit(rnbd_srv_cleanup_module);