btrfs-progs: check: introduce traversal function for fsck
[platform/upstream/btrfs-progs.git] / disk-io.c
index 86abdeb..3647ecc 100644 (file)
--- a/disk-io.c
+++ b/disk-io.c
@@ -67,6 +67,11 @@ static int check_tree_block(struct btrfs_fs_info *fs_info,
                                                    nodesize))
                return BTRFS_BAD_NRITEMS;
 
+       /* Only leaf can be empty */
+       if (btrfs_header_nritems(buf) == 0 &&
+           btrfs_header_level(buf) != 0)
+               return BTRFS_BAD_NRITEMS;
+
        fs_devices = fs_info->fs_devices;
        while (fs_devices) {
                if (fs_info->ignore_fsid_mismatch ||
@@ -120,7 +125,7 @@ u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
 
 void btrfs_csum_final(u32 crc, char *result)
 {
-       *(__le32 *)result = ~cpu_to_le32(crc);
+       put_unaligned_le32(~crc, result);
 }
 
 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
@@ -1323,7 +1328,7 @@ struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
        int fp;
        int ret;
        struct btrfs_fs_info *info;
-       int oflags = O_CREAT | O_RDWR;
+       int oflags = O_RDWR;
        struct stat st;
 
        ret = stat(filename, &st);
@@ -1339,7 +1344,7 @@ struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
        if (!(flags & OPEN_CTREE_WRITES))
                oflags = O_RDONLY;
 
-       fp = open(filename, oflags, 0600);
+       fp = open(filename, oflags);
        if (fp < 0) {
                error("cannot open '%s': %s", filename, strerror(errno));
                return NULL;
@@ -1471,7 +1476,8 @@ static int check_super(struct btrfs_super_block *sb)
                error("invalid bytes_used %llu", btrfs_super_bytes_used(sb));
                goto error_out;
        }
-       if (btrfs_super_stripesize(sb) != 4096) {
+       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;
        }