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>
47 #include <boost/tokenizer.hpp>
50 static const int UDEV_BUF_SIZE
= 1 << 20; /* doubled to 2M (SO_RCVBUFFORCE) */
51 static const char DEVNODE_PREFIX
[] = "/dev/rbd";
52 static const char SNAP_HEAD_NAME
[] = "-";
54 #define DEFINE_UDEV_UPTR(what) \
55 struct udev_##what##_deleter { \
56 void operator()(udev_##what *p) { \
57 udev_##what##_unref(p); \
60 using udev_##what##_uptr = \
61 std::unique_ptr<udev_##what, udev_##what##_deleter>;
63 DEFINE_UDEV_UPTR(monitor
) /* udev_monitor_uptr */
64 DEFINE_UDEV_UPTR(enumerate
) /* udev_enumerate_uptr */
65 DEFINE_UDEV_UPTR(device
) /* udev_device_uptr */
70 uint32_t flags
; /* KRBD_CTX_F_* */
74 std::string pool_name
;
75 std::string nspace_name
;
76 std::string image_name
;
77 std::string snap_name
;
79 krbd_spec(const char *pool_name
, const char *nspace_name
,
80 const char *image_name
, const char *snap_name
)
81 : pool_name(pool_name
),
82 nspace_name(nspace_name
),
83 image_name(image_name
),
84 snap_name(*snap_name
? snap_name
: SNAP_HEAD_NAME
) { }
86 bool operator==(const krbd_spec
& rhs
) const {
87 return pool_name
== rhs
.pool_name
&&
88 nspace_name
== rhs
.nspace_name
&&
89 image_name
== rhs
.image_name
&&
90 snap_name
== rhs
.snap_name
;
94 static std::ostream
& operator<<(std::ostream
& os
, const krbd_spec
& spec
)
96 os
<< spec
.pool_name
<< "/";
97 if (!spec
.nspace_name
.empty())
98 os
<< spec
.nspace_name
<< "/";
99 os
<< spec
.image_name
;
100 if (spec
.snap_name
!= SNAP_HEAD_NAME
)
101 os
<< "@" << spec
.snap_name
;
105 static std::optional
<krbd_spec
> spec_from_dev(udev_device
*dev
)
107 const char *pool_name
= udev_device_get_sysattr_value(dev
, "pool");
108 const char *nspace_name
= udev_device_get_sysattr_value(dev
, "pool_ns");
109 const char *image_name
= udev_device_get_sysattr_value(dev
, "name");
110 const char *snap_name
= udev_device_get_sysattr_value(dev
, "current_snap");
112 if (!pool_name
|| !image_name
|| !snap_name
)
115 return std::make_optional
<krbd_spec
>(
116 pool_name
, nspace_name
?: "", image_name
, snap_name
);
119 static udev_device_uptr
dev_from_list_entry(udev
*udev
, udev_list_entry
*l
)
121 return udev_device_uptr(
122 udev_device_new_from_syspath(udev
, udev_list_entry_get_name(l
)));
125 static std::string
get_devnode(udev_device
*dev
)
127 std::string devnode
= DEVNODE_PREFIX
;
128 devnode
+= udev_device_get_sysname(dev
);
132 static int sysfs_write_rbd(const char *which
, const string
& buf
)
134 const string s
= string("/sys/bus/rbd/") + which
;
135 const string t
= s
+ "_single_major";
140 * 'add' and 'add_single_major' interfaces are identical, but if rbd
141 * kernel module is new enough and is configured to use single-major
142 * scheme, 'add' is disabled in order to prevent old userspace from
143 * doing weird things at unmap time.
145 * Same goes for 'remove' vs 'remove_single_major'.
147 fd
= open(t
.c_str(), O_WRONLY
);
149 if (errno
== ENOENT
) {
150 fd
= open(s
.c_str(), O_WRONLY
);
158 r
= safe_write(fd
, buf
.c_str(), buf
.size());
164 static int sysfs_write_rbd_add(const string
& buf
)
166 return sysfs_write_rbd("add", buf
);
169 static int sysfs_write_rbd_remove(const string
& buf
)
171 return sysfs_write_rbd("remove", buf
);
174 static int have_minor_attr(void)
177 * 'minor' attribute was added as part of single_major merge, which
178 * exposed the 'single_major' parameter. 'minor' is always present,
179 * regardless of whether single-major scheme is turned on or not.
181 * (Something like ver >= KERNEL_VERSION(3, 14, 0) is a no-go because
182 * this has to work with rbd.ko backported to various kernels.)
184 return access("/sys/module/rbd/parameters/single_major", F_OK
) == 0;
187 static int build_map_buf(CephContext
*cct
, const krbd_spec
& spec
,
188 const string
& options
, string
*pbuf
)
194 boost::char_separator
<char> sep(",");
195 boost::tokenizer
<boost::char_separator
<char>> tok(options
, sep
);
196 for (const auto& t
: tok
) {
197 if (boost::starts_with(t
, "ms_mode=")) {
198 /* msgr2 unless ms_mode=legacy */
199 msgr2
= t
.compare(8, t
.npos
, "legacy");
204 r
= monmap
.build_initial(cct
, false, cerr
);
209 * If msgr2, filter TYPE_MSGR2 addresses. Otherwise, filter
210 * TYPE_LEGACY addresses.
212 for (const auto& p
: monmap
.mon_info
) {
213 for (const auto& a
: p
.second
.public_addrs
.v
) {
214 if ((msgr2
&& a
.is_msgr2()) || (!msgr2
&& a
.is_legacy())) {
215 if (oss
.tellp() > 0) {
218 oss
<< a
.get_sockaddr();
223 oss
<< " name=" << cct
->_conf
->name
.get_id();
226 auto auth_client_required
=
227 cct
->_conf
.get_val
<std::string
>("auth_client_required");
228 if (auth_client_required
!= "none") {
229 r
= keyring
.from_ceph_context(cct
);
230 auto keyfile
= cct
->_conf
.get_val
<std::string
>("keyfile");
231 auto key
= cct
->_conf
.get_val
<std::string
>("key");
232 if (r
== -ENOENT
&& keyfile
.empty() && key
.empty())
235 cerr
<< "rbd: failed to get secret" << std::endl
;
241 string key_name
= string("client.") + cct
->_conf
->name
.get_id();
242 if (keyring
.get_secret(cct
->_conf
->name
, secret
)) {
244 secret
.encode_base64(secret_str
);
246 r
= set_kernel_secret(secret_str
.c_str(), key_name
.c_str());
249 cerr
<< "rbd: warning: secret has length 0" << std::endl
;
250 oss
<< ",key=" << key_name
;
251 } else if (r
== -ENODEV
|| r
== -ENOSYS
) {
252 // running against older kernel; fall back to secret= in options
253 oss
<< ",secret=" << secret_str
;
255 cerr
<< "rbd: failed to add secret '" << key_name
<< "' to kernel"
259 } else if (is_kernel_secret(key_name
.c_str())) {
260 oss
<< ",key=" << key_name
;
263 if (!options
.empty())
264 oss
<< "," << options
;
265 if (!spec
.nspace_name
.empty())
266 oss
<< ",_pool_ns=" << spec
.nspace_name
;
268 oss
<< " " << spec
.pool_name
<< " " << spec
.image_name
<< " "
277 * <kernel error, false> - didn't map
278 * <0 or udev error, true> - mapped
280 template <typename F
>
281 static std::pair
<int, bool> wait_for_mapping(int sysfs_r_fd
, udev_monitor
*mon
,
282 F udev_device_handler
)
284 struct pollfd fds
[2];
285 int sysfs_r
= INT_MAX
, udev_r
= INT_MAX
;
288 fds
[0].fd
= sysfs_r_fd
;
289 fds
[0].events
= POLLIN
;
290 fds
[1].fd
= udev_monitor_get_fd(mon
);
291 fds
[1].events
= POLLIN
;
294 if (poll(fds
, 2, -1) < 0) {
295 ceph_abort_msgf("poll failed: %d", -errno
);
298 if (fds
[0].revents
) {
299 r
= safe_read_exact(sysfs_r_fd
, &sysfs_r
, sizeof(sysfs_r
));
301 ceph_abort_msgf("safe_read_exact failed: %d", r
);
304 return std::make_pair(sysfs_r
, false);
306 if (udev_r
!= INT_MAX
) {
307 ceph_assert(!sysfs_r
);
308 return std::make_pair(udev_r
, true);
313 if (fds
[1].revents
) {
315 udev_device_uptr
dev(udev_monitor_receive_device(mon
));
317 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
319 if (sysfs_r
!= INT_MAX
) {
320 ceph_assert(!sysfs_r
);
321 return std::make_pair(udev_r
, true);
327 if (udev_device_handler(std::move(dev
))) {
329 if (sysfs_r
!= INT_MAX
) {
330 ceph_assert(!sysfs_r
);
331 return std::make_pair(udev_r
, true);
341 class UdevMapHandler
{
343 UdevMapHandler(const krbd_spec
*spec
, std::string
*pdevnode
) :
344 m_spec(spec
), m_pdevnode(pdevnode
) {}
347 * Catch /sys/devices/rbd/<id>/ and wait for the corresponding
348 * block device to show up. This is necessary because rbd devices
349 * and block devices aren't linked together in our sysfs layout.
351 * Note that our "block" event can come before the "rbd" event, so
352 * all potential "block" events are gathered in m_block_devs before
353 * m_bus_dev is caught.
355 bool operator()(udev_device_uptr dev
) {
356 if (strcmp(udev_device_get_action(dev
.get()), "add")) {
359 if (!strcmp(udev_device_get_subsystem(dev
.get()), "rbd")) {
361 auto spec
= spec_from_dev(dev
.get());
362 if (spec
&& *spec
== *m_spec
) {
363 m_bus_dev
= std::move(dev
);
364 m_devnode
= get_devnode(m_bus_dev
.get());
367 } else if (!strcmp(udev_device_get_subsystem(dev
.get()), "block")) {
368 if (boost::starts_with(udev_device_get_devnode(dev
.get()),
370 m_block_devs
.push_back(std::move(dev
));
374 if (m_bus_dev
&& !m_block_devs
.empty()) {
375 for (const auto& p
: m_block_devs
) {
376 if (udev_device_get_devnode(p
.get()) == m_devnode
) {
378 udev_device_get_sysattr_value(m_bus_dev
.get(), "major"),
379 udev_device_get_property_value(p
.get(), "MAJOR")));
380 ceph_assert(!have_minor_attr() || !strcmp(
381 udev_device_get_sysattr_value(m_bus_dev
.get(), "minor"),
382 udev_device_get_property_value(p
.get(), "MINOR")));
383 *m_pdevnode
= std::move(m_devnode
);
387 m_block_devs
.clear();
393 udev_device_uptr m_bus_dev
;
394 std::vector
<udev_device_uptr
> m_block_devs
;
395 std::string m_devnode
;
396 const krbd_spec
*m_spec
;
397 std::string
*m_pdevnode
;
400 static const char *get_event_source(const krbd_ctx
*ctx
)
402 if (ctx
->flags
& KRBD_CTX_F_NOUDEV
) {
404 * For block devices (unlike network interfaces, they don't
405 * carry any namespace tags), the kernel broadcasts uevents
406 * into all network namespaces that are owned by the initial
407 * user namespace. This restriction is new in 4.18: starting
408 * with 2.6.35 and through 4.17 the kernel broadcast uevents
409 * into all network namespaces, period.
411 * However, when invoked from a non-initial user namespace,
412 * udev_monitor_receive_device() has always ignored both kernel
413 * and udev uevents by virtue of requiring SCM_CREDENTIALS and
414 * checking that ucred->uid == 0. When UIDs and GIDs are sent to
415 * a process in a user namespace, they are translated according
416 * to that process's UID and GID mappings and, unless root in the
417 * user namespace is mapped to the global root, that check fails.
418 * Normally they show up as 65534(nobody) because the global root
425 * Like most netlink messages, udev uevents don't cross network
426 * namespace boundaries and are therefore confined to the initial
432 static int do_map(krbd_ctx
*ctx
, const krbd_spec
& spec
, const string
& buf
,
439 udev_monitor_uptr
mon(udev_monitor_new_from_netlink(ctx
->udev
,
440 get_event_source(ctx
)));
444 r
= udev_monitor_filter_add_match_subsystem_devtype(mon
.get(), "rbd",
449 r
= udev_monitor_filter_add_match_subsystem_devtype(mon
.get(), "block",
454 r
= udev_monitor_set_receive_buffer_size(mon
.get(), UDEV_BUF_SIZE
);
456 std::cerr
<< "rbd: failed to set udev buffer size: " << cpp_strerror(r
)
461 r
= udev_monitor_enable_receiving(mon
.get());
465 if (pipe2(fds
, O_NONBLOCK
) < 0)
468 auto mapper
= make_named_thread("mapper", [&buf
, sysfs_r_fd
= fds
[1]]() {
469 int sysfs_r
= sysfs_write_rbd_add(buf
);
470 int r
= safe_write(sysfs_r_fd
, &sysfs_r
, sizeof(sysfs_r
));
472 ceph_abort_msgf("safe_write failed: %d", r
);
476 std::tie(r
, mapped
) = wait_for_mapping(fds
[0], mon
.get(),
477 UdevMapHandler(&spec
, pname
));
480 std::cerr
<< "rbd: sysfs write failed" << std::endl
;
482 std::cerr
<< "rbd: udev wait failed" << std::endl
;
483 /* TODO: fall back to enumeration */
493 static int map_image(struct krbd_ctx
*ctx
, const krbd_spec
& spec
,
494 const char *options
, string
*pname
)
500 * Modprobe rbd kernel module. If it supports single-major device
501 * number allocation scheme, make sure it's turned on.
503 * Do this before calling build_map_buf() - it wants "ceph" key type
506 if (access("/sys/bus/rbd", F_OK
) != 0) {
507 const char *module_options
= NULL
;
508 if (module_has_param("rbd", "single_major"))
509 module_options
= "single_major=Y";
511 r
= module_load("rbd", module_options
);
513 cerr
<< "rbd: failed to load rbd kernel module (" << r
<< ")"
516 * Ignore the error: modprobe failing doesn't necessarily prevent
522 r
= build_map_buf(ctx
->cct
, spec
, options
, &buf
);
526 return do_map(ctx
, spec
, buf
, pname
);
529 static int devno_to_krbd_id(struct udev
*udev
, dev_t devno
, string
*pid
)
531 udev_enumerate_uptr enm
;
532 struct udev_list_entry
*l
;
536 enm
.reset(udev_enumerate_new(udev
));
540 r
= udev_enumerate_add_match_subsystem(enm
.get(), "rbd");
544 r
= udev_enumerate_add_match_sysattr(enm
.get(), "major",
545 stringify(major(devno
)).c_str());
549 if (have_minor_attr()) {
550 r
= udev_enumerate_add_match_sysattr(enm
.get(), "minor",
551 stringify(minor(devno
)).c_str());
556 r
= udev_enumerate_scan_devices(enm
.get());
558 if (r
== -ENOENT
|| r
== -ENODEV
) {
559 std::cerr
<< "rbd: udev enumerate failed, retrying" << std::endl
;
565 l
= udev_enumerate_get_list_entry(enm
.get());
569 /* make sure there is only one match */
570 ceph_assert(!udev_list_entry_get_next(l
));
572 auto dev
= dev_from_list_entry(udev
, l
);
576 *pid
= udev_device_get_sysname(dev
.get());
580 static int __enumerate_devices(struct udev
*udev
, const krbd_spec
& spec
,
581 bool match_nspace
, udev_enumerate_uptr
*penm
)
583 udev_enumerate_uptr enm
;
587 enm
.reset(udev_enumerate_new(udev
));
591 r
= udev_enumerate_add_match_subsystem(enm
.get(), "rbd");
595 r
= udev_enumerate_add_match_sysattr(enm
.get(), "pool",
596 spec
.pool_name
.c_str());
601 r
= udev_enumerate_add_match_sysattr(enm
.get(), "pool_ns",
602 spec
.nspace_name
.c_str());
605 * Match _only_ devices that don't have pool_ns attribute.
606 * If the kernel supports namespaces, the result will be empty.
608 r
= udev_enumerate_add_nomatch_sysattr(enm
.get(), "pool_ns", nullptr);
613 r
= udev_enumerate_add_match_sysattr(enm
.get(), "name",
614 spec
.image_name
.c_str());
618 r
= udev_enumerate_add_match_sysattr(enm
.get(), "current_snap",
619 spec
.snap_name
.c_str());
623 r
= udev_enumerate_scan_devices(enm
.get());
625 if (r
== -ENOENT
|| r
== -ENODEV
) {
626 std::cerr
<< "rbd: udev enumerate failed, retrying" << std::endl
;
632 *penm
= std::move(enm
);
636 static int enumerate_devices(struct udev
*udev
, const krbd_spec
& spec
,
637 udev_enumerate_uptr
*penm
)
639 udev_enumerate_uptr enm
;
642 r
= __enumerate_devices(udev
, spec
, true, &enm
);
647 * If no namespace is set, try again with match_nspace=false to
648 * handle older kernels. On a newer kernel the result will remain
649 * the same (i.e. empty).
651 if (!udev_enumerate_get_list_entry(enm
.get()) && spec
.nspace_name
.empty()) {
652 r
= __enumerate_devices(udev
, spec
, false, &enm
);
657 *penm
= std::move(enm
);
661 static int spec_to_devno_and_krbd_id(struct udev
*udev
, const krbd_spec
& spec
,
662 dev_t
*pdevno
, string
*pid
)
664 udev_enumerate_uptr enm
;
665 struct udev_list_entry
*l
;
666 unsigned int maj
, min
= 0;
670 r
= enumerate_devices(udev
, spec
, &enm
);
674 l
= udev_enumerate_get_list_entry(enm
.get());
678 auto dev
= dev_from_list_entry(udev
, l
);
682 maj
= strict_strtoll(udev_device_get_sysattr_value(dev
.get(), "major"), 10,
685 cerr
<< "rbd: couldn't parse major: " << err
<< std::endl
;
688 if (have_minor_attr()) {
689 min
= strict_strtoll(udev_device_get_sysattr_value(dev
.get(), "minor"), 10,
692 cerr
<< "rbd: couldn't parse minor: " << err
<< std::endl
;
698 * If an image is mapped more than once don't bother trying to unmap
699 * all devices - let users run unmap the same number of times they
702 if (udev_list_entry_get_next(l
))
703 cerr
<< "rbd: " << spec
<< ": mapped more than once, unmapping "
704 << get_devnode(dev
.get()) << " only" << std::endl
;
706 *pdevno
= makedev(maj
, min
);
707 *pid
= udev_device_get_sysname(dev
.get());
711 static void append_unmap_options(std::string
*buf
, const char *options
)
713 if (strcmp(options
, "") != 0) {
719 class UdevUnmapHandler
{
721 UdevUnmapHandler(dev_t devno
) : m_devno(devno
) {}
723 bool operator()(udev_device_uptr dev
) {
724 if (strcmp(udev_device_get_action(dev
.get()), "remove")) {
727 return udev_device_get_devnum(dev
.get()) == m_devno
;
734 static int do_unmap(krbd_ctx
*ctx
, dev_t devno
, const string
& buf
)
740 udev_monitor_uptr
mon(udev_monitor_new_from_netlink(ctx
->udev
,
741 get_event_source(ctx
)));
745 r
= udev_monitor_filter_add_match_subsystem_devtype(mon
.get(), "block",
750 r
= udev_monitor_set_receive_buffer_size(mon
.get(), UDEV_BUF_SIZE
);
752 std::cerr
<< "rbd: failed to set udev buffer size: " << cpp_strerror(r
)
757 r
= udev_monitor_enable_receiving(mon
.get());
761 if (pipe2(fds
, O_NONBLOCK
) < 0)
764 auto unmapper
= make_named_thread(
765 "unmapper", [&buf
, sysfs_r_fd
= fds
[1], flags
= ctx
->flags
]() {
767 * On final device close(), kernel sends a block change event, in
768 * response to which udev apparently runs blkid on the device. This
769 * makes unmap fail with EBUSY, if issued right after final close().
770 * Try to circumvent this with a retry before turning to udev.
772 for (int tries
= 0; ; tries
++) {
773 int sysfs_r
= sysfs_write_rbd_remove(buf
);
774 if (sysfs_r
== -EBUSY
&& tries
< 2) {
777 } else if (!(flags
& KRBD_CTX_F_NOUDEV
)) {
779 * libudev does not provide the "wait until the queue is empty"
780 * API or the sufficient amount of primitives to build it from.
782 std::string err
= run_cmd("udevadm", "settle", "--timeout", "10",
785 std::cerr
<< "rbd: " << err
<< std::endl
;
788 int r
= safe_write(sysfs_r_fd
, &sysfs_r
, sizeof(sysfs_r
));
790 ceph_abort_msgf("safe_write failed: %d", r
);
797 std::tie(r
, unmapped
) = wait_for_mapping(fds
[0], mon
.get(),
798 UdevUnmapHandler(devno
));
801 std::cerr
<< "rbd: sysfs write failed" << std::endl
;
803 std::cerr
<< "rbd: udev wait failed: " << cpp_strerror(r
) << std::endl
;
814 static int unmap_image(struct krbd_ctx
*ctx
, const char *devnode
,
818 dev_t wholedevno
= 0;
822 if (stat(devnode
, &sb
) < 0 || !S_ISBLK(sb
.st_mode
)) {
823 cerr
<< "rbd: '" << devnode
<< "' is not a block device" << std::endl
;
827 r
= blkid_devno_to_wholedisk(sb
.st_rdev
, NULL
, 0, &wholedevno
);
829 cerr
<< "rbd: couldn't compute wholedevno: " << cpp_strerror(r
)
832 * Ignore the error: we are given whole disks most of the time, and
833 * if it turns out this is a partition we will fail later anyway.
835 wholedevno
= sb
.st_rdev
;
838 for (int tries
= 0; ; tries
++) {
839 r
= devno_to_krbd_id(ctx
->udev
, wholedevno
, &buf
);
840 if (r
== -ENOENT
&& tries
< 2) {
845 std::cerr
<< "rbd: '" << devnode
<< "' is not an rbd device"
852 std::cerr
<< "rbd: udev enumerate missed a device, tries = " << tries
859 append_unmap_options(&buf
, options
);
860 return do_unmap(ctx
, wholedevno
, buf
);
863 static int unmap_image(struct krbd_ctx
*ctx
, const krbd_spec
& spec
,
870 for (int tries
= 0; ; tries
++) {
871 r
= spec_to_devno_and_krbd_id(ctx
->udev
, spec
, &devno
, &buf
);
872 if (r
== -ENOENT
&& tries
< 2) {
877 std::cerr
<< "rbd: " << spec
<< ": not a mapped image or snapshot"
884 std::cerr
<< "rbd: udev enumerate missed a device, tries = " << tries
891 append_unmap_options(&buf
, options
);
892 return do_unmap(ctx
, devno
, buf
);
895 static bool dump_one_image(Formatter
*f
, TextTable
*tbl
,
896 struct udev_device
*dev
)
898 auto spec
= spec_from_dev(dev
);
899 std::string devnode
= get_devnode(dev
);
900 const char *id
= devnode
.c_str() + sizeof(DEVNODE_PREFIX
) - 1;
906 f
->open_object_section("device");
907 f
->dump_string("id", id
);
908 f
->dump_string("pool", spec
->pool_name
);
909 f
->dump_string("namespace", spec
->nspace_name
);
910 f
->dump_string("name", spec
->image_name
);
911 f
->dump_string("snap", spec
->snap_name
);
912 f
->dump_string("device", devnode
);
915 *tbl
<< id
<< spec
->pool_name
<< spec
->nspace_name
<< spec
->image_name
916 << spec
->snap_name
<< devnode
<< TextTable::endrow
;
922 static int do_dump(struct udev
*udev
, Formatter
*f
, TextTable
*tbl
)
924 udev_enumerate_uptr enm
;
925 struct udev_list_entry
*l
= NULL
;
926 bool have_output
= false;
930 enm
.reset(udev_enumerate_new(udev
));
934 r
= udev_enumerate_add_match_subsystem(enm
.get(), "rbd");
938 r
= udev_enumerate_scan_devices(enm
.get());
940 if (r
== -ENOENT
|| r
== -ENODEV
) {
941 std::cerr
<< "rbd: udev enumerate failed, retrying" << std::endl
;
947 udev_list_entry_foreach(l
, udev_enumerate_get_list_entry(enm
.get())) {
948 auto dev
= dev_from_list_entry(udev
, l
);
950 have_output
|= dump_one_image(f
, tbl
, dev
.get());
957 static int dump_images(struct krbd_ctx
*ctx
, Formatter
*f
)
963 f
->open_array_section("devices");
965 tbl
.define_column("id", TextTable::LEFT
, TextTable::LEFT
);
966 tbl
.define_column("pool", TextTable::LEFT
, TextTable::LEFT
);
967 tbl
.define_column("namespace", TextTable::LEFT
, TextTable::LEFT
);
968 tbl
.define_column("image", TextTable::LEFT
, TextTable::LEFT
);
969 tbl
.define_column("snap", TextTable::LEFT
, TextTable::LEFT
);
970 tbl
.define_column("device", TextTable::LEFT
, TextTable::LEFT
);
973 r
= do_dump(ctx
->udev
, f
, &tbl
);
986 static int is_mapped_image(struct udev
*udev
, const krbd_spec
& spec
,
989 udev_enumerate_uptr enm
;
990 struct udev_list_entry
*l
;
993 r
= enumerate_devices(udev
, spec
, &enm
);
997 l
= udev_enumerate_get_list_entry(enm
.get());
999 auto dev
= dev_from_list_entry(udev
, l
);
1003 *pname
= get_devnode(dev
.get());
1007 return 0; /* not mapped */
1010 extern "C" int krbd_create_from_context(rados_config_t cct
, uint32_t flags
,
1011 struct krbd_ctx
**pctx
)
1013 struct krbd_ctx
*ctx
= new struct krbd_ctx();
1015 ctx
->cct
= reinterpret_cast<CephContext
*>(cct
);
1016 ctx
->udev
= udev_new();
1027 extern "C" void krbd_destroy(struct krbd_ctx
*ctx
)
1032 udev_unref(ctx
->udev
);
1037 extern "C" int krbd_map(struct krbd_ctx
*ctx
,
1038 const char *pool_name
,
1039 const char *nspace_name
,
1040 const char *image_name
,
1041 const char *snap_name
,
1042 const char *options
,
1045 krbd_spec
spec(pool_name
, nspace_name
, image_name
, snap_name
);
1050 r
= map_image(ctx
, spec
, options
, &name
);
1054 devnode
= strdup(name
.c_str());
1058 *pdevnode
= devnode
;
1062 extern "C" int krbd_unmap(struct krbd_ctx
*ctx
, const char *devnode
,
1063 const char *options
)
1065 return unmap_image(ctx
, devnode
, options
);
1068 extern "C" int krbd_unmap_by_spec(struct krbd_ctx
*ctx
,
1069 const char *pool_name
,
1070 const char *nspace_name
,
1071 const char *image_name
,
1072 const char *snap_name
,
1073 const char *options
)
1075 krbd_spec
spec(pool_name
, nspace_name
, image_name
, snap_name
);
1076 return unmap_image(ctx
, spec
, options
);
1079 int krbd_showmapped(struct krbd_ctx
*ctx
, Formatter
*f
)
1081 return dump_images(ctx
, f
);
1084 extern "C" int krbd_is_mapped(struct krbd_ctx
*ctx
,
1085 const char *pool_name
,
1086 const char *nspace_name
,
1087 const char *image_name
,
1088 const char *snap_name
,
1091 krbd_spec
spec(pool_name
, nspace_name
, image_name
, snap_name
);
1096 r
= is_mapped_image(ctx
->udev
, spec
, &name
);
1097 if (r
<= 0) /* error or not mapped */
1100 devnode
= strdup(name
.c_str());
1104 *pdevnode
= devnode
;