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