]> git.sesse.net Git - bcachefs-tools-debian/blob - bcacheadm-run.c
Don't install udev hook - it's confusing with bcachefs
[bcachefs-tools-debian] / bcacheadm-run.c
1
2 #include <errno.h>
3 #include <stdbool.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <fcntl.h>
9 #include <sys/ioctl.h>
10 #include <unistd.h>
11
12 #include <nih/command.h>
13 #include <nih/option.h>
14
15 #include <uuid/uuid.h>
16
17 #include "bcache.h"
18 #include "bcacheadm-run.h"
19
20 static bool force_data = false;
21 static bool force_metadata = false;
22
23 NihOption opts_run[] = {
24         NIH_OPTION_LAST
25 };
26
27 int cmd_run(NihCommand *command, char *const *args)
28 {
29         return 0;
30 }
31
32 NihOption opts_stop[] = {
33         NIH_OPTION_LAST
34 };
35
36 int cmd_stop(NihCommand *command, char *const *args)
37 {
38         int bcachefd = open("/dev/bcache_extent0", O_RDWR);
39         if (bcachefd < 0)
40                 die("Can't open bcache device");
41
42         int ret = ioctl(bcachefd, BCH_IOCTL_STOP);
43         if (ret < 0)
44                 die("BCH_IOCTL_STOP error: %s", strerror(errno));
45
46         close(bcachefd);
47         return 0;
48 }
49
50 NihOption opts_add[] = {
51         NIH_OPTION_LAST
52 };
53
54 int cmd_add(NihCommand *command, char *const *args)
55 {
56         if (nr_args(args) != 1)
57                 die("Please supply exactly one device");
58
59         int ret, bcachefd;
60
61         bcachefd = open("/dev/bcache_extent0", O_RDWR);
62         if (bcachefd < 0)
63                 die("Can't open bcache device: %s", strerror(errno));
64
65         struct bch_ioctl_disk_add ia = {
66                 .dev = (__u64) args[0],
67         };
68
69         ret = ioctl(bcachefd, BCH_IOCTL_DISK_ADD, &ia);
70         if (ret < 0)
71                 die("BCH_IOCTL_DISK_ADD error: %s", strerror(ret));
72
73         close(bcachefd);
74         return 0;
75 }
76
77 NihOption opts_readd[] = {
78         NIH_OPTION_LAST
79 };
80
81 int cmd_readd(NihCommand *command, char *const *args)
82 {
83         if (nr_args(args) != 1)
84                 die("Please supply exactly one device");
85
86         return 0;
87 }
88
89 NihOption opts_remove[] = {
90         {
91                 'f', "force", N_("force if data present"),
92                 NULL, NULL, &force_data, NULL
93         },
94         {
95                 '\0', "force-metadata", N_("force if metadata present"),
96                 NULL, NULL, &force_metadata, NULL},
97         NIH_OPTION_LAST
98 };
99
100 int cmd_remove(NihCommand *command, char *const *args)
101 {
102         if (nr_args(args) != 1)
103                 die("Please supply exactly one device");
104
105         int bcachefd = open("/dev/bcache_extent0", O_RDWR);
106         if (bcachefd < 0)
107                 die("Can't open bcache device");
108
109         struct bch_ioctl_disk_remove ir = {
110                 .dev = (__u64) args[0],
111         };
112
113         if (force_data)
114                 ir.flags |= BCH_FORCE_IF_DATA_MISSING;
115         if (force_metadata)
116                 ir.flags |= BCH_FORCE_IF_METADATA_MISSING;
117
118         int ret = ioctl(bcachefd, BCH_IOCTL_DISK_REMOVE, &ir);
119         if (ret < 0)
120                 die("BCH_IOCTL_DISK_REMOVE error: %s\n", strerror(errno));
121
122         close(bcachefd);
123         return 0;
124 }
125
126 static const char *dev_failed_uuid = NULL;
127
128 NihOption opts_fail[] = {
129         {'d', "dev", N_("dev UUID"), NULL, "UUID", &dev_failed_uuid, NULL},
130         NIH_OPTION_LAST
131 };
132
133 int cmd_fail(NihCommand *command, char *const *args)
134 {
135         if (nr_args(args) != 1)
136                 die("Please supply exactly one device");
137
138         int bcachefd = open("/dev/bcache_extent0", O_RDWR);
139         if (bcachefd < 0)
140                 die("Can't open bcache device");
141
142         struct bch_ioctl_disk_fail df = {
143                 .dev = (__u64) args[0],
144         };
145
146         int ret = ioctl(bcachefd, BCH_IOCTL_DISK_FAIL, &df);
147         if (ret < 0)
148                 die("BCH_IOCTL_DISK_FAIL error: %s\n", strerror(errno));
149
150         return 0;
151 }