Btrfs-progs: check out if the swap device
[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         if (ret)
617                 return ret;
618
619         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
620         generation = btrfs_root_generation(&root->root_item);
621         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
622                                      blocksize, generation);
623         if (!extent_buffer_uptodate(root->node))
624                 return -EIO;
625
626         return 0;
627 }
628
629 static int find_and_setup_log_root(struct btrfs_root *tree_root,
630                                struct btrfs_fs_info *fs_info,
631                                struct btrfs_super_block *disk_super)
632 {
633         u32 blocksize;
634         u64 blocknr = btrfs_super_log_root(disk_super);
635         struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
636
637         if (!log_root)
638                 return -ENOMEM;
639
640         if (blocknr == 0) {
641                 free(log_root);
642                 return 0;
643         }
644
645         blocksize = btrfs_level_size(tree_root,
646                              btrfs_super_log_root_level(disk_super));
647
648         __setup_root(tree_root->nodesize, tree_root->leafsize,
649                      tree_root->sectorsize, tree_root->stripesize,
650                      log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
651
652         log_root->node = read_tree_block(tree_root, blocknr,
653                                      blocksize,
654                                      btrfs_super_generation(disk_super) + 1);
655
656         fs_info->log_root_tree = log_root;
657
658         if (!extent_buffer_uptodate(log_root->node)) {
659                 free(log_root);
660                 return -EIO;
661         }
662
663         free(log_root);
664         return 0;
665 }
666
667
668 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
669                        struct btrfs_root *root)
670 {
671         if (root->node)
672                 free_extent_buffer(root->node);
673         if (root->commit_root)
674                 free_extent_buffer(root->commit_root);
675         kfree(root);
676         return 0;
677 }
678
679 static int free_fs_roots(struct btrfs_fs_info *fs_info)
680 {
681         struct cache_extent *cache;
682         struct btrfs_root *root;
683
684         while (1) {
685                 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
686                 if (!cache)
687                         break;
688                 root = container_of(cache, struct btrfs_root, cache);
689                 remove_cache_extent(&fs_info->fs_root_cache, cache);
690                 btrfs_free_fs_root(fs_info, root);
691         }
692         return 0;
693 }
694
695 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
696                                                struct btrfs_key *location)
697 {
698         struct btrfs_root *root;
699         struct btrfs_root *tree_root = fs_info->tree_root;
700         struct btrfs_path *path;
701         struct extent_buffer *l;
702         u64 generation;
703         u32 blocksize;
704         int ret = 0;
705
706         root = malloc(sizeof(*root));
707         if (!root)
708                 return ERR_PTR(-ENOMEM);
709         memset(root, 0, sizeof(*root));
710         if (location->offset == (u64)-1) {
711                 ret = find_and_setup_root(tree_root, fs_info,
712                                           location->objectid, root);
713                 if (ret) {
714                         free(root);
715                         return ERR_PTR(ret);
716                 }
717                 goto insert;
718         }
719
720         __setup_root(tree_root->nodesize, tree_root->leafsize,
721                      tree_root->sectorsize, tree_root->stripesize,
722                      root, fs_info, location->objectid);
723
724         path = btrfs_alloc_path();
725         BUG_ON(!path);
726         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
727         if (ret != 0) {
728                 if (ret > 0)
729                         ret = -ENOENT;
730                 goto out;
731         }
732         l = path->nodes[0];
733         read_extent_buffer(l, &root->root_item,
734                btrfs_item_ptr_offset(l, path->slots[0]),
735                sizeof(root->root_item));
736         memcpy(&root->root_key, location, sizeof(*location));
737         ret = 0;
738 out:
739         btrfs_release_path(root, path);
740         btrfs_free_path(path);
741         if (ret) {
742                 free(root);
743                 return ERR_PTR(ret);
744         }
745         generation = btrfs_root_generation(&root->root_item);
746         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
747         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
748                                      blocksize, generation);
749         BUG_ON(!root->node);
750 insert:
751         root->ref_cows = 1;
752         return root;
753 }
754
755 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
756                                       struct btrfs_key *location)
757 {
758         struct btrfs_root *root;
759         struct cache_extent *cache;
760         int ret;
761
762         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
763                 return fs_info->tree_root;
764         if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
765                 return fs_info->extent_root;
766         if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
767                 return fs_info->chunk_root;
768         if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
769                 return fs_info->dev_root;
770         if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
771                 return fs_info->csum_root;
772
773         BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
774                location->offset != (u64)-1);
775
776         cache = find_cache_extent(&fs_info->fs_root_cache,
777                                   location->objectid, 1);
778         if (cache)
779                 return container_of(cache, struct btrfs_root, cache);
780
781         root = btrfs_read_fs_root_no_cache(fs_info, location);
782         if (IS_ERR(root))
783                 return root;
784
785         root->cache.start = location->objectid;
786         root->cache.size = 1;
787         ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
788                                            &root->cache);
789         BUG_ON(ret);
790         return root;
791 }
792
793 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
794                                              u64 sb_bytenr,
795                                              u64 root_tree_bytenr, int writes,
796                                              int partial)
797 {
798         u32 sectorsize;
799         u32 nodesize;
800         u32 leafsize;
801         u32 blocksize;
802         u32 stripesize;
803         u64 generation;
804         struct btrfs_key key;
805         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
806         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
807         struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
808         struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
809         struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
810         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
811         int ret;
812         struct btrfs_super_block *disk_super;
813         struct btrfs_fs_devices *fs_devices = NULL;
814         u64 total_devs;
815         u64 features;
816
817         if (sb_bytenr == 0)
818                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
819
820         /* try to drop all the caches */
821         posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED);
822
823         ret = btrfs_scan_one_device(fp, path, &fs_devices,
824                                     &total_devs, sb_bytenr);
825
826         if (ret) {
827                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
828                 goto out;
829         }
830
831         if (total_devs != 1) {
832                 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
833                 if (ret)
834                         goto out;
835         }
836
837         memset(fs_info, 0, sizeof(*fs_info));
838         fs_info->tree_root = tree_root;
839         fs_info->extent_root = extent_root;
840         fs_info->chunk_root = chunk_root;
841         fs_info->dev_root = dev_root;
842         fs_info->csum_root = csum_root;
843
844         if (!writes)
845                 fs_info->readonly = 1;
846
847         extent_io_tree_init(&fs_info->extent_cache);
848         extent_io_tree_init(&fs_info->free_space_cache);
849         extent_io_tree_init(&fs_info->block_group_cache);
850         extent_io_tree_init(&fs_info->pinned_extents);
851         extent_io_tree_init(&fs_info->pending_del);
852         extent_io_tree_init(&fs_info->extent_ins);
853         cache_tree_init(&fs_info->fs_root_cache);
854
855         cache_tree_init(&fs_info->mapping_tree.cache_tree);
856
857         mutex_init(&fs_info->fs_mutex);
858         fs_info->fs_devices = fs_devices;
859         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
860         INIT_LIST_HEAD(&fs_info->space_info);
861
862         __setup_root(4096, 4096, 4096, 4096, tree_root,
863                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
864
865         if (writes)
866                 ret = btrfs_open_devices(fs_devices, O_RDWR);
867         else
868                 ret = btrfs_open_devices(fs_devices, O_RDONLY);
869         if (ret)
870                 goto out_cleanup;
871
872         fs_info->super_bytenr = sb_bytenr;
873         disk_super = &fs_info->super_copy;
874         ret = btrfs_read_dev_super(fs_devices->latest_bdev,
875                                    disk_super, sb_bytenr);
876         if (ret) {
877                 printk("No valid btrfs found\n");
878                 goto out_devices;
879         }
880
881         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
882
883
884         features = btrfs_super_incompat_flags(disk_super) &
885                    ~BTRFS_FEATURE_INCOMPAT_SUPP;
886         if (features) {
887                 printk("couldn't open because of unsupported "
888                        "option features (%Lx).\n",
889                        (unsigned long long)features);
890                 goto out_devices;
891         }
892
893         features = btrfs_super_incompat_flags(disk_super);
894         if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
895                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
896                 btrfs_set_super_incompat_flags(disk_super, features);
897         }
898
899         features = btrfs_super_compat_ro_flags(disk_super) &
900                 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
901         if (writes && features) {
902                 printk("couldn't open RDWR because of unsupported "
903                        "option features (%Lx).\n",
904                        (unsigned long long)features);
905                 goto out_devices;
906         }
907
908         nodesize = btrfs_super_nodesize(disk_super);
909         leafsize = btrfs_super_leafsize(disk_super);
910         sectorsize = btrfs_super_sectorsize(disk_super);
911         stripesize = btrfs_super_stripesize(disk_super);
912         tree_root->nodesize = nodesize;
913         tree_root->leafsize = leafsize;
914         tree_root->sectorsize = sectorsize;
915         tree_root->stripesize = stripesize;
916
917         ret = btrfs_read_sys_array(tree_root);
918         if (ret)
919                 goto out_devices;
920         blocksize = btrfs_level_size(tree_root,
921                                      btrfs_super_chunk_root_level(disk_super));
922         generation = btrfs_super_chunk_root_generation(disk_super);
923
924         __setup_root(nodesize, leafsize, sectorsize, stripesize,
925                      chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
926
927         chunk_root->node = read_tree_block(chunk_root,
928                                            btrfs_super_chunk_root(disk_super),
929                                            blocksize, generation);
930         if (!extent_buffer_uptodate(chunk_root->node)) {
931                 printk("Couldn't read chunk root\n");
932                 goto out_devices;
933         }
934
935         read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
936                  (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
937                  BTRFS_UUID_SIZE);
938
939         if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
940                 ret = btrfs_read_chunk_tree(chunk_root);
941                 if (ret)
942                         goto out_failed;
943         }
944
945         blocksize = btrfs_level_size(tree_root,
946                                      btrfs_super_root_level(disk_super));
947         generation = btrfs_super_generation(disk_super);
948
949         if (!root_tree_bytenr)
950                 root_tree_bytenr = btrfs_super_root(disk_super);
951         tree_root->node = read_tree_block(tree_root,
952                                           root_tree_bytenr,
953                                           blocksize, generation);
954         if (!extent_buffer_uptodate(tree_root->node)) {
955                 printk("Couldn't read tree root\n");
956                 goto out_failed;
957         }
958         ret = find_and_setup_root(tree_root, fs_info,
959                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
960         if (ret) {
961                 printk("Couldn't setup extent tree\n");
962                 goto out_failed;
963         }
964         extent_root->track_dirty = 1;
965
966         ret = find_and_setup_root(tree_root, fs_info,
967                                   BTRFS_DEV_TREE_OBJECTID, dev_root);
968         if (ret) {
969                 printk("Couldn't setup device tree\n");
970                 goto out_failed;
971         }
972         dev_root->track_dirty = 1;
973
974         ret = find_and_setup_root(tree_root, fs_info,
975                                   BTRFS_CSUM_TREE_OBJECTID, csum_root);
976         if (ret) {
977                 printk("Couldn't setup csum tree\n");
978                 if (!partial)
979                         goto out_failed;
980         }
981         csum_root->track_dirty = 1;
982
983         find_and_setup_log_root(tree_root, fs_info, disk_super);
984
985         fs_info->generation = generation;
986         fs_info->last_trans_committed = generation;
987         btrfs_read_block_groups(fs_info->tree_root);
988
989         key.objectid = BTRFS_FS_TREE_OBJECTID;
990         key.type = BTRFS_ROOT_ITEM_KEY;
991         key.offset = (u64)-1;
992         fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
993
994         if (!fs_info->fs_root)
995                 goto out_failed;
996
997         fs_info->data_alloc_profile = (u64)-1;
998         fs_info->metadata_alloc_profile = (u64)-1;
999         fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1000
1001         return fs_info;
1002
1003 out_failed:
1004         if (partial)
1005                 return fs_info;
1006
1007         if (fs_info->csum_root)
1008                 free_extent_buffer(fs_info->csum_root->node);
1009         if (fs_info->dev_root)
1010                 free_extent_buffer(fs_info->dev_root->node);
1011         if (fs_info->extent_root)
1012                 free_extent_buffer(fs_info->extent_root->node);
1013         if (fs_info->tree_root)
1014                 free_extent_buffer(fs_info->tree_root->node);
1015         if (fs_info->chunk_root)
1016                 free_extent_buffer(fs_info->chunk_root->node);
1017 out_devices:
1018         close_all_devices(fs_info);
1019 out_cleanup:
1020         extent_io_tree_cleanup(&fs_info->extent_cache);
1021         extent_io_tree_cleanup(&fs_info->free_space_cache);
1022         extent_io_tree_cleanup(&fs_info->block_group_cache);
1023         extent_io_tree_cleanup(&fs_info->pinned_extents);
1024         extent_io_tree_cleanup(&fs_info->pending_del);
1025         extent_io_tree_cleanup(&fs_info->extent_ins);
1026 out:
1027         free(tree_root);
1028         free(extent_root);
1029         free(chunk_root);
1030         free(dev_root);
1031         free(csum_root);
1032         free(fs_info);
1033         return NULL;
1034 }
1035
1036 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1037                                          u64 sb_bytenr, int writes,
1038                                          int partial)
1039 {
1040         int fp;
1041         struct btrfs_fs_info *info;
1042         int flags = O_CREAT | O_RDWR;
1043
1044         if (!writes)
1045                 flags = O_RDONLY;
1046
1047         fp = open(filename, flags, 0600);
1048         if (fp < 0) {
1049                 fprintf (stderr, "Could not open %s\n", filename);
1050                 return NULL;
1051         }
1052         info = __open_ctree_fd(fp, filename, sb_bytenr, 0, writes, partial);
1053         close(fp);
1054         return info;
1055 }
1056
1057 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
1058 {
1059         struct btrfs_fs_info *info;
1060
1061         info = open_ctree_fs_info(filename, sb_bytenr, writes, 0);
1062         if (!info)
1063                 return NULL;
1064         return info->fs_root;
1065 }
1066
1067 struct btrfs_root *open_ctree_recovery(const char *filename, u64 sb_bytenr,
1068                                        u64 root_tree_bytenr)
1069 {
1070         int fp;
1071         struct btrfs_fs_info *info;
1072
1073
1074         fp = open(filename, O_RDONLY);
1075         if (fp < 0) {
1076                 fprintf (stderr, "Could not open %s\n", filename);
1077                 return NULL;
1078         }
1079         info = __open_ctree_fd(fp, filename, sb_bytenr,
1080                                root_tree_bytenr, 0, 0);
1081         close(fp);
1082
1083         if (!info)
1084                 return NULL;
1085         return info->fs_root;
1086 }
1087
1088 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1089                                  int writes)
1090 {
1091         struct btrfs_fs_info *info;
1092         info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0);
1093         if (!info)
1094                 return NULL;
1095         return info->fs_root;
1096 }
1097
1098 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
1099 {
1100         u8 fsid[BTRFS_FSID_SIZE];
1101         int fsid_is_initialized = 0;
1102         struct btrfs_super_block buf;
1103         int i;
1104         int ret;
1105         u64 transid = 0;
1106         u64 bytenr;
1107
1108         if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1109                 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1110                 if (ret < sizeof(buf))
1111                         return -1;
1112
1113                 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1114                     buf.magic != cpu_to_le64(BTRFS_MAGIC))
1115                         return -1;
1116
1117                 memcpy(sb, &buf, sizeof(*sb));
1118                 return 0;
1119         }
1120
1121         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1122                 bytenr = btrfs_sb_offset(i);
1123                 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1124                 if (ret < sizeof(buf))
1125                         break;
1126
1127                 if (btrfs_super_bytenr(&buf) != bytenr )
1128                         continue;
1129                 /* if magic is NULL, the device was removed */
1130                 if (buf.magic == 0 && i == 0) 
1131                         return -1;
1132                 if (buf.magic != cpu_to_le64(BTRFS_MAGIC))
1133                         continue;
1134
1135                 if (!fsid_is_initialized) {
1136                         memcpy(fsid, buf.fsid, sizeof(fsid));
1137                         fsid_is_initialized = 1;
1138                 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1139                         /*
1140                          * the superblocks (the original one and
1141                          * its backups) contain data of different
1142                          * filesystems -> the super cannot be trusted
1143                          */
1144                         continue;
1145                 }
1146
1147                 if (btrfs_super_generation(&buf) > transid) {
1148                         memcpy(sb, &buf, sizeof(*sb));
1149                         transid = btrfs_super_generation(&buf);
1150                 }
1151         }
1152
1153         return transid > 0 ? 0 : -1;
1154 }
1155
1156 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
1157                      struct btrfs_device *device)
1158 {
1159         u64 bytenr;
1160         u32 crc;
1161         int i, ret;
1162         void *buf;
1163
1164         buf = calloc(1, BTRFS_SUPER_INFO_SIZE);
1165         BUG_ON(!buf);
1166
1167         if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1168                 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1169                 crc = ~(u32)0;
1170                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1171                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1172                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1173
1174                 memcpy(buf, sb, sizeof(*sb));
1175                 ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE,
1176                                root->fs_info->super_bytenr);
1177                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1178                 goto out;
1179         }
1180
1181         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1182                 bytenr = btrfs_sb_offset(i);
1183                 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1184                         break;
1185
1186                 btrfs_set_super_bytenr(sb, bytenr);
1187
1188                 crc = ~(u32)0;
1189                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1190                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1191                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1192
1193                 memcpy(buf, sb, sizeof(*sb));
1194                 ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1195                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1196         }
1197 out:
1198         free(buf);
1199         return 0;
1200 }
1201
1202 int write_all_supers(struct btrfs_root *root)
1203 {
1204         struct list_head *cur;
1205         struct list_head *head = &root->fs_info->fs_devices->devices;
1206         struct btrfs_device *dev;
1207         struct btrfs_super_block *sb;
1208         struct btrfs_dev_item *dev_item;
1209         int ret;
1210         u64 flags;
1211
1212         sb = &root->fs_info->super_copy;
1213         dev_item = &sb->dev_item;
1214         list_for_each(cur, head) {
1215                 dev = list_entry(cur, struct btrfs_device, dev_list);
1216                 if (!dev->writeable)
1217                         continue;
1218
1219                 btrfs_set_stack_device_generation(dev_item, 0);
1220                 btrfs_set_stack_device_type(dev_item, dev->type);
1221                 btrfs_set_stack_device_id(dev_item, dev->devid);
1222                 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1223                 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1224                 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1225                 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1226                 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1227                 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1228                 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1229
1230                 flags = btrfs_super_flags(sb);
1231                 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1232
1233                 ret = write_dev_supers(root, sb, dev);
1234                 BUG_ON(ret);
1235         }
1236         return 0;
1237 }
1238
1239 int write_ctree_super(struct btrfs_trans_handle *trans,
1240                       struct btrfs_root *root)
1241 {
1242         int ret;
1243         struct btrfs_root *tree_root = root->fs_info->tree_root;
1244         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1245
1246         if (root->fs_info->readonly)
1247                 return 0;
1248
1249         btrfs_set_super_generation(&root->fs_info->super_copy,
1250                                    trans->transid);
1251         btrfs_set_super_root(&root->fs_info->super_copy,
1252                              tree_root->node->start);
1253         btrfs_set_super_root_level(&root->fs_info->super_copy,
1254                                    btrfs_header_level(tree_root->node));
1255         btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1256                                    chunk_root->node->start);
1257         btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1258                                          btrfs_header_level(chunk_root->node));
1259         btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1260                                 btrfs_header_generation(chunk_root->node));
1261
1262         ret = write_all_supers(root);
1263         if (ret)
1264                 fprintf(stderr, "failed to write new super block err %d\n", ret);
1265         return ret;
1266 }
1267
1268 static int close_all_devices(struct btrfs_fs_info *fs_info)
1269 {
1270         struct list_head *list;
1271         struct list_head *next;
1272         struct btrfs_device *device;
1273
1274         return 0;
1275
1276         list = &fs_info->fs_devices->devices;
1277         list_for_each(next, list) {
1278                 device = list_entry(next, struct btrfs_device, dev_list);
1279                 if (device->fd) {
1280                         fsync(device->fd);
1281                         posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED);
1282                 }
1283                 close(device->fd);
1284         }
1285         return 0;
1286 }
1287
1288 int close_ctree(struct btrfs_root *root)
1289 {
1290         int ret;
1291         struct btrfs_trans_handle *trans;
1292         struct btrfs_fs_info *fs_info = root->fs_info;
1293
1294         if (fs_info->last_trans_committed !=
1295             fs_info->generation) {
1296                 trans = btrfs_start_transaction(root, 1);
1297                 btrfs_commit_transaction(trans, root);
1298                 trans = btrfs_start_transaction(root, 1);
1299                 ret = commit_tree_roots(trans, fs_info);
1300                 BUG_ON(ret);
1301                 ret = __commit_transaction(trans, root);
1302                 BUG_ON(ret);
1303                 write_ctree_super(trans, root);
1304                 btrfs_free_transaction(root, trans);
1305         }
1306         btrfs_free_block_groups(fs_info);
1307
1308         free_fs_roots(fs_info);
1309
1310         if (fs_info->extent_root->node)
1311                 free_extent_buffer(fs_info->extent_root->node);
1312         if (fs_info->tree_root->node)
1313                 free_extent_buffer(fs_info->tree_root->node);
1314         if (fs_info->chunk_root->node)
1315                 free_extent_buffer(fs_info->chunk_root->node);
1316         if (fs_info->dev_root->node)
1317                 free_extent_buffer(fs_info->dev_root->node);
1318         if (fs_info->csum_root->node)
1319                 free_extent_buffer(fs_info->csum_root->node);
1320
1321         if (fs_info->log_root_tree) {
1322                 if (fs_info->log_root_tree->node)
1323                         free_extent_buffer(fs_info->log_root_tree->node);
1324                 free(fs_info->log_root_tree);
1325         }
1326
1327         close_all_devices(fs_info);
1328         extent_io_tree_cleanup(&fs_info->extent_cache);
1329         extent_io_tree_cleanup(&fs_info->free_space_cache);
1330         extent_io_tree_cleanup(&fs_info->block_group_cache);
1331         extent_io_tree_cleanup(&fs_info->pinned_extents);
1332         extent_io_tree_cleanup(&fs_info->pending_del);
1333         extent_io_tree_cleanup(&fs_info->extent_ins);
1334
1335         free(fs_info->tree_root);
1336         free(fs_info->extent_root);
1337         free(fs_info->chunk_root);
1338         free(fs_info->dev_root);
1339         free(fs_info->csum_root);
1340         free(fs_info);
1341
1342         return 0;
1343 }
1344
1345 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1346                      struct extent_buffer *eb)
1347 {
1348         return clear_extent_buffer_dirty(eb);
1349 }
1350
1351 int wait_on_tree_block_writeback(struct btrfs_root *root,
1352                                  struct extent_buffer *eb)
1353 {
1354         return 0;
1355 }
1356
1357 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1358 {
1359         set_extent_buffer_dirty(eb);
1360 }
1361
1362 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1363 {
1364         int ret;
1365
1366         ret = extent_buffer_uptodate(buf);
1367         if (!ret)
1368                 return ret;
1369
1370         ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1371         return !ret;
1372 }
1373
1374 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1375 {
1376         return set_extent_buffer_uptodate(eb);
1377 }