Btrfs-progs: update rbtree libs
[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 #include "rbtree-utils.h"
38
39 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
40 {
41
42         struct btrfs_fs_devices *fs_devices;
43         int ret = 1;
44
45         if (buf->start != btrfs_header_bytenr(buf)) {
46                 printk("Check tree block failed, want=%Lu, have=%Lu\n",
47                        buf->start, btrfs_header_bytenr(buf));
48                 return ret;
49         }
50
51         fs_devices = root->fs_info->fs_devices;
52         while (fs_devices) {
53                 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
54                                           btrfs_header_fsid(),
55                                           BTRFS_FSID_SIZE)) {
56                         ret = 0;
57                         break;
58                 }
59                 fs_devices = fs_devices->seed;
60         }
61         return ret;
62 }
63
64 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
65 {
66         return crc32c(seed, data, len);
67 }
68
69 void btrfs_csum_final(u32 crc, char *result)
70 {
71         *(__le32 *)result = ~cpu_to_le32(crc);
72 }
73
74 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
75                                   int verify, int silent)
76 {
77         char *result;
78         u32 len;
79         u32 crc = ~(u32)0;
80
81         result = malloc(csum_size * sizeof(char));
82         if (!result)
83                 return 1;
84
85         len = buf->len - BTRFS_CSUM_SIZE;
86         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
87         btrfs_csum_final(crc, result);
88
89         if (verify) {
90                 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
91                         if (!silent)
92                                 printk("checksum verify failed on %llu found %08X wanted %08X\n",
93                                        (unsigned long long)buf->start,
94                                        *((u32 *)result),
95                                        *((u32*)(char *)buf->data));
96                         free(result);
97                         return 1;
98                 }
99         } else {
100                 write_extent_buffer(buf, result, 0, csum_size);
101         }
102         free(result);
103         return 0;
104 }
105
106 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
107 {
108         return __csum_tree_block_size(buf, csum_size, verify, 0);
109 }
110
111 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
112 {
113         return __csum_tree_block_size(buf, csum_size, 1, 1);
114 }
115
116 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
117                            int verify)
118 {
119         u16 csum_size =
120                 btrfs_super_csum_size(root->fs_info->super_copy);
121         return csum_tree_block_size(buf, csum_size, verify);
122 }
123
124 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
125                                             u64 bytenr, u32 blocksize)
126 {
127         return find_extent_buffer(&root->fs_info->extent_cache,
128                                   bytenr, blocksize);
129 }
130
131 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
132                                                  u64 bytenr, u32 blocksize)
133 {
134         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
135                                    blocksize);
136 }
137
138 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
139                           u64 parent_transid)
140 {
141         struct extent_buffer *eb;
142         u64 length;
143         struct btrfs_multi_bio *multi = NULL;
144         struct btrfs_device *device;
145
146         eb = btrfs_find_tree_block(root, bytenr, blocksize);
147         if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
148             !btrfs_map_block(&root->fs_info->mapping_tree, READ,
149                              bytenr, &length, &multi, 0, NULL)) {
150                 device = multi->stripes[0].dev;
151                 device->total_ios++;
152                 blocksize = min(blocksize, (u32)(64 * 1024));
153                 readahead(device->fd, multi->stripes[0].physical, blocksize);
154         }
155
156         free_extent_buffer(eb);
157         kfree(multi);
158 }
159
160 static int verify_parent_transid(struct extent_io_tree *io_tree,
161                                  struct extent_buffer *eb, u64 parent_transid,
162                                  int ignore)
163 {
164         int ret;
165
166         if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
167                 return 0;
168
169         if (extent_buffer_uptodate(eb) &&
170             btrfs_header_generation(eb) == parent_transid) {
171                 ret = 0;
172                 goto out;
173         }
174         printk("parent transid verify failed on %llu wanted %llu found %llu\n",
175                (unsigned long long)eb->start,
176                (unsigned long long)parent_transid,
177                (unsigned long long)btrfs_header_generation(eb));
178         if (ignore) {
179                 eb->flags |= EXTENT_BAD_TRANSID;
180                 printk("Ignoring transid failure\n");
181                 return 0;
182         }
183
184         ret = 1;
185 out:
186         clear_extent_buffer_uptodate(io_tree, eb);
187         return ret;
188
189 }
190
191
192 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
193 {
194         unsigned long offset = 0;
195         struct btrfs_multi_bio *multi = NULL;
196         struct btrfs_device *device;
197         int ret = 0;
198         u64 read_len;
199         unsigned long bytes_left = eb->len;
200
201         while (bytes_left) {
202                 read_len = bytes_left;
203                 device = NULL;
204
205                 if (!info->on_restoring &&
206                     eb->start != BTRFS_SUPER_INFO_OFFSET) {
207                         ret = btrfs_map_block(&info->mapping_tree, READ,
208                                               eb->start + offset, &read_len, &multi,
209                                               mirror, NULL);
210                         if (ret) {
211                                 printk("Couldn't map the block %Lu\n", eb->start + offset);
212                                 kfree(multi);
213                                 return -EIO;
214                         }
215                         device = multi->stripes[0].dev;
216
217                         if (device->fd == 0) {
218                                 kfree(multi);
219                                 return -EIO;
220                         }
221
222                         eb->fd = device->fd;
223                         device->total_ios++;
224                         eb->dev_bytenr = multi->stripes[0].physical;
225                         kfree(multi);
226                         multi = NULL;
227                 } else {
228                         /* special case for restore metadump */
229                         list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
230                                 if (device->devid == 1)
231                                         break;
232                         }
233
234                         eb->fd = device->fd;
235                         eb->dev_bytenr = eb->start;
236                         device->total_ios++;
237                 }
238
239                 if (read_len > bytes_left)
240                         read_len = bytes_left;
241
242                 ret = read_extent_from_disk(eb, offset, read_len);
243                 if (ret)
244                         return -EIO;
245                 offset += read_len;
246                 bytes_left -= read_len;
247         }
248         return 0;
249 }
250
251 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
252                                      u32 blocksize, u64 parent_transid)
253 {
254         int ret;
255         struct extent_buffer *eb;
256         u64 best_transid = 0;
257         int mirror_num = 0;
258         int good_mirror = 0;
259         int num_copies;
260         int ignore = 0;
261
262         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
263         if (!eb)
264                 return NULL;
265
266         if (btrfs_buffer_uptodate(eb, parent_transid))
267                 return eb;
268
269         while (1) {
270                 ret = read_whole_eb(root->fs_info, eb, mirror_num);
271                 if (ret == 0 && check_tree_block(root, eb) == 0 &&
272                     csum_tree_block(root, eb, 1) == 0 &&
273                     verify_parent_transid(eb->tree, eb, parent_transid, ignore)
274                     == 0) {
275                         if (eb->flags & EXTENT_BAD_TRANSID &&
276                             list_empty(&eb->recow)) {
277                                 list_add_tail(&eb->recow,
278                                               &root->fs_info->recow_ebs);
279                                 eb->refs++;
280                         }
281                         btrfs_set_buffer_uptodate(eb);
282                         return eb;
283                 }
284                 if (ignore) {
285                         if (check_tree_block(root, eb))
286                                 printk("read block failed check_tree_block\n");
287                         else
288                                 printk("Csum didn't match\n");
289                         break;
290                 }
291                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
292                                               eb->start, eb->len);
293                 if (num_copies == 1) {
294                         ignore = 1;
295                         continue;
296                 }
297                 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
298                         best_transid = btrfs_header_generation(eb);
299                         good_mirror = mirror_num;
300                 }
301                 mirror_num++;
302                 if (mirror_num > num_copies) {
303                         mirror_num = good_mirror;
304                         ignore = 1;
305                         continue;
306                 }
307         }
308         free_extent_buffer(eb);
309         return NULL;
310 }
311
312 int write_and_map_eb(struct btrfs_trans_handle *trans,
313                      struct btrfs_root *root,
314                      struct extent_buffer *eb)
315 {
316         int ret;
317         int dev_nr;
318         u64 length;
319         u64 *raid_map = NULL;
320         struct btrfs_multi_bio *multi = NULL;
321
322         dev_nr = 0;
323         length = eb->len;
324         ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
325                               eb->start, &length, &multi, 0, &raid_map);
326
327         if (raid_map) {
328                 ret = write_raid56_with_parity(root->fs_info, eb, multi,
329                                                length, raid_map);
330                 BUG_ON(ret);
331         } else while (dev_nr < multi->num_stripes) {
332                 BUG_ON(ret);
333                 eb->fd = multi->stripes[dev_nr].dev->fd;
334                 eb->dev_bytenr = multi->stripes[dev_nr].physical;
335                 multi->stripes[dev_nr].dev->total_ios++;
336                 dev_nr++;
337                 ret = write_extent_to_disk(eb);
338                 BUG_ON(ret);
339         }
340         kfree(multi);
341         return 0;
342 }
343
344 static int write_tree_block(struct btrfs_trans_handle *trans,
345                      struct btrfs_root *root,
346                      struct extent_buffer *eb)
347 {
348         if (check_tree_block(root, eb))
349                 BUG();
350
351         if (!btrfs_buffer_uptodate(eb, trans->transid))
352                 BUG();
353
354         btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
355         csum_tree_block(root, eb, 0);
356
357         return write_and_map_eb(trans, root, eb);
358 }
359
360 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
361                         u32 stripesize, struct btrfs_root *root,
362                         struct btrfs_fs_info *fs_info, u64 objectid)
363 {
364         root->node = NULL;
365         root->commit_root = NULL;
366         root->sectorsize = sectorsize;
367         root->nodesize = nodesize;
368         root->leafsize = leafsize;
369         root->stripesize = stripesize;
370         root->ref_cows = 0;
371         root->track_dirty = 0;
372
373         root->fs_info = fs_info;
374         root->objectid = objectid;
375         root->last_trans = 0;
376         root->highest_inode = 0;
377         root->last_inode_alloc = 0;
378
379         INIT_LIST_HEAD(&root->dirty_list);
380         memset(&root->root_key, 0, sizeof(root->root_key));
381         memset(&root->root_item, 0, sizeof(root->root_item));
382         root->root_key.objectid = objectid;
383         return 0;
384 }
385
386 static int update_cowonly_root(struct btrfs_trans_handle *trans,
387                                struct btrfs_root *root)
388 {
389         int ret;
390         u64 old_root_bytenr;
391         struct btrfs_root *tree_root = root->fs_info->tree_root;
392
393         btrfs_write_dirty_block_groups(trans, root);
394         while(1) {
395                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
396                 if (old_root_bytenr == root->node->start)
397                         break;
398                 btrfs_set_root_bytenr(&root->root_item,
399                                        root->node->start);
400                 btrfs_set_root_generation(&root->root_item,
401                                           trans->transid);
402                 root->root_item.level = btrfs_header_level(root->node);
403                 ret = btrfs_update_root(trans, tree_root,
404                                         &root->root_key,
405                                         &root->root_item);
406                 BUG_ON(ret);
407                 btrfs_write_dirty_block_groups(trans, root);
408         }
409         return 0;
410 }
411
412 static int commit_tree_roots(struct btrfs_trans_handle *trans,
413                              struct btrfs_fs_info *fs_info)
414 {
415         struct btrfs_root *root;
416         struct list_head *next;
417         struct extent_buffer *eb;
418         int ret;
419
420         if (fs_info->readonly)
421                 return 0;
422
423         eb = fs_info->tree_root->node;
424         extent_buffer_get(eb);
425         ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
426         free_extent_buffer(eb);
427         if (ret)
428                 return ret;
429
430         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
431                 next = fs_info->dirty_cowonly_roots.next;
432                 list_del_init(next);
433                 root = list_entry(next, struct btrfs_root, dirty_list);
434                 update_cowonly_root(trans, root);
435                 free_extent_buffer(root->commit_root);
436                 root->commit_root = NULL;
437         }
438
439         return 0;
440 }
441
442 static int __commit_transaction(struct btrfs_trans_handle *trans,
443                                 struct btrfs_root *root)
444 {
445         u64 start;
446         u64 end;
447         struct extent_buffer *eb;
448         struct extent_io_tree *tree = &root->fs_info->extent_cache;
449         int ret;
450
451         while(1) {
452                 ret = find_first_extent_bit(tree, 0, &start, &end,
453                                             EXTENT_DIRTY);
454                 if (ret)
455                         break;
456                 while(start <= end) {
457                         eb = find_first_extent_buffer(tree, start);
458                         BUG_ON(!eb || eb->start != start);
459                         ret = write_tree_block(trans, root, eb);
460                         BUG_ON(ret);
461                         start += eb->len;
462                         clear_extent_buffer_dirty(eb);
463                         free_extent_buffer(eb);
464                 }
465         }
466         return 0;
467 }
468
469 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
470                              struct btrfs_root *root)
471 {
472         u64 transid = trans->transid;
473         int ret = 0;
474         struct btrfs_fs_info *fs_info = root->fs_info;
475
476         if (root->commit_root == root->node)
477                 goto commit_tree;
478
479         free_extent_buffer(root->commit_root);
480         root->commit_root = NULL;
481
482         btrfs_set_root_bytenr(&root->root_item, root->node->start);
483         btrfs_set_root_generation(&root->root_item, trans->transid);
484         root->root_item.level = btrfs_header_level(root->node);
485         ret = btrfs_update_root(trans, root->fs_info->tree_root,
486                                 &root->root_key, &root->root_item);
487         BUG_ON(ret);
488 commit_tree:
489         ret = commit_tree_roots(trans, fs_info);
490         BUG_ON(ret);
491         ret = __commit_transaction(trans, root);
492         BUG_ON(ret);
493         write_ctree_super(trans, root);
494         btrfs_finish_extent_commit(trans, fs_info->extent_root,
495                                    &fs_info->pinned_extents);
496         btrfs_free_transaction(root, trans);
497         free_extent_buffer(root->commit_root);
498         root->commit_root = NULL;
499         fs_info->running_transaction = NULL;
500         fs_info->last_trans_committed = transid;
501         return 0;
502 }
503
504 static int find_and_setup_root(struct btrfs_root *tree_root,
505                                struct btrfs_fs_info *fs_info,
506                                u64 objectid, struct btrfs_root *root)
507 {
508         int ret;
509         u32 blocksize;
510         u64 generation;
511
512         __setup_root(tree_root->nodesize, tree_root->leafsize,
513                      tree_root->sectorsize, tree_root->stripesize,
514                      root, fs_info, objectid);
515         ret = btrfs_find_last_root(tree_root, objectid,
516                                    &root->root_item, &root->root_key);
517         if (ret)
518                 return ret;
519
520         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
521         generation = btrfs_root_generation(&root->root_item);
522         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
523                                      blocksize, generation);
524         if (!extent_buffer_uptodate(root->node))
525                 return -EIO;
526
527         return 0;
528 }
529
530 static int find_and_setup_log_root(struct btrfs_root *tree_root,
531                                struct btrfs_fs_info *fs_info,
532                                struct btrfs_super_block *disk_super)
533 {
534         u32 blocksize;
535         u64 blocknr = btrfs_super_log_root(disk_super);
536         struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
537
538         if (!log_root)
539                 return -ENOMEM;
540
541         if (blocknr == 0) {
542                 free(log_root);
543                 return 0;
544         }
545
546         blocksize = btrfs_level_size(tree_root,
547                              btrfs_super_log_root_level(disk_super));
548
549         __setup_root(tree_root->nodesize, tree_root->leafsize,
550                      tree_root->sectorsize, tree_root->stripesize,
551                      log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
552
553         log_root->node = read_tree_block(tree_root, blocknr,
554                                      blocksize,
555                                      btrfs_super_generation(disk_super) + 1);
556
557         fs_info->log_root_tree = log_root;
558
559         if (!extent_buffer_uptodate(log_root->node)) {
560                 free_extent_buffer(log_root->node);
561                 free(log_root);
562                 fs_info->log_root_tree = NULL;
563                 return -EIO;
564         }
565
566         return 0;
567 }
568
569 int btrfs_free_fs_root(struct btrfs_root *root)
570 {
571         if (root->node)
572                 free_extent_buffer(root->node);
573         if (root->commit_root)
574                 free_extent_buffer(root->commit_root);
575         kfree(root);
576         return 0;
577 }
578
579 static void __free_fs_root(struct rb_node *node)
580 {
581         struct btrfs_root *root;
582
583         root = container_of(node, struct btrfs_root, rb_node);
584         btrfs_free_fs_root(root);
585 }
586
587 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
588
589 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
590                                                struct btrfs_key *location)
591 {
592         struct btrfs_root *root;
593         struct btrfs_root *tree_root = fs_info->tree_root;
594         struct btrfs_path *path;
595         struct extent_buffer *l;
596         u64 generation;
597         u32 blocksize;
598         int ret = 0;
599
600         root = malloc(sizeof(*root));
601         if (!root)
602                 return ERR_PTR(-ENOMEM);
603         memset(root, 0, sizeof(*root));
604         if (location->offset == (u64)-1) {
605                 ret = find_and_setup_root(tree_root, fs_info,
606                                           location->objectid, root);
607                 if (ret) {
608                         free(root);
609                         return ERR_PTR(ret);
610                 }
611                 goto insert;
612         }
613
614         __setup_root(tree_root->nodesize, tree_root->leafsize,
615                      tree_root->sectorsize, tree_root->stripesize,
616                      root, fs_info, location->objectid);
617
618         path = btrfs_alloc_path();
619         BUG_ON(!path);
620         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
621         if (ret != 0) {
622                 if (ret > 0)
623                         ret = -ENOENT;
624                 goto out;
625         }
626         l = path->nodes[0];
627         read_extent_buffer(l, &root->root_item,
628                btrfs_item_ptr_offset(l, path->slots[0]),
629                sizeof(root->root_item));
630         memcpy(&root->root_key, location, sizeof(*location));
631         ret = 0;
632 out:
633         btrfs_free_path(path);
634         if (ret) {
635                 free(root);
636                 return ERR_PTR(ret);
637         }
638         generation = btrfs_root_generation(&root->root_item);
639         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
640         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
641                                      blocksize, generation);
642         if (!root->node) {
643                 free(root);
644                 return ERR_PTR(-EIO);
645         }
646 insert:
647         root->ref_cows = 1;
648         return root;
649 }
650
651 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
652                                             void *data)
653 {
654         u64 objectid = *((u64 *)data);
655         struct btrfs_root *root;
656
657         root = rb_entry(node, struct btrfs_root, rb_node);
658         if (objectid > root->objectid)
659                 return 1;
660         else if (objectid < root->objectid)
661                 return -1;
662         else
663                 return 0;
664 }
665
666 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
667                                         struct rb_node *node2)
668 {
669         struct btrfs_root *root;
670
671         root = rb_entry(node2, struct btrfs_root, rb_node);
672         return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
673 }
674
675 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
676                                       struct btrfs_key *location)
677 {
678         struct btrfs_root *root;
679         struct rb_node *node;
680         int ret;
681         u64 objectid = location->objectid;
682
683         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
684                 return fs_info->tree_root;
685         if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
686                 return fs_info->extent_root;
687         if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
688                 return fs_info->chunk_root;
689         if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
690                 return fs_info->dev_root;
691         if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
692                 return fs_info->csum_root;
693         if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
694                 return fs_info->csum_root;
695
696         BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
697                location->offset != (u64)-1);
698
699         node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
700                          btrfs_fs_roots_compare_objectids, NULL);
701         if (node)
702                 return container_of(node, struct btrfs_root, rb_node);
703
704         root = btrfs_read_fs_root_no_cache(fs_info, location);
705         if (IS_ERR(root))
706                 return root;
707
708         ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
709                         btrfs_fs_roots_compare_roots);
710         BUG_ON(ret);
711         return root;
712 }
713
714 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
715 {
716         free(fs_info->tree_root);
717         free(fs_info->extent_root);
718         free(fs_info->chunk_root);
719         free(fs_info->dev_root);
720         free(fs_info->csum_root);
721         free(fs_info->quota_root);
722         free(fs_info->super_copy);
723         free(fs_info->log_root_tree);
724         free(fs_info);
725 }
726
727 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
728 {
729         struct btrfs_fs_info *fs_info;
730
731         fs_info = malloc(sizeof(struct btrfs_fs_info));
732         if (!fs_info)
733                 return NULL;
734
735         memset(fs_info, 0, sizeof(struct btrfs_fs_info));
736
737         fs_info->tree_root = malloc(sizeof(struct btrfs_root));
738         fs_info->extent_root = malloc(sizeof(struct btrfs_root));
739         fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
740         fs_info->dev_root = malloc(sizeof(struct btrfs_root));
741         fs_info->csum_root = malloc(sizeof(struct btrfs_root));
742         fs_info->quota_root = malloc(sizeof(struct btrfs_root));
743         fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
744
745         if (!fs_info->tree_root || !fs_info->extent_root ||
746             !fs_info->chunk_root || !fs_info->dev_root ||
747             !fs_info->csum_root || !fs_info->quota_root ||
748             !fs_info->super_copy)
749                 goto free_all;
750
751         memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
752         memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
753         memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
754         memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
755         memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
756         memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
757         memset(fs_info->quota_root, 0, sizeof(struct btrfs_root));
758
759         extent_io_tree_init(&fs_info->extent_cache);
760         extent_io_tree_init(&fs_info->free_space_cache);
761         extent_io_tree_init(&fs_info->block_group_cache);
762         extent_io_tree_init(&fs_info->pinned_extents);
763         extent_io_tree_init(&fs_info->pending_del);
764         extent_io_tree_init(&fs_info->extent_ins);
765         fs_info->fs_root_tree = RB_ROOT;
766         cache_tree_init(&fs_info->mapping_tree.cache_tree);
767
768         mutex_init(&fs_info->fs_mutex);
769         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
770         INIT_LIST_HEAD(&fs_info->space_info);
771         INIT_LIST_HEAD(&fs_info->recow_ebs);
772
773         if (!writable)
774                 fs_info->readonly = 1;
775
776         fs_info->super_bytenr = sb_bytenr;
777         fs_info->data_alloc_profile = (u64)-1;
778         fs_info->metadata_alloc_profile = (u64)-1;
779         fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
780         return fs_info;
781 free_all:
782         btrfs_free_fs_info(fs_info);
783         return NULL;
784 }
785
786 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
787 {
788         u64 features;
789
790         features = btrfs_super_incompat_flags(sb) &
791                    ~BTRFS_FEATURE_INCOMPAT_SUPP;
792         if (features) {
793                 printk("couldn't open because of unsupported "
794                        "option features (%Lx).\n",
795                        (unsigned long long)features);
796                 return -ENOTSUP;
797         }
798
799         features = btrfs_super_incompat_flags(sb);
800         if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
801                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
802                 btrfs_set_super_incompat_flags(sb, features);
803         }
804
805         features = btrfs_super_compat_ro_flags(sb) &
806                 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
807         if (writable && features) {
808                 printk("couldn't open RDWR because of unsupported "
809                        "option features (%Lx).\n",
810                        (unsigned long long)features);
811                 return -ENOTSUP;
812         }
813         return 0;
814 }
815
816 static int find_best_backup_root(struct btrfs_super_block *super)
817 {
818         struct btrfs_root_backup *backup;
819         u64 orig_gen = btrfs_super_generation(super);
820         u64 gen = 0;
821         int best_index = 0;
822         int i;
823
824         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
825                 backup = super->super_roots + i;
826                 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
827                     btrfs_backup_tree_root_gen(backup) > gen) {
828                         best_index = i;
829                         gen = btrfs_backup_tree_root_gen(backup);
830                 }
831         }
832         return best_index;
833 }
834
835 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
836                           enum btrfs_open_ctree_flags flags)
837 {
838         struct btrfs_super_block *sb = fs_info->super_copy;
839         struct btrfs_root *root;
840         struct btrfs_key key;
841         u32 sectorsize;
842         u32 nodesize;
843         u32 leafsize;
844         u32 stripesize;
845         u64 generation;
846         u32 blocksize;
847         int ret;
848
849         nodesize = btrfs_super_nodesize(sb);
850         leafsize = btrfs_super_leafsize(sb);
851         sectorsize = btrfs_super_sectorsize(sb);
852         stripesize = btrfs_super_stripesize(sb);
853
854         root = fs_info->tree_root;
855         __setup_root(nodesize, leafsize, sectorsize, stripesize,
856                      root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
857         blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
858         generation = btrfs_super_generation(sb);
859
860         if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
861                 root_tree_bytenr = btrfs_super_root(sb);
862         } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
863                 struct btrfs_root_backup *backup;
864                 int index = find_best_backup_root(sb);
865                 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
866                         fprintf(stderr, "Invalid backup root number\n");
867                         return -EIO;
868                 }
869                 backup = fs_info->super_copy->super_roots + index;
870                 root_tree_bytenr = btrfs_backup_tree_root(backup);
871                 generation = btrfs_backup_tree_root_gen(backup);
872         }
873
874         root->node = read_tree_block(root, root_tree_bytenr, blocksize,
875                                      generation);
876         if (!extent_buffer_uptodate(root->node)) {
877                 fprintf(stderr, "Couldn't read tree root\n");
878                 return -EIO;
879         }
880
881         ret = find_and_setup_root(root, fs_info, BTRFS_EXTENT_TREE_OBJECTID,
882                                   fs_info->extent_root);
883         if (ret) {
884                 printk("Couldn't setup extent tree\n");
885                 if (!(flags & OPEN_CTREE_PARTIAL))
886                         return -EIO;
887                 /* Need a blank node here just so we don't screw up in the
888                  * million of places that assume a root has a valid ->node
889                  */
890                 fs_info->extent_root->node =
891                         btrfs_find_create_tree_block(fs_info->extent_root, 0,
892                                                      leafsize);
893                 if (!fs_info->extent_root->node)
894                         return -ENOMEM;
895                 clear_extent_buffer_uptodate(NULL, fs_info->extent_root->node);
896         }
897         fs_info->extent_root->track_dirty = 1;
898
899         ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
900                                   fs_info->dev_root);
901         if (ret) {
902                 printk("Couldn't setup device tree\n");
903                 return -EIO;
904         }
905         fs_info->dev_root->track_dirty = 1;
906
907         ret = find_and_setup_root(root, fs_info, BTRFS_CSUM_TREE_OBJECTID,
908                                   fs_info->csum_root);
909         if (ret) {
910                 printk("Couldn't setup csum tree\n");
911                 if (!(flags & OPEN_CTREE_PARTIAL))
912                         return -EIO;
913                 /* do the same thing as extent tree rebuilding */
914                 fs_info->csum_root->node =
915                         btrfs_find_create_tree_block(fs_info->extent_root, 0,
916                                                      leafsize);
917                 if (!fs_info->csum_root->node)
918                         return -ENOMEM;
919                 clear_extent_buffer_uptodate(NULL, fs_info->csum_root->node);
920         }
921         fs_info->csum_root->track_dirty = 1;
922
923         ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
924                                   fs_info->quota_root);
925         if (ret == 0)
926                 fs_info->quota_enabled = 1;
927
928         ret = find_and_setup_log_root(root, fs_info, sb);
929         if (ret) {
930                 printk("Couldn't setup log root tree\n");
931                 return -EIO;
932         }
933
934         fs_info->generation = generation;
935         fs_info->last_trans_committed = generation;
936         if (extent_buffer_uptodate(fs_info->extent_root->node) &&
937             !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
938                 btrfs_read_block_groups(fs_info->tree_root);
939
940         key.objectid = BTRFS_FS_TREE_OBJECTID;
941         key.type = BTRFS_ROOT_ITEM_KEY;
942         key.offset = (u64)-1;
943         fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
944
945         if (IS_ERR(fs_info->fs_root))
946                 return -EIO;
947         return 0;
948 }
949
950 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
951 {
952         if (fs_info->quota_root)
953                 free_extent_buffer(fs_info->quota_root->node);
954         if (fs_info->csum_root)
955                 free_extent_buffer(fs_info->csum_root->node);
956         if (fs_info->dev_root)
957                 free_extent_buffer(fs_info->dev_root->node);
958         if (fs_info->extent_root)
959                 free_extent_buffer(fs_info->extent_root->node);
960         if (fs_info->tree_root)
961                 free_extent_buffer(fs_info->tree_root->node);
962         if (fs_info->log_root_tree)
963                 free_extent_buffer(fs_info->log_root_tree->node);
964         if (fs_info->chunk_root)
965                 free_extent_buffer(fs_info->chunk_root->node);
966 }
967
968 static void free_map_lookup(struct cache_extent *ce)
969 {
970         struct map_lookup *map;
971
972         map = container_of(ce, struct map_lookup, ce);
973         kfree(map);
974 }
975
976 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
977
978 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
979 {
980         while (!list_empty(&fs_info->recow_ebs)) {
981                 struct extent_buffer *eb;
982                 eb = list_first_entry(&fs_info->recow_ebs,
983                                       struct extent_buffer, recow);
984                 list_del_init(&eb->recow);
985                 free_extent_buffer(eb);
986         }
987         free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
988         extent_io_tree_cleanup(&fs_info->extent_cache);
989         extent_io_tree_cleanup(&fs_info->free_space_cache);
990         extent_io_tree_cleanup(&fs_info->block_group_cache);
991         extent_io_tree_cleanup(&fs_info->pinned_extents);
992         extent_io_tree_cleanup(&fs_info->pending_del);
993         extent_io_tree_cleanup(&fs_info->extent_ins);
994 }
995
996 int btrfs_scan_fs_devices(int fd, const char *path,
997                           struct btrfs_fs_devices **fs_devices,
998                           u64 sb_bytenr, int run_ioctl, int super_recover)
999 {
1000         u64 total_devs;
1001         int ret;
1002         if (!sb_bytenr)
1003                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1004
1005         ret = btrfs_scan_one_device(fd, path, fs_devices,
1006                                     &total_devs, sb_bytenr, super_recover);
1007         if (ret) {
1008                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1009                 return ret;
1010         }
1011
1012         if (total_devs != 1) {
1013                 ret = btrfs_scan_lblkid(run_ioctl);
1014                 if (ret)
1015                         return ret;
1016         }
1017         return 0;
1018 }
1019
1020 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
1021 {
1022         struct btrfs_super_block *sb = fs_info->super_copy;
1023         u32 sectorsize;
1024         u32 nodesize;
1025         u32 leafsize;
1026         u32 blocksize;
1027         u32 stripesize;
1028         u64 generation;
1029         int ret;
1030
1031         nodesize = btrfs_super_nodesize(sb);
1032         leafsize = btrfs_super_leafsize(sb);
1033         sectorsize = btrfs_super_sectorsize(sb);
1034         stripesize = btrfs_super_stripesize(sb);
1035
1036         __setup_root(nodesize, leafsize, sectorsize, stripesize,
1037                      fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1038
1039         ret = btrfs_read_sys_array(fs_info->chunk_root);
1040         if (ret)
1041                 return ret;
1042
1043         blocksize = btrfs_level_size(fs_info->chunk_root,
1044                                      btrfs_super_chunk_root_level(sb));
1045         generation = btrfs_super_chunk_root_generation(sb);
1046
1047         fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1048                                                     btrfs_super_chunk_root(sb),
1049                                                     blocksize, generation);
1050         if (!fs_info->chunk_root->node ||
1051             !extent_buffer_uptodate(fs_info->chunk_root->node)) {
1052                 fprintf(stderr, "Couldn't read chunk root\n");
1053                 return -EIO;
1054         }
1055
1056         if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1057                 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1058                 if (ret) {
1059                         fprintf(stderr, "Couldn't read chunk tree\n");
1060                         return ret;
1061                 }
1062         }
1063         return 0;
1064 }
1065
1066 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1067                                              u64 sb_bytenr,
1068                                              u64 root_tree_bytenr,
1069                                              enum btrfs_open_ctree_flags flags)
1070 {
1071         struct btrfs_fs_info *fs_info;
1072         struct btrfs_super_block *disk_super;
1073         struct btrfs_fs_devices *fs_devices = NULL;
1074         struct extent_buffer *eb;
1075         int ret;
1076         int oflags;
1077
1078         if (sb_bytenr == 0)
1079                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1080
1081         /* try to drop all the caches */
1082         if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1083                 fprintf(stderr, "Warning, could not drop caches\n");
1084
1085         fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1086         if (!fs_info) {
1087                 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1088                 return NULL;
1089         }
1090         if (flags & OPEN_CTREE_RESTORE)
1091                 fs_info->on_restoring = 1;
1092
1093         ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1094                                     !(flags & OPEN_CTREE_RECOVER_SUPER),
1095                                     (flags & OPEN_CTREE_RECOVER_SUPER));
1096         if (ret)
1097                 goto out;
1098
1099         fs_info->fs_devices = fs_devices;
1100         if (flags & OPEN_CTREE_WRITES)
1101                 oflags = O_RDWR;
1102         else
1103                 oflags = O_RDONLY;
1104
1105         if (flags & OPEN_CTREE_EXCLUSIVE)
1106                 oflags |= O_EXCL;
1107
1108         ret = btrfs_open_devices(fs_devices, oflags);
1109         if (ret)
1110                 goto out;
1111
1112         disk_super = fs_info->super_copy;
1113         if (!(flags & OPEN_CTREE_RECOVER_SUPER))
1114                 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1115                                            disk_super, sb_bytenr, 1);
1116         else
1117                 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
1118         if (ret) {
1119                 printk("No valid btrfs found\n");
1120                 goto out_devices;
1121         }
1122
1123         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1124
1125         ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1126                                            flags & OPEN_CTREE_WRITES);
1127         if (ret)
1128                 goto out_devices;
1129
1130         ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1131         if (ret)
1132                 goto out_chunk;
1133
1134         eb = fs_info->chunk_root->node;
1135         read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1136                            btrfs_header_chunk_tree_uuid(eb),
1137                            BTRFS_UUID_SIZE);
1138
1139         ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1140         if (ret)
1141                 goto out_chunk;
1142
1143         return fs_info;
1144
1145 out_chunk:
1146         btrfs_release_all_roots(fs_info);
1147         btrfs_cleanup_all_caches(fs_info);
1148 out_devices:
1149         btrfs_close_devices(fs_devices);
1150 out:
1151         btrfs_free_fs_info(fs_info);
1152         return NULL;
1153 }
1154
1155 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1156                                          u64 sb_bytenr, u64 root_tree_bytenr,
1157                                          enum btrfs_open_ctree_flags flags)
1158 {
1159         int fp;
1160         struct btrfs_fs_info *info;
1161         int oflags = O_CREAT | O_RDWR;
1162
1163         if (!(flags & OPEN_CTREE_WRITES))
1164                 oflags = O_RDONLY;
1165
1166         fp = open(filename, oflags, 0600);
1167         if (fp < 0) {
1168                 fprintf (stderr, "Could not open %s\n", filename);
1169                 return NULL;
1170         }
1171         info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1172                                flags);
1173         close(fp);
1174         return info;
1175 }
1176
1177 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1178                               enum btrfs_open_ctree_flags flags)
1179 {
1180         struct btrfs_fs_info *info;
1181
1182         info = open_ctree_fs_info(filename, sb_bytenr, 0, flags);
1183         if (!info)
1184                 return NULL;
1185         return info->fs_root;
1186 }
1187
1188 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1189                                  enum btrfs_open_ctree_flags flags)
1190 {
1191         struct btrfs_fs_info *info;
1192         info = __open_ctree_fd(fp, path, sb_bytenr, 0, flags);
1193         if (!info)
1194                 return NULL;
1195         return info->fs_root;
1196 }
1197
1198 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1199                          int super_recover)
1200 {
1201         u8 fsid[BTRFS_FSID_SIZE];
1202         int fsid_is_initialized = 0;
1203         struct btrfs_super_block buf;
1204         int i;
1205         int ret;
1206         int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
1207         u64 transid = 0;
1208         u64 bytenr;
1209
1210         if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1211                 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1212                 if (ret < sizeof(buf))
1213                         return -1;
1214
1215                 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1216                     btrfs_super_magic(&buf) != BTRFS_MAGIC)
1217                         return -1;
1218
1219                 memcpy(sb, &buf, sizeof(*sb));
1220                 return 0;
1221         }
1222
1223         /*
1224         * we would like to check all the supers, but that would make
1225         * a btrfs mount succeed after a mkfs from a different FS.
1226         * So, we need to add a special mount option to scan for
1227         * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1228         */
1229
1230         for (i = 0; i < max_super; i++) {
1231                 bytenr = btrfs_sb_offset(i);
1232                 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1233                 if (ret < sizeof(buf))
1234                         break;
1235
1236                 if (btrfs_super_bytenr(&buf) != bytenr )
1237                         continue;
1238                 /* if magic is NULL, the device was removed */
1239                 if (btrfs_super_magic(&buf) == 0 && i == 0)
1240                         return -1;
1241                 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1242                         continue;
1243
1244                 if (!fsid_is_initialized) {
1245                         memcpy(fsid, buf.fsid, sizeof(fsid));
1246                         fsid_is_initialized = 1;
1247                 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1248                         /*
1249                          * the superblocks (the original one and
1250                          * its backups) contain data of different
1251                          * filesystems -> the super cannot be trusted
1252                          */
1253                         continue;
1254                 }
1255
1256                 if (btrfs_super_generation(&buf) > transid) {
1257                         memcpy(sb, &buf, sizeof(*sb));
1258                         transid = btrfs_super_generation(&buf);
1259                 }
1260         }
1261
1262         return transid > 0 ? 0 : -1;
1263 }
1264
1265 static int write_dev_supers(struct btrfs_root *root,
1266                             struct btrfs_super_block *sb,
1267                             struct btrfs_device *device)
1268 {
1269         u64 bytenr;
1270         u32 crc;
1271         int i, ret;
1272
1273         if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1274                 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1275                 crc = ~(u32)0;
1276                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1277                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1278                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1279
1280                 /*
1281                  * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1282                  * zero filled, we can use it directly
1283                  */
1284                 ret = pwrite64(device->fd, root->fs_info->super_copy,
1285                                 BTRFS_SUPER_INFO_SIZE,
1286                                 root->fs_info->super_bytenr);
1287                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1288                 return 0;
1289         }
1290
1291         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1292                 bytenr = btrfs_sb_offset(i);
1293                 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1294                         break;
1295
1296                 btrfs_set_super_bytenr(sb, bytenr);
1297
1298                 crc = ~(u32)0;
1299                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1300                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1301                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1302
1303                 /*
1304                  * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1305                  * zero filled, we can use it directly
1306                  */
1307                 ret = pwrite64(device->fd, root->fs_info->super_copy,
1308                                 BTRFS_SUPER_INFO_SIZE, bytenr);
1309                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1310         }
1311
1312         return 0;
1313 }
1314
1315 int write_all_supers(struct btrfs_root *root)
1316 {
1317         struct list_head *cur;
1318         struct list_head *head = &root->fs_info->fs_devices->devices;
1319         struct btrfs_device *dev;
1320         struct btrfs_super_block *sb;
1321         struct btrfs_dev_item *dev_item;
1322         int ret;
1323         u64 flags;
1324
1325         sb = root->fs_info->super_copy;
1326         dev_item = &sb->dev_item;
1327         list_for_each(cur, head) {
1328                 dev = list_entry(cur, struct btrfs_device, dev_list);
1329                 if (!dev->writeable)
1330                         continue;
1331
1332                 btrfs_set_stack_device_generation(dev_item, 0);
1333                 btrfs_set_stack_device_type(dev_item, dev->type);
1334                 btrfs_set_stack_device_id(dev_item, dev->devid);
1335                 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1336                 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1337                 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1338                 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1339                 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1340                 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1341                 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1342
1343                 flags = btrfs_super_flags(sb);
1344                 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1345
1346                 ret = write_dev_supers(root, sb, dev);
1347                 BUG_ON(ret);
1348         }
1349         return 0;
1350 }
1351
1352 int write_ctree_super(struct btrfs_trans_handle *trans,
1353                       struct btrfs_root *root)
1354 {
1355         int ret;
1356         struct btrfs_root *tree_root = root->fs_info->tree_root;
1357         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1358
1359         if (root->fs_info->readonly)
1360                 return 0;
1361
1362         btrfs_set_super_generation(root->fs_info->super_copy,
1363                                    trans->transid);
1364         btrfs_set_super_root(root->fs_info->super_copy,
1365                              tree_root->node->start);
1366         btrfs_set_super_root_level(root->fs_info->super_copy,
1367                                    btrfs_header_level(tree_root->node));
1368         btrfs_set_super_chunk_root(root->fs_info->super_copy,
1369                                    chunk_root->node->start);
1370         btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1371                                          btrfs_header_level(chunk_root->node));
1372         btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1373                                 btrfs_header_generation(chunk_root->node));
1374
1375         ret = write_all_supers(root);
1376         if (ret)
1377                 fprintf(stderr, "failed to write new super block err %d\n", ret);
1378         return ret;
1379 }
1380
1381 int close_ctree(struct btrfs_root *root)
1382 {
1383         int ret;
1384         struct btrfs_trans_handle *trans;
1385         struct btrfs_fs_info *fs_info = root->fs_info;
1386
1387         if (fs_info->last_trans_committed !=
1388             fs_info->generation) {
1389                 trans = btrfs_start_transaction(root, 1);
1390                 btrfs_commit_transaction(trans, root);
1391                 trans = btrfs_start_transaction(root, 1);
1392                 ret = commit_tree_roots(trans, fs_info);
1393                 BUG_ON(ret);
1394                 ret = __commit_transaction(trans, root);
1395                 BUG_ON(ret);
1396                 write_ctree_super(trans, root);
1397                 btrfs_free_transaction(root, trans);
1398         }
1399         btrfs_free_block_groups(fs_info);
1400
1401         free_fs_roots_tree(&fs_info->fs_root_tree);
1402
1403         btrfs_release_all_roots(fs_info);
1404         btrfs_close_devices(fs_info->fs_devices);
1405         btrfs_cleanup_all_caches(fs_info);
1406         btrfs_free_fs_info(fs_info);
1407         return 0;
1408 }
1409
1410 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1411                      struct extent_buffer *eb)
1412 {
1413         return clear_extent_buffer_dirty(eb);
1414 }
1415
1416 int wait_on_tree_block_writeback(struct btrfs_root *root,
1417                                  struct extent_buffer *eb)
1418 {
1419         return 0;
1420 }
1421
1422 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1423 {
1424         set_extent_buffer_dirty(eb);
1425 }
1426
1427 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1428 {
1429         int ret;
1430
1431         ret = extent_buffer_uptodate(buf);
1432         if (!ret)
1433                 return ret;
1434
1435         ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1436         return !ret;
1437 }
1438
1439 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1440 {
1441         return set_extent_buffer_uptodate(eb);
1442 }