btrfs-progs: tree-stats: use on-stack path buffer in calc_root_size
[platform/upstream/btrfs-progs.git] / cmds-restore.c
index 0878b8e..40806ef 100644 (file)
@@ -56,7 +56,6 @@ static int get_xattrs = 0;
 static int dry_run = 0;
 
 #define LZO_LEN 4
-#define PAGE_CACHE_SIZE 4096
 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
 
 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
@@ -93,8 +92,8 @@ static inline size_t read_compress_length(unsigned char *buf)
        return le32_to_cpu(dlen);
 }
 
-static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
-                         u64 *decompress_len)
+static int decompress_lzo(struct btrfs_root *root, unsigned char *inbuf,
+                       char *outbuf, u64 compress_len, u64 *decompress_len)
 {
        size_t new_len;
        size_t in_len;
@@ -126,8 +125,7 @@ static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
 
                inbuf += LZO_LEN;
                tot_in += LZO_LEN;
-
-               new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
+               new_len = lzo1x_worst_compress(root->sectorsize);
                ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
                                            (unsigned char *)outbuf,
                                            (void *)&new_len, NULL);
@@ -144,8 +142,8 @@ static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
                 * If the 4 byte header does not fit to the rest of the page we
                 * have to move to the next one, unless we read some garbage
                 */
-               mod_page = tot_in % PAGE_CACHE_SIZE;
-               rem_page = PAGE_CACHE_SIZE - mod_page;
+               mod_page = tot_in % root->sectorsize;
+               rem_page = root->sectorsize - mod_page;
                if (rem_page < LZO_LEN) {
                        inbuf += rem_page;
                        tot_in += rem_page;
@@ -157,16 +155,16 @@ static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
        return 0;
 }
 
-static int decompress(char *inbuf, char *outbuf, u64 compress_len,
-                     u64 *decompress_len, int compress)
+static int decompress(struct btrfs_root *root, char *inbuf, char *outbuf,
+                       u64 compress_len, u64 *decompress_len, int compress)
 {
        switch (compress) {
        case BTRFS_COMPRESS_ZLIB:
                return decompress_zlib(inbuf, outbuf, compress_len,
                                       *decompress_len);
        case BTRFS_COMPRESS_LZO:
-               return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
-                                     decompress_len);
+               return decompress_lzo(root, (unsigned char *)inbuf, outbuf,
+                                       compress_len, decompress_len);
        default:
                break;
        }
@@ -234,7 +232,8 @@ again:
        return 0;
 }
 
-static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
+static int copy_one_inline(struct btrfs_root *root, int fd,
+                               struct btrfs_path *path, u64 pos)
 {
        struct extent_buffer *leaf = path->nodes[0];
        struct btrfs_file_extent_item *fi;
@@ -273,7 +272,7 @@ static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
                return -ENOMEM;
        }
 
-       ret = decompress(buf, outbuf, len, &ram_size, compress);
+       ret = decompress(root, buf, outbuf, len, &ram_size, compress);
        if (ret) {
                free(outbuf);
                return ret;
@@ -357,7 +356,7 @@ again:
        dev_fd = device->fd;
        device->total_ios++;
        dev_bytenr = multi->stripes[0].physical;
-       kfree(multi);
+       free(multi);
 
        if (size_left < length)
                length = size_left;
@@ -401,7 +400,7 @@ again:
                goto out;
        }
 
-       ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
+       ret = decompress(root, inbuf, outbuf, disk_size, &ram_size, compress);
        if (ret) {
                num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
                                              bytenr, length);
@@ -738,7 +737,7 @@ static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
                if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
                        goto next;
                if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
-                       ret = copy_one_inline(fd, path, found_key.offset);
+                       ret = copy_one_inline(root, fd, path, found_key.offset);
                        if (ret)
                                goto out;
                } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
@@ -1234,7 +1233,7 @@ static int do_list_roots(struct btrfs_root *root)
                }
                btrfs_item_key(leaf, &disk_key, slot);
                btrfs_disk_key_to_cpu(&found_key, &disk_key);
-               if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
+               if (found_key.type != BTRFS_ROOT_ITEM_KEY) {
                        path->slots[0]++;
                        continue;
                }
@@ -1285,7 +1284,7 @@ static struct btrfs_root *open_fs(const char *dev, u64 root_location,
                        root_location = btrfs_super_root(fs_info->super_copy);
                generation = btrfs_super_generation(fs_info->super_copy);
                root->node = read_tree_block(root, root_location,
-                                            root->leafsize, generation);
+                                            root->nodesize, generation);
                if (!extent_buffer_uptodate(root->node)) {
                        fprintf(stderr, "Error opening tree root\n");
                        close_ctree(root);
@@ -1381,9 +1380,9 @@ const char * const cmd_restore_usage[] = {
        "Try to restore files from a damaged filesystem (unmounted)",
        "",
        "-s|--snapshots       get snapshots",
-       "-x|--xattr           get extended attributes",
+       "-x|--xattr           restore extended attributes",
        "-m|--metadata        restore owner, mode and times",
-       "-S|--symlinks        restore symbolic links",
+       "-S|--symlink         restore symbolic links",
        "-v|--verbose         verbose",
        "-i|--ignore-errors   ignore errors",
        "-o|--overwrite       overwrite",
@@ -1422,8 +1421,10 @@ int cmd_restore(int argc, char **argv)
 
        while (1) {
                int opt;
+               enum { GETOPT_VAL_PATH_REGEX = 256 };
                static const struct option long_options[] = {
-                       { "path-regex", required_argument, NULL, 256},
+                       { "path-regex", required_argument, NULL,
+                               GETOPT_VAL_PATH_REGEX },
                        { "dry-run", no_argument, NULL, 'D'},
                        { "metadata", no_argument, NULL, 'm'},
                        { "symlinks", no_argument, NULL, 'S'},
@@ -1496,8 +1497,7 @@ int cmd_restore(int argc, char **argv)
                        case 'c':
                                match_cflags |= REG_ICASE;
                                break;
-                       /* long option without single letter alternative */
-                       case 256:
+                       case GETOPT_VAL_PATH_REGEX:
                                match_regstr = optarg;
                                break;
                        case 'x':
@@ -1536,7 +1536,7 @@ int cmd_restore(int argc, char **argv)
 
        if (fs_location != 0) {
                free_extent_buffer(root->node);
-               root->node = read_tree_block(root, fs_location, root->leafsize, 0);
+               root->node = read_tree_block(root, fs_location, root->nodesize, 0);
                if (!extent_buffer_uptodate(root->node)) {
                        fprintf(stderr, "Failed to read fs location\n");
                        ret = 1;