Debian Codegraph @

src/udev/udev-builtin-btrfs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/btrfs.h>
5 #include <sys/ioctl.h>
6
7 #include "device-util.h"
8 #include "errno-util.h"
9 #include "fd-util.h"
10 #include "string-util.h"
11 #include "udev-builtin.h"
12
13 static int builtin_btrfs(UdevEvent *event, int argc, char *argv[]) {
14 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
15 int r;
16
17 if (!IN_SET(argc, 2, 3) || !streq(argv[1], "ready"))
18 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invalid arguments.");
19
20 const char *node;
21 r = sd_device_get_devname(dev, &node);
22 if (r < 0)
23 return log_device_error_errno(dev, r, "Failed to get device node: %m");
24
25 if (argc == 3 && !streq(argv[2], node))
26 return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Device node '%s' is not owned by the device, it must be '%s'.", argv[2], node);
27
28 if (strlen(node) >= sizeof_field(struct btrfs_ioctl_vol_args, name))
29 return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Device name too long for BTRFS_IOC_DEVICES_READY call: %s", node);
30
31 if (event->event_mode != EVENT_UDEV_WORKER) {
32 log_device_debug(dev, "Running in test mode, skipping execution of 'btrfs' builtin command.");
33 return 0;
34 }
35
36 _cleanup_close_ int fd = open("/dev/btrfs-control", O_RDWR|O_CLOEXEC|O_NOCTTY);
37 if (fd < 0) {
38 if (ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(errno)) {
39 /* Driver not installed? Then we aren't ready. This is useful in initrds that lack
40 * btrfs.ko. After the host transition (where btrfs.ko will hopefully become
41 * available) the device can be retriggered and will then be considered ready. */
42 udev_builtin_add_property(event, "ID_BTRFS_READY", "0");
43 return 0;
44 }
45
46 return log_device_debug_errno(dev, errno, "Failed to open %s: %m", "/dev/btrfs-control");
47 }
48
49 struct btrfs_ioctl_vol_args args = {};
50 strncpy(args.name, node, sizeof(args.name)-1);
51 r = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
52 if (r < 0)
53 return log_device_debug_errno(dev, errno, "Failed to call BTRFS_IOC_DEVICES_READY: %m");
54
55 udev_builtin_add_property(event, "ID_BTRFS_READY", one_zero(r == 0));
56 return 0;
57 }
58
59 const UdevBuiltin udev_builtin_btrfs = {
60 .name = "btrfs",
61 .cmd = builtin_btrfs,
62 .help = "btrfs volume management",
63 };