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