"Fossies" - the Fresh Open Source Software Archive

Member "btrfs-progs-v5.4.1/disk-io.c" (9 Jan 2020, 55330 Bytes) of package /linux/misc/btrfs-progs-v5.4.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "disk-io.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: v5.4_vs_v5.4.1.

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