btrfs-progs: readme: fix link to issue tracker on github.
[btrfs-progs-unstable/devel.git] / cmds-restore.c
blobc327cef0187ae3a032f8c59871e7f9a654706575
1 /*
2 * Copyright (C) 2011 Red Hat. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include "kerncompat.h"
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <lzo/lzoconf.h>
30 #include <lzo/lzo1x.h>
31 #include <zlib.h>
32 #include <regex.h>
33 #include <getopt.h>
34 #include <sys/types.h>
35 #include <sys/xattr.h>
37 #include "ctree.h"
38 #include "disk-io.h"
39 #include "print-tree.h"
40 #include "transaction.h"
41 #include "list.h"
42 #include "volumes.h"
43 #include "utils.h"
44 #include "commands.h"
45 #include "help.h"
47 static char fs_name[PATH_MAX];
48 static char path_name[PATH_MAX];
49 static char symlink_target[PATH_MAX];
50 static int get_snaps = 0;
51 static int verbose = 0;
52 static int restore_metadata = 0;
53 static int restore_symlinks = 0;
54 static int ignore_errors = 0;
55 static int overwrite = 0;
56 static int get_xattrs = 0;
57 static int dry_run = 0;
59 #define LZO_LEN 4
60 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
62 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
63 u64 decompress_len)
65 z_stream strm;
66 int ret;
68 memset(&strm, 0, sizeof(strm));
69 ret = inflateInit(&strm);
70 if (ret != Z_OK) {
71 error("zlib init returned %d", ret);
72 return -1;
75 strm.avail_in = compress_len;
76 strm.next_in = (unsigned char *)inbuf;
77 strm.avail_out = decompress_len;
78 strm.next_out = (unsigned char *)outbuf;
79 ret = inflate(&strm, Z_NO_FLUSH);
80 if (ret != Z_STREAM_END) {
81 (void)inflateEnd(&strm);
82 error("zlib inflate failed: %d", ret);
83 return -1;
86 (void)inflateEnd(&strm);
87 return 0;
89 static inline size_t read_compress_length(unsigned char *buf)
91 __le32 dlen;
92 memcpy(&dlen, buf, LZO_LEN);
93 return le32_to_cpu(dlen);
96 static int decompress_lzo(struct btrfs_root *root, unsigned char *inbuf,
97 char *outbuf, u64 compress_len, u64 *decompress_len)
99 size_t new_len;
100 size_t in_len;
101 size_t out_len = 0;
102 size_t tot_len;
103 size_t tot_in;
104 int ret;
106 ret = lzo_init();
107 if (ret != LZO_E_OK) {
108 error("lzo init returned %d", ret);
109 return -1;
112 tot_len = read_compress_length(inbuf);
113 inbuf += LZO_LEN;
114 tot_in = LZO_LEN;
116 while (tot_in < tot_len) {
117 size_t mod_page;
118 size_t rem_page;
119 in_len = read_compress_length(inbuf);
121 if ((tot_in + LZO_LEN + in_len) > tot_len) {
122 error("bad compress length %lu",
123 (unsigned long)in_len);
124 return -1;
127 inbuf += LZO_LEN;
128 tot_in += LZO_LEN;
129 new_len = lzo1x_worst_compress(root->sectorsize);
130 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
131 (unsigned char *)outbuf,
132 (void *)&new_len, NULL);
133 if (ret != LZO_E_OK) {
134 error("lzo decompress failed: %d", ret);
135 return -1;
137 out_len += new_len;
138 outbuf += new_len;
139 inbuf += in_len;
140 tot_in += in_len;
143 * If the 4 byte header does not fit to the rest of the page we
144 * have to move to the next one, unless we read some garbage
146 mod_page = tot_in % root->sectorsize;
147 rem_page = root->sectorsize - mod_page;
148 if (rem_page < LZO_LEN) {
149 inbuf += rem_page;
150 tot_in += rem_page;
154 *decompress_len = out_len;
156 return 0;
159 static int decompress(struct btrfs_root *root, char *inbuf, char *outbuf,
160 u64 compress_len, u64 *decompress_len, int compress)
162 switch (compress) {
163 case BTRFS_COMPRESS_ZLIB:
164 return decompress_zlib(inbuf, outbuf, compress_len,
165 *decompress_len);
166 case BTRFS_COMPRESS_LZO:
167 return decompress_lzo(root, (unsigned char *)inbuf, outbuf,
168 compress_len, decompress_len);
169 default:
170 break;
173 error("invalid compression type: %d", compress);
174 return -1;
177 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
179 int slot;
180 int level = 1;
181 int offset = 1;
182 struct extent_buffer *c;
183 struct extent_buffer *next = NULL;
185 again:
186 for (; level < BTRFS_MAX_LEVEL; level++) {
187 if (path->nodes[level])
188 break;
191 if (level >= BTRFS_MAX_LEVEL)
192 return 1;
194 slot = path->slots[level] + 1;
196 while(level < BTRFS_MAX_LEVEL) {
197 if (!path->nodes[level])
198 return 1;
200 slot = path->slots[level] + offset;
201 c = path->nodes[level];
202 if (slot >= btrfs_header_nritems(c)) {
203 level++;
204 if (level == BTRFS_MAX_LEVEL)
205 return 1;
206 offset = 1;
207 continue;
210 if (path->reada)
211 reada_for_search(root, path, level, slot, 0);
213 next = read_node_slot(root, c, slot);
214 if (extent_buffer_uptodate(next))
215 break;
216 offset++;
218 path->slots[level] = slot;
219 while(1) {
220 level--;
221 c = path->nodes[level];
222 free_extent_buffer(c);
223 path->nodes[level] = next;
224 path->slots[level] = 0;
225 if (!level)
226 break;
227 if (path->reada)
228 reada_for_search(root, path, level, 0, 0);
229 next = read_node_slot(root, next, 0);
230 if (!extent_buffer_uptodate(next))
231 goto again;
233 return 0;
236 static int copy_one_inline(struct btrfs_root *root, int fd,
237 struct btrfs_path *path, u64 pos)
239 struct extent_buffer *leaf = path->nodes[0];
240 struct btrfs_file_extent_item *fi;
241 char buf[4096];
242 char *outbuf;
243 u64 ram_size;
244 ssize_t done;
245 unsigned long ptr;
246 int ret;
247 int len;
248 int inline_item_len;
249 int compress;
251 fi = btrfs_item_ptr(leaf, path->slots[0],
252 struct btrfs_file_extent_item);
253 ptr = btrfs_file_extent_inline_start(fi);
254 len = btrfs_file_extent_inline_len(leaf, path->slots[0], fi);
255 inline_item_len = btrfs_file_extent_inline_item_len(leaf, btrfs_item_nr(path->slots[0]));
256 read_extent_buffer(leaf, buf, ptr, inline_item_len);
258 compress = btrfs_file_extent_compression(leaf, fi);
259 if (compress == BTRFS_COMPRESS_NONE) {
260 done = pwrite(fd, buf, len, pos);
261 if (done < len) {
262 fprintf(stderr, "Short inline write, wanted %d, did "
263 "%zd: %d\n", len, done, errno);
264 return -1;
266 return 0;
269 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
270 outbuf = calloc(1, ram_size);
271 if (!outbuf) {
272 error("not enough memory");
273 return -ENOMEM;
276 ret = decompress(root, buf, outbuf, len, &ram_size, compress);
277 if (ret) {
278 free(outbuf);
279 return ret;
282 done = pwrite(fd, outbuf, ram_size, pos);
283 free(outbuf);
284 if (done < ram_size) {
285 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
286 "did %zd: %d\n", ram_size, done, errno);
287 return -1;
290 return 0;
293 static int copy_one_extent(struct btrfs_root *root, int fd,
294 struct extent_buffer *leaf,
295 struct btrfs_file_extent_item *fi, u64 pos)
297 struct btrfs_multi_bio *multi = NULL;
298 struct btrfs_device *device;
299 char *inbuf, *outbuf = NULL;
300 ssize_t done, total = 0;
301 u64 bytenr;
302 u64 ram_size;
303 u64 disk_size;
304 u64 num_bytes;
305 u64 length;
306 u64 size_left;
307 u64 dev_bytenr;
308 u64 offset;
309 u64 count = 0;
310 int compress;
311 int ret;
312 int dev_fd;
313 int mirror_num = 1;
314 int num_copies;
316 compress = btrfs_file_extent_compression(leaf, fi);
317 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
318 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
319 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
320 offset = btrfs_file_extent_offset(leaf, fi);
321 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
322 size_left = disk_size;
323 if (compress == BTRFS_COMPRESS_NONE)
324 bytenr += offset;
326 if (verbose && offset)
327 printf("offset is %Lu\n", offset);
328 /* we found a hole */
329 if (disk_size == 0)
330 return 0;
332 inbuf = malloc(size_left);
333 if (!inbuf) {
334 error("not enough memory");
335 return -ENOMEM;
338 if (compress != BTRFS_COMPRESS_NONE) {
339 outbuf = calloc(1, ram_size);
340 if (!outbuf) {
341 error("not enough memory");
342 free(inbuf);
343 return -ENOMEM;
346 again:
347 length = size_left;
348 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
349 bytenr, &length, &multi, mirror_num, NULL);
350 if (ret) {
351 error("cannot map block logical %llu length %llu: %d",
352 (unsigned long long)bytenr,
353 (unsigned long long)length, ret);
354 goto out;
356 device = multi->stripes[0].dev;
357 dev_fd = device->fd;
358 device->total_ios++;
359 dev_bytenr = multi->stripes[0].physical;
360 free(multi);
362 if (size_left < length)
363 length = size_left;
365 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
366 /* Need both checks, or we miss negative values due to u64 conversion */
367 if (done < 0 || done < length) {
368 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
369 bytenr, length);
370 mirror_num++;
371 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
372 if (mirror_num > num_copies) {
373 ret = -1;
374 error("exhausted mirrors trying to read (%d > %d)",
375 mirror_num, num_copies);
376 goto out;
378 fprintf(stderr, "Trying another mirror\n");
379 goto again;
382 mirror_num = 1;
383 size_left -= length;
384 count += length;
385 bytenr += length;
386 if (size_left)
387 goto again;
389 if (compress == BTRFS_COMPRESS_NONE) {
390 while (total < num_bytes) {
391 done = pwrite(fd, inbuf+total, num_bytes-total,
392 pos+total);
393 if (done < 0) {
394 ret = -1;
395 error("cannot write data: %d %s", errno, strerror(errno));
396 goto out;
398 total += done;
400 ret = 0;
401 goto out;
404 ret = decompress(root, inbuf, outbuf, disk_size, &ram_size, compress);
405 if (ret) {
406 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
407 bytenr, length);
408 mirror_num++;
409 if (mirror_num >= num_copies) {
410 ret = -1;
411 goto out;
413 fprintf(stderr, "Trying another mirror\n");
414 goto again;
417 while (total < num_bytes) {
418 done = pwrite(fd, outbuf + offset + total,
419 num_bytes - total,
420 pos + total);
421 if (done < 0) {
422 ret = -1;
423 goto out;
425 total += done;
427 out:
428 free(inbuf);
429 free(outbuf);
430 return ret;
433 enum loop_response {
434 LOOP_STOP,
435 LOOP_CONTINUE,
436 LOOP_DONTASK
439 static enum loop_response ask_to_continue(const char *file)
441 char buf[2];
442 char *ret;
444 printf("We seem to be looping a lot on %s, do you want to keep going "
445 "on ? (y/N/a): ", file);
446 again:
447 ret = fgets(buf, 2, stdin);
448 if (*ret == '\n' || tolower(*ret) == 'n')
449 return LOOP_STOP;
450 if (tolower(*ret) == 'a')
451 return LOOP_DONTASK;
452 if (tolower(*ret) != 'y') {
453 printf("Please enter one of 'y', 'n', or 'a': ");
454 goto again;
457 return LOOP_CONTINUE;
461 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
462 int fd, const char *file_name)
464 struct btrfs_key key;
465 struct btrfs_path path;
466 struct extent_buffer *leaf;
467 struct btrfs_dir_item *di;
468 u32 name_len = 0;
469 u32 data_len = 0;
470 u32 len = 0;
471 u32 cur, total_len;
472 char *name = NULL;
473 char *data = NULL;
474 int ret = 0;
476 btrfs_init_path(&path);
477 key.objectid = inode;
478 key.type = BTRFS_XATTR_ITEM_KEY;
479 key.offset = 0;
480 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
481 if (ret < 0)
482 goto out;
484 leaf = path.nodes[0];
485 while (1) {
486 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
487 do {
488 ret = next_leaf(root, &path);
489 if (ret < 0) {
490 error("searching for extended attributes: %d",
491 ret);
492 goto out;
493 } else if (ret) {
494 /* No more leaves to search */
495 ret = 0;
496 goto out;
498 leaf = path.nodes[0];
499 } while (!leaf);
500 continue;
503 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
504 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
505 break;
506 cur = 0;
507 total_len = btrfs_item_size_nr(leaf, path.slots[0]);
508 di = btrfs_item_ptr(leaf, path.slots[0],
509 struct btrfs_dir_item);
511 while (cur < total_len) {
512 len = btrfs_dir_name_len(leaf, di);
513 if (len > name_len) {
514 free(name);
515 name = (char *) malloc(len + 1);
516 if (!name) {
517 ret = -ENOMEM;
518 goto out;
521 read_extent_buffer(leaf, name,
522 (unsigned long)(di + 1), len);
523 name[len] = '\0';
524 name_len = len;
526 len = btrfs_dir_data_len(leaf, di);
527 if (len > data_len) {
528 free(data);
529 data = (char *) malloc(len);
530 if (!data) {
531 ret = -ENOMEM;
532 goto out;
535 read_extent_buffer(leaf, data,
536 (unsigned long)(di + 1) + name_len,
537 len);
538 data_len = len;
540 if (fsetxattr(fd, name, data, data_len, 0))
541 error("setting extended attribute %s on file %s: %s",
542 name, file_name, strerror(errno));
544 len = sizeof(*di) + name_len + data_len;
545 cur += len;
546 di = (struct btrfs_dir_item *)((char *)di + len);
548 path.slots[0]++;
550 ret = 0;
551 out:
552 btrfs_release_path(&path);
553 free(name);
554 free(data);
556 return ret;
559 static int copy_metadata(struct btrfs_root *root, int fd,
560 struct btrfs_key *key)
562 struct btrfs_path path;
563 struct btrfs_inode_item *inode_item;
564 int ret;
566 btrfs_init_path(&path);
567 ret = btrfs_lookup_inode(NULL, root, &path, key, 0);
568 if (ret == 0) {
569 struct btrfs_timespec *bts;
570 struct timespec times[2];
572 inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
573 struct btrfs_inode_item);
575 ret = fchown(fd, btrfs_inode_uid(path.nodes[0], inode_item),
576 btrfs_inode_gid(path.nodes[0], inode_item));
577 if (ret) {
578 error("failed to change owner: %s", strerror(errno));
579 goto out;
582 ret = fchmod(fd, btrfs_inode_mode(path.nodes[0], inode_item));
583 if (ret) {
584 error("failed to change mode: %s", strerror(errno));
585 goto out;
588 bts = btrfs_inode_atime(inode_item);
589 times[0].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
590 times[0].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
592 bts = btrfs_inode_mtime(inode_item);
593 times[1].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
594 times[1].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
596 ret = futimens(fd, times);
597 if (ret) {
598 error("failed to set times: %s", strerror(errno));
599 goto out;
602 out:
603 btrfs_release_path(&path);
604 return ret;
607 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
608 const char *file)
610 struct extent_buffer *leaf;
611 struct btrfs_path path;
612 struct btrfs_file_extent_item *fi;
613 struct btrfs_inode_item *inode_item;
614 struct btrfs_timespec *bts;
615 struct btrfs_key found_key;
616 int ret;
617 int extent_type;
618 int compression;
619 int loops = 0;
620 u64 found_size = 0;
621 struct timespec times[2];
622 int times_ok = 0;
624 btrfs_init_path(&path);
625 ret = btrfs_lookup_inode(NULL, root, &path, key, 0);
626 if (ret == 0) {
627 inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
628 struct btrfs_inode_item);
629 found_size = btrfs_inode_size(path.nodes[0], inode_item);
631 if (restore_metadata) {
633 * Change the ownership and mode now, set times when
634 * copyout is finished.
637 ret = fchown(fd, btrfs_inode_uid(path.nodes[0], inode_item),
638 btrfs_inode_gid(path.nodes[0], inode_item));
639 if (ret && !ignore_errors)
640 goto out;
642 ret = fchmod(fd, btrfs_inode_mode(path.nodes[0], inode_item));
643 if (ret && !ignore_errors)
644 goto out;
646 bts = btrfs_inode_atime(inode_item);
647 times[0].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
648 times[0].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
650 bts = btrfs_inode_mtime(inode_item);
651 times[1].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
652 times[1].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
653 times_ok = 1;
656 btrfs_release_path(&path);
658 key->offset = 0;
659 key->type = BTRFS_EXTENT_DATA_KEY;
661 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
662 if (ret < 0) {
663 error("searching extent data returned %d", ret);
664 goto out;
667 leaf = path.nodes[0];
668 while (!leaf) {
669 ret = next_leaf(root, &path);
670 if (ret < 0) {
671 error("cannot get next leaf: %d", ret);
672 goto out;
673 } else if (ret > 0) {
674 /* No more leaves to search */
675 ret = 0;
676 goto out;
678 leaf = path.nodes[0];
681 while (1) {
682 if (loops >= 0 && loops++ >= 1024) {
683 enum loop_response resp;
685 resp = ask_to_continue(file);
686 if (resp == LOOP_STOP)
687 break;
688 else if (resp == LOOP_CONTINUE)
689 loops = 0;
690 else if (resp == LOOP_DONTASK)
691 loops = -1;
693 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
694 do {
695 ret = next_leaf(root, &path);
696 if (ret < 0) {
697 fprintf(stderr, "Error searching %d\n", ret);
698 goto out;
699 } else if (ret) {
700 /* No more leaves to search */
701 btrfs_release_path(&path);
702 goto set_size;
704 leaf = path.nodes[0];
705 } while (!leaf);
706 continue;
708 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
709 if (found_key.objectid != key->objectid)
710 break;
711 if (found_key.type != key->type)
712 break;
713 fi = btrfs_item_ptr(leaf, path.slots[0],
714 struct btrfs_file_extent_item);
715 extent_type = btrfs_file_extent_type(leaf, fi);
716 compression = btrfs_file_extent_compression(leaf, fi);
717 if (compression >= BTRFS_COMPRESS_LAST) {
718 warning("compression type %d not supported",
719 compression);
720 ret = -1;
721 goto out;
724 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
725 goto next;
726 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
727 ret = copy_one_inline(root, fd, &path, found_key.offset);
728 if (ret)
729 goto out;
730 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
731 ret = copy_one_extent(root, fd, leaf, fi,
732 found_key.offset);
733 if (ret)
734 goto out;
735 } else {
736 warning("weird extent type %d", extent_type);
738 next:
739 path.slots[0]++;
742 btrfs_release_path(&path);
743 set_size:
744 if (found_size) {
745 ret = ftruncate(fd, (loff_t)found_size);
746 if (ret)
747 return ret;
749 if (get_xattrs) {
750 ret = set_file_xattrs(root, key->objectid, fd, file);
751 if (ret)
752 return ret;
754 if (restore_metadata && times_ok) {
755 ret = futimens(fd, times);
756 if (ret)
757 return ret;
759 return 0;
761 out:
762 btrfs_release_path(&path);
763 return ret;
767 * returns:
768 * 0 if the file exists and should be skipped.
769 * 1 if the file does NOT exist
770 * 2 if the file exists but is OK to overwrite
772 static int overwrite_ok(const char * path)
774 static int warn = 0;
775 struct stat st;
776 int ret;
778 /* don't be fooled by symlinks */
779 ret = fstatat(-1, path_name, &st, AT_SYMLINK_NOFOLLOW);
781 if (!ret) {
782 if (overwrite)
783 return 2;
785 if (verbose || !warn)
786 printf("Skipping existing file"
787 " %s\n", path);
788 if (!warn)
789 printf("If you wish to overwrite use -o\n");
790 warn = 1;
791 return 0;
793 return 1;
796 static int copy_symlink(struct btrfs_root *root, struct btrfs_key *key,
797 const char *file)
799 struct btrfs_path path;
800 struct extent_buffer *leaf;
801 struct btrfs_file_extent_item *extent_item;
802 struct btrfs_inode_item *inode_item;
803 u32 len;
804 u32 name_offset;
805 int ret;
806 struct btrfs_timespec *bts;
807 struct timespec times[2];
809 ret = overwrite_ok(path_name);
810 if (ret == 0)
811 return 0; /* skip this file */
813 /* symlink() can't overwrite, so unlink first */
814 if (ret == 2) {
815 ret = unlink(path_name);
816 if (ret) {
817 fprintf(stderr, "failed to unlink '%s' for overwrite\n",
818 path_name);
819 return ret;
823 btrfs_init_path(&path);
824 key->type = BTRFS_EXTENT_DATA_KEY;
825 key->offset = 0;
826 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
827 if (ret < 0)
828 goto out;
830 leaf = path.nodes[0];
831 if (!leaf) {
832 fprintf(stderr, "Error getting leaf for symlink '%s'\n", file);
833 ret = -1;
834 goto out;
837 extent_item = btrfs_item_ptr(leaf, path.slots[0],
838 struct btrfs_file_extent_item);
840 len = btrfs_file_extent_inline_item_len(leaf,
841 btrfs_item_nr(path.slots[0]));
842 if (len >= PATH_MAX) {
843 fprintf(stderr, "Symlink '%s' target length %d is longer than PATH_MAX\n",
844 fs_name, len);
845 ret = -1;
846 goto out;
849 name_offset = (unsigned long) extent_item
850 + offsetof(struct btrfs_file_extent_item, disk_bytenr);
851 read_extent_buffer(leaf, symlink_target, name_offset, len);
853 symlink_target[len] = 0;
855 if (!dry_run) {
856 ret = symlink(symlink_target, path_name);
857 if (ret < 0) {
858 fprintf(stderr, "Failed to restore symlink '%s': %s\n",
859 path_name, strerror(errno));
860 goto out;
863 printf("SYMLINK: '%s' => '%s'\n", path_name, symlink_target);
865 ret = 0;
866 if (!restore_metadata)
867 goto out;
870 * Symlink metadata operates differently than files/directories, so do
871 * our own work here.
873 key->type = BTRFS_INODE_ITEM_KEY;
874 key->offset = 0;
876 btrfs_release_path(&path);
878 ret = btrfs_lookup_inode(NULL, root, &path, key, 0);
879 if (ret) {
880 fprintf(stderr, "Failed to lookup inode for '%s'\n", file);
881 goto out;
884 inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
885 struct btrfs_inode_item);
887 ret = fchownat(-1, file, btrfs_inode_uid(path.nodes[0], inode_item),
888 btrfs_inode_gid(path.nodes[0], inode_item),
889 AT_SYMLINK_NOFOLLOW);
890 if (ret) {
891 fprintf(stderr, "Failed to change owner: %s\n",
892 strerror(errno));
893 goto out;
896 bts = btrfs_inode_atime(inode_item);
897 times[0].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
898 times[0].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
900 bts = btrfs_inode_mtime(inode_item);
901 times[1].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
902 times[1].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
904 ret = utimensat(-1, file, times, AT_SYMLINK_NOFOLLOW);
905 if (ret)
906 fprintf(stderr, "Failed to set times: %s\n", strerror(errno));
907 out:
908 btrfs_release_path(&path);
909 return ret;
912 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
913 const char *output_rootdir, const char *in_dir,
914 const regex_t *mreg)
916 struct btrfs_path path;
917 struct extent_buffer *leaf;
918 struct btrfs_dir_item *dir_item;
919 struct btrfs_key found_key, location;
920 char filename[BTRFS_NAME_LEN + 1];
921 unsigned long name_ptr;
922 int name_len;
923 int ret = 0;
924 int fd;
925 int loops = 0;
926 u8 type;
928 btrfs_init_path(&path);
929 key->offset = 0;
930 key->type = BTRFS_DIR_INDEX_KEY;
931 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
932 if (ret < 0) {
933 fprintf(stderr, "Error searching %d\n", ret);
934 goto out;
937 ret = 0;
939 leaf = path.nodes[0];
940 while (!leaf) {
941 if (verbose > 1)
942 printf("No leaf after search, looking for the next "
943 "leaf\n");
944 ret = next_leaf(root, &path);
945 if (ret < 0) {
946 fprintf(stderr, "Error getting next leaf %d\n",
947 ret);
948 goto out;
949 } else if (ret > 0) {
950 /* No more leaves to search */
951 if (verbose)
952 printf("Reached the end of the tree looking "
953 "for the directory\n");
954 ret = 0;
955 goto out;
957 leaf = path.nodes[0];
960 while (leaf) {
961 if (loops++ >= 1024) {
962 printf("We have looped trying to restore files in %s "
963 "too many times to be making progress, "
964 "stopping\n", in_dir);
965 break;
968 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
969 do {
970 ret = next_leaf(root, &path);
971 if (ret < 0) {
972 fprintf(stderr, "Error searching %d\n",
973 ret);
974 goto out;
975 } else if (ret > 0) {
976 /* No more leaves to search */
977 if (verbose)
978 printf("Reached the end of "
979 "the tree searching the"
980 " directory\n");
981 ret = 0;
982 goto out;
984 leaf = path.nodes[0];
985 } while (!leaf);
986 continue;
988 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
989 if (found_key.objectid != key->objectid) {
990 if (verbose > 1)
991 printf("Found objectid=%Lu, key=%Lu\n",
992 found_key.objectid, key->objectid);
993 break;
995 if (found_key.type != key->type) {
996 if (verbose > 1)
997 printf("Found type=%u, want=%u\n",
998 found_key.type, key->type);
999 break;
1001 dir_item = btrfs_item_ptr(leaf, path.slots[0],
1002 struct btrfs_dir_item);
1003 name_ptr = (unsigned long)(dir_item + 1);
1004 name_len = btrfs_dir_name_len(leaf, dir_item);
1005 read_extent_buffer(leaf, filename, name_ptr, name_len);
1006 filename[name_len] = '\0';
1007 type = btrfs_dir_type(leaf, dir_item);
1008 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
1010 /* full path from root of btrfs being restored */
1011 snprintf(fs_name, PATH_MAX, "%s/%s", in_dir, filename);
1013 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
1014 goto next;
1016 /* full path from system root */
1017 snprintf(path_name, PATH_MAX, "%s%s", output_rootdir, fs_name);
1020 * Restore directories, files, symlinks and metadata.
1022 if (type == BTRFS_FT_REG_FILE) {
1023 if (!overwrite_ok(path_name))
1024 goto next;
1026 if (verbose)
1027 printf("Restoring %s\n", path_name);
1028 if (dry_run)
1029 goto next;
1030 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
1031 if (fd < 0) {
1032 fprintf(stderr, "Error creating %s: %d\n",
1033 path_name, errno);
1034 if (ignore_errors)
1035 goto next;
1036 ret = -1;
1037 goto out;
1039 loops = 0;
1040 ret = copy_file(root, fd, &location, path_name);
1041 close(fd);
1042 if (ret) {
1043 fprintf(stderr, "Error copying data for %s\n",
1044 path_name);
1045 if (ignore_errors)
1046 goto next;
1047 goto out;
1049 } else if (type == BTRFS_FT_DIR) {
1050 struct btrfs_root *search_root = root;
1051 char *dir = strdup(fs_name);
1053 if (!dir) {
1054 fprintf(stderr, "Ran out of memory\n");
1055 ret = -ENOMEM;
1056 goto out;
1059 if (location.type == BTRFS_ROOT_ITEM_KEY) {
1061 * If we are a snapshot and this is the index
1062 * object to ourselves just skip it.
1064 if (location.objectid ==
1065 root->root_key.objectid) {
1066 free(dir);
1067 goto next;
1070 location.offset = (u64)-1;
1071 search_root = btrfs_read_fs_root(root->fs_info,
1072 &location);
1073 if (IS_ERR(search_root)) {
1074 free(dir);
1075 fprintf(stderr, "Error reading "
1076 "subvolume %s: %lu\n",
1077 path_name,
1078 PTR_ERR(search_root));
1079 if (ignore_errors)
1080 goto next;
1081 ret = PTR_ERR(search_root);
1082 goto out;
1086 * A subvolume will have a key.offset of 0, a
1087 * snapshot will have key.offset of a transid.
1089 if (search_root->root_key.offset != 0 &&
1090 get_snaps == 0) {
1091 free(dir);
1092 printf("Skipping snapshot %s\n",
1093 filename);
1094 goto next;
1096 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
1099 if (verbose)
1100 printf("Restoring %s\n", path_name);
1102 errno = 0;
1103 if (dry_run)
1104 ret = 0;
1105 else
1106 ret = mkdir(path_name, 0755);
1107 if (ret && errno != EEXIST) {
1108 free(dir);
1109 fprintf(stderr, "Error mkdiring %s: %d\n",
1110 path_name, errno);
1111 if (ignore_errors)
1112 goto next;
1113 ret = -1;
1114 goto out;
1116 loops = 0;
1117 ret = search_dir(search_root, &location,
1118 output_rootdir, dir, mreg);
1119 free(dir);
1120 if (ret) {
1121 fprintf(stderr, "Error searching %s\n",
1122 path_name);
1123 if (ignore_errors)
1124 goto next;
1125 goto out;
1127 } else if (type == BTRFS_FT_SYMLINK) {
1128 if (restore_symlinks)
1129 ret = copy_symlink(root, &location, path_name);
1130 if (ret < 0) {
1131 if (ignore_errors)
1132 goto next;
1133 btrfs_release_path(&path);
1134 return ret;
1137 next:
1138 path.slots[0]++;
1141 if (restore_metadata) {
1142 snprintf(path_name, PATH_MAX, "%s%s", output_rootdir, in_dir);
1143 fd = open(path_name, O_RDONLY);
1144 if (fd < 0) {
1145 fprintf(stderr, "ERROR: Failed to access %s to restore metadata\n",
1146 path_name);
1147 if (!ignore_errors) {
1148 ret = -1;
1149 goto out;
1151 } else {
1153 * Set owner/mode/time on the directory as well
1155 key->type = BTRFS_INODE_ITEM_KEY;
1156 ret = copy_metadata(root, fd, key);
1157 close(fd);
1158 if (ret && !ignore_errors)
1159 goto out;
1163 if (verbose)
1164 printf("Done searching %s\n", in_dir);
1165 out:
1166 btrfs_release_path(&path);
1167 return ret;
1170 static int do_list_roots(struct btrfs_root *root)
1172 struct btrfs_key key;
1173 struct btrfs_key found_key;
1174 struct btrfs_disk_key disk_key;
1175 struct btrfs_path path;
1176 struct extent_buffer *leaf;
1177 struct btrfs_root_item ri;
1178 unsigned long offset;
1179 int slot;
1180 int ret;
1182 root = root->fs_info->tree_root;
1184 btrfs_init_path(&path);
1185 key.offset = 0;
1186 key.objectid = 0;
1187 key.type = BTRFS_ROOT_ITEM_KEY;
1188 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1189 if (ret < 0) {
1190 fprintf(stderr, "Failed to do search %d\n", ret);
1191 btrfs_release_path(&path);
1192 return -1;
1195 leaf = path.nodes[0];
1197 while (1) {
1198 slot = path.slots[0];
1199 if (slot >= btrfs_header_nritems(leaf)) {
1200 ret = btrfs_next_leaf(root, &path);
1201 if (ret)
1202 break;
1203 leaf = path.nodes[0];
1204 slot = path.slots[0];
1206 btrfs_item_key(leaf, &disk_key, slot);
1207 btrfs_disk_key_to_cpu(&found_key, &disk_key);
1208 if (found_key.type != BTRFS_ROOT_ITEM_KEY) {
1209 path.slots[0]++;
1210 continue;
1213 offset = btrfs_item_ptr_offset(leaf, slot);
1214 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
1215 printf(" tree ");
1216 btrfs_print_key(&disk_key);
1217 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
1218 btrfs_root_level(&ri));
1219 path.slots[0]++;
1221 btrfs_release_path(&path);
1223 return 0;
1226 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
1227 int super_mirror, int list_roots)
1229 struct btrfs_fs_info *fs_info = NULL;
1230 struct btrfs_root *root = NULL;
1231 u64 bytenr;
1232 int i;
1234 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1235 bytenr = btrfs_sb_offset(i);
1236 fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0,
1237 OPEN_CTREE_PARTIAL);
1238 if (fs_info)
1239 break;
1240 fprintf(stderr, "Could not open root, trying backup super\n");
1243 if (!fs_info)
1244 return NULL;
1247 * All we really need to succeed is reading the chunk tree, everything
1248 * else we can do by hand, since we only need to read the tree root and
1249 * the fs_root.
1251 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
1252 u64 generation;
1254 root = fs_info->tree_root;
1255 if (!root_location)
1256 root_location = btrfs_super_root(fs_info->super_copy);
1257 generation = btrfs_super_generation(fs_info->super_copy);
1258 root->node = read_tree_block(root, root_location,
1259 root->nodesize, generation);
1260 if (!extent_buffer_uptodate(root->node)) {
1261 fprintf(stderr, "Error opening tree root\n");
1262 close_ctree(root);
1263 return NULL;
1267 if (!list_roots && !fs_info->fs_root) {
1268 struct btrfs_key key;
1270 key.objectid = BTRFS_FS_TREE_OBJECTID;
1271 key.type = BTRFS_ROOT_ITEM_KEY;
1272 key.offset = (u64)-1;
1273 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1274 if (IS_ERR(fs_info->fs_root)) {
1275 fprintf(stderr, "Couldn't read fs root: %ld\n",
1276 PTR_ERR(fs_info->fs_root));
1277 close_ctree(fs_info->tree_root);
1278 return NULL;
1282 if (list_roots && do_list_roots(fs_info->tree_root)) {
1283 close_ctree(fs_info->tree_root);
1284 return NULL;
1287 return fs_info->fs_root;
1290 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1292 struct btrfs_path path;
1293 struct btrfs_key found_key;
1294 struct btrfs_key key;
1295 int ret = -1;
1296 int i;
1298 btrfs_init_path(&path);
1299 key.objectid = 0;
1300 key.type = BTRFS_DIR_INDEX_KEY;
1301 key.offset = 0;
1302 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1303 if (ret < 0) {
1304 fprintf(stderr, "Error searching %d\n", ret);
1305 goto out;
1308 if (!path.nodes[0]) {
1309 fprintf(stderr, "No leaf!\n");
1310 goto out;
1312 again:
1313 for (i = path.slots[0];
1314 i < btrfs_header_nritems(path.nodes[0]); i++) {
1315 btrfs_item_key_to_cpu(path.nodes[0], &found_key, i);
1316 if (found_key.type != key.type)
1317 continue;
1319 printf("Using objectid %Lu for first dir\n",
1320 found_key.objectid);
1321 *objectid = found_key.objectid;
1322 ret = 0;
1323 goto out;
1325 do {
1326 ret = next_leaf(root, &path);
1327 if (ret < 0) {
1328 fprintf(stderr, "Error getting next leaf %d\n",
1329 ret);
1330 goto out;
1331 } else if (ret > 0) {
1332 fprintf(stderr, "No more leaves\n");
1333 goto out;
1335 } while (!path.nodes[0]);
1336 if (path.nodes[0])
1337 goto again;
1338 printf("Couldn't find a dir index item\n");
1339 out:
1340 btrfs_release_path(&path);
1341 return ret;
1344 const char * const cmd_restore_usage[] = {
1345 "btrfs restore [options] <device> <path> | -l <device>",
1346 "Try to restore files from a damaged filesystem (unmounted)",
1348 "-s|--snapshots get snapshots",
1349 "-x|--xattr restore extended attributes",
1350 "-m|--metadata restore owner, mode and times",
1351 "-S|--symlink restore symbolic links",
1352 "-v|--verbose verbose",
1353 "-i|--ignore-errors ignore errors",
1354 "-o|--overwrite overwrite",
1355 "-t <bytenr> tree location",
1356 "-f <bytenr> filesystem location",
1357 "-u|--super <mirror> super mirror",
1358 "-r|--root <rootid> root objectid",
1359 "-d find dir",
1360 "-l|--list-roots list tree roots",
1361 "-D|--dry-run dry run (only list files that would be recovered)",
1362 "--path-regex <regex>",
1363 " restore only filenames matching regex,",
1364 " you have to use following syntax (possibly quoted):",
1365 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1366 "-c ignore case (--path-regex only)",
1367 NULL
1370 int cmd_restore(int argc, char **argv)
1372 struct btrfs_root *root;
1373 struct btrfs_key key;
1374 char dir_name[PATH_MAX];
1375 u64 tree_location = 0;
1376 u64 fs_location = 0;
1377 u64 root_objectid = 0;
1378 int len;
1379 int ret;
1380 int super_mirror = 0;
1381 int find_dir = 0;
1382 int list_roots = 0;
1383 const char *match_regstr = NULL;
1384 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1385 regex_t match_reg, *mreg = NULL;
1386 char reg_err[256];
1388 while (1) {
1389 int opt;
1390 enum { GETOPT_VAL_PATH_REGEX = 256 };
1391 static const struct option long_options[] = {
1392 { "path-regex", required_argument, NULL,
1393 GETOPT_VAL_PATH_REGEX },
1394 { "dry-run", no_argument, NULL, 'D'},
1395 { "metadata", no_argument, NULL, 'm'},
1396 { "symlinks", no_argument, NULL, 'S'},
1397 { "snapshots", no_argument, NULL, 's'},
1398 { "xattr", no_argument, NULL, 'x'},
1399 { "verbose", no_argument, NULL, 'v'},
1400 { "ignore-errors", no_argument, NULL, 'i'},
1401 { "overwrite", no_argument, NULL, 'o'},
1402 { "super", required_argument, NULL, 'u'},
1403 { "root", required_argument, NULL, 'r'},
1404 { "list-roots", no_argument, NULL, 'l'},
1405 { NULL, 0, NULL, 0}
1408 opt = getopt_long(argc, argv, "sSxviot:u:dmf:r:lDc", long_options,
1409 NULL);
1410 if (opt < 0)
1411 break;
1413 switch (opt) {
1414 case 's':
1415 get_snaps = 1;
1416 break;
1417 case 'v':
1418 verbose++;
1419 break;
1420 case 'i':
1421 ignore_errors = 1;
1422 break;
1423 case 'o':
1424 overwrite = 1;
1425 break;
1426 case 't':
1427 tree_location = arg_strtou64(optarg);
1428 break;
1429 case 'f':
1430 fs_location = arg_strtou64(optarg);
1431 break;
1432 case 'u':
1433 super_mirror = arg_strtou64(optarg);
1434 if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1435 fprintf(stderr, "Super mirror not "
1436 "valid\n");
1437 exit(1);
1439 break;
1440 case 'd':
1441 find_dir = 1;
1442 break;
1443 case 'r':
1444 root_objectid = arg_strtou64(optarg);
1445 if (!is_fstree(root_objectid)) {
1446 fprintf(stderr, "objectid %llu is not a valid fs/file tree\n",
1447 root_objectid);
1448 exit(1);
1450 break;
1451 case 'l':
1452 list_roots = 1;
1453 break;
1454 case 'm':
1455 restore_metadata = 1;
1456 break;
1457 case 'S':
1458 restore_symlinks = 1;
1459 break;
1460 case 'D':
1461 dry_run = 1;
1462 break;
1463 case 'c':
1464 match_cflags |= REG_ICASE;
1465 break;
1466 case GETOPT_VAL_PATH_REGEX:
1467 match_regstr = optarg;
1468 break;
1469 case 'x':
1470 get_xattrs = 1;
1471 break;
1472 default:
1473 usage(cmd_restore_usage);
1477 if (!list_roots && check_argc_min(argc - optind, 2))
1478 usage(cmd_restore_usage);
1479 else if (list_roots && check_argc_min(argc - optind, 1))
1480 usage(cmd_restore_usage);
1482 if (fs_location && root_objectid) {
1483 fprintf(stderr, "don't use -f and -r at the same time.\n");
1484 return 1;
1487 if ((ret = check_mounted(argv[optind])) < 0) {
1488 fprintf(stderr, "Could not check mount status: %s\n",
1489 strerror(-ret));
1490 return 1;
1491 } else if (ret) {
1492 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1493 return 1;
1496 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1497 if (root == NULL)
1498 return 1;
1500 if (list_roots)
1501 goto out;
1503 if (fs_location != 0) {
1504 free_extent_buffer(root->node);
1505 root->node = read_tree_block(root, fs_location, root->nodesize, 0);
1506 if (!extent_buffer_uptodate(root->node)) {
1507 fprintf(stderr, "Failed to read fs location\n");
1508 ret = 1;
1509 goto out;
1513 memset(path_name, 0, PATH_MAX);
1515 if (strlen(argv[optind + 1]) >= PATH_MAX) {
1516 fprintf(stderr, "ERROR: path too long\n");
1517 ret = 1;
1518 goto out;
1520 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1521 dir_name[sizeof dir_name - 1] = 0;
1523 /* Strip the trailing / on the dir name */
1524 len = strlen(dir_name);
1525 while (len && dir_name[--len] == '/') {
1526 dir_name[len] = '\0';
1529 if (root_objectid != 0) {
1530 struct btrfs_root *orig_root = root;
1532 key.objectid = root_objectid;
1533 key.type = BTRFS_ROOT_ITEM_KEY;
1534 key.offset = (u64)-1;
1535 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1536 if (IS_ERR(root)) {
1537 fprintf(stderr, "fail to read root %llu: %s\n",
1538 root_objectid, strerror(-PTR_ERR(root)));
1539 root = orig_root;
1540 ret = 1;
1541 goto out;
1543 key.type = 0;
1544 key.offset = 0;
1547 if (find_dir) {
1548 ret = find_first_dir(root, &key.objectid);
1549 if (ret)
1550 goto out;
1551 } else {
1552 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1555 if (match_regstr) {
1556 ret = regcomp(&match_reg, match_regstr, match_cflags);
1557 if (ret) {
1558 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1559 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1560 goto out;
1562 mreg = &match_reg;
1565 if (dry_run)
1566 printf("This is a dry-run, no files are going to be restored\n");
1568 ret = search_dir(root, &key, dir_name, "", mreg);
1570 out:
1571 if (mreg)
1572 regfree(mreg);
1573 close_ctree(root);
1574 return !!ret;