]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - fs/btrfs/tree-checker.c
Merge branch 'linus' into perf/urgent, to synchronize with upstream
[linux.git] / fs / btrfs / tree-checker.c
index 285c362b31219a415786faefc6ffe6541cec82af..a92f8a6dd192911b0922de13ebc72ba4fd2ed367 100644 (file)
@@ -428,6 +428,49 @@ static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
        return 0;
 }
 
+static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
+                         int slot)
+{
+       struct btrfs_key item_key;
+       bool is_root_item;
+
+       btrfs_item_key_to_cpu(leaf, &item_key, slot);
+       is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
+
+       /* No such tree id */
+       if (key->objectid == 0) {
+               if (is_root_item)
+                       generic_err(leaf, slot, "invalid root id 0");
+               else
+                       dir_item_err(leaf, slot,
+                                    "invalid location key root id 0");
+               return -EUCLEAN;
+       }
+
+       /* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
+       if (!is_fstree(key->objectid) && !is_root_item) {
+               dir_item_err(leaf, slot,
+               "invalid location key objectid, have %llu expect [%llu, %llu]",
+                               key->objectid, BTRFS_FIRST_FREE_OBJECTID,
+                               BTRFS_LAST_FREE_OBJECTID);
+               return -EUCLEAN;
+       }
+
+       /*
+        * ROOT_ITEM with non-zero offset means this is a snapshot, created at
+        * @offset transid.
+        * Furthermore, for location key in DIR_ITEM, its offset is always -1.
+        *
+        * So here we only check offset for reloc tree whose key->offset must
+        * be a valid tree.
+        */
+       if (key->objectid == BTRFS_TREE_RELOC_OBJECTID && key->offset == 0) {
+               generic_err(leaf, slot, "invalid root id 0 for reloc tree");
+               return -EUCLEAN;
+       }
+       return 0;
+}
+
 static int check_dir_item(struct extent_buffer *leaf,
                          struct btrfs_key *key, struct btrfs_key *prev_key,
                          int slot)
@@ -441,12 +484,14 @@ static int check_dir_item(struct extent_buffer *leaf,
                return -EUCLEAN;
        di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
        while (cur < item_size) {
+               struct btrfs_key location_key;
                u32 name_len;
                u32 data_len;
                u32 max_name_len;
                u32 total_size;
                u32 name_hash;
                u8 dir_type;
+               int ret;
 
                /* header itself should not cross item boundary */
                if (cur + sizeof(*di) > item_size) {
@@ -456,6 +501,25 @@ static int check_dir_item(struct extent_buffer *leaf,
                        return -EUCLEAN;
                }
 
+               /* Location key check */
+               btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
+               if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
+                       ret = check_root_key(leaf, &location_key, slot);
+                       if (ret < 0)
+                               return ret;
+               } else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
+                          location_key.type == 0) {
+                       ret = check_inode_key(leaf, &location_key, slot);
+                       if (ret < 0)
+                               return ret;
+               } else {
+                       dir_item_err(leaf, slot,
+                       "invalid location key type, have %u, expect %u or %u",
+                                    location_key.type, BTRFS_ROOT_ITEM_KEY,
+                                    BTRFS_INODE_ITEM_KEY);
+                       return -EUCLEAN;
+               }
+
                /* dir type check */
                dir_type = btrfs_dir_type(leaf, di);
                if (dir_type >= BTRFS_FT_MAX) {
@@ -978,22 +1042,11 @@ static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
        struct btrfs_root_item ri;
        const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
                                     BTRFS_ROOT_SUBVOL_DEAD;
+       int ret;
 
-       /* No such tree id */
-       if (key->objectid == 0) {
-               generic_err(leaf, slot, "invalid root id 0");
-               return -EUCLEAN;
-       }
-
-       /*
-        * Some older kernel may create ROOT_ITEM with non-zero offset, so here
-        * we only check offset for reloc tree whose key->offset must be a
-        * valid tree.
-        */
-       if (key->objectid == BTRFS_TREE_RELOC_OBJECTID && key->offset == 0) {
-               generic_err(leaf, slot, "invalid root id 0 for reloc tree");
-               return -EUCLEAN;
-       }
+       ret = check_root_key(leaf, key, slot);
+       if (ret < 0)
+               return ret;
 
        if (btrfs_item_size_nr(leaf, slot) != sizeof(ri)) {
                generic_err(leaf, slot,