1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RDMA Network Block Driver
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.
11 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
13 #include <linux/types.h>
14 #include <linux/ctype.h>
15 #include <linux/parser.h>
16 #include <linux/module.h>
17 #include <linux/in6.h>
19 #include <linux/uaccess.h>
20 #include <linux/device.h>
22 #include <rdma/rdma_cm.h>
26 static struct device *rnbd_dev;
27 static struct class *rnbd_dev_class;
28 static struct kobject *rnbd_devs_kobj;
32 RNBD_OPT_DEST_PORT = 1 << 0,
33 RNBD_OPT_PATH = 1 << 1,
34 RNBD_OPT_DEV_PATH = 1 << 2,
35 RNBD_OPT_ACCESS_MODE = 1 << 3,
36 RNBD_OPT_SESSNAME = 1 << 6,
37 RNBD_OPT_NR_POLL_QUEUES = 1 << 7,
40 static const unsigned int rnbd_opt_mandatory[] = {
45 static const match_table_t rnbd_opt_tokens = {
46 {RNBD_OPT_PATH, "path=%s" },
47 {RNBD_OPT_DEV_PATH, "device_path=%s" },
48 {RNBD_OPT_DEST_PORT, "dest_port=%d" },
49 {RNBD_OPT_ACCESS_MODE, "access_mode=%s" },
50 {RNBD_OPT_SESSNAME, "sessname=%s" },
51 {RNBD_OPT_NR_POLL_QUEUES, "nr_poll_queues=%d" },
52 {RNBD_OPT_ERR, NULL },
55 struct rnbd_map_options {
57 struct rtrs_addr *paths;
61 enum rnbd_access_mode *access_mode;
65 static int rnbd_clt_parse_map_options(const char *buf, size_t max_path_cnt,
66 struct rnbd_map_options *opt)
68 char *options, *sep_opt;
70 substring_t args[MAX_OPT_ARGS];
74 int nr_poll_queues = 0;
79 options = kstrdup(buf, GFP_KERNEL);
83 sep_opt = strstrip(options);
84 while ((p = strsep(&sep_opt, " ")) != NULL) {
88 token = match_token(p, rnbd_opt_tokens, args);
92 case RNBD_OPT_SESSNAME:
93 p = match_strdup(args);
98 if (strlen(p) > NAME_MAX) {
99 pr_err("map_device: sessname too long\n");
104 strscpy(opt->sessname, p, NAME_MAX);
109 if (p_cnt >= max_path_cnt) {
110 pr_err("map_device: too many (> %zu) paths provided\n",
115 p = match_strdup(args);
121 ret = rtrs_addr_to_sockaddr(p, strlen(p),
125 pr_err("Can't parse path %s: %d\n", p, ret);
135 case RNBD_OPT_DEV_PATH:
136 p = match_strdup(args);
141 if (strlen(p) > NAME_MAX) {
142 pr_err("map_device: Device path too long\n");
147 strscpy(opt->pathname, p, NAME_MAX);
151 case RNBD_OPT_DEST_PORT:
152 if (match_int(args, &dest_port) || dest_port < 0 ||
154 pr_err("bad destination port number parameter '%d'\n",
159 *opt->dest_port = dest_port;
162 case RNBD_OPT_ACCESS_MODE:
163 p = match_strdup(args);
169 if (!strcmp(p, "ro")) {
170 *opt->access_mode = RNBD_ACCESS_RO;
171 } else if (!strcmp(p, "rw")) {
172 *opt->access_mode = RNBD_ACCESS_RW;
173 } else if (!strcmp(p, "migration")) {
174 *opt->access_mode = RNBD_ACCESS_MIGRATION;
176 pr_err("map_device: Invalid access_mode: '%s'\n",
186 case RNBD_OPT_NR_POLL_QUEUES:
187 if (match_int(args, &nr_poll_queues) || nr_poll_queues < -1 ||
188 nr_poll_queues > (int)nr_cpu_ids) {
189 pr_err("bad nr_poll_queues parameter '%d'\n",
194 if (nr_poll_queues == -1)
195 nr_poll_queues = nr_cpu_ids;
196 *opt->nr_poll_queues = nr_poll_queues;
200 pr_err("map_device: Unknown parameter or missing value '%s'\n",
207 for (i = 0; i < ARRAY_SIZE(rnbd_opt_mandatory); i++) {
208 if ((opt_mask & rnbd_opt_mandatory[i])) {
211 pr_err("map_device: Parameters missing\n");
218 *opt->path_cnt = p_cnt;
223 static ssize_t state_show(struct kobject *kobj,
224 struct kobj_attribute *attr, char *page)
226 struct rnbd_clt_dev *dev;
228 dev = container_of(kobj, struct rnbd_clt_dev, kobj);
230 switch (dev->dev_state) {
232 return sysfs_emit(page, "init\n");
233 case DEV_STATE_MAPPED:
234 /* TODO fix cli tool before changing to proper state */
235 return sysfs_emit(page, "open\n");
236 case DEV_STATE_MAPPED_DISCONNECTED:
237 /* TODO fix cli tool before changing to proper state */
238 return sysfs_emit(page, "closed\n");
239 case DEV_STATE_UNMAPPED:
240 return sysfs_emit(page, "unmapped\n");
242 return sysfs_emit(page, "unknown\n");
246 static struct kobj_attribute rnbd_clt_state_attr = __ATTR_RO(state);
248 static ssize_t nr_poll_queues_show(struct kobject *kobj,
249 struct kobj_attribute *attr, char *page)
251 struct rnbd_clt_dev *dev;
253 dev = container_of(kobj, struct rnbd_clt_dev, kobj);
255 return sysfs_emit(page, "%d\n", dev->nr_poll_queues);
258 static struct kobj_attribute rnbd_clt_nr_poll_queues =
259 __ATTR_RO(nr_poll_queues);
261 static ssize_t mapping_path_show(struct kobject *kobj,
262 struct kobj_attribute *attr, char *page)
264 struct rnbd_clt_dev *dev;
266 dev = container_of(kobj, struct rnbd_clt_dev, kobj);
268 return sysfs_emit(page, "%s\n", dev->pathname);
271 static struct kobj_attribute rnbd_clt_mapping_path_attr =
272 __ATTR_RO(mapping_path);
274 static ssize_t access_mode_show(struct kobject *kobj,
275 struct kobj_attribute *attr, char *page)
277 struct rnbd_clt_dev *dev;
279 dev = container_of(kobj, struct rnbd_clt_dev, kobj);
281 return sysfs_emit(page, "%s\n", rnbd_access_mode_str(dev->access_mode));
284 static struct kobj_attribute rnbd_clt_access_mode =
285 __ATTR_RO(access_mode);
287 static ssize_t rnbd_clt_unmap_dev_show(struct kobject *kobj,
288 struct kobj_attribute *attr, char *page)
290 return sysfs_emit(page, "Usage: echo <normal|force> > %s\n",
294 static ssize_t rnbd_clt_unmap_dev_store(struct kobject *kobj,
295 struct kobj_attribute *attr,
296 const char *buf, size_t count)
298 struct rnbd_clt_dev *dev;
303 opt = kstrdup(buf, GFP_KERNEL);
307 options = strstrip(opt);
308 dev = container_of(kobj, struct rnbd_clt_dev, kobj);
309 if (sysfs_streq(options, "normal")) {
311 } else if (sysfs_streq(options, "force")) {
315 "unmap_device: Invalid value: %s\n",
321 rnbd_clt_info(dev, "Unmapping device, option: %s.\n",
322 force ? "force" : "normal");
325 * We take explicit module reference only for one reason: do not
326 * race with lockless rnbd_destroy_sessions().
328 if (!try_module_get(THIS_MODULE)) {
332 err = rnbd_clt_unmap_device(dev, force, &attr->attr);
334 if (err != -EALREADY)
335 rnbd_clt_err(dev, "unmap_device: %d\n", err);
340 * Here device can be vanished!
346 module_put(THIS_MODULE);
353 static struct kobj_attribute rnbd_clt_unmap_device_attr =
354 __ATTR(unmap_device, 0644, rnbd_clt_unmap_dev_show,
355 rnbd_clt_unmap_dev_store);
357 static ssize_t rnbd_clt_resize_dev_show(struct kobject *kobj,
358 struct kobj_attribute *attr,
361 return sysfs_emit(page, "Usage: echo <new size in sectors> > %s\n",
365 static ssize_t rnbd_clt_resize_dev_store(struct kobject *kobj,
366 struct kobj_attribute *attr,
367 const char *buf, size_t count)
370 unsigned long sectors;
371 struct rnbd_clt_dev *dev;
373 dev = container_of(kobj, struct rnbd_clt_dev, kobj);
375 ret = kstrtoul(buf, 0, §ors);
379 ret = rnbd_clt_resize_disk(dev, (size_t)sectors);
386 static struct kobj_attribute rnbd_clt_resize_dev_attr =
387 __ATTR(resize, 0644, rnbd_clt_resize_dev_show,
388 rnbd_clt_resize_dev_store);
390 static ssize_t rnbd_clt_remap_dev_show(struct kobject *kobj,
391 struct kobj_attribute *attr, char *page)
393 return sysfs_emit(page, "Usage: echo <1> > %s\n", attr->attr.name);
396 static ssize_t rnbd_clt_remap_dev_store(struct kobject *kobj,
397 struct kobj_attribute *attr,
398 const char *buf, size_t count)
400 struct rnbd_clt_dev *dev;
404 opt = kstrdup(buf, GFP_KERNEL);
408 options = strstrip(opt);
409 dev = container_of(kobj, struct rnbd_clt_dev, kobj);
410 if (!sysfs_streq(options, "1")) {
412 "remap_device: Invalid value: %s\n",
417 err = rnbd_clt_remap_device(dev);
427 static struct kobj_attribute rnbd_clt_remap_device_attr =
428 __ATTR(remap_device, 0644, rnbd_clt_remap_dev_show,
429 rnbd_clt_remap_dev_store);
431 static ssize_t session_show(struct kobject *kobj, struct kobj_attribute *attr,
434 struct rnbd_clt_dev *dev;
436 dev = container_of(kobj, struct rnbd_clt_dev, kobj);
438 return sysfs_emit(page, "%s\n", dev->sess->sessname);
441 static struct kobj_attribute rnbd_clt_session_attr =
444 static struct attribute *rnbd_dev_attrs[] = {
445 &rnbd_clt_unmap_device_attr.attr,
446 &rnbd_clt_resize_dev_attr.attr,
447 &rnbd_clt_remap_device_attr.attr,
448 &rnbd_clt_mapping_path_attr.attr,
449 &rnbd_clt_state_attr.attr,
450 &rnbd_clt_session_attr.attr,
451 &rnbd_clt_access_mode.attr,
452 &rnbd_clt_nr_poll_queues.attr,
456 void rnbd_clt_remove_dev_symlink(struct rnbd_clt_dev *dev)
459 * The module unload rnbd_client_exit path is racing with unmapping of
460 * the last single device from the sysfs manually
461 * i.e. rnbd_clt_unmap_dev_store() leading to a sysfs warning because
462 * of sysfs link already was removed already.
464 if (dev->blk_symlink_name) {
465 if (try_module_get(THIS_MODULE)) {
466 sysfs_remove_link(rnbd_devs_kobj, dev->blk_symlink_name);
467 module_put(THIS_MODULE);
469 /* It should be freed always. */
470 kfree(dev->blk_symlink_name);
471 dev->blk_symlink_name = NULL;
475 static struct kobj_type rnbd_dev_ktype = {
476 .sysfs_ops = &kobj_sysfs_ops,
477 .default_attrs = rnbd_dev_attrs,
480 static int rnbd_clt_add_dev_kobj(struct rnbd_clt_dev *dev)
483 struct kobject *gd_kobj = &disk_to_dev(dev->gd)->kobj;
485 ret = kobject_init_and_add(&dev->kobj, &rnbd_dev_ktype, gd_kobj, "%s",
488 rnbd_clt_err(dev, "Failed to create device sysfs dir, err: %d\n",
490 kobject_put(&dev->kobj);
492 kobject_uevent(gd_kobj, KOBJ_ONLINE);
497 static ssize_t rnbd_clt_map_device_show(struct kobject *kobj,
498 struct kobj_attribute *attr,
501 return sysfs_emit(page,
502 "Usage: echo \"[dest_port=server port number] sessname=<name of the rtrs session> path=<[srcaddr@]dstaddr> [path=<[srcaddr@]dstaddr>] device_path=<full path on remote side> [access_mode=<ro|rw|migration>] [nr_poll_queues=<number of queues>]\" > %s\n\naddr ::= [ ip:<ipv4> | ip:<ipv6> | gid:<gid> ]\n",
506 static int rnbd_clt_get_path_name(struct rnbd_clt_dev *dev, char *buf,
510 char pathname[NAME_MAX], *s;
512 strscpy(pathname, dev->pathname, sizeof(pathname));
513 while ((s = strchr(pathname, '/')))
516 ret = snprintf(buf, len, "%s@%s", pathname, dev->sess->sessname);
518 return -ENAMETOOLONG;
523 static int rnbd_clt_add_dev_symlink(struct rnbd_clt_dev *dev)
525 struct kobject *gd_kobj = &disk_to_dev(dev->gd)->kobj;
528 len = strlen(dev->pathname) + strlen(dev->sess->sessname) + 2;
529 dev->blk_symlink_name = kzalloc(len, GFP_KERNEL);
530 if (!dev->blk_symlink_name) {
531 rnbd_clt_err(dev, "Failed to allocate memory for blk_symlink_name\n");
535 ret = rnbd_clt_get_path_name(dev, dev->blk_symlink_name,
538 rnbd_clt_err(dev, "Failed to get /sys/block symlink path, err: %d\n",
543 ret = sysfs_create_link(rnbd_devs_kobj, gd_kobj,
544 dev->blk_symlink_name);
546 rnbd_clt_err(dev, "Creating /sys/block symlink failed, err: %d\n",
554 kfree(dev->blk_symlink_name);
555 dev->blk_symlink_name = NULL ;
559 static ssize_t rnbd_clt_map_device_store(struct kobject *kobj,
560 struct kobj_attribute *attr,
561 const char *buf, size_t count)
563 struct rnbd_clt_dev *dev;
564 struct rnbd_map_options opt;
566 char pathname[NAME_MAX];
567 char sessname[NAME_MAX];
568 enum rnbd_access_mode access_mode = RNBD_ACCESS_RW;
569 u16 port_nr = RTRS_PORT;
570 u32 nr_poll_queues = 0;
572 struct sockaddr_storage *addrs;
573 struct rtrs_addr paths[6];
576 opt.sessname = sessname;
578 opt.path_cnt = &path_cnt;
579 opt.pathname = pathname;
580 opt.dest_port = &port_nr;
581 opt.access_mode = &access_mode;
582 opt.nr_poll_queues = &nr_poll_queues;
583 addrs = kcalloc(ARRAY_SIZE(paths) * 2, sizeof(*addrs), GFP_KERNEL);
587 for (path_cnt = 0; path_cnt < ARRAY_SIZE(paths); path_cnt++) {
588 paths[path_cnt].src = &addrs[path_cnt * 2];
589 paths[path_cnt].dst = &addrs[path_cnt * 2 + 1];
592 ret = rnbd_clt_parse_map_options(buf, ARRAY_SIZE(paths), &opt);
596 pr_info("Mapping device %s on session %s, (access_mode: %s, nr_poll_queues: %d)\n",
598 rnbd_access_mode_str(access_mode),
601 dev = rnbd_clt_map_device(sessname, paths, path_cnt, port_nr, pathname,
602 access_mode, nr_poll_queues);
608 ret = rnbd_clt_add_dev_kobj(dev);
612 ret = rnbd_clt_add_dev_symlink(dev);
620 rnbd_clt_unmap_device(dev, true, NULL);
626 static struct kobj_attribute rnbd_clt_map_device_attr =
627 __ATTR(map_device, 0644,
628 rnbd_clt_map_device_show, rnbd_clt_map_device_store);
630 static struct attribute *default_attrs[] = {
631 &rnbd_clt_map_device_attr.attr,
635 static struct attribute_group default_attr_group = {
636 .attrs = default_attrs,
639 static const struct attribute_group *default_attr_groups[] = {
644 int rnbd_clt_create_sysfs_files(void)
648 rnbd_dev_class = class_create(THIS_MODULE, "rnbd-client");
649 if (IS_ERR(rnbd_dev_class))
650 return PTR_ERR(rnbd_dev_class);
652 rnbd_dev = device_create_with_groups(rnbd_dev_class, NULL,
654 default_attr_groups, "ctl");
655 if (IS_ERR(rnbd_dev)) {
656 err = PTR_ERR(rnbd_dev);
659 rnbd_devs_kobj = kobject_create_and_add("devices", &rnbd_dev->kobj);
660 if (!rnbd_devs_kobj) {
668 device_destroy(rnbd_dev_class, MKDEV(0, 0));
670 class_destroy(rnbd_dev_class);
675 void rnbd_clt_destroy_sysfs_files(void)
677 sysfs_remove_group(&rnbd_dev->kobj, &default_attr_group);
678 kobject_del(rnbd_devs_kobj);
679 kobject_put(rnbd_devs_kobj);
680 device_destroy(rnbd_dev_class, MKDEV(0, 0));
681 class_destroy(rnbd_dev_class);