Add scan of the btrfs log tree to btrfs-debug-tree
[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
41         struct btrfs_fs_devices *fs_devices;
42         int ret = 1;
43
44         if (buf->start != btrfs_header_bytenr(buf))
45                 return ret;
46
47         fs_devices = root->fs_info->fs_devices;
48         while (fs_devices) {
49                 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
50                                           (unsigned long)btrfs_header_fsid(buf),
51                                           BTRFS_FSID_SIZE)) {
52                         ret = 0;
53                         break;
54                 }
55                 fs_devices = fs_devices->seed;
56         }
57         return ret;
58 }
59
60 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
61 {
62         return crc32c(seed, data, len);
63 }
64
65 void btrfs_csum_final(u32 crc, char *result)
66 {
67         *(__le32 *)result = ~cpu_to_le32(crc);
68 }
69
70 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
71                          int verify)
72 {
73         char *result;
74         u32 len;
75         u32 crc = ~(u32)0;
76
77         result = malloc(csum_size * sizeof(char));
78         if (!result)
79                 return 1;
80
81         len = buf->len - BTRFS_CSUM_SIZE;
82         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
83         btrfs_csum_final(crc, result);
84
85         if (verify) {
86                 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
87                         printk("checksum verify failed on %llu wanted %X "
88                                "found %X\n", (unsigned long long)buf->start,
89                                *((int *)result), *((int *)buf));
90                         free(result);
91                         return 1;
92                 }
93         } else {
94                 write_extent_buffer(buf, result, 0, csum_size);
95         }
96         free(result);
97         return 0;
98 }
99
100 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
101                     int verify)
102 {
103         u16 csum_size =
104                 btrfs_super_csum_size(&root->fs_info->super_copy);
105         return csum_tree_block_size(buf, csum_size, verify);
106 }
107
108 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
109                                             u64 bytenr, u32 blocksize)
110 {
111         return find_extent_buffer(&root->fs_info->extent_cache,
112                                   bytenr, blocksize);
113 }
114
115 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
116                                                  u64 bytenr, u32 blocksize)
117 {
118         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
119                                    blocksize);
120 }
121
122 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
123                          u64 parent_transid)
124 {
125         int ret;
126         int dev_nr;
127         struct extent_buffer *eb;
128         u64 length;
129         struct btrfs_multi_bio *multi = NULL;
130         struct btrfs_device *device;
131
132         eb = btrfs_find_tree_block(root, bytenr, blocksize);
133         if (eb && btrfs_buffer_uptodate(eb, parent_transid)) {
134                 free_extent_buffer(eb);
135                 return 0;
136         }
137
138         dev_nr = 0;
139         length = blocksize;
140         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
141                               bytenr, &length, &multi, 0);
142         BUG_ON(ret);
143         device = multi->stripes[0].dev;
144         device->total_ios++;
145         blocksize = min(blocksize, (u32)(64 * 1024));
146         readahead(device->fd, multi->stripes[0].physical, blocksize);
147         kfree(multi);
148         return 0;
149 }
150
151 static int verify_parent_transid(struct extent_io_tree *io_tree,
152                                  struct extent_buffer *eb, u64 parent_transid)
153 {
154         int ret;
155
156         if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
157                 return 0;
158
159         if (extent_buffer_uptodate(eb) &&
160             btrfs_header_generation(eb) == parent_transid) {
161                 ret = 0;
162                 goto out;
163         }
164         printk("parent transid verify failed on %llu wanted %llu found %llu\n",
165                (unsigned long long)eb->start,
166                (unsigned long long)parent_transid,
167                (unsigned long long)btrfs_header_generation(eb));
168         ret = 1;
169 out:
170         clear_extent_buffer_uptodate(io_tree, eb);
171         return ret;
172
173 }
174
175
176 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
177                                      u32 blocksize, u64 parent_transid)
178 {
179         int ret;
180         int dev_nr;
181         struct extent_buffer *eb;
182         u64 length;
183         struct btrfs_multi_bio *multi = NULL;
184         struct btrfs_device *device;
185         int mirror_num = 0;
186         int num_copies;
187
188         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
189         if (!eb)
190                 return NULL;
191
192         if (btrfs_buffer_uptodate(eb, parent_transid))
193                 return eb;
194
195         dev_nr = 0;
196         length = blocksize;
197         while (1) {
198                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
199                                       eb->start, &length, &multi, mirror_num);
200                 BUG_ON(ret);
201                 device = multi->stripes[0].dev;
202                 eb->fd = device->fd;
203                 device->total_ios++;
204                 eb->dev_bytenr = multi->stripes[0].physical;
205                 kfree(multi);
206                 ret = read_extent_from_disk(eb);
207                 if (ret == 0 && check_tree_block(root, eb) == 0 &&
208                     csum_tree_block(root, eb, 1) == 0 &&
209                     verify_parent_transid(eb->tree, eb, parent_transid) == 0) {
210                         btrfs_set_buffer_uptodate(eb);
211                         return eb;
212                 }
213                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
214                                               eb->start, eb->len);
215                 if (num_copies == 1) {
216                         break;
217                 }
218                 mirror_num++;
219                 if (mirror_num > num_copies) {
220                         break;
221                 }
222         }
223         free_extent_buffer(eb);
224         return NULL;
225 }
226
227 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
228                      struct extent_buffer *eb)
229 {
230         int ret;
231         int dev_nr;
232         u64 length;
233         struct btrfs_multi_bio *multi = NULL;
234
235         if (check_tree_block(root, eb))
236                 BUG();
237         if (!btrfs_buffer_uptodate(eb, trans->transid))
238                 BUG();
239
240         btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
241         csum_tree_block(root, eb, 0);
242
243         dev_nr = 0;
244         length = eb->len;
245         ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
246                               eb->start, &length, &multi, 0);
247
248         while(dev_nr < multi->num_stripes) {
249                 BUG_ON(ret);
250                 eb->fd = multi->stripes[dev_nr].dev->fd;
251                 eb->dev_bytenr = multi->stripes[dev_nr].physical;
252                 multi->stripes[dev_nr].dev->total_ios++;
253                 dev_nr++;
254                 ret = write_extent_to_disk(eb);
255                 BUG_ON(ret);
256         }
257         kfree(multi);
258         return 0;
259 }
260
261 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
262                         u32 stripesize, struct btrfs_root *root,
263                         struct btrfs_fs_info *fs_info, u64 objectid)
264 {
265         root->node = NULL;
266         root->commit_root = NULL;
267         root->sectorsize = sectorsize;
268         root->nodesize = nodesize;
269         root->leafsize = leafsize;
270         root->stripesize = stripesize;
271         root->ref_cows = 0;
272         root->track_dirty = 0;
273
274         root->fs_info = fs_info;
275         root->objectid = objectid;
276         root->last_trans = 0;
277         root->highest_inode = 0;
278         root->last_inode_alloc = 0;
279
280         INIT_LIST_HEAD(&root->dirty_list);
281         memset(&root->root_key, 0, sizeof(root->root_key));
282         memset(&root->root_item, 0, sizeof(root->root_item));
283         root->root_key.objectid = objectid;
284         return 0;
285 }
286
287 static int update_cowonly_root(struct btrfs_trans_handle *trans,
288                                struct btrfs_root *root)
289 {
290         int ret;
291         u64 old_root_bytenr;
292         struct btrfs_root *tree_root = root->fs_info->tree_root;
293
294         btrfs_write_dirty_block_groups(trans, root);
295         while(1) {
296                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
297                 if (old_root_bytenr == root->node->start)
298                         break;
299                 btrfs_set_root_bytenr(&root->root_item,
300                                        root->node->start);
301                 btrfs_set_root_generation(&root->root_item,
302                                           trans->transid);
303                 root->root_item.level = btrfs_header_level(root->node);
304                 ret = btrfs_update_root(trans, tree_root,
305                                         &root->root_key,
306                                         &root->root_item);
307                 BUG_ON(ret);
308                 btrfs_write_dirty_block_groups(trans, root);
309         }
310         return 0;
311 }
312
313 static int commit_tree_roots(struct btrfs_trans_handle *trans,
314                              struct btrfs_fs_info *fs_info)
315 {
316         struct btrfs_root *root;
317         struct list_head *next;
318         struct extent_buffer *eb;
319
320         if (fs_info->readonly)
321                 return 0;
322
323         eb = fs_info->tree_root->node;
324         extent_buffer_get(eb);
325         btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
326         free_extent_buffer(eb);
327
328         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
329                 next = fs_info->dirty_cowonly_roots.next;
330                 list_del_init(next);
331                 root = list_entry(next, struct btrfs_root, dirty_list);
332                 update_cowonly_root(trans, root);
333         }
334         return 0;
335 }
336
337 static int __commit_transaction(struct btrfs_trans_handle *trans,
338                                 struct btrfs_root *root)
339 {
340         u64 start;
341         u64 end;
342         struct extent_buffer *eb;
343         struct extent_io_tree *tree = &root->fs_info->extent_cache;
344         int ret;
345
346         while(1) {
347                 ret = find_first_extent_bit(tree, 0, &start, &end,
348                                             EXTENT_DIRTY);
349                 if (ret)
350                         break;
351                 while(start <= end) {
352                         eb = find_first_extent_buffer(tree, start);
353                         BUG_ON(!eb || eb->start != start);
354                         ret = write_tree_block(trans, root, eb);
355                         BUG_ON(ret);
356                         start += eb->len;
357                         clear_extent_buffer_dirty(eb);
358                         free_extent_buffer(eb);
359                 }
360         }
361         return 0;
362 }
363
364 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
365                              struct btrfs_root *root)
366 {
367         int ret = 0;
368         struct btrfs_root *new_root = NULL;
369         struct btrfs_fs_info *fs_info = root->fs_info;
370
371         if (root->commit_root == root->node)
372                 goto commit_tree;
373
374         new_root = malloc(sizeof(*new_root));
375         if (!new_root)
376                 return -ENOMEM;
377         memcpy(new_root, root, sizeof(*new_root));
378         new_root->node = root->commit_root;
379         root->commit_root = NULL;
380
381         root->root_key.offset = trans->transid;
382         btrfs_set_root_bytenr(&root->root_item, root->node->start);
383         btrfs_set_root_generation(&root->root_item, root->root_key.offset);
384         root->root_item.level = btrfs_header_level(root->node);
385         ret = btrfs_insert_root(trans, fs_info->tree_root,
386                                 &root->root_key, &root->root_item);
387         BUG_ON(ret);
388
389         btrfs_set_root_refs(&new_root->root_item, 0);
390         ret = btrfs_update_root(trans, root->fs_info->tree_root,
391                                 &new_root->root_key, &new_root->root_item);
392         BUG_ON(ret);
393
394         ret = commit_tree_roots(trans, fs_info);
395         BUG_ON(ret);
396         ret = __commit_transaction(trans, root);
397         BUG_ON(ret);
398         write_ctree_super(trans, root);
399         btrfs_finish_extent_commit(trans, fs_info->extent_root,
400                                    &fs_info->pinned_extents);
401         btrfs_free_transaction(root, trans);
402         fs_info->running_transaction = NULL;
403
404         trans = btrfs_start_transaction(root, 1);
405         ret = btrfs_drop_snapshot(trans, new_root);
406         BUG_ON(ret);
407         ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
408         BUG_ON(ret);
409 commit_tree:
410         ret = commit_tree_roots(trans, fs_info);
411         BUG_ON(ret);
412         ret = __commit_transaction(trans, root);
413         BUG_ON(ret);
414         write_ctree_super(trans, root);
415         btrfs_finish_extent_commit(trans, fs_info->extent_root,
416                                    &fs_info->pinned_extents);
417         btrfs_free_transaction(root, trans);
418         free_extent_buffer(root->commit_root);
419         root->commit_root = NULL;
420         fs_info->running_transaction = NULL;
421         if (new_root) {
422                 free_extent_buffer(new_root->node);
423                 free(new_root);
424         }
425         return 0;
426 }
427
428 static int find_and_setup_root(struct btrfs_root *tree_root,
429                                struct btrfs_fs_info *fs_info,
430                                u64 objectid, struct btrfs_root *root)
431 {
432         int ret;
433         u32 blocksize;
434         u64 generation;
435
436         __setup_root(tree_root->nodesize, tree_root->leafsize,
437                      tree_root->sectorsize, tree_root->stripesize,
438                      root, fs_info, objectid);
439         ret = btrfs_find_last_root(tree_root, objectid,
440                                    &root->root_item, &root->root_key);
441         BUG_ON(ret);
442
443         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
444         generation = btrfs_root_generation(&root->root_item);
445         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
446                                      blocksize, generation);
447         BUG_ON(!root->node);
448         return 0;
449 }
450
451 static int find_and_setup_log_root(struct btrfs_root *tree_root,
452                                struct btrfs_fs_info *fs_info,
453                                struct btrfs_super_block *disk_super)
454 {
455         u32 blocksize;
456         u64 blocknr = btrfs_super_log_root(disk_super);
457         struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
458
459         if (blocknr == 0)
460                 return 0;
461
462         blocksize = btrfs_level_size(tree_root,
463                              btrfs_super_log_root_level(disk_super));
464
465         __setup_root(tree_root->nodesize, tree_root->leafsize,
466                      tree_root->sectorsize, tree_root->stripesize,
467                      log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
468
469         log_root->node = read_tree_block(tree_root, blocknr,
470                                      blocksize,
471                                      btrfs_super_generation(disk_super) + 1);
472
473         fs_info->log_root_tree = log_root;
474         BUG_ON(!log_root->node);
475         return 0;
476 }
477
478 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
479 {
480         if (root->node)
481                 free_extent_buffer(root->node);
482         if (root->commit_root)
483                 free_extent_buffer(root->commit_root);
484
485         free(root);
486         return 0;
487 }
488
489 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
490                                       struct btrfs_key *location)
491 {
492         struct btrfs_root *root;
493         struct btrfs_root *tree_root = fs_info->tree_root;
494         struct btrfs_path *path;
495         struct extent_buffer *l;
496         u64 generation;
497         u32 blocksize;
498         int ret = 0;
499
500         root = malloc(sizeof(*root));
501         if (!root)
502                 return ERR_PTR(-ENOMEM);
503         memset(root, 0, sizeof(*root));
504         if (location->offset == (u64)-1) {
505                 ret = find_and_setup_root(tree_root, fs_info,
506                                           location->objectid, root);
507                 if (ret) {
508                         free(root);
509                         return ERR_PTR(ret);
510                 }
511                 goto insert;
512         }
513
514         __setup_root(tree_root->nodesize, tree_root->leafsize,
515                      tree_root->sectorsize, tree_root->stripesize,
516                      root, fs_info, location->objectid);
517
518         path = btrfs_alloc_path();
519         BUG_ON(!path);
520         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
521         if (ret != 0) {
522                 if (ret > 0)
523                         ret = -ENOENT;
524                 goto out;
525         }
526         l = path->nodes[0];
527         read_extent_buffer(l, &root->root_item,
528                btrfs_item_ptr_offset(l, path->slots[0]),
529                sizeof(root->root_item));
530         memcpy(&root->root_key, location, sizeof(*location));
531         ret = 0;
532 out:
533         btrfs_release_path(root, path);
534         btrfs_free_path(path);
535         if (ret) {
536                 free(root);
537                 return ERR_PTR(ret);
538         }
539         generation = btrfs_root_generation(&root->root_item);
540         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
541         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
542                                      blocksize, generation);
543         BUG_ON(!root->node);
544 insert:
545         root->ref_cows = 1;
546         return root;
547 }
548
549 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
550 {
551         int fp;
552         struct btrfs_root *root;
553         int flags = O_CREAT | O_RDWR;
554
555         if (!writes)
556                 flags = O_RDONLY;
557
558         fp = open(filename, flags, 0600);
559         if (fp < 0) {
560                 fprintf (stderr, "Coult not open %s\n", filename);
561                 return NULL;
562         }
563         root = open_ctree_fd(fp, filename, sb_bytenr, writes);
564         close(fp);
565
566         return root;
567 }
568
569 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
570                                  int writes)
571 {
572         u32 sectorsize;
573         u32 nodesize;
574         u32 leafsize;
575         u32 blocksize;
576         u32 stripesize;
577         u64 generation;
578         struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
579         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
580         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
581         struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
582         struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
583         struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
584         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
585         int ret;
586         struct btrfs_super_block *disk_super;
587         struct btrfs_fs_devices *fs_devices = NULL;
588         u64 total_devs;
589
590         if (sb_bytenr == 0)
591                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
592
593         ret = btrfs_scan_one_device(fp, path, &fs_devices,
594                                     &total_devs, sb_bytenr);
595
596         if (ret) {
597                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
598                 return NULL;
599         }
600
601         if (total_devs != 1) {
602                 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
603                 BUG_ON(ret);
604         }
605
606         memset(fs_info, 0, sizeof(*fs_info));
607         fs_info->fs_root = root;
608         fs_info->tree_root = tree_root;
609         fs_info->extent_root = extent_root;
610         fs_info->chunk_root = chunk_root;
611         fs_info->dev_root = dev_root;
612         fs_info->csum_root = csum_root;
613
614         if (!writes)
615                 fs_info->readonly = 1;
616
617         extent_io_tree_init(&fs_info->extent_cache);
618         extent_io_tree_init(&fs_info->free_space_cache);
619         extent_io_tree_init(&fs_info->block_group_cache);
620         extent_io_tree_init(&fs_info->pinned_extents);
621         extent_io_tree_init(&fs_info->pending_del);
622         extent_io_tree_init(&fs_info->extent_ins);
623
624         cache_tree_init(&fs_info->mapping_tree.cache_tree);
625
626         mutex_init(&fs_info->fs_mutex);
627         fs_info->fs_devices = fs_devices;
628         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
629         INIT_LIST_HEAD(&fs_info->space_info);
630
631         __setup_root(4096, 4096, 4096, 4096, tree_root,
632                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
633
634         if (writes)
635                 ret = btrfs_open_devices(fs_devices, O_RDWR);
636         else
637                 ret = btrfs_open_devices(fs_devices, O_RDONLY);
638         BUG_ON(ret);
639
640         fs_info->super_bytenr = sb_bytenr;
641         disk_super = &fs_info->super_copy;
642         ret = btrfs_read_dev_super(fs_devices->latest_bdev,
643                                    disk_super, sb_bytenr);
644         if (ret) {
645                 printk("No valid btrfs found\n");
646                 BUG_ON(1);
647         }
648
649         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
650
651         nodesize = btrfs_super_nodesize(disk_super);
652         leafsize = btrfs_super_leafsize(disk_super);
653         sectorsize = btrfs_super_sectorsize(disk_super);
654         stripesize = btrfs_super_stripesize(disk_super);
655         tree_root->nodesize = nodesize;
656         tree_root->leafsize = leafsize;
657         tree_root->sectorsize = sectorsize;
658         tree_root->stripesize = stripesize;
659
660         ret = btrfs_read_sys_array(tree_root);
661         BUG_ON(ret);
662         blocksize = btrfs_level_size(tree_root,
663                                      btrfs_super_chunk_root_level(disk_super));
664         generation = btrfs_super_chunk_root_generation(disk_super);
665
666         __setup_root(nodesize, leafsize, sectorsize, stripesize,
667                      chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
668
669         chunk_root->node = read_tree_block(chunk_root,
670                                            btrfs_super_chunk_root(disk_super),
671                                            blocksize, generation);
672
673         BUG_ON(!chunk_root->node);
674
675         read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
676                  (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
677                  BTRFS_UUID_SIZE);
678
679         if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
680                 ret = btrfs_read_chunk_tree(chunk_root);
681                 BUG_ON(ret);
682         }
683
684         blocksize = btrfs_level_size(tree_root,
685                                      btrfs_super_root_level(disk_super));
686         generation = btrfs_super_generation(disk_super);
687
688         tree_root->node = read_tree_block(tree_root,
689                                           btrfs_super_root(disk_super),
690                                           blocksize, generation);
691         BUG_ON(!tree_root->node);
692         ret = find_and_setup_root(tree_root, fs_info,
693                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
694         BUG_ON(ret);
695         extent_root->track_dirty = 1;
696
697         ret = find_and_setup_root(tree_root, fs_info,
698                                   BTRFS_DEV_TREE_OBJECTID, dev_root);
699         BUG_ON(ret);
700         dev_root->track_dirty = 1;
701
702         ret = find_and_setup_root(tree_root, fs_info,
703                                   BTRFS_CSUM_TREE_OBJECTID, csum_root);
704         BUG_ON(ret);
705         csum_root->track_dirty = 1;
706
707         ret = find_and_setup_root(tree_root, fs_info,
708                                   BTRFS_FS_TREE_OBJECTID, root);
709         BUG_ON(ret);
710         root->ref_cows = 1;
711
712         find_and_setup_log_root(tree_root, fs_info, disk_super);
713         btrfs_read_block_groups(root);
714
715         fs_info->data_alloc_profile = (u64)-1;
716         fs_info->metadata_alloc_profile = (u64)-1;
717         fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
718
719         return root;
720 }
721
722 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
723 {
724         struct btrfs_super_block buf;
725         int i;
726         int ret;
727         u64 transid = 0;
728         u64 bytenr;
729
730         if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
731                 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
732                 if (ret < sizeof(buf))
733                         return -1;
734
735                 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
736                     strncmp((char *)(&buf.magic), BTRFS_MAGIC,
737                             sizeof(buf.magic)))
738                         return -1;
739
740                 memcpy(sb, &buf, sizeof(*sb));
741                 return 0;
742         }
743
744         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
745                 bytenr = btrfs_sb_offset(i);
746                 ret = pread64(fd, &buf, sizeof(buf), bytenr);
747                 if (ret < sizeof(buf))
748                         break;
749
750                 if (btrfs_super_bytenr(&buf) != bytenr ||
751                     strncmp((char *)(&buf.magic), BTRFS_MAGIC,
752                             sizeof(buf.magic)))
753                         continue;
754
755                 if (btrfs_super_generation(&buf) > transid) {
756                         memcpy(sb, &buf, sizeof(*sb));
757                         transid = btrfs_super_generation(&buf);
758                 }
759         }
760
761         return transid > 0 ? 0 : -1;
762 }
763
764 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
765                      struct btrfs_device *device)
766 {
767         u64 bytenr;
768         u32 crc;
769         int i, ret;
770
771         if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
772                 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
773
774                 crc = ~(u32)0;
775                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
776                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
777                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
778
779                 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE,
780                                root->fs_info->super_bytenr);
781                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
782                 return 0;
783         }
784
785         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
786                 bytenr = btrfs_sb_offset(i);
787                 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
788                         break;
789
790                 btrfs_set_super_bytenr(sb, bytenr);
791
792                 crc = ~(u32)0;
793                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
794                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
795                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
796
797                 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE, bytenr);
798                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
799         }
800         return 0;
801 }
802
803 int write_all_supers(struct btrfs_root *root)
804 {
805         struct list_head *cur;
806         struct list_head *head = &root->fs_info->fs_devices->devices;
807         struct btrfs_device *dev;
808         struct btrfs_super_block *sb;
809         struct btrfs_dev_item *dev_item;
810         int ret;
811         u64 flags;
812
813         sb = &root->fs_info->super_copy;
814         dev_item = &sb->dev_item;
815         list_for_each(cur, head) {
816                 dev = list_entry(cur, struct btrfs_device, dev_list);
817                 if (!dev->writeable)
818                         continue;
819
820                 btrfs_set_stack_device_generation(dev_item, 0);
821                 btrfs_set_stack_device_type(dev_item, dev->type);
822                 btrfs_set_stack_device_id(dev_item, dev->devid);
823                 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
824                 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
825                 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
826                 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
827                 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
828                 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
829                 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
830
831                 flags = btrfs_super_flags(sb);
832                 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
833
834                 ret = write_dev_supers(root, sb, dev);
835                 BUG_ON(ret);
836         }
837         return 0;
838 }
839
840 int write_ctree_super(struct btrfs_trans_handle *trans,
841                       struct btrfs_root *root)
842 {
843         int ret;
844         struct btrfs_root *tree_root = root->fs_info->tree_root;
845         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
846
847         if (root->fs_info->readonly)
848                 return 0;
849
850         btrfs_set_super_generation(&root->fs_info->super_copy,
851                                    trans->transid);
852         btrfs_set_super_root(&root->fs_info->super_copy,
853                              tree_root->node->start);
854         btrfs_set_super_root_level(&root->fs_info->super_copy,
855                                    btrfs_header_level(tree_root->node));
856         btrfs_set_super_chunk_root(&root->fs_info->super_copy,
857                                    chunk_root->node->start);
858         btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
859                                          btrfs_header_level(chunk_root->node));
860         btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
861                                 btrfs_header_generation(chunk_root->node));
862
863         ret = write_all_supers(root);
864         if (ret)
865                 fprintf(stderr, "failed to write new super block err %d\n", ret);
866         return ret;
867 }
868
869 static int close_all_devices(struct btrfs_fs_info *fs_info)
870 {
871         struct list_head *list;
872         struct list_head *next;
873         struct btrfs_device *device;
874
875         return 0;
876
877         list = &fs_info->fs_devices->devices;
878         list_for_each(next, list) {
879                 device = list_entry(next, struct btrfs_device, dev_list);
880                 close(device->fd);
881         }
882         return 0;
883 }
884
885 int close_ctree(struct btrfs_root *root)
886 {
887         int ret;
888         struct btrfs_trans_handle *trans;
889         struct btrfs_fs_info *fs_info = root->fs_info;
890
891         trans = btrfs_start_transaction(root, 1);
892         btrfs_commit_transaction(trans, root);
893         trans = btrfs_start_transaction(root, 1);
894         ret = commit_tree_roots(trans, root->fs_info);
895         BUG_ON(ret);
896         ret = __commit_transaction(trans, root);
897         BUG_ON(ret);
898         write_ctree_super(trans, root);
899         btrfs_free_transaction(root, trans);
900         btrfs_free_block_groups(root->fs_info);
901         if (root->node)
902                 free_extent_buffer(root->node);
903         if (root->fs_info->extent_root->node)
904                 free_extent_buffer(root->fs_info->extent_root->node);
905         if (root->fs_info->tree_root->node)
906                 free_extent_buffer(root->fs_info->tree_root->node);
907         free_extent_buffer(root->commit_root);
908
909         if (root->fs_info->chunk_root->node)
910                 free_extent_buffer(root->fs_info->chunk_root->node);
911         if (root->fs_info->dev_root->node)
912                 free_extent_buffer(root->fs_info->dev_root->node);
913         if (root->fs_info->csum_root->node)
914                 free_extent_buffer(root->fs_info->csum_root->node);
915
916         if (root->fs_info->log_root_tree) {
917                 if (root->fs_info->log_root_tree->node)
918                         free_extent_buffer(root->fs_info->log_root_tree->node);
919                 free(root->fs_info->log_root_tree);
920         }
921
922         close_all_devices(root->fs_info);
923         extent_io_tree_cleanup(&fs_info->extent_cache);
924         extent_io_tree_cleanup(&fs_info->free_space_cache);
925         extent_io_tree_cleanup(&fs_info->block_group_cache);
926         extent_io_tree_cleanup(&fs_info->pinned_extents);
927         extent_io_tree_cleanup(&fs_info->pending_del);
928         extent_io_tree_cleanup(&fs_info->extent_ins);
929
930         free(fs_info->tree_root);
931         free(fs_info->extent_root);
932         free(fs_info->fs_root);
933         free(fs_info->chunk_root);
934         free(fs_info->dev_root);
935         free(fs_info->csum_root);
936         free(fs_info);
937
938         return 0;
939 }
940
941 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
942                      struct extent_buffer *eb)
943 {
944         return clear_extent_buffer_dirty(eb);
945 }
946
947 int wait_on_tree_block_writeback(struct btrfs_root *root,
948                                  struct extent_buffer *eb)
949 {
950         return 0;
951 }
952
953 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
954 {
955         set_extent_buffer_dirty(eb);
956 }
957
958 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
959 {
960         int ret;
961
962         ret = extent_buffer_uptodate(buf);
963         if (!ret)
964                 return ret;
965
966         ret = verify_parent_transid(buf->tree, buf, parent_transid);
967         return !ret;
968 }
969
970 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
971 {
972         return set_extent_buffer_uptodate(eb);
973 }