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