GNU Linux-libre 4.14.262-gnu1
[releases.git] / fs / ext4 / block_validity.c
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
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
40 void ext4_exit_system_zone(void)
41 {
42         kmem_cache_destroy(ext4_system_zone_cachep);
43 }
44
45 static inline int can_merge(struct ext4_system_zone *entry1,
46                      struct ext4_system_zone *entry2)
47 {
48         if ((entry1->start_blk + entry1->count) == entry2->start_blk &&
49             entry1->ino == entry2->ino)
50                 return 1;
51         return 0;
52 }
53
54 /*
55  * Mark a range of blocks as belonging to the "system zone" --- that
56  * is, filesystem metadata blocks which should never be used by
57  * inodes.
58  */
59 static int add_system_zone(struct ext4_sb_info *sbi,
60                            ext4_fsblk_t start_blk,
61                            unsigned int count, u32 ino)
62 {
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;
66
67         while (*n) {
68                 parent = *n;
69                 entry = rb_entry(parent, struct ext4_system_zone, node);
70                 if (start_blk < entry->start_blk)
71                         n = &(*n)->rb_left;
72                 else if (start_blk >= (entry->start_blk + entry->count))
73                         n = &(*n)->rb_right;
74                 else    /* Unexpected overlap of system zones. */
75                         return -EFSCORRUPTED;
76         }
77
78         new_entry = kmem_cache_alloc(ext4_system_zone_cachep,
79                                      GFP_KERNEL);
80         if (!new_entry)
81                 return -ENOMEM;
82         new_entry->start_blk = start_blk;
83         new_entry->count = count;
84         new_entry->ino = ino;
85         new_node = &new_entry->node;
86
87         rb_link_node(new_node, parent, n);
88         rb_insert_color(new_node, &sbi->system_blks);
89
90         /* Can we merge to the left? */
91         node = rb_prev(new_node);
92         if (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);
99                 }
100         }
101
102         /* Can we merge to the right? */
103         node = rb_next(new_node);
104         if (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);
110                 }
111         }
112         return 0;
113 }
114
115 static void debug_print_tree(struct ext4_sb_info *sbi)
116 {
117         struct rb_node *node;
118         struct ext4_system_zone *entry;
119         int first = 1;
120
121         printk(KERN_INFO "System zones: ");
122         node = rb_first(&sbi->system_blks);
123         while (node) {
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);
127                 first = 0;
128                 node = rb_next(node);
129         }
130         printk(KERN_CONT "\n");
131 }
132
133 static int ext4_protect_reserved_inode(struct super_block *sb, u32 ino)
134 {
135         struct inode *inode;
136         struct ext4_sb_info *sbi = EXT4_SB(sb);
137         struct ext4_map_blocks map;
138         u32 i = 0, num;
139         int err = 0, n;
140
141         if ((ino < EXT4_ROOT_INO) ||
142             (ino > le32_to_cpu(sbi->s_es->s_inodes_count)))
143                 return -EINVAL;
144         inode = ext4_iget(sb, ino, EXT4_IGET_SPECIAL);
145         if (IS_ERR(inode))
146                 return PTR_ERR(inode);
147         num = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
148         while (i < num) {
149                 cond_resched();
150                 map.m_lblk = i;
151                 map.m_len = num - i;
152                 n = ext4_map_blocks(NULL, inode, &map, 0);
153                 if (n < 0) {
154                         err = n;
155                         break;
156                 }
157                 if (n == 0) {
158                         i++;
159                 } else {
160                         err = add_system_zone(sbi, map.m_pblk, n, ino);
161                         if (err < 0) {
162                                 if (err == -EFSCORRUPTED) {
163                                         ext4_error(sb,
164                                            "blocks %llu-%llu from inode %u "
165                                            "overlap system zone", map.m_pblk,
166                                            map.m_pblk + map.m_len - 1, ino);
167                                 }
168                                 break;
169                         }
170                         i += n;
171                 }
172         }
173         iput(inode);
174         return err;
175 }
176
177 int ext4_setup_system_zone(struct super_block *sb)
178 {
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;
182         ext4_group_t i;
183         int flex_size = ext4_flex_bg_size(sbi);
184         int ret;
185
186         if (!test_opt(sb, BLOCK_VALIDITY)) {
187                 if (EXT4_SB(sb)->system_blks.rb_node)
188                         ext4_release_system_zone(sb);
189                 return 0;
190         }
191         if (EXT4_SB(sb)->system_blks.rb_node)
192                 return 0;
193
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);
201                 if (ret)
202                         return ret;
203                 ret = add_system_zone(sbi, ext4_inode_bitmap(sb, gdp), 1, 0);
204                 if (ret)
205                         return ret;
206                 ret = add_system_zone(sbi, ext4_inode_table(sb, gdp),
207                                 sbi->s_itb_per_group, 0);
208                 if (ret)
209                         return ret;
210         }
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));
214                 if (ret)
215                         return ret;
216         }
217
218         if (test_opt(sb, DEBUG))
219                 debug_print_tree(EXT4_SB(sb));
220         return 0;
221 }
222
223 /* Called when the filesystem is unmounted */
224 void ext4_release_system_zone(struct super_block *sb)
225 {
226         struct ext4_system_zone *entry, *n;
227
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);
231
232         EXT4_SB(sb)->system_blks = RB_ROOT;
233 }
234
235 /*
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.
239  */
240 int ext4_inode_block_valid(struct inode *inode, ext4_fsblk_t start_blk,
241                            unsigned int count)
242 {
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;
246
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);
251                 return 0;
252         }
253         while (n) {
254                 entry = rb_entry(n, struct ext4_system_zone, node);
255                 if (start_blk + count - 1 < entry->start_blk)
256                         n = n->rb_left;
257                 else if (start_blk >= (entry->start_blk + entry->count))
258                         n = n->rb_right;
259                 else {
260                         if (entry->ino == inode->i_ino)
261                                 return 1;
262                         sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
263                         return 0;
264                 }
265         }
266         return 1;
267 }
268
269 int ext4_check_blockref(const char *function, unsigned int line,
270                         struct inode *inode, __le32 *p, unsigned int max)
271 {
272         struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
273         __le32 *bref = p;
274         unsigned int blk;
275
276         if (ext4_has_feature_journal(inode->i_sb) &&
277             (inode->i_ino ==
278              le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
279                 return 0;
280
281         while (bref < p+max) {
282                 blk = le32_to_cpu(*bref++);
283                 if (blk &&
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,
287                                          "invalid block");
288                         return -EFSCORRUPTED;
289                 }
290         }
291         return 0;
292 }
293