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