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,
memset(&strm, 0, sizeof(strm));
ret = inflateInit(&strm);
if (ret != Z_OK) {
- fprintf(stderr, "inflate init returnd %d\n", ret);
+ error("zlib init returned %d", ret);
return -1;
}
ret = inflate(&strm, Z_NO_FLUSH);
if (ret != Z_STREAM_END) {
(void)inflateEnd(&strm);
- fprintf(stderr, "failed to inflate: %d\n", ret);
+ error("zlib inflate failed: %d", ret);
return -1;
}
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;
ret = lzo_init();
if (ret != LZO_E_OK) {
- fprintf(stderr, "lzo init returned %d\n", ret);
+ error("lzo init returned %d", ret);
return -1;
}
in_len = read_compress_length(inbuf);
if ((tot_in + LZO_LEN + in_len) > tot_len) {
- fprintf(stderr, "bad compress length %lu\n",
+ error("bad compress length %lu",
(unsigned long)in_len);
return -1;
}
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);
if (ret != LZO_E_OK) {
- fprintf(stderr, "failed to inflate: %d\n", ret);
+ error("lzo decompress failed: %d", ret);
return -1;
}
out_len += new_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;
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;
}
- fprintf(stderr, "invalid compression type: %d\n", compress);
+ error("invalid compression type: %d", compress);
return -1;
}
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;
ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
outbuf = calloc(1, ram_size);
if (!outbuf) {
- fprintf(stderr, "No memory\n");
+ error("not enough memory");
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;
inbuf = malloc(size_left);
if (!inbuf) {
- fprintf(stderr, "No memory\n");
+ error("not enough memory\n");
return -ENOMEM;
}
if (compress != BTRFS_COMPRESS_NONE) {
outbuf = calloc(1, ram_size);
if (!outbuf) {
- fprintf(stderr, "No memory\n");
+ error("not enough memory");
free(inbuf);
return -ENOMEM;
}
ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
bytenr, &length, &multi, mirror_num, NULL);
if (ret) {
- fprintf(stderr, "Error mapping block %d\n", ret);
+ error("cannot map block logical %llu length %llu: %d",
+ (unsigned long long)bytenr,
+ (unsigned long long)length, ret);
goto out;
}
device = multi->stripes[0].dev;
/* mirror_num is 1-indexed, so num_copies is a valid mirror. */
if (mirror_num > num_copies) {
ret = -1;
- fprintf(stderr, "Exhausted mirrors trying to read\n");
+ error("exhausted mirrors trying to read (%d > %d)",
+ mirror_num, num_copies);
goto out;
}
fprintf(stderr, "Trying another mirror\n");
pos+total);
if (done < 0) {
ret = -1;
- fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
+ error("cannot write data: %d %s", errno, strerror(errno));
goto out;
}
total += done;
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);
do {
ret = next_leaf(root, path);
if (ret < 0) {
- fprintf(stderr,
- "Error searching for extended attributes: %d\n",
+ error("searching for extended attributes: %d\n",
ret);
goto out;
} else if (ret) {
len);
data_len = len;
- if (fsetxattr(fd, name, data, data_len, 0)) {
- int err = errno;
-
- fprintf(stderr,
- "Error setting extended attribute %s on file %s: %s\n",
- name, file_name, strerror(err));
- }
+ if (fsetxattr(fd, name, data, data_len, 0))
+ error("setting extended attribute %s on file %s: %s",
+ name, file_name, strerror(errno));
len = sizeof(*di) + name_len + data_len;
cur += len;
path = btrfs_alloc_path();
if (!path) {
- fprintf(stderr, "ERROR: Ran out of memory\n");
+ error("not enough memory");
return -ENOMEM;
}
ret = fchown(fd, btrfs_inode_uid(path->nodes[0], inode_item),
btrfs_inode_gid(path->nodes[0], inode_item));
if (ret) {
- fprintf(stderr, "ERROR: Failed to change owner: %s\n",
- strerror(errno));
+ error("failed to change owner: %s", strerror(errno));
goto out;
}
ret = fchmod(fd, btrfs_inode_mode(path->nodes[0], inode_item));
if (ret) {
- fprintf(stderr, "ERROR: Failed to change mode: %s\n",
- strerror(errno));
+ error("failed to change mode: %s", strerror(errno));
goto out;
}
ret = futimens(fd, times);
if (ret) {
- fprintf(stderr, "ERROR: Failed to set times: %s\n",
- strerror(errno));
+ error("failed to set times: %s", strerror(errno));
goto out;
}
}
path = btrfs_alloc_path();
if (!path) {
- fprintf(stderr, "Ran out of memory\n");
+ error("not enough memory");
return -ENOMEM;
}
ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
if (ret < 0) {
- fprintf(stderr, "Error searching %d\n", ret);
+ error("searching extent data returned %d", ret);
goto out;
}
while (!leaf) {
ret = next_leaf(root, path);
if (ret < 0) {
- fprintf(stderr, "Error getting next leaf %d\n",
- ret);
+ error("cannot get next leaf: %d", ret);
goto out;
} else if (ret > 0) {
/* No more leaves to search */
extent_type = btrfs_file_extent_type(leaf, fi);
compression = btrfs_file_extent_compression(leaf, fi);
if (compression >= BTRFS_COMPRESS_LAST) {
- fprintf(stderr, "Don't support compression yet %d\n",
+ warning("compression type %d not supported",
compression);
ret = -1;
goto out;
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) {
if (ret)
goto out;
} else {
- printf("Weird extent type %d\n", extent_type);
+ warning("weird extent type %d", extent_type);
}
next:
path->slots[0]++;
len = btrfs_file_extent_inline_item_len(leaf,
btrfs_item_nr(path->slots[0]));
- if (len > PATH_MAX) {
+ if (len >= PATH_MAX) {
fprintf(stderr, "Symlink '%s' target length %d is longer than PATH_MAX\n",
fs_name, len);
ret = -1;
for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
bytenr = btrfs_sb_offset(i);
- fs_info = open_ctree_fs_info(dev, bytenr, root_location,
+ fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0,
OPEN_CTREE_PARTIAL);
if (fs_info)
break;
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);
"btrfs restore [options] <device> <path> | -l <device>",
"Try to restore files from a damaged filesystem (unmounted)",
"",
- "-s get snapshots",
- "-x get extended attributes",
+ "-s|--snapshots get snapshots",
+ "-x|--xattr get extended attributes",
"-m|--metadata restore owner, mode and times",
- "-S|--symlinks restore symbolic links"
- "-v verbose",
- "-i ignore errors",
- "-o overwrite",
+ "-S|--symlinks restore symbolic links",
+ "-v|--verbose verbose",
+ "-i|--ignore-errors ignore errors",
+ "-o|--overwrite overwrite",
"-t <bytenr> tree location",
"-f <bytenr> filesystem location",
- "-u <mirror> super mirror",
- "-r <rootid> root objectid",
+ "-u|--super <mirror> super mirror",
+ "-r|--root <rootid> root objectid",
"-d find dir",
- "-l list tree roots",
+ "-l|--list-roots list tree roots",
"-D|--dry-run dry run (only list files that would be recovered)",
"--path-regex <regex>",
" restore only filenames matching regex,",
{ "dry-run", no_argument, NULL, 'D'},
{ "metadata", no_argument, NULL, 'm'},
{ "symlinks", no_argument, NULL, 'S'},
+ { "snapshots", no_argument, NULL, 's'},
+ { "xattr", no_argument, NULL, 'x'},
+ { "verbose", no_argument, NULL, 'v'},
+ { "ignore-errors", no_argument, NULL, 'i'},
+ { "overwrite", no_argument, NULL, 'o'},
+ { "super", required_argument, NULL, 'u'},
+ { "root", required_argument, NULL, 'r'},
+ { "list-roots", no_argument, NULL, 'l'},
{ NULL, 0, NULL, 0}
};
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;