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