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