btrfs-progs: introduce global config
[platform/upstream/btrfs-progs.git] / utils.c
diff --git a/utils.c b/utils.c
index f9d2774..1adcc84 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -63,6 +63,8 @@ static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
 static int rand_seed_initlized = 0;
 static unsigned short rand_seed[3];
 
+struct btrfs_config bconf;
+
 const char *get_argv0_buf(void)
 {
        return argv0_buf;
@@ -560,14 +562,18 @@ static int insert_temp_chunk_item(int fd, struct extent_buffer *buf,
         */
        if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
                char *cur;
+               u32 array_size;
 
-               cur = (char *)sb->sys_chunk_array + sb->sys_chunk_array_size;
+               cur = (char *)sb->sys_chunk_array
+                       + btrfs_super_sys_array_size(sb);
                memcpy(cur, &disk_key, sizeof(disk_key));
                cur += sizeof(disk_key);
                read_extent_buffer(buf, cur, (unsigned long int)chunk,
                                   btrfs_chunk_item_size(1));
-               sb->sys_chunk_array_size += btrfs_chunk_item_size(1) +
+               array_size = btrfs_super_sys_array_size(sb);
+               array_size += btrfs_chunk_item_size(1) +
                                            sizeof(disk_key);
+               btrfs_set_super_sys_array_size(sb, array_size);
 
                ret = write_temp_super(fd, sb, cfg->super_bytenr);
        }
@@ -939,7 +945,7 @@ out:
  *    Split into small blocks and reuse codes.
  *    TODO: Reuse tree operation facilities by introducing new flags
  */
-static int make_convert_btrfs(int fd, struct btrfs_mkfs_config *cfg,
+int make_convert_btrfs(int fd, struct btrfs_mkfs_config *cfg,
                              struct btrfs_convert_context *cctx)
 {
        struct cache_tree *free = &cctx->free;
@@ -1048,8 +1054,7 @@ out:
  * The superblock signature is not valid, denotes a partially created
  * filesystem, needs to be finalized.
  */
-int make_btrfs(int fd, struct btrfs_mkfs_config *cfg,
-               struct btrfs_convert_context *cctx)
+int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
 {
        struct btrfs_super_block super;
        struct extent_buffer *buf;
@@ -1074,8 +1079,6 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg,
                                 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
        u64 num_bytes;
 
-       if (cctx)
-               return make_convert_btrfs(fd, cfg, cctx);
        buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
        if (!buf)
                return -ENOMEM;
@@ -1460,6 +1463,7 @@ out:
 static const struct btrfs_fs_feature {
        const char *name;
        u64 flag;
+       const char *sysfs_name;
        /*
         * Compatibility with kernel of given version. Filesystem can be
         * mounted.
@@ -1481,26 +1485,31 @@ static const struct btrfs_fs_feature {
        const char *desc;
 } mkfs_features[] = {
        { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
+               "mixed_groups",
                VERSION_TO_STRING3(2,6,37),
                VERSION_TO_STRING3(2,6,37),
                NULL, 0,
                "mixed data and metadata block groups" },
        { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
+               "extended_iref",
                VERSION_TO_STRING2(3,7),
                VERSION_TO_STRING2(3,12),
                VERSION_TO_STRING2(3,12),
                "increased hardlink limit per file to 65536" },
        { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
+               "raid56",
                VERSION_TO_STRING2(3,9),
                NULL, 0,
                NULL, 0,
                "raid56 extended format" },
        { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
+               "skinny_metadata",
                VERSION_TO_STRING2(3,10),
                VERSION_TO_STRING2(3,18),
                VERSION_TO_STRING2(3,18),
                "reduced-size metadata extent refs" },
        { "no-holes", BTRFS_FEATURE_INCOMPAT_NO_HOLES,
+               "no_holes",
                VERSION_TO_STRING2(3,14),
                VERSION_TO_STRING2(4,0),
                NULL, 0,
@@ -1859,8 +1868,8 @@ int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret,
                 */
                if (discard_range(fd, 0, 0) == 0) {
                        if (opflags & PREP_DEVICE_VERBOSE)
-                               printf("Performing full device TRIM (%s) ...\n",
-                                               pretty_size(block_count));
+                               printf("Performing full device TRIM %s (%s) ...\n",
+                                               file, pretty_size(block_count));
                        discard_blocks(fd, 0, block_count);
                }
        }
@@ -2596,12 +2605,19 @@ int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mod
        int mult = 0;
        const char** suffix = NULL;
        u64 last_size;
+       int negative;
 
        if (str_size == 0)
                return 0;
 
+       negative = !!(unit_mode & UNITS_NEGATIVE);
+       unit_mode &= ~UNITS_NEGATIVE;
+
        if ((unit_mode & ~UNITS_MODE_MASK) == UNITS_RAW) {
-               snprintf(str, str_size, "%llu", size);
+               if (negative)
+                       snprintf(str, str_size, "%lld", size);
+               else
+                       snprintf(str, str_size, "%llu", size);
                return 0;
        }
 
@@ -2636,10 +2652,22 @@ int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mod
                           num_divs = 0;
                           break;
        default:
-               while (size >= mult) {
-                       last_size = size;
-                       size /= mult;
-                       num_divs++;
+               if (negative) {
+                       s64 ssize = (s64)size;
+                       s64 last_ssize = ssize;
+
+                       while ((ssize < 0 ? -ssize : ssize) >= mult) {
+                               last_ssize = ssize;
+                               ssize /= mult;
+                               num_divs++;
+                       }
+                       last_size = (u64)last_ssize;
+               } else {
+                       while (size >= mult) {
+                               last_size = size;
+                               size /= mult;
+                               num_divs++;
+                       }
                }
                /*
                 * If the value is smaller than base, we didn't do any
@@ -2657,7 +2685,12 @@ int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mod
                assert(0);
                return -1;
        }
-       fraction = (float)last_size / base;
+
+       if (negative) {
+               fraction = (float)(s64)last_size / base;
+       } else {
+               fraction = (float)last_size / base;
+       }
 
        return snprintf(str, str_size, "%.2f%s", fraction, suffix[num_divs]);
 }
@@ -3641,31 +3674,6 @@ int ask_user(const char *question)
 }
 
 /*
- * For a given:
- * - file or directory return the containing tree root id
- * - subvolume return its own tree id
- * - BTRFS_EMPTY_SUBVOL_DIR_OBJECTID (directory with ino == 2) the result is
- *   undefined and function returns -1
- */
-int lookup_path_rootid(int fd, u64 *rootid)
-{
-       struct btrfs_ioctl_ino_lookup_args args;
-       int ret;
-
-       memset(&args, 0, sizeof(args));
-       args.treeid = 0;
-       args.objectid = BTRFS_FIRST_FREE_OBJECTID;
-
-       ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
-       if (ret < 0)
-               return -errno;
-
-       *rootid = args.treeid;
-
-       return 0;
-}
-
-/*
  * return 0 if a btrfs mount point is found
  * return 1 if a mount point is found but not btrfs
  * return <0 if something goes wrong
@@ -4050,6 +4058,8 @@ unsigned int get_unit_mode_from_arg(int *argc, char *argv[], int df_mode)
 
 int string_is_numerical(const char *str)
 {
+       if (!str)
+               return 0;
        if (!(*str >= '0' && *str <= '9'))
                return 0;
        while (*str >= '0' && *str <= '9')
@@ -4279,3 +4289,7 @@ unsigned int rand_range(unsigned int upper)
         */
        return (unsigned int)(jrand48(rand_seed) % upper);
 }
+
+void btrfs_config_init(void)
+{
+}