GNU Linux-libre 4.9.326-gnu1
[releases.git] / fs / ext4 / block_validity.c
1 /*
2  *  linux/fs/ext4/block_validity.c
3  *
4  * Copyright (C) 2009
5  * Theodore Ts'o (tytso@mit.edu)
6  *
7  * Track which blocks in the filesystem are metadata blocks that
8  * should never be used as data blocks by files or directories.
9  */
10
11 #include <linux/time.h>
12 #include <linux/fs.h>
13 #include <linux/namei.h>
14 #include <linux/quotaops.h>
15 #include <linux/buffer_head.h>
16 #include <linux/swap.h>
17 #include <linux/pagemap.h>
18 #include <linux/blkdev.h>
19 #include <linux/slab.h>
20 #include "ext4.h"
21
22 struct ext4_system_zone {
23         struct rb_node  node;
24         ext4_fsblk_t    start_blk;
25         unsigned int    count;
26         u32             ino;
27 };
28
29 static struct kmem_cache *ext4_system_zone_cachep;
30
31 int __init ext4_init_system_zone(void)
32 {
33         ext4_system_zone_cachep = KMEM_CACHE(ext4_system_zone, 0);
34         if (ext4_system_zone_cachep == NULL)
35                 return -ENOMEM;
36         return 0;
37 }
38
39 void ext4_exit_system_zone(void)
40 {
41         kmem_cache_destroy(ext4_system_zone_cachep);
42 }
43
44 static inline int can_merge(struct ext4_system_zone *entry1,
45                      struct ext4_system_zone *entry2)
46 {
47         if ((entry1->start_blk + entry1->count) == entry2->start_blk &&
48             entry1->ino == entry2->ino)
49                 return 1;
50         return 0;
51 }
52
53 /*
54  * Mark a range of blocks as belonging to the "system zone" --- that
55  * is, filesystem metadata blocks which should never be used by
56  * inodes.
57  */
58 static int add_system_zone(struct ext4_sb_info *sbi,
59                            ext4_fsblk_t start_blk,
60                            unsigned int count, u32 ino)
61 {
62         struct ext4_system_zone *new_entry, *entry;
63         struct rb_node **n = &sbi->system_blks.rb_node, *node;
64         struct rb_node *parent = NULL, *new_node = NULL;
65
66         while (*n) {
67                 parent = *n;
68                 entry = rb_entry(parent, struct ext4_system_zone, node);
69                 if (start_blk < entry->start_blk)
70                         n = &(*n)->rb_left;
71                 else if (start_blk >= (entry->start_blk + entry->count))
72                         n = &(*n)->rb_right;
73                 else    /* Unexpected overlap of system zones. */
74                         return -EFSCORRUPTED;
75         }
76
77         new_entry = kmem_cache_alloc(ext4_system_zone_cachep,
78                                      GFP_KERNEL);
79         if (!new_entry)
80                 return -ENOMEM;
81         new_entry->start_blk = start_blk;
82         new_entry->count = count;
83         new_entry->ino = ino;
84         new_node = &new_entry->node;
85
86         rb_link_node(new_node, parent, n);
87         rb_insert_color(new_node, &sbi->system_blks);
88
89         /* Can we merge to the left? */
90         node = rb_prev(new_node);
91         if (node) {
92                 entry = rb_entry(node, struct ext4_system_zone, node);
93                 if (can_merge(entry, new_entry)) {
94                         new_entry->start_blk = entry->start_blk;
95                         new_entry->count += entry->count;
96                         rb_erase(node, &sbi->system_blks);
97                         kmem_cache_free(ext4_system_zone_cachep, entry);
98                 }
99         }
100
101         /* Can we merge to the right? */
102         node = rb_next(new_node);
103         if (node) {
104                 entry = rb_entry(node, struct ext4_system_zone, node);
105                 if (can_merge(new_entry, entry)) {
106                         new_entry->count += entry->count;
107                         rb_erase(node, &sbi->system_blks);
108                         kmem_cache_free(ext4_system_zone_cachep, entry);
109                 }
110         }
111         return 0;
112 }
113
114 static void debug_print_tree(struct ext4_sb_info *sbi)
115 {
116         struct rb_node *node;
117         struct ext4_system_zone *entry;
118         int first = 1;
119
120         printk(KERN_INFO "System zones: ");
121         node = rb_first(&sbi->system_blks);
122         while (node) {
123                 entry = rb_entry(node, struct ext4_system_zone, node);
124                 printk(KERN_CONT "%s%llu-%llu", first ? "" : ", ",
125                        entry->start_blk, entry->start_blk + entry->count - 1);
126                 first = 0;
127                 node = rb_next(node);
128         }
129         printk(KERN_CONT "\n");
130 }
131
132 static int ext4_protect_reserved_inode(struct super_block *sb, u32 ino)
133 {
134         struct inode *inode;
135         struct ext4_sb_info *sbi = EXT4_SB(sb);
136         struct ext4_map_blocks map;
137         u32 i = 0, num;
138         int err = 0, n;
139
140         if ((ino < EXT4_ROOT_INO) ||
141             (ino > le32_to_cpu(sbi->s_es->s_inodes_count)))
142                 return -EINVAL;
143         inode = ext4_iget(sb, ino, EXT4_IGET_SPECIAL);
144         if (IS_ERR(inode))
145                 return PTR_ERR(inode);
146         num = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
147         while (i < num) {
148                 cond_resched();
149                 map.m_lblk = i;
150                 map.m_len = num - i;
151                 n = ext4_map_blocks(NULL, inode, &map, 0);
152                 if (n < 0) {
153                         err = n;
154                         break;
155                 }
156                 if (n == 0) {
157                         i++;
158                 } else {
159                         err = add_system_zone(sbi, map.m_pblk, n, ino);
160                         if (err < 0) {
161                                 if (err == -EFSCORRUPTED) {
162                                         ext4_error(sb,
163                                            "blocks %llu-%llu from inode %u "
164                                            "overlap system zone", map.m_pblk,
165                                            map.m_pblk + map.m_len - 1, ino);
166                                 }
167                                 break;
168                         }
169                         i += n;
170                 }
171         }
172         iput(inode);
173         return err;
174 }
175
176 int ext4_setup_system_zone(struct super_block *sb)
177 {
178         ext4_group_t ngroups = ext4_get_groups_count(sb);
179         struct ext4_sb_info *sbi = EXT4_SB(sb);
180         struct ext4_group_desc *gdp;
181         ext4_group_t i;
182         int flex_size = ext4_flex_bg_size(sbi);
183         int ret;
184
185         if (!test_opt(sb, BLOCK_VALIDITY)) {
186                 if (EXT4_SB(sb)->system_blks.rb_node)
187                         ext4_release_system_zone(sb);
188                 return 0;
189         }
190         if (EXT4_SB(sb)->system_blks.rb_node)
191                 return 0;
192
193         for (i=0; i < ngroups; i++) {
194                 if (ext4_bg_has_super(sb, i) &&
195                     ((i < 5) || ((i % flex_size) == 0)))
196                         add_system_zone(sbi, ext4_group_first_block_no(sb, i),
197                                         ext4_bg_num_gdb(sb, i) + 1, 0);
198                 gdp = ext4_get_group_desc(sb, i, NULL);
199                 ret = add_system_zone(sbi, ext4_block_bitmap(sb, gdp), 1, 0);
200                 if (ret)
201                         return ret;
202                 ret = add_system_zone(sbi, ext4_inode_bitmap(sb, gdp), 1, 0);
203                 if (ret)
204                         return ret;
205                 ret = add_system_zone(sbi, ext4_inode_table(sb, gdp),
206                                 sbi->s_itb_per_group, 0);
207                 if (ret)
208                         return ret;
209         }
210         if (ext4_has_feature_journal(sb) && sbi->s_es->s_journal_inum) {
211                 ret = ext4_protect_reserved_inode(sb,
212                                 le32_to_cpu(sbi->s_es->s_journal_inum));
213                 if (ret)
214                         return ret;
215         }
216
217         if (test_opt(sb, DEBUG))
218                 debug_print_tree(EXT4_SB(sb));
219         return 0;
220 }
221
222 /* Called when the filesystem is unmounted */
223 void ext4_release_system_zone(struct super_block *sb)
224 {
225         struct ext4_system_zone *entry, *n;
226
227         rbtree_postorder_for_each_entry_safe(entry, n,
228                         &EXT4_SB(sb)->system_blks, node)
229                 kmem_cache_free(ext4_system_zone_cachep, entry);
230
231         EXT4_SB(sb)->system_blks = RB_ROOT;
232 }
233
234 /*
235  * Returns 1 if the passed-in block region (start_blk,
236  * start_blk+count) is valid; 0 if some part of the block region
237  * overlaps with filesystem metadata blocks.
238  */
239 int ext4_inode_block_valid(struct inode *inode, ext4_fsblk_t start_blk,
240                            unsigned int count)
241 {
242         struct ext4_system_zone *entry;
243         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
244         struct rb_node *n = sbi->system_blks.rb_node;
245
246         if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
247             (start_blk + count < start_blk) ||
248             (start_blk + count > ext4_blocks_count(sbi->s_es))) {
249                 sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
250                 return 0;
251         }
252         while (n) {
253                 entry = rb_entry(n, struct ext4_system_zone, node);
254                 if (start_blk + count - 1 < entry->start_blk)
255                         n = n->rb_left;
256                 else if (start_blk >= (entry->start_blk + entry->count))
257                         n = n->rb_right;
258                 else {
259                         if (entry->ino == inode->i_ino)
260                                 return 1;
261                         sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
262                         return 0;
263                 }
264         }
265         return 1;
266 }
267
268 int ext4_check_blockref(const char *function, unsigned int line,
269                         struct inode *inode, __le32 *p, unsigned int max)
270 {
271         struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
272         __le32 *bref = p;
273         unsigned int blk;
274
275         if (ext4_has_feature_journal(inode->i_sb) &&
276             (inode->i_ino ==
277              le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
278                 return 0;
279
280         while (bref < p+max) {
281                 blk = le32_to_cpu(*bref++);
282                 if (blk &&
283                     unlikely(!ext4_inode_block_valid(inode, blk, 1))) {
284                         es->s_last_error_block = cpu_to_le64(blk);
285                         ext4_error_inode(inode, function, line, blk,
286                                          "invalid block");
287                         return -EFSCORRUPTED;
288                 }
289         }
290         return 0;
291 }
292