]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs.c
Factor alignment logic out of bcachefs_fuse_read.
[bcachefs-tools-debian] / libbcachefs.c
index 6c78f282136e34bca890118f889923d2e1f03fef..7c9687d35acff0cd54641a0917703a167f10880b 100644 (file)
@@ -23,6 +23,7 @@
 #include "libbcachefs/opts.h"
 #include "libbcachefs/replicas.h"
 #include "libbcachefs/super-io.h"
+#include "tools-util.h"
 
 #define NSEC_PER_SEC   1000000000L
 
@@ -71,7 +72,7 @@ static void init_layout(struct bch_sb_layout *l, unsigned block_size,
        l->sb_offset[1]         = cpu_to_le64(backup);
 }
 
-void bch2_pick_bucket_size(struct format_opts opts, struct dev_opts *dev)
+void bch2_pick_bucket_size(struct bch_opts opts, struct dev_opts *dev)
 {
        if (!dev->sb_offset) {
                dev->sb_offset  = BCH_SB_SECTOR;
@@ -90,7 +91,9 @@ void bch2_pick_bucket_size(struct format_opts opts, struct dev_opts *dev)
                dev->bucket_size = opts.block_size;
 
                /* Bucket size must be >= btree node size: */
-               dev->bucket_size = max(dev->bucket_size, opts.btree_node_size);
+               if (opt_defined(opts, btree_node_size))
+                       dev->bucket_size = max_t(unsigned, dev->bucket_size,
+                                                opts.btree_node_size);
 
                /* Want a bucket size of at least 128k, if possible: */
                dev->bucket_size = max(dev->bucket_size, 256U);
@@ -115,7 +118,8 @@ void bch2_pick_bucket_size(struct format_opts opts, struct dev_opts *dev)
        if (dev->bucket_size < opts.block_size)
                die("Bucket size cannot be smaller than block size");
 
-       if (dev->bucket_size < opts.btree_node_size)
+       if (opt_defined(opts, btree_node_size) &&
+           dev->bucket_size < opts.btree_node_size)
                die("Bucket size cannot be smaller than btree node size");
 
        if (dev->nbuckets < BCH_MIN_NR_NBUCKETS)
@@ -146,37 +150,48 @@ static unsigned parse_target(struct bch_sb_handle *sb,
        return 0;
 }
 
-struct bch_sb *bch2_format(struct format_opts opts,
-                          struct dev_opts *devs, size_t nr_devs)
+struct bch_sb *bch2_format(struct bch_opt_strs fs_opt_strs,
+                          struct bch_opts      fs_opts,
+                          struct format_opts   opts,
+                          struct dev_opts      *devs,
+                          size_t               nr_devs)
 {
        struct bch_sb_handle sb = { NULL };
        struct dev_opts *i;
        struct bch_sb_field_members *mi;
+       unsigned max_dev_block_size = 0;
+       unsigned opt_id;
+
+       for (i = devs; i < devs + nr_devs; i++)
+               max_dev_block_size = max(max_dev_block_size,
+                                        get_blocksize(i->path, i->fd));
 
        /* calculate block size: */
-       if (!opts.block_size)
-               for (i = devs; i < devs + nr_devs; i++)
-                       opts.block_size = max(opts.block_size,
-                                             get_blocksize(i->path, i->fd));
+       if (!opt_defined(fs_opts, block_size)) {
+               opt_set(fs_opts, block_size, max_dev_block_size);
+       } else if (fs_opts.block_size < max_dev_block_size)
+               die("blocksize too small: %u, must be greater than device blocksize %u",
+                   fs_opts.block_size, max_dev_block_size);
 
        /* calculate bucket sizes: */
        for (i = devs; i < devs + nr_devs; i++)
-               bch2_pick_bucket_size(opts, i);
+               bch2_pick_bucket_size(fs_opts, i);
 
        /* calculate btree node size: */
-       if (!opts.btree_node_size) {
+       if (!opt_defined(fs_opts, btree_node_size)) {
                /* 256k default btree node size */
-               opts.btree_node_size = 512;
+               opt_set(fs_opts, btree_node_size, 512);
 
                for (i = devs; i < devs + nr_devs; i++)
-                       opts.btree_node_size =
-                               min(opts.btree_node_size, i->bucket_size);
+                       fs_opts.btree_node_size =
+                               min_t(unsigned, fs_opts.btree_node_size,
+                                     i->bucket_size);
        }
 
-       if (!is_power_of_2(opts.block_size))
+       if (!is_power_of_2(fs_opts.block_size))
                die("block size must be power of 2");
 
-       if (!is_power_of_2(opts.btree_node_size))
+       if (!is_power_of_2(fs_opts.btree_node_size))
                die("btree node size must be power of 2");
 
        if (uuid_is_null(opts.uuid.b))
@@ -185,9 +200,10 @@ struct bch_sb *bch2_format(struct format_opts opts,
        if (bch2_sb_realloc(&sb, 0))
                die("insufficient memory");
 
-       sb.sb->version          = cpu_to_le64(BCH_SB_VERSION_MAX);
+       sb.sb->version          = le16_to_cpu(bcachefs_metadata_version_current);
+       sb.sb->version_min      = le16_to_cpu(bcachefs_metadata_version_current);
        sb.sb->magic            = BCACHE_MAGIC;
-       sb.sb->block_size       = cpu_to_le16(opts.block_size);
+       sb.sb->block_size       = cpu_to_le16(fs_opts.block_size);
        sb.sb->user_uuid        = opts.uuid;
        sb.sb->nr_devices       = nr_devs;
 
@@ -198,24 +214,24 @@ struct bch_sb *bch2_format(struct format_opts opts,
                       opts.label,
                       min(strlen(opts.label), sizeof(sb.sb->label)));
 
-       SET_BCH_SB_CSUM_TYPE(sb.sb,             opts.meta_csum_type);
-       SET_BCH_SB_META_CSUM_TYPE(sb.sb,        opts.meta_csum_type);
-       SET_BCH_SB_DATA_CSUM_TYPE(sb.sb,        opts.data_csum_type);
-       SET_BCH_SB_COMPRESSION_TYPE(sb.sb,      opts.compression_type);
-       SET_BCH_SB_BACKGROUND_COMPRESSION_TYPE(sb.sb,
-                                               opts.background_compression_type);
-
-       SET_BCH_SB_BTREE_NODE_SIZE(sb.sb,       opts.btree_node_size);
-       SET_BCH_SB_GC_RESERVE(sb.sb,            8);
-       SET_BCH_SB_META_REPLICAS_WANT(sb.sb,    opts.meta_replicas);
-       SET_BCH_SB_META_REPLICAS_REQ(sb.sb,     opts.meta_replicas_required);
-       SET_BCH_SB_DATA_REPLICAS_WANT(sb.sb,    opts.data_replicas);
-       SET_BCH_SB_DATA_REPLICAS_REQ(sb.sb,     opts.data_replicas_required);
-       SET_BCH_SB_ERROR_ACTION(sb.sb,          opts.on_error_action);
-       SET_BCH_SB_STR_HASH_TYPE(sb.sb,         BCH_STR_HASH_SIPHASH);
-       SET_BCH_SB_ENCODED_EXTENT_MAX_BITS(sb.sb,ilog2(opts.encoded_extent_max));
-
-       SET_BCH_SB_POSIX_ACL(sb.sb,             1);
+       for (opt_id = 0;
+            opt_id < bch2_opts_nr;
+            opt_id++) {
+               const struct bch_option *opt = &bch2_opt_table[opt_id];
+               u64 v;
+
+               if (opt->set_sb == SET_NO_SB_OPT)
+                       continue;
+
+               v = bch2_opt_defined_by_id(&fs_opts, opt_id)
+                       ? bch2_opt_get_by_id(&fs_opts, opt_id)
+                       : bch2_opt_get_by_id(&bch2_opts_default, opt_id);
+
+               opt->set_sb(sb.sb, v);
+       }
+
+       SET_BCH_SB_ENCODED_EXTENT_MAX_BITS(sb.sb,
+                               ilog2(opts.encoded_extent_max));
 
        struct timespec now;
        if (clock_gettime(CLOCK_REALTIME, &now))
@@ -224,6 +240,8 @@ struct bch_sb *bch2_format(struct format_opts opts,
        sb.sb->time_base_lo     = cpu_to_le64(now.tv_sec * NSEC_PER_SEC + now.tv_nsec);
        sb.sb->time_precision   = cpu_to_le32(1);
 
+       sb.sb->features[0] |= 1ULL << BCH_FEATURE_NEW_SIPHASH;
+
        /* Member info: */
        mi = bch2_sb_resize_members(&sb,
                        (sizeof(*mi) + sizeof(struct bch_member) *
@@ -259,11 +277,11 @@ struct bch_sb *bch2_format(struct format_opts opts,
        }
 
        SET_BCH_SB_FOREGROUND_TARGET(sb.sb,
-               parse_target(&sb, devs, nr_devs, opts.foreground_target));
+               parse_target(&sb, devs, nr_devs, fs_opt_strs.foreground_target));
        SET_BCH_SB_BACKGROUND_TARGET(sb.sb,
-               parse_target(&sb, devs, nr_devs, opts.background_target));
+               parse_target(&sb, devs, nr_devs, fs_opt_strs.background_target));
        SET_BCH_SB_PROMOTE_TARGET(sb.sb,
-               parse_target(&sb, devs, nr_devs, opts.promote_target));
+               parse_target(&sb, devs, nr_devs, fs_opt_strs.promote_target));
 
        /* Crypt: */
        if (opts.encrypted) {
@@ -277,7 +295,7 @@ struct bch_sb *bch2_format(struct format_opts opts,
        for (i = devs; i < devs + nr_devs; i++) {
                sb.sb->dev_idx = i - devs;
 
-               init_layout(&sb.sb->layout, opts.block_size,
+               init_layout(&sb.sb->layout, fs_opts.block_size,
                            i->sb_offset, i->sb_end);
 
                if (i->sb_offset == BCH_SB_SECTOR) {
@@ -470,17 +488,15 @@ static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f,
                        strcpy(group, "(none)");
                }
 
-               bch2_scnprint_flag_list(data_allowed_str,
-                                       sizeof(data_allowed_str),
-                                       bch2_data_types,
-                                       BCH_MEMBER_DATA_ALLOWED(m));
+               bch2_flags_to_text(&PBUF(data_allowed_str),
+                                  bch2_data_types,
+                                  BCH_MEMBER_DATA_ALLOWED(m));
                if (!data_allowed_str[0])
                        strcpy(data_allowed_str, "(none)");
 
-               bch2_scnprint_flag_list(data_has_str,
-                                       sizeof(data_has_str),
-                                       bch2_data_types,
-                                       get_dev_has_data(sb, i));
+               bch2_flags_to_text(&PBUF(data_has_str),
+                                  bch2_data_types,
+                                  get_dev_has_data(sb, i));
                if (!data_has_str[0])
                        strcpy(data_has_str, "(none)");
 
@@ -547,6 +563,26 @@ static void bch2_sb_print_crypt(struct bch_sb *sb, struct bch_sb_field *f,
               BCH_KDF_SCRYPT_P(crypt));
 }
 
+static void bch2_sb_print_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f,
+                                  enum units units)
+{
+       struct bch_sb_field_replicas_v0 *replicas = field_to_type(f, replicas_v0);
+       struct bch_replicas_entry_v0 *e;
+       unsigned i;
+
+       for_each_replicas_entry(replicas, e) {
+               printf_pad(32, "  %s:", bch2_data_types[e->data_type]);
+
+               putchar('[');
+               for (i = 0; i < e->nr_devs; i++) {
+                       if (i)
+                               putchar(' ');
+                       printf("%u", e->devs[i]);
+               }
+               printf("]\n");
+       }
+}
+
 static void bch2_sb_print_replicas(struct bch_sb *sb, struct bch_sb_field *f,
                                   enum units units)
 {
@@ -555,7 +591,10 @@ static void bch2_sb_print_replicas(struct bch_sb *sb, struct bch_sb_field *f,
        unsigned i;
 
        for_each_replicas_entry(replicas, e) {
-               printf_pad(32, "  %s:", bch2_data_types[e->data_type]);
+               printf_pad(32, "  %s: %u/%u",
+                          bch2_data_types[e->data_type],
+                          e->nr_required,
+                          e->nr_devs);
 
                putchar('[');
                for (i = 0; i < e->nr_devs; i++) {
@@ -582,6 +621,11 @@ static void bch2_sb_print_clean(struct bch_sb *sb, struct bch_sb_field *f,
 {
 }
 
+static void bch2_sb_print_journal_seq_blacklist(struct bch_sb *sb, struct bch_sb_field *f,
+                               enum units units)
+{
+}
+
 typedef void (*sb_field_print_fn)(struct bch_sb *, struct bch_sb_field *, enum units);
 
 struct bch_sb_field_toolops {
@@ -661,8 +705,8 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
 
        vstruct_for_each(sb, f)
                fields_have |= 1 << le32_to_cpu(f->type);
-       bch2_scnprint_flag_list(fields_have_str, sizeof(fields_have_str),
-                               bch2_sb_fields, fields_have);
+       bch2_flags_to_text(&PBUF(fields_have_str),
+                          bch2_sb_fields, fields_have);
 
        printf("External UUID:                  %s\n"
               "Internal UUID:                  %s\n"
@@ -890,7 +934,9 @@ int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
                printf("\33[2K\r");
 
                printf("%llu%% complete: current position %s",
-                      e.p.sectors_done * 100 / e.p.sectors_total,
+                      e.p.sectors_total
+                      ? e.p.sectors_done * 100 / e.p.sectors_total
+                      : 0,
                       bch2_data_types[e.p.data_type]);
 
                switch (e.p.data_type) {
@@ -909,3 +955,151 @@ int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
        close(progress_fd);
        return 0;
 }
+
+/* option parsing */
+
+struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
+                                         unsigned opt_types)
+{
+       struct bch_opt_strs opts;
+       unsigned i = 1;
+
+       memset(&opts, 0, sizeof(opts));
+
+       while (i < *argc) {
+               char *optstr = strcmp_prefix(argv[i], "--");
+               char *valstr = NULL, *p;
+               int optid, nr_args = 1;
+
+               if (!optstr) {
+                       i++;
+                       continue;
+               }
+
+               optstr = strdup(optstr);
+
+               p = optstr;
+               while (isalpha(*p) || *p == '_')
+                       p++;
+
+               if (*p == '=') {
+                       *p = '\0';
+                       valstr = p + 1;
+               }
+
+               optid = bch2_opt_lookup(optstr);
+               if (optid < 0 ||
+                   !(bch2_opt_table[optid].mode & opt_types)) {
+                       free(optstr);
+                       i++;
+                       continue;
+               }
+
+               if (!valstr &&
+                   bch2_opt_table[optid].type != BCH_OPT_BOOL) {
+                       nr_args = 2;
+                       valstr = argv[i + 1];
+               }
+
+               if (!valstr)
+                       valstr = "1";
+
+               opts.by_id[optid] = valstr;
+
+               *argc -= nr_args;
+               memmove(&argv[i],
+                       &argv[i + nr_args],
+                       sizeof(char *) * (*argc - i));
+               argv[*argc] = NULL;
+       }
+
+       return opts;
+}
+
+struct bch_opts bch2_parse_opts(struct bch_opt_strs strs)
+{
+       struct bch_opts opts = bch2_opts_empty();
+       unsigned i;
+       int ret;
+       u64 v;
+
+       for (i = 0; i < bch2_opts_nr; i++) {
+               if (!strs.by_id[i] ||
+                   bch2_opt_table[i].type == BCH_OPT_FN)
+                       continue;
+
+               ret = bch2_opt_parse(NULL, &bch2_opt_table[i],
+                                    strs.by_id[i], &v);
+               if (ret < 0)
+                       die("Invalid %s: %s", strs.by_id[i], strerror(-ret));
+
+               bch2_opt_set_by_id(&opts, i, v);
+       }
+
+       return opts;
+}
+
+void bch2_opts_usage(unsigned opt_types)
+{
+       const struct bch_option *opt;
+       unsigned i, c = 0, helpcol = 30;
+
+       void tabalign() {
+               while (c < helpcol) {
+                       putchar(' ');
+                       c++;
+               }
+       }
+
+       void newline() {
+               printf("\n");
+               c = 0;
+       }
+
+       for (opt = bch2_opt_table;
+            opt < bch2_opt_table + bch2_opts_nr;
+            opt++) {
+               if (!(opt->mode & opt_types))
+                       continue;
+
+               c += printf("      --%s", opt->attr.name);
+
+               switch (opt->type) {
+               case BCH_OPT_BOOL:
+                       break;
+               case BCH_OPT_STR:
+                       c += printf("=(");
+                       for (i = 0; opt->choices[i]; i++) {
+                               if (i)
+                                       c += printf("|");
+                               c += printf("%s", opt->choices[i]);
+                       }
+                       c += printf(")");
+                       break;
+               default:
+                       c += printf("=%s", opt->hint);
+                       break;
+               }
+
+               if (opt->help) {
+                       const char *l = opt->help;
+
+                       if (c >= helpcol)
+                               newline();
+
+                       while (1) {
+                               const char *n = strchrnul(l, '\n');
+
+                               tabalign();
+                               printf("%.*s", (int) (n - l), l);
+                               newline();
+
+                               if (!*n)
+                                       break;
+                               l = n + 1;
+                       }
+               } else {
+                       newline();
+               }
+       }
+}