204abe088b4be371a9f6731216569e7d9d9db875
[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
37 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
38 {
39         if (buf->start != btrfs_header_bytenr(buf))
40                 BUG();
41
42         if (memcmp_extent_buffer(buf, root->fs_info->fsid,
43                                  (unsigned long)btrfs_header_fsid(buf),
44                                  BTRFS_FSID_SIZE))
45                 BUG();
46         return 0;
47 }
48
49 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
50 {
51         return crc32c(seed, data, len);
52 }
53
54 void btrfs_csum_final(u32 crc, char *result)
55 {
56         *(__le32 *)result = ~cpu_to_le32(crc);
57 }
58
59 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
60                            int verify)
61 {
62         char result[BTRFS_CRC32_SIZE];
63         u32 len;
64         u32 crc = ~(u32)0;
65
66         len = buf->len - BTRFS_CSUM_SIZE;
67         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
68         btrfs_csum_final(crc, result);
69
70         if (verify) {
71                 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
72                         printk("checksum verify failed on %llu\n",
73                                 (unsigned long long)buf->start);
74                         return 1;
75                 }
76         } else {
77                 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
78         }
79         return 0;
80 }
81
82 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
83                                             u64 bytenr, u32 blocksize)
84 {
85         return find_extent_buffer(&root->fs_info->extent_cache,
86                                   bytenr, blocksize);
87 }
88
89 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
90                                                  u64 bytenr, u32 blocksize)
91 {
92         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
93                                    blocksize);
94 }
95
96 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
97 {
98         int ret;
99         int total_devs = 1;
100         int dev_nr;
101         struct extent_buffer *eb;
102         u64 physical;
103         u64 length;
104         struct btrfs_device *device;
105
106         eb = btrfs_find_tree_block(root, bytenr, blocksize);
107         if (eb && btrfs_buffer_uptodate(eb)) {
108                 free_extent_buffer(eb);
109                 return 0;
110         }
111
112         dev_nr = 0;
113         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, dev_nr,
114                               bytenr, &physical, &length, &device,
115                               &total_devs);
116         BUG_ON(ret);
117         device->total_ios++;
118         blocksize = min(blocksize, (u32)(64 * 1024));
119         readahead(device->fd, physical, blocksize);
120         return 0;
121 }
122
123 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
124                                      u32 blocksize)
125 {
126         int ret;
127         int total_devs = 1;
128         int dev_nr;
129         struct extent_buffer *eb;
130         u64 physical;
131         u64 length;
132         struct btrfs_device *device;
133
134         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
135         if (!eb)
136                 return NULL;
137
138         if (btrfs_buffer_uptodate(eb))
139                 return eb;
140
141         dev_nr = 0;
142         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, dev_nr,
143                               eb->start, &physical, &length, &device,
144                               &total_devs);
145         BUG_ON(ret);
146         eb->fd = device->fd;
147         device->total_ios++;
148         eb->dev_bytenr = physical;
149         ret = read_extent_from_disk(eb);
150         if (ret) {
151                 free_extent_buffer(eb);
152                 return NULL;
153         }
154         btrfs_set_buffer_uptodate(eb);
155         return eb;
156 }
157
158 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
159                      struct extent_buffer *eb)
160 {
161         int ret;
162         int total_devs = 1;
163         int dev_nr;
164         u64 physical;
165         u64 length;
166         struct btrfs_device *device;
167
168         if (check_tree_block(root, eb))
169                 BUG();
170         if (!btrfs_buffer_uptodate(eb))
171                 BUG();
172
173         btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
174         csum_tree_block(root, eb, 0);
175
176         dev_nr = 0;
177         while(dev_nr < total_devs) {
178                 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
179                                       dev_nr, eb->start, &physical, &length,
180                                       &device, &total_devs);
181                 BUG_ON(ret);
182                 eb->fd = device->fd;
183                 eb->dev_bytenr = physical;
184                 dev_nr++;
185                 device->total_ios++;
186                 ret = write_extent_to_disk(eb);
187                 BUG_ON(ret);
188         }
189         return 0;
190 }
191
192 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
193                         u32 stripesize, struct btrfs_root *root,
194                         struct btrfs_fs_info *fs_info, u64 objectid)
195 {
196         root->node = NULL;
197         root->commit_root = NULL;
198         root->sectorsize = sectorsize;
199         root->nodesize = nodesize;
200         root->leafsize = leafsize;
201         root->stripesize = stripesize;
202         root->ref_cows = 0;
203         root->track_dirty = 0;
204
205         root->fs_info = fs_info;
206         root->objectid = objectid;
207         root->last_trans = 0;
208         root->highest_inode = 0;
209         root->last_inode_alloc = 0;
210
211         INIT_LIST_HEAD(&root->dirty_list);
212         memset(&root->root_key, 0, sizeof(root->root_key));
213         memset(&root->root_item, 0, sizeof(root->root_item));
214         root->root_key.objectid = objectid;
215         return 0;
216 }
217
218 static int update_cowonly_root(struct btrfs_trans_handle *trans,
219                                struct btrfs_root *root)
220 {
221         int ret;
222         u64 old_root_bytenr;
223         struct btrfs_root *tree_root = root->fs_info->tree_root;
224
225         btrfs_write_dirty_block_groups(trans, root);
226         while(1) {
227                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
228                 if (old_root_bytenr == root->node->start)
229                         break;
230                 btrfs_set_root_bytenr(&root->root_item,
231                                        root->node->start);
232                 root->root_item.level = btrfs_header_level(root->node);
233                 ret = btrfs_update_root(trans, tree_root,
234                                         &root->root_key,
235                                         &root->root_item);
236                 BUG_ON(ret);
237                 btrfs_write_dirty_block_groups(trans, root);
238         }
239         return 0;
240 }
241
242 static int commit_tree_roots(struct btrfs_trans_handle *trans,
243                              struct btrfs_fs_info *fs_info)
244 {
245         struct btrfs_root *root;
246         struct list_head *next;
247
248         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
249                 next = fs_info->dirty_cowonly_roots.next;
250                 list_del_init(next);
251                 root = list_entry(next, struct btrfs_root, dirty_list);
252                 update_cowonly_root(trans, root);
253         }
254         return 0;
255 }
256
257 static int __commit_transaction(struct btrfs_trans_handle *trans,
258                                 struct btrfs_root *root)
259 {
260         u64 start;
261         u64 end;
262         struct extent_buffer *eb;
263         struct extent_io_tree *tree = &root->fs_info->extent_cache;
264         int ret;
265
266         while(1) {
267                 ret = find_first_extent_bit(tree, 0, &start, &end,
268                                             EXTENT_DIRTY);
269                 if (ret)
270                         break;
271                 while(start <= end) {
272                         eb = find_first_extent_buffer(tree, start);
273                         BUG_ON(!eb || eb->start != start);
274                         ret = write_tree_block(trans, root, eb);
275                         BUG_ON(ret);
276                         start += eb->len;
277                         clear_extent_buffer_dirty(eb);
278                         free_extent_buffer(eb);
279                 }
280         }
281         return 0;
282 }
283
284 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
285                              struct btrfs_root *root)
286 {
287         int ret = 0;
288         struct btrfs_root *new_root = NULL;
289         struct btrfs_fs_info *fs_info = root->fs_info;
290
291         if (root->commit_root == root->node)
292                 goto commit_tree;
293
294         new_root = malloc(sizeof(*new_root));
295         if (!new_root)
296                 return -ENOMEM;
297         memcpy(new_root, root, sizeof(*new_root));
298         new_root->node = root->commit_root;
299         root->commit_root = NULL;
300
301         root->root_key.offset = trans->transid;
302         btrfs_set_root_bytenr(&root->root_item, root->node->start);
303         root->root_item.level = btrfs_header_level(root->node);
304         ret = btrfs_insert_root(trans, fs_info->tree_root,
305                                 &root->root_key, &root->root_item);
306         BUG_ON(ret);
307
308         btrfs_set_root_refs(&new_root->root_item, 0);
309         ret = btrfs_update_root(trans, root->fs_info->tree_root,
310                                 &new_root->root_key, &new_root->root_item);
311         BUG_ON(ret);
312
313         ret = commit_tree_roots(trans, fs_info);
314         BUG_ON(ret);
315         ret = __commit_transaction(trans, root);
316         BUG_ON(ret);
317         write_ctree_super(trans, root);
318         btrfs_finish_extent_commit(trans, fs_info->extent_root,
319                                    &fs_info->pinned_extents);
320         btrfs_free_transaction(root, trans);
321         fs_info->running_transaction = NULL;
322
323         trans = btrfs_start_transaction(root, 1);
324         ret = btrfs_drop_snapshot(trans, new_root);
325         BUG_ON(ret);
326         ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
327         BUG_ON(ret);
328 commit_tree:
329         ret = commit_tree_roots(trans, fs_info);
330         BUG_ON(ret);
331         ret = __commit_transaction(trans, root);
332         BUG_ON(ret);
333         write_ctree_super(trans, root);
334         btrfs_finish_extent_commit(trans, fs_info->extent_root,
335                                    &fs_info->pinned_extents);
336         btrfs_free_transaction(root, trans);
337         free_extent_buffer(root->commit_root);
338         root->commit_root = NULL;
339         fs_info->running_transaction = NULL;
340         if (new_root) {
341                 free_extent_buffer(new_root->node);
342                 free(new_root);
343         }
344         return 0;
345 }
346
347 static int find_and_setup_root(struct btrfs_root *tree_root,
348                                struct btrfs_fs_info *fs_info,
349                                u64 objectid, struct btrfs_root *root)
350 {
351         int ret;
352         u32 blocksize;
353
354         __setup_root(tree_root->nodesize, tree_root->leafsize,
355                      tree_root->sectorsize, tree_root->stripesize,
356                      root, fs_info, objectid);
357         ret = btrfs_find_last_root(tree_root, objectid,
358                                    &root->root_item, &root->root_key);
359         BUG_ON(ret);
360
361         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
362         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
363                                      blocksize);
364         BUG_ON(!root->node);
365         return 0;
366 }
367
368 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
369 {
370         if (root->node)
371                 free_extent_buffer(root->node);
372         if (root->commit_root)
373                 free_extent_buffer(root->commit_root);
374
375         free(root);
376         return 0;
377 }
378
379 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
380                                       struct btrfs_key *location)
381 {
382         struct btrfs_root *root;
383         struct btrfs_root *tree_root = fs_info->tree_root;
384         struct btrfs_path *path;
385         struct extent_buffer *l;
386         u32 blocksize;
387         int ret = 0;
388
389         root = malloc(sizeof(*root));
390         if (!root)
391                 return ERR_PTR(-ENOMEM);
392         memset(root, 0, sizeof(*root));
393         if (location->offset == (u64)-1) {
394                 ret = find_and_setup_root(tree_root, fs_info,
395                                           location->objectid, root);
396                 if (ret) {
397                         free(root);
398                         return ERR_PTR(ret);
399                 }
400                 goto insert;
401         }
402
403         __setup_root(tree_root->nodesize, tree_root->leafsize,
404                      tree_root->sectorsize, tree_root->stripesize,
405                      root, fs_info, location->objectid);
406
407         path = btrfs_alloc_path();
408         BUG_ON(!path);
409         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
410         if (ret != 0) {
411                 if (ret > 0)
412                         ret = -ENOENT;
413                 goto out;
414         }
415         l = path->nodes[0];
416         read_extent_buffer(l, &root->root_item,
417                btrfs_item_ptr_offset(l, path->slots[0]),
418                sizeof(root->root_item));
419         memcpy(&root->root_key, location, sizeof(*location));
420         ret = 0;
421 out:
422         btrfs_release_path(root, path);
423         btrfs_free_path(path);
424         if (ret) {
425                 free(root);
426                 return ERR_PTR(ret);
427         }
428         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
429         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
430                                      blocksize);
431         BUG_ON(!root->node);
432 insert:
433         root->ref_cows = 1;
434         return root;
435 }
436
437 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr)
438 {
439         int fp;
440
441         fp = open(filename, O_CREAT | O_RDWR, 0600);
442         if (fp < 0) {
443                 return NULL;
444         }
445         return open_ctree_fd(fp, filename, sb_bytenr);
446 }
447
448 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr)
449 {
450         u32 sectorsize;
451         u32 nodesize;
452         u32 leafsize;
453         u32 blocksize;
454         u32 stripesize;
455         struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
456         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
457         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
458         struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
459         struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
460         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
461         int ret;
462         struct btrfs_super_block *disk_super;
463         struct btrfs_fs_devices *fs_devices = NULL;
464         u64 total_devs;
465
466         if (sb_bytenr == 0)
467                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
468
469         ret = btrfs_scan_one_device(fp, path, &fs_devices,
470                                     &total_devs, sb_bytenr);
471
472         if (ret) {
473                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
474                 return NULL;
475         }
476         fprintf(stderr, "found Btrfs on %s with %lu devices\n", path,
477                 (unsigned long)total_devs);
478
479         if (total_devs != 1) {
480                 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
481                 BUG_ON(ret);
482         }
483
484         fs_info->fp = fs_devices->lowest_bdev;
485         fs_info->running_transaction = NULL;
486         fs_info->fs_root = root;
487         fs_info->tree_root = tree_root;
488         fs_info->extent_root = extent_root;
489         fs_info->extent_ops = NULL;
490         fs_info->priv_data = NULL;
491         fs_info->chunk_root = chunk_root;
492         fs_info->dev_root = dev_root;
493         fs_info->force_system_allocs = 0;
494
495         extent_io_tree_init(&fs_info->extent_cache);
496         extent_io_tree_init(&fs_info->free_space_cache);
497         extent_io_tree_init(&fs_info->block_group_cache);
498         extent_io_tree_init(&fs_info->pinned_extents);
499         extent_io_tree_init(&fs_info->pending_del);
500         extent_io_tree_init(&fs_info->extent_ins);
501
502         cache_tree_init(&fs_info->mapping_tree.cache_tree);
503
504         mutex_init(&fs_info->fs_mutex);
505         fs_info->fs_devices = fs_devices;
506         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
507         INIT_LIST_HEAD(&fs_info->space_info);
508
509         __setup_root(4096, 4096, 4096, 4096, tree_root,
510                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
511
512         ret = btrfs_open_devices(fs_devices, O_RDWR);
513         BUG_ON(ret);
514
515         fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
516                                                           4096);
517         BUG_ON(!fs_info->sb_buffer);
518         fs_info->sb_buffer->fd = fs_devices->lowest_bdev;
519         fs_info->sb_buffer->dev_bytenr = sb_bytenr;
520         ret = read_extent_from_disk(fs_info->sb_buffer);
521         BUG_ON(ret);
522         btrfs_set_buffer_uptodate(fs_info->sb_buffer);
523
524         read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
525                            sizeof(fs_info->super_copy));
526         read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
527                            (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
528                            BTRFS_FSID_SIZE);
529
530         disk_super = &fs_info->super_copy;
531         if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
532                     sizeof(disk_super->magic))) {
533                 printk("No valid btrfs found\n");
534                 BUG_ON(1);
535         }
536         nodesize = btrfs_super_nodesize(disk_super);
537         leafsize = btrfs_super_leafsize(disk_super);
538         sectorsize = btrfs_super_sectorsize(disk_super);
539         stripesize = btrfs_super_stripesize(disk_super);
540         tree_root->nodesize = nodesize;
541         tree_root->leafsize = leafsize;
542         tree_root->sectorsize = sectorsize;
543         tree_root->stripesize = stripesize;
544
545         ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
546         BUG_ON(ret);
547         ret = btrfs_read_sys_array(tree_root);
548         BUG_ON(ret);
549         blocksize = btrfs_level_size(tree_root,
550                                      btrfs_super_chunk_root_level(disk_super));
551
552         __setup_root(nodesize, leafsize, sectorsize, stripesize,
553                      chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
554
555         chunk_root->node = read_tree_block(chunk_root,
556                                            btrfs_super_chunk_root(disk_super),
557                                            blocksize);
558
559         BUG_ON(!chunk_root->node);
560
561         ret = btrfs_read_chunk_tree(chunk_root);
562         BUG_ON(ret);
563
564         blocksize = btrfs_level_size(tree_root,
565                                      btrfs_super_root_level(disk_super));
566
567         tree_root->node = read_tree_block(tree_root,
568                                           btrfs_super_root(disk_super),
569                                           blocksize);
570         BUG_ON(!tree_root->node);
571         ret = find_and_setup_root(tree_root, fs_info,
572                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
573         BUG_ON(ret);
574         extent_root->track_dirty = 1;
575
576         ret = find_and_setup_root(tree_root, fs_info,
577                                   BTRFS_DEV_TREE_OBJECTID, dev_root);
578         BUG_ON(ret);
579         dev_root->track_dirty = 1;
580
581         ret = find_and_setup_root(tree_root, fs_info,
582                                   BTRFS_FS_TREE_OBJECTID, root);
583         BUG_ON(ret);
584         root->ref_cows = 1;
585         fs_info->generation = btrfs_super_generation(disk_super) + 1;
586         btrfs_read_block_groups(root);
587         return root;
588 }
589
590 int write_ctree_super(struct btrfs_trans_handle *trans,
591                       struct btrfs_root *root)
592 {
593         int ret;
594         struct btrfs_root *tree_root = root->fs_info->tree_root;
595         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
596         btrfs_set_super_generation(&root->fs_info->super_copy,
597                                    trans->transid);
598         btrfs_set_super_root(&root->fs_info->super_copy,
599                              tree_root->node->start);
600         btrfs_set_super_root_level(&root->fs_info->super_copy,
601                                    btrfs_header_level(tree_root->node));
602         btrfs_set_super_chunk_root(&root->fs_info->super_copy,
603                                    chunk_root->node->start);
604         btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
605                                          btrfs_header_level(chunk_root->node));
606         write_extent_buffer(root->fs_info->sb_buffer,
607                             &root->fs_info->super_copy, 0,
608                             sizeof(root->fs_info->super_copy));
609         ret = write_tree_block(trans, root, root->fs_info->sb_buffer);
610         if (ret)
611                 fprintf(stderr, "failed to write new super block err %d\n", ret);
612         return ret;
613 }
614
615 static int close_all_devices(struct btrfs_fs_info *fs_info)
616 {
617         struct list_head *list;
618         struct list_head *next;
619         struct btrfs_device *device;
620
621         return 0;
622
623         list = &fs_info->fs_devices->devices;
624         list_for_each(next, list) {
625                 device = list_entry(next, struct btrfs_device, dev_list);
626                 // close(device->fd);
627         }
628         return 0;
629 }
630
631 int close_ctree(struct btrfs_root *root)
632 {
633         int ret;
634         struct btrfs_trans_handle *trans;
635         struct btrfs_fs_info *fs_info = root->fs_info;
636
637         trans = btrfs_start_transaction(root, 1);
638         btrfs_commit_transaction(trans, root);
639         trans = btrfs_start_transaction(root, 1);
640         ret = commit_tree_roots(trans, root->fs_info);
641         BUG_ON(ret);
642         ret = __commit_transaction(trans, root);
643         BUG_ON(ret);
644         write_ctree_super(trans, root);
645         btrfs_free_transaction(root, trans);
646         btrfs_free_block_groups(root->fs_info);
647         close(root->fs_info->fp);
648         if (root->node)
649                 free_extent_buffer(root->node);
650         if (root->fs_info->extent_root->node)
651                 free_extent_buffer(root->fs_info->extent_root->node);
652         if (root->fs_info->tree_root->node)
653                 free_extent_buffer(root->fs_info->tree_root->node);
654         free_extent_buffer(root->commit_root);
655         free_extent_buffer(root->fs_info->sb_buffer);
656
657         if (root->fs_info->chunk_root->node);
658                 free_extent_buffer(root->fs_info->chunk_root->node);
659
660         if (root->fs_info->dev_root->node);
661                 free_extent_buffer(root->fs_info->dev_root->node);
662
663         close_all_devices(root->fs_info);
664         extent_io_tree_cleanup(&fs_info->extent_cache);
665         extent_io_tree_cleanup(&fs_info->free_space_cache);
666         extent_io_tree_cleanup(&fs_info->block_group_cache);
667         extent_io_tree_cleanup(&fs_info->pinned_extents);
668         extent_io_tree_cleanup(&fs_info->pending_del);
669         extent_io_tree_cleanup(&fs_info->extent_ins);
670
671         free(fs_info->tree_root);
672         free(fs_info->extent_root);
673         free(fs_info->fs_root);
674         free(fs_info->chunk_root);
675         free(fs_info->dev_root);
676         free(fs_info);
677
678         return 0;
679 }
680
681 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
682                      struct extent_buffer *eb)
683 {
684         return clear_extent_buffer_dirty(eb);
685 }
686
687 int wait_on_tree_block_writeback(struct btrfs_root *root,
688                                  struct extent_buffer *eb)
689 {
690         return 0;
691 }
692
693 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
694 {
695         set_extent_buffer_dirty(eb);
696 }
697
698 int btrfs_buffer_uptodate(struct extent_buffer *eb)
699 {
700         return extent_buffer_uptodate(eb);
701 }
702
703 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
704 {
705         return set_extent_buffer_uptodate(eb);
706 }