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