2 * Ceph - scalable distributed file system
4 * Copyright (C) 2014 Inktank Storage, Inc.
6 * This is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software
9 * Foundation. See file COPYING.
25 #include <sys/sysmacros.h>
26 #include <sys/types.h>
31 #include "auth/KeyRing.h"
32 #include "common/errno.h"
33 #include "common/Formatter.h"
34 #include "common/module.h"
35 #include "common/run_cmd.h"
36 #include "common/safe_io.h"
37 #include "common/secret.h"
38 #include "common/TextTable.h"
39 #include "common/Thread.h"
40 #include "include/ceph_assert.h"
41 #include "include/stringify.h"
42 #include "include/krbd.h"
43 #include "mon/MonMap.h"
45 #include <blkid/blkid.h>
46 #include <boost/algorithm/string/predicate.hpp>
49 static const int UDEV_BUF_SIZE
= 1 << 20; /* doubled to 2M (SO_RCVBUFFORCE) */
50 static const char DEVNODE_PREFIX
[] = "/dev/rbd";
51 static const char SNAP_HEAD_NAME
[] = "-";
53 #define DEFINE_UDEV_UPTR(what) \
54 struct udev_##what##_deleter { \
55 void operator()(udev_##what *p) { \
56 udev_##what##_unref(p); \
59 using udev_##what##_uptr = \
60 std::unique_ptr<udev_##what, udev_##what##_deleter>;
62 DEFINE_UDEV_UPTR(monitor
) /* udev_monitor_uptr */
63 DEFINE_UDEV_UPTR(enumerate
) /* udev_enumerate_uptr */
64 DEFINE_UDEV_UPTR(device
) /* udev_device_uptr */
72 std::string pool_name
;
73 std::string nspace_name
;
74 std::string image_name
;
75 std::string snap_name
;
77 krbd_spec(const char *pool_name
, const char *nspace_name
,
78 const char *image_name
, const char *snap_name
)
79 : pool_name(pool_name
),
80 nspace_name(nspace_name
),
81 image_name(image_name
),
82 snap_name(*snap_name
? snap_name
: SNAP_HEAD_NAME
) { }
84 bool operator==(const krbd_spec
& rhs
) const {
85 return pool_name
== rhs
.pool_name
&&
86 nspace_name
== rhs
.nspace_name
&&
87 image_name
== rhs
.image_name
&&
88 snap_name
== rhs
.snap_name
;
92 static std::ostream
& operator<<(std::ostream
& os
, const krbd_spec
& spec
)
94 os
<< spec
.pool_name
<< "/";
95 if (!spec
.nspace_name
.empty())
96 os
<< spec
.nspace_name
<< "/";
97 os
<< spec
.image_name
;
98 if (spec
.snap_name
!= SNAP_HEAD_NAME
)
99 os
<< "@" << spec
.snap_name
;
103 static std::optional
<krbd_spec
> spec_from_dev(udev_device
*dev
)
105 const char *pool_name
= udev_device_get_sysattr_value(dev
, "pool");
106 const char *nspace_name
= udev_device_get_sysattr_value(dev
, "pool_ns");
107 const char *image_name
= udev_device_get_sysattr_value(dev
, "name");
108 const char *snap_name
= udev_device_get_sysattr_value(dev
, "current_snap");
110 if (!pool_name
|| !image_name
|| !snap_name
)
113 return std::make_optional
<krbd_spec
>(
114 pool_name
, nspace_name
?: "", image_name
, snap_name
);
117 static udev_device_uptr
dev_from_list_entry(udev
*udev
, udev_list_entry
*l
)
119 return udev_device_uptr(
120 udev_device_new_from_syspath(udev
, udev_list_entry_get_name(l
)));
123 static std::string
get_devnode(udev_device
*dev
)
125 std::string devnode
= DEVNODE_PREFIX
;
126 devnode
+= udev_device_get_sysname(dev
);
130 static int sysfs_write_rbd(const char *which
, const string
& buf
)
132 const string s
= string("/sys/bus/rbd/") + which
;
133 const string t
= s
+ "_single_major";
138 * 'add' and 'add_single_major' interfaces are identical, but if rbd
139 * kernel module is new enough and is configured to use single-major
140 * scheme, 'add' is disabled in order to prevent old userspace from
141 * doing weird things at unmap time.
143 * Same goes for 'remove' vs 'remove_single_major'.
145 fd
= open(t
.c_str(), O_WRONLY
);
147 if (errno
== ENOENT
) {
148 fd
= open(s
.c_str(), O_WRONLY
);
156 r
= safe_write(fd
, buf
.c_str(), buf
.size());
162 static int sysfs_write_rbd_add(const string
& buf
)
164 return sysfs_write_rbd("add", buf
);
167 static int sysfs_write_rbd_remove(const string
& buf
)
169 return sysfs_write_rbd("remove", buf
);
172 static int have_minor_attr(void)
175 * 'minor' attribute was added as part of single_major merge, which
176 * exposed the 'single_major' parameter. 'minor' is always present,
177 * regardless of whether single-major scheme is turned on or not.
179 * (Something like ver >= KERNEL_VERSION(3, 14, 0) is a no-go because
180 * this has to work with rbd.ko backported to various kernels.)
182 return access("/sys/module/rbd/parameters/single_major", F_OK
) == 0;
185 static int build_map_buf(CephContext
*cct
, const krbd_spec
& spec
,
186 const char *options
, string
*pbuf
)
192 r
= monmap
.build_initial(cct
, false, cerr
);
196 list
<entity_addr_t
> mon_addr
;
197 monmap
.list_addrs(mon_addr
);
199 for (const auto &p
: mon_addr
) {
200 if (oss
.tellp() > 0) {
203 oss
<< p
.get_sockaddr();
206 oss
<< " name=" << cct
->_conf
->name
.get_id();
209 auto auth_client_required
=
210 cct
->_conf
.get_val
<std::string
>("auth_client_required");
211 if (auth_client_required
!= "none") {
212 r
= keyring
.from_ceph_context(cct
);
213 auto keyfile
= cct
->_conf
.get_val
<std::string
>("keyfile");
214 auto key
= cct
->_conf
.get_val
<std::string
>("key");
215 if (r
== -ENOENT
&& keyfile
.empty() && key
.empty())
218 cerr
<< "rbd: failed to get secret" << std::endl
;
224 string key_name
= string("client.") + cct
->_conf
->name
.get_id();
225 if (keyring
.get_secret(cct
->_conf
->name
, secret
)) {
227 secret
.encode_base64(secret_str
);
229 r
= set_kernel_secret(secret_str
.c_str(), key_name
.c_str());
232 cerr
<< "rbd: warning: secret has length 0" << std::endl
;
233 oss
<< ",key=" << key_name
;
234 } else if (r
== -ENODEV
|| r
== -ENOSYS
) {
235 // running against older kernel; fall back to secret= in options
236 oss
<< ",secret=" << secret_str
;
238 cerr
<< "rbd: failed to add secret '" << key_name
<< "' to kernel"
242 } else if (is_kernel_secret(key_name
.c_str())) {
243 oss
<< ",key=" << key_name
;
246 if (strcmp(options
, "") != 0)
247 oss
<< "," << options
;
248 if (!spec
.nspace_name
.empty())
249 oss
<< ",_pool_ns=" << spec
.nspace_name
;
251 oss
<< " " << spec
.pool_name
<< " " << spec
.image_name
<< " "
260 * <kernel error, false> - didn't map
261 * <0 or udev error, true> - mapped
263 template <typename F
>
264 static std::pair
<int, bool> wait_for_mapping(int sysfs_r_fd
, udev_monitor
*mon
,
265 F udev_device_handler
)
267 struct pollfd fds
[2];
268 int sysfs_r
= INT_MAX
, udev_r
= INT_MAX
;
271 fds
[0].fd
= sysfs_r_fd
;
272 fds
[0].events
= POLLIN
;
273 fds
[1].fd
= udev_monitor_get_fd(mon
);
274 fds
[1].events
= POLLIN
;
277 if (poll(fds
, 2, -1) < 0) {
278 ceph_abort_msgf("poll failed: %d", -errno
);
281 if (fds
[0].revents
) {
282 r
= safe_read_exact(sysfs_r_fd
, &sysfs_r
, sizeof(sysfs_r
));
284 ceph_abort_msgf("safe_read_exact failed: %d", r
);
287 return std::make_pair(sysfs_r
, false);
289 if (udev_r
!= INT_MAX
) {
290 ceph_assert(!sysfs_r
);
291 return std::make_pair(udev_r
, true);
296 if (fds
[1].revents
) {
298 udev_device_uptr
dev(udev_monitor_receive_device(mon
));
300 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
302 if (sysfs_r
!= INT_MAX
) {
303 ceph_assert(!sysfs_r
);
304 return std::make_pair(udev_r
, true);
310 if (udev_device_handler(std::move(dev
))) {
312 if (sysfs_r
!= INT_MAX
) {
313 ceph_assert(!sysfs_r
);
314 return std::make_pair(udev_r
, true);
324 class UdevMapHandler
{
326 UdevMapHandler(const krbd_spec
*spec
, std::string
*pdevnode
) :
327 m_spec(spec
), m_pdevnode(pdevnode
) {}
330 * Catch /sys/devices/rbd/<id>/ and wait for the corresponding
331 * block device to show up. This is necessary because rbd devices
332 * and block devices aren't linked together in our sysfs layout.
334 * Note that our "block" event can come before the "rbd" event, so
335 * all potential "block" events are gathered in m_block_devs before
336 * m_bus_dev is caught.
338 bool operator()(udev_device_uptr dev
) {
339 if (strcmp(udev_device_get_action(dev
.get()), "add")) {
342 if (!strcmp(udev_device_get_subsystem(dev
.get()), "rbd")) {
344 auto spec
= spec_from_dev(dev
.get());
345 if (spec
&& *spec
== *m_spec
) {
346 m_bus_dev
= std::move(dev
);
347 m_devnode
= get_devnode(m_bus_dev
.get());
350 } else if (!strcmp(udev_device_get_subsystem(dev
.get()), "block")) {
351 if (boost::starts_with(udev_device_get_devnode(dev
.get()),
353 m_block_devs
.push_back(std::move(dev
));
357 if (m_bus_dev
&& !m_block_devs
.empty()) {
358 for (const auto& p
: m_block_devs
) {
359 if (udev_device_get_devnode(p
.get()) == m_devnode
) {
361 udev_device_get_sysattr_value(m_bus_dev
.get(), "major"),
362 udev_device_get_property_value(p
.get(), "MAJOR")));
363 ceph_assert(!have_minor_attr() || !strcmp(
364 udev_device_get_sysattr_value(m_bus_dev
.get(), "minor"),
365 udev_device_get_property_value(p
.get(), "MINOR")));
366 *m_pdevnode
= std::move(m_devnode
);
370 m_block_devs
.clear();
376 udev_device_uptr m_bus_dev
;
377 std::vector
<udev_device_uptr
> m_block_devs
;
378 std::string m_devnode
;
379 const krbd_spec
*m_spec
;
380 std::string
*m_pdevnode
;
383 static int do_map(struct udev
*udev
, const krbd_spec
& spec
, const string
& buf
,
390 udev_monitor_uptr
mon(udev_monitor_new_from_netlink(udev
, "udev"));
394 r
= udev_monitor_filter_add_match_subsystem_devtype(mon
.get(), "rbd",
399 r
= udev_monitor_filter_add_match_subsystem_devtype(mon
.get(), "block",
404 r
= udev_monitor_set_receive_buffer_size(mon
.get(), UDEV_BUF_SIZE
);
406 std::cerr
<< "rbd: failed to set udev buffer size: " << cpp_strerror(r
)
411 r
= udev_monitor_enable_receiving(mon
.get());
415 if (pipe2(fds
, O_NONBLOCK
) < 0)
418 auto mapper
= make_named_thread("mapper", [&buf
, sysfs_r_fd
= fds
[1]]() {
419 int sysfs_r
= sysfs_write_rbd_add(buf
);
420 int r
= safe_write(sysfs_r_fd
, &sysfs_r
, sizeof(sysfs_r
));
422 ceph_abort_msgf("safe_write failed: %d", r
);
426 std::tie(r
, mapped
) = wait_for_mapping(fds
[0], mon
.get(),
427 UdevMapHandler(&spec
, pname
));
430 std::cerr
<< "rbd: sysfs write failed" << std::endl
;
432 std::cerr
<< "rbd: udev wait failed" << std::endl
;
433 /* TODO: fall back to enumeration */
443 static int map_image(struct krbd_ctx
*ctx
, const krbd_spec
& spec
,
444 const char *options
, string
*pname
)
450 * Modprobe rbd kernel module. If it supports single-major device
451 * number allocation scheme, make sure it's turned on.
453 * Do this before calling build_map_buf() - it wants "ceph" key type
456 if (access("/sys/bus/rbd", F_OK
) != 0) {
457 const char *module_options
= NULL
;
458 if (module_has_param("rbd", "single_major"))
459 module_options
= "single_major=Y";
461 r
= module_load("rbd", module_options
);
463 cerr
<< "rbd: failed to load rbd kernel module (" << r
<< ")"
466 * Ignore the error: modprobe failing doesn't necessarily prevent
472 r
= build_map_buf(ctx
->cct
, spec
, options
, &buf
);
476 return do_map(ctx
->udev
, spec
, buf
, pname
);
479 static int devno_to_krbd_id(struct udev
*udev
, dev_t devno
, string
*pid
)
481 udev_enumerate_uptr enm
;
482 struct udev_list_entry
*l
;
486 enm
.reset(udev_enumerate_new(udev
));
490 r
= udev_enumerate_add_match_subsystem(enm
.get(), "rbd");
494 r
= udev_enumerate_add_match_sysattr(enm
.get(), "major",
495 stringify(major(devno
)).c_str());
499 if (have_minor_attr()) {
500 r
= udev_enumerate_add_match_sysattr(enm
.get(), "minor",
501 stringify(minor(devno
)).c_str());
506 r
= udev_enumerate_scan_devices(enm
.get());
508 if (r
== -ENOENT
|| r
== -ENODEV
) {
509 std::cerr
<< "rbd: udev enumerate failed, retrying" << std::endl
;
515 l
= udev_enumerate_get_list_entry(enm
.get());
519 /* make sure there is only one match */
520 ceph_assert(!udev_list_entry_get_next(l
));
522 auto dev
= dev_from_list_entry(udev
, l
);
526 *pid
= udev_device_get_sysname(dev
.get());
530 static int __enumerate_devices(struct udev
*udev
, const krbd_spec
& spec
,
531 bool match_nspace
, udev_enumerate_uptr
*penm
)
533 udev_enumerate_uptr enm
;
537 enm
.reset(udev_enumerate_new(udev
));
541 r
= udev_enumerate_add_match_subsystem(enm
.get(), "rbd");
545 r
= udev_enumerate_add_match_sysattr(enm
.get(), "pool",
546 spec
.pool_name
.c_str());
551 r
= udev_enumerate_add_match_sysattr(enm
.get(), "pool_ns",
552 spec
.nspace_name
.c_str());
555 * Match _only_ devices that don't have pool_ns attribute.
556 * If the kernel supports namespaces, the result will be empty.
558 r
= udev_enumerate_add_nomatch_sysattr(enm
.get(), "pool_ns", nullptr);
563 r
= udev_enumerate_add_match_sysattr(enm
.get(), "name",
564 spec
.image_name
.c_str());
568 r
= udev_enumerate_add_match_sysattr(enm
.get(), "current_snap",
569 spec
.snap_name
.c_str());
573 r
= udev_enumerate_scan_devices(enm
.get());
575 if (r
== -ENOENT
|| r
== -ENODEV
) {
576 std::cerr
<< "rbd: udev enumerate failed, retrying" << std::endl
;
582 *penm
= std::move(enm
);
586 static int enumerate_devices(struct udev
*udev
, const krbd_spec
& spec
,
587 udev_enumerate_uptr
*penm
)
589 udev_enumerate_uptr enm
;
592 r
= __enumerate_devices(udev
, spec
, true, &enm
);
597 * If no namespace is set, try again with match_nspace=false to
598 * handle older kernels. On a newer kernel the result will remain
599 * the same (i.e. empty).
601 if (!udev_enumerate_get_list_entry(enm
.get()) && spec
.nspace_name
.empty()) {
602 r
= __enumerate_devices(udev
, spec
, false, &enm
);
607 *penm
= std::move(enm
);
611 static int spec_to_devno_and_krbd_id(struct udev
*udev
, const krbd_spec
& spec
,
612 dev_t
*pdevno
, string
*pid
)
614 udev_enumerate_uptr enm
;
615 struct udev_list_entry
*l
;
616 unsigned int maj
, min
= 0;
620 r
= enumerate_devices(udev
, spec
, &enm
);
624 l
= udev_enumerate_get_list_entry(enm
.get());
628 auto dev
= dev_from_list_entry(udev
, l
);
632 maj
= strict_strtoll(udev_device_get_sysattr_value(dev
.get(), "major"), 10,
635 cerr
<< "rbd: couldn't parse major: " << err
<< std::endl
;
638 if (have_minor_attr()) {
639 min
= strict_strtoll(udev_device_get_sysattr_value(dev
.get(), "minor"), 10,
642 cerr
<< "rbd: couldn't parse minor: " << err
<< std::endl
;
648 * If an image is mapped more than once don't bother trying to unmap
649 * all devices - let users run unmap the same number of times they
652 if (udev_list_entry_get_next(l
))
653 cerr
<< "rbd: " << spec
<< ": mapped more than once, unmapping "
654 << get_devnode(dev
.get()) << " only" << std::endl
;
656 *pdevno
= makedev(maj
, min
);
657 *pid
= udev_device_get_sysname(dev
.get());
661 static void append_unmap_options(std::string
*buf
, const char *options
)
663 if (strcmp(options
, "") != 0) {
669 class UdevUnmapHandler
{
671 UdevUnmapHandler(dev_t devno
) : m_devno(devno
) {}
673 bool operator()(udev_device_uptr dev
) {
674 if (strcmp(udev_device_get_action(dev
.get()), "remove")) {
677 return udev_device_get_devnum(dev
.get()) == m_devno
;
684 static int do_unmap(struct udev
*udev
, dev_t devno
, const string
& buf
)
690 udev_monitor_uptr
mon(udev_monitor_new_from_netlink(udev
, "udev"));
694 r
= udev_monitor_filter_add_match_subsystem_devtype(mon
.get(), "block",
699 r
= udev_monitor_set_receive_buffer_size(mon
.get(), UDEV_BUF_SIZE
);
701 std::cerr
<< "rbd: failed to set udev buffer size: " << cpp_strerror(r
)
706 r
= udev_monitor_enable_receiving(mon
.get());
710 if (pipe2(fds
, O_NONBLOCK
) < 0)
713 auto unmapper
= make_named_thread("unmapper", [&buf
, sysfs_r_fd
= fds
[1]]() {
715 * On final device close(), kernel sends a block change event, in
716 * response to which udev apparently runs blkid on the device. This
717 * makes unmap fail with EBUSY, if issued right after final close().
718 * Try to circumvent this with a retry before turning to udev.
720 for (int tries
= 0; ; tries
++) {
721 int sysfs_r
= sysfs_write_rbd_remove(buf
);
722 if (sysfs_r
== -EBUSY
&& tries
< 2) {
727 * libudev does not provide the "wait until the queue is empty"
728 * API or the sufficient amount of primitives to build it from.
730 std::string err
= run_cmd("udevadm", "settle", "--timeout", "10",
733 std::cerr
<< "rbd: " << err
<< std::endl
;
736 int r
= safe_write(sysfs_r_fd
, &sysfs_r
, sizeof(sysfs_r
));
738 ceph_abort_msgf("safe_write failed: %d", r
);
745 std::tie(r
, unmapped
) = wait_for_mapping(fds
[0], mon
.get(),
746 UdevUnmapHandler(devno
));
749 std::cerr
<< "rbd: sysfs write failed" << std::endl
;
751 std::cerr
<< "rbd: udev wait failed: " << cpp_strerror(r
) << std::endl
;
762 static int unmap_image(struct krbd_ctx
*ctx
, const char *devnode
,
766 dev_t wholedevno
= 0;
770 if (stat(devnode
, &sb
) < 0 || !S_ISBLK(sb
.st_mode
)) {
771 cerr
<< "rbd: '" << devnode
<< "' is not a block device" << std::endl
;
775 r
= blkid_devno_to_wholedisk(sb
.st_rdev
, NULL
, 0, &wholedevno
);
777 cerr
<< "rbd: couldn't compute wholedevno: " << cpp_strerror(r
)
780 * Ignore the error: we are given whole disks most of the time, and
781 * if it turns out this is a partition we will fail later anyway.
783 wholedevno
= sb
.st_rdev
;
786 for (int tries
= 0; ; tries
++) {
787 r
= devno_to_krbd_id(ctx
->udev
, wholedevno
, &buf
);
788 if (r
== -ENOENT
&& tries
< 2) {
793 std::cerr
<< "rbd: '" << devnode
<< "' is not an rbd device"
800 std::cerr
<< "rbd: udev enumerate missed a device, tries = " << tries
807 append_unmap_options(&buf
, options
);
808 return do_unmap(ctx
->udev
, wholedevno
, buf
);
811 static int unmap_image(struct krbd_ctx
*ctx
, const krbd_spec
& spec
,
818 for (int tries
= 0; ; tries
++) {
819 r
= spec_to_devno_and_krbd_id(ctx
->udev
, spec
, &devno
, &buf
);
820 if (r
== -ENOENT
&& tries
< 2) {
825 std::cerr
<< "rbd: " << spec
<< ": not a mapped image or snapshot"
832 std::cerr
<< "rbd: udev enumerate missed a device, tries = " << tries
839 append_unmap_options(&buf
, options
);
840 return do_unmap(ctx
->udev
, devno
, buf
);
843 static bool dump_one_image(Formatter
*f
, TextTable
*tbl
,
844 struct udev_device
*dev
)
846 auto spec
= spec_from_dev(dev
);
847 std::string devnode
= get_devnode(dev
);
848 const char *id
= devnode
.c_str() + sizeof(DEVNODE_PREFIX
) - 1;
854 f
->open_object_section("device");
855 f
->dump_string("id", id
);
856 f
->dump_string("pool", spec
->pool_name
);
857 f
->dump_string("namespace", spec
->nspace_name
);
858 f
->dump_string("name", spec
->image_name
);
859 f
->dump_string("snap", spec
->snap_name
);
860 f
->dump_string("device", devnode
);
863 *tbl
<< id
<< spec
->pool_name
<< spec
->nspace_name
<< spec
->image_name
864 << spec
->snap_name
<< devnode
<< TextTable::endrow
;
870 static int do_dump(struct udev
*udev
, Formatter
*f
, TextTable
*tbl
)
872 udev_enumerate_uptr enm
;
873 struct udev_list_entry
*l
= NULL
;
874 bool have_output
= false;
878 enm
.reset(udev_enumerate_new(udev
));
882 r
= udev_enumerate_add_match_subsystem(enm
.get(), "rbd");
886 r
= udev_enumerate_scan_devices(enm
.get());
888 if (r
== -ENOENT
|| r
== -ENODEV
) {
889 std::cerr
<< "rbd: udev enumerate failed, retrying" << std::endl
;
895 udev_list_entry_foreach(l
, udev_enumerate_get_list_entry(enm
.get())) {
896 auto dev
= dev_from_list_entry(udev
, l
);
898 have_output
|= dump_one_image(f
, tbl
, dev
.get());
905 static int dump_images(struct krbd_ctx
*ctx
, Formatter
*f
)
911 f
->open_array_section("devices");
913 tbl
.define_column("id", TextTable::LEFT
, TextTable::LEFT
);
914 tbl
.define_column("pool", TextTable::LEFT
, TextTable::LEFT
);
915 tbl
.define_column("namespace", TextTable::LEFT
, TextTable::LEFT
);
916 tbl
.define_column("image", TextTable::LEFT
, TextTable::LEFT
);
917 tbl
.define_column("snap", TextTable::LEFT
, TextTable::LEFT
);
918 tbl
.define_column("device", TextTable::LEFT
, TextTable::LEFT
);
921 r
= do_dump(ctx
->udev
, f
, &tbl
);
934 static int is_mapped_image(struct udev
*udev
, const krbd_spec
& spec
,
937 udev_enumerate_uptr enm
;
938 struct udev_list_entry
*l
;
941 r
= enumerate_devices(udev
, spec
, &enm
);
945 l
= udev_enumerate_get_list_entry(enm
.get());
947 auto dev
= dev_from_list_entry(udev
, l
);
951 *pname
= get_devnode(dev
.get());
955 return 0; /* not mapped */
958 extern "C" int krbd_create_from_context(rados_config_t cct
,
959 struct krbd_ctx
**pctx
)
961 struct krbd_ctx
*ctx
= new struct krbd_ctx();
963 ctx
->cct
= reinterpret_cast<CephContext
*>(cct
);
964 ctx
->udev
= udev_new();
974 extern "C" void krbd_destroy(struct krbd_ctx
*ctx
)
979 udev_unref(ctx
->udev
);
984 extern "C" int krbd_map(struct krbd_ctx
*ctx
,
985 const char *pool_name
,
986 const char *nspace_name
,
987 const char *image_name
,
988 const char *snap_name
,
992 krbd_spec
spec(pool_name
, nspace_name
, image_name
, snap_name
);
997 r
= map_image(ctx
, spec
, options
, &name
);
1001 devnode
= strdup(name
.c_str());
1005 *pdevnode
= devnode
;
1009 extern "C" int krbd_unmap(struct krbd_ctx
*ctx
, const char *devnode
,
1010 const char *options
)
1012 return unmap_image(ctx
, devnode
, options
);
1015 extern "C" int krbd_unmap_by_spec(struct krbd_ctx
*ctx
,
1016 const char *pool_name
,
1017 const char *nspace_name
,
1018 const char *image_name
,
1019 const char *snap_name
,
1020 const char *options
)
1022 krbd_spec
spec(pool_name
, nspace_name
, image_name
, snap_name
);
1023 return unmap_image(ctx
, spec
, options
);
1026 int krbd_showmapped(struct krbd_ctx
*ctx
, Formatter
*f
)
1028 return dump_images(ctx
, f
);
1031 extern "C" int krbd_is_mapped(struct krbd_ctx
*ctx
,
1032 const char *pool_name
,
1033 const char *nspace_name
,
1034 const char *image_name
,
1035 const char *snap_name
,
1038 krbd_spec
spec(pool_name
, nspace_name
, image_name
, snap_name
);
1043 r
= is_mapped_image(ctx
->udev
, spec
, &name
);
1044 if (r
<= 0) /* error or not mapped */
1047 devnode
= strdup(name
.c_str());
1051 *pdevnode
= devnode
;