blob: 1ee808fc600cf29f77595ea412e4e3f17621e369 [file] [log] [blame]
Jack Wang2de6c8d2020-05-11 15:51:26 +02001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * RDMA Network Block Driver
4 *
5 * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6 * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7 * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8 */
9#undef pr_fmt
10#define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
11
12#include <linux/module.h>
13#include <linux/blkdev.h>
14
15#include "rnbd-srv.h"
16#include "rnbd-srv-dev.h"
17
18MODULE_DESCRIPTION("RDMA Network Block Device Server");
19MODULE_LICENSE("GPL");
20
21static u16 port_nr = RTRS_PORT;
22
23module_param_named(port_nr, port_nr, ushort, 0444);
24MODULE_PARM_DESC(port_nr,
25 "The port number the server is listening on (default: "
26 __stringify(RTRS_PORT)")");
27
28#define DEFAULT_DEV_SEARCH_PATH "/"
29
30static char dev_search_path[PATH_MAX] = DEFAULT_DEV_SEARCH_PATH;
31
32static int dev_search_path_set(const char *val, const struct kernel_param *kp)
33{
34 const char *p = strrchr(val, '\n') ? : val + strlen(val);
35
36 if (strlen(val) >= sizeof(dev_search_path))
37 return -EINVAL;
38
39 snprintf(dev_search_path, sizeof(dev_search_path), "%.*s",
40 (int)(p - val), val);
41
42 pr_info("dev_search_path changed to '%s'\n", dev_search_path);
43
44 return 0;
45}
46
47static struct kparam_string dev_search_path_kparam_str = {
48 .maxlen = sizeof(dev_search_path),
49 .string = dev_search_path
50};
51
52static const struct kernel_param_ops dev_search_path_ops = {
53 .set = dev_search_path_set,
54 .get = param_get_string,
55};
56
57module_param_cb(dev_search_path, &dev_search_path_ops,
58 &dev_search_path_kparam_str, 0444);
59MODULE_PARM_DESC(dev_search_path,
60 "Sets the dev_search_path. When a device is mapped this path is prepended to the device path from the map device operation. If %SESSNAME% is specified in a path, then device will be searched in a session namespace. (default: "
61 DEFAULT_DEV_SEARCH_PATH ")");
62
63static DEFINE_MUTEX(sess_lock);
64static DEFINE_SPINLOCK(dev_lock);
65
66static LIST_HEAD(sess_list);
67static LIST_HEAD(dev_list);
68
69struct rnbd_io_private {
70 struct rtrs_srv_op *id;
71 struct rnbd_srv_sess_dev *sess_dev;
72};
73
74static void rnbd_sess_dev_release(struct kref *kref)
75{
76 struct rnbd_srv_sess_dev *sess_dev;
77
78 sess_dev = container_of(kref, struct rnbd_srv_sess_dev, kref);
79 complete(sess_dev->destroy_comp);
80}
81
82static inline void rnbd_put_sess_dev(struct rnbd_srv_sess_dev *sess_dev)
83{
84 kref_put(&sess_dev->kref, rnbd_sess_dev_release);
85}
86
87void rnbd_endio(void *priv, int error)
88{
89 struct rnbd_io_private *rnbd_priv = priv;
90 struct rnbd_srv_sess_dev *sess_dev = rnbd_priv->sess_dev;
91
92 rnbd_put_sess_dev(sess_dev);
93
94 rtrs_srv_resp_rdma(rnbd_priv->id, error);
95
96 kfree(priv);
97}
98
99static struct rnbd_srv_sess_dev *
100rnbd_get_sess_dev(int dev_id, struct rnbd_srv_session *srv_sess)
101{
102 struct rnbd_srv_sess_dev *sess_dev;
103 int ret = 0;
104
105 rcu_read_lock();
106 sess_dev = xa_load(&srv_sess->index_idr, dev_id);
Gioh Kim1e310162021-04-28 08:13:59 +0200107 if (sess_dev)
Jack Wang2de6c8d2020-05-11 15:51:26 +0200108 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
Gioh Kimc81cba82021-04-19 09:37:18 +0200117static int process_rdma(struct rnbd_srv_session *srv_sess,
Jack Wang2de6c8d2020-05-11 15:51:26 +0200118 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;
Guoqing Jiang735d77d2020-07-30 11:13:57 +0200126 struct rnbd_dev_blk_io *io;
127 struct bio *bio;
128 short prio;
Jack Wang2de6c8d2020-05-11 15:51:26 +0200129
130 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
131 if (!priv)
132 return -ENOMEM;
133
134 dev_id = le32_to_cpu(msg->device_id);
135
136 sess_dev = rnbd_get_sess_dev(dev_id, srv_sess);
137 if (IS_ERR(sess_dev)) {
138 pr_err_ratelimited("Got I/O request on session %s for unknown device id %d\n",
139 srv_sess->sessname, dev_id);
140 err = -ENOTCONN;
141 goto err;
142 }
143
144 priv->sess_dev = sess_dev;
145 priv->id = id;
146
Guoqing Jiang735d77d2020-07-30 11:13:57 +0200147 /* Generate bio with pages pointing to the rdma buffer */
148 bio = rnbd_bio_map_kern(data, sess_dev->rnbd_dev->ibd_bio_set, datalen, GFP_KERNEL);
149 if (IS_ERR(bio)) {
Nathan Chancellor17bc1032020-08-17 23:49:25 -0700150 err = PTR_ERR(bio);
151 rnbd_srv_err(sess_dev, "Failed to generate bio, err: %d\n", err);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200152 goto sess_dev_put;
153 }
154
Guoqing Jiang735d77d2020-07-30 11:13:57 +0200155 io = container_of(bio, struct rnbd_dev_blk_io, bio);
156 io->dev = sess_dev->rnbd_dev;
157 io->priv = priv;
158
159 bio->bi_end_io = rnbd_dev_bi_end_io;
160 bio->bi_private = io;
161 bio->bi_opf = rnbd_to_bio_flags(le32_to_cpu(msg->rw));
162 bio->bi_iter.bi_sector = le64_to_cpu(msg->sector);
163 bio->bi_iter.bi_size = le32_to_cpu(msg->bi_size);
164 prio = srv_sess->ver < RNBD_PROTO_VER_MAJOR ||
165 usrlen < sizeof(*msg) ? 0 : le16_to_cpu(msg->prio);
166 bio_set_prio(bio, prio);
167 bio_set_dev(bio, sess_dev->rnbd_dev->bdev);
168
169 submit_bio(bio);
170
Jack Wang2de6c8d2020-05-11 15:51:26 +0200171 return 0;
172
173sess_dev_put:
174 rnbd_put_sess_dev(sess_dev);
175err:
176 kfree(priv);
177 return err;
178}
179
Guoqing Jiangd16b5ac2021-04-19 09:37:10 +0200180static void destroy_device(struct kref *kref)
Jack Wang2de6c8d2020-05-11 15:51:26 +0200181{
Guoqing Jiangd16b5ac2021-04-19 09:37:10 +0200182 struct rnbd_srv_dev *dev = container_of(kref, struct rnbd_srv_dev, kref);
183
Jack Wang2de6c8d2020-05-11 15:51:26 +0200184 WARN_ONCE(!list_empty(&dev->sess_dev_list),
185 "Device %s is being destroyed but still in use!\n",
186 dev->id);
187
188 spin_lock(&dev_lock);
189 list_del(&dev->list);
190 spin_unlock(&dev_lock);
191
192 mutex_destroy(&dev->lock);
193 if (dev->dev_kobj.state_in_sysfs)
194 /*
195 * Destroy kobj only if it was really created.
196 */
197 rnbd_srv_destroy_dev_sysfs(dev);
198 else
199 kfree(dev);
200}
201
Jack Wang2de6c8d2020-05-11 15:51:26 +0200202static void rnbd_put_srv_dev(struct rnbd_srv_dev *dev)
203{
Guoqing Jiangd16b5ac2021-04-19 09:37:10 +0200204 kref_put(&dev->kref, destroy_device);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200205}
206
Lutz Pogrell786998052020-11-26 11:47:20 +0100207void rnbd_destroy_sess_dev(struct rnbd_srv_sess_dev *sess_dev, bool keep_id)
Jack Wang2de6c8d2020-05-11 15:51:26 +0200208{
209 DECLARE_COMPLETION_ONSTACK(dc);
210
Lutz Pogrell786998052020-11-26 11:47:20 +0100211 if (keep_id)
212 /* free the resources for the id but don't */
213 /* allow to re-use the id itself because it */
214 /* is still used by the client */
215 xa_cmpxchg(&sess_dev->sess->index_idr, sess_dev->device_id,
216 sess_dev, NULL, 0);
217 else
218 xa_erase(&sess_dev->sess->index_idr, sess_dev->device_id);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200219 synchronize_rcu();
Lutz Pogrell786998052020-11-26 11:47:20 +0100220
Jack Wang2de6c8d2020-05-11 15:51:26 +0200221 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
239static 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
252out:
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
Vaishali Thakkarf7ecac62022-01-05 19:07:07 +0100266static int create_sess(struct rtrs_srv_sess *rtrs)
Jack Wang2de6c8d2020-05-11 15:51:26 +0200267{
268 struct rnbd_srv_session *srv_sess;
Vaishali Thakkarae4c8162022-01-05 19:07:05 +0100269 char pathname[NAME_MAX];
Jack Wang2de6c8d2020-05-11 15:51:26 +0200270 int err;
271
Vaishali Thakkarae4c8162022-01-05 19:07:05 +0100272 err = rtrs_srv_get_path_name(rtrs, pathname, sizeof(pathname));
Jack Wang2de6c8d2020-05-11 15:51:26 +0200273 if (err) {
Vaishali Thakkarae4c8162022-01-05 19:07:05 +0100274 pr_err("rtrs_srv_get_path_name(%s): %d\n", pathname, err);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200275
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) {
Vaishali Thakkarae4c8162022-01-05 19:07:05 +0100287 pr_err("Allocating srv_session for path %s failed\n",
288 pathname);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200289 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;
Vaishali Thakkarae4c8162022-01-05 19:07:05 +0100301 strscpy(srv_sess->sessname, pathname, sizeof(srv_sess->sessname));
Jack Wang2de6c8d2020-05-11 15:51:26 +0200302
303 rtrs_srv_set_sess_priv(rtrs, srv_sess);
304
305 return 0;
306}
307
Vaishali Thakkarf7ecac62022-01-05 19:07:07 +0100308static int rnbd_srv_link_ev(struct rtrs_srv_sess *rtrs,
Jack Wang2de6c8d2020-05-11 15:51:26 +0200309 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
Gioh Kimc77bfa82021-04-19 09:37:13 +0200331void rnbd_srv_sess_dev_force_close(struct rnbd_srv_sess_dev *sess_dev,
332 struct kobj_attribute *attr)
Lutz Pogrell786998052020-11-26 11:47:20 +0100333{
Jack Wang1a84e7c2021-01-08 15:36:31 +0100334 struct rnbd_srv_session *sess = sess_dev->sess;
335
Lutz Pogrell786998052020-11-26 11:47:20 +0100336 sess_dev->keep_id = true;
Gioh Kimb168e1d2021-04-19 09:37:12 +0200337 /* It is already started to close by client's close message. */
338 if (!mutex_trylock(&sess->lock))
339 return;
Gioh Kimc77bfa82021-04-19 09:37:13 +0200340 /* first remove sysfs itself to avoid deadlock */
341 sysfs_remove_file_self(&sess_dev->kobj, &attr->attr);
Jack Wang1a84e7c2021-01-08 15:36:31 +0100342 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
343 mutex_unlock(&sess->lock);
Lutz Pogrell786998052020-11-26 11:47:20 +0100344}
345
Gioh Kimc81cba82021-04-19 09:37:18 +0200346static int process_msg_close(struct rnbd_srv_session *srv_sess,
Jack Wang2de6c8d2020-05-11 15:51:26 +0200347 void *data, size_t datalen, const void *usr,
348 size_t usrlen)
349{
350 const struct rnbd_msg_close *close_msg = usr;
351 struct rnbd_srv_sess_dev *sess_dev;
352
353 sess_dev = rnbd_get_sess_dev(le32_to_cpu(close_msg->device_id),
354 srv_sess);
355 if (IS_ERR(sess_dev))
356 return 0;
357
358 rnbd_put_sess_dev(sess_dev);
359 mutex_lock(&srv_sess->lock);
360 rnbd_srv_destroy_dev_session_sysfs(sess_dev);
361 mutex_unlock(&srv_sess->lock);
362 return 0;
363}
364
Gioh Kimc81cba82021-04-19 09:37:18 +0200365static int process_msg_open(struct rnbd_srv_session *srv_sess,
Jack Wang2de6c8d2020-05-11 15:51:26 +0200366 const void *msg, size_t len,
367 void *data, size_t datalen);
368
Gioh Kimc81cba82021-04-19 09:37:18 +0200369static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
Jack Wang2de6c8d2020-05-11 15:51:26 +0200370 const void *msg, size_t len,
371 void *data, size_t datalen);
372
Gioh Kimc81cba82021-04-19 09:37:18 +0200373static int rnbd_srv_rdma_ev(void *priv,
374 struct rtrs_srv_op *id, int dir,
375 void *data, size_t datalen, const void *usr,
376 size_t usrlen)
Jack Wang2de6c8d2020-05-11 15:51:26 +0200377{
378 struct rnbd_srv_session *srv_sess = priv;
379 const struct rnbd_msg_hdr *hdr = usr;
380 int ret = 0;
381 u16 type;
382
383 if (WARN_ON_ONCE(!srv_sess))
384 return -ENODEV;
385
386 type = le16_to_cpu(hdr->type);
387
388 switch (type) {
389 case RNBD_MSG_IO:
Gioh Kimc81cba82021-04-19 09:37:18 +0200390 return process_rdma(srv_sess, id, data, datalen, usr, usrlen);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200391 case RNBD_MSG_CLOSE:
Gioh Kimc81cba82021-04-19 09:37:18 +0200392 ret = process_msg_close(srv_sess, data, datalen, usr, usrlen);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200393 break;
394 case RNBD_MSG_OPEN:
Gioh Kimc81cba82021-04-19 09:37:18 +0200395 ret = process_msg_open(srv_sess, usr, usrlen, data, datalen);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200396 break;
397 case RNBD_MSG_SESS_INFO:
Gioh Kimc81cba82021-04-19 09:37:18 +0200398 ret = process_msg_sess_info(srv_sess, usr, usrlen, data,
399 datalen);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200400 break;
401 default:
402 pr_warn("Received unexpected message type %d with dir %d from session %s\n",
403 type, dir, srv_sess->sessname);
404 return -EINVAL;
405 }
406
407 rtrs_srv_resp_rdma(id, ret);
408 return 0;
409}
410
411static struct rnbd_srv_sess_dev
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
432static struct rnbd_srv_dev *rnbd_srv_init_srv_dev(const char *id)
433{
434 struct rnbd_srv_dev *dev;
435
436 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
437 if (!dev)
438 return ERR_PTR(-ENOMEM);
439
Dima Stepanov57b93ed2021-04-19 09:37:22 +0200440 strscpy(dev->id, id, sizeof(dev->id));
Jack Wang2de6c8d2020-05-11 15:51:26 +0200441 kref_init(&dev->kref);
442 INIT_LIST_HEAD(&dev->sess_dev_list);
443 mutex_init(&dev->lock);
444
445 return dev;
446}
447
448static struct rnbd_srv_dev *
449rnbd_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->id, new_dev->id, sizeof(dev->id))) {
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
472static 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 = -EPERM;
477
478 mutex_lock(&srv_dev->lock);
479
480 switch (access_mode) {
481 case RNBD_ACCESS_RO:
482 ret = 0;
483 break;
484 case RNBD_ACCESS_RW:
485 if (srv_dev->open_write_cnt == 0) {
486 srv_dev->open_write_cnt++;
487 ret = 0;
488 } else {
489 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",
490 srv_dev->id, srv_sess->sessname,
491 srv_dev->open_write_cnt,
492 rnbd_access_mode_str(access_mode));
493 }
494 break;
495 case RNBD_ACCESS_MIGRATION:
496 if (srv_dev->open_write_cnt < 2) {
497 srv_dev->open_write_cnt++;
498 ret = 0;
499 } else {
500 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",
501 srv_dev->id, srv_sess->sessname,
502 srv_dev->open_write_cnt,
503 rnbd_access_mode_str(access_mode));
504 }
505 break;
506 default:
507 pr_err("Received mapping request for device '%s' on session %s with invalid access mode: %d\n",
508 srv_dev->id, srv_sess->sessname, access_mode);
509 ret = -EINVAL;
510 }
511
512 mutex_unlock(&srv_dev->lock);
513
514 return ret;
515}
516
517static struct rnbd_srv_dev *
518rnbd_srv_get_or_create_srv_dev(struct rnbd_dev *rnbd_dev,
519 struct rnbd_srv_session *srv_sess,
520 enum rnbd_access_mode access_mode)
521{
522 int ret;
523 struct rnbd_srv_dev *new_dev, *dev;
524
525 new_dev = rnbd_srv_init_srv_dev(rnbd_dev->name);
526 if (IS_ERR(new_dev))
527 return new_dev;
528
529 dev = rnbd_srv_find_or_add_srv_dev(new_dev);
530 if (dev != new_dev)
531 kfree(new_dev);
532
533 ret = rnbd_srv_check_update_open_perm(dev, srv_sess, access_mode);
534 if (ret) {
535 rnbd_put_srv_dev(dev);
536 return ERR_PTR(ret);
537 }
538
539 return dev;
540}
541
542static void rnbd_srv_fill_msg_open_rsp(struct rnbd_msg_open_rsp *rsp,
543 struct rnbd_srv_sess_dev *sess_dev)
544{
545 struct rnbd_dev *rnbd_dev = sess_dev->rnbd_dev;
Gioh Kim512c7812020-12-10 11:18:24 +0100546 struct request_queue *q = bdev_get_queue(rnbd_dev->bdev);
Jack Wang2de6c8d2020-05-11 15:51:26 +0200547
548 rsp->hdr.type = cpu_to_le16(RNBD_MSG_OPEN_RSP);
549 rsp->device_id =
550 cpu_to_le32(sess_dev->device_id);
551 rsp->nsectors =
552 cpu_to_le64(get_capacity(rnbd_dev->bdev->bd_disk));
553 rsp->logical_block_size =
554 cpu_to_le16(bdev_logical_block_size(rnbd_dev->bdev));
555 rsp->physical_block_size =
556 cpu_to_le16(bdev_physical_block_size(rnbd_dev->bdev));
557 rsp->max_segments =
558 cpu_to_le16(rnbd_dev_get_max_segs(rnbd_dev));
559 rsp->max_hw_sectors =
560 cpu_to_le32(rnbd_dev_get_max_hw_sects(rnbd_dev));
561 rsp->max_write_same_sectors =
562 cpu_to_le32(bdev_write_same(rnbd_dev->bdev));
563 rsp->max_discard_sectors =
564 cpu_to_le32(rnbd_dev_get_max_discard_sects(rnbd_dev));
565 rsp->discard_granularity =
566 cpu_to_le32(rnbd_dev_get_discard_granularity(rnbd_dev));
567 rsp->discard_alignment =
568 cpu_to_le32(rnbd_dev_get_discard_alignment(rnbd_dev));
569 rsp->secure_discard =
570 cpu_to_le16(rnbd_dev_get_secure_discard(rnbd_dev));
Gioh Kim512c7812020-12-10 11:18:24 +0100571 rsp->rotational = !blk_queue_nonrot(q);
572 rsp->cache_policy = 0;
573 if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
574 rsp->cache_policy |= RNBD_WRITEBACK;
575 if (blk_queue_fua(q))
576 rsp->cache_policy |= RNBD_FUA;
Jack Wang2de6c8d2020-05-11 15:51:26 +0200577}
578
579static struct rnbd_srv_sess_dev *
580rnbd_srv_create_set_sess_dev(struct rnbd_srv_session *srv_sess,
581 const struct rnbd_msg_open *open_msg,
582 struct rnbd_dev *rnbd_dev, fmode_t open_flags,
583 struct rnbd_srv_dev *srv_dev)
584{
585 struct rnbd_srv_sess_dev *sdev = rnbd_sess_dev_alloc(srv_sess);
586
587 if (IS_ERR(sdev))
588 return sdev;
589
590 kref_init(&sdev->kref);
591
Dima Stepanov57b93ed2021-04-19 09:37:22 +0200592 strscpy(sdev->pathname, open_msg->dev_name, sizeof(sdev->pathname));
Jack Wang2de6c8d2020-05-11 15:51:26 +0200593
594 sdev->rnbd_dev = rnbd_dev;
595 sdev->sess = srv_sess;
596 sdev->dev = srv_dev;
597 sdev->open_flags = open_flags;
598 sdev->access_mode = open_msg->access_mode;
599
600 return sdev;
601}
602
603static char *rnbd_srv_get_full_path(struct rnbd_srv_session *srv_sess,
604 const char *dev_name)
605{
606 char *full_path;
607 char *a, *b;
608
609 full_path = kmalloc(PATH_MAX, GFP_KERNEL);
610 if (!full_path)
611 return ERR_PTR(-ENOMEM);
612
613 /*
614 * Replace %SESSNAME% with a real session name in order to
615 * create device namespace.
616 */
617 a = strnstr(dev_search_path, "%SESSNAME%", sizeof(dev_search_path));
618 if (a) {
619 int len = a - dev_search_path;
620
621 len = snprintf(full_path, PATH_MAX, "%.*s/%s/%s", len,
622 dev_search_path, srv_sess->sessname, dev_name);
623 if (len >= PATH_MAX) {
624 pr_err("Too long path: %s, %s, %s\n",
625 dev_search_path, srv_sess->sessname, dev_name);
626 kfree(full_path);
627 return ERR_PTR(-EINVAL);
628 }
629 } else {
630 snprintf(full_path, PATH_MAX, "%s/%s",
631 dev_search_path, dev_name);
632 }
633
634 /* eliminitate duplicated slashes */
635 a = strchr(full_path, '/');
636 b = a;
637 while (*b != '\0') {
638 if (*b == '/' && *a == '/') {
639 b++;
640 } else {
641 a++;
642 *a = *b;
643 b++;
644 }
645 }
646 a++;
647 *a = '\0';
648
649 return full_path;
650}
651
Gioh Kimc81cba82021-04-19 09:37:18 +0200652static int process_msg_sess_info(struct rnbd_srv_session *srv_sess,
Jack Wang2de6c8d2020-05-11 15:51:26 +0200653 const void *msg, size_t len,
654 void *data, size_t datalen)
655{
656 const struct rnbd_msg_sess_info *sess_info_msg = msg;
657 struct rnbd_msg_sess_info_rsp *rsp = data;
658
659 srv_sess->ver = min_t(u8, sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
660 pr_debug("Session %s using protocol version %d (client version: %d, server version: %d)\n",
661 srv_sess->sessname, srv_sess->ver,
662 sess_info_msg->ver, RNBD_PROTO_VER_MAJOR);
663
664 rsp->hdr.type = cpu_to_le16(RNBD_MSG_SESS_INFO_RSP);
665 rsp->ver = srv_sess->ver;
666
667 return 0;
668}
669
670/**
671 * find_srv_sess_dev() - a dev is already opened by this name
672 * @srv_sess: the session to search.
673 * @dev_name: string containing the name of the device.
674 *
675 * Return struct rnbd_srv_sess_dev if srv_sess already opened the dev_name
676 * NULL if the session didn't open the device yet.
677 */
678static struct rnbd_srv_sess_dev *
679find_srv_sess_dev(struct rnbd_srv_session *srv_sess, const char *dev_name)
680{
681 struct rnbd_srv_sess_dev *sess_dev;
682
683 if (list_empty(&srv_sess->sess_dev_list))
684 return NULL;
685
686 list_for_each_entry(sess_dev, &srv_sess->sess_dev_list, sess_list)
687 if (!strcmp(sess_dev->pathname, dev_name))
688 return sess_dev;
689
690 return NULL;
691}
692
Gioh Kimc81cba82021-04-19 09:37:18 +0200693static int process_msg_open(struct rnbd_srv_session *srv_sess,
Jack Wang2de6c8d2020-05-11 15:51:26 +0200694 const void *msg, size_t len,
695 void *data, size_t datalen)
696{
697 int ret;
698 struct rnbd_srv_dev *srv_dev;
699 struct rnbd_srv_sess_dev *srv_sess_dev;
700 const struct rnbd_msg_open *open_msg = msg;
701 fmode_t open_flags;
702 char *full_path;
703 struct rnbd_dev *rnbd_dev;
704 struct rnbd_msg_open_rsp *rsp = data;
705
706 pr_debug("Open message received: session='%s' path='%s' access_mode=%d\n",
707 srv_sess->sessname, open_msg->dev_name,
708 open_msg->access_mode);
709 open_flags = FMODE_READ;
710 if (open_msg->access_mode != RNBD_ACCESS_RO)
711 open_flags |= FMODE_WRITE;
712
713 mutex_lock(&srv_sess->lock);
714
715 srv_sess_dev = find_srv_sess_dev(srv_sess, open_msg->dev_name);
716 if (srv_sess_dev)
717 goto fill_response;
718
719 if ((strlen(dev_search_path) + strlen(open_msg->dev_name))
720 >= PATH_MAX) {
721 pr_err("Opening device for session %s failed, device path too long. '%s/%s' is longer than PATH_MAX (%d)\n",
722 srv_sess->sessname, dev_search_path, open_msg->dev_name,
723 PATH_MAX);
724 ret = -EINVAL;
725 goto reject;
726 }
727 if (strstr(open_msg->dev_name, "..")) {
728 pr_err("Opening device for session %s failed, device path %s contains relative path ..\n",
729 srv_sess->sessname, open_msg->dev_name);
730 ret = -EINVAL;
731 goto reject;
732 }
733 full_path = rnbd_srv_get_full_path(srv_sess, open_msg->dev_name);
734 if (IS_ERR(full_path)) {
735 ret = PTR_ERR(full_path);
736 pr_err("Opening device '%s' for client %s failed, failed to get device full path, err: %d\n",
737 open_msg->dev_name, srv_sess->sessname, ret);
738 goto reject;
739 }
740
741 rnbd_dev = rnbd_dev_open(full_path, open_flags,
742 &srv_sess->sess_bio_set);
743 if (IS_ERR(rnbd_dev)) {
744 pr_err("Opening device '%s' on session %s failed, failed to open the block device, err: %ld\n",
745 full_path, srv_sess->sessname, PTR_ERR(rnbd_dev));
746 ret = PTR_ERR(rnbd_dev);
747 goto free_path;
748 }
749
750 srv_dev = rnbd_srv_get_or_create_srv_dev(rnbd_dev, srv_sess,
751 open_msg->access_mode);
752 if (IS_ERR(srv_dev)) {
753 pr_err("Opening device '%s' on session %s failed, creating srv_dev failed, err: %ld\n",
754 full_path, srv_sess->sessname, PTR_ERR(srv_dev));
755 ret = PTR_ERR(srv_dev);
756 goto rnbd_dev_close;
757 }
758
759 srv_sess_dev = rnbd_srv_create_set_sess_dev(srv_sess, open_msg,
760 rnbd_dev, open_flags,
761 srv_dev);
762 if (IS_ERR(srv_sess_dev)) {
763 pr_err("Opening device '%s' on session %s failed, creating sess_dev failed, err: %ld\n",
764 full_path, srv_sess->sessname, PTR_ERR(srv_sess_dev));
765 ret = PTR_ERR(srv_sess_dev);
766 goto srv_dev_put;
767 }
768
769 /* Create the srv_dev sysfs files if they haven't been created yet. The
770 * reason to delay the creation is not to create the sysfs files before
771 * we are sure the device can be opened.
772 */
773 mutex_lock(&srv_dev->lock);
774 if (!srv_dev->dev_kobj.state_in_sysfs) {
775 ret = rnbd_srv_create_dev_sysfs(srv_dev, rnbd_dev->bdev,
776 rnbd_dev->name);
777 if (ret) {
778 mutex_unlock(&srv_dev->lock);
779 rnbd_srv_err(srv_sess_dev,
780 "Opening device failed, failed to create device sysfs files, err: %d\n",
781 ret);
782 goto free_srv_sess_dev;
783 }
784 }
785
786 ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
787 if (ret) {
788 mutex_unlock(&srv_dev->lock);
789 rnbd_srv_err(srv_sess_dev,
790 "Opening device failed, failed to create dev client sysfs files, err: %d\n",
791 ret);
792 goto free_srv_sess_dev;
793 }
794
795 list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
796 mutex_unlock(&srv_dev->lock);
797
798 list_add(&srv_sess_dev->sess_list, &srv_sess->sess_dev_list);
799
800 rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->id);
801
802 kfree(full_path);
803
804fill_response:
805 rnbd_srv_fill_msg_open_rsp(rsp, srv_sess_dev);
806 mutex_unlock(&srv_sess->lock);
807 return 0;
808
809free_srv_sess_dev:
810 xa_erase(&srv_sess->index_idr, srv_sess_dev->device_id);
811 synchronize_rcu();
812 kfree(srv_sess_dev);
813srv_dev_put:
814 if (open_msg->access_mode != RNBD_ACCESS_RO) {
815 mutex_lock(&srv_dev->lock);
816 srv_dev->open_write_cnt--;
817 mutex_unlock(&srv_dev->lock);
818 }
819 rnbd_put_srv_dev(srv_dev);
820rnbd_dev_close:
821 rnbd_dev_close(rnbd_dev);
822free_path:
823 kfree(full_path);
824reject:
825 mutex_unlock(&srv_sess->lock);
826 return ret;
827}
828
829static struct rtrs_srv_ctx *rtrs_ctx;
830
831static struct rtrs_srv_ops rtrs_ops;
832static int __init rnbd_srv_init_module(void)
833{
834 int err;
835
836 BUILD_BUG_ON(sizeof(struct rnbd_msg_hdr) != 4);
837 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info) != 36);
838 BUILD_BUG_ON(sizeof(struct rnbd_msg_sess_info_rsp) != 36);
839 BUILD_BUG_ON(sizeof(struct rnbd_msg_open) != 264);
840 BUILD_BUG_ON(sizeof(struct rnbd_msg_close) != 8);
841 BUILD_BUG_ON(sizeof(struct rnbd_msg_open_rsp) != 56);
842 rtrs_ops = (struct rtrs_srv_ops) {
843 .rdma_ev = rnbd_srv_rdma_ev,
844 .link_ev = rnbd_srv_link_ev,
845 };
846 rtrs_ctx = rtrs_srv_open(&rtrs_ops, port_nr);
847 if (IS_ERR(rtrs_ctx)) {
848 err = PTR_ERR(rtrs_ctx);
849 pr_err("rtrs_srv_open(), err: %d\n", err);
850 return err;
851 }
852
853 err = rnbd_srv_create_sysfs_files();
854 if (err) {
855 pr_err("rnbd_srv_create_sysfs_files(), err: %d\n", err);
856 rtrs_srv_close(rtrs_ctx);
857 return err;
858 }
859
860 return 0;
861}
862
863static void __exit rnbd_srv_cleanup_module(void)
864{
865 rtrs_srv_close(rtrs_ctx);
866 WARN_ON(!list_empty(&sess_list));
867 rnbd_srv_destroy_sysfs_files();
868}
869
870module_init(rnbd_srv_init_module);
871module_exit(rnbd_srv_cleanup_module);