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