]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs.c
Update bcachefs sources to ea3414eed5 fixup! bcachefs: Improve diagnostics when journ...
[bcachefs-tools-debian] / libbcachefs.c
index b24e7f37c33fe04ddffac3647150a626855b5e0d..e7c1ca23224ca721217e4ae14a7ec456c4df9ac3 100644 (file)
@@ -1,4 +1,5 @@
 #include <ctype.h>
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdbool.h>
 #include "libbcachefs/btree_cache.h"
 #include "libbcachefs/checksum.h"
 #include "libbcachefs/disk_groups.h"
+#include "libbcachefs/journal_seq_blacklist.h"
 #include "libbcachefs/opts.h"
 #include "libbcachefs/replicas.h"
 #include "libbcachefs/super-io.h"
+#include "tools-util.h"
 
 #define NSEC_PER_SEC   1000000000L
 
@@ -71,7 +74,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 +93,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 +120,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 +152,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))
@@ -188,7 +205,7 @@ struct bch_sb *bch2_format(struct format_opts opts,
        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;
 
@@ -199,24 +216,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))
@@ -260,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) {
@@ -278,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) {
@@ -602,6 +619,29 @@ static void bch2_sb_print_disk_groups(struct bch_sb *sb, struct bch_sb_field *f,
 static void bch2_sb_print_clean(struct bch_sb *sb, struct bch_sb_field *f,
                                enum units units)
 {
+       struct bch_sb_field_clean *clean = field_to_type(f, clean);
+
+
+       printf("  flags:       %x", le32_to_cpu(clean->flags));
+       printf("  read clock:  %x", le16_to_cpu(clean->read_clock));
+       printf("  write clock: %x", le16_to_cpu(clean->write_clock));
+       printf("  journal seq: %llx", le64_to_cpu(clean->journal_seq));
+}
+
+static void bch2_sb_print_journal_seq_blacklist(struct bch_sb *sb, struct bch_sb_field *f,
+                               enum units units)
+{
+       struct bch_sb_field_journal_seq_blacklist *bl = field_to_type(f, journal_seq_blacklist);
+       unsigned i, nr = blacklist_nr_entries(bl);
+
+       for (i = 0; i < nr; i++) {
+               struct journal_seq_blacklist_entry *e =
+                       bl->start + i;
+
+               printf("  %llu-%llu\n",
+                      le64_to_cpu(e->start),
+                      le64_to_cpu(e->end));
+       }
 }
 
 typedef void (*sb_field_print_fn)(struct bch_sb *, struct bch_sb_field *, enum units);
@@ -636,6 +676,7 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
 {
        struct bch_sb_field_members *mi;
        char user_uuid_str[40], internal_uuid_str[40];
+       char features_str[500];
        char fields_have_str[200];
        char label[BCH_SB_LABEL_SIZE + 1];
        char time_str[64];
@@ -681,6 +722,10 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
        bch2_sb_get_target(sb, promote_str, sizeof(promote_str),
                BCH_SB_PROMOTE_TARGET(sb));
 
+       bch2_flags_to_text(&PBUF(features_str),
+                          bch2_sb_features,
+                          le64_to_cpu(sb->features[0]));
+
        vstruct_for_each(sb, f)
                fields_have |= 1 << le32_to_cpu(f->type);
        bch2_flags_to_text(&PBUF(fields_have_str),
@@ -688,13 +733,16 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
 
        printf("External UUID:                  %s\n"
               "Internal UUID:                  %s\n"
+              "Device index:                   %u\n"
               "Label:                          %s\n"
               "Version:                        %llu\n"
               "Created:                        %s\n"
+              "Squence number:                 %llu\n"
               "Block_size:                     %s\n"
               "Btree node size:                %s\n"
               "Error action:                   %s\n"
               "Clean:                          %llu\n"
+              "Features:                       %s\n"
 
               "Metadata replicas:              %llu\n"
               "Data replicas:                  %llu\n"
@@ -717,9 +765,11 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
               "Superblock size:                %llu\n",
               user_uuid_str,
               internal_uuid_str,
+              sb->dev_idx,
               label,
               le64_to_cpu(sb->version),
               time_str,
+              le64_to_cpu(sb->seq),
               pr_units(le16_to_cpu(sb->block_size), units),
               pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units),
 
@@ -728,22 +778,23 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
               : "unknown",
 
               BCH_SB_CLEAN(sb),
+              features_str,
 
               BCH_SB_META_REPLICAS_WANT(sb),
               BCH_SB_DATA_REPLICAS_WANT(sb),
 
               BCH_SB_META_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
-              ? bch2_csum_types[BCH_SB_META_CSUM_TYPE(sb)]
+              ? bch2_csum_opts[BCH_SB_META_CSUM_TYPE(sb)]
               : "unknown",
               BCH_SB_META_CSUM_TYPE(sb),
 
               BCH_SB_DATA_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
-              ? bch2_csum_types[BCH_SB_DATA_CSUM_TYPE(sb)]
+              ? bch2_csum_opts[BCH_SB_DATA_CSUM_TYPE(sb)]
               : "unknown",
               BCH_SB_DATA_CSUM_TYPE(sb),
 
               BCH_SB_COMPRESSION_TYPE(sb) < BCH_COMPRESSION_OPT_NR
-              ? bch2_compression_types[BCH_SB_COMPRESSION_TYPE(sb)]
+              ? bch2_compression_opts[BCH_SB_COMPRESSION_TYPE(sb)]
               : "unknown",
               BCH_SB_COMPRESSION_TYPE(sb),
 
@@ -912,18 +963,21 @@ 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) {
-               case BCH_DATA_BTREE:
-               case BCH_DATA_USER:
+               case BCH_DATA_btree:
+               case BCH_DATA_user:
                        printf(" %s:%llu:%llu",
                               bch2_btree_ids[e.p.btree_id],
                               e.p.pos.inode,
                               e.p.pos.offset);
                }
 
+               fflush(stdout);
                sleep(1);
        }
        printf("\nDone\n");
@@ -931,3 +985,191 @@ 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",
+                           bch2_opt_table[i].attr.name,
+                           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();
+               }
+       }
+}
+
+dev_names bchu_fs_get_devices(struct bchfs_handle fs)
+{
+       DIR *dir = fdopendir(fs.sysfs_fd);
+       struct dirent *d;
+       dev_names devs;
+
+       darray_init(devs);
+
+       while ((errno = 0), (d = readdir(dir))) {
+               struct dev_name n = { 0, NULL, NULL };
+
+               if (sscanf(d->d_name, "dev-%u", &n.idx) != 1)
+                       continue;
+
+               char *block_attr = mprintf("dev-%u/block", n.idx);
+
+               char sysfs_block_buf[4096];
+               ssize_t r = readlinkat(fs.sysfs_fd, block_attr,
+                                      sysfs_block_buf, sizeof(sysfs_block_buf));
+               if (r > 0) {
+                       sysfs_block_buf[r] = '\0';
+                       n.dev = strdup(basename(sysfs_block_buf));
+               }
+
+               free(block_attr);
+
+               char *label_attr = mprintf("dev-%u/label", n.idx);
+               n.label = read_file_str(fs.sysfs_fd, label_attr);
+               free(label_attr);
+
+               darray_append(devs, n);
+       }
+
+       closedir(dir);
+
+       return devs;
+}