]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs.c
Makefile: rename D to BCACHEFS_DEBUG
[bcachefs-tools-debian] / libbcachefs.c
index e359d48b758d3a105848a00d9265d1ca849aa265..65ca07e2d7d57e13bab3f1a977e8bd9a2b01aa58 100644 (file)
@@ -35,52 +35,35 @@ static u64 min_size(unsigned bucket_size)
        return BCH_MIN_NR_NBUCKETS * bucket_size;
 }
 
-static void init_layout(struct bch_sb_layout *l, unsigned block_size,
+static void init_layout(struct bch_sb_layout *l,
+                       unsigned block_size,
+                       unsigned sb_size,
                        u64 start, u64 end)
 {
-       unsigned sb_size;
-       u64 backup; /* offset of 2nd sb */
+       unsigned i;
 
        memset(l, 0, sizeof(*l));
 
-       if (start != BCH_SB_SECTOR)
-               start = round_up(start, block_size);
-       end = round_down(end, block_size);
-
-       if (start >= end)
-               die("insufficient space for superblocks");
-
-       /*
-        * Create two superblocks in the allowed range: reserve a maximum of 64k
-        */
-       sb_size = min_t(u64, 128, end - start / 2);
-
-       backup = start + sb_size;
-       backup = round_up(backup, block_size);
-
-       backup = min(backup, end);
-
-       sb_size = min(end - backup, backup- start);
-       sb_size = rounddown_pow_of_two(sb_size);
-
-       if (sb_size < 8)
-               die("insufficient space for superblocks");
-
        l->magic                = BCACHE_MAGIC;
        l->layout_type          = 0;
        l->nr_superblocks       = 2;
        l->sb_max_size_bits     = ilog2(sb_size);
-       l->sb_offset[0]         = cpu_to_le64(start);
-       l->sb_offset[1]         = cpu_to_le64(backup);
+
+       /* Create two superblocks in the allowed range: */
+       for (i = 0; i < l->nr_superblocks; i++) {
+               if (start != BCH_SB_SECTOR)
+                       start = round_up(start, block_size);
+
+               l->sb_offset[i] = cpu_to_le64(start);
+               start += sb_size;
+       }
+
+       if (start >= end)
+               die("insufficient space for superblocks");
 }
 
 void bch2_pick_bucket_size(struct bch_opts opts, struct dev_opts *dev)
 {
-       if (!dev->sb_offset) {
-               dev->sb_offset  = BCH_SB_SECTOR;
-               dev->sb_end     = BCH_SB_SECTOR + 256;
-       }
-
        if (!dev->size)
                dev->size = get_size(dev->path, dev->fd) >> 9;
 
@@ -202,13 +185,16 @@ struct bch_sb *bch2_format(struct bch_opt_strs    fs_opt_strs,
        if (bch2_sb_realloc(&sb, 0))
                die("insufficient memory");
 
-       sb.sb->version          = le16_to_cpu(bcachefs_metadata_version_current);
-       sb.sb->version_min      = le16_to_cpu(bcachefs_metadata_version_current);
+       sb.sb->version          = le16_to_cpu(opts.version);
+       sb.sb->version_min      = le16_to_cpu(opts.version);
        sb.sb->magic            = BCACHE_MAGIC;
        sb.sb->block_size       = cpu_to_le16(fs_opts.block_size);
        sb.sb->user_uuid        = opts.uuid;
        sb.sb->nr_devices       = nr_devs;
 
+       if (opts.version == bcachefs_metadata_version_current)
+               sb.sb->features[0] |= BCH_SB_FEATURES_ALL;
+
        uuid_generate(sb.sb->uuid.b);
 
        if (opts.label)
@@ -255,7 +241,7 @@ struct bch_sb *bch2_format(struct bch_opt_strs      fs_opt_strs,
                m->first_bucket = 0;
                m->bucket_size  = cpu_to_le16(i->bucket_size);
 
-               SET_BCH_MEMBER_REPLACEMENT(m,   CACHE_REPLACEMENT_LRU);
+               SET_BCH_MEMBER_REPLACEMENT(m,   BCH_CACHE_REPLACEMENT_lru);
                SET_BCH_MEMBER_DISCARD(m,       i->discard);
                SET_BCH_MEMBER_DATA_ALLOWED(m,  i->data_allowed);
                SET_BCH_MEMBER_DURABILITY(m,    i->durability + 1);
@@ -282,6 +268,8 @@ struct bch_sb *bch2_format(struct bch_opt_strs      fs_opt_strs,
                parse_target(&sb, devs, nr_devs, fs_opt_strs.background_target));
        SET_BCH_SB_PROMOTE_TARGET(sb.sb,
                parse_target(&sb, devs, nr_devs, fs_opt_strs.promote_target));
+       SET_BCH_SB_METADATA_TARGET(sb.sb,
+               parse_target(&sb, devs, nr_devs, fs_opt_strs.metadata_target));
 
        /* Crypt: */
        if (opts.encrypted) {
@@ -295,7 +283,13 @@ struct bch_sb *bch2_format(struct bch_opt_strs     fs_opt_strs,
        for (i = devs; i < devs + nr_devs; i++) {
                sb.sb->dev_idx = i - devs;
 
+               if (!i->sb_offset) {
+                       i->sb_offset    = BCH_SB_SECTOR;
+                       i->sb_end       = i->size;
+               }
+
                init_layout(&sb.sb->layout, fs_opts.block_size,
+                           opts.superblock_size,
                            i->sb_offset, i->sb_end);
 
                if (i->sb_offset == BCH_SB_SECTOR) {
@@ -533,14 +527,14 @@ static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f,
                       time_str,
 
                       BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
-                      ? bch2_dev_state[BCH_MEMBER_STATE(m)]
+                      ? bch2_member_states[BCH_MEMBER_STATE(m)]
                       : "unknown",
 
                       group,
                       data_allowed_str,
                       data_has_str,
 
-                      BCH_MEMBER_REPLACEMENT(m) < CACHE_REPLACEMENT_NR
+                      BCH_MEMBER_REPLACEMENT(m) < BCH_CACHE_REPLACEMENT_NR
                       ? bch2_cache_replacement_policies[BCH_MEMBER_REPLACEMENT(m)]
                       : "unknown",
 
@@ -675,12 +669,14 @@ 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 compat_features_str[500];
        char fields_have_str[200];
        char label[BCH_SB_LABEL_SIZE + 1];
        char time_str[64];
        char foreground_str[64];
        char background_str[64];
        char promote_str[64];
+       char metadata_str[64];
        struct bch_sb_field *f;
        u64 fields_have = 0;
        unsigned nr_devices = 0;
@@ -720,10 +716,17 @@ 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_sb_get_target(sb, metadata_str, sizeof(metadata_str),
+               BCH_SB_METADATA_TARGET(sb));
+
        bch2_flags_to_text(&PBUF(features_str),
                           bch2_sb_features,
                           le64_to_cpu(sb->features[0]));
 
+       bch2_flags_to_text(&PBUF(compat_features_str),
+                          bch2_sb_compat,
+                          le64_to_cpu(sb->compat[0]));
+
        vstruct_for_each(sb, f)
                fields_have |= 1 << le32_to_cpu(f->type);
        bch2_flags_to_text(&PBUF(fields_have_str),
@@ -733,7 +736,8 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
               "Internal UUID:                  %s\n"
               "Device index:                   %u\n"
               "Label:                          %s\n"
-              "Version:                        %llu\n"
+              "Version:                        %u\n"
+              "Oldest version on disk:         %u\n"
               "Created:                        %s\n"
               "Squence number:                 %llu\n"
               "Block_size:                     %s\n"
@@ -741,6 +745,7 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
               "Error action:                   %s\n"
               "Clean:                          %llu\n"
               "Features:                       %s\n"
+              "Compat features:                %s\n"
 
               "Metadata replicas:              %llu\n"
               "Data replicas:                  %llu\n"
@@ -752,6 +757,7 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
               "Foreground write target:        %s\n"
               "Background write target:        %s\n"
               "Promote target:                 %s\n"
+               "Metadata target:                %s\n"
 
               "String hash type:               %s (%llu)\n"
               "32 bit inodes:                  %llu\n"
@@ -765,18 +771,20 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
               internal_uuid_str,
               sb->dev_idx,
               label,
-              le64_to_cpu(sb->version),
+              le16_to_cpu(sb->version),
+              le16_to_cpu(sb->version_min),
               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),
 
-              BCH_SB_ERROR_ACTION(sb) < BCH_NR_ERROR_ACTIONS
+              BCH_SB_ERROR_ACTION(sb) < BCH_ON_ERROR_NR
               ? bch2_error_actions[BCH_SB_ERROR_ACTION(sb)]
               : "unknown",
 
               BCH_SB_CLEAN(sb),
               features_str,
+              compat_features_str,
 
               BCH_SB_META_REPLICAS_WANT(sb),
               BCH_SB_DATA_REPLICAS_WANT(sb),
@@ -799,6 +807,7 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout,
               foreground_str,
               background_str,
               promote_str,
+               metadata_str,
 
               BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR
               ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)]
@@ -898,7 +907,7 @@ struct bchfs_handle bcache_fs_open(const char *path)
  * Given a path to a block device, open the filesystem it belongs to; also
  * return the device's idx:
  */
-struct bchfs_handle bchu_fs_open_by_dev(const char *path, unsigned *idx)
+struct bchfs_handle bchu_fs_open_by_dev(const char *path, int *idx)
 {
        char buf[1024], *uuid_str;
 
@@ -942,6 +951,17 @@ struct bchfs_handle bchu_fs_open_by_dev(const char *path, unsigned *idx)
        return bcache_fs_open(uuid_str);
 }
 
+int bchu_dev_path_to_idx(struct bchfs_handle fs, const char *dev_path)
+{
+       int idx;
+       struct bchfs_handle fs2 = bchu_fs_open_by_dev(dev_path, &idx);
+
+       if (memcmp(&fs.uuid, &fs2.uuid, sizeof(fs.uuid)))
+               idx = -1;
+       bcache_fs_close(fs2);
+       return idx;
+}
+
 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
 {
        int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
@@ -986,6 +1006,16 @@ int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
 
 /* option parsing */
 
+void bch2_opt_strs_free(struct bch_opt_strs *opts)
+{
+       unsigned i;
+
+       for (i = 0; i < bch2_opts_nr; i++) {
+               free(opts->by_id[i]);
+               opts->by_id[i] = NULL;
+       }
+}
+
 struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
                                          unsigned opt_types)
 {
@@ -1018,9 +1048,8 @@ struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
                optid = bch2_opt_lookup(optstr);
                if (optid < 0 ||
                    !(bch2_opt_table[optid].mode & opt_types)) {
-                       free(optstr);
                        i++;
-                       continue;
+                       goto next;
                }
 
                if (!valstr &&
@@ -1032,13 +1061,15 @@ struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
                if (!valstr)
                        valstr = "1";
 
-               opts.by_id[optid] = valstr;
+               opts.by_id[optid] = strdup(valstr);
 
                *argc -= nr_args;
                memmove(&argv[i],
                        &argv[i + nr_args],
                        sizeof(char *) * (*argc - i));
                argv[*argc] = NULL;
+next:
+               free(optstr);
        }
 
        return opts;
@@ -1069,22 +1100,17 @@ struct bch_opts bch2_parse_opts(struct bch_opt_strs strs)
        return opts;
 }
 
+#define newline(c)             \
+       do {                    \
+               printf("\n");   \
+               c = 0;          \
+       } while(0)
 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;
@@ -1115,21 +1141,24 @@ void bch2_opts_usage(unsigned opt_types)
                        const char *l = opt->help;
 
                        if (c >= helpcol)
-                               newline();
+                               newline(c);
 
                        while (1) {
                                const char *n = strchrnul(l, '\n');
 
-                               tabalign();
+                               while (c < helpcol) {
+                                       putchar(' ');
+                                       c++;
+                               }
                                printf("%.*s", (int) (n - l), l);
-                               newline();
+                               newline(c);
 
                                if (!*n)
                                        break;
                                l = n + 1;
                        }
                } else {
-                       newline();
+                       newline(c);
                }
        }
 }