btrfs-progs: mkfs: add option to skip trim
[btrfs-progs-unstable/devel.git] / restore.c
blobd1ac542076e9bff6957406f799bf22f808588fbf
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;
388 return 0;
390 leaf = path->nodes[0];
391 } while (!leaf);
392 continue;
394 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
395 if (found_key.objectid != key->objectid)
396 break;
397 if (found_key.type != key->type)
398 break;
399 fi = btrfs_item_ptr(leaf, path->slots[0],
400 struct btrfs_file_extent_item);
401 extent_type = btrfs_file_extent_type(leaf, fi);
402 compression = btrfs_file_extent_compression(leaf, fi);
403 if (compression >= BTRFS_COMPRESS_LAST) {
404 fprintf(stderr, "Don't support compression yet %d\n",
405 compression);
406 btrfs_free_path(path);
407 return -1;
410 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
411 goto next;
412 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
413 ret = copy_one_inline(fd, path, found_key.offset);
414 if (ret) {
415 btrfs_free_path(path);
416 return -1;
418 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
419 ret = copy_one_extent(root, fd, leaf, fi,
420 found_key.offset);
421 if (ret) {
422 btrfs_free_path(path);
423 return ret;
425 } else {
426 printf("Weird extent type %d\n", extent_type);
428 next:
429 path->slots[0]++;
432 btrfs_free_path(path);
433 set_size:
434 if (found_size)
435 ftruncate(fd, (loff_t)found_size);
436 return 0;
439 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
440 const char *dir)
442 struct btrfs_path *path;
443 struct extent_buffer *leaf;
444 struct btrfs_dir_item *dir_item;
445 struct btrfs_key found_key, location;
446 char filename[BTRFS_NAME_LEN + 1];
447 unsigned long name_ptr;
448 int name_len;
449 int ret;
450 int fd;
451 int loops = 0;
452 u8 type;
454 path = btrfs_alloc_path();
455 if (!path) {
456 fprintf(stderr, "Ran out of memory\n");
457 return -1;
459 path->skip_locking = 1;
461 key->offset = 0;
462 key->type = BTRFS_DIR_INDEX_KEY;
464 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
465 if (ret < 0) {
466 fprintf(stderr, "Error searching %d\n", ret);
467 btrfs_free_path(path);
468 return ret;
471 leaf = path->nodes[0];
472 while (!leaf) {
473 if (verbose > 1)
474 printf("No leaf after search, looking for the next "
475 "leaf\n");
476 ret = next_leaf(root, path);
477 if (ret < 0) {
478 fprintf(stderr, "Error getting next leaf %d\n",
479 ret);
480 btrfs_free_path(path);
481 return ret;
482 } else if (ret > 0) {
483 /* No more leaves to search */
484 if (verbose)
485 printf("Reached the end of the tree looking "
486 "for the directory\n");
487 btrfs_free_path(path);
488 return 0;
490 leaf = path->nodes[0];
493 while (leaf) {
494 if (loops++ >= 1024) {
495 printf("We have looped trying to restore files in %s "
496 "too many times to be making progress, "
497 "stopping\n", dir);
498 break;
501 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
502 do {
503 ret = next_leaf(root, path);
504 if (ret < 0) {
505 fprintf(stderr, "Error searching %d\n",
506 ret);
507 btrfs_free_path(path);
508 return ret;
509 } else if (ret > 0) {
510 /* No more leaves to search */
511 if (verbose)
512 printf("Reached the end of "
513 "the tree searching the"
514 " directory\n");
515 btrfs_free_path(path);
516 return 0;
518 leaf = path->nodes[0];
519 } while (!leaf);
520 continue;
522 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
523 if (found_key.objectid != key->objectid) {
524 if (verbose > 1)
525 printf("Found objectid=%Lu, key=%Lu\n",
526 found_key.objectid, key->objectid);
527 break;
529 if (found_key.type != key->type) {
530 if (verbose > 1)
531 printf("Found type=%u, want=%u\n",
532 found_key.type, key->type);
533 break;
535 dir_item = btrfs_item_ptr(leaf, path->slots[0],
536 struct btrfs_dir_item);
537 name_ptr = (unsigned long)(dir_item + 1);
538 name_len = btrfs_dir_name_len(leaf, dir_item);
539 read_extent_buffer(leaf, filename, name_ptr, name_len);
540 filename[name_len] = '\0';
541 type = btrfs_dir_type(leaf, dir_item);
542 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
544 snprintf(path_name, 4096, "%s/%s", dir, filename);
548 * At this point we're only going to restore directories and
549 * files, no symlinks or anything else.
551 if (type == BTRFS_FT_REG_FILE) {
552 if (!overwrite) {
553 static int warn = 0;
554 struct stat st;
556 ret = stat(path_name, &st);
557 if (!ret) {
558 loops = 0;
559 if (verbose || !warn)
560 printf("Skipping existing file"
561 " %s\n", path_name);
562 if (warn)
563 goto next;
564 printf("If you wish to overwrite use "
565 "the -o option to overwrite\n");
566 warn = 1;
567 goto next;
569 ret = 0;
571 if (verbose)
572 printf("Restoring %s\n", path_name);
573 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
574 if (fd < 0) {
575 fprintf(stderr, "Error creating %s: %d\n",
576 path_name, errno);
577 if (ignore_errors)
578 goto next;
579 btrfs_free_path(path);
580 return -1;
582 loops = 0;
583 ret = copy_file(root, fd, &location, path_name);
584 close(fd);
585 if (ret) {
586 if (ignore_errors)
587 goto next;
588 btrfs_free_path(path);
589 return ret;
591 } else if (type == BTRFS_FT_DIR) {
592 struct btrfs_root *search_root = root;
593 char *dir = strdup(path_name);
595 if (!dir) {
596 fprintf(stderr, "Ran out of memory\n");
597 btrfs_free_path(path);
598 return -1;
601 if (location.type == BTRFS_ROOT_ITEM_KEY) {
603 * If we are a snapshot and this is the index
604 * object to ourselves just skip it.
606 if (location.objectid ==
607 root->root_key.objectid) {
608 free(dir);
609 goto next;
612 search_root = btrfs_read_fs_root(root->fs_info,
613 &location);
614 if (IS_ERR(search_root)) {
615 free(dir);
616 fprintf(stderr, "Error reading "
617 "subvolume %s: %lu\n",
618 path_name,
619 PTR_ERR(search_root));
620 if (ignore_errors)
621 goto next;
622 return PTR_ERR(search_root);
626 * A subvolume will have a key.offset of 0, a
627 * snapshot will have key.offset of a transid.
629 if (search_root->root_key.offset != 0 &&
630 get_snaps == 0) {
631 free(dir);
632 printf("Skipping snapshot %s\n",
633 filename);
634 goto next;
636 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
639 if (verbose)
640 printf("Restoring %s\n", path_name);
642 errno = 0;
643 ret = mkdir(path_name, 0755);
644 if (ret && errno != EEXIST) {
645 free(dir);
646 fprintf(stderr, "Error mkdiring %s: %d\n",
647 path_name, errno);
648 if (ignore_errors)
649 goto next;
650 btrfs_free_path(path);
651 return -1;
653 loops = 0;
654 ret = search_dir(search_root, &location, dir);
655 free(dir);
656 if (ret) {
657 if (ignore_errors)
658 goto next;
659 btrfs_free_path(path);
660 return ret;
663 next:
664 path->slots[0]++;
667 if (verbose)
668 printf("Done searching %s\n", dir);
669 btrfs_free_path(path);
670 return 0;
673 static void usage()
675 fprintf(stderr, "Usage: restore [-svio] [-t disk offset] <device> "
676 "<directory>\n");
679 static struct btrfs_root *open_fs(const char *dev, u64 root_location, int super_mirror)
681 struct btrfs_root *root;
682 u64 bytenr;
683 int i;
685 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
686 bytenr = btrfs_sb_offset(i);
687 root = open_ctree_recovery(dev, bytenr, root_location);
688 if (root)
689 return root;
690 fprintf(stderr, "Could not open root, trying backup super\n");
693 return NULL;
696 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
698 struct btrfs_path *path;
699 struct btrfs_key found_key;
700 struct btrfs_key key;
701 int ret = -1;
702 int i;
704 key.objectid = 0;
705 key.type = BTRFS_DIR_INDEX_KEY;
706 key.offset = 0;
708 path = btrfs_alloc_path();
709 if (!path) {
710 fprintf(stderr, "Ran out of memory\n");
711 goto out;
714 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
715 if (ret < 0) {
716 fprintf(stderr, "Error searching %d\n", ret);
717 goto out;
720 if (!path->nodes[0]) {
721 fprintf(stderr, "No leaf!\n");
722 goto out;
724 again:
725 for (i = path->slots[0];
726 i < btrfs_header_nritems(path->nodes[0]); i++) {
727 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
728 if (found_key.type != key.type)
729 continue;
731 printf("Using objectid %Lu for first dir\n",
732 found_key.objectid);
733 *objectid = found_key.objectid;
734 ret = 0;
735 goto out;
737 do {
738 ret = next_leaf(root, path);
739 if (ret < 0) {
740 fprintf(stderr, "Error getting next leaf %d\n",
741 ret);
742 goto out;
743 } else if (ret > 0) {
744 fprintf(stderr, "No more leaves\n");
745 goto out;
747 } while (!path->nodes[0]);
748 if (path->nodes[0])
749 goto again;
750 printf("Couldn't find a dir index item\n");
751 out:
752 btrfs_free_path(path);
753 return ret;
756 int main(int argc, char **argv)
758 struct btrfs_root *root;
759 struct btrfs_key key;
760 char dir_name[128];
761 u64 tree_location = 0;
762 u64 fs_location = 0;
763 int len;
764 int ret;
765 int opt;
766 int super_mirror = 0;
767 int find_dir = 0;
769 while ((opt = getopt(argc, argv, "sviot:u:df:")) != -1) {
770 switch (opt) {
771 case 's':
772 get_snaps = 1;
773 break;
774 case 'v':
775 verbose++;
776 break;
777 case 'i':
778 ignore_errors = 1;
779 break;
780 case 'o':
781 overwrite = 1;
782 break;
783 case 't':
784 errno = 0;
785 tree_location = (u64)strtoll(optarg, NULL, 10);
786 if (errno != 0) {
787 fprintf(stderr, "Tree location not valid\n");
788 exit(1);
790 break;
791 case 'f':
792 errno = 0;
793 fs_location = (u64)strtoll(optarg, NULL, 10);
794 if (errno != 0) {
795 fprintf(stderr, "Fs location not valid\n");
796 exit(1);
798 break;
799 case 'u':
800 errno = 0;
801 super_mirror = (int)strtol(optarg, NULL, 10);
802 if (errno != 0 ||
803 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
804 fprintf(stderr, "Super mirror not "
805 "valid\n");
806 exit(1);
808 break;
809 case 'd':
810 find_dir = 1;
811 break;
812 default:
813 usage();
814 exit(1);
818 if (optind + 1 >= argc) {
819 usage();
820 exit(1);
823 if ((ret = check_mounted(argv[optind])) < 0) {
824 fprintf(stderr, "Could not check mount status: %s\n",
825 strerror(ret));
826 return ret;
827 } else if (ret) {
828 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind + 1]);
829 return -EBUSY;
832 root = open_fs(argv[optind], tree_location, super_mirror);
833 if (root == NULL)
834 return 1;
836 if (fs_location != 0) {
837 free_extent_buffer(root->node);
838 root->node = read_tree_block(root, fs_location, 4096, 0);
839 if (!root->node) {
840 fprintf(stderr, "Failed to read fs location\n");
841 goto out;
845 printf("Root objectid is %Lu\n", root->objectid);
847 memset(path_name, 0, 4096);
849 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
850 dir_name[sizeof dir_name - 1] = 0;
852 /* Strip the trailing / on the dir name */
853 len = strlen(dir_name);
854 while (len && dir_name[--len] == '/') {
855 dir_name[len] = '\0';
858 if (find_dir) {
859 ret = find_first_dir(root, &key.objectid);
860 if (ret)
861 goto out;
862 } else {
863 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
866 ret = search_dir(root->fs_info->fs_root, &key, dir_name);
868 out:
869 close_ctree(root);
870 return ret;