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