btrfs-progs: use libbtrfsutil for subvol show
[platform/upstream/btrfs-progs.git] / mkfs / rootdir.c
index 0dc022b..e06b65a 100644 (file)
@@ -139,7 +139,7 @@ static int fill_inode_item(struct btrfs_trans_handle *trans,
        }
        if (S_ISREG(src->st_mode)) {
                btrfs_set_stack_inode_size(dst, (u64)src->st_size);
-               if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
+               if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info))
                        btrfs_set_stack_inode_nbytes(dst, src->st_size);
                else {
                        blocks = src->st_size / sectorsize;
@@ -327,7 +327,7 @@ static int add_file_items(struct btrfs_trans_handle *trans,
        if (st->st_size % sectorsize)
                blocks += 1;
 
-       if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
+       if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info)) {
                char *buffer = malloc(st->st_size);
 
                if (!buffer) {
@@ -453,7 +453,6 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
        ino_t parent_inum, cur_inum;
        ino_t highest_inum = 0;
        const char *parent_dir_name;
-       char real_path[PATH_MAX];
        struct btrfs_path path;
        struct extent_buffer *leaf;
        struct btrfs_key root_dir_key;
@@ -464,7 +463,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
        if (!dir_entry)
                return -ENOMEM;
        dir_entry->dir_name = dir_name;
-       dir_entry->path = realpath(dir_name, real_path);
+       dir_entry->path = realpath(dir_name, NULL);
        if (!dir_entry->path) {
                error("realpath  failed for %s: %s", dir_name, strerror(errno));
                ret = -1;
@@ -616,6 +615,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
                }
 
                free_namelist(files, count);
+               free(parent_dir_entry->path);
                free(parent_dir_entry);
 
                index_cnt = 2;
@@ -686,6 +686,7 @@ fail:
                dir_entry = list_entry(dir_head.list.next,
                                       struct directory_name_entry, list);
                list_del(&dir_entry->list);
+               free(dir_entry->path);
                free(dir_entry);
        }
 out:
@@ -899,11 +900,13 @@ err:
        return ret;
 }
 
-int btrfs_mkfs_shrink_fs(struct btrfs_fs_info *fs_info, u64 *new_size_ret)
+int btrfs_mkfs_shrink_fs(struct btrfs_fs_info *fs_info, u64 *new_size_ret,
+                        bool shrink_file_size)
 {
        u64 new_size;
        struct btrfs_device *device;
        struct list_head *cur;
+       struct stat64 file_stat;
        int nr_devs = 0;
        int ret;
 
@@ -931,5 +934,22 @@ int btrfs_mkfs_shrink_fs(struct btrfs_fs_info *fs_info, u64 *new_size_ret)
                return ret;
        if (new_size_ret)
                *new_size_ret = new_size;
+
+       if (shrink_file_size) {
+               ret = fstat64(device->fd, &file_stat);
+               if (ret < 0) {
+                       error("failed to stat devid %llu: %s", device->devid,
+                               strerror(errno));
+                       return ret;
+               }
+               if (!S_ISREG(file_stat.st_mode))
+                       return ret;
+               ret = ftruncate64(device->fd, new_size);
+               if (ret < 0) {
+                       error("failed to truncate device file of devid %llu: %s",
+                               device->devid, strerror(errno));
+                       return ret;
+               }
+       }
        return ret;
 }