btrfs-progs: pretty print key in extent_item
[btrfs-progs-unstable/devel.git] / restore.c
blobb9e5381f1d87c5400644c061386d58c1479a1351
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.
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <zlib.h>
28 #include "kerncompat.h"
29 #include "ctree.h"
30 #include "disk-io.h"
31 #include "print-tree.h"
32 #include "transaction.h"
33 #include "list.h"
34 #include "version.h"
35 #include "volumes.h"
36 #include "utils.h"
38 static char path_name[4096];
39 static int get_snaps = 0;
40 static int verbose = 0;
41 static int ignore_errors = 0;
42 static int overwrite = 0;
44 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
45 u64 decompress_len)
47 z_stream strm;
48 int ret;
50 memset(&strm, 0, sizeof(strm));
51 ret = inflateInit(&strm);
52 if (ret != Z_OK) {
53 fprintf(stderr, "inflate init returnd %d\n", ret);
54 return -1;
57 strm.avail_in = compress_len;
58 strm.next_in = (unsigned char *)inbuf;
59 strm.avail_out = decompress_len;
60 strm.next_out = (unsigned char *)outbuf;
61 ret = inflate(&strm, Z_NO_FLUSH);
62 if (ret != Z_STREAM_END) {
63 (void)inflateEnd(&strm);
64 fprintf(stderr, "ret is %d\n", ret);
65 return -1;
68 (void)inflateEnd(&strm);
69 return 0;
72 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
74 int slot;
75 int level = 1;
76 struct extent_buffer *c;
77 struct extent_buffer *next = NULL;
79 for (; level < BTRFS_MAX_LEVEL; level++) {
80 if (path->nodes[level])
81 break;
84 if (level == BTRFS_MAX_LEVEL)
85 return 1;
87 slot = path->slots[level] + 1;
89 while(level < BTRFS_MAX_LEVEL) {
90 if (!path->nodes[level])
91 return 1;
93 slot = path->slots[level] + 1;
94 c = path->nodes[level];
95 if (slot >= btrfs_header_nritems(c)) {
96 level++;
97 if (level == BTRFS_MAX_LEVEL)
98 return 1;
99 continue;
102 if (next)
103 free_extent_buffer(next);
105 if (path->reada)
106 reada_for_search(root, path, level, slot, 0);
108 next = read_node_slot(root, c, slot);
109 break;
111 path->slots[level] = slot;
112 while(1) {
113 level--;
114 c = path->nodes[level];
115 free_extent_buffer(c);
116 path->nodes[level] = next;
117 path->slots[level] = 0;
118 if (!level)
119 break;
120 if (path->reada)
121 reada_for_search(root, path, level, 0, 0);
122 next = read_node_slot(root, next, 0);
124 return 0;
127 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
129 struct extent_buffer *leaf = path->nodes[0];
130 struct btrfs_file_extent_item *fi;
131 char buf[4096];
132 char *outbuf;
133 ssize_t done;
134 unsigned long ptr;
135 int ret;
136 int len;
137 int ram_size;
138 int compress;
140 fi = btrfs_item_ptr(leaf, path->slots[0],
141 struct btrfs_file_extent_item);
142 ptr = btrfs_file_extent_inline_start(fi);
143 len = btrfs_file_extent_inline_item_len(leaf,
144 btrfs_item_nr(leaf, path->slots[0]));
145 read_extent_buffer(leaf, buf, ptr, len);
147 compress = btrfs_file_extent_compression(leaf, fi);
148 if (compress == BTRFS_COMPRESS_NONE) {
149 done = pwrite(fd, buf, len, pos);
150 if (done < len) {
151 fprintf(stderr, "Short inline write, wanted %d, did "
152 "%zd: %d\n", len, done, errno);
153 return -1;
155 return 0;
158 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
159 outbuf = malloc(ram_size);
160 if (!outbuf) {
161 fprintf(stderr, "No memory\n");
162 return -1;
165 ret = decompress(buf, outbuf, len, ram_size);
166 if (ret) {
167 free(outbuf);
168 return ret;
171 done = pwrite(fd, outbuf, ram_size, pos);
172 free(outbuf);
173 if (done < len) {
174 fprintf(stderr, "Short compressed inline write, wanted %d, "
175 "did %zd: %d\n", ram_size, done, errno);
176 return -1;
179 return 0;
182 static int copy_one_extent(struct btrfs_root *root, int fd,
183 struct extent_buffer *leaf,
184 struct btrfs_file_extent_item *fi, u64 pos)
186 struct btrfs_multi_bio *multi = NULL;
187 struct btrfs_device *device;
188 char *inbuf, *outbuf = NULL;
189 ssize_t done, total = 0;
190 u64 bytenr;
191 u64 ram_size;
192 u64 disk_size;
193 u64 length;
194 u64 size_left;
195 u64 dev_bytenr;
196 u64 count = 0;
197 int compress;
198 int ret;
199 int dev_fd;
201 compress = btrfs_file_extent_compression(leaf, fi);
202 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
203 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
204 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
205 size_left = disk_size;
207 /* we found a hole */
208 if (disk_size == 0)
209 return 0;
211 inbuf = malloc(disk_size);
212 if (!inbuf) {
213 fprintf(stderr, "No memory\n");
214 return -1;
217 if (compress != BTRFS_COMPRESS_NONE) {
218 outbuf = malloc(ram_size);
219 if (!outbuf) {
220 fprintf(stderr, "No memory\n");
221 free(inbuf);
222 return -1;
225 again:
226 length = size_left;
227 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
228 bytenr, &length, &multi, 0);
229 if (ret) {
230 free(inbuf);
231 free(outbuf);
232 fprintf(stderr, "Error mapping block %d\n", ret);
233 return ret;
235 device = multi->stripes[0].dev;
236 dev_fd = device->fd;
237 device->total_ios++;
238 dev_bytenr = multi->stripes[0].physical;
239 kfree(multi);
241 if (size_left < length)
242 length = size_left;
243 size_left -= length;
245 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
246 if (done < length) {
247 free(inbuf);
248 free(outbuf);
249 fprintf(stderr, "Short read %d\n", errno);
250 return -1;
253 count += length;
254 bytenr += length;
255 if (size_left)
256 goto again;
259 if (compress == BTRFS_COMPRESS_NONE) {
260 while (total < ram_size) {
261 done = pwrite(fd, inbuf+total, ram_size-total,
262 pos+total);
263 if (done < 0) {
264 free(inbuf);
265 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
266 return -1;
268 total += done;
270 free(inbuf);
271 return 0;
274 ret = decompress(inbuf, outbuf, disk_size, ram_size);
275 free(inbuf);
276 if (ret) {
277 free(outbuf);
278 return ret;
281 while (total < ram_size) {
282 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
283 if (done < 0) {
284 free(outbuf);
285 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
286 return -1;
288 total += done;
290 free(outbuf);
292 return 0;
295 static int ask_to_continue(const char *file)
297 char buf[2];
298 char *ret;
300 printf("We seem to be looping a lot on %s, do you want to keep going "
301 "on ? (y/N): ", file);
302 again:
303 ret = fgets(buf, 2, stdin);
304 if (*ret == '\n' || tolower(*ret) == 'n')
305 return 1;
306 if (tolower(*ret) != 'y') {
307 printf("Please enter either 'y' or 'n': ");
308 goto again;
311 return 0;
315 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
316 const char *file)
318 struct extent_buffer *leaf;
319 struct btrfs_path *path;
320 struct btrfs_file_extent_item *fi;
321 struct btrfs_inode_item *inode_item;
322 struct btrfs_key found_key;
323 int ret;
324 int extent_type;
325 int compression;
326 int loops = 0;
327 u64 found_size = 0;
329 path = btrfs_alloc_path();
330 if (!path) {
331 fprintf(stderr, "Ran out of memory\n");
332 return -1;
334 path->skip_locking = 1;
336 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
337 if (ret == 0) {
338 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
339 struct btrfs_inode_item);
340 found_size = btrfs_inode_size(path->nodes[0], inode_item);
342 btrfs_release_path(root, path);
344 key->offset = 0;
345 key->type = BTRFS_EXTENT_DATA_KEY;
347 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
348 if (ret < 0) {
349 fprintf(stderr, "Error searching %d\n", ret);
350 btrfs_free_path(path);
351 return ret;
354 leaf = path->nodes[0];
355 while (!leaf) {
356 ret = next_leaf(root, path);
357 if (ret < 0) {
358 fprintf(stderr, "Error getting next leaf %d\n",
359 ret);
360 btrfs_free_path(path);
361 return ret;
362 } else if (ret > 0) {
363 /* No more leaves to search */
364 btrfs_free_path(path);
365 return 0;
367 leaf = path->nodes[0];
370 while (1) {
371 if (loops++ >= 1024) {
372 ret = ask_to_continue(file);
373 if (ret)
374 break;
375 loops = 0;
377 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
378 do {
379 ret = next_leaf(root, path);
380 if (ret < 0) {
381 fprintf(stderr, "Error searching %d\n", ret);
382 btrfs_free_path(path);
383 return ret;
384 } else if (ret) {
385 /* No more leaves to search */
386 btrfs_free_path(path);
387 goto set_size;
389 leaf = path->nodes[0];
390 } while (!leaf);
391 continue;
393 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
394 if (found_key.objectid != key->objectid)
395 break;
396 if (found_key.type != key->type)
397 break;
398 fi = btrfs_item_ptr(leaf, path->slots[0],
399 struct btrfs_file_extent_item);
400 extent_type = btrfs_file_extent_type(leaf, fi);
401 compression = btrfs_file_extent_compression(leaf, fi);
402 if (compression >= BTRFS_COMPRESS_LAST) {
403 fprintf(stderr, "Don't support compression yet %d\n",
404 compression);
405 btrfs_free_path(path);
406 return -1;
409 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
410 goto next;
411 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
412 ret = copy_one_inline(fd, path, found_key.offset);
413 if (ret) {
414 btrfs_free_path(path);
415 return -1;
417 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
418 ret = copy_one_extent(root, fd, leaf, fi,
419 found_key.offset);
420 if (ret) {
421 btrfs_free_path(path);
422 return ret;
424 } else {
425 printf("Weird extent type %d\n", extent_type);
427 next:
428 path->slots[0]++;
431 btrfs_free_path(path);
432 set_size:
433 if (found_size) {
434 ret = ftruncate(fd, (loff_t)found_size);
435 if (ret)
436 return ret;
438 return 0;
441 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
442 const char *dir)
444 struct btrfs_path *path;
445 struct extent_buffer *leaf;
446 struct btrfs_dir_item *dir_item;
447 struct btrfs_key found_key, location;
448 char filename[BTRFS_NAME_LEN + 1];
449 unsigned long name_ptr;
450 int name_len;
451 int ret;
452 int fd;
453 int loops = 0;
454 u8 type;
456 path = btrfs_alloc_path();
457 if (!path) {
458 fprintf(stderr, "Ran out of memory\n");
459 return -1;
461 path->skip_locking = 1;
463 key->offset = 0;
464 key->type = BTRFS_DIR_INDEX_KEY;
466 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
467 if (ret < 0) {
468 fprintf(stderr, "Error searching %d\n", ret);
469 btrfs_free_path(path);
470 return ret;
473 leaf = path->nodes[0];
474 while (!leaf) {
475 if (verbose > 1)
476 printf("No leaf after search, looking for the next "
477 "leaf\n");
478 ret = next_leaf(root, path);
479 if (ret < 0) {
480 fprintf(stderr, "Error getting next leaf %d\n",
481 ret);
482 btrfs_free_path(path);
483 return ret;
484 } else if (ret > 0) {
485 /* No more leaves to search */
486 if (verbose)
487 printf("Reached the end of the tree looking "
488 "for the directory\n");
489 btrfs_free_path(path);
490 return 0;
492 leaf = path->nodes[0];
495 while (leaf) {
496 if (loops++ >= 1024) {
497 printf("We have looped trying to restore files in %s "
498 "too many times to be making progress, "
499 "stopping\n", dir);
500 break;
503 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
504 do {
505 ret = next_leaf(root, path);
506 if (ret < 0) {
507 fprintf(stderr, "Error searching %d\n",
508 ret);
509 btrfs_free_path(path);
510 return ret;
511 } else if (ret > 0) {
512 /* No more leaves to search */
513 if (verbose)
514 printf("Reached the end of "
515 "the tree searching the"
516 " directory\n");
517 btrfs_free_path(path);
518 return 0;
520 leaf = path->nodes[0];
521 } while (!leaf);
522 continue;
524 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
525 if (found_key.objectid != key->objectid) {
526 if (verbose > 1)
527 printf("Found objectid=%Lu, key=%Lu\n",
528 found_key.objectid, key->objectid);
529 break;
531 if (found_key.type != key->type) {
532 if (verbose > 1)
533 printf("Found type=%u, want=%u\n",
534 found_key.type, key->type);
535 break;
537 dir_item = btrfs_item_ptr(leaf, path->slots[0],
538 struct btrfs_dir_item);
539 name_ptr = (unsigned long)(dir_item + 1);
540 name_len = btrfs_dir_name_len(leaf, dir_item);
541 read_extent_buffer(leaf, filename, name_ptr, name_len);
542 filename[name_len] = '\0';
543 type = btrfs_dir_type(leaf, dir_item);
544 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
546 snprintf(path_name, 4096, "%s/%s", dir, filename);
550 * At this point we're only going to restore directories and
551 * files, no symlinks or anything else.
553 if (type == BTRFS_FT_REG_FILE) {
554 if (!overwrite) {
555 static int warn = 0;
556 struct stat st;
558 ret = stat(path_name, &st);
559 if (!ret) {
560 loops = 0;
561 if (verbose || !warn)
562 printf("Skipping existing file"
563 " %s\n", path_name);
564 if (warn)
565 goto next;
566 printf("If you wish to overwrite use "
567 "the -o option to overwrite\n");
568 warn = 1;
569 goto next;
571 ret = 0;
573 if (verbose)
574 printf("Restoring %s\n", path_name);
575 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
576 if (fd < 0) {
577 fprintf(stderr, "Error creating %s: %d\n",
578 path_name, errno);
579 if (ignore_errors)
580 goto next;
581 btrfs_free_path(path);
582 return -1;
584 loops = 0;
585 ret = copy_file(root, fd, &location, path_name);
586 close(fd);
587 if (ret) {
588 if (ignore_errors)
589 goto next;
590 btrfs_free_path(path);
591 return ret;
593 } else if (type == BTRFS_FT_DIR) {
594 struct btrfs_root *search_root = root;
595 char *dir = strdup(path_name);
597 if (!dir) {
598 fprintf(stderr, "Ran out of memory\n");
599 btrfs_free_path(path);
600 return -1;
603 if (location.type == BTRFS_ROOT_ITEM_KEY) {
605 * If we are a snapshot and this is the index
606 * object to ourselves just skip it.
608 if (location.objectid ==
609 root->root_key.objectid) {
610 free(dir);
611 goto next;
614 search_root = btrfs_read_fs_root(root->fs_info,
615 &location);
616 if (IS_ERR(search_root)) {
617 free(dir);
618 fprintf(stderr, "Error reading "
619 "subvolume %s: %lu\n",
620 path_name,
621 PTR_ERR(search_root));
622 if (ignore_errors)
623 goto next;
624 return PTR_ERR(search_root);
628 * A subvolume will have a key.offset of 0, a
629 * snapshot will have key.offset of a transid.
631 if (search_root->root_key.offset != 0 &&
632 get_snaps == 0) {
633 free(dir);
634 printf("Skipping snapshot %s\n",
635 filename);
636 goto next;
638 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
641 if (verbose)
642 printf("Restoring %s\n", path_name);
644 errno = 0;
645 ret = mkdir(path_name, 0755);
646 if (ret && errno != EEXIST) {
647 free(dir);
648 fprintf(stderr, "Error mkdiring %s: %d\n",
649 path_name, errno);
650 if (ignore_errors)
651 goto next;
652 btrfs_free_path(path);
653 return -1;
655 loops = 0;
656 ret = search_dir(search_root, &location, dir);
657 free(dir);
658 if (ret) {
659 if (ignore_errors)
660 goto next;
661 btrfs_free_path(path);
662 return ret;
665 next:
666 path->slots[0]++;
669 if (verbose)
670 printf("Done searching %s\n", dir);
671 btrfs_free_path(path);
672 return 0;
675 static void usage()
677 fprintf(stderr, "Usage: restore [-svio] [-t disk offset] <device> "
678 "<directory>\n");
681 static struct btrfs_root *open_fs(const char *dev, u64 root_location, int super_mirror)
683 struct btrfs_root *root;
684 u64 bytenr;
685 int i;
687 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
688 bytenr = btrfs_sb_offset(i);
689 root = open_ctree_recovery(dev, bytenr, root_location);
690 if (root)
691 return root;
692 fprintf(stderr, "Could not open root, trying backup super\n");
695 return NULL;
698 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
700 struct btrfs_path *path;
701 struct btrfs_key found_key;
702 struct btrfs_key key;
703 int ret = -1;
704 int i;
706 key.objectid = 0;
707 key.type = BTRFS_DIR_INDEX_KEY;
708 key.offset = 0;
710 path = btrfs_alloc_path();
711 if (!path) {
712 fprintf(stderr, "Ran out of memory\n");
713 goto out;
716 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
717 if (ret < 0) {
718 fprintf(stderr, "Error searching %d\n", ret);
719 goto out;
722 if (!path->nodes[0]) {
723 fprintf(stderr, "No leaf!\n");
724 goto out;
726 again:
727 for (i = path->slots[0];
728 i < btrfs_header_nritems(path->nodes[0]); i++) {
729 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
730 if (found_key.type != key.type)
731 continue;
733 printf("Using objectid %Lu for first dir\n",
734 found_key.objectid);
735 *objectid = found_key.objectid;
736 ret = 0;
737 goto out;
739 do {
740 ret = next_leaf(root, path);
741 if (ret < 0) {
742 fprintf(stderr, "Error getting next leaf %d\n",
743 ret);
744 goto out;
745 } else if (ret > 0) {
746 fprintf(stderr, "No more leaves\n");
747 goto out;
749 } while (!path->nodes[0]);
750 if (path->nodes[0])
751 goto again;
752 printf("Couldn't find a dir index item\n");
753 out:
754 btrfs_free_path(path);
755 return ret;
758 int main(int argc, char **argv)
760 struct btrfs_root *root;
761 struct btrfs_key key;
762 char dir_name[128];
763 u64 tree_location = 0;
764 u64 fs_location = 0;
765 int len;
766 int ret;
767 int opt;
768 int super_mirror = 0;
769 int find_dir = 0;
771 while ((opt = getopt(argc, argv, "sviot:u:df:")) != -1) {
772 switch (opt) {
773 case 's':
774 get_snaps = 1;
775 break;
776 case 'v':
777 verbose++;
778 break;
779 case 'i':
780 ignore_errors = 1;
781 break;
782 case 'o':
783 overwrite = 1;
784 break;
785 case 't':
786 errno = 0;
787 tree_location = (u64)strtoll(optarg, NULL, 10);
788 if (errno != 0) {
789 fprintf(stderr, "Tree location not valid\n");
790 exit(1);
792 break;
793 case 'f':
794 errno = 0;
795 fs_location = (u64)strtoll(optarg, NULL, 10);
796 if (errno != 0) {
797 fprintf(stderr, "Fs location not valid\n");
798 exit(1);
800 break;
801 case 'u':
802 errno = 0;
803 super_mirror = (int)strtol(optarg, NULL, 10);
804 if (errno != 0 ||
805 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
806 fprintf(stderr, "Super mirror not "
807 "valid\n");
808 exit(1);
810 break;
811 case 'd':
812 find_dir = 1;
813 break;
814 default:
815 usage();
816 exit(1);
820 if (optind + 1 >= argc) {
821 usage();
822 exit(1);
825 if ((ret = check_mounted(argv[optind])) < 0) {
826 fprintf(stderr, "Could not check mount status: %s\n",
827 strerror(ret));
828 return ret;
829 } else if (ret) {
830 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind + 1]);
831 return -EBUSY;
834 root = open_fs(argv[optind], tree_location, super_mirror);
835 if (root == NULL)
836 return 1;
838 if (fs_location != 0) {
839 free_extent_buffer(root->node);
840 root->node = read_tree_block(root, fs_location, 4096, 0);
841 if (!root->node) {
842 fprintf(stderr, "Failed to read fs location\n");
843 goto out;
847 printf("Root objectid is %Lu\n", root->objectid);
849 memset(path_name, 0, 4096);
851 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
852 dir_name[sizeof dir_name - 1] = 0;
854 /* Strip the trailing / on the dir name */
855 len = strlen(dir_name);
856 while (len && dir_name[--len] == '/') {
857 dir_name[len] = '\0';
860 if (find_dir) {
861 ret = find_first_dir(root, &key.objectid);
862 if (ret)
863 goto out;
864 } else {
865 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
868 ret = search_dir(root->fs_info->fs_root, &key, dir_name);
870 out:
871 close_ctree(root);
872 return ret;