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,
39 static const unsigned int rnbd_opt_mandatory
[] = {
44 static const match_table_t rnbd_opt_tokens
= {
45 {RNBD_OPT_PATH
, "path=%s" },
46 {RNBD_OPT_DEV_PATH
, "device_path=%s"},
47 {RNBD_OPT_DEST_PORT
, "dest_port=%d" },
48 {RNBD_OPT_ACCESS_MODE
, "access_mode=%s"},
49 {RNBD_OPT_SESSNAME
, "sessname=%s" },
50 {RNBD_OPT_ERR
, NULL
},
53 struct rnbd_map_options
{
55 struct rtrs_addr
*paths
;
59 enum rnbd_access_mode
*access_mode
;
62 static int rnbd_clt_parse_map_options(const char *buf
, size_t max_path_cnt
,
63 struct rnbd_map_options
*opt
)
65 char *options
, *sep_opt
;
67 substring_t args
[MAX_OPT_ARGS
];
74 options
= kstrdup(buf
, GFP_KERNEL
);
78 sep_opt
= strstrip(options
);
79 while ((p
= strsep(&sep_opt
, " ")) != NULL
) {
83 token
= match_token(p
, rnbd_opt_tokens
, args
);
87 case RNBD_OPT_SESSNAME
:
88 p
= match_strdup(args
);
93 if (strlen(p
) > NAME_MAX
) {
94 pr_err("map_device: sessname too long\n");
99 strlcpy(opt
->sessname
, p
, NAME_MAX
);
104 if (p_cnt
>= max_path_cnt
) {
105 pr_err("map_device: too many (> %zu) paths provided\n",
110 p
= match_strdup(args
);
116 ret
= rtrs_addr_to_sockaddr(p
, strlen(p
),
120 pr_err("Can't parse path %s: %d\n", p
, ret
);
130 case RNBD_OPT_DEV_PATH
:
131 p
= match_strdup(args
);
136 if (strlen(p
) > NAME_MAX
) {
137 pr_err("map_device: Device path too long\n");
142 strlcpy(opt
->pathname
, p
, NAME_MAX
);
146 case RNBD_OPT_DEST_PORT
:
147 if (match_int(args
, &dest_port
) || dest_port
< 0 ||
149 pr_err("bad destination port number parameter '%d'\n",
154 *opt
->dest_port
= dest_port
;
157 case RNBD_OPT_ACCESS_MODE
:
158 p
= match_strdup(args
);
164 if (!strcmp(p
, "ro")) {
165 *opt
->access_mode
= RNBD_ACCESS_RO
;
166 } else if (!strcmp(p
, "rw")) {
167 *opt
->access_mode
= RNBD_ACCESS_RW
;
168 } else if (!strcmp(p
, "migration")) {
169 *opt
->access_mode
= RNBD_ACCESS_MIGRATION
;
171 pr_err("map_device: Invalid access_mode: '%s'\n",
182 pr_err("map_device: Unknown parameter or missing value '%s'\n",
189 for (i
= 0; i
< ARRAY_SIZE(rnbd_opt_mandatory
); i
++) {
190 if ((opt_mask
& rnbd_opt_mandatory
[i
])) {
193 pr_err("map_device: Parameters missing\n");
200 *opt
->path_cnt
= p_cnt
;
205 static ssize_t
state_show(struct kobject
*kobj
,
206 struct kobj_attribute
*attr
, char *page
)
208 struct rnbd_clt_dev
*dev
;
210 dev
= container_of(kobj
, struct rnbd_clt_dev
, kobj
);
212 switch (dev
->dev_state
) {
214 return snprintf(page
, PAGE_SIZE
, "init\n");
215 case DEV_STATE_MAPPED
:
216 /* TODO fix cli tool before changing to proper state */
217 return snprintf(page
, PAGE_SIZE
, "open\n");
218 case DEV_STATE_MAPPED_DISCONNECTED
:
219 /* TODO fix cli tool before changing to proper state */
220 return snprintf(page
, PAGE_SIZE
, "closed\n");
221 case DEV_STATE_UNMAPPED
:
222 return snprintf(page
, PAGE_SIZE
, "unmapped\n");
224 return snprintf(page
, PAGE_SIZE
, "unknown\n");
228 static struct kobj_attribute rnbd_clt_state_attr
= __ATTR_RO(state
);
230 static ssize_t
mapping_path_show(struct kobject
*kobj
,
231 struct kobj_attribute
*attr
, char *page
)
233 struct rnbd_clt_dev
*dev
;
235 dev
= container_of(kobj
, struct rnbd_clt_dev
, kobj
);
237 return scnprintf(page
, PAGE_SIZE
, "%s\n", dev
->pathname
);
240 static struct kobj_attribute rnbd_clt_mapping_path_attr
=
241 __ATTR_RO(mapping_path
);
243 static ssize_t
access_mode_show(struct kobject
*kobj
,
244 struct kobj_attribute
*attr
, char *page
)
246 struct rnbd_clt_dev
*dev
;
248 dev
= container_of(kobj
, struct rnbd_clt_dev
, kobj
);
250 return snprintf(page
, PAGE_SIZE
, "%s\n",
251 rnbd_access_mode_str(dev
->access_mode
));
254 static struct kobj_attribute rnbd_clt_access_mode
=
255 __ATTR_RO(access_mode
);
257 static ssize_t
rnbd_clt_unmap_dev_show(struct kobject
*kobj
,
258 struct kobj_attribute
*attr
, char *page
)
260 return scnprintf(page
, PAGE_SIZE
, "Usage: echo <normal|force> > %s\n",
264 static ssize_t
rnbd_clt_unmap_dev_store(struct kobject
*kobj
,
265 struct kobj_attribute
*attr
,
266 const char *buf
, size_t count
)
268 struct rnbd_clt_dev
*dev
;
273 opt
= kstrdup(buf
, GFP_KERNEL
);
277 options
= strstrip(opt
);
278 dev
= container_of(kobj
, struct rnbd_clt_dev
, kobj
);
279 if (sysfs_streq(options
, "normal")) {
281 } else if (sysfs_streq(options
, "force")) {
285 "unmap_device: Invalid value: %s\n",
291 rnbd_clt_info(dev
, "Unmapping device, option: %s.\n",
292 force
? "force" : "normal");
295 * We take explicit module reference only for one reason: do not
296 * race with lockless rnbd_destroy_sessions().
298 if (!try_module_get(THIS_MODULE
)) {
302 err
= rnbd_clt_unmap_device(dev
, force
, &attr
->attr
);
304 if (err
!= -EALREADY
)
305 rnbd_clt_err(dev
, "unmap_device: %d\n", err
);
310 * Here device can be vanished!
316 module_put(THIS_MODULE
);
323 static struct kobj_attribute rnbd_clt_unmap_device_attr
=
324 __ATTR(unmap_device
, 0644, rnbd_clt_unmap_dev_show
,
325 rnbd_clt_unmap_dev_store
);
327 static ssize_t
rnbd_clt_resize_dev_show(struct kobject
*kobj
,
328 struct kobj_attribute
*attr
,
331 return scnprintf(page
, PAGE_SIZE
,
332 "Usage: echo <new size in sectors> > %s\n",
336 static ssize_t
rnbd_clt_resize_dev_store(struct kobject
*kobj
,
337 struct kobj_attribute
*attr
,
338 const char *buf
, size_t count
)
341 unsigned long sectors
;
342 struct rnbd_clt_dev
*dev
;
344 dev
= container_of(kobj
, struct rnbd_clt_dev
, kobj
);
346 ret
= kstrtoul(buf
, 0, §ors
);
350 ret
= rnbd_clt_resize_disk(dev
, (size_t)sectors
);
357 static struct kobj_attribute rnbd_clt_resize_dev_attr
=
358 __ATTR(resize
, 0644, rnbd_clt_resize_dev_show
,
359 rnbd_clt_resize_dev_store
);
361 static ssize_t
rnbd_clt_remap_dev_show(struct kobject
*kobj
,
362 struct kobj_attribute
*attr
, char *page
)
364 return scnprintf(page
, PAGE_SIZE
, "Usage: echo <1> > %s\n",
368 static ssize_t
rnbd_clt_remap_dev_store(struct kobject
*kobj
,
369 struct kobj_attribute
*attr
,
370 const char *buf
, size_t count
)
372 struct rnbd_clt_dev
*dev
;
376 opt
= kstrdup(buf
, GFP_KERNEL
);
380 options
= strstrip(opt
);
381 dev
= container_of(kobj
, struct rnbd_clt_dev
, kobj
);
382 if (!sysfs_streq(options
, "1")) {
384 "remap_device: Invalid value: %s\n",
389 err
= rnbd_clt_remap_device(dev
);
399 static struct kobj_attribute rnbd_clt_remap_device_attr
=
400 __ATTR(remap_device
, 0644, rnbd_clt_remap_dev_show
,
401 rnbd_clt_remap_dev_store
);
403 static ssize_t
session_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
406 struct rnbd_clt_dev
*dev
;
408 dev
= container_of(kobj
, struct rnbd_clt_dev
, kobj
);
410 return scnprintf(page
, PAGE_SIZE
, "%s\n", dev
->sess
->sessname
);
413 static struct kobj_attribute rnbd_clt_session_attr
=
416 static struct attribute
*rnbd_dev_attrs
[] = {
417 &rnbd_clt_unmap_device_attr
.attr
,
418 &rnbd_clt_resize_dev_attr
.attr
,
419 &rnbd_clt_remap_device_attr
.attr
,
420 &rnbd_clt_mapping_path_attr
.attr
,
421 &rnbd_clt_state_attr
.attr
,
422 &rnbd_clt_session_attr
.attr
,
423 &rnbd_clt_access_mode
.attr
,
427 void rnbd_clt_remove_dev_symlink(struct rnbd_clt_dev
*dev
)
430 * The module unload rnbd_client_exit path is racing with unmapping of
431 * the last single device from the sysfs manually
432 * i.e. rnbd_clt_unmap_dev_store() leading to a sysfs warning because
433 * of sysfs link already was removed already.
435 if (dev
->blk_symlink_name
&& try_module_get(THIS_MODULE
)) {
436 sysfs_remove_link(rnbd_devs_kobj
, dev
->blk_symlink_name
);
437 kfree(dev
->blk_symlink_name
);
438 module_put(THIS_MODULE
);
442 static struct kobj_type rnbd_dev_ktype
= {
443 .sysfs_ops
= &kobj_sysfs_ops
,
444 .default_attrs
= rnbd_dev_attrs
,
447 static int rnbd_clt_add_dev_kobj(struct rnbd_clt_dev
*dev
)
450 struct kobject
*gd_kobj
= &disk_to_dev(dev
->gd
)->kobj
;
452 ret
= kobject_init_and_add(&dev
->kobj
, &rnbd_dev_ktype
, gd_kobj
, "%s",
455 rnbd_clt_err(dev
, "Failed to create device sysfs dir, err: %d\n",
457 kobject_put(&dev
->kobj
);
463 static ssize_t
rnbd_clt_map_device_show(struct kobject
*kobj
,
464 struct kobj_attribute
*attr
,
467 return scnprintf(page
, PAGE_SIZE
,
468 "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>]\" > %s\n\naddr ::= [ ip:<ipv4> | ip:<ipv6> | gid:<gid> ]\n",
472 static int rnbd_clt_get_path_name(struct rnbd_clt_dev
*dev
, char *buf
,
476 char pathname
[NAME_MAX
], *s
;
478 strlcpy(pathname
, dev
->pathname
, sizeof(pathname
));
479 while ((s
= strchr(pathname
, '/')))
482 ret
= snprintf(buf
, len
, "%s", pathname
);
484 return -ENAMETOOLONG
;
486 ret
= snprintf(buf
, len
, "%s@%s", buf
, dev
->sess
->sessname
);
488 return -ENAMETOOLONG
;
493 static int rnbd_clt_add_dev_symlink(struct rnbd_clt_dev
*dev
)
495 struct kobject
*gd_kobj
= &disk_to_dev(dev
->gd
)->kobj
;
498 len
= strlen(dev
->pathname
) + strlen(dev
->sess
->sessname
) + 2;
499 dev
->blk_symlink_name
= kzalloc(len
, GFP_KERNEL
);
500 if (!dev
->blk_symlink_name
) {
501 rnbd_clt_err(dev
, "Failed to allocate memory for blk_symlink_name\n");
505 ret
= rnbd_clt_get_path_name(dev
, dev
->blk_symlink_name
,
508 rnbd_clt_err(dev
, "Failed to get /sys/block symlink path, err: %d\n",
513 ret
= sysfs_create_link(rnbd_devs_kobj
, gd_kobj
,
514 dev
->blk_symlink_name
);
516 rnbd_clt_err(dev
, "Creating /sys/block symlink failed, err: %d\n",
524 kfree(dev
->blk_symlink_name
);
525 dev
->blk_symlink_name
= NULL
;
529 static ssize_t
rnbd_clt_map_device_store(struct kobject
*kobj
,
530 struct kobj_attribute
*attr
,
531 const char *buf
, size_t count
)
533 struct rnbd_clt_dev
*dev
;
534 struct rnbd_map_options opt
;
536 char pathname
[NAME_MAX
];
537 char sessname
[NAME_MAX
];
538 enum rnbd_access_mode access_mode
= RNBD_ACCESS_RW
;
539 u16 port_nr
= RTRS_PORT
;
541 struct sockaddr_storage
*addrs
;
542 struct rtrs_addr paths
[6];
545 opt
.sessname
= sessname
;
547 opt
.path_cnt
= &path_cnt
;
548 opt
.pathname
= pathname
;
549 opt
.dest_port
= &port_nr
;
550 opt
.access_mode
= &access_mode
;
551 addrs
= kcalloc(ARRAY_SIZE(paths
) * 2, sizeof(*addrs
), GFP_KERNEL
);
555 for (path_cnt
= 0; path_cnt
< ARRAY_SIZE(paths
); path_cnt
++) {
556 paths
[path_cnt
].src
= &addrs
[path_cnt
* 2];
557 paths
[path_cnt
].dst
= &addrs
[path_cnt
* 2 + 1];
560 ret
= rnbd_clt_parse_map_options(buf
, ARRAY_SIZE(paths
), &opt
);
564 pr_info("Mapping device %s on session %s, (access_mode: %s)\n",
566 rnbd_access_mode_str(access_mode
));
568 dev
= rnbd_clt_map_device(sessname
, paths
, path_cnt
, port_nr
, pathname
,
575 ret
= rnbd_clt_add_dev_kobj(dev
);
579 ret
= rnbd_clt_add_dev_symlink(dev
);
587 rnbd_clt_unmap_device(dev
, true, NULL
);
593 static struct kobj_attribute rnbd_clt_map_device_attr
=
594 __ATTR(map_device
, 0644,
595 rnbd_clt_map_device_show
, rnbd_clt_map_device_store
);
597 static struct attribute
*default_attrs
[] = {
598 &rnbd_clt_map_device_attr
.attr
,
602 static struct attribute_group default_attr_group
= {
603 .attrs
= default_attrs
,
606 static const struct attribute_group
*default_attr_groups
[] = {
611 int rnbd_clt_create_sysfs_files(void)
615 rnbd_dev_class
= class_create(THIS_MODULE
, "rnbd-client");
616 if (IS_ERR(rnbd_dev_class
))
617 return PTR_ERR(rnbd_dev_class
);
619 rnbd_dev
= device_create_with_groups(rnbd_dev_class
, NULL
,
621 default_attr_groups
, "ctl");
622 if (IS_ERR(rnbd_dev
)) {
623 err
= PTR_ERR(rnbd_dev
);
626 rnbd_devs_kobj
= kobject_create_and_add("devices", &rnbd_dev
->kobj
);
627 if (!rnbd_devs_kobj
) {
635 device_destroy(rnbd_dev_class
, MKDEV(0, 0));
637 class_destroy(rnbd_dev_class
);
642 void rnbd_clt_destroy_default_group(void)
644 sysfs_remove_group(&rnbd_dev
->kobj
, &default_attr_group
);
647 void rnbd_clt_destroy_sysfs_files(void)
649 kobject_del(rnbd_devs_kobj
);
650 kobject_put(rnbd_devs_kobj
);
651 device_destroy(rnbd_dev_class
, MKDEV(0, 0));
652 class_destroy(rnbd_dev_class
);