From 5730e47b6f6b1463c27eb8f6bcc8c1345be50e75 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Tue, 27 Jan 2015 13:45:02 -0800 Subject: [PATCH] Cleanup, rip out more code, fix compiler warnings Change-Id: I76b410dc700a917d2717b1ceda0df061a1599144 --- .gitignore | 3 - Makefile | 32 ++---- Makefile.am | 46 ++------ bcache-super-show.8 | 11 -- bcache-super-show.c | 89 --------------- bcache-tools.spec | 4 - bcache.c | 80 ++++++++++---- bcache.h | 41 +------ bcacheadm.c | 6 +- bcachectl.c | 52 --------- make-bcache.8 | 26 ----- make-bcache.c | 259 -------------------------------------------- 12 files changed, 83 insertions(+), 566 deletions(-) delete mode 100644 bcache-super-show.8 delete mode 100644 bcache-super-show.c delete mode 100644 bcachectl.c delete mode 100644 make-bcache.8 delete mode 100644 make-bcache.c diff --git a/.gitignore b/.gitignore index 74da3a4..7dcf732 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,5 @@ -bcache-super-show -make-bcache bcacheadm probe-bcache -bcachectl .* *.o tags diff --git a/Makefile b/Makefile index cda5187..34d8694 100644 --- a/Makefile +++ b/Makefile @@ -3,40 +3,30 @@ PREFIX=/usr UDEVLIBDIR=/lib/udev DRACUTLIBDIR=/lib/dracut INSTALL=install -CFLAGS+=-O2 -Wall -Werror -g -I. +CFLAGS+=-std=gnu99 -O2 -Wall -Werror -g -I. -all: bcacheadm make-bcache probe-bcache bcachectl +all: bcacheadm probe-bcache -install: bcacheadm make-bcache probe-bcache - $(INSTALL) -m0755 bcacheadm bcachectl $(DESTDIR)${PREFIX}/sbin/ - $(INSTALL) -m0755 make-bcache bcachectl $(DESTDIR)${PREFIX}/sbin/ +install: bcacheadm probe-bcache + $(INSTALL) -m0755 bcacheadm $(DESTDIR)${PREFIX}/sbin/ $(INSTALL) -m0755 probe-bcache bcache-register $(DESTDIR)$(UDEVLIBDIR)/ $(INSTALL) -m0644 69-bcache.rules $(DESTDIR)$(UDEVLIBDIR)/rules.d/ #-$(INSTALL) -T -m0755 initramfs/hook $(DESTDIR)/usr/share/initramfs-tools/hooks/bcache - if [ -d $(DESTDIR)$(DRACUTLIBDIR)/modules.d ]; \ - then $(INSTALL) -D -m0755 dracut/module-setup.sh $(DESTDIR)$(DRACUTLIBDIR)/modules.d/90bcache/module-setup.sh; \ + if [ -d $(DESTDIR)$(DRACUTLIBDIR)/modules.d ]; then\ + $(INSTALL) -D -m0755 dracut/module-setup.sh $(DESTDIR)$(DRACUTLIBDIR)/modules.d/90bcache/module-setup.sh; \ fi $(INSTALL) -m0644 -- *.8 $(DESTDIR)${PREFIX}/share/man/man8/ -# $(INSTALL) -m0755 bcache-test $(DESTDIR)${PREFIX}/sbin/ clean: - $(RM) -f make-bcache probe-bcache bcache-super-show bcache-test bcachectl *.o + $(RM) -f probe-bcache bcacheadm bcache-test *.o -bcache-test: LDLIBS += `pkg-config --libs openssl` +bcache.o: CFLAGS += `pkg-config --cflags uuid blkid` +bcacheadm.o: CFLAGS += `pkg-config --cflags uuid blkid libnih` bcacheadm: LDLIBS += `pkg-config --libs uuid blkid libnih` -bcacheadm: CFLAGS += `pkg-config --cflags uuid blkid libnih` bcacheadm: bcache.o -make-bcache: LDLIBS += `pkg-config --libs uuid blkid` -make-bcache: CFLAGS += `pkg-config --cflags uuid blkid` -make-bcache: bcache.o - +probe-bcache.o: CFLAGS += `pkg-config --cflags uuid blkid` probe-bcache: LDLIBS += `pkg-config --libs uuid blkid` -probe-bcache: CFLAGS += `pkg-config --cflags uuid blkid` -bcache-super-show: LDLIBS += `pkg-config --libs uuid` -bcache-super-show: CFLAGS += -std=gnu99 -bcache-super-show: bcache.o - -bcachectl: bcachectl.o +bcache-test: LDLIBS += `pkg-config --libs openssl` diff --git a/Makefile.am b/Makefile.am index e421b80..5b3a257 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,42 +2,21 @@ AUTOMAKE_OPTIONS=subdir-objects PREFIX=/usr -AM_CFLAGS=-std=gnu99 `pkg-config --cflags uuid blkid` -AM_LDFLAGS=`pkg-config --libs uuid blkid` -L$(top_builddir) +AM_CFLAGS=-std=gnu99 -O2 -Wall -Werror -g +bin_PROGRAMS=bcacheadm probe-bcache -bin_PROGRAMS=make-bcache \ - probe-bcache \ - bcache-super-show \ - bcachectl \ - bcacheadm - -noinst_PROGRAMS=bcache-test - -lib_LIBRARIES=libbcache.a -libbcache_a_SOURCES=bcache.c -bcache_LDADD=libbcache.a - -make_bcache_SOURCES=make-bcache.c -make_bcache_LDADD=libbcache.a - -probe_bcache_SOURCES=probe-bcache.c -probe_bcache_LDADD=libbcache.a - -bcache_super_show_SOURCES=bcache-super-show.c -bcache_super_show_LDADD=libbcache.a - -bcachectl_SOURCES=bcachectl.c - -bcache_test_SOURCES=bcache-test.c -bcache_test_LDFLAGS=-lm `pkg-config --libs openssl` -bcache_test_CFLAGS=$(AM_CFLAGS) `pkg-config --cflags openssl` +bcache.o: CFLAGS += `pkg-config --cflags uuid blkid` bcacheadm_SOURCES=bcacheadm.c -bcacheadm_CFLAGS=$(AM_CFLAGS) -g -bcacheadm_LDFLAGS=$(AM_LDFLAGS) -lnih -bcacheadm_LDADD=libbcache.a +bcacheadm_CFLAGS=$(AM_CFLAGS) `pkg-config --cflags uuid blkid libnih` +bcacheadm_LDFLAGS=`pkg-config --libs uuid blkid libnih` +bcacheadm_LDADD=bcache.o +probe_bcache_SOURCES=probe-bcache.c +probe_bcache_CFLAGS=$(AM_CFLAGS) `pkg-config --cflags uuid blkid` +probe_bcache_LDFLAGS=`pkg-config --libs uuid blkid` +probe_bcache_LDADD=bcache.o udevrule_DATA=69-bcache.rules udevruledir=$(prefix)/lib/udev/rules.d @@ -48,10 +27,7 @@ udevrdir=$(prefix)/lib/udev/ initramfs_SCRIPTS=initramfs/bcache initramfsdir=$(prefix)/etc/initramfs-tools/hooks/ -man8_MANS= bcache-super-show.8 \ - make-bcache.8 \ - probe-bcache.8 \ - bcacheadm.8 +man8_MANS=bcacheadm.8 probe-bcache.8 include ../../../../../../../../../../usr/share/DateraContainer/Makefile.rpm # some version of automake don't like absolute path names for included makefiles diff --git a/bcache-super-show.8 b/bcache-super-show.8 deleted file mode 100644 index 7d15a93..0000000 --- a/bcache-super-show.8 +++ /dev/null @@ -1,11 +0,0 @@ -.TH bcache-super-show 8 -.SH NAME -bcache-super-show \- Print the bcache superblock -.SH SYNOPSIS -.B bcache-super-show -[\fB \-f] -.I device -.SH OPTIONS -.TP -.BR \-f -Keep going if the superblock crc is invalid diff --git a/bcache-super-show.c b/bcache-super-show.c deleted file mode 100644 index 61c28aa..0000000 --- a/bcache-super-show.c +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Author: Gabriel de Perthuis - * - * GPLv2 - */ - - -#define _FILE_OFFSET_BITS 64 -#define __USE_FILE_OFFSET64 -#define _XOPEN_SOURCE 500 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "bcache.h" - -void usage() -{ - fprintf(stderr, "Usage: bcache-super-show [-f] \n"); -} - -int main(int argc, char **argv) -{ - int o; - extern char *optarg; - struct cache_sb sb_stack, *sb = &sb_stack; - size_t bytes = sizeof(*sb); - bool force_csum = false; - - while ((o = getopt(argc, argv, "f")) != EOF) - switch (o) { - case 'f': - force_csum = 1; - break; - - default: - usage(); - exit(1); - } - - argv += optind; - argc -= optind; - - if (argc != 1) { - usage(); - exit(1); - } - - int fd = open(argv[0], O_RDONLY); - if (fd < 0) { - printf("Can't open dev %s: %s\n", argv[0], strerror(errno)); - exit(2); - } - - if (pread(fd, sb, bytes, SB_START) != bytes) { - fprintf(stderr, "Couldn't read\n"); - exit(2); - } - - if (sb->u64s) { - bytes = sizeof(*sb) + sb->u64s * sizeof(uint64_t); - sb = malloc(bytes); - - if (pread(fd, sb, bytes, SB_START) != bytes) { - fprintf(stderr, "Couldn't read\n"); - exit(2); - } - } - - if (!SB_IS_BDEV(sb)) - show_super_cache(sb, force_csum); - else - show_super_backingdev(sb, force_csum); - - return 0; -} diff --git a/bcache-tools.spec b/bcache-tools.spec index 0215f63..f46df12 100644 --- a/bcache-tools.spec +++ b/bcache-tools.spec @@ -20,11 +20,7 @@ make DESTDIR=%buildroot INSTALL=/usr/bin/install -C /bld/$RPM_PACKAGE_NAME insta %files %_bindir/bcacheadm -%_bindir/bcachectl -%_bindir/make-bcache %_bindir/probe-bcache -%_bindir/bcache-super-show -%_libdir/libbcache.a %_mandir/man8/*.gz %exclude %_prefix/etc/initramfs-tools/hooks/bcache %exclude %_prefix/lib/udev/bcache-register diff --git a/bcache.c b/bcache.c index c9a0258..11f5c04 100644 --- a/bcache.c +++ b/bcache.c @@ -64,6 +64,42 @@ const char * const bdev_state[] = { NULL }; +const char * const set_attrs[] = { + "btree_flush_delay", + "btree_scan_ratelimit", + "bucket_reserve_percent", + "cache_reserve_percent", + "checksum_type", + "congested_read_threshold_us", + "congested_write_threshold_us", + "data_replicas", + "errors", + "foreground_target_percent", + "gc_sector_percent", + "journal_delay_ms", + "meta_replicas", + "sector_reserve_percent", + "tiering_percent", + NULL +}; + +const char * const cache_attrs[] = { + "cache_replacement_policy", + "discard", + "state", + "tier", + NULL +}; + +const char * const internal_attrs[] = { + "btree_shrinker_disabled", + "copy_gc_enabled", + "foreground_write_rate", + "tiering_enabled", + "tiering_rate", + NULL +}; + char *skip_spaces(const char *str) { while (isspace(*str)) @@ -583,8 +619,8 @@ void write_cache_sbs(int *fds, struct cache_sb *sb, m->bucket_size = bucket_sizes[0]; } else { if (!bucket_sizes[i]) { - printf("No bucket size specified for cache %d," - " using the default of %d", + printf("No bucket size specified for cache %zu," + " using the default of %u", i, bucket_sizes[0]); m->bucket_size = bucket_sizes[0]; } else { @@ -821,11 +857,6 @@ void show_super_backingdev(struct cache_sb *sb, bool force_csum) bdev_state[BDEV_STATE(sb)]); } -static void show_cache_member(struct cache_sb *sb, unsigned i) -{ - -} - void show_super_cache(struct cache_sb *sb, bool force_csum) { struct cache_member *m = sb->members + sb->nr_this_dev; @@ -866,16 +897,16 @@ void show_super_cache(struct cache_sb *sb, bool force_csum) printf("cache.discard\t%llu\n", CACHE_DISCARD(m)); } -static int __sysfs_attr_type(char *attr, const char **attr_arr) { - int i, j; - for(i = 0; attr_arr[i] != NULL; i++) +static int __sysfs_attr_type(const char *attr, const char * const *attr_arr) +{ + for (unsigned i = 0; attr_arr[i] != NULL; i++) if(!strcmp(attr, attr_arr[i])) return 1; return 0; } -enum sysfs_attr sysfs_attr_type(char *attr) { - int ret; +enum sysfs_attr sysfs_attr_type(const char *attr) +{ if(__sysfs_attr_type(attr, set_attrs)) return SET_ATTR; if(__sysfs_attr_type(attr, cache_attrs)) @@ -888,13 +919,14 @@ enum sysfs_attr sysfs_attr_type(char *attr) { return -1; } -static void __sysfs_attr_list(const char **attr_arr) { - int i, j; - for (i = 0; attr_arr[i] != NULL; i++) +static void __sysfs_attr_list(const char * const *attr_arr) +{ + for (unsigned i = 0; attr_arr[i] != NULL; i++) printf("%s\n", attr_arr[i]); } -void sysfs_attr_list() { +void sysfs_attr_list() +{ __sysfs_attr_list(set_attrs); __sysfs_attr_list(cache_attrs); __sysfs_attr_list(internal_attrs); @@ -968,8 +1000,8 @@ char *dev_name(const char *ugly_path) { return strdup(buf); } -static void list_cacheset_devs(char *cset_dir, char *cset_name, bool parse_dev_name) { - int i = 0; +static void list_cacheset_devs(char *cset_dir, char *cset_name, bool parse_dev_name) +{ DIR *cachedir, *dir; struct stat cache_stat; char entry[MAX_PATH]; @@ -1017,7 +1049,8 @@ static void list_cacheset_devs(char *cset_dir, char *cset_name, bool parse_dev_n } } -char *find_matching_uuid(char *stats_dir, char *subdir, const char *stats_dev_uuid) { +char *find_matching_uuid(char *stats_dir, char *subdir, const char *stats_dev_uuid) +{ /* Do a query-dev --uuid only to get the uuid * repeat on each dev until we find a matching one * append that cache# to subdir and return @@ -1338,7 +1371,6 @@ char *read_stat_dir(DIR *dir, char *stats_dir, char *stat_name, char *ret) } if (S_ISREG(statbuf.st_mode)) { - char buf[MAX_PATH]; FILE *fp = NULL; fp = fopen(entry, "r"); @@ -1356,7 +1388,7 @@ err: } char *bcache_get_capacity(const char *cset_dir, const char *capacity_uuid, - bool show_devs) + bool show_devs) { char *err = NULL; char bucket_size_path[MAX_PATH]; @@ -1373,7 +1405,6 @@ char *bcache_get_capacity(const char *cset_dir, const char *capacity_uuid, double total_cap = 0, total_free = 0; int precision = 2; - snprintf(intbuf, 4, "%d", i); snprintf(bucket_size_path, MAX_PATH, "%s/%s/%s/%s", cset_dir, capacity_uuid, "cache0", "bucket_size_bytes"); snprintf(nbuckets_path, MAX_PATH, "%s/%s/%s/%s", cset_dir, @@ -1383,7 +1414,7 @@ char *bcache_get_capacity(const char *cset_dir, const char *capacity_uuid, snprintf(cache_path, MAX_PATH, "%s/%s/%s", cset_dir, capacity_uuid, "cache0"); - while(true) { + while (true) { char buf[MAX_PATH]; int len; DIR *cache_dir; @@ -1417,6 +1448,8 @@ char *bcache_get_capacity(const char *cset_dir, const char *capacity_uuid, dev_names[dev_count] = dev_name(buf); } + snprintf(intbuf, 4, "%d", dev_count); + /* remove i/stat and append i++/stat */ bucket_size_path[strlen(cache_path) - strlen(intbuf)] = 0; nbuckets_path[strlen(cache_path) - strlen(intbuf)] = 0; @@ -1425,7 +1458,6 @@ char *bcache_get_capacity(const char *cset_dir, const char *capacity_uuid, dev_count++; - snprintf(intbuf, 4, "%d", dev_count); strcat(cache_path, intbuf); strcat(bucket_size_path, intbuf); strcat(nbuckets_path, intbuf); diff --git a/bcache.h b/bcache.h index c9987d0..39fbb40 100644 --- a/bcache.h +++ b/bcache.h @@ -50,6 +50,9 @@ extern const char * const replacement_policies[]; extern const char * const csum_types[]; extern const char * const bdev_cache_mode[]; extern const char * const bdev_state[]; +extern const char * const set_attr[]; +extern const char * const cache_attrs[]; +extern const char * const internal_attrs[]; ssize_t read_string_list(const char *, const char * const[]); ssize_t read_string_list_or_die(const char *, const char * const[], @@ -74,43 +77,7 @@ void show_super_cache(struct cache_sb *, bool); enum sysfs_attr {SET_ATTR, CACHE_ATTR, INTERNAL_ATTR}; -static const char *set_attrs[] = { - "btree_flush_delay", - "btree_scan_ratelimit", - "bucket_reserve_percent", - "cache_reserve_percent", - "checksum_type", - "congested_read_threshold_us", - "congested_write_threshold_us", - "data_replicas", - "errors", - "foreground_target_percent", - "gc_sector_percent", - "journal_delay_ms", - "meta_replicas", - "sector_reserve_percent", - "tiering_percent", - NULL -}; - -static const char *cache_attrs[] = { - "cache_replacement_policy", - "discard", - "state", - "tier", - NULL -}; - -static const char *internal_attrs[] = { - "btree_shrinker_disabled", - "copy_gc_enabled", - "foreground_write_rate", - "tiering_enabled", - "tiering_rate", - NULL -}; - -enum sysfs_attr sysfs_attr_type(char *attr); +enum sysfs_attr sysfs_attr_type(const char *attr); void sysfs_attr_list(); struct cache_sb *query_dev(char *, bool, bool, bool, char *dev_uuid); diff --git a/bcacheadm.c b/bcacheadm.c index f376245..cf1ecfc 100644 --- a/bcacheadm.c +++ b/bcacheadm.c @@ -477,8 +477,6 @@ int bcache_modify(NihCommand *command, char *const *args) { char *err; char path[MAX_PATH]; - DIR *path_dir; - struct stat cache_stat; char *attr = args[0]; char *val = NULL; int fd = -1; @@ -604,7 +602,7 @@ int bcache_query_devs(NihCommand *command, char *const *args) int bcache_status(NihCommand *command, char *const *args) { - int i, dev_count = 0, seq, nr_in_set = 0; + int i, dev_count = 0, seq; struct cache_sb *seq_sb = NULL; char cache_path[MAX_PATH]; char *dev_names[MAX_DEVS]; @@ -624,7 +622,6 @@ int bcache_status(NihCommand *command, char *const *args) if (!seq_sb || sb->seq > seq) { seq = sb->seq; seq_sb = sb; - nr_in_set = sb->nr_in_set; } else free(sb); } @@ -825,7 +822,6 @@ err: int bcache_set_failed(NihCommand *command, char *const *args) { - int i; char *err = NULL; if (!dev_failed_uuid) { diff --git a/bcachectl.c b/bcachectl.c deleted file mode 100644 index 508f89e..0000000 --- a/bcachectl.c +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define __KERNEL__ -#include -#undef __KERNEL__ - -int bcachefd; - -static int register_devices(int argc, char *argv[]) -{ - int ret; - ret = ioctl(bcachefd, BCH_IOCTL_REGISTER, argv); - if (ret < 0) { - fprintf(stderr, "ioctl error %d", ret); - exit(EXIT_FAILURE); - } - return 0; -} - -int main(int argc, char *argv[]) -{ - char *ioctl = argv[1]; - - if (argc < 3) { - fprintf(stderr, " %s ", argv[0]); - fprintf(stderr, "\n Possible actions are: \n"); - fprintf(stderr, " \t 1. register_devices\n"); - exit(EXIT_FAILURE); - } - - bcachefd = open("/dev/bcache", O_RDWR); - if (bcachefd < 0) { - perror("Can't open bcache device"); - exit(EXIT_FAILURE); - } - - argc -= 2; - argv += 2; - - if (!strcmp(ioctl, "register")) - return register_devices(argc, argv); - else { - fprintf(stderr, "Unknown ioctl\n"); - exit(EXIT_FAILURE); - } - return 0; -} diff --git a/make-bcache.8 b/make-bcache.8 deleted file mode 100644 index 337a414..0000000 --- a/make-bcache.8 +++ /dev/null @@ -1,26 +0,0 @@ -.TH make-bcache 8 -.SH NAME -make-bcache \- create a cache device -.SH SYNOPSIS -.B make-bcache -[\fB \-U\ \fIUUID\fR ] -[\fB \-b\ \fIbucket-size\fR ] -.I device -.SH OPTIONS -.TP -.BR \-C -Create a cache -.TP -.BR \-B -Create a backing device (kernel functionality not yet implemented) -.TP -.BR \-U\ \fIUUID -Create a cache device with the specified UUID -.TP -.BR \-b\ \fIbucket-size -Spcifies the bucket size. Allocation is done in terms of buckets, and cache -hits are counted per bucket; thus a smaller bucket size will give better cache -utilization, but poorer write performance. The bucket size is intended to be -equal to the size of your SSD's erase blocks, which seems to be 128k-512k for -most SSDs. Must be a power of two; accepts human readable units. Defaults to -128k. diff --git a/make-bcache.c b/make-bcache.c deleted file mode 100644 index 8a94461..0000000 --- a/make-bcache.c +++ /dev/null @@ -1,259 +0,0 @@ -/* - * Author: Kent Overstreet - * - * GPLv2 - */ - -#define _FILE_OFFSET_BITS 64 -#define __USE_FILE_OFFSET 64 -#define _XOPEN_SOURCE 600 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "bcache.h" - -void usage() -{ - fprintf(stderr, - "Usage: make-bcache [options] device\n" - " -C, --cache Format a cache device\n" - " -B, --bdev Format a backing device\n" - " --wipe-bcache destroy existing bcache data if present\n" - " -l, --label label\n" - " --cset-uuid UUID for the cache set\n" - " --csum-type One of (none|crc32c|crc64)\n" - - " -b, --bucket bucket size\n" - " -w, --block block size (hard sector size of SSD, often 2k)\n" - - " --replication-set replication set of subsequent devices\n" - " --meta-replicas number of metadata replicas\n" - " --data-replicas number of data replicas\n" - " --tier tier of subsequent devices\n" - " --cache_replacement_policy one of (lru|fifo|random)\n" - " --discard enable discards\n" - - " --writeback enable writeback\n" - " -o, --data-offset data offset in sectors\n" - " -h, --help display this help and exit\n"); - exit(EXIT_FAILURE); -} - -int main(int argc, char **argv) -{ - int c, bdev = -1; - size_t i, nr_backing_devices = 0; - - unsigned block_size = 0; - unsigned bucket_sizes[argc]; - int num_bucket_sizes = 0; - int writeback = 0, discard = 0, wipe_bcache = 0; - unsigned replication_set = 0, tier = 0, replacement_policy = 0; - uint64_t data_offset = BDEV_DATA_START_DEFAULT; - char *label = NULL; - - const char *cache_devices[argc]; - int cache_dev_fd[argc]; - - const char *backing_devices[argc]; - int backing_dev_fd[argc]; - const char *backing_dev_labels[argc]; - - enum long_opts { - CACHE_SET_UUID = 256, - CSUM_TYPE, - REPLICATION_SET, - META_REPLICAS, - DATA_REPLICAS, - }; - - const struct option opts[] = { - { "cache", 0, NULL, 'C' }, - { "bdev", 0, NULL, 'B' }, - { "wipe-bcache", 0, &wipe_bcache, 1 }, - { "label", 1, NULL, 'l' }, - { "cset-uuid", 1, NULL, CACHE_SET_UUID }, - { "csum-type", 1, NULL, CSUM_TYPE }, - - { "bucket", 1, NULL, 'b' }, - { "block", 1, NULL, 'w' }, - - { "replication-set", 1, NULL, REPLICATION_SET }, - { "meta-replicas", 1, NULL, META_REPLICAS}, - { "data-replicas", 1, NULL, DATA_REPLICAS }, - { "tier", 1, NULL, 't' }, - { "cache_replacement_policy", 1, NULL, 'p' }, - { "discard", 0, &discard, 1 }, - - { "writeback", 0, &writeback, 1 }, - { "data_offset", 1, NULL, 'o' }, - - { "help", 0, NULL, 'h' }, - { NULL, 0, NULL, 0 }, - }; - - struct cache_sb *cache_set_sb = calloc(1, sizeof(*cache_set_sb) + - sizeof(struct cache_member) * argc); - - uuid_generate(cache_set_sb->set_uuid.b); - SET_CACHE_PREFERRED_CSUM_TYPE(cache_set_sb, BCH_CSUM_CRC32C); - SET_CACHE_SET_META_REPLICAS_WANT(cache_set_sb, 1); - SET_CACHE_SET_DATA_REPLICAS_WANT(cache_set_sb, 1); - bucket_sizes[0] = 1024; - - while ((c = getopt_long(argc, argv, - "-hCBU:w:b:l:", - opts, NULL)) != -1) { - - switch (c) { - case 'C': - bdev = 0; - break; - case 'B': - bdev = 1; - break; - case 'l': - label = optarg; - memcpy(cache_set_sb->label, label, - sizeof(cache_set_sb->label)); - break; - case CACHE_SET_UUID: - if (uuid_parse(optarg, cache_set_sb->set_uuid.b)) { - fprintf(stderr, "Bad uuid\n"); - exit(EXIT_FAILURE); - } - break; - case CSUM_TYPE: - SET_CACHE_PREFERRED_CSUM_TYPE(cache_set_sb, - read_string_list_or_die(optarg, csum_types, - "csum type")); - break; - - case 'b': - bucket_sizes[num_bucket_sizes] = hatoi_validate(optarg, "bucket size"); - num_bucket_sizes++; - break; - case 'w': - block_size = hatoi_validate(optarg, "block size"); - break; - - case REPLICATION_SET: - replication_set = strtoul_or_die(optarg, - CACHE_REPLICATION_SET_MAX, - "replication set"); - break; - case META_REPLICAS: - SET_CACHE_SET_META_REPLICAS_WANT(cache_set_sb, - strtoul_or_die(optarg, - CACHE_SET_META_REPLICAS_WANT_MAX, - "meta replicas")); - break; - case DATA_REPLICAS: - SET_CACHE_SET_DATA_REPLICAS_WANT(cache_set_sb, - strtoul_or_die(optarg, - CACHE_SET_DATA_REPLICAS_WANT_MAX, - "data replicas")); - break; - case 't': - tier = strtoul_or_die(optarg, CACHE_TIERS, "tier"); - break; - case 'p': - replacement_policy = read_string_list_or_die(optarg, - replacement_policies, - "cache replacement policy"); - break; - - case 'o': - data_offset = atoll(optarg); - if (data_offset < BDEV_DATA_START_DEFAULT) { - fprintf(stderr, "Bad data offset; minimum %d sectors\n", - BDEV_DATA_START_DEFAULT); - exit(EXIT_FAILURE); - } - break; - case 'h': - usage(); - break; - case 1: - if (bdev == -1) { - fprintf(stderr, "Please specify -C or -B\n"); - exit(EXIT_FAILURE); - } - - if (bdev) { - backing_dev_labels[nr_backing_devices] = label; - backing_devices[nr_backing_devices++] = optarg; - } else { - cache_devices[cache_set_sb->nr_in_set] = optarg; - next_cache_device(cache_set_sb, - replication_set, - tier, - replacement_policy, - discard); - } - - break; - } - } - - if (!cache_set_sb->nr_in_set && !nr_backing_devices) { - fprintf(stderr, "Please supply a device\n"); - usage(); - } - - i = 0; - do { - if (bucket_sizes[i] < block_size) { - fprintf(stderr, "Bucket size cannot be smaller than block size\n"); - exit(EXIT_FAILURE); - } - i++; - } while (i < num_bucket_sizes); - - if (!block_size) { - for (i = 0; i < cache_set_sb->nr_in_set; i++) - block_size = max(block_size, - get_blocksize(cache_devices[i])); - - for (i = 0; i < nr_backing_devices; i++) - block_size = max(block_size, - get_blocksize(backing_devices[i])); - } - - for (i = 0; i < cache_set_sb->nr_in_set; i++) - cache_dev_fd[i] = dev_open(cache_devices[i], wipe_bcache); - - for (i = 0; i < nr_backing_devices; i++) - backing_dev_fd[i] = dev_open(backing_devices[i], wipe_bcache); - - write_cache_sbs(cache_dev_fd, cache_set_sb, block_size, - bucket_sizes, num_bucket_sizes); - - for (i = 0; i < nr_backing_devices; i++) - write_backingdev_sb(backing_dev_fd[i], - block_size, bucket_sizes, - writeback - ? CACHE_MODE_WRITEBACK - : CACHE_MODE_WRITETHROUGH, - data_offset, - backing_dev_labels[i], - cache_set_sb->user_uuid, - cache_set_sb->set_uuid); - - return 0; -} -- 2.39.5