More patch description fixups. Standardize case.
[ext4-patch-queue.git] / ext4_grp_t_int_fix.patch
blobeaa787fe425e7355009a10e4fc76eb6570a6ba68
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
6 value.
8 Signed-off-by: Avantika Mathur <mathur@us.ibm.com>
9 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
10 ---
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;
32 + ext4_group_t group;
33 + int ret = -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)
38 if (!best_desc ||
39 (le16_to_cpu(desc->bg_free_blocks_count) >
40 le16_to_cpu(best_desc->bg_free_blocks_count))) {
41 - best_group = group;
42 + *best_group = group;
43 best_desc = desc;
44 + ret = 0;
47 - return best_group;
48 + return ret;
52 @@ -314,8 +317,8 @@ static ext4_group_t find_group_dir(struct super_block *sb, struct inode *parent)
53 #define INODE_COST 64
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,
64 unsigned int ndirs;
65 int max_debt, max_dirs, min_inodes;
66 ext4_grpblk_t min_blocks;
67 - ext4_group_t group = -1, i;
68 + ext4_group_t 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;
77 + ext4_group_t grp;
78 + int ret = -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)
90 continue;
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,
93 continue;
94 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
95 continue;
96 - best_group = group;
97 + *group = grp;
98 + ret = 0;
99 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
101 - if (best_group >= 0)
102 - return best_group;
103 + if (ret == 0)
104 + return ret;
105 goto fallback;
108 @@ -381,8 +386,8 @@ static ext4_group_t find_group_orlov(struct super_block *sb,
109 max_debt = 1;
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)
117 continue;
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,
120 continue;
121 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
122 continue;
123 - return group;
124 + return 0;
127 fallback:
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)
132 - continue;
133 - if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
134 - return group;
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)
139 + return 0;
142 if (avefreei) {
143 @@ -416,22 +420,22 @@ fallback:
144 return -1;
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;
156 + ext4_group_t 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))
167 - return group;
168 + return 0;
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
181 * blocks.
183 for (i = 1; i < ngroups; i <<= 1) {
184 - group += i;
185 - if (group >= ngroups)
186 - group -= ngroups;
187 - desc = ext4_get_group_desc (sb, group, NULL);
188 + *group += i;
189 + if (*group >= ngroups)
190 + *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))
194 - return group;
195 + return 0;
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)
206 - group = 0;
207 - desc = ext4_get_group_desc (sb, group, NULL);
208 + if (++*group >= ngroups)
209 + *group = 0;
210 + desc = ext4_get_group_desc(sb, *group, NULL);
211 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
212 - return group;
213 + return 0;
216 return -1;
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;
229 - int err = 0;
230 + int ret2, err = 0;
231 struct inode *ret;
232 - int i, free = 0;
233 + ext4_group_t i;
234 + int free = 0;
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)
239 es = sbi->s_es;
240 if (S_ISDIR(mode)) {
241 if (test_opt (sb, OLDALLOC))
242 - group = find_group_dir(sb, dir);
243 + ret2 = find_group_dir(sb, dir, &group);
244 else
245 - group = find_group_orlov(sb, dir);
246 + ret2 = find_group_orlov(sb, dir, &group);
247 } else
248 - group = find_group_other(sb, dir);
249 + ret2 = find_group_other(sb, dir, &group);
251 err = -ENOSPC;
252 - if (group == -1)
253 + if (ret2 == -1)
254 goto out;
256 for (i = 0; i < sbi->s_groups_count; i++) {