1 Index: linux-2.4.20/fs/ext3/ialloc.c
2 ===================================================================
3 --- linux-2.4.20.orig/fs/ext3/ialloc.c 2003-10-25 00:37:13.000000000 +0400
4 +++ linux-2.4.20/fs/ext3/ialloc.c 2003-10-29 20:33:33.000000000 +0300
7 bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
9 - BUFFER_TRACE(bh, "get_write_access");
10 - fatal = ext3_journal_get_write_access(handle, bh);
11 + BUFFER_TRACE(bh, "get_undo_access");
12 + fatal = ext3_journal_get_undo_access(handle, bh);
16 + /* to prevent inode reusing within single transaction -bzzz */
17 + BUFFER_TRACE(bh, "clear in b_committed_data");
18 + J_ASSERT_BH(bh, bh2jh(bh)->b_committed_data != NULL);
19 + ext3_set_bit(bit, bh2jh(bh)->b_committed_data);
21 /* Ok, now we can actually update the inode bitmaps.. */
22 if (!ext3_clear_bit (bit, bh->b_data))
23 ext3_error (sb, "ext3_free_inode",
28 +static int ext3_test_allocatable(int nr, struct buffer_head *bh)
30 + if (ext3_test_bit(nr, bh->b_data))
32 + if (!buffer_jbd(bh) || !bh2jh(bh)->b_committed_data)
35 + if (!ext3_test_bit(nr, bh2jh(bh)->b_committed_data))
36 + printk("EXT3-fs: inode %d was used\n", nr);
38 + return !ext3_test_bit(nr, bh2jh(bh)->b_committed_data);
41 +int ext3_find_group_dir(const struct inode *dir,
42 + struct ext3_group_desc **gdp,
43 + struct buffer_head **bh)
45 + struct super_block *sb = dir->i_sb;
46 + struct ext3_super_block *es;
47 + struct ext3_group_desc *tmp;
48 + int i = 0, j, avefreei;
50 + es = EXT3_SB(sb)->s_es;
51 + avefreei = le32_to_cpu(es->s_free_inodes_count) /
52 + sb->u.ext3_sb.s_groups_count;
53 + for (j = 0; j < sb->u.ext3_sb.s_groups_count; j++) {
54 + struct buffer_head *temp_buffer;
55 + tmp = ext3_get_group_desc(sb, j, &temp_buffer);
56 + if (tmp && le16_to_cpu(tmp->bg_free_inodes_count) &&
57 + le16_to_cpu(tmp->bg_free_inodes_count) >= avefreei) {
58 + if (!*gdp || (le16_to_cpu(tmp->bg_free_blocks_count) >
59 + le16_to_cpu((*gdp)->bg_free_blocks_count))) {
70 +int ext3_find_group_other(const struct inode *dir,
71 + struct ext3_group_desc **gdp,
72 + struct buffer_head **bh)
74 + struct super_block *sb = dir->i_sb;
75 + struct ext3_group_desc *tmp;
79 + * Try to place the inode in its parent directory
81 + i = dir->u.ext3_i.i_block_group;
82 + tmp = ext3_get_group_desc(sb, i, bh);
83 + if (tmp && le16_to_cpu(tmp->bg_free_inodes_count))
87 + * Use a quadratic hash to find a group with a
90 + for (j = 1; j < sb->u.ext3_sb.s_groups_count; j <<= 1) {
92 + if (i >= sb->u.ext3_sb.s_groups_count)
93 + i -= sb->u.ext3_sb.s_groups_count;
94 + tmp = ext3_get_group_desc (sb, i, bh);
95 + if (tmp && le16_to_cpu(tmp->bg_free_inodes_count)) {
103 + * That failed: try linear search for a free inode
105 + i = dir->u.ext3_i.i_block_group + 1;
106 + for (j = 2; j < sb->u.ext3_sb.s_groups_count; j++) {
107 + if (++i >= sb->u.ext3_sb.s_groups_count)
109 + tmp = ext3_get_group_desc (sb, i, bh);
110 + if (tmp && le16_to_cpu(tmp->bg_free_inodes_count)) {
120 +static int ext3_find_group(const struct inode *dir, int mode,
121 + struct ext3_group_desc **gdp,
122 + struct buffer_head **bh)
125 + return ext3_find_group_dir(dir, gdp, bh);
126 + return ext3_find_group_other(dir, gdp, bh);
129 +static int ext3_find_usable_inode(struct super_block *sb,
130 + struct buffer_head *bh)
132 + int here, maxinodes, next;
134 + maxinodes = EXT3_INODES_PER_GROUP(sb);
137 + while (here < maxinodes) {
138 + next = ext3_find_next_zero_bit((unsigned long *) bh->b_data,
140 + if (next >= maxinodes)
142 + if (ext3_test_allocatable(next, bh))
145 + J_ASSERT_BH(bh, bh2jh(bh)->b_committed_data);
146 + here = ext3_find_next_zero_bit
147 + ((unsigned long *) bh2jh(bh)->b_committed_data,
154 * There are two policies for allocating an inode. If the new inode is
155 * a directory, then a forward search is made for a block group with both
157 struct super_block * sb;
158 struct buffer_head * bh;
159 struct buffer_head * bh2;
160 - int i, j, avefreei;
162 struct inode * inode;
164 struct ext3_group_desc * gdp;
165 @@ -371,11 +501,12 @@
167 bh = EXT3_SB(sb)->s_inode_bitmap[bitmap_nr];
169 - BUFFER_TRACE(bh, "get_write_access");
170 - err = ext3_journal_get_write_access(handle, bh);
171 + BUFFER_TRACE(bh, "get_undo_access");
172 + err = ext3_journal_get_undo_access(handle, bh);
175 - if (ext3_set_bit(j, bh->b_data)) {
176 + if (!ext3_test_allocatable(j, bh) ||
177 + ext3_set_bit(j, bh->b_data)) {
178 printk(KERN_ERR "goal inode %lu unavailable\n", goal);
179 /* Oh well, we tried. */
181 @@ -393,119 +524,70 @@
187 - if (S_ISDIR(mode)) {
188 - avefreei = le32_to_cpu(es->s_free_inodes_count) /
189 - sb->u.ext3_sb.s_groups_count;
191 - for (j = 0; j < sb->u.ext3_sb.s_groups_count; j++) {
192 - struct buffer_head *temp_buffer;
193 - tmp = ext3_get_group_desc (sb, j, &temp_buffer);
195 - le16_to_cpu(tmp->bg_free_inodes_count) &&
196 - le16_to_cpu(tmp->bg_free_inodes_count) >=
198 - if (!gdp || (le16_to_cpu(tmp->bg_free_blocks_count) >
199 - le16_to_cpu(gdp->bg_free_blocks_count))) {
209 - * Try to place the inode in its parent directory
211 - i = dir->u.ext3_i.i_block_group;
212 - tmp = ext3_get_group_desc (sb, i, &bh2);
213 - if (tmp && le16_to_cpu(tmp->bg_free_inodes_count))
218 - * Use a quadratic hash to find a group with a
221 - for (j = 1; j < sb->u.ext3_sb.s_groups_count; j <<= 1) {
223 - if (i >= sb->u.ext3_sb.s_groups_count)
224 - i -= sb->u.ext3_sb.s_groups_count;
225 - tmp = ext3_get_group_desc (sb, i, &bh2);
227 - le16_to_cpu(tmp->bg_free_inodes_count)) {
235 - * That failed: try linear search for a free inode
237 - i = dir->u.ext3_i.i_block_group + 1;
238 - for (j = 2; j < sb->u.ext3_sb.s_groups_count; j++) {
239 - if (++i >= sb->u.ext3_sb.s_groups_count)
241 - tmp = ext3_get_group_desc (sb, i, &bh2);
243 - le16_to_cpu(tmp->bg_free_inodes_count)) {
252 + i = ext3_find_group(dir, mode, &gdp, &bh2);
259 - bitmap_nr = load_inode_bitmap (sb, i);
260 + bitmap_nr = load_inode_bitmap(sb, i);
264 bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
266 - if ((j = ext3_find_first_zero_bit ((unsigned long *) bh->b_data,
267 - EXT3_INODES_PER_GROUP(sb))) <
268 - EXT3_INODES_PER_GROUP(sb)) {
269 - BUFFER_TRACE(bh, "get_write_access");
270 - err = ext3_journal_get_write_access(handle, bh);
271 - if (err) goto fail;
273 - if (ext3_set_bit (j, bh->b_data)) {
274 - ext3_error (sb, "ext3_new_inode",
275 - "bit already set for inode %d", j);
278 - BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
279 - err = ext3_journal_dirty_metadata(handle, bh);
280 - if (err) goto fail;
282 - if (le16_to_cpu(gdp->bg_free_inodes_count) != 0) {
283 - ext3_error (sb, "ext3_new_inode",
284 - "Free inodes count corrupted in group %d",
286 - /* Is it really ENOSPC? */
288 - if (sb->s_flags & MS_RDONLY)
291 - BUFFER_TRACE(bh2, "get_write_access");
292 - err = ext3_journal_get_write_access(handle, bh2);
293 - if (err) goto fail;
294 - gdp->bg_free_inodes_count = 0;
295 - BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
296 - err = ext3_journal_dirty_metadata(handle, bh2);
297 - if (err) goto fail;
298 + /* try to allocate in selected group */
299 + j = ext3_find_usable_inode(sb, bh);
304 + /* can't allocate: try to allocate in ANY another groups */
307 + for (i = i + 1; i != k; i++) {
308 + if (i >= sb->u.ext3_sb.s_groups_count)
310 + tmp = ext3_get_group_desc(sb, i, &bh2);
311 + if (le16_to_cpu(tmp->bg_free_inodes_count) == 0)
314 + bitmap_nr = load_inode_bitmap(sb, i);
317 + bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
319 + /* try to allocate in selected group */
320 + if ((j = ext3_find_usable_inode(sb, bh)) >= 0) {
331 + BUFFER_TRACE(bh, "get_undo_access");
332 + err = ext3_journal_get_undo_access(handle, bh);
336 + if (ext3_set_bit(j, bh->b_data)) {
337 + ext3_error (sb, "ext3_new_inode",
338 + "bit already set for inode %d", j);
341 + BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
342 + err = ext3_journal_dirty_metadata(handle, bh);
347 + if (buffer_jbd(bh) && bh2jh(bh)->b_committed_data)
348 + J_ASSERT_BH(bh, !ext3_test_bit(j, bh2jh(bh)->b_committed_data));
350 j += i * EXT3_INODES_PER_GROUP(sb) + 1;
351 if (j < EXT3_FIRST_INO(sb) || j > le32_to_cpu(es->s_inodes_count)) {
352 ext3_error (sb, "ext3_new_inode",