btrfs-progs: build btrfs.static needs libbtrfsutil to build
[platform/upstream/btrfs-progs.git] / mkfs / main.c
index 9bb1f60..0337993 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 /* #include <sys/dir.h> included via androidcompat.h */
 #include <fcntl.h>
+#include <limits.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <uuid/uuid.h>
@@ -72,7 +73,6 @@ static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
         */
        ret = btrfs_make_block_group(trans, fs_info, bytes_used,
                                     BTRFS_BLOCK_GROUP_SYSTEM,
-                                    BTRFS_FIRST_CHUNK_TREE_OBJECTID,
                                     BTRFS_BLOCK_RESERVED_1M_FOR_SUPER,
                                     BTRFS_MKFS_SYSTEM_GROUP_SIZE);
        allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
@@ -93,7 +93,6 @@ static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
                ret = btrfs_make_block_group(trans, fs_info, 0,
                                             BTRFS_BLOCK_GROUP_METADATA |
                                             BTRFS_BLOCK_GROUP_DATA,
-                                            BTRFS_FIRST_CHUNK_TREE_OBJECTID,
                                             chunk_start, chunk_size);
                if (ret)
                        return ret;
@@ -110,7 +109,6 @@ static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
                        return ret;
                ret = btrfs_make_block_group(trans, fs_info, 0,
                                             BTRFS_BLOCK_GROUP_METADATA,
-                                            BTRFS_FIRST_CHUNK_TREE_OBJECTID,
                                             chunk_start, chunk_size);
                allocation->metadata += chunk_size;
                if (ret)
@@ -145,7 +143,6 @@ static int create_data_block_groups(struct btrfs_trans_handle *trans,
                        return ret;
                ret = btrfs_make_block_group(trans, fs_info, 0,
                                             BTRFS_BLOCK_GROUP_DATA,
-                                            BTRFS_FIRST_CHUNK_TREE_OBJECTID,
                                             chunk_start, chunk_size);
                allocation->data += chunk_size;
                if (ret)
@@ -254,8 +251,7 @@ static int create_one_raid_group(struct btrfs_trans_handle *trans,
                return ret;
 
        ret = btrfs_make_block_group(trans, fs_info, 0,
-                                    type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
-                                    chunk_start, chunk_size);
+                                    type, chunk_start, chunk_size);
 
        type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
        if (type == BTRFS_BLOCK_GROUP_DATA) {
@@ -319,6 +315,7 @@ static int create_tree(struct btrfs_trans_handle *trans,
        struct btrfs_key location;
        struct btrfs_root_item root_item;
        struct extent_buffer *tmp;
+       u8 uuid[BTRFS_UUID_SIZE] = {0};
        int ret;
 
        ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
@@ -329,6 +326,10 @@ static int create_tree(struct btrfs_trans_handle *trans,
        btrfs_set_root_bytenr(&root_item, tmp->start);
        btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
        btrfs_set_root_generation(&root_item, trans->transid);
+       /* clear uuid and o/ctime of source tree */
+       memcpy(root_item.uuid, uuid, BTRFS_UUID_SIZE);
+       btrfs_set_stack_timespec_sec(&root_item.otime, 0);
+       btrfs_set_stack_timespec_sec(&root_item.ctime, 0);
        free_extent_buffer(tmp);
 
        location.objectid = objectid;
@@ -985,12 +986,13 @@ int main(int argc, char **argv)
                fd = open(file, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
                                         S_IROTH);
                if (fd < 0) {
-                       error("unable to open %s: %s", file, strerror(errno));
+                       error("unable to open %s: %m", file);
                        goto error;
                }
+
                ret = fstat(fd, &statbuf);
                if (ret < 0) {
-                       error("unable to stat %s: %s", file, strerror(errno));
+                       error("unable to stat %s: %m", file);
                        ret = -errno;
                        goto error;
                }
@@ -1029,8 +1031,7 @@ int main(int argc, char **argv)
                path = argv[i];
                ret = test_minimum_size(path, min_dev_size);
                if (ret < 0) {
-                       error("failed to check size for %s: %s",
-                               path, strerror(-ret));
+                       error("failed to check size for %s: %m", path);
                        goto error;
                }
                if (ret > 0) {
@@ -1055,7 +1056,7 @@ int main(int argc, char **argv)
         */
        fd = open(file, O_RDWR);
        if (fd < 0) {
-               error("unable to open %s: %s", file, strerror(errno));
+               error("unable to open %s: %m", file);
                goto error;
        }
        ret = btrfs_prepare_device(fd, file, &dev_block_count, block_count,
@@ -1157,7 +1158,7 @@ int main(int argc, char **argv)
                 */
                fd = open(file, O_RDWR);
                if (fd < 0) {
-                       error("unable to open %s: %s", file, strerror(errno));
+                       error("unable to open %s: %m", file);
                        goto error;
                }
                ret = btrfs_device_already_in_root(root, fd,
@@ -1207,6 +1208,11 @@ raid_groups:
                goto out;
        }
 
+       ret = create_tree(trans, root, BTRFS_UUID_TREE_OBJECTID);
+       if (ret)
+               warning(
+       "unable to create uuid tree, will be created after mount: %d", ret);
+
        ret = btrfs_commit_transaction(trans, root);
        if (ret) {
                error("unable to commit transaction: %d", ret);
@@ -1289,6 +1295,12 @@ out:
                }
        }
 
+       if (!ret && close_ret) {
+               ret = close_ret;
+               error("failed to close ctree, the filesystem may be inconsistent: %d",
+                     ret);
+       }
+
        btrfs_close_all_devices();
        free(label);