1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/block_validity.c
6 * Theodore Ts'o (tytso@mit.edu)
8 * Track which blocks in the filesystem are metadata blocks that
9 * should never be used as data blocks by files or directories.
12 #include <linux/time.h>
14 #include <linux/namei.h>
15 #include <linux/quotaops.h>
16 #include <linux/buffer_head.h>
17 #include <linux/swap.h>
18 #include <linux/pagemap.h>
19 #include <linux/blkdev.h>
20 #include <linux/slab.h>
23 struct ext4_system_zone {
25 ext4_fsblk_t start_blk;
30 static struct kmem_cache *ext4_system_zone_cachep;
32 int __init ext4_init_system_zone(void)
34 ext4_system_zone_cachep = KMEM_CACHE(ext4_system_zone, 0);
35 if (ext4_system_zone_cachep == NULL)
40 void ext4_exit_system_zone(void)
42 kmem_cache_destroy(ext4_system_zone_cachep);
45 static inline int can_merge(struct ext4_system_zone *entry1,
46 struct ext4_system_zone *entry2)
48 if ((entry1->start_blk + entry1->count) == entry2->start_blk &&
49 entry1->ino == entry2->ino)
55 * Mark a range of blocks as belonging to the "system zone" --- that
56 * is, filesystem metadata blocks which should never be used by
59 static int add_system_zone(struct ext4_sb_info *sbi,
60 ext4_fsblk_t start_blk,
61 unsigned int count, u32 ino)
63 struct ext4_system_zone *new_entry, *entry;
64 struct rb_node **n = &sbi->system_blks.rb_node, *node;
65 struct rb_node *parent = NULL, *new_node = NULL;
69 entry = rb_entry(parent, struct ext4_system_zone, node);
70 if (start_blk < entry->start_blk)
72 else if (start_blk >= (entry->start_blk + entry->count))
74 else /* Unexpected overlap of system zones. */
78 new_entry = kmem_cache_alloc(ext4_system_zone_cachep,
82 new_entry->start_blk = start_blk;
83 new_entry->count = count;
85 new_node = &new_entry->node;
87 rb_link_node(new_node, parent, n);
88 rb_insert_color(new_node, &sbi->system_blks);
90 /* Can we merge to the left? */
91 node = rb_prev(new_node);
93 entry = rb_entry(node, struct ext4_system_zone, node);
94 if (can_merge(entry, new_entry)) {
95 new_entry->start_blk = entry->start_blk;
96 new_entry->count += entry->count;
97 rb_erase(node, &sbi->system_blks);
98 kmem_cache_free(ext4_system_zone_cachep, entry);
102 /* Can we merge to the right? */
103 node = rb_next(new_node);
105 entry = rb_entry(node, struct ext4_system_zone, node);
106 if (can_merge(new_entry, entry)) {
107 new_entry->count += entry->count;
108 rb_erase(node, &sbi->system_blks);
109 kmem_cache_free(ext4_system_zone_cachep, entry);
115 static void debug_print_tree(struct ext4_sb_info *sbi)
117 struct rb_node *node;
118 struct ext4_system_zone *entry;
121 printk(KERN_INFO "System zones: ");
122 node = rb_first(&sbi->system_blks);
124 entry = rb_entry(node, struct ext4_system_zone, node);
125 printk(KERN_CONT "%s%llu-%llu", first ? "" : ", ",
126 entry->start_blk, entry->start_blk + entry->count - 1);
128 node = rb_next(node);
130 printk(KERN_CONT "\n");
133 static int ext4_protect_reserved_inode(struct super_block *sb, u32 ino)
136 struct ext4_sb_info *sbi = EXT4_SB(sb);
137 struct ext4_map_blocks map;
141 if ((ino < EXT4_ROOT_INO) ||
142 (ino > le32_to_cpu(sbi->s_es->s_inodes_count)))
144 inode = ext4_iget(sb, ino, EXT4_IGET_SPECIAL);
146 return PTR_ERR(inode);
147 num = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
152 n = ext4_map_blocks(NULL, inode, &map, 0);
160 err = add_system_zone(sbi, map.m_pblk, n, ino);
162 if (err == -EFSCORRUPTED) {
164 "blocks %llu-%llu from inode %u "
165 "overlap system zone", map.m_pblk,
166 map.m_pblk + map.m_len - 1, ino);
177 int ext4_setup_system_zone(struct super_block *sb)
179 ext4_group_t ngroups = ext4_get_groups_count(sb);
180 struct ext4_sb_info *sbi = EXT4_SB(sb);
181 struct ext4_group_desc *gdp;
183 int flex_size = ext4_flex_bg_size(sbi);
186 if (!test_opt(sb, BLOCK_VALIDITY)) {
187 if (EXT4_SB(sb)->system_blks.rb_node)
188 ext4_release_system_zone(sb);
191 if (EXT4_SB(sb)->system_blks.rb_node)
194 for (i=0; i < ngroups; i++) {
195 if (ext4_bg_has_super(sb, i) &&
196 ((i < 5) || ((i % flex_size) == 0)))
197 add_system_zone(sbi, ext4_group_first_block_no(sb, i),
198 ext4_bg_num_gdb(sb, i) + 1, 0);
199 gdp = ext4_get_group_desc(sb, i, NULL);
200 ret = add_system_zone(sbi, ext4_block_bitmap(sb, gdp), 1, 0);
203 ret = add_system_zone(sbi, ext4_inode_bitmap(sb, gdp), 1, 0);
206 ret = add_system_zone(sbi, ext4_inode_table(sb, gdp),
207 sbi->s_itb_per_group, 0);
211 if (ext4_has_feature_journal(sb) && sbi->s_es->s_journal_inum) {
212 ret = ext4_protect_reserved_inode(sb,
213 le32_to_cpu(sbi->s_es->s_journal_inum));
218 if (test_opt(sb, DEBUG))
219 debug_print_tree(EXT4_SB(sb));
223 /* Called when the filesystem is unmounted */
224 void ext4_release_system_zone(struct super_block *sb)
226 struct ext4_system_zone *entry, *n;
228 rbtree_postorder_for_each_entry_safe(entry, n,
229 &EXT4_SB(sb)->system_blks, node)
230 kmem_cache_free(ext4_system_zone_cachep, entry);
232 EXT4_SB(sb)->system_blks = RB_ROOT;
236 * Returns 1 if the passed-in block region (start_blk,
237 * start_blk+count) is valid; 0 if some part of the block region
238 * overlaps with filesystem metadata blocks.
240 int ext4_inode_block_valid(struct inode *inode, ext4_fsblk_t start_blk,
243 struct ext4_system_zone *entry;
244 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
245 struct rb_node *n = sbi->system_blks.rb_node;
247 if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
248 (start_blk + count < start_blk) ||
249 (start_blk + count > ext4_blocks_count(sbi->s_es))) {
250 sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
254 entry = rb_entry(n, struct ext4_system_zone, node);
255 if (start_blk + count - 1 < entry->start_blk)
257 else if (start_blk >= (entry->start_blk + entry->count))
260 if (entry->ino == inode->i_ino)
262 sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
269 int ext4_check_blockref(const char *function, unsigned int line,
270 struct inode *inode, __le32 *p, unsigned int max)
272 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
276 if (ext4_has_feature_journal(inode->i_sb) &&
278 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
281 while (bref < p+max) {
282 blk = le32_to_cpu(*bref++);
284 unlikely(!ext4_inode_block_valid(inode, blk, 1))) {
285 es->s_last_error_block = cpu_to_le64(blk);
286 ext4_error_inode(inode, function, line, blk,
288 return -EFSCORRUPTED;