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