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