X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=cmds-restore.c;h=f228acab8276fbd7a5175f946c58e7b1fcb7c3a2;hb=HEAD;hp=b5741a8a6c47e56edda29c3344d95d7ba5f52834;hpb=047f343721b1b1298a606ca31d90cd9b507abb72;p=platform%2Fupstream%2Fbtrfs-progs.git diff --git a/cmds-restore.c b/cmds-restore.c index b5741a8..f228aca 100644 --- a/cmds-restore.c +++ b/cmds-restore.c @@ -29,6 +29,9 @@ #include #include #include +#if BTRFSRESTORE_ZSTD +#include +#endif #include #include #include @@ -42,6 +45,7 @@ #include "volumes.h" #include "utils.h" #include "commands.h" +#include "help.h" static char fs_name[PATH_MAX]; static char path_name[PATH_MAX]; @@ -125,7 +129,7 @@ static int decompress_lzo(struct btrfs_root *root, unsigned char *inbuf, inbuf += LZO_LEN; tot_in += LZO_LEN; - new_len = lzo1x_worst_compress(root->sectorsize); + new_len = lzo1x_worst_compress(root->fs_info->sectorsize); ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len, (unsigned char *)outbuf, (void *)&new_len, NULL); @@ -142,8 +146,8 @@ static int decompress_lzo(struct btrfs_root *root, unsigned char *inbuf, * 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 % root->sectorsize; - rem_page = root->sectorsize - mod_page; + mod_page = tot_in % root->fs_info->sectorsize; + rem_page = root->fs_info->sectorsize - mod_page; if (rem_page < LZO_LEN) { inbuf += rem_page; tot_in += rem_page; @@ -155,6 +159,50 @@ static int decompress_lzo(struct btrfs_root *root, unsigned char *inbuf, return 0; } +static int decompress_zstd(const char *inbuf, char *outbuf, u64 compress_len, + u64 decompress_len) +{ +#if !BTRFSRESTORE_ZSTD + error("btrfs not compiled with zstd support"); + return -1; +#else + ZSTD_DStream *strm; + size_t zret; + int ret = 0; + ZSTD_inBuffer in = {inbuf, compress_len, 0}; + ZSTD_outBuffer out = {outbuf, decompress_len, 0}; + + strm = ZSTD_createDStream(); + if (!strm) { + error("zstd create failed"); + return -1; + } + + zret = ZSTD_initDStream(strm); + if (ZSTD_isError(zret)) { + error("zstd init failed: %s", ZSTD_getErrorName(zret)); + ret = -1; + goto out; + } + + zret = ZSTD_decompressStream(strm, &out, &in); + if (ZSTD_isError(zret)) { + error("zstd decompress failed %s\n", ZSTD_getErrorName(zret)); + ret = -1; + goto out; + } + if (zret != 0) { + error("zstd frame incomplete"); + ret = -1; + goto out; + } + +out: + ZSTD_freeDStream(strm); + return ret; +#endif +} + static int decompress(struct btrfs_root *root, char *inbuf, char *outbuf, u64 compress_len, u64 *decompress_len, int compress) { @@ -165,6 +213,9 @@ static int decompress(struct btrfs_root *root, char *inbuf, char *outbuf, case BTRFS_COMPRESS_LZO: return decompress_lzo(root, (unsigned char *)inbuf, outbuf, compress_len, decompress_len); + case BTRFS_COMPRESS_ZSTD: + return decompress_zstd(inbuf, outbuf, compress_len, + *decompress_len); default: break; } @@ -180,6 +231,7 @@ static int next_leaf(struct btrfs_root *root, struct btrfs_path *path) int offset = 1; struct extent_buffer *c; struct extent_buffer *next = NULL; + struct btrfs_fs_info *fs_info = root->fs_info; again: for (; level < BTRFS_MAX_LEVEL; level++) { @@ -209,7 +261,7 @@ again: if (path->reada) reada_for_search(root, path, level, slot, 0); - next = read_node_slot(root, c, slot); + next = read_node_slot(fs_info, c, slot); if (extent_buffer_uptodate(next)) break; offset++; @@ -225,7 +277,7 @@ again: break; if (path->reada) reada_for_search(root, path, level, 0, 0); - next = read_node_slot(root, next, 0); + next = read_node_slot(fs_info, next, 0); if (!extent_buffer_uptodate(next)) goto again; } @@ -330,7 +382,7 @@ static int copy_one_extent(struct btrfs_root *root, int fd, inbuf = malloc(size_left); if (!inbuf) { - error("not enough memory\n"); + error("not enough memory"); return -ENOMEM; } @@ -344,8 +396,8 @@ static int copy_one_extent(struct btrfs_root *root, int fd, } again: length = size_left; - ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, - bytenr, &length, &multi, mirror_num, NULL); + ret = btrfs_map_block(root->fs_info, READ, bytenr, &length, &multi, + mirror_num, NULL); if (ret) { error("cannot map block logical %llu length %llu: %d", (unsigned long long)bytenr, @@ -364,8 +416,7 @@ again: done = pread(dev_fd, inbuf+count, length, dev_bytenr); /* Need both checks, or we miss negative values due to u64 conversion */ if (done < 0 || done < length) { - num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, - bytenr, length); + num_copies = btrfs_num_copies(root->fs_info, bytenr, length); mirror_num++; /* mirror_num is 1-indexed, so num_copies is a valid mirror. */ if (mirror_num > num_copies) { @@ -391,7 +442,7 @@ again: pos+total); if (done < 0) { ret = -1; - error("cannot write data: %d %s", errno, strerror(errno)); + error("cannot write data: %d %m", errno); goto out; } total += done; @@ -402,8 +453,7 @@ again: ret = decompress(root, inbuf, outbuf, disk_size, &ram_size, compress); if (ret) { - num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, - bytenr, length); + num_copies = btrfs_num_copies(root->fs_info, bytenr, length); mirror_num++; if (mirror_num >= num_copies) { ret = -1; @@ -486,7 +536,7 @@ static int set_file_xattrs(struct btrfs_root *root, u64 inode, do { ret = next_leaf(root, &path); if (ret < 0) { - error("searching for extended attributes: %d\n", + error("searching for extended attributes: %d", ret); goto out; } else if (ret) { @@ -537,8 +587,8 @@ static int set_file_xattrs(struct btrfs_root *root, u64 inode, data_len = len; if (fsetxattr(fd, name, data, data_len, 0)) - error("setting extended attribute %s on file %s: %s", - name, file_name, strerror(errno)); + error("setting extended attribute %s on file %s: %m", + name, file_name); len = sizeof(*di) + name_len + data_len; cur += len; @@ -574,13 +624,13 @@ static int copy_metadata(struct btrfs_root *root, int fd, ret = fchown(fd, btrfs_inode_uid(path.nodes[0], inode_item), btrfs_inode_gid(path.nodes[0], inode_item)); if (ret) { - error("failed to change owner: %s", strerror(errno)); + error("failed to change owner: %m"); goto out; } ret = fchmod(fd, btrfs_inode_mode(path.nodes[0], inode_item)); if (ret) { - error("failed to change mode: %s", strerror(errno)); + error("failed to change mode: %m"); goto out; } @@ -594,7 +644,7 @@ static int copy_metadata(struct btrfs_root *root, int fd, ret = futimens(fd, times); if (ret) { - error("failed to set times: %s", strerror(errno)); + error("failed to set times: %m"); goto out; } } @@ -795,7 +845,7 @@ static int overwrite_ok(const char * path) static int copy_symlink(struct btrfs_root *root, struct btrfs_key *key, const char *file) { - struct btrfs_path *path; + struct btrfs_path path; struct extent_buffer *leaf; struct btrfs_file_extent_item *extent_item; struct btrfs_inode_item *inode_item; @@ -819,29 +869,25 @@ static int copy_symlink(struct btrfs_root *root, struct btrfs_key *key, } } + btrfs_init_path(&path); key->type = BTRFS_EXTENT_DATA_KEY; key->offset = 0; - - path = btrfs_alloc_path(); - if (!path) - return -ENOMEM; - - ret = btrfs_search_slot(NULL, root, key, path, 0, 0); + ret = btrfs_search_slot(NULL, root, key, &path, 0, 0); if (ret < 0) goto out; - leaf = path->nodes[0]; + leaf = path.nodes[0]; if (!leaf) { fprintf(stderr, "Error getting leaf for symlink '%s'\n", file); ret = -1; goto out; } - extent_item = btrfs_item_ptr(leaf, path->slots[0], + extent_item = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_file_extent_item); len = btrfs_file_extent_inline_item_len(leaf, - btrfs_item_nr(path->slots[0])); + btrfs_item_nr(path.slots[0])); if (len >= PATH_MAX) { fprintf(stderr, "Symlink '%s' target length %d is longer than PATH_MAX\n", fs_name, len); @@ -858,8 +904,8 @@ static int copy_symlink(struct btrfs_root *root, struct btrfs_key *key, if (!dry_run) { ret = symlink(symlink_target, path_name); if (ret < 0) { - fprintf(stderr, "Failed to restore symlink '%s': %s\n", - path_name, strerror(errno)); + fprintf(stderr, "Failed to restore symlink '%s': %m\n", + path_name); goto out; } } @@ -876,39 +922,38 @@ static int copy_symlink(struct btrfs_root *root, struct btrfs_key *key, key->type = BTRFS_INODE_ITEM_KEY; key->offset = 0; - btrfs_release_path(path); + btrfs_release_path(&path); - ret = btrfs_lookup_inode(NULL, root, path, key, 0); + ret = btrfs_lookup_inode(NULL, root, &path, key, 0); if (ret) { fprintf(stderr, "Failed to lookup inode for '%s'\n", file); goto out; } - inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], + inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0], struct btrfs_inode_item); - ret = fchownat(-1, file, btrfs_inode_uid(path->nodes[0], inode_item), - btrfs_inode_gid(path->nodes[0], inode_item), + ret = fchownat(-1, file, btrfs_inode_uid(path.nodes[0], inode_item), + btrfs_inode_gid(path.nodes[0], inode_item), AT_SYMLINK_NOFOLLOW); if (ret) { - fprintf(stderr, "Failed to change owner: %s\n", - strerror(errno)); + fprintf(stderr, "Failed to change owner: %m\n"); goto out; } bts = btrfs_inode_atime(inode_item); - times[0].tv_sec = btrfs_timespec_sec(path->nodes[0], bts); - times[0].tv_nsec = btrfs_timespec_nsec(path->nodes[0], bts); + times[0].tv_sec = btrfs_timespec_sec(path.nodes[0], bts); + times[0].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts); bts = btrfs_inode_mtime(inode_item); - times[1].tv_sec = btrfs_timespec_sec(path->nodes[0], bts); - times[1].tv_nsec = btrfs_timespec_nsec(path->nodes[0], bts); + times[1].tv_sec = btrfs_timespec_sec(path.nodes[0], bts); + times[1].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts); ret = utimensat(-1, file, times, AT_SYMLINK_NOFOLLOW); if (ret) - fprintf(stderr, "Failed to set times: %s\n", strerror(errno)); + fprintf(stderr, "Failed to set times: %m\n"); out: - btrfs_free_path(path); + btrfs_release_path(&path); return ret; } @@ -916,7 +961,7 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, const char *output_rootdir, const char *in_dir, const regex_t *mreg) { - struct btrfs_path *path; + struct btrfs_path path; struct extent_buffer *leaf; struct btrfs_dir_item *dir_item; struct btrfs_key found_key, location; @@ -928,16 +973,10 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, int loops = 0; u8 type; - path = btrfs_alloc_path(); - if (!path) { - fprintf(stderr, "Ran out of memory\n"); - return -ENOMEM; - } - + btrfs_init_path(&path); key->offset = 0; key->type = BTRFS_DIR_INDEX_KEY; - - ret = btrfs_search_slot(NULL, root, key, path, 0, 0); + ret = btrfs_search_slot(NULL, root, key, &path, 0, 0); if (ret < 0) { fprintf(stderr, "Error searching %d\n", ret); goto out; @@ -945,12 +984,12 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, ret = 0; - leaf = path->nodes[0]; + leaf = path.nodes[0]; while (!leaf) { if (verbose > 1) printf("No leaf after search, looking for the next " "leaf\n"); - ret = next_leaf(root, path); + ret = next_leaf(root, &path); if (ret < 0) { fprintf(stderr, "Error getting next leaf %d\n", ret); @@ -963,7 +1002,7 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, ret = 0; goto out; } - leaf = path->nodes[0]; + leaf = path.nodes[0]; } while (leaf) { @@ -974,9 +1013,9 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, break; } - if (path->slots[0] >= btrfs_header_nritems(leaf)) { + if (path.slots[0] >= btrfs_header_nritems(leaf)) { do { - ret = next_leaf(root, path); + ret = next_leaf(root, &path); if (ret < 0) { fprintf(stderr, "Error searching %d\n", ret); @@ -990,11 +1029,11 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, ret = 0; goto out; } - leaf = path->nodes[0]; + leaf = path.nodes[0]; } while (!leaf); continue; } - btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); + btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]); if (found_key.objectid != key->objectid) { if (verbose > 1) printf("Found objectid=%Lu, key=%Lu\n", @@ -1007,7 +1046,7 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, found_key.type, key->type); break; } - dir_item = btrfs_item_ptr(leaf, path->slots[0], + dir_item = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_dir_item); name_ptr = (unsigned long)(dir_item + 1); name_len = btrfs_dir_name_len(leaf, dir_item); @@ -1139,12 +1178,12 @@ static int search_dir(struct btrfs_root *root, struct btrfs_key *key, if (ret < 0) { if (ignore_errors) goto next; - btrfs_free_path(path); + btrfs_release_path(&path); return ret; } } next: - path->slots[0]++; + path.slots[0]++; } if (restore_metadata) { @@ -1172,7 +1211,7 @@ next: if (verbose) printf("Done searching %s\n", in_dir); out: - btrfs_free_path(path); + btrfs_release_path(&path); return ret; } @@ -1181,7 +1220,7 @@ static int do_list_roots(struct btrfs_root *root) struct btrfs_key key; struct btrfs_key found_key; struct btrfs_disk_key disk_key; - struct btrfs_path *path; + struct btrfs_path path; struct extent_buffer *leaf; struct btrfs_root_item ri; unsigned long offset; @@ -1189,38 +1228,33 @@ static int do_list_roots(struct btrfs_root *root) int ret; root = root->fs_info->tree_root; - path = btrfs_alloc_path(); - if (!path) { - fprintf(stderr, "Failed to alloc path\n"); - return -ENOMEM; - } + btrfs_init_path(&path); key.offset = 0; key.objectid = 0; key.type = BTRFS_ROOT_ITEM_KEY; - - ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); + ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); if (ret < 0) { fprintf(stderr, "Failed to do search %d\n", ret); - btrfs_free_path(path); + btrfs_release_path(&path); return -1; } - leaf = path->nodes[0]; + leaf = path.nodes[0]; while (1) { - slot = path->slots[0]; + slot = path.slots[0]; if (slot >= btrfs_header_nritems(leaf)) { - ret = btrfs_next_leaf(root, path); + ret = btrfs_next_leaf(root, &path); if (ret) break; - leaf = path->nodes[0]; - slot = path->slots[0]; + leaf = path.nodes[0]; + slot = path.slots[0]; } btrfs_item_key(leaf, &disk_key, slot); btrfs_disk_key_to_cpu(&found_key, &disk_key); if (found_key.type != BTRFS_ROOT_ITEM_KEY) { - path->slots[0]++; + path.slots[0]++; continue; } @@ -1230,9 +1264,9 @@ static int do_list_roots(struct btrfs_root *root) btrfs_print_key(&disk_key); printf(" %Lu level %d\n", btrfs_root_bytenr(&ri), btrfs_root_level(&ri)); - path->slots[0]++; + path.slots[0]++; } - btrfs_free_path(path); + btrfs_release_path(&path); return 0; } @@ -1247,8 +1281,15 @@ static struct btrfs_root *open_fs(const char *dev, u64 root_location, for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) { bytenr = btrfs_sb_offset(i); + + /* + * Restore won't allocate extent and doesn't care anything + * in extent tree. Skip block group item search will allow + * restore to be executed on heavily damaged fs. + */ fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0, - OPEN_CTREE_PARTIAL); + OPEN_CTREE_PARTIAL | + OPEN_CTREE_NO_BLOCK_GROUPS); if (fs_info) break; fprintf(stderr, "Could not open root, trying backup super\n"); @@ -1269,8 +1310,8 @@ static struct btrfs_root *open_fs(const char *dev, u64 root_location, if (!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->nodesize, generation); + root->node = read_tree_block(fs_info, root_location, + generation); if (!extent_buffer_uptodate(root->node)) { fprintf(stderr, "Error opening tree root\n"); close_ctree(root); @@ -1303,36 +1344,30 @@ static struct btrfs_root *open_fs(const char *dev, u64 root_location, static int find_first_dir(struct btrfs_root *root, u64 *objectid) { - struct btrfs_path *path; + struct btrfs_path path; struct btrfs_key found_key; struct btrfs_key key; int ret = -1; int i; + btrfs_init_path(&path); key.objectid = 0; key.type = BTRFS_DIR_INDEX_KEY; key.offset = 0; - - path = btrfs_alloc_path(); - if (!path) { - fprintf(stderr, "Ran out of memory\n"); - return ret; - } - - ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); + ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); if (ret < 0) { fprintf(stderr, "Error searching %d\n", ret); goto out; } - if (!path->nodes[0]) { + if (!path.nodes[0]) { fprintf(stderr, "No leaf!\n"); goto out; } again: - for (i = path->slots[0]; - i < btrfs_header_nritems(path->nodes[0]); i++) { - btrfs_item_key_to_cpu(path->nodes[0], &found_key, i); + for (i = path.slots[0]; + i < btrfs_header_nritems(path.nodes[0]); i++) { + btrfs_item_key_to_cpu(path.nodes[0], &found_key, i); if (found_key.type != key.type) continue; @@ -1343,7 +1378,7 @@ again: goto out; } do { - ret = next_leaf(root, path); + ret = next_leaf(root, &path); if (ret < 0) { fprintf(stderr, "Error getting next leaf %d\n", ret); @@ -1352,12 +1387,12 @@ again: fprintf(stderr, "No more leaves\n"); goto out; } - } while (!path->nodes[0]); - if (path->nodes[0]) + } while (!path.nodes[0]); + if (path.nodes[0]) goto again; printf("Couldn't find a dir index item\n"); out: - btrfs_free_path(path); + btrfs_release_path(&path); return ret; } @@ -1522,7 +1557,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->nodesize, 0); + root->node = read_tree_block(root->fs_info, fs_location, 0); if (!extent_buffer_uptodate(root->node)) { fprintf(stderr, "Failed to read fs location\n"); ret = 1;