]> git.sesse.net Git - bcachefs-tools-debian/blob - bcacheadm.c
efe9065b265ca154557cd29b1bf4f458295673bb
[bcachefs-tools-debian] / bcacheadm.c
1 /*
2  * Authors: Kent Overstreet <kmo@daterainc.com>
3  *          Gabriel de Perthuis <g2p.code@gmail.com>
4  *          Jacob Malevich <jam@datera.io>
5  *
6  * GPLv2
7  */
8
9 #include <nih/option.h>
10 #include <nih/command.h>
11 #include <nih/main.h>
12 #include <nih/logging.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <inttypes.h>
18 #include <limits.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <blkid.h>
24 #include <string.h>
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <uuid/uuid.h>
29 #include <dirent.h>
30
31 #include "bcache.h"
32 #include "bcacheadm-format.h"
33 #include "bcacheadm-assemble.h"
34 #include "bcacheadm-run.h"
35 #include "bcacheadm-query.h"
36
37 #define PACKAGE_NAME "bcacheadm"
38 #define PACKAGE_VERSION "1.0"
39 #define PACKAGE_BUGREPORT "linux-bcache@vger.kernel.org"
40
41 #if 0
42 static bool modify_list_attrs = false;
43 static const char *modify_set_uuid = NULL;
44 static const char *modify_dev_uuid = NULL;
45
46 static NihOption bcache_modify_options[] = {
47         {'l', "list", N_("list attributes"), NULL, NULL, &modify_list_attrs, NULL},
48         {'u', "set", N_("cacheset uuid"), NULL, "UUID", &modify_set_uuid, NULL},
49         {'d', "dev", N_("device uuid"), NULL, "UUID", &modify_dev_uuid, NULL},
50         NIH_OPTION_LAST
51 };
52
53 int bcache_modify(NihCommand *command, char *const *args)
54 {
55         char *err;
56         char path[MAX_PATH];
57         char *attr = args[0];
58         char *val = NULL;
59         int fd = -1;
60
61         if (modify_list_attrs) {
62                 sysfs_attr_list();
63                 return 0;
64         }
65
66         if (!modify_set_uuid) {
67                 printf("Must provide a cacheset uuid\n");
68                 return -1;
69         }
70
71         snprintf(path, MAX_PATH, "%s/%s", cset_dir, modify_set_uuid);
72
73         if(!attr) {
74                 printf("Must provide the name of an attribute to modify\n");
75                 goto err;
76         }
77
78         enum sysfs_attr type = sysfs_attr_type(attr);
79
80         if (type == -1)
81                 goto err;
82         else if(type == INTERNAL_ATTR)
83                 strcat(path, "/internal");
84         else if(type == CACHE_ATTR) {
85                 if(modify_dev_uuid) {
86                         /* searches all cache# for a matching uuid,
87                          * path gets modified to the correct cache path */
88                         char subdir[10] = "/cache";
89                         err = find_matching_uuid(path, subdir,
90                                         modify_dev_uuid);
91                         if (err) {
92                                 printf("Failed to find "
93                                         "matching dev %s\n", err);
94                                 goto err;
95                         } else {
96                                 strcat(path, subdir);
97                         }
98                 } else {
99                         printf("Must provide a device uuid\n");
100                 }
101         }
102         /* SET_ATTRs are just in the current dir */
103
104         strcat(path, "/");
105         strcat(path, attr);
106
107         val = args[1];
108         if (!val) {
109                 printf("Must provide a value to change the attribute to\n");
110                 goto err;
111         }
112
113         fd = open(path, O_WRONLY);
114         if (fd < 0) {
115                 printf("Unable to open modify attr with path %s\n", path);
116                 goto err;
117         }
118
119         write(fd, val, strlen(val));
120
121 err:
122         if(fd)
123                 close(fd);
124         return 0;
125 }
126 #endif
127
128 #define CMD(_command, _usage, _synopsis, _help)                         \
129 {                                                                       \
130         .command        = #_command,                                    \
131         .usage          = _usage,                                       \
132         .synopsis       = _synopsis,                                    \
133         .help           = _help,                                        \
134         .group          = NULL,                                         \
135         .options        = opts_##_command,                              \
136         .action         = cmd_##_command,                               \
137 }
138
139 static NihCommand commands[] = {
140         CMD(format, N_("<list of devices>"),
141             "Create a new bcache volume from one or more devices",
142             N_("format drive[s] for bcache")),
143
144         CMD(assemble, N_("<devices>"),
145             "Assembles one or more devices into a bcache volume",
146             N_("Registers a list of devices")),
147         CMD(incremental, N_("<device"),
148             "Incremental assemble bcache volumes",
149             N_("Incrementally registers a single device")),
150
151         CMD(run, N_("<volume>"),
152             "Start a partially assembled volume",
153             N_("Registers a list of devices")),
154         CMD(stop, N_("<volume>"),
155             "Stops a running bcache volume",
156             N_("Unregisters a list of devices")),
157         CMD(add, N_("<volume> <devices>"),
158             "Adds a list of devices to a volume",
159             N_("Adds a list of devices to a volume")),
160         CMD(readd, N_("<volume> <devices>"),
161             "Adds previously used members of a volume",
162             N_("Adds a list of devices to a volume")),
163         CMD(remove, N_("<volume> <devices>"),
164             "Removes a device from its volume",
165             N_("Removes a device from its volume")),
166         CMD(fail, N_("<volume> <devices>"),
167             "Sets a device to the FAILED state",
168             N_("Sets a device to the FAILED state")),
169
170 #if 0
171         CMD(modify, N_("<options>"),
172             "Modifies attributes related to the volume",
173             N_("Modifies attributes related to the volume")),
174 #endif
175         CMD(list, N_("list-cachesets"),
176             "Lists cachesets in /sys/fs/bcache",
177             N_("Lists cachesets in /sys/fs/bcache")),
178         CMD(query, N_("query <list of devices>"),
179             "Gives info about the superblock of a list of devices",
180             N_("show superblock on each of the listed drive")),
181         CMD(status, N_("status <list of devices>"),
182             "Finds the status of the most up to date superblock",
183             N_("Finds the status of the most up to date superblock")),
184         NIH_COMMAND_LAST
185 };
186
187 static NihOption options[] = {
188         NIH_OPTION_LAST
189 };
190
191 int main(int argc, char *argv[])
192 {
193         nih_main_init(argv[0]);
194         nih_option_set_synopsis(_("Manage bcache devices"));
195         nih_option_set_help( _("Helps you manage bcache devices"));
196
197         int ret = nih_command_parser(NULL, argc, argv, options, commands);
198         if (ret < 0)
199                 exit(EXIT_FAILURE);
200
201         nih_signal_reset();
202
203         return 0;
204 }