Blame view

fs/ext4/block_validity.c 6.07 KB
6fd058f77   Theodore Ts'o   ext4: Add a compr...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   *  linux/fs/ext4/block_validity.c
   *
   * Copyright (C) 2009
   * Theodore Ts'o (tytso@mit.edu)
   *
   * Track which blocks in the filesystem are metadata blocks that
   * should never be used as data blocks by files or directories.
   */
  
  #include <linux/time.h>
  #include <linux/fs.h>
  #include <linux/namei.h>
  #include <linux/quotaops.h>
  #include <linux/buffer_head.h>
6fd058f77   Theodore Ts'o   ext4: Add a compr...
16
17
  #include <linux/swap.h>
  #include <linux/pagemap.h>
6fd058f77   Theodore Ts'o   ext4: Add a compr...
18
  #include <linux/blkdev.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
19
  #include <linux/slab.h>
6fd058f77   Theodore Ts'o   ext4: Add a compr...
20
21
22
23
24
25
26
27
28
  #include "ext4.h"
  
  struct ext4_system_zone {
  	struct rb_node	node;
  	ext4_fsblk_t	start_blk;
  	unsigned int	count;
  };
  
  static struct kmem_cache *ext4_system_zone_cachep;
5dabfc78d   Theodore Ts'o   ext4: rename {exi...
29
  int __init ext4_init_system_zone(void)
6fd058f77   Theodore Ts'o   ext4: Add a compr...
30
  {
16828088f   Theodore Ts'o   ext4: use KMEM_CA...
31
  	ext4_system_zone_cachep = KMEM_CACHE(ext4_system_zone, 0);
6fd058f77   Theodore Ts'o   ext4: Add a compr...
32
33
34
35
  	if (ext4_system_zone_cachep == NULL)
  		return -ENOMEM;
  	return 0;
  }
5dabfc78d   Theodore Ts'o   ext4: rename {exi...
36
  void ext4_exit_system_zone(void)
