Merge branch 'qu/find-root-v3-part1' into v3.19.x
[platform/upstream/btrfs-progs.git] / disk-io.c
1 /*
2  * Copyright (C) 2007 Oracle.  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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <uuid/uuid.h>
26 #include "kerncompat.h"
27 #include "radix-tree.h"
28 #include "ctree.h"
29 #include "disk-io.h"
30 #include "volumes.h"
31 #include "transaction.h"
32 #include "crc32c.h"
33 #include "utils.h"
34 #include "print-tree.h"
35 #include "rbtree-utils.h"
36
37 /* specified errno for check_tree_block */
38 #define BTRFS_BAD_BYTENR                (-1)
39 #define BTRFS_BAD_FSID                  (-2)
40
41 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
42 {
43
44         struct btrfs_fs_devices *fs_devices;
45         int ret = BTRFS_BAD_FSID;
46
47         if (buf->start != btrfs_header_bytenr(buf))
48                 return BTRFS_BAD_BYTENR;
49
50         fs_devices = root->fs_info->fs_devices;
51         while (fs_devices) {
52                 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
53                                           btrfs_header_fsid(),
54                                           BTRFS_FSID_SIZE)) {
55                         ret = 0;
56                         break;
57                 }
58                 fs_devices = fs_devices->seed;
59         }
60         return ret;
61 }
62
63 static void print_tree_block_error(struct btrfs_root *root,
64                                 struct extent_buffer *eb,
65                                 int err)
66 {
67         char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
68         char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
69         u8 buf[BTRFS_UUID_SIZE];
70
71         switch (err) {
72         case BTRFS_BAD_FSID:
73                 read_extent_buffer(eb, buf, btrfs_header_fsid(),
74                                    BTRFS_UUID_SIZE);
75                 uuid_unparse(buf, found_uuid);
76                 uuid_unparse(root->fs_info->fsid, fs_uuid);
77                 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
78                         fs_uuid, found_uuid);
79                 break;
80         case BTRFS_BAD_BYTENR:
81                 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
82                         eb->start, btrfs_header_bytenr(eb));
83                 break;
84         }
85 }
86
87 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
88 {
89         return crc32c(seed, data, len);
90 }
91
92 void btrfs_csum_final(u32 crc, char *result)
93 {
94         *(__le32 *)result = ~cpu_to_le32(crc);
95 }
96
97 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
98                                   int verify, int silent)
99 {
100         char *result;
101         u32 len;
102         u32 crc = ~(u32)0;
103
104         result = malloc(csum_size * sizeof(char));
105         if (!result)
106                 return 1;
107
108         len = buf->len - BTRFS_CSUM_SIZE;
109         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
110         btrfs_csum_final(crc, result);
111
112         if (verify) {
113                 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
114                         if (!silent)
115                                 printk("checksum verify failed on %llu found %08X wanted %08X\n",
116                                        (unsigned long long)buf->start,
117                                        *((u32 *)result),
118                                        *((u32*)(char *)buf->data));
119                         free(result);
120                         return 1;
121                 }
122         } else {
123                 write_extent_buffer(buf, result, 0, csum_size);
124         }
125         free(result);
126         return 0;
127 }
128
129 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
130 {
131         return __csum_tree_block_size(buf, csum_size, verify, 0);
132 }
133
134 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
135 {
136         return __csum_tree_block_size(buf, csum_size, 1, 1);
137 }
138
139 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
140                            int verify)
141 {
142         u16 csum_size =
143                 btrfs_super_csum_size(root->fs_info->super_copy);
144         if (verify && root->fs_info->suppress_check_block_errors)
145                 return verify_tree_block_csum_silent(buf, csum_size);
146         return csum_tree_block_size(buf, csum_size, verify);
147 }
148
149 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
150                                             u64 bytenr, u32 blocksize)
151 {
152         return find_extent_buffer(&root->fs_info->extent_cache,
153                                   bytenr, blocksize);
154 }
155
156 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
157                                                  u64 bytenr, u32 blocksize)
158 {
159         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
160                                    blocksize);
161 }
162
163 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
164                           u64 parent_transid)
165 {
166         struct extent_buffer *eb;
167         u64 length;
168         struct btrfs_multi_bio *multi = NULL;
169         struct btrfs_device *device;
170
171         eb = btrfs_find_tree_block(root, bytenr, blocksize);
172         if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
173             !btrfs_map_block(&root->fs_info->mapping_tree, READ,
174                              bytenr, &length, &multi, 0, NULL)) {
175                 device = multi->stripes[0].dev;
176                 device->total_ios++;
177                 blocksize = min(blocksize, (u32)(64 * 1024));
178                 readahead(device->fd, multi->stripes[0].physical, blocksize);
179         }
180
181         free_extent_buffer(eb);
182         kfree(multi);
183 }
184
185 static int verify_parent_transid(struct extent_io_tree *io_tree,
186                                  struct extent_buffer *eb, u64 parent_transid,
187                                  int ignore)
188 {
189         int ret;
190
191         if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
192                 return 0;
193
194         if (extent_buffer_uptodate(eb) &&
195             btrfs_header_generation(eb) == parent_transid) {
196                 ret = 0;
197                 goto out;
198         }
199         printk("parent transid verify failed on %llu wanted %llu found %llu\n",
200                (unsigned long long)eb->start,
201                (unsigned long long)parent_transid,
202                (unsigned long long)btrfs_header_generation(eb));
203         if (ignore) {
204                 eb->flags |= EXTENT_BAD_TRANSID;
205                 printk("Ignoring transid failure\n");
206                 return 0;
207         }
208
209         ret = 1;
210 out:
211         clear_extent_buffer_uptodate(io_tree, eb);
212         return ret;
213
214 }
215
216
217 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
218 {
219         unsigned long offset = 0;
220         struct btrfs_multi_bio *multi = NULL;
221         struct btrfs_device *device;
222         int ret = 0;
223         u64 read_len;
224         unsigned long bytes_left = eb->len;
225
226         while (bytes_left) {
227                 read_len = bytes_left;
228                 device = NULL;
229
230                 if (!info->on_restoring &&
231                     eb->start != BTRFS_SUPER_INFO_OFFSET) {
232                         ret = btrfs_map_block(&info->mapping_tree, READ,
233                                               eb->start + offset, &read_len, &multi,
234                                               mirror, NULL);
235                         if (ret) {
236                                 printk("Couldn't map the block %Lu\n", eb->start + offset);
237                                 kfree(multi);
238                                 return -EIO;
239                         }
240                         device = multi->stripes[0].dev;
241
242                         if (device->fd == 0) {
243                                 kfree(multi);
244                                 return -EIO;
245                         }
246
247                         eb->fd = device->fd;
248                         device->total_ios++;
249                         eb->dev_bytenr = multi->stripes[0].physical;
250                         kfree(multi);
251                         multi = NULL;
252                 } else {
253                         /* special case for restore metadump */
254                         list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
255                                 if (device->devid == 1)
256                                         break;
257                         }
258
259                         eb->fd = device->fd;
260                         eb->dev_bytenr = eb->start;
261                         device->total_ios++;
262                 }
263
264                 if (read_len > bytes_left)
265                         read_len = bytes_left;
266
267                 ret = read_extent_from_disk(eb, offset, read_len);
268                 if (ret)
269                         return -EIO;
270                 offset += read_len;
271                 bytes_left -= read_len;
272         }
273         return 0;
274 }
275
276 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
277                                      u32 blocksize, u64 parent_transid)
278 {
279         int ret;
280         struct extent_buffer *eb;
281         u64 best_transid = 0;
282         int mirror_num = 0;
283         int good_mirror = 0;
284         int num_copies;
285         int ignore = 0;
286
287         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
288         if (!eb)
289                 return ERR_PTR(-ENOMEM);
290
291         if (btrfs_buffer_uptodate(eb, parent_transid))
292                 return eb;
293
294         while (1) {
295                 ret = read_whole_eb(root->fs_info, eb, mirror_num);
296                 if (ret == 0 && csum_tree_block(root, eb, 1) == 0 &&
297                     check_tree_block(root, eb) == 0 &&
298                     verify_parent_transid(eb->tree, eb, parent_transid, ignore)
299                     == 0) {
300                         if (eb->flags & EXTENT_BAD_TRANSID &&
301                             list_empty(&eb->recow)) {
302                                 list_add_tail(&eb->recow,
303                                               &root->fs_info->recow_ebs);
304                                 eb->refs++;
305                         }
306                         btrfs_set_buffer_uptodate(eb);
307                         return eb;
308                 }
309                 if (ignore) {
310                         if (check_tree_block(root, eb)) {
311                                 if (!root->fs_info->suppress_check_block_errors)
312                                         print_tree_block_error(root, eb,
313                                                 check_tree_block(root, eb));
314                         } else {
315                                 if (!root->fs_info->suppress_check_block_errors)
316                                         fprintf(stderr, "Csum didn't match\n");
317                         }
318                         ret = -EIO;
319                         break;
320                 }
321                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
322                                               eb->start, eb->len);
323                 if (num_copies == 1) {
324                         ignore = 1;
325                         continue;
326                 }
327                 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
328                         best_transid = btrfs_header_generation(eb);
329                         good_mirror = mirror_num;
330                 }
331                 mirror_num++;
332                 if (mirror_num > num_copies) {
333                         mirror_num = good_mirror;
334                         ignore = 1;
335                         continue;
336                 }
337         }
338         free_extent_buffer(eb);
339         return ERR_PTR(ret);
340 }
341
342 int write_and_map_eb(struct btrfs_trans_handle *trans,
343                      struct btrfs_root *root,
344                      struct extent_buffer *eb)
345 {
346         int ret;
347         int dev_nr;
348         u64 length;
349         u64 *raid_map = NULL;
350         struct btrfs_multi_bio *multi = NULL;
351
352         dev_nr = 0;
353         length = eb->len;
354         ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
355                               eb->start, &length, &multi, 0, &raid_map);
356
357         if (raid_map) {
358                 ret = write_raid56_with_parity(root->fs_info, eb, multi,
359                                                length, raid_map);
360                 BUG_ON(ret);
361         } else while (dev_nr < multi->num_stripes) {
362                 BUG_ON(ret);
363                 eb->fd = multi->stripes[dev_nr].dev->fd;
364                 eb->dev_bytenr = multi->stripes[dev_nr].physical;
365                 multi->stripes[dev_nr].dev->total_ios++;
366                 dev_nr++;
367                 ret = write_extent_to_disk(eb);
368                 BUG_ON(ret);
369         }
370         kfree(multi);
371         return 0;
372 }
373
374 static int write_tree_block(struct btrfs_trans_handle *trans,
375                      struct btrfs_root *root,
376                      struct extent_buffer *eb)
377 {
378         if (check_tree_block(root, eb)) {
379                 print_tree_block_error(root, eb, check_tree_block(root, eb));
380                 BUG();
381         }
382
383         if (!btrfs_buffer_uptodate(eb, trans->transid))
384                 BUG();
385
386         btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
387         csum_tree_block(root, eb, 0);
388
389         return write_and_map_eb(trans, root, eb);
390 }
391
392 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
393                         u32 stripesize, struct btrfs_root *root,
394                         struct btrfs_fs_info *fs_info, u64 objectid)
395 {
396         root->node = NULL;
397         root->commit_root = NULL;
398         root->sectorsize = sectorsize;
399         root->nodesize = nodesize;
400         root->leafsize = leafsize;
401         root->stripesize = stripesize;
402         root->ref_cows = 0;
403         root->track_dirty = 0;
404
405         root->fs_info = fs_info;
406         root->objectid = objectid;
407         root->last_trans = 0;
408         root->highest_inode = 0;
409         root->last_inode_alloc = 0;
410
411         INIT_LIST_HEAD(&root->dirty_list);
412         INIT_LIST_HEAD(&root->orphan_data_extents);
413         memset(&root->root_key, 0, sizeof(root->root_key));
414         memset(&root->root_item, 0, sizeof(root->root_item));
415         root->root_key.objectid = objectid;
416         return 0;
417 }
418
419 static int update_cowonly_root(struct btrfs_trans_handle *trans,
420                                struct btrfs_root *root)
421 {
422         int ret;
423         u64 old_root_bytenr;
424         struct btrfs_root *tree_root = root->fs_info->tree_root;
425
426         btrfs_write_dirty_block_groups(trans, root);
427         while(1) {
428                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
429                 if (old_root_bytenr == root->node->start)
430                         break;
431                 btrfs_set_root_bytenr(&root->root_item,
432                                        root->node->start);
433                 btrfs_set_root_generation(&root->root_item,
434                                           trans->transid);
435                 root->root_item.level = btrfs_header_level(root->node);
436                 ret = btrfs_update_root(trans, tree_root,
437                                         &root->root_key,
438                                         &root->root_item);
439                 BUG_ON(ret);
440                 btrfs_write_dirty_block_groups(trans, root);
441         }
442         return 0;
443 }
444
445 static int commit_tree_roots(struct btrfs_trans_handle *trans,
446                              struct btrfs_fs_info *fs_info)
447 {
448         struct btrfs_root *root;
449         struct list_head *next;
450         struct extent_buffer *eb;
451         int ret;
452
453         if (fs_info->readonly)
454                 return 0;
455
456         eb = fs_info->tree_root->node;
457         extent_buffer_get(eb);
458         ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
459         free_extent_buffer(eb);
460         if (ret)
461                 return ret;
462
463         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
464                 next = fs_info->dirty_cowonly_roots.next;
465                 list_del_init(next);
466                 root = list_entry(next, struct btrfs_root, dirty_list);
467                 update_cowonly_root(trans, root);
468                 free_extent_buffer(root->commit_root);
469                 root->commit_root = NULL;
470         }
471
472         return 0;
473 }
474
475 static int __commit_transaction(struct btrfs_trans_handle *trans,
476                                 struct btrfs_root *root)
477 {
478         u64 start;
479         u64 end;
480         struct extent_buffer *eb;
481         struct extent_io_tree *tree = &root->fs_info->extent_cache;
482         int ret;
483
484         while(1) {
485                 ret = find_first_extent_bit(tree, 0, &start, &end,
486                                             EXTENT_DIRTY);
487                 if (ret)
488                         break;
489                 while(start <= end) {
490                         eb = find_first_extent_buffer(tree, start);
491                         BUG_ON(!eb || eb->start != start);
492                         ret = write_tree_block(trans, root, eb);
493                         BUG_ON(ret);
494                         start += eb->len;
495                         clear_extent_buffer_dirty(eb);
496                         free_extent_buffer(eb);
497                 }
498         }
499         return 0;
500 }
501
502 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
503                              struct btrfs_root *root)
504 {
505         u64 transid = trans->transid;
506         int ret = 0;
507         struct btrfs_fs_info *fs_info = root->fs_info;
508
509         if (root->commit_root == root->node)
510                 goto commit_tree;
511         if (root == root->fs_info->tree_root)
512                 goto commit_tree;
513
514         free_extent_buffer(root->commit_root);
515         root->commit_root = NULL;
516
517         btrfs_set_root_bytenr(&root->root_item, root->node->start);
518         btrfs_set_root_generation(&root->root_item, trans->transid);
519         root->root_item.level = btrfs_header_level(root->node);
520         ret = btrfs_update_root(trans, root->fs_info->tree_root,
521                                 &root->root_key, &root->root_item);
522         BUG_ON(ret);
523 commit_tree:
524         ret = commit_tree_roots(trans, fs_info);
525         BUG_ON(ret);
526         ret = __commit_transaction(trans, root);
527         BUG_ON(ret);
528         write_ctree_super(trans, root);
529         btrfs_finish_extent_commit(trans, fs_info->extent_root,
530                                    &fs_info->pinned_extents);
531         btrfs_free_transaction(root, trans);
532         free_extent_buffer(root->commit_root);
533         root->commit_root = NULL;
534         fs_info->running_transaction = NULL;
535         fs_info->last_trans_committed = transid;
536         return 0;
537 }
538
539 static int find_and_setup_root(struct btrfs_root *tree_root,
540                                struct btrfs_fs_info *fs_info,
541                                u64 objectid, struct btrfs_root *root)
542 {
543         int ret;
544         u32 blocksize;
545         u64 generation;
546
547         __setup_root(tree_root->nodesize, tree_root->leafsize,
548                      tree_root->sectorsize, tree_root->stripesize,
549                      root, fs_info, objectid);
550         ret = btrfs_find_last_root(tree_root, objectid,
551                                    &root->root_item, &root->root_key);
552         if (ret)
553                 return ret;
554
555         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
556         generation = btrfs_root_generation(&root->root_item);
557         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
558                                      blocksize, generation);
559         if (!extent_buffer_uptodate(root->node))
560                 return -EIO;
561
562         return 0;
563 }
564
565 static int find_and_setup_log_root(struct btrfs_root *tree_root,
566                                struct btrfs_fs_info *fs_info,
567                                struct btrfs_super_block *disk_super)
568 {
569         u32 blocksize;
570         u64 blocknr = btrfs_super_log_root(disk_super);
571         struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
572
573         if (!log_root)
574                 return -ENOMEM;
575
576         if (blocknr == 0) {
577                 free(log_root);
578                 return 0;
579         }
580
581         blocksize = btrfs_level_size(tree_root,
582                              btrfs_super_log_root_level(disk_super));
583
584         __setup_root(tree_root->nodesize, tree_root->leafsize,
585                      tree_root->sectorsize, tree_root->stripesize,
586                      log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
587
588         log_root->node = read_tree_block(tree_root, blocknr,
589                                      blocksize,
590                                      btrfs_super_generation(disk_super) + 1);
591
592         fs_info->log_root_tree = log_root;
593
594         if (!extent_buffer_uptodate(log_root->node)) {
595                 free_extent_buffer(log_root->node);
596                 free(log_root);
597                 fs_info->log_root_tree = NULL;
598                 return -EIO;
599         }
600
601         return 0;
602 }
603
604 int btrfs_free_fs_root(struct btrfs_root *root)
605 {
606         if (root->node)
607                 free_extent_buffer(root->node);
608         if (root->commit_root)
609                 free_extent_buffer(root->commit_root);
610         kfree(root);
611         return 0;
612 }
613
614 static void __free_fs_root(struct rb_node *node)
615 {
616         struct btrfs_root *root;
617
618         root = container_of(node, struct btrfs_root, rb_node);
619         btrfs_free_fs_root(root);
620 }
621
622 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
623
624 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
625                                                struct btrfs_key *location)
626 {
627         struct btrfs_root *root;
628         struct btrfs_root *tree_root = fs_info->tree_root;
629         struct btrfs_path *path;
630         struct extent_buffer *l;
631         u64 generation;
632         u32 blocksize;
633         int ret = 0;
634
635         root = malloc(sizeof(*root));
636         if (!root)
637                 return ERR_PTR(-ENOMEM);
638         memset(root, 0, sizeof(*root));
639         if (location->offset == (u64)-1) {
640                 ret = find_and_setup_root(tree_root, fs_info,
641                                           location->objectid, root);
642                 if (ret) {
643                         free(root);
644                         return ERR_PTR(ret);
645                 }
646                 goto insert;
647         }
648
649         __setup_root(tree_root->nodesize, tree_root->leafsize,
650                      tree_root->sectorsize, tree_root->stripesize,
651                      root, fs_info, location->objectid);
652
653         path = btrfs_alloc_path();
654         BUG_ON(!path);
655         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
656         if (ret != 0) {
657                 if (ret > 0)
658                         ret = -ENOENT;
659                 goto out;
660         }
661         l = path->nodes[0];
662         read_extent_buffer(l, &root->root_item,
663                btrfs_item_ptr_offset(l, path->slots[0]),
664                sizeof(root->root_item));
665         memcpy(&root->root_key, location, sizeof(*location));
666         ret = 0;
667 out:
668         btrfs_free_path(path);
669         if (ret) {
670                 free(root);
671                 return ERR_PTR(ret);
672         }
673         generation = btrfs_root_generation(&root->root_item);
674         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
675         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
676                                      blocksize, generation);
677         if (!extent_buffer_uptodate(root->node)) {
678                 free(root);
679                 return ERR_PTR(-EIO);
680         }
681 insert:
682         root->ref_cows = 1;
683         return root;
684 }
685
686 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
687                                             void *data)
688 {
689         u64 objectid = *((u64 *)data);
690         struct btrfs_root *root;
691
692         root = rb_entry(node, struct btrfs_root, rb_node);
693         if (objectid > root->objectid)
694                 return 1;
695         else if (objectid < root->objectid)
696                 return -1;
697         else
698                 return 0;
699 }
700
701 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
702                                         struct rb_node *node2)
703 {
704         struct btrfs_root *root;
705
706         root = rb_entry(node2, struct btrfs_root, rb_node);
707         return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
708 }
709
710 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
711                                       struct btrfs_key *location)
712 {
713         struct btrfs_root *root;
714         struct rb_node *node;
715         int ret;
716         u64 objectid = location->objectid;
717
718         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
719                 return fs_info->tree_root;
720         if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
721                 return fs_info->extent_root;
722         if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
723                 return fs_info->chunk_root;
724         if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
725                 return fs_info->dev_root;
726         if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
727                 return fs_info->csum_root;
728         if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
729                 return fs_info->quota_root;
730
731         BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
732                location->offset != (u64)-1);
733
734         node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
735                          btrfs_fs_roots_compare_objectids, NULL);
736         if (node)
737                 return container_of(node, struct btrfs_root, rb_node);
738
739         root = btrfs_read_fs_root_no_cache(fs_info, location);
740         if (IS_ERR(root))
741                 return root;
742
743         ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
744                         btrfs_fs_roots_compare_roots);
745         BUG_ON(ret);
746         return root;
747 }
748
749 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
750 {
751         free(fs_info->tree_root);
752         free(fs_info->extent_root);
753         free(fs_info->chunk_root);
754         free(fs_info->dev_root);
755         free(fs_info->csum_root);
756         free(fs_info->quota_root);
757         free(fs_info->super_copy);
758         free(fs_info->log_root_tree);
759         free(fs_info);
760 }
761
762 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
763 {
764         struct btrfs_fs_info *fs_info;
765
766         fs_info = malloc(sizeof(struct btrfs_fs_info));
767         if (!fs_info)
768                 return NULL;
769
770         memset(fs_info, 0, sizeof(struct btrfs_fs_info));
771
772         fs_info->tree_root = malloc(sizeof(struct btrfs_root));
773         fs_info->extent_root = malloc(sizeof(struct btrfs_root));
774         fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
775         fs_info->dev_root = malloc(sizeof(struct btrfs_root));
776         fs_info->csum_root = malloc(sizeof(struct btrfs_root));
777         fs_info->quota_root = malloc(sizeof(struct btrfs_root));
778         fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
779
780         if (!fs_info->tree_root || !fs_info->extent_root ||
781             !fs_info->chunk_root || !fs_info->dev_root ||
782             !fs_info->csum_root || !fs_info->quota_root ||
783             !fs_info->super_copy)
784                 goto free_all;
785
786         memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
787         memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
788         memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
789         memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
790         memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
791         memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
792         memset(fs_info->quota_root, 0, sizeof(struct btrfs_root));
793
794         extent_io_tree_init(&fs_info->extent_cache);
795         extent_io_tree_init(&fs_info->free_space_cache);
796         extent_io_tree_init(&fs_info->block_group_cache);
797         extent_io_tree_init(&fs_info->pinned_extents);
798         extent_io_tree_init(&fs_info->pending_del);
799         extent_io_tree_init(&fs_info->extent_ins);
800         fs_info->excluded_extents = NULL;
801
802         fs_info->fs_root_tree = RB_ROOT;
803         cache_tree_init(&fs_info->mapping_tree.cache_tree);
804
805         mutex_init(&fs_info->fs_mutex);
806         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
807         INIT_LIST_HEAD(&fs_info->space_info);
808         INIT_LIST_HEAD(&fs_info->recow_ebs);
809
810         if (!writable)
811                 fs_info->readonly = 1;
812
813         fs_info->super_bytenr = sb_bytenr;
814         fs_info->data_alloc_profile = (u64)-1;
815         fs_info->metadata_alloc_profile = (u64)-1;
816         fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
817         return fs_info;
818 free_all:
819         btrfs_free_fs_info(fs_info);
820         return NULL;
821 }
822
823 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
824 {
825         u64 features;
826
827         features = btrfs_super_incompat_flags(sb) &
828                    ~BTRFS_FEATURE_INCOMPAT_SUPP;
829         if (features) {
830                 printk("couldn't open because of unsupported "
831                        "option features (%Lx).\n",
832                        (unsigned long long)features);
833                 return -ENOTSUP;
834         }
835
836         features = btrfs_super_incompat_flags(sb);
837         if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
838                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
839                 btrfs_set_super_incompat_flags(sb, features);
840         }
841
842         features = btrfs_super_compat_ro_flags(sb) &
843                 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
844         if (writable && features) {
845                 printk("couldn't open RDWR because of unsupported "
846                        "option features (%Lx).\n",
847                        (unsigned long long)features);
848                 return -ENOTSUP;
849         }
850         return 0;
851 }
852
853 static int find_best_backup_root(struct btrfs_super_block *super)
854 {
855         struct btrfs_root_backup *backup;
856         u64 orig_gen = btrfs_super_generation(super);
857         u64 gen = 0;
858         int best_index = 0;
859         int i;
860
861         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
862                 backup = super->super_roots + i;
863                 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
864                     btrfs_backup_tree_root_gen(backup) > gen) {
865                         best_index = i;
866                         gen = btrfs_backup_tree_root_gen(backup);
867                 }
868         }
869         return best_index;
870 }
871
872 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
873                                       enum btrfs_open_ctree_flags flags,
874                                       struct btrfs_root *info_root,
875                                       u64 objectid, char *str)
876 {
877         struct btrfs_super_block *sb = fs_info->super_copy;
878         struct btrfs_root *root = fs_info->tree_root;
879         u32 leafsize = btrfs_super_leafsize(sb);
880         int ret;
881
882         ret = find_and_setup_root(root, fs_info, objectid, info_root);
883         if (ret) {
884                 printk("Couldn't setup %s tree\n", str);
885                 if (!(flags & OPEN_CTREE_PARTIAL))
886                         return -EIO;
887                 /*
888                  * Need a blank node here just so we don't screw up in the
889                  * million of places that assume a root has a valid ->node
890                  */
891                 info_root->node =
892                         btrfs_find_create_tree_block(info_root, 0, leafsize);
893                 if (!info_root->node)
894                         return -ENOMEM;
895                 clear_extent_buffer_uptodate(NULL, info_root->node);
896         }
897
898         return 0;
899 }
900
901 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
902                           enum btrfs_open_ctree_flags flags)
903 {
904         struct btrfs_super_block *sb = fs_info->super_copy;
905         struct btrfs_root *root;
906         struct btrfs_key key;
907         u32 sectorsize;
908         u32 nodesize;
909         u32 leafsize;
910         u32 stripesize;
911         u64 generation;
912         u32 blocksize;
913         int ret;
914
915         nodesize = btrfs_super_nodesize(sb);
916         leafsize = btrfs_super_leafsize(sb);
917         sectorsize = btrfs_super_sectorsize(sb);
918         stripesize = btrfs_super_stripesize(sb);
919
920         root = fs_info->tree_root;
921         __setup_root(nodesize, leafsize, sectorsize, stripesize,
922                      root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
923         blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
924         generation = btrfs_super_generation(sb);
925
926         if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
927                 root_tree_bytenr = btrfs_super_root(sb);
928         } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
929                 struct btrfs_root_backup *backup;
930                 int index = find_best_backup_root(sb);
931                 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
932                         fprintf(stderr, "Invalid backup root number\n");
933                         return -EIO;
934                 }
935                 backup = fs_info->super_copy->super_roots + index;
936                 root_tree_bytenr = btrfs_backup_tree_root(backup);
937                 generation = btrfs_backup_tree_root_gen(backup);
938         }
939
940         root->node = read_tree_block(root, root_tree_bytenr, blocksize,
941                                      generation);
942         if (!extent_buffer_uptodate(root->node)) {
943                 fprintf(stderr, "Couldn't read tree root\n");
944                 return -EIO;
945         }
946
947         ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
948                                          BTRFS_EXTENT_TREE_OBJECTID, "extent");
949         if (ret)
950                 return ret;
951         fs_info->extent_root->track_dirty = 1;
952
953         ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
954                                   fs_info->dev_root);
955         if (ret) {
956                 printk("Couldn't setup device tree\n");
957                 return -EIO;
958         }
959         fs_info->dev_root->track_dirty = 1;
960
961         ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
962                                          BTRFS_CSUM_TREE_OBJECTID, "csum");
963         if (ret)
964                 return ret;
965         fs_info->csum_root->track_dirty = 1;
966
967         ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
968                                   fs_info->quota_root);
969         if (ret == 0)
970                 fs_info->quota_enabled = 1;
971
972         ret = find_and_setup_log_root(root, fs_info, sb);
973         if (ret) {
974                 printk("Couldn't setup log root tree\n");
975                 if (!(flags & OPEN_CTREE_PARTIAL))
976                         return -EIO;
977         }
978
979         fs_info->generation = generation;
980         fs_info->last_trans_committed = generation;
981         if (extent_buffer_uptodate(fs_info->extent_root->node) &&
982             !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
983                 btrfs_read_block_groups(fs_info->tree_root);
984
985         key.objectid = BTRFS_FS_TREE_OBJECTID;
986         key.type = BTRFS_ROOT_ITEM_KEY;
987         key.offset = (u64)-1;
988         fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
989
990         if (IS_ERR(fs_info->fs_root))
991                 return -EIO;
992         return 0;
993 }
994
995 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
996 {
997         if (fs_info->quota_root)
998                 free_extent_buffer(fs_info->quota_root->node);
999         if (fs_info->csum_root)
1000                 free_extent_buffer(fs_info->csum_root->node);
1001         if (fs_info->dev_root)
1002                 free_extent_buffer(fs_info->dev_root->node);
1003         if (fs_info->extent_root)
1004                 free_extent_buffer(fs_info->extent_root->node);
1005         if (fs_info->tree_root)
1006                 free_extent_buffer(fs_info->tree_root->node);
1007         if (fs_info->log_root_tree)
1008                 free_extent_buffer(fs_info->log_root_tree->node);
1009         if (fs_info->chunk_root)
1010                 free_extent_buffer(fs_info->chunk_root->node);
1011 }
1012
1013 static void free_map_lookup(struct cache_extent *ce)
1014 {
1015         struct map_lookup *map;
1016
1017         map = container_of(ce, struct map_lookup, ce);
1018         kfree(map);
1019 }
1020
1021 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
1022
1023 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
1024 {
1025         while (!list_empty(&fs_info->recow_ebs)) {
1026                 struct extent_buffer *eb;
1027                 eb = list_first_entry(&fs_info->recow_ebs,
1028                                       struct extent_buffer, recow);
1029                 list_del_init(&eb->recow);
1030                 free_extent_buffer(eb);
1031         }
1032         free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
1033         extent_io_tree_cleanup(&fs_info->extent_cache);
1034         extent_io_tree_cleanup(&fs_info->free_space_cache);
1035         extent_io_tree_cleanup(&fs_info->block_group_cache);
1036         extent_io_tree_cleanup(&fs_info->pinned_extents);
1037         extent_io_tree_cleanup(&fs_info->pending_del);
1038         extent_io_tree_cleanup(&fs_info->extent_ins);
1039 }
1040
1041 int btrfs_scan_fs_devices(int fd, const char *path,
1042                           struct btrfs_fs_devices **fs_devices,
1043                           u64 sb_bytenr, int super_recover,
1044                           int skip_devices)
1045 {
1046         u64 total_devs;
1047         u64 dev_size;
1048         off_t seek_ret;
1049         int ret;
1050         if (!sb_bytenr)
1051                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1052
1053         seek_ret = lseek(fd, 0, SEEK_END);
1054         if (seek_ret < 0)
1055                 return -errno;
1056
1057         dev_size = seek_ret;
1058         lseek(fd, 0, SEEK_SET);
1059         if (sb_bytenr > dev_size) {
1060                 fprintf(stderr, "Superblock bytenr is larger than device size\n");
1061                 return -EINVAL;
1062         }
1063
1064         ret = btrfs_scan_one_device(fd, path, fs_devices,
1065                                     &total_devs, sb_bytenr, super_recover);
1066         if (ret) {
1067                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1068                 return ret;
1069         }
1070
1071         if (!skip_devices && total_devs != 1) {
1072                 ret = btrfs_scan_lblkid();
1073                 if (ret)
1074                         return ret;
1075         }
1076         return 0;
1077 }
1078
1079 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
1080 {
1081         struct btrfs_super_block *sb = fs_info->super_copy;
1082         u32 sectorsize;
1083         u32 nodesize;
1084         u32 leafsize;
1085         u32 blocksize;
1086         u32 stripesize;
1087         u64 generation;
1088         int ret;
1089
1090         nodesize = btrfs_super_nodesize(sb);
1091         leafsize = btrfs_super_leafsize(sb);
1092         sectorsize = btrfs_super_sectorsize(sb);
1093         stripesize = btrfs_super_stripesize(sb);
1094
1095         __setup_root(nodesize, leafsize, sectorsize, stripesize,
1096                      fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1097
1098         ret = btrfs_read_sys_array(fs_info->chunk_root);
1099         if (ret)
1100                 return ret;
1101
1102         blocksize = btrfs_level_size(fs_info->chunk_root,
1103                                      btrfs_super_chunk_root_level(sb));
1104         generation = btrfs_super_chunk_root_generation(sb);
1105
1106         fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1107                                                     btrfs_super_chunk_root(sb),
1108                                                     blocksize, generation);
1109         if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1110                 fprintf(stderr, "Couldn't read chunk root\n");
1111                 return -EIO;
1112         }
1113
1114         if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1115                 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1116                 if (ret) {
1117                         fprintf(stderr, "Couldn't read chunk tree\n");
1118                         return ret;
1119                 }
1120         }
1121         return 0;
1122 }
1123
1124 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1125                                              u64 sb_bytenr,
1126                                              u64 root_tree_bytenr,
1127                                              enum btrfs_open_ctree_flags flags)
1128 {
1129         struct btrfs_fs_info *fs_info;
1130         struct btrfs_super_block *disk_super;
1131         struct btrfs_fs_devices *fs_devices = NULL;
1132         struct extent_buffer *eb;
1133         int ret;
1134         int oflags;
1135
1136         if (sb_bytenr == 0)
1137                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1138
1139         /* try to drop all the caches */
1140         if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1141                 fprintf(stderr, "Warning, could not drop caches\n");
1142
1143         fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1144         if (!fs_info) {
1145                 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1146                 return NULL;
1147         }
1148         if (flags & OPEN_CTREE_RESTORE)
1149                 fs_info->on_restoring = 1;
1150         if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
1151                 fs_info->suppress_check_block_errors = 1;
1152
1153         ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1154                                     (flags & OPEN_CTREE_RECOVER_SUPER),
1155                                     (flags & OPEN_CTREE_NO_DEVICES));
1156         if (ret)
1157                 goto out;
1158
1159         fs_info->fs_devices = fs_devices;
1160         if (flags & OPEN_CTREE_WRITES)
1161                 oflags = O_RDWR;
1162         else
1163                 oflags = O_RDONLY;
1164
1165         if (flags & OPEN_CTREE_EXCLUSIVE)
1166                 oflags |= O_EXCL;
1167
1168         ret = btrfs_open_devices(fs_devices, oflags);
1169         if (ret)
1170                 goto out;
1171
1172         disk_super = fs_info->super_copy;
1173         if (!(flags & OPEN_CTREE_RECOVER_SUPER))
1174                 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1175                                            disk_super, sb_bytenr, 1);
1176         else
1177                 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
1178         if (ret) {
1179                 printk("No valid btrfs found\n");
1180                 goto out_devices;
1181         }
1182
1183         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1184
1185         ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1186                                            flags & OPEN_CTREE_WRITES);
1187         if (ret)
1188                 goto out_devices;
1189
1190         ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1191         if (ret)
1192                 goto out_chunk;
1193
1194         eb = fs_info->chunk_root->node;
1195         read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1196                            btrfs_header_chunk_tree_uuid(eb),
1197                            BTRFS_UUID_SIZE);
1198
1199         ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1200         if (ret && !(flags & __OPEN_CTREEE_RETURN_CHUNK_ROOT))
1201                 goto out_chunk;
1202
1203         return fs_info;
1204
1205 out_chunk:
1206         btrfs_release_all_roots(fs_info);
1207         btrfs_cleanup_all_caches(fs_info);
1208 out_devices:
1209         btrfs_close_devices(fs_devices);
1210 out:
1211         btrfs_free_fs_info(fs_info);
1212         return NULL;
1213 }
1214
1215 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1216                                          u64 sb_bytenr, u64 root_tree_bytenr,
1217                                          enum btrfs_open_ctree_flags flags)
1218 {
1219         int fp;
1220         struct btrfs_fs_info *info;
1221         int oflags = O_CREAT | O_RDWR;
1222
1223         if (!(flags & OPEN_CTREE_WRITES))
1224                 oflags = O_RDONLY;
1225
1226         fp = open(filename, oflags, 0600);
1227         if (fp < 0) {
1228                 fprintf (stderr, "Could not open %s\n", filename);
1229                 return NULL;
1230         }
1231         info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1232                                flags);
1233         close(fp);
1234         return info;
1235 }
1236
1237 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1238                               enum btrfs_open_ctree_flags flags)
1239 {
1240         struct btrfs_fs_info *info;
1241
1242         info = open_ctree_fs_info(filename, sb_bytenr, 0, flags);
1243         if (!info)
1244                 return NULL;
1245         if (flags & __OPEN_CTREEE_RETURN_CHUNK_ROOT)
1246                 return info->chunk_root;
1247         return info->fs_root;
1248 }
1249
1250 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1251                                  enum btrfs_open_ctree_flags flags)
1252 {
1253         struct btrfs_fs_info *info;
1254         info = __open_ctree_fd(fp, path, sb_bytenr, 0, flags);
1255         if (!info)
1256                 return NULL;
1257         if (flags & __OPEN_CTREEE_RETURN_CHUNK_ROOT)
1258                 return info->chunk_root;
1259         return info->fs_root;
1260 }
1261
1262 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1263                          int super_recover)
1264 {
1265         u8 fsid[BTRFS_FSID_SIZE];
1266         int fsid_is_initialized = 0;
1267         struct btrfs_super_block buf;
1268         int i;
1269         int ret;
1270         int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
1271         u64 transid = 0;
1272         u64 bytenr;
1273
1274         if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1275                 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1276                 if (ret < sizeof(buf))
1277                         return -1;
1278
1279                 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1280                     btrfs_super_magic(&buf) != BTRFS_MAGIC)
1281                         return -1;
1282
1283                 memcpy(sb, &buf, sizeof(*sb));
1284                 return 0;
1285         }
1286
1287         /*
1288         * we would like to check all the supers, but that would make
1289         * a btrfs mount succeed after a mkfs from a different FS.
1290         * So, we need to add a special mount option to scan for
1291         * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1292         */
1293
1294         for (i = 0; i < max_super; i++) {
1295                 bytenr = btrfs_sb_offset(i);
1296                 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1297                 if (ret < sizeof(buf))
1298                         break;
1299
1300                 if (btrfs_super_bytenr(&buf) != bytenr )
1301                         continue;
1302                 /* if magic is NULL, the device was removed */
1303                 if (btrfs_super_magic(&buf) == 0 && i == 0)
1304                         return -1;
1305                 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1306                         continue;
1307
1308                 if (!fsid_is_initialized) {
1309                         memcpy(fsid, buf.fsid, sizeof(fsid));
1310                         fsid_is_initialized = 1;
1311                 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1312                         /*
1313                          * the superblocks (the original one and
1314                          * its backups) contain data of different
1315                          * filesystems -> the super cannot be trusted
1316                          */
1317                         continue;
1318                 }
1319
1320                 if (btrfs_super_generation(&buf) > transid) {
1321                         memcpy(sb, &buf, sizeof(*sb));
1322                         transid = btrfs_super_generation(&buf);
1323                 }
1324         }
1325
1326         return transid > 0 ? 0 : -1;
1327 }
1328
1329 static int write_dev_supers(struct btrfs_root *root,
1330                             struct btrfs_super_block *sb,
1331                             struct btrfs_device *device)
1332 {
1333         u64 bytenr;
1334         u32 crc;
1335         int i, ret;
1336
1337         if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1338                 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1339                 crc = ~(u32)0;
1340                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1341                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1342                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1343
1344                 /*
1345                  * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1346                  * zero filled, we can use it directly
1347                  */
1348                 ret = pwrite64(device->fd, root->fs_info->super_copy,
1349                                 BTRFS_SUPER_INFO_SIZE,
1350                                 root->fs_info->super_bytenr);
1351                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1352                 return 0;
1353         }
1354
1355         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1356                 bytenr = btrfs_sb_offset(i);
1357                 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1358                         break;
1359
1360                 btrfs_set_super_bytenr(sb, bytenr);
1361
1362                 crc = ~(u32)0;
1363                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1364                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1365                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1366
1367                 /*
1368                  * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1369                  * zero filled, we can use it directly
1370                  */
1371                 ret = pwrite64(device->fd, root->fs_info->super_copy,
1372                                 BTRFS_SUPER_INFO_SIZE, bytenr);
1373                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1374         }
1375
1376         return 0;
1377 }
1378
1379 int write_all_supers(struct btrfs_root *root)
1380 {
1381         struct list_head *cur;
1382         struct list_head *head = &root->fs_info->fs_devices->devices;
1383         struct btrfs_device *dev;
1384         struct btrfs_super_block *sb;
1385         struct btrfs_dev_item *dev_item;
1386         int ret;
1387         u64 flags;
1388
1389         sb = root->fs_info->super_copy;
1390         dev_item = &sb->dev_item;
1391         list_for_each(cur, head) {
1392                 dev = list_entry(cur, struct btrfs_device, dev_list);
1393                 if (!dev->writeable)
1394                         continue;
1395
1396                 btrfs_set_stack_device_generation(dev_item, 0);
1397                 btrfs_set_stack_device_type(dev_item, dev->type);
1398                 btrfs_set_stack_device_id(dev_item, dev->devid);
1399                 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1400                 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1401                 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1402                 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1403                 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1404                 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1405                 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1406
1407                 flags = btrfs_super_flags(sb);
1408                 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1409
1410                 ret = write_dev_supers(root, sb, dev);
1411                 BUG_ON(ret);
1412         }
1413         return 0;
1414 }
1415
1416 int write_ctree_super(struct btrfs_trans_handle *trans,
1417                       struct btrfs_root *root)
1418 {
1419         int ret;
1420         struct btrfs_root *tree_root = root->fs_info->tree_root;
1421         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1422
1423         if (root->fs_info->readonly)
1424                 return 0;
1425
1426         btrfs_set_super_generation(root->fs_info->super_copy,
1427                                    trans->transid);
1428         btrfs_set_super_root(root->fs_info->super_copy,
1429                              tree_root->node->start);
1430         btrfs_set_super_root_level(root->fs_info->super_copy,
1431                                    btrfs_header_level(tree_root->node));
1432         btrfs_set_super_chunk_root(root->fs_info->super_copy,
1433                                    chunk_root->node->start);
1434         btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1435                                          btrfs_header_level(chunk_root->node));
1436         btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1437                                 btrfs_header_generation(chunk_root->node));
1438
1439         ret = write_all_supers(root);
1440         if (ret)
1441                 fprintf(stderr, "failed to write new super block err %d\n", ret);
1442         return ret;
1443 }
1444
1445 int close_ctree(struct btrfs_root *root)
1446 {
1447         int ret;
1448         struct btrfs_trans_handle *trans;
1449         struct btrfs_fs_info *fs_info = root->fs_info;
1450
1451         if (fs_info->last_trans_committed !=
1452             fs_info->generation) {
1453                 trans = btrfs_start_transaction(root, 1);
1454                 btrfs_commit_transaction(trans, root);
1455                 trans = btrfs_start_transaction(root, 1);
1456                 ret = commit_tree_roots(trans, fs_info);
1457                 BUG_ON(ret);
1458                 ret = __commit_transaction(trans, root);
1459                 BUG_ON(ret);
1460                 write_ctree_super(trans, root);
1461                 btrfs_free_transaction(root, trans);
1462         }
1463         btrfs_free_block_groups(fs_info);
1464
1465         free_fs_roots_tree(&fs_info->fs_root_tree);
1466
1467         btrfs_release_all_roots(fs_info);
1468         btrfs_close_devices(fs_info->fs_devices);
1469         btrfs_cleanup_all_caches(fs_info);
1470         btrfs_free_fs_info(fs_info);
1471         return 0;
1472 }
1473
1474 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1475                      struct extent_buffer *eb)
1476 {
1477         return clear_extent_buffer_dirty(eb);
1478 }
1479
1480 int wait_on_tree_block_writeback(struct btrfs_root *root,
1481                                  struct extent_buffer *eb)
1482 {
1483         return 0;
1484 }
1485
1486 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1487 {
1488         set_extent_buffer_dirty(eb);
1489 }
1490
1491 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1492 {
1493         int ret;
1494
1495         ret = extent_buffer_uptodate(buf);
1496         if (!ret)
1497                 return ret;
1498
1499         ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1500         return !ret;
1501 }
1502
1503 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1504 {
1505         return set_extent_buffer_uptodate(eb);
1506 }