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