^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/ext4/block_validity.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2009
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Theodore Ts'o (tytso@mit.edu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Track which blocks in the filesystem are metadata blocks that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * should never be used as data blocks by files or directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "ext4.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct ext4_system_zone {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct rb_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ext4_fsblk_t start_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u32 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static struct kmem_cache *ext4_system_zone_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int __init ext4_init_system_zone(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ext4_system_zone_cachep = KMEM_CACHE(ext4_system_zone, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (ext4_system_zone_cachep == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void ext4_exit_system_zone(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) kmem_cache_destroy(ext4_system_zone_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static inline int can_merge(struct ext4_system_zone *entry1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct ext4_system_zone *entry2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if ((entry1->start_blk + entry1->count) == entry2->start_blk &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) entry1->ino == entry2->ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static void release_system_zone(struct ext4_system_blocks *system_blks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct ext4_system_zone *entry, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) rbtree_postorder_for_each_entry_safe(entry, n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) &system_blks->root, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) kmem_cache_free(ext4_system_zone_cachep, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Mark a range of blocks as belonging to the "system zone" --- that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * is, filesystem metadata blocks which should never be used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int add_system_zone(struct ext4_system_blocks *system_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ext4_fsblk_t start_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) unsigned int count, u32 ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct ext4_system_zone *new_entry, *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct rb_node **n = &system_blks->root.rb_node, *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct rb_node *parent = NULL, *new_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) while (*n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) parent = *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) entry = rb_entry(parent, struct ext4_system_zone, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (start_blk < entry->start_blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) n = &(*n)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) else if (start_blk >= (entry->start_blk + entry->count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) n = &(*n)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) else /* Unexpected overlap of system zones. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) new_entry = kmem_cache_alloc(ext4_system_zone_cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!new_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) new_entry->start_blk = start_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) new_entry->count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) new_entry->ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) new_node = &new_entry->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) rb_link_node(new_node, parent, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) rb_insert_color(new_node, &system_blks->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* Can we merge to the left? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) node = rb_prev(new_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) entry = rb_entry(node, struct ext4_system_zone, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (can_merge(entry, new_entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) new_entry->start_blk = entry->start_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) new_entry->count += entry->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) rb_erase(node, &system_blks->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) kmem_cache_free(ext4_system_zone_cachep, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* Can we merge to the right? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) node = rb_next(new_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) entry = rb_entry(node, struct ext4_system_zone, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (can_merge(new_entry, entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) new_entry->count += entry->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) rb_erase(node, &system_blks->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) kmem_cache_free(ext4_system_zone_cachep, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static void debug_print_tree(struct ext4_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct ext4_system_zone *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct ext4_system_blocks *system_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int first = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) printk(KERN_INFO "System zones: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) system_blks = rcu_dereference(sbi->s_system_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) node = rb_first(&system_blks->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) entry = rb_entry(node, struct ext4_system_zone, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) printk(KERN_CONT "%s%llu-%llu", first ? "" : ", ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) entry->start_blk, entry->start_blk + entry->count - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) first = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) printk(KERN_CONT "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int ext4_protect_reserved_inode(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct ext4_system_blocks *system_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) u32 ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct ext4_map_blocks map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) u32 i = 0, num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int err = 0, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if ((ino < EXT4_ROOT_INO) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) (ino > le32_to_cpu(sbi->s_es->s_inodes_count)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) inode = ext4_iget(sb, ino, EXT4_IGET_SPECIAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) num = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) while (i < num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) map.m_lblk = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) map.m_len = num - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) n = ext4_map_blocks(NULL, inode, &map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) err = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (n == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) err = add_system_zone(system_blks, map.m_pblk, n, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (err == -EFSCORRUPTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) __ext4_error(sb, __func__, __LINE__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) -err, map.m_pblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) "blocks %llu-%llu from inode %u overlap system zone",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) map.m_pblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) map.m_pblk + map.m_len - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) i += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static void ext4_destroy_system_zone(struct rcu_head *rcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct ext4_system_blocks *system_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) system_blks = container_of(rcu, struct ext4_system_blocks, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) release_system_zone(system_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) kfree(system_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * Build system zone rbtree which is used for block validity checking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * The update of system_blks pointer in this function is protected by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * sb->s_umount semaphore. However we have to be careful as we can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * racing with ext4_data_block_valid() calls reading system_blks rbtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * protected only by RCU. That's why we first build the rbtree and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * swap it in place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int ext4_setup_system_zone(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ext4_group_t ngroups = ext4_get_groups_count(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct ext4_system_blocks *system_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct ext4_group_desc *gdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) ext4_group_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) int flex_size = ext4_flex_bg_size(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) system_blks = kzalloc(sizeof(*system_blks), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (!system_blks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) for (i=0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (ext4_bg_has_super(sb, i) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) ((i < 5) || ((i % flex_size) == 0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ret = add_system_zone(system_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ext4_group_first_block_no(sb, i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) ext4_bg_num_gdb(sb, i) + 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) gdp = ext4_get_group_desc(sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ret = add_system_zone(system_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) ext4_block_bitmap(sb, gdp), 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ret = add_system_zone(system_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) ext4_inode_bitmap(sb, gdp), 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) ret = add_system_zone(system_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) ext4_inode_table(sb, gdp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) sbi->s_itb_per_group, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (ext4_has_feature_journal(sb) && sbi->s_es->s_journal_inum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ret = ext4_protect_reserved_inode(sb, system_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) le32_to_cpu(sbi->s_es->s_journal_inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * System blks rbtree complete, announce it once to prevent racing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * with ext4_data_block_valid() accessing the rbtree at the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) rcu_assign_pointer(sbi->s_system_blks, system_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (test_opt(sb, DEBUG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) debug_print_tree(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) release_system_zone(system_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) kfree(system_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * Called when the filesystem is unmounted or when remounting it with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * noblock_validity specified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * The update of system_blks pointer in this function is protected by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * sb->s_umount semaphore. However we have to be careful as we can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * racing with ext4_data_block_valid() calls reading system_blks rbtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * protected only by RCU. So we first clear the system_blks pointer and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * then free the rbtree only after RCU grace period expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) void ext4_release_system_zone(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct ext4_system_blocks *system_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) system_blks = rcu_dereference_protected(EXT4_SB(sb)->s_system_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) lockdep_is_held(&sb->s_umount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) rcu_assign_pointer(EXT4_SB(sb)->s_system_blks, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (system_blks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) call_rcu(&system_blks->rcu, ext4_destroy_system_zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * Returns 1 if the passed-in block region (start_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * start_blk+count) is valid; 0 if some part of the block region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * overlaps with some other filesystem metadata blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) int ext4_inode_block_valid(struct inode *inode, ext4_fsblk_t start_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct ext4_system_blocks *system_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct ext4_system_zone *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct rb_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) (start_blk + count < start_blk) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) (start_blk + count > ext4_blocks_count(sbi->s_es)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * Lock the system zone to prevent it being released concurrently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * when doing a remount which inverse current "[no]block_validity"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * mount option.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) system_blks = rcu_dereference(sbi->s_system_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (system_blks == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) goto out_rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) n = system_blks->root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) entry = rb_entry(n, struct ext4_system_zone, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (start_blk + count - 1 < entry->start_blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) n = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) else if (start_blk >= (entry->start_blk + entry->count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = (entry->ino == inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) out_rcu:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) int ext4_check_blockref(const char *function, unsigned int line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct inode *inode, __le32 *p, unsigned int max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) __le32 *bref = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) unsigned int blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (ext4_has_feature_journal(inode->i_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) (inode->i_ino ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) while (bref < p+max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) blk = le32_to_cpu(*bref++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (blk &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) unlikely(!ext4_inode_block_valid(inode, blk, 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ext4_error_inode(inode, function, line, blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) "invalid block");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)