+ u64 total_devs;
+ u64 dev_size;
+ off_t seek_ret;
+ int ret;
+ if (!sb_bytenr)
+ sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
+
+ seek_ret = lseek(fd, 0, SEEK_END);
+ if (seek_ret < 0)
+ return -errno;
+
+ dev_size = seek_ret;
+ lseek(fd, 0, SEEK_SET);
+ if (sb_bytenr > dev_size) {
+ error("superblock bytenr %llu is larger than device size %llu",
+ (unsigned long long)sb_bytenr,
+ (unsigned long long)dev_size);
+ return -EINVAL;
+ }
+
+ ret = btrfs_scan_one_device(fd, path, fs_devices,
+ &total_devs, sb_bytenr, sbflags);
+ if (ret) {
+ fprintf(stderr, "No valid Btrfs found on %s\n", path);
+ return ret;
+ }
+
+ if (!skip_devices && total_devs != 1) {
+ ret = btrfs_scan_devices();
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
+ u64 chunk_root_bytenr)
+{
+ struct btrfs_super_block *sb = fs_info->super_copy;
+ u64 generation;
+ int ret;
+
+ btrfs_setup_root(fs_info->chunk_root, fs_info,
+ BTRFS_CHUNK_TREE_OBJECTID);
+
+ ret = btrfs_read_sys_array(fs_info);
+ if (ret)
+ return ret;
+
+ generation = btrfs_super_chunk_root_generation(sb);
+
+ if (chunk_root_bytenr && !IS_ALIGNED(chunk_root_bytenr,
+ fs_info->sectorsize)) {
+ warning("chunk_root_bytenr %llu is unaligned to %u, ignore it",
+ chunk_root_bytenr, fs_info->sectorsize);
+ chunk_root_bytenr = 0;
+ }
+
+ if (!chunk_root_bytenr)
+ chunk_root_bytenr = btrfs_super_chunk_root(sb);
+ else
+ generation = 0;
+
+ fs_info->chunk_root->node = read_tree_block(fs_info,
+ chunk_root_bytenr,
+ generation);
+ if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
+ if (fs_info->ignore_chunk_tree_error) {
+ warning("cannot read chunk root, continue anyway");
+ fs_info->chunk_root = NULL;
+ return 0;
+ } else {
+ error("cannot read chunk root");
+ return -EIO;
+ }
+ }
+
+ if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
+ ret = btrfs_read_chunk_tree(fs_info);
+ if (ret) {
+ fprintf(stderr, "Couldn't read chunk tree\n");
+ return ret;
+ }
+ }
+ return 0;
+}
+
+static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
+ u64 sb_bytenr,
+ u64 root_tree_bytenr,
+ u64 chunk_root_bytenr,
+ unsigned flags)
+{
+ struct btrfs_fs_info *fs_info;
+ struct btrfs_super_block *disk_super;
+ struct btrfs_fs_devices *fs_devices = NULL;
+ struct extent_buffer *eb;
+ int ret;
+ int oflags;
+ unsigned sbflags = SBREAD_DEFAULT;
+
+ if (sb_bytenr == 0)
+ sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
+
+ /* try to drop all the caches */
+ if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
+ fprintf(stderr, "Warning, could not drop caches\n");
+
+ fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
+ if (!fs_info) {
+ fprintf(stderr, "Failed to allocate memory for fs_info\n");
+ return NULL;
+ }
+ if (flags & OPEN_CTREE_RESTORE)
+ fs_info->on_restoring = 1;
+ if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
+ fs_info->suppress_check_block_errors = 1;
+ if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
+ fs_info->ignore_fsid_mismatch = 1;
+ if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR)
+ fs_info->ignore_chunk_tree_error = 1;
+
+ if ((flags & OPEN_CTREE_RECOVER_SUPER)
+ && (flags & OPEN_CTREE_FS_PARTIAL)) {
+ fprintf(stderr,
+ "cannot open a partially created filesystem for recovery");
+ goto out;
+ }
+
+ if (flags & OPEN_CTREE_FS_PARTIAL)
+ sbflags = SBREAD_PARTIAL;
+
+ ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr, sbflags,
+ (flags & OPEN_CTREE_NO_DEVICES));
+ if (ret)
+ goto out;
+
+ fs_info->fs_devices = fs_devices;
+ if (flags & OPEN_CTREE_WRITES)
+ oflags = O_RDWR;
+ else
+ oflags = O_RDONLY;
+
+ if (flags & OPEN_CTREE_EXCLUSIVE)
+ oflags |= O_EXCL;
+
+ ret = btrfs_open_devices(fs_devices, oflags);
+ if (ret)
+ goto out;
+
+ disk_super = fs_info->super_copy;
+ if (flags & OPEN_CTREE_RECOVER_SUPER)
+ ret = btrfs_read_dev_super(fs_devices->latest_bdev, disk_super,
+ sb_bytenr, SBREAD_RECOVER);
+ else
+ ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr,
+ sbflags);
+ if (ret) {
+ printk("No valid btrfs found\n");
+ goto out_devices;
+ }
+
+ if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID &&
+ !fs_info->ignore_fsid_mismatch) {
+ fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
+ goto out_devices;
+ }
+
+ memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
+ fs_info->sectorsize = btrfs_super_sectorsize(disk_super);
+ fs_info->nodesize = btrfs_super_nodesize(disk_super);
+ fs_info->stripesize = btrfs_super_stripesize(disk_super);
+
+ ret = btrfs_check_fs_compatibility(fs_info->super_copy, flags);
+ if (ret)
+ goto out_devices;
+
+ ret = btrfs_setup_chunk_tree_and_device_map(fs_info, chunk_root_bytenr);
+ if (ret)
+ goto out_chunk;
+
+ /* Chunk tree root is unable to read, return directly */
+ if (!fs_info->chunk_root)
+ return fs_info;
+
+ eb = fs_info->chunk_root->node;
+ read_extent_buffer(eb, fs_info->chunk_tree_uuid,
+ btrfs_header_chunk_tree_uuid(eb),
+ BTRFS_UUID_SIZE);
+
+ ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
+ if (ret && !(flags & __OPEN_CTREE_RETURN_CHUNK_ROOT) &&
+ !fs_info->ignore_chunk_tree_error)
+ goto out_chunk;
+
+ return fs_info;
+
+out_chunk:
+ btrfs_release_all_roots(fs_info);
+ btrfs_cleanup_all_caches(fs_info);
+out_devices:
+ btrfs_close_devices(fs_devices);
+out:
+ btrfs_free_fs_info(fs_info);
+ return NULL;
+}
+
+struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
+ u64 sb_bytenr, u64 root_tree_bytenr,
+ u64 chunk_root_bytenr,
+ unsigned flags)
+{
+ int fp;
+ int ret;
+ struct btrfs_fs_info *info;
+ int oflags = O_RDWR;
+ struct stat st;
+
+ ret = stat(filename, &st);
+ if (ret < 0) {
+ error("cannot stat '%s': %m", filename);
+ return NULL;
+ }
+ if (!(((st.st_mode & S_IFMT) == S_IFREG) || ((st.st_mode & S_IFMT) == S_IFBLK))) {
+ error("not a regular file or block device: %s", filename);
+ return NULL;
+ }
+
+ if (!(flags & OPEN_CTREE_WRITES))
+ oflags = O_RDONLY;
+
+ fp = open(filename, oflags);
+ if (fp < 0) {
+ error("cannot open '%s': %m", filename);
+ return NULL;
+ }
+ info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
+ chunk_root_bytenr, flags);
+ close(fp);
+ return info;
+}
+
+struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
+ unsigned flags)
+{
+ struct btrfs_fs_info *info;
+
+ /* This flags may not return fs_info with any valid root */
+ BUG_ON(flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR);
+ info = open_ctree_fs_info(filename, sb_bytenr, 0, 0, flags);
+ if (!info)
+ return NULL;
+ if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
+ return info->chunk_root;
+ return info->fs_root;
+}
+
+struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
+ unsigned flags)
+{
+ struct btrfs_fs_info *info;
+
+ /* This flags may not return fs_info with any valid root */
+ if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR) {
+ error("invalid open_ctree flags: 0x%llx",
+ (unsigned long long)flags);
+ return NULL;
+ }
+ info = __open_ctree_fd(fp, path, sb_bytenr, 0, 0, flags);
+ if (!info)
+ return NULL;
+ if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
+ return info->chunk_root;
+ return info->fs_root;
+}
+
+/*
+ * Check if the super is valid:
+ * - nodesize/sectorsize - minimum, maximum, alignment
+ * - tree block starts - alignment
+ * - number of devices - something sane
+ * - sys array size - maximum
+ */
+static int check_super(struct btrfs_super_block *sb, unsigned sbflags)
+{
+ u8 result[BTRFS_CSUM_SIZE];
+ u32 crc;
+ u16 csum_type;
+ int csum_size;
+
+ if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
+ if (btrfs_super_magic(sb) == BTRFS_MAGIC_PARTIAL) {
+ if (!(sbflags & SBREAD_PARTIAL)) {
+ error("superblock magic doesn't match");
+ return -EIO;
+ }
+ }
+ }
+
+ csum_type = btrfs_super_csum_type(sb);
+ if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
+ error("unsupported checksum algorithm %u", csum_type);
+ return -EIO;
+ }
+ csum_size = btrfs_csum_sizes[csum_type];
+
+ crc = ~(u32)0;
+ crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, crc,
+ BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+ btrfs_csum_final(crc, result);
+
+ if (memcmp(result, sb->csum, csum_size)) {
+ error("superblock checksum mismatch");
+ return -EIO;
+ }
+ if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
+ error("tree_root level too big: %d >= %d",
+ btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
+ goto error_out;
+ }
+ if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
+ error("chunk_root level too big: %d >= %d",
+ btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
+ goto error_out;
+ }
+ if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
+ error("log_root level too big: %d >= %d",
+ btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
+ goto error_out;
+ }
+
+ if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
+ error("tree_root block unaligned: %llu", btrfs_super_root(sb));
+ goto error_out;
+ }
+ if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
+ error("chunk_root block unaligned: %llu",
+ btrfs_super_chunk_root(sb));
+ goto error_out;
+ }
+ if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
+ error("log_root block unaligned: %llu",
+ btrfs_super_log_root(sb));
+ goto error_out;
+ }
+ if (btrfs_super_nodesize(sb) < 4096) {
+ error("nodesize too small: %u < 4096",
+ btrfs_super_nodesize(sb));
+ goto error_out;
+ }
+ if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
+ error("nodesize unaligned: %u", btrfs_super_nodesize(sb));
+ goto error_out;
+ }
+ if (btrfs_super_sectorsize(sb) < 4096) {
+ error("sectorsize too small: %u < 4096",
+ btrfs_super_sectorsize(sb));
+ goto error_out;
+ }
+ if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
+ error("sectorsize unaligned: %u", btrfs_super_sectorsize(sb));
+ goto error_out;
+ }
+ if (btrfs_super_total_bytes(sb) == 0) {
+ error("invalid total_bytes 0");
+ goto error_out;
+ }
+ if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
+ error("invalid bytes_used %llu", btrfs_super_bytes_used(sb));
+ goto error_out;
+ }
+ if ((btrfs_super_stripesize(sb) != 4096)
+ && (btrfs_super_stripesize(sb) != btrfs_super_sectorsize(sb))) {
+ error("invalid stripesize %u", btrfs_super_stripesize(sb));
+ goto error_out;
+ }
+
+ if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
+ char fsid[BTRFS_UUID_UNPARSED_SIZE];
+ char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
+
+ uuid_unparse(sb->fsid, fsid);
+ uuid_unparse(sb->dev_item.fsid, dev_fsid);
+ error("dev_item UUID does not match fsid: %s != %s",
+ dev_fsid, fsid);
+ goto error_out;
+ }
+
+ /*
+ * Hint to catch really bogus numbers, bitflips or so
+ */
+ if (btrfs_super_num_devices(sb) > (1UL << 31)) {
+ warning("suspicious number of devices: %llu",
+ btrfs_super_num_devices(sb));
+ }
+
+ if (btrfs_super_num_devices(sb) == 0) {
+ error("number of devices is 0");
+ goto error_out;
+ }
+
+ /*
+ * Obvious sys_chunk_array corruptions, it must hold at least one key
+ * and one chunk
+ */
+ if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
+ error("system chunk array too big %u > %u",
+ btrfs_super_sys_array_size(sb),
+ BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
+ goto error_out;
+ }
+ if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
+ + sizeof(struct btrfs_chunk)) {
+ error("system chunk array too small %u < %zu",
+ btrfs_super_sys_array_size(sb),
+ sizeof(struct btrfs_disk_key) +
+ sizeof(struct btrfs_chunk));
+ goto error_out;
+ }
+
+ return 0;
+
+error_out:
+ error("superblock checksum matches but it has invalid members");
+ return -EIO;
+}
+
+/*
+ * btrfs_read_dev_super - read a valid superblock from a block device
+ * @fd: file descriptor of the device
+ * @sb: buffer where the superblock is going to be read in
+ * @sb_bytenr: offset of the particular superblock copy we want
+ * @sbflags: flags controlling how the superblock is read
+ *
+ * This function is used by various btrfs comands to obtain a valid superblock.
+ *
+ * It's mode of operation is controlled by the @sb_bytenr and @sbdflags
+ * parameters. If SBREAD_RECOVER flag is set and @sb_bytenr is
+ * BTRFS_SUPER_INFO_OFFSET then the function reads all 3 superblock copies and
+ * returns the newest one. If SBREAD_RECOVER is not set then only a single
+ * copy is read, which one is decided by @sb_bytenr. If @sb_bytenr !=
+ * BTRFS_SUPER_INFO_OFFSET then the @sbflags is effectively ignored and only a
+ * single copy is read.
+ */
+int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
+ unsigned sbflags)
+{
+ u8 fsid[BTRFS_FSID_SIZE];
+ int fsid_is_initialized = 0;
+ char tmp[BTRFS_SUPER_INFO_SIZE];
+ struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
+ int i;
+ int ret;
+ int max_super = sbflags & SBREAD_RECOVER ? BTRFS_SUPER_MIRROR_MAX : 1;
+ u64 transid = 0;
+ u64 bytenr;
+
+ if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
+ ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
+ /* real error */
+ if (ret < 0)
+ return -errno;
+
+ /* Not large enough sb, return -ENOENT instead of normal -EIO */
+ if (ret < BTRFS_SUPER_INFO_SIZE)
+ return -ENOENT;
+
+ if (btrfs_super_bytenr(buf) != sb_bytenr)
+ return -EIO;
+
+ ret = check_super(buf, sbflags);
+ if (ret < 0)
+ return ret;
+ memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
+ return 0;
+ }
+
+ /*
+ * we would like to check all the supers, but that would make
+ * a btrfs mount succeed after a mkfs from a different FS.
+ * So, we need to add a special mount option to scan for
+ * later supers, using BTRFS_SUPER_MIRROR_MAX instead
+ */
+
+ for (i = 0; i < max_super; i++) {
+ bytenr = btrfs_sb_offset(i);
+ ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
+ if (ret < BTRFS_SUPER_INFO_SIZE)
+ break;
+
+ if (btrfs_super_bytenr(buf) != bytenr )
+ continue;
+ /* if magic is NULL, the device was removed */
+ if (btrfs_super_magic(buf) == 0 && i == 0)
+ break;
+ if (check_super(buf, sbflags))
+ continue;
+
+ if (!fsid_is_initialized) {
+ memcpy(fsid, buf->fsid, sizeof(fsid));
+ fsid_is_initialized = 1;
+ } else if (memcmp(fsid, buf->fsid, sizeof(fsid))) {
+ /*
+ * the superblocks (the original one and
+ * its backups) contain data of different
+ * filesystems -> the super cannot be trusted
+ */
+ continue;
+ }
+
+ if (btrfs_super_generation(buf) > transid) {
+ memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
+ transid = btrfs_super_generation(buf);
+ }
+ }
+
+ return transid > 0 ? 0 : -1;
+}
+
+static int write_dev_supers(struct btrfs_fs_info *fs_info,
+ struct btrfs_super_block *sb,
+ struct btrfs_device *device)
+{
+ u64 bytenr;
+ u32 crc;
+ int i, ret;
+
+ if (fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
+ btrfs_set_super_bytenr(sb, fs_info->super_bytenr);
+ crc = ~(u32)0;
+ crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, crc,
+ BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+ btrfs_csum_final(crc, &sb->csum[0]);
+
+ /*
+ * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
+ * zero filled, we can use it directly
+ */
+ ret = pwrite64(device->fd, fs_info->super_copy,
+ BTRFS_SUPER_INFO_SIZE,
+ fs_info->super_bytenr);
+ if (ret != BTRFS_SUPER_INFO_SIZE)
+ goto write_err;
+ return 0;
+ }
+
+ for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
+ bytenr = btrfs_sb_offset(i);
+ if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
+ break;
+
+ btrfs_set_super_bytenr(sb, bytenr);
+
+ crc = ~(u32)0;
+ crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, crc,
+ BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+ btrfs_csum_final(crc, &sb->csum[0]);
+
+ /*
+ * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
+ * zero filled, we can use it directly
+ */
+ ret = pwrite64(device->fd, fs_info->super_copy,
+ BTRFS_SUPER_INFO_SIZE, bytenr);
+ if (ret != BTRFS_SUPER_INFO_SIZE)
+ goto write_err;
+ }
+
+ return 0;
+
+write_err:
+ if (ret > 0)
+ fprintf(stderr, "WARNING: failed to write all sb data\n");
+ else
+ fprintf(stderr, "WARNING: failed to write sb: %m\n");
+ return ret;
+}
+
+int write_all_supers(struct btrfs_fs_info *fs_info)
+{
+ struct list_head *head = &fs_info->fs_devices->devices;