2 * Authors: Kent Overstreet <kmo@daterainc.com>
3 * Gabriel de Perthuis <g2p.code@gmail.com>
4 * Jacob Malevich <jam@datera.io>
9 #include <nih/option.h>
10 #include <nih/command.h>
12 #include <nih/logging.h>
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
29 #include <uuid/uuid.h>
31 #include <bcache.h> //libbcache
33 #define PACKAGE_NAME "bcacheadm"
34 #define PACKAGE_VERSION "1.0"
35 #define PACKAGE_BUGREPORT "bugreport"
37 #define MAX_DEVS MAX_CACHES_PER_SET
40 /* bcacheadm globals */
43 EXIT_ERROR = 1, /* General/OS error */
44 EXIT_SHELL = 2, /* Start maintenance shell */
45 EXIT_SHELL_REBOOT = 3, /* Start maintenance shell, reboot when done */
46 EXIT_REBOOT = 4, /* System must reboot */
50 /* make-bcache globals */
53 char *cache_devices[MAX_DEVS];
54 unsigned tier_mapping[MAX_DEVS];
55 char *backing_devices[MAX_DEVS];
56 char *backing_dev_labels[MAX_DEVS];
57 size_t i, nr_backing_devices = 0, nr_cache_devices = 0;
58 unsigned block_size = 0;
59 unsigned bucket_sizes[MAX_DEVS];
60 int num_bucket_sizes = 0;
61 int writeback = 0, discard = 0, wipe_bcache = 0;
62 unsigned replication_set = 0, replacement_policy = 0;
63 uint64_t data_offset = BDEV_DATA_START_DEFAULT;
65 struct cache_sb *cache_set_sb = NULL;
73 const char *cache_set_uuid = 0;
74 const char *csum_type = 0;
75 char *metadata_replicas = 0;
76 char *data_replicas = 0;
80 /* super-show globals */
81 bool force_csum = false;
87 char *cset_dir = "/sys/fs/bcache";
90 bool status_all = false;
93 bool stats_all = false;
94 bool stats_list = false;
95 static const char *stats_uuid = NULL;
96 static const char *stats_cache_num = NULL;
97 bool stats_five_min = false;
98 bool stats_hour = false;
99 bool stats_day = false;
100 bool stats_total = false;
102 /* make-bcache option setters */
103 static int set_block_size(NihOption *option, const char *arg)
105 block_size = hatoi_validate(arg, "block size");
109 static int set_cache(NihOption *option, const char *arg)
112 cache_devices[nr_cache_devices] = (char *)malloc(sizeof(char *) *
114 strcpy(cache_devices[nr_cache_devices], arg);
115 tier_mapping[nr_cache_devices] = tier;
121 static int set_bdev(NihOption *option, const char *arg)
125 backing_dev_labels[nr_backing_devices] = (char *)malloc(sizeof(char *) *
127 strcpy(backing_dev_labels[nr_backing_devices], label);
129 backing_devices[nr_backing_devices] = (char *)malloc(sizeof(char *) *
131 strcpy(backing_devices[nr_backing_devices], arg);
133 nr_backing_devices++;
139 static int set_bucket_sizes(NihOption *option, const char *arg)
141 bucket_sizes[num_bucket_sizes]=hatoi_validate(arg, "bucket size");
147 static int set_udev(NihOption *option, const char *arg)
149 if (strcmp("udev", arg)) {
150 printf("Invalid output format %s\n", arg);
159 static NihOption make_bcache_options[] = {
160 // {int shortoption, char* longoption, char* help, NihOptionGroup, char* argname, void *value, NihOptionSetter}
161 {'C', "cache", N_("Format a cache device"), NULL, "dev", NULL, set_cache},
162 {'B', "bdev", N_("Format a backing device"), NULL, "dev", NULL, set_bdev},
163 {'l', "label", N_("label"), NULL, "label", &label, NULL},
164 //Only one bucket_size supported until a list of bucket sizes is parsed correctly
165 {'b', "bucket", N_("bucket size"), NULL, "size", NULL, set_bucket_sizes},
166 //Does the default setter automatically convert strings to an int?
167 {'w', "block", N_("block size (hard sector size of SSD, often 2k"), NULL,"size", NULL, set_block_size},
168 {'t', "tier", N_("tier of subsequent devices"), NULL,"#", &tier, NULL},
169 {'p', "cache_replacement_policy", N_("one of (lru|fifo|random)"), NULL,"policy", &replacement_policy, NULL},
170 {'o', "data_offset", N_("data offset in sectors"), NULL,"offset", &data_offset, NULL},
172 {0, "cset-uuid", N_("UUID for the cache set"), NULL, "uuid", &cache_set_uuid, NULL},
173 {0, "csum-type", N_("One of (none|crc32c|crc64)"), NULL, "type", &csum_type, NULL },
174 {0, "replication-set",N_("replication set of subsequent devices"), NULL, NULL, &replication_set, NULL },
175 {0, "meta-replicas",N_("number of metadata replicas"), NULL, "#", &metadata_replicas, NULL},
176 {0, "data-replicas",N_("number of data replicas"), NULL, "#", &data_replicas, NULL },
178 {0, "wipe-bcache", N_("destroy existing bcache data if present"), NULL, NULL, &wipe_bcache, NULL},
179 {0, "discard", N_("enable discards"), NULL, NULL, &discard, NULL},
180 {0, "writeback", N_("enable writeback"), NULL, NULL, &writeback, NULL},
185 static NihOption probe_bcache_options[] = {
186 {'o', "udev", N_("udev"), NULL, NULL, NULL, set_udev},
190 static NihOption bcache_register_options[] = {
194 static NihOption query_devs_options[] = {
195 {'f', "force_csum", N_("force_csum"), NULL, NULL, &force_csum, NULL},
199 static NihOption list_cachesets_options[] = {
200 {'d', "dir", N_("directory"), NULL, NULL, &cset_dir, NULL},
204 static NihOption status_options[] = {
205 {'a', "all", N_("all"), NULL, NULL, &status_all, NULL},
209 static NihOption stats_options[] = {
210 {'a', "all", N_("all"), NULL, NULL, &stats_all, NULL},
211 {'l', "list", N_("list"), NULL, NULL, &stats_list, NULL},
212 {'u', "uuid", N_("cache_set UUID"), NULL, "UUID", &stats_uuid, NULL},
213 {'c', "cache", N_("cache number (starts from 0)"), NULL, "CACHE#", &stats_cache_num, NULL},
214 {0, "five-min-stats", N_("stats accumulated in last 5 minutes"), NULL, NULL, &stats_five_min, NULL},
215 {0, "hour-stats", N_("stats accumulated in last hour"), NULL, NULL, &stats_hour, NULL},
216 {0, "day-stats", N_("stats accumulated in last day"), NULL, NULL, &stats_day, NULL},
217 {0, "total-stats", N_("stats accumulated in total"), NULL, NULL, &stats_total, NULL},
221 static NihOption options[] = {
227 int make_bcache (NihCommand *command, char *const *args)
229 int cache_dev_fd[devs];
231 int backing_dev_fd[devs];
232 printf("meta: %s, data: %s", metadata_replicas, data_replicas);
234 cache_set_sb = calloc(1, sizeof(*cache_set_sb) +
235 sizeof(struct cache_member) * devs);
237 if (cache_set_uuid) {
238 if(uuid_parse(cache_set_uuid, cache_set_sb->set_uuid.b)) {
239 fprintf(stderr, "Bad uuid\n");
243 uuid_generate(cache_set_sb->set_uuid.b);
247 SET_CACHE_PREFERRED_CSUM_TYPE(cache_set_sb,
248 read_string_list_or_die(csum_type, csum_types,
251 SET_CACHE_PREFERRED_CSUM_TYPE(cache_set_sb, BCH_CSUM_CRC32C);
254 if (metadata_replicas) {
255 SET_CACHE_SET_META_REPLICAS_WANT(cache_set_sb,
256 strtoul_or_die(metadata_replicas,
257 CACHE_SET_META_REPLICAS_WANT_MAX,
260 SET_CACHE_SET_META_REPLICAS_WANT(cache_set_sb, 1);
264 SET_CACHE_SET_DATA_REPLICAS_WANT(cache_set_sb,
265 strtoul_or_die(data_replicas,
266 CACHE_SET_DATA_REPLICAS_WANT_MAX,
269 SET_CACHE_SET_DATA_REPLICAS_WANT(cache_set_sb, 1);
273 fprintf(stderr, "Please specify -C or -B\n");
277 if(!bucket_sizes[0]) bucket_sizes[0] = 1024;
280 for(i = 0; i < nr_cache_devices; i++)
281 next_cache_device(cache_set_sb,
287 if (!cache_set_sb->nr_in_set && !nr_backing_devices) {
288 fprintf(stderr, "Please supply a device\n");
294 if (bucket_sizes[i] < block_size) {
296 "Bucket size cannot be smaller than block size\n");
300 } while (i < num_bucket_sizes);
303 for (i = 0; i < cache_set_sb->nr_in_set; i++)
304 block_size = max(block_size,
305 get_blocksize(cache_devices[i]));
307 for (i = 0; i < nr_backing_devices; i++)
308 block_size = max(block_size,
309 get_blocksize(backing_devices[i]));
312 for (i = 0; i < cache_set_sb->nr_in_set; i++)
313 cache_dev_fd[i] = dev_open(cache_devices[i], wipe_bcache);
315 for (i = 0; i < nr_backing_devices; i++)
316 backing_dev_fd[i] = dev_open(backing_devices[i], wipe_bcache);
318 write_cache_sbs(cache_dev_fd, cache_set_sb, block_size,
319 bucket_sizes, num_bucket_sizes);
321 for (i = 0; i < nr_backing_devices; i++)
322 write_backingdev_sb(backing_dev_fd[i],
323 block_size, bucket_sizes,
324 writeback, data_offset,
325 backing_dev_labels[i],
326 cache_set_sb->set_uuid);
332 int probe_bcache (NihCommand *command, char *const *args)
336 for (i = 0; args[i] != NULL; i++) {
337 probe(args[i], udev);
343 int bcache_register (NihCommand *command, char *const *args)
346 char *arg_list = parse_array_to_list(args);
349 ret = register_bcache(arg_list);
356 int bcache_list_cachesets (NihCommand *command, char *const *args)
358 return list_cachesets(cset_dir);
361 int bcache_query_devs (NihCommand *command, char *const *args)
366 for (i = 0; args[i] != NULL; i++) {
367 printf("query-devs on :%s\n", args[i]);
368 struct cache_sb *sb = query_dev(args[i], false);
369 print_dev_info(sb, force_csum);
373 int bcache_status (NihCommand *command, char *const *args)
376 struct cache_sb *sb_tier0 = NULL, *sb_tier1 = NULL;
377 char *dev0 = NULL, *dev1 = NULL;
379 for (i = 0; args[i] != NULL; i++) {
380 struct cache_sb *sb = query_dev(args[i], false);
381 struct cache_member *m = ((struct cache_member *) sb->d) +
383 long long unsigned cache_tier = CACHE_TIER(m);
386 if (!sb_tier0 || sb->seq > sb_tier0->seq) {
390 else if (cache_tier == 1)
391 if (!sb_tier1 || sb->seq > sb_tier1->seq) {
396 if (sb_tier0) sb_state(sb_tier0, dev0);
397 if (sb_tier1) sb_state(sb_tier1, dev1);
400 static void stats_subdir(char* stats_dir)
403 if(stats_cache_num) {
404 strcat(tmp, "cache");
405 strcat(tmp, stats_cache_num);
406 } else if (stats_five_min)
407 strcat(tmp, "stats_five_minute");
409 strcat(tmp, "stats_hour");
411 strcat(tmp, "stats_day");
412 else if (stats_total)
413 strcat(tmp, "stats_total");
417 strcat(stats_dir, tmp);
420 int bcache_stats (NihCommand *command, char *const *args)
425 struct dirent *ent = NULL;
428 strcpy(stats_dir, cset_dir);
429 strcat(stats_dir, "/");
430 strcat(stats_dir, stats_uuid);
431 stats_subdir(stats_dir);
432 dir = opendir(stats_dir);
434 fprintf(stderr, "Failed to open dir %s\n", cset_dir);
438 printf("Must provide a cacheset uuid\n");
442 if(stats_list || stats_all)
443 while ((ent = readdir(dir)) != NULL)
444 read_stat_dir(dir, stats_dir, ent->d_name, stats_all);
447 for (i = 0; args[i] != NULL; i++)
448 read_stat_dir(dir, stats_dir, args[i], true);
453 static NihCommand commands[] = {
454 {"format", N_("format <list of drives>"),
455 "Format one or a list of devices with bcache datastructures."
456 " You need to do this before you create a volume",
457 N_("format drive[s] with bcache"),
458 NULL, make_bcache_options, make_bcache},
459 {"probe", N_("probe <list of devices>"),
460 "Does a blkid_probe on a device",
461 N_("Does a blkid_probe on a device"),
462 NULL, probe_bcache_options, probe_bcache},
463 {"register", N_("register <list of devices>"),
464 "Registers a list of devices",
465 N_("Registers a list of devices"),
466 NULL, bcache_register_options, bcache_register},
467 {"list-cachesets", N_("list-cachesets"),
468 "Lists cachesets in /sys/fs/bcache",
469 N_("Lists cachesets in /sys/fs/bcache"),
470 NULL, list_cachesets_options, bcache_list_cachesets},
471 {"query-devs", N_("query <list of devices>"),
472 "Gives info about the superblock of a list of devices",
473 N_("show superblock on each of the listed drive"),
474 NULL, query_devs_options, bcache_query_devs},
475 {"status", N_("status <list of devices>"),
476 "Finds the status of the most up to date superblock",
477 N_("Finds the status of the most up to date superblock"),
478 NULL, status_options, bcache_status},
479 {"stats", N_("stats <list of devices>"),
480 "List various bcache statistics",
481 N_("List various bcache statistics"),
482 NULL, stats_options, bcache_stats},
487 int main(int argc, char *argv[])
490 nih_main_init (argv[0]);
492 nih_option_set_synopsis (_("Manage bcache devices"));
493 nih_option_set_help (
494 _("Helps you manage bcache devices"));
496 ret = nih_command_parser (NULL, argc, argv, options, commands);