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