1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <fcntl.h>
4 #include <linux/btrfs.h>
5 #include <stdlib.h>
6 #include <sys/ioctl.h>
7 
8 #include "device-util.h"
9 #include "errno-util.h"
10 #include "fd-util.h"
11 #include "string-util.h"
12 #include "strxcpyx.h"
13 #include "udev-builtin.h"
14 #include "util.h"
15 
builtin_btrfs(sd_device * dev,sd_netlink ** rtnl,int argc,char * argv[],bool test)16 static int builtin_btrfs(sd_device *dev, sd_netlink **rtnl, int argc, char *argv[], bool test) {
17         struct btrfs_ioctl_vol_args args = {};
18         _cleanup_close_ int fd = -1;
19         int r;
20 
21         if (argc != 3 || !streq(argv[1], "ready"))
22                 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invalid arguments");
23 
24         fd = open("/dev/btrfs-control", O_RDWR|O_CLOEXEC);
25         if (fd < 0) {
26                 if (ERRNO_IS_DEVICE_ABSENT(errno)) {
27                         /* Driver not installed? Then we aren't ready. This is useful in initrds that lack
28                          * btrfs.ko. After the host transition (where btrfs.ko will hopefully become
29                          * available) the device can be retriggered and will then be considered ready. */
30                         udev_builtin_add_property(dev, test, "ID_BTRFS_READY", "0");
31                         return 0;
32                 }
33 
34                 return log_device_debug_errno(dev, errno, "Failed to open /dev/btrfs-control: %m");
35         }
36 
37         strscpy(args.name, sizeof(args.name), argv[2]);
38         r = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
39         if (r < 0)
40                 return log_device_debug_errno(dev, errno, "Failed to call BTRFS_IOC_DEVICES_READY: %m");
41 
42         udev_builtin_add_property(dev, test, "ID_BTRFS_READY", one_zero(r == 0));
43         return 0;
44 }
45 
46 const UdevBuiltin udev_builtin_btrfs = {
47         .name = "btrfs",
48         .cmd = builtin_btrfs,
49         .help = "btrfs volume management",
50 };
51