1 ext4: fixes block group number being set to a negative value
3 From: Avantika Mathur <mathur@us.ibm.com>
5 This patch fixes various places where the group number is set to a negative
8 Signed-off-by: Avantika Mathur <mathur@us.ibm.com>
9 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
12 fs/ext4/ialloc.c | 101 ++++++++++++++++++++++++++++--------------------------
13 1 files changed, 53 insertions(+), 48 deletions(-)
16 diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
17 index 64dea86..7b5cfa6 100644
18 --- a/fs/ext4/ialloc.c
19 +++ b/fs/ext4/ialloc.c
20 @@ -260,12 +260,14 @@ error_return:
21 * For other inodes, search forward from the parent directory\'s block
22 * group to find a free inode.
24 -static ext4_group_t find_group_dir(struct super_block *sb, struct inode *parent)
25 +static int find_group_dir(struct super_block *sb, struct inode *parent,
26 + ext4_group_t *best_group)
28 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
29 unsigned int freei, avefreei;
30 struct ext4_group_desc *desc, *best_desc = NULL;
31 - ext4_group_t group, best_group = -1;
35 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
36 avefreei = freei / ngroups;
37 @@ -279,11 +281,12 @@ static ext4_group_t find_group_dir(struct super_block *sb, struct inode *parent)
39 (le16_to_cpu(desc->bg_free_blocks_count) >
40 le16_to_cpu(best_desc->bg_free_blocks_count))) {
42 + *best_group = group;
52 @@ -314,8 +317,8 @@ static ext4_group_t find_group_dir(struct super_block *sb, struct inode *parent)
54 #define BLOCK_COST 256
56 -static ext4_group_t find_group_orlov(struct super_block *sb,
57 - struct inode *parent)
58 +static int find_group_orlov(struct super_block *sb, struct inode *parent,
59 + ext4_group_t *group)
61 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
62 struct ext4_sb_info *sbi = EXT4_SB(sb);
63 @@ -328,7 +331,7 @@ static ext4_group_t find_group_orlov(struct super_block *sb,
65 int max_debt, max_dirs, min_inodes;
66 ext4_grpblk_t min_blocks;
67 - ext4_group_t group = -1, i;
69 struct ext4_group_desc *desc;
71 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
72 @@ -341,13 +344,14 @@ static ext4_group_t find_group_orlov(struct super_block *sb,
73 if ((parent == sb->s_root->d_inode) ||
74 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
75 int best_ndir = inodes_per_group;
76 - ext4_group_t best_group = -1;
80 - get_random_bytes(&group, sizeof(group));
81 - parent_group = (unsigned)group % ngroups;
82 + get_random_bytes(&grp, sizeof(grp));
83 + parent_group = (unsigned)grp % ngroups;
84 for (i = 0; i < ngroups; i++) {
85 - group = (parent_group + i) % ngroups;
86 - desc = ext4_get_group_desc (sb, group, NULL);
87 + grp = (parent_group + i) % ngroups;
88 + desc = ext4_get_group_desc(sb, grp, NULL);
89 if (!desc || !desc->bg_free_inodes_count)
91 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
92 @@ -356,11 +360,12 @@ static ext4_group_t find_group_orlov(struct super_block *sb,
94 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
99 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
101 - if (best_group >= 0)
108 @@ -381,8 +386,8 @@ static ext4_group_t find_group_orlov(struct super_block *sb,
111 for (i = 0; i < ngroups; i++) {
112 - group = (parent_group + i) % ngroups;
113 - desc = ext4_get_group_desc (sb, group, NULL);
114 + *group = (parent_group + i) % ngroups;
115 + desc = ext4_get_group_desc(sb, *group, NULL);
116 if (!desc || !desc->bg_free_inodes_count)
118 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
119 @@ -391,17 +396,16 @@ static ext4_group_t find_group_orlov(struct super_block *sb,
121 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
128 for (i = 0; i < ngroups; i++) {
129 - group = (parent_group + i) % ngroups;
130 - desc = ext4_get_group_desc (sb, group, NULL);
131 - if (!desc || !desc->bg_free_inodes_count)
133 - if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
135 + *group = (parent_group + i) % ngroups;
136 + desc = ext4_get_group_desc(sb, *group, NULL);
137 + if (desc && desc->bg_free_inodes_count &&
138 + le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
143 @@ -416,22 +420,22 @@ fallback:
147 -static ext4_group_t find_group_other(struct super_block *sb,
148 - struct inode *parent)
149 +static int find_group_other(struct super_block *sb, struct inode *parent,
150 + ext4_group_t *group)
152 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
153 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
154 struct ext4_group_desc *desc;
155 - ext4_group_t group, i;
159 * Try to place the inode in its parent directory
161 - group = parent_group;
162 - desc = ext4_get_group_desc (sb, group, NULL);
163 + *group = parent_group;
164 + desc = ext4_get_group_desc(sb, *group, NULL);
165 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
166 le16_to_cpu(desc->bg_free_blocks_count))
171 * We're going to place this inode in a different blockgroup from its
172 @@ -442,33 +446,33 @@ static ext4_group_t find_group_other(struct super_block *sb,
174 * So add our directory's i_ino into the starting point for the hash.
176 - group = (group + parent->i_ino) % ngroups;
177 + *group = (*group + parent->i_ino) % ngroups;
180 * Use a quadratic hash to find a group with a free inode and some free
183 for (i = 1; i < ngroups; i <<= 1) {
185 - if (group >= ngroups)
187 - desc = ext4_get_group_desc (sb, group, NULL);
189 + if (*group >= ngroups)
191 + desc = ext4_get_group_desc(sb, *group, NULL);
192 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
193 le16_to_cpu(desc->bg_free_blocks_count))
199 * That failed: try linear search for a free inode, even if that group
200 * has no free blocks.
202 - group = parent_group;
203 + *group = parent_group;
204 for (i = 0; i < ngroups; i++) {
205 - if (++group >= ngroups)
207 - desc = ext4_get_group_desc (sb, group, NULL);
208 + if (++*group >= ngroups)
210 + desc = ext4_get_group_desc(sb, *group, NULL);
211 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
217 @@ -489,16 +493,17 @@ struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
218 struct super_block *sb;
219 struct buffer_head *bitmap_bh = NULL;
220 struct buffer_head *bh2;
221 - ext4_group_t group;
222 + ext4_group_t group = 0;
223 unsigned long ino = 0;
224 struct inode * inode;
225 struct ext4_group_desc * gdp = NULL;
226 struct ext4_super_block * es;
227 struct ext4_inode_info *ei;
228 struct ext4_sb_info *sbi;
236 /* Cannot create files in a deleted directory */
237 if (!dir || !dir->i_nlink)
238 @@ -514,14 +519,14 @@ struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
241 if (test_opt (sb, OLDALLOC))
242 - group = find_group_dir(sb, dir);
243 + ret2 = find_group_dir(sb, dir, &group);
245 - group = find_group_orlov(sb, dir);
246 + ret2 = find_group_orlov(sb, dir, &group);
248 - group = find_group_other(sb, dir);
249 + ret2 = find_group_other(sb, dir, &group);
256 for (i = 0; i < sbi->s_groups_count; i++) {