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