X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libbcachefs.c;h=7c9687d35acff0cd54641a0917703a167f10880b;hb=1a8288cba2aefc918b53496115690ca65119819a;hp=df27d59da62fbdb0c67d43c3ff039659288a2bc5;hpb=03c79a9a258669bb18bb54da805353049d4811d0;p=bcachefs-tools-debian diff --git a/libbcachefs.c b/libbcachefs.c index df27d59..7c9687d 100644 --- a/libbcachefs.c +++ b/libbcachefs.c @@ -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) { @@ -351,6 +369,52 @@ static unsigned get_dev_has_data(struct bch_sb *sb, unsigned dev) return data_has; } +static int bch2_sb_get_target(struct bch_sb *sb, char *buf, size_t len, u64 v) +{ + struct target t = target_decode(v); + int ret; + + switch (t.type) { + case TARGET_NULL: + return scnprintf(buf, len, "none"); + case TARGET_DEV: { + struct bch_sb_field_members *mi = bch2_sb_get_members(sb); + struct bch_member *m = mi->members + t.dev; + + if (bch2_dev_exists(sb, mi, t.dev)) { + char uuid_str[40]; + + uuid_unparse(m->uuid.b, uuid_str); + + ret = scnprintf(buf, len, "Device %u (%s)", t.dev, + uuid_str); + } else { + ret = scnprintf(buf, len, "Bad device %u", t.dev); + } + + break; + } + case TARGET_GROUP: { + struct bch_sb_field_disk_groups *gi; + gi = bch2_sb_get_disk_groups(sb); + + struct bch_disk_group *g = gi->entries + t.group; + + if (t.group < disk_groups_nr(gi) && !BCH_GROUP_DELETED(g)) { + ret = scnprintf(buf, len, "Group %u (%.*s)", t.group, + BCH_SB_LABEL_SIZE, g->label); + } else { + ret = scnprintf(buf, len, "Bad group %u", t.group); + } + break; + } + default: + BUG(); + } + + return ret; +} + /* superblock printing: */ static void bch2_sb_print_layout(struct bch_sb *sb, enum units units) @@ -402,7 +466,7 @@ static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f, char member_uuid_str[40]; char data_allowed_str[100]; char data_has_str[100]; - char group[64]; + char group[BCH_SB_LABEL_SIZE+10]; char time_str[64]; if (!bch2_member_exists(m)) @@ -414,25 +478,25 @@ static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f, unsigned idx = BCH_MEMBER_GROUP(m) - 1; if (idx < disk_groups_nr(gi)) { - memcpy(group, gi->entries[idx].label, - BCH_SB_LABEL_SIZE); - group[BCH_SB_LABEL_SIZE] = '\0'; + snprintf(group, sizeof(group), "%.*s (%u)", + BCH_SB_LABEL_SIZE, + gi->entries[idx].label, idx); } else { - strcpy(group, "(bad disk groups section"); + strcpy(group, "(bad disk groups section)"); } + } else { + 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)"); @@ -499,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) { @@ -507,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++) { @@ -534,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 { @@ -568,15 +660,30 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout, char user_uuid_str[40], internal_uuid_str[40]; 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]; struct bch_sb_field *f; u64 fields_have = 0; unsigned nr_devices = 0; + time_t time_base = le64_to_cpu(sb->time_base_lo) / NSEC_PER_SEC; + + memcpy(label, sb->label, BCH_SB_LABEL_SIZE); + label[BCH_SB_LABEL_SIZE] = '\0'; - memset(label, 0, sizeof(label)); - memcpy(label, sb->label, sizeof(sb->label)); uuid_unparse(sb->user_uuid.b, user_uuid_str); uuid_unparse(sb->uuid.b, internal_uuid_str); + if (time_base) { + struct tm *tm = localtime(&time_base); + size_t err = strftime(time_str, sizeof(time_str), "%c", tm); + if (!err) + strcpy(time_str, "(formatting error)"); + } else { + strcpy(time_str, "(not set)"); + } + mi = bch2_sb_get_members(sb); if (mi) { struct bch_member *m; @@ -587,15 +694,25 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout, nr_devices += bch2_member_exists(m); } + bch2_sb_get_target(sb, foreground_str, sizeof(foreground_str), + BCH_SB_FOREGROUND_TARGET(sb)); + + bch2_sb_get_target(sb, background_str, sizeof(background_str), + BCH_SB_BACKGROUND_TARGET(sb)); + + bch2_sb_get_target(sb, promote_str, sizeof(promote_str), + BCH_SB_PROMOTE_TARGET(sb)); + 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" "Label: %s\n" "Version: %llu\n" + "Created: %s\n" "Block_size: %s\n" "Btree node size: %s\n" "Error action: %s\n" @@ -608,9 +725,9 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout, "Data checksum type: %s (%llu)\n" "Compression type: %s (%llu)\n" - "Foreground write target: %llu\n" - "Background write target: %llu\n" - "Promote target: %llu\n" + "Foreground write target: %s\n" + "Background write target: %s\n" + "Promote target: %s\n" "String hash type: %s (%llu)\n" "32 bit inodes: %llu\n" @@ -624,6 +741,7 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout, internal_uuid_str, label, le64_to_cpu(sb->version), + time_str, pr_units(le16_to_cpu(sb->block_size), units), pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units), @@ -651,9 +769,9 @@ void bch2_sb_print(struct bch_sb *sb, bool print_layout, : "unknown", BCH_SB_COMPRESSION_TYPE(sb), - BCH_SB_FOREGROUND_TARGET(sb), - BCH_SB_BACKGROUND_TARGET(sb), - BCH_SB_PROMOTE_TARGET(sb), + foreground_str, + background_str, + promote_str, BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)] @@ -816,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) { @@ -835,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(); + } + } +}