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 int 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 /* query-dev globals */
81 bool force_csum = false;
82 bool uuid_only = false;
88 char *cset_dir = "/sys/fs/bcache";
89 bool list_devs = false;
92 bool status_all = false;
95 bool stats_all = false;
96 bool stats_list = false;
97 static const char *stats_uuid = NULL;
98 static const char *stats_dev_uuid = NULL;
99 static const char *stats_cache_num = NULL;
100 bool stats_five_min = false;
101 bool stats_hour = false;
102 bool stats_day = false;
103 bool stats_total = false;
105 /* make-bcache option setters */
106 static int set_block_size(NihOption *option, const char *arg)
108 block_size = hatoi_validate(arg, "block size");
112 static int set_cache(NihOption *option, const char *arg)
115 cache_devices[nr_cache_devices] = strdup(arg);
117 tier_mapping[nr_cache_devices] = 0;
119 int ntier = atoi(tier);
120 if(ntier == 0 || ntier == 1)
121 tier_mapping[nr_cache_devices] = ntier;
123 printf("Invalid tier\n");
130 static int set_bdev(NihOption *option, const char *arg)
135 backing_dev_labels[nr_backing_devices] = strdup(label);
137 backing_devices[nr_backing_devices] = strdup(arg);
139 nr_backing_devices++;
145 static int set_bucket_sizes(NihOption *option, const char *arg)
147 bucket_sizes[num_bucket_sizes]=hatoi_validate(arg, "bucket size");
153 static int set_udev(NihOption *option, const char *arg)
155 if (strcmp("udev", arg)) {
156 printf("Invalid output format %s\n", arg);
165 static NihOption make_bcache_options[] = {
166 // {int shortoption, char* longoption, char* help, NihOptionGroup, char* argname, void *value, NihOptionSetter}
167 {'C', "cache", N_("Format a cache device"), NULL, "dev", NULL, set_cache},
168 {'B', "bdev", N_("Format a backing device"), NULL, "dev", NULL, set_bdev},
169 {'l', "label", N_("label"), NULL, "label", &label, NULL},
170 //Only one bucket_size supported until a list of bucket sizes is parsed correctly
171 {'b', "bucket", N_("bucket size"), NULL, "size", NULL, set_bucket_sizes},
172 //Does the default setter automatically convert strings to an int?
173 {'w', "block", N_("block size (hard sector size of SSD, often 2k"), NULL,"size", NULL, set_block_size},
174 {'t', "tier", N_("tier of subsequent devices"), NULL,"#", &tier, NULL},
175 {'p', "cache_replacement_policy", N_("one of (lru|fifo|random)"), NULL,"policy", &replacement_policy, NULL},
176 {'o', "data_offset", N_("data offset in sectors"), NULL,"offset", &data_offset, NULL},
178 {0, "cset-uuid", N_("UUID for the cache set"), NULL, "uuid", &cache_set_uuid, NULL},
179 {0, "csum-type", N_("One of (none|crc32c|crc64)"), NULL, "type", &csum_type, NULL },
180 {0, "replication-set",N_("replication set of subsequent devices"), NULL, NULL, &replication_set, NULL },
181 {0, "meta-replicas",N_("number of metadata replicas"), NULL, "#", &metadata_replicas, NULL},
182 {0, "data-replicas",N_("number of data replicas"), NULL, "#", &data_replicas, NULL },
184 {0, "wipe-bcache", N_("destroy existing bcache data if present"), NULL, NULL, &wipe_bcache, NULL},
185 {0, "discard", N_("enable discards"), NULL, NULL, &discard, NULL},
186 {0, "writeback", N_("enable writeback"), NULL, NULL, &writeback, NULL},
191 static NihOption probe_bcache_options[] = {
192 {'o', "udev", N_("udev"), NULL, NULL, NULL, set_udev},
196 static NihOption bcache_register_options[] = {
200 static NihOption bcache_unregister_options[] = {
204 static NihOption query_devs_options[] = {
205 {'f', "force_csum", N_("force_csum"), NULL, NULL, &force_csum, NULL},
206 {'u', "uuid-only", N_("only print out the uuid for the devices, not the whole superblock"), NULL, NULL, &uuid_only, NULL},
210 static NihOption list_cachesets_options[] = {
211 {'d', "dir", N_("directory"), NULL, NULL, &cset_dir, NULL},
212 {0, "list-devs", N_("list all devices in the cache sets as well"), NULL, NULL, &list_devs, NULL},
216 static NihOption status_options[] = {
217 {'a', "all", N_("all"), NULL, NULL, &status_all, NULL},
221 static NihOption stats_options[] = {
222 {'a', "all", N_("all"), NULL, NULL, &stats_all, NULL},
223 {'l', "list", N_("list"), NULL, NULL, &stats_list, NULL},
224 {'u', "set", N_("cache_set UUID"), NULL, "UUID", &stats_uuid, NULL},
225 {'d', "dev", N_("dev UUID"), NULL, "UUID", &stats_dev_uuid, NULL},
226 {'c', "cache", N_("cache number (starts from 0)"), NULL, "CACHE#", &stats_cache_num, NULL},
227 {0, "five-min-stats", N_("stats accumulated in last 5 minutes"), NULL, NULL, &stats_five_min, NULL},
228 {0, "hour-stats", N_("stats accumulated in last hour"), NULL, NULL, &stats_hour, NULL},
229 {0, "day-stats", N_("stats accumulated in last day"), NULL, NULL, &stats_day, NULL},
230 {0, "total-stats", N_("stats accumulated in total"), NULL, NULL, &stats_total, NULL},
234 static NihOption options[] = {
240 int make_bcache(NihCommand *command, char *const *args)
242 int cache_dev_fd[devs];
244 int backing_dev_fd[devs];
246 cache_set_sb = calloc(1, sizeof(*cache_set_sb) +
247 sizeof(struct cache_member) * devs);
249 if (cache_set_uuid) {
250 if(uuid_parse(cache_set_uuid, cache_set_sb->set_uuid.b)) {
251 fprintf(stderr, "Bad uuid\n");
255 uuid_generate(cache_set_sb->set_uuid.b);
259 SET_CACHE_PREFERRED_CSUM_TYPE(cache_set_sb,
260 read_string_list_or_die(csum_type, csum_types,
263 SET_CACHE_PREFERRED_CSUM_TYPE(cache_set_sb, BCH_CSUM_CRC32C);
266 if (metadata_replicas) {
267 SET_CACHE_SET_META_REPLICAS_WANT(cache_set_sb,
268 strtoul_or_die(metadata_replicas,
269 CACHE_SET_META_REPLICAS_WANT_MAX,
272 SET_CACHE_SET_META_REPLICAS_WANT(cache_set_sb, 1);
276 SET_CACHE_SET_DATA_REPLICAS_WANT(cache_set_sb,
277 strtoul_or_die(data_replicas,
278 CACHE_SET_DATA_REPLICAS_WANT_MAX,
281 SET_CACHE_SET_DATA_REPLICAS_WANT(cache_set_sb, 1);
285 fprintf(stderr, "Please specify -C or -B\n");
289 if(!bucket_sizes[0]) bucket_sizes[0] = 1024;
291 for(i = 0; i < nr_cache_devices; i++)
292 next_cache_device(cache_set_sb,
298 if (!cache_set_sb->nr_in_set && !nr_backing_devices) {
299 fprintf(stderr, "Please supply a device\n");
305 if (bucket_sizes[i] < block_size) {
307 "Bucket size cannot be smaller than block size\n");
311 } while (i < num_bucket_sizes);
314 for (i = 0; i < cache_set_sb->nr_in_set; i++)
315 block_size = max(block_size,
316 get_blocksize(cache_devices[i]));
318 for (i = 0; i < nr_backing_devices; i++)
319 block_size = max(block_size,
320 get_blocksize(backing_devices[i]));
323 for (i = 0; i < cache_set_sb->nr_in_set; i++)
324 cache_dev_fd[i] = dev_open(cache_devices[i], wipe_bcache);
326 for (i = 0; i < nr_backing_devices; i++)
327 backing_dev_fd[i] = dev_open(backing_devices[i], wipe_bcache);
329 write_cache_sbs(cache_dev_fd, cache_set_sb, block_size,
330 bucket_sizes, num_bucket_sizes);
332 for (i = 0; i < nr_backing_devices; i++)
333 write_backingdev_sb(backing_dev_fd[i],
334 block_size, bucket_sizes,
335 writeback, data_offset,
336 backing_dev_labels[i],
337 cache_set_sb->set_uuid);
343 int probe_bcache(NihCommand *command, char *const *args)
348 for (i = 0; args[i] != NULL; i++) {
349 err = probe(args[i], udev);
351 printf("probe_bcache error: %s\n", err);
359 int bcache_register(NihCommand *command, char *const *args)
363 err = register_bcache(args);
365 printf("bcache_register error: %s\n", err);
372 int bcache_unregister(NihCommand *command, char *const *args)
376 err = unregister_bcache(args);
378 printf("bcache_unregister error: %s\n", err);
385 int bcache_list_cachesets(NihCommand *command, char *const *args)
388 err = list_cachesets(cset_dir, list_devs);
390 printf("bcache_list_cachesets error :%s\n", err);
397 int bcache_query_devs(NihCommand *command, char *const *args)
401 for (i = 0; args[i] != NULL; i++){
403 query_dev(args[i], force_csum, true, uuid_only, dev_uuid);
405 printf("%s\n", dev_uuid);
409 int bcache_status(NihCommand *command, char *const *args)
412 struct cache_sb *sb_tier0 = NULL, *sb_tier1 = NULL;
413 char *dev0 = NULL, *dev1 = NULL;
415 for (i = 0; args[i] != NULL; i++) {
416 struct cache_sb *sb = query_dev(args[i], false, false, false, NULL);
417 struct cache_member *m = ((struct cache_member *) sb->d) +
419 long long unsigned cache_tier = CACHE_TIER(m);
422 if (!sb_tier0 || sb->seq > sb_tier0->seq) {
426 else if (cache_tier == 1)
427 if (!sb_tier1 || sb->seq > sb_tier1->seq) {
432 if (sb_tier0) sb_state(sb_tier0, dev0);
433 if (sb_tier1) sb_state(sb_tier1, dev1);
436 static char *stats_subdir(char* stats_dir)
441 strcat(tmp, "cache");
442 err = find_matching_uuid(stats_dir, tmp, stats_dev_uuid);
445 } else if(stats_cache_num) {
446 strcat(tmp, "cache");
447 strcat(tmp, stats_cache_num);
448 } else if (stats_five_min)
449 strcat(tmp, "stats_five_minute");
451 strcat(tmp, "stats_hour");
453 strcat(tmp, "stats_day");
454 else if (stats_total)
455 strcat(tmp, "stats_total");
459 strcat(stats_dir, tmp);
465 int bcache_stats(NihCommand *command, char *const *args)
468 char stats_dir[MAX_PATH];
470 struct dirent *ent = NULL;
474 snprintf(stats_dir, MAX_PATH, "%s/%s", cset_dir, stats_uuid);
475 err = stats_subdir(stats_dir);
479 dir = opendir(stats_dir);
481 err = "Failed to open dir";
485 err = "Must provide a cacheset uuid";
489 if(stats_list || stats_all) {
490 while ((ent = readdir(dir)) != NULL) {
491 err = read_stat_dir(dir, stats_dir, ent->d_name, stats_all);
498 for (i = 0; args[i] != NULL; i++) {
499 err = read_stat_dir(dir, stats_dir, args[i], true);
509 printf("bcache_stats error: %s\n", err);
513 static NihCommand commands[] = {
514 {"format", N_("format <list of drives>"),
515 "Format one or a list of devices with bcache datastructures."
516 " You need to do this before you create a volume",
517 N_("format drive[s] with bcache"),
518 NULL, make_bcache_options, make_bcache},
519 {"probe", N_("probe <list of devices>"),
520 "Does a blkid_probe on a device",
521 N_("Does a blkid_probe on a device"),
522 NULL, probe_bcache_options, probe_bcache},
523 {"register", N_("register <list of devices>"),
524 "Registers a list of devices",
525 N_("Registers a list of devices"),
526 NULL, bcache_register_options, bcache_register},
527 {"unregister", N_("unregister <list of devices>"),
528 "Unregisters a list of devices",
529 N_("Unregisters a list of devices"),
530 NULL, bcache_unregister_options, bcache_unregister},
531 {"list-cachesets", N_("list-cachesets"),
532 "Lists cachesets in /sys/fs/bcache",
533 N_("Lists cachesets in /sys/fs/bcache"),
534 NULL, list_cachesets_options, bcache_list_cachesets},
535 {"query-devs", N_("query <list of devices>"),
536 "Gives info about the superblock of a list of devices",
537 N_("show superblock on each of the listed drive"),
538 NULL, query_devs_options, bcache_query_devs},
539 {"status", N_("status <list of devices>"),
540 "Finds the status of the most up to date superblock",
541 N_("Finds the status of the most up to date superblock"),
542 NULL, status_options, bcache_status},
543 {"stats", N_("stats <list of devices>"),
544 "List various bcache statistics",
545 N_("List various bcache statistics"),
546 NULL, stats_options, bcache_stats},
551 int main(int argc, char *argv[])
554 nih_main_init (argv[0]);
556 nih_option_set_synopsis (_("Manage bcache devices"));
557 nih_option_set_help (
558 _("Helps you manage bcache devices"));
560 ret = nih_command_parser (NULL, argc, argv, options, commands);