6fd058f77   Theodore Ts'o   ext4: Add a compr...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  {
  	kmem_cache_destroy(ext4_system_zone_cachep);
  }
  
  static inline int can_merge(struct ext4_system_zone *entry1,
  		     struct ext4_system_zone *entry2)
  {
  	if ((entry1->start_blk + entry1->count) == entry2->start_blk)
  		return 1;
  	return 0;
  }
  
  /*
   * Mark a range of blocks as belonging to the "system zone" --- that
   * is, filesystem metadata blocks which should never be used by
   * inodes.
   */
  static int add_system_zone(struct ext4_sb_info *sbi,
  			   ext4_fsblk_t start_blk,
  			   unsigned int count)
  {
  	struct ext4_system_zone *new_entry = NULL, *entry;
  	struct rb_node **n = &sbi->system_blks.rb_node, *node;
  	struct rb_node *parent = NULL, *new_node = NULL;
  
  	while (*n) {
  		parent = *n;
  		entry = rb_entry(parent, struct ext4_system_zone, node);
  		if (start_blk < entry->start_blk)
  			n = &(*n)->rb_left;
  		else if (start_blk >= (entry->start_blk + entry->count))
  			n = &(*n)->rb_right;
  		else {
60e6679e2   Theodore Ts'o   ext4: Drop whites...
70
  			if (start_blk + count > (entry->start_blk +
6fd058f77   Theodore Ts'o   ext4: Add a compr...
71
  						 entry->count))
60e6679e2   Theodore Ts'o   ext4: Drop whites...
72
  				entry->count = (start_blk + count -
6fd058f77   Theodore Ts'o   ext4: Add a compr...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  						entry->start_blk);
  			new_node = *n;
  			new_entry = rb_entry(new_node, struct ext4_system_zone,
  					     node);
  			break;
  		}
  	}
  
  	if (!new_entry) {
  		new_entry = kmem_cache_alloc(ext4_system_zone_cachep,
  					     GFP_KERNEL);
  		if (!new_entry)
  			return -ENOMEM;
  		new_entry->start_blk = start_blk;
  		new_entry->count = count;
  		new_node = &new_entry->node;
  
  		rb_link_node(new_node, parent, n);
  		rb_insert_color(new_node, &sbi->system_blks);
  	}
  
  	/* Can we merge to the left? */
  	node = rb_prev(new_node);
  	if (node) {
  		entry = rb_entry(node, struct ext4_system_zone, node);
  		if (can_merge(entry, new_entry)) {
  			new_entry->start_blk = entry->start_blk;
  			new_entry->count += entry->count;
  			rb_erase(node, &sbi->system_blks);
  			kmem_cache_free(ext4_system_zone_cachep, entry);
  		}
  	}
  
  	/* Can we merge to the right? */
  	node = rb_next(new_node);
  	if (node) {
  		entry = rb_entry(node, struct ext4_system_zone, node);
  		if (can_merge(new_entry, entry)) {
  			new_entry->count += entry->count;
  			rb_erase(node, &sbi->system_blks);
  			kmem_cache_free(ext4_system_zone_cachep, entry);
  		}
  	}
  	return 0;
  }
  
  static void debug_print_tree(struct ext4_sb_info *sbi)
  {
  	struct rb_node *node;
  	struct ext4_system_zone *entry;
  	int first = 1;
  
  	printk(KERN_INFO "System zones: ");
  	node = rb_first(&sbi->system_blks);
  	while (node) {
  		entry = rb_entry(node, struct ext4_system_zone, node);
d74f3d252   Joe Perches   ext4: add missing...
129
  		printk(KERN_CONT "%s%llu-%llu", first ? "" : ", ",
6fd058f77   Theodore Ts'o   ext4: Add a compr...
130
131
132
133
  		       entry->start_blk, entry->start_blk + entry->count - 1);
  		first = 0;
  		node = rb_next(node);
  	}
d74f3d252   Joe Perches   ext4: add missing...
134
135
  	printk(KERN_CONT "
  ");
6fd058f77   Theodore Ts'o   ext4: Add a compr...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  }
  
  int ext4_setup_system_zone(struct super_block *sb)
  {
  	ext4_group_t ngroups = ext4_get_groups_count(sb);
  	struct ext4_sb_info *sbi = EXT4_SB(sb);
  	struct ext4_group_desc *gdp;
  	ext4_group_t i;
  	int flex_size = ext4_flex_bg_size(sbi);
  	int ret;
  
  	if (!test_opt(sb, BLOCK_VALIDITY)) {
  		if (EXT4_SB(sb)->system_blks.rb_node)
  			ext4_release_system_zone(sb);
  		return 0;
  	}
  	if (EXT4_SB(sb)->system_blks.rb_node)
  		return 0;
  
  	for (i=0; i < ngroups; i++) {
  		if (ext4_bg_has_super(sb, i) &&
  		    ((i < 5) || ((i % flex_size) == 0)))
  			add_system_zone(sbi, ext4_group_first_block_no(sb, i),
1032988c7   Theodore Ts'o   ext4: fix block v...
159
  					ext4_bg_num_gdb(sb, i) + 1);
6fd058f77   Theodore Ts'o   ext4: Add a compr...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  		gdp = ext4_get_group_desc(sb, i, NULL);
  		ret = add_system_zone(sbi, ext4_block_bitmap(sb, gdp), 1);
  		if (ret)
  			return ret;
  		ret = add_system_zone(sbi, ext4_inode_bitmap(sb, gdp), 1);
  		if (ret)
  			return ret;
  		ret = add_system_zone(sbi, ext4_inode_table(sb, gdp),
  				sbi->s_itb_per_group);
  		if (ret)
  			return ret;
  	}
  
  	if (test_opt(sb, DEBUG))
  		debug_print_tree(EXT4_SB(sb));
  	return 0;
  }
  
  /* Called when the filesystem is unmounted */
  void ext4_release_system_zone(struct super_block *sb)
  {
d1866bd06   Cody P Schafer   fs/ext4: use rbtr...
181
182
183
184
  	struct ext4_system_zone	*entry, *n;
  
  	rbtree_postorder_for_each_entry_safe(entry, n,
  			&EXT4_SB(sb)->system_blks, node)
6fd058f77   Theodore Ts'o   ext4: Add a compr...
185
  		kmem_cache_free(ext4_system_zone_cachep, entry);
d1866bd06   Cody P Schafer   fs/ext4: use rbtr...
186

64e290ec6   Venkatesh Pallipadi   ext4: fix up rb_r...
187
  	EXT4_SB(sb)->system_blks = RB_ROOT;
6fd058f77   Theodore Ts'o   ext4: Add a compr...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  }
  
  /*
   * Returns 1 if the passed-in block region (start_blk,
   * start_blk+count) is valid; 0 if some part of the block region
   * overlaps with filesystem metadata blocks.
   */
  int ext4_data_block_valid(struct ext4_sb_info *sbi, ext4_fsblk_t start_blk,
  			  unsigned int count)
  {
  	struct ext4_system_zone *entry;
  	struct rb_node *n = sbi->system_blks.rb_node;
  
  	if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
1585d8d89   Theodore Ts'o   ext4: add check f...
202
  	    (start_blk + count < start_blk) ||
1c13d5c08   Theodore Ts'o   ext4: Save error ...
203
204
  	    (start_blk + count > ext4_blocks_count(sbi->s_es))) {
  		sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
6fd058f77   Theodore Ts'o   ext4: Add a compr...
205
  		return 0;
1c13d5c08   Theodore Ts'o   ext4: Save error ...
206
  	}
6fd058f77   Theodore Ts'o   ext4: Add a compr...
207
208
209
210
211
212
  	while (n) {
  		entry = rb_entry(n, struct ext4_system_zone, node);
  		if (start_blk + count - 1 < entry->start_blk)
  			n = n->rb_left;
  		else if (start_blk >= (entry->start_blk + entry->count))
  			n = n->rb_right;
1c13d5c08   Theodore Ts'o   ext4: Save error ...
213
214
  		else {
  			sbi->s_es->s_last_error_block = cpu_to_le64(start_blk);
6fd058f77   Theodore Ts'o   ext4: Add a compr...
215
  			return 0;
1c13d5c08   Theodore Ts'o   ext4: Save error ...
216
  		}
6fd058f77   Theodore Ts'o   ext4: Add a compr...
217
218
219
  	}
  	return 1;
  }
1f7d1e774   Theodore Ts'o   ext4: move __ext4...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  int ext4_check_blockref(const char *function, unsigned int line,
  			struct inode *inode, __le32 *p, unsigned int max)
  {
  	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
  	__le32 *bref = p;
  	unsigned int blk;
  
  	while (bref < p+max) {
  		blk = le32_to_cpu(*bref++);
  		if (blk &&
  		    unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
  						    blk, 1))) {
  			es->s_last_error_block = cpu_to_le64(blk);
  			ext4_error_inode(inode, function, line, blk,
  					 "invalid block");
6a797d273   Darrick J. Wong   ext4: call out CR...
235
  			return -EFSCORRUPTED;
1f7d1e774   Theodore Ts'o   ext4: move __ext4...
236
237
238
239
  		}
  	}
  	return 0;
  }
dae1e52cb   Amir Goldstein   ext4: move ext4_i...
240