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