Btrfs-progs: Rename btrfsck.c -> cmds-check.c
[platform/upstream/btrfs-progs.git] / restore.c
1 /*
2  * Copyright (C) 2011 Red Hat.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21
22 #include "kerncompat.h"
23
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <zlib.h>
31
32 #include "ctree.h"
33 #include "disk-io.h"
34 #include "print-tree.h"
35 #include "transaction.h"
36 #include "list.h"
37 #include "version.h"
38 #include "volumes.h"
39 #include "utils.h"
40
41 static char path_name[4096];
42 static int get_snaps = 0;
43 static int verbose = 0;
44 static int ignore_errors = 0;
45 static int overwrite = 0;
46
47 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
48                       u64 decompress_len)
49 {
50         z_stream strm;
51         int ret;
52
53         memset(&strm, 0, sizeof(strm));
54         ret = inflateInit(&strm);
55         if (ret != Z_OK) {
56                 fprintf(stderr, "inflate init returnd %d\n", ret);
57                 return -1;
58         }
59
60         strm.avail_in = compress_len;
61         strm.next_in = (unsigned char *)inbuf;
62         strm.avail_out = decompress_len;
63         strm.next_out = (unsigned char *)outbuf;
64         ret = inflate(&strm, Z_NO_FLUSH);
65         if (ret != Z_STREAM_END) {
66                 (void)inflateEnd(&strm);
67                 fprintf(stderr, "ret is %d\n", ret);
68                 return -1;
69         }
70
71         (void)inflateEnd(&strm);
72         return 0;
73 }
74
75 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
76 {
77         int slot;
78         int level = 1;
79         struct extent_buffer *c;
80         struct extent_buffer *next = NULL;
81
82         for (; level < BTRFS_MAX_LEVEL; level++) {
83                 if (path->nodes[level])
84                         break;
85         }
86
87         if (level == BTRFS_MAX_LEVEL)
88                 return 1;
89
90         slot = path->slots[level] + 1;
91
92         while(level < BTRFS_MAX_LEVEL) {
93                 if (!path->nodes[level])
94                         return 1;
95
96                 slot = path->slots[level] + 1;
97                 c = path->nodes[level];
98                 if (slot >= btrfs_header_nritems(c)) {
99                         level++;
100                         if (level == BTRFS_MAX_LEVEL)
101                                 return 1;
102                         continue;
103                 }
104
105                 if (path->reada)
106                         reada_for_search(root, path, level, slot, 0);
107
108                 next = read_node_slot(root, c, slot);
109                 break;
110         }
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);
123         }
124         return 0;
125 }
126
127 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
128 {
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;
139
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);
146
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;
154                 }
155                 return 0;
156         }
157
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;
163         }
164
165         ret = decompress(buf, outbuf, len, ram_size);
166         if (ret) {
167                 free(outbuf);
168                 return ret;
169         }
170
171         done = pwrite(fd, outbuf, ram_size, pos);
172         free(outbuf);
173         if (done < ram_size) {
174                 fprintf(stderr, "Short compressed inline write, wanted %d, "
175                         "did %zd: %d\n", ram_size, done, errno);
176                 return -1;
177         }
178
179         return 0;
180 }
181
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)
185 {
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;
200
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;
206
207         /* we found a hole */
208         if (disk_size == 0)
209                 return 0;
210
211         inbuf = malloc(disk_size);
212         if (!inbuf) {
213                 fprintf(stderr, "No memory\n");
214                 return -1;
215         }
216
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;
223                 }
224         }
225 again:
226         length = size_left;
227         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
228                               bytenr, &length, &multi, 0, NULL);
229         if (ret) {
230                 free(inbuf);
231                 free(outbuf);
232                 fprintf(stderr, "Error mapping block %d\n", ret);
233                 return ret;
234         }
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);
240
241         if (size_left < length)
242                 length = size_left;
243         size_left -= length;
244
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;
251         }
252
253         count += length;
254         bytenr += length;
255         if (size_left)
256                 goto again;
257
258
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;
267                         }
268                         total += done;
269                 }
270                 free(inbuf);
271                 return 0;
272         }
273
274         ret = decompress(inbuf, outbuf, disk_size, ram_size);
275         free(inbuf);
276         if (ret) {
277                 free(outbuf);
278                 return ret;
279         }
280
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;
287                 }
288                 total += done;
289         }
290         free(outbuf);
291
292         return 0;
293 }
294
295 static int ask_to_continue(const char *file)
296 {
297         char buf[2];
298         char *ret;
299
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;
309         }
310
311         return 0;
312 }
313
314
315 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
316                      const char *file)
317 {
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;
328
329         path = btrfs_alloc_path();
330         if (!path) {
331                 fprintf(stderr, "Ran out of memory\n");
332                 return -1;
333         }
334         path->skip_locking = 1;
335
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);
341         }
342         btrfs_release_path(root, path);
343
344         key->offset = 0;
345         key->type = BTRFS_EXTENT_DATA_KEY;
346
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;
352         }
353
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;
366                 }
367                 leaf = path->nodes[0];
368         }
369
370         while (1) {
371                 if (loops++ >= 1024) {
372                         ret = ask_to_continue(file);
373                         if (ret)
374                                 break;
375                         loops = 0;
376                 }
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                                 }
389                                 leaf = path->nodes[0];
390                         } while (!leaf);
391                         continue;
392                 }
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;
407                 }
408
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;
416                         }
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;
423                         }
424                 } else {
425                         printf("Weird extent type %d\n", extent_type);
426                 }
427 next:
428                 path->slots[0]++;
429         }
430
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;
437         }
438         return 0;
439 }
440
441 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
442                       const char *dir)
443 {
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;
455
456         path = btrfs_alloc_path();
457         if (!path) {
458                 fprintf(stderr, "Ran out of memory\n");
459                 return -1;
460         }
461         path->skip_locking = 1;
462
463         key->offset = 0;
464         key->type = BTRFS_DIR_INDEX_KEY;
465
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;
471         }
472
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;
491                 }
492                 leaf = path->nodes[0];
493         }
494
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;
501                 }
502
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;
519                                 }
520                                 leaf = path->nodes[0];
521                         } while (!leaf);
522                         continue;
523                 }
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;
530                 }
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;
536                 }
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);
545
546                 snprintf(path_name, 4096, "%s/%s", dir, filename);
547
548
549                 /*
550                  * At this point we're only going to restore directories and
551                  * files, no symlinks or anything else.
552                  */
553                 if (type == BTRFS_FT_REG_FILE) {
554                         if (!overwrite) {
555                                 static int warn = 0;
556                                 struct stat st;
557
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;
570                                 }
571                                 ret = 0;
572                         }
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;
583                         }
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;
592                         }
593                 } else if (type == BTRFS_FT_DIR) {
594                         struct btrfs_root *search_root = root;
595                         char *dir = strdup(path_name);
596
597                         if (!dir) {
598                                 fprintf(stderr, "Ran out of memory\n");
599                                 btrfs_free_path(path);
600                                 return -1;
601                         }
602
603                         if (location.type == BTRFS_ROOT_ITEM_KEY) {
604                                 /*
605                                  * If we are a snapshot and this is the index
606                                  * object to ourselves just skip it.
607                                  */
608                                 if (location.objectid ==
609                                     root->root_key.objectid) {
610                                         free(dir);
611                                         goto next;
612                                 }
613
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                                         btrfs_free_path(path);
625                                         return PTR_ERR(search_root);
626                                 }
627
628                                 /*
629                                  * A subvolume will have a key.offset of 0, a
630                                  * snapshot will have key.offset of a transid.
631                                  */
632                                 if (search_root->root_key.offset != 0 &&
633                                     get_snaps == 0) {
634                                         free(dir);
635                                         printf("Skipping snapshot %s\n",
636                                                filename);
637                                         goto next;
638                                 }
639                                 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
640                         }
641
642                         if (verbose)
643                                 printf("Restoring %s\n", path_name);
644
645                         errno = 0;
646                         ret = mkdir(path_name, 0755);
647                         if (ret && errno != EEXIST) {
648                                 free(dir);
649                                 fprintf(stderr, "Error mkdiring %s: %d\n",
650                                         path_name, errno);
651                                 if (ignore_errors)
652                                         goto next;
653                                 btrfs_free_path(path);
654                                 return -1;
655                         }
656                         loops = 0;
657                         ret = search_dir(search_root, &location, dir);
658                         free(dir);
659                         if (ret) {
660                                 if (ignore_errors)
661                                         goto next;
662                                 btrfs_free_path(path);
663                                 return ret;
664                         }
665                 }
666 next:
667                 path->slots[0]++;
668         }
669
670         if (verbose)
671                 printf("Done searching %s\n", dir);
672         btrfs_free_path(path);
673         return 0;
674 }
675
676 static void usage()
677 {
678         fprintf(stderr, "Usage: restore [-svio] [-t disk offset] <device> "
679                 "<directory>\n");
680 }
681
682 static struct btrfs_root *open_fs(const char *dev, u64 root_location, int super_mirror)
683 {
684         struct btrfs_root *root;
685         u64 bytenr;
686         int i;
687
688         for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
689                 bytenr = btrfs_sb_offset(i);
690                 root = open_ctree_recovery(dev, bytenr, root_location);
691                 if (root)
692                         return root;
693                 fprintf(stderr, "Could not open root, trying backup super\n");
694         }
695
696         return NULL;
697 }
698
699 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
700 {
701         struct btrfs_path *path;
702         struct btrfs_key found_key;
703         struct btrfs_key key;
704         int ret = -1;
705         int i;
706
707         key.objectid = 0;
708         key.type = BTRFS_DIR_INDEX_KEY;
709         key.offset = 0;
710
711         path = btrfs_alloc_path();
712         if (!path) {
713                 fprintf(stderr, "Ran out of memory\n");
714                 return ret;
715         }
716
717         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
718         if (ret < 0) {
719                 fprintf(stderr, "Error searching %d\n", ret);
720                 goto out;
721         }
722
723         if (!path->nodes[0]) {
724                 fprintf(stderr, "No leaf!\n");
725                 goto out;
726         }
727 again:
728         for (i = path->slots[0];
729              i < btrfs_header_nritems(path->nodes[0]); i++) {
730                 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
731                 if (found_key.type != key.type)
732                         continue;
733
734                 printf("Using objectid %Lu for first dir\n",
735                        found_key.objectid);
736                 *objectid = found_key.objectid;
737                 ret = 0;
738                 goto out;
739         }
740         do {
741                 ret = next_leaf(root, path);
742                 if (ret < 0) {
743                         fprintf(stderr, "Error getting next leaf %d\n",
744                                 ret);
745                         goto out;
746                 } else if (ret > 0) {
747                         fprintf(stderr, "No more leaves\n");
748                         goto out;
749                 }
750         } while (!path->nodes[0]);
751         if (path->nodes[0])
752                 goto again;
753         printf("Couldn't find a dir index item\n");
754 out:
755         btrfs_free_path(path);
756         return ret;
757 }
758
759 int main(int argc, char **argv)
760 {
761         struct btrfs_root *root;
762         struct btrfs_key key;
763         char dir_name[128];
764         u64 tree_location = 0;
765         u64 fs_location = 0;
766         int len;
767         int ret;
768         int opt;
769         int super_mirror = 0;
770         int find_dir = 0;
771
772         while ((opt = getopt(argc, argv, "sviot:u:df:")) != -1) {
773                 switch (opt) {
774                         case 's':
775                                 get_snaps = 1;
776                                 break;
777                         case 'v':
778                                 verbose++;
779                                 break;
780                         case 'i':
781                                 ignore_errors = 1;
782                                 break;
783                         case 'o':
784                                 overwrite = 1;
785                                 break;
786                         case 't':
787                                 errno = 0;
788                                 tree_location = (u64)strtoll(optarg, NULL, 10);
789                                 if (errno != 0) {
790                                         fprintf(stderr, "Tree location not valid\n");
791                                         exit(1);
792                                 }
793                                 break;
794                         case 'f':
795                                 errno = 0;
796                                 fs_location = (u64)strtoll(optarg, NULL, 10);
797                                 if (errno != 0) {
798                                         fprintf(stderr, "Fs location not valid\n");
799                                         exit(1);
800                                 }
801                                 break;
802                         case 'u':
803                                 errno = 0;
804                                 super_mirror = (int)strtol(optarg, NULL, 10);
805                                 if (errno != 0 ||
806                                     super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
807                                         fprintf(stderr, "Super mirror not "
808                                                 "valid\n");
809                                         exit(1);
810                                 }
811                                 break;
812                         case 'd':
813                                 find_dir = 1;
814                                 break;
815                         default:
816                                 usage();
817                                 exit(1);
818                 }
819         }
820
821         if (optind + 1 >= argc) {
822                 usage();
823                 exit(1);
824         }
825
826         if ((ret = check_mounted(argv[optind])) < 0) {
827                 fprintf(stderr, "Could not check mount status: %s\n",
828                         strerror(ret));
829                 return ret;
830         } else if (ret) {
831                 fprintf(stderr, "%s is currently mounted.  Aborting.\n", argv[optind]);
832                 return 1;
833         }
834
835         root = open_fs(argv[optind], tree_location, super_mirror);
836         if (root == NULL)
837                 return 1;
838
839         if (fs_location != 0) {
840                 free_extent_buffer(root->node);
841                 root->node = read_tree_block(root, fs_location, 4096, 0);
842                 if (!root->node) {
843                         fprintf(stderr, "Failed to read fs location\n");
844                         goto out;
845                 }
846         }
847
848         printf("Root objectid is %Lu\n", root->objectid);
849
850         memset(path_name, 0, 4096);
851
852         strncpy(dir_name, argv[optind + 1], sizeof dir_name);
853         dir_name[sizeof dir_name - 1] = 0;
854
855         /* Strip the trailing / on the dir name */
856         len = strlen(dir_name);
857         while (len && dir_name[--len] == '/') {
858                 dir_name[len] = '\0';
859         }
860
861         if (find_dir) {
862                 ret = find_first_dir(root, &key.objectid);
863                 if (ret)
864                         goto out;
865         } else {
866                 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
867         }
868
869         ret = search_dir(root->fs_info->fs_root, &key, dir_name);
870
871 out:
872         close_ctree(root);
873         return ret;
874 }