2 * linux/fs/ext3/ialloc.c
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
9 * BSD ufs-inspired inode and directory allocation by
10 * Stephen Tweedie (sct@redhat.com), 1993
11 * Big-endian to little-endian byte-swapping/bitmaps by
12 * David S. Miller (davem@caip.rutgers.edu), 1995
15 #include <linux/quotaops.h>
16 #include <linux/random.h>
23 * ialloc.c contains the inodes allocation and deallocation routines
27 * The free inodes are managed by bitmaps. A file system contains several
28 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
29 * block for inodes, N blocks for the inode table and data blocks.
31 * The file system contains group descriptors which are located after the
32 * super block. Each descriptor contains the number of the bitmap block and
33 * the free blocks count in the block.
38 * Read the inode allocation bitmap for a given block_group, reading
39 * into the specified slot in the superblock's bitmap cache.
41 * Return buffer_head of bitmap on success or NULL.
43 static struct buffer_head
*
44 read_inode_bitmap(struct super_block
* sb
, unsigned long block_group
)
46 struct ext3_group_desc
*desc
;
47 struct buffer_head
*bh
= NULL
;
49 desc
= ext3_get_group_desc(sb
, block_group
, NULL
);
53 bh
= sb_bread(sb
, le32_to_cpu(desc
->bg_inode_bitmap
));
55 ext3_error(sb
, "read_inode_bitmap",
56 "Cannot read inode bitmap - "
57 "block_group = %lu, inode_bitmap = %u",
58 block_group
, le32_to_cpu(desc
->bg_inode_bitmap
));
64 * NOTE! When we get the inode, we're the only people
65 * that have access to it, and as such there are no
66 * race conditions we have to worry about. The inode
67 * is not on the hash-lists, and it cannot be reached
68 * through the filesystem because the directory entry
69 * has been deleted earlier.
71 * HOWEVER: we must make sure that we get no aliases,
72 * which means that we have to call "clear_inode()"
73 * _before_ we mark the inode not in use in the inode
74 * bitmaps. Otherwise a newly created file might use
75 * the same inode number (not actually the same pointer
76 * though), and then we'd have two inodes sharing the
77 * same inode number and space on the harddisk.
79 void ext3_free_inode (handle_t
*handle
, struct inode
* inode
)
81 struct super_block
* sb
= inode
->i_sb
;
84 struct buffer_head
*bitmap_bh
= NULL
;
85 struct buffer_head
*bh2
;
86 unsigned long block_group
;
88 struct ext3_group_desc
* gdp
;
89 struct ext3_super_block
* es
;
90 struct ext3_sb_info
*sbi
;
93 if (atomic_read(&inode
->i_count
) > 1) {
94 printk ("ext3_free_inode: inode has count=%d\n",
95 atomic_read(&inode
->i_count
));
99 printk ("ext3_free_inode: inode has nlink=%d\n",
104 printk("ext3_free_inode: inode on nonexistent device\n");
110 ext3_debug ("freeing inode %lu\n", ino
);
111 trace_ext3_free_inode(inode
);
113 is_directory
= S_ISDIR(inode
->i_mode
);
115 es
= EXT3_SB(sb
)->s_es
;
116 if (ino
< EXT3_FIRST_INO(sb
) || ino
> le32_to_cpu(es
->s_inodes_count
)) {
117 ext3_error (sb
, "ext3_free_inode",
118 "reserved or nonexistent inode %lu", ino
);
121 block_group
= (ino
- 1) / EXT3_INODES_PER_GROUP(sb
);
122 bit
= (ino
- 1) % EXT3_INODES_PER_GROUP(sb
);
123 bitmap_bh
= read_inode_bitmap(sb
, block_group
);
127 BUFFER_TRACE(bitmap_bh
, "get_write_access");
128 fatal
= ext3_journal_get_write_access(handle
, bitmap_bh
);
132 /* Ok, now we can actually update the inode bitmaps.. */
133 if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi
, block_group
),
134 bit
, bitmap_bh
->b_data
))
135 ext3_error (sb
, "ext3_free_inode",
136 "bit already cleared for inode %lu", ino
);
138 gdp
= ext3_get_group_desc (sb
, block_group
, &bh2
);
140 BUFFER_TRACE(bh2
, "get_write_access");
141 fatal
= ext3_journal_get_write_access(handle
, bh2
);
142 if (fatal
) goto error_return
;
145 spin_lock(sb_bgl_lock(sbi
, block_group
));
146 le16_add_cpu(&gdp
->bg_free_inodes_count
, 1);
148 le16_add_cpu(&gdp
->bg_used_dirs_count
, -1);
149 spin_unlock(sb_bgl_lock(sbi
, block_group
));
150 percpu_counter_inc(&sbi
->s_freeinodes_counter
);
152 percpu_counter_dec(&sbi
->s_dirs_counter
);
155 BUFFER_TRACE(bh2
, "call ext3_journal_dirty_metadata");
156 err
= ext3_journal_dirty_metadata(handle
, bh2
);
157 if (!fatal
) fatal
= err
;
159 BUFFER_TRACE(bitmap_bh
, "call ext3_journal_dirty_metadata");
160 err
= ext3_journal_dirty_metadata(handle
, bitmap_bh
);
166 ext3_std_error(sb
, fatal
);
170 * Orlov's allocator for directories.
172 * We always try to spread first-level directories.
174 * If there are blockgroups with both free inodes and free blocks counts
175 * not worse than average we return one with smallest directory count.
176 * Otherwise we simply return a random group.
178 * For the rest rules look so:
180 * It's OK to put directory into a group unless
181 * it has too many directories already (max_dirs) or
182 * it has too few free inodes left (min_inodes) or
183 * it has too few free blocks left (min_blocks) or
184 * it's already running too large debt (max_debt).
185 * Parent's group is preferred, if it doesn't satisfy these
186 * conditions we search cyclically through the rest. If none
187 * of the groups look good we just look for a group with more
188 * free inodes than average (starting at parent's group).
190 * Debt is incremented each time we allocate a directory and decremented
191 * when we allocate an inode, within 0--255.
194 #define INODE_COST 64
195 #define BLOCK_COST 256
197 static int find_group_orlov(struct super_block
*sb
, struct inode
*parent
)
199 int parent_group
= EXT3_I(parent
)->i_block_group
;
200 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
201 struct ext3_super_block
*es
= sbi
->s_es
;
202 int ngroups
= sbi
->s_groups_count
;
203 int inodes_per_group
= EXT3_INODES_PER_GROUP(sb
);
204 unsigned int freei
, avefreei
;
205 ext3_fsblk_t freeb
, avefreeb
;
206 ext3_fsblk_t blocks_per_dir
;
208 int max_debt
, max_dirs
, min_inodes
;
209 ext3_grpblk_t min_blocks
;
211 struct ext3_group_desc
*desc
;
213 freei
= percpu_counter_read_positive(&sbi
->s_freeinodes_counter
);
214 avefreei
= freei
/ ngroups
;
215 freeb
= percpu_counter_read_positive(&sbi
->s_freeblocks_counter
);
216 avefreeb
= freeb
/ ngroups
;
217 ndirs
= percpu_counter_read_positive(&sbi
->s_dirs_counter
);
219 if ((parent
== sb
->s_root
->d_inode
) ||
220 (EXT3_I(parent
)->i_flags
& EXT3_TOPDIR_FL
)) {
221 int best_ndir
= inodes_per_group
;
224 get_random_bytes(&group
, sizeof(group
));
225 parent_group
= (unsigned)group
% ngroups
;
226 for (i
= 0; i
< ngroups
; i
++) {
227 group
= (parent_group
+ i
) % ngroups
;
228 desc
= ext3_get_group_desc (sb
, group
, NULL
);
229 if (!desc
|| !desc
->bg_free_inodes_count
)
231 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= best_ndir
)
233 if (le16_to_cpu(desc
->bg_free_inodes_count
) < avefreei
)
235 if (le16_to_cpu(desc
->bg_free_blocks_count
) < avefreeb
)
238 best_ndir
= le16_to_cpu(desc
->bg_used_dirs_count
);
245 blocks_per_dir
= (le32_to_cpu(es
->s_blocks_count
) - freeb
) / ndirs
;
247 max_dirs
= ndirs
/ ngroups
+ inodes_per_group
/ 16;
248 min_inodes
= avefreei
- inodes_per_group
/ 4;
249 min_blocks
= avefreeb
- EXT3_BLOCKS_PER_GROUP(sb
) / 4;
251 max_debt
= EXT3_BLOCKS_PER_GROUP(sb
) / max(blocks_per_dir
, (ext3_fsblk_t
)BLOCK_COST
);
252 if (max_debt
* INODE_COST
> inodes_per_group
)
253 max_debt
= inodes_per_group
/ INODE_COST
;
259 for (i
= 0; i
< ngroups
; i
++) {
260 group
= (parent_group
+ i
) % ngroups
;
261 desc
= ext3_get_group_desc (sb
, group
, NULL
);
262 if (!desc
|| !desc
->bg_free_inodes_count
)
264 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= max_dirs
)
266 if (le16_to_cpu(desc
->bg_free_inodes_count
) < min_inodes
)
268 if (le16_to_cpu(desc
->bg_free_blocks_count
) < min_blocks
)
274 for (i
= 0; i
< ngroups
; i
++) {
275 group
= (parent_group
+ i
) % ngroups
;
276 desc
= ext3_get_group_desc (sb
, group
, NULL
);
277 if (!desc
|| !desc
->bg_free_inodes_count
)
279 if (le16_to_cpu(desc
->bg_free_inodes_count
) >= avefreei
)
285 * The free-inodes counter is approximate, and for really small
286 * filesystems the above test can fail to find any blockgroups
295 static int find_group_other(struct super_block
*sb
, struct inode
*parent
)
297 int parent_group
= EXT3_I(parent
)->i_block_group
;
298 int ngroups
= EXT3_SB(sb
)->s_groups_count
;
299 struct ext3_group_desc
*desc
;
303 * Try to place the inode in its parent directory
305 group
= parent_group
;
306 desc
= ext3_get_group_desc (sb
, group
, NULL
);
307 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
308 le16_to_cpu(desc
->bg_free_blocks_count
))
312 * We're going to place this inode in a different blockgroup from its
313 * parent. We want to cause files in a common directory to all land in
314 * the same blockgroup. But we want files which are in a different
315 * directory which shares a blockgroup with our parent to land in a
316 * different blockgroup.
318 * So add our directory's i_ino into the starting point for the hash.
320 group
= (group
+ parent
->i_ino
) % ngroups
;
323 * Use a quadratic hash to find a group with a free inode and some free
326 for (i
= 1; i
< ngroups
; i
<<= 1) {
328 if (group
>= ngroups
)
330 desc
= ext3_get_group_desc (sb
, group
, NULL
);
331 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
332 le16_to_cpu(desc
->bg_free_blocks_count
))
337 * That failed: try linear search for a free inode, even if that group
338 * has no free blocks.
340 group
= parent_group
;
341 for (i
= 0; i
< ngroups
; i
++) {
342 if (++group
>= ngroups
)
344 desc
= ext3_get_group_desc (sb
, group
, NULL
);
345 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
))
353 * There are two policies for allocating an inode. If the new inode is
354 * a directory, then a forward search is made for a block group with both
355 * free space and a low directory-to-inode ratio; if that fails, then of
356 * the groups with above-average free space, that group with the fewest
357 * directories already is chosen.
359 * For other inodes, search forward from the parent directory's block
360 * group to find a free inode.
362 struct inode
*ext3_new_inode(handle_t
*handle
, struct inode
* dir
,
363 const struct qstr
*qstr
, umode_t mode
)
365 struct super_block
*sb
;
366 struct buffer_head
*bitmap_bh
= NULL
;
367 struct buffer_head
*bh2
;
369 unsigned long ino
= 0;
370 struct inode
* inode
;
371 struct ext3_group_desc
* gdp
= NULL
;
372 struct ext3_super_block
* es
;
373 struct ext3_inode_info
*ei
;
374 struct ext3_sb_info
*sbi
;
379 /* Cannot create files in a deleted directory */
380 if (!dir
|| !dir
->i_nlink
)
381 return ERR_PTR(-EPERM
);
384 trace_ext3_request_inode(dir
, mode
);
385 inode
= new_inode(sb
);
387 return ERR_PTR(-ENOMEM
);
393 group
= find_group_orlov(sb
, dir
);
395 group
= find_group_other(sb
, dir
);
401 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
404 gdp
= ext3_get_group_desc(sb
, group
, &bh2
);
409 bitmap_bh
= read_inode_bitmap(sb
, group
);
415 repeat_in_this_group
:
416 ino
= ext3_find_next_zero_bit((unsigned long *)
417 bitmap_bh
->b_data
, EXT3_INODES_PER_GROUP(sb
), ino
);
418 if (ino
< EXT3_INODES_PER_GROUP(sb
)) {
420 BUFFER_TRACE(bitmap_bh
, "get_write_access");
421 err
= ext3_journal_get_write_access(handle
, bitmap_bh
);
425 if (!ext3_set_bit_atomic(sb_bgl_lock(sbi
, group
),
426 ino
, bitmap_bh
->b_data
)) {
428 BUFFER_TRACE(bitmap_bh
,
429 "call ext3_journal_dirty_metadata");
430 err
= ext3_journal_dirty_metadata(handle
,
437 journal_release_buffer(handle
, bitmap_bh
);
439 if (++ino
< EXT3_INODES_PER_GROUP(sb
))
440 goto repeat_in_this_group
;
444 * This case is possible in concurrent environment. It is very
445 * rare. We cannot repeat the find_group_xxx() call because
446 * that will simply return the same blockgroup, because the
447 * group descriptor metadata has not yet been updated.
448 * So we just go onto the next blockgroup.
450 if (++group
== sbi
->s_groups_count
)
457 ino
+= group
* EXT3_INODES_PER_GROUP(sb
) + 1;
458 if (ino
< EXT3_FIRST_INO(sb
) || ino
> le32_to_cpu(es
->s_inodes_count
)) {
459 ext3_error (sb
, "ext3_new_inode",
460 "reserved inode or inode > inodes count - "
461 "block_group = %d, inode=%lu", group
, ino
);
466 BUFFER_TRACE(bh2
, "get_write_access");
467 err
= ext3_journal_get_write_access(handle
, bh2
);
469 spin_lock(sb_bgl_lock(sbi
, group
));
470 le16_add_cpu(&gdp
->bg_free_inodes_count
, -1);
472 le16_add_cpu(&gdp
->bg_used_dirs_count
, 1);
474 spin_unlock(sb_bgl_lock(sbi
, group
));
475 BUFFER_TRACE(bh2
, "call ext3_journal_dirty_metadata");
476 err
= ext3_journal_dirty_metadata(handle
, bh2
);
479 percpu_counter_dec(&sbi
->s_freeinodes_counter
);
481 percpu_counter_inc(&sbi
->s_dirs_counter
);
484 if (test_opt(sb
, GRPID
)) {
485 inode
->i_mode
= mode
;
486 inode
->i_uid
= current_fsuid();
487 inode
->i_gid
= dir
->i_gid
;
489 inode_init_owner(inode
, dir
, mode
);
492 /* This is the optimal IO size (for stat), not the fs block size */
494 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
496 memset(ei
->i_data
, 0, sizeof(ei
->i_data
));
497 ei
->i_dir_start_lookup
= 0;
501 ext3_mask_flags(mode
, EXT3_I(dir
)->i_flags
& EXT3_FL_INHERITED
);
502 #ifdef EXT3_FRAGMENTS
510 ei
->i_block_alloc_info
= NULL
;
511 ei
->i_block_group
= group
;
513 ext3_set_inode_flags(inode
);
514 if (IS_DIRSYNC(inode
))
516 if (insert_inode_locked(inode
) < 0) {
518 * Likely a bitmap corruption causing inode to be allocated
524 spin_lock(&sbi
->s_next_gen_lock
);
525 inode
->i_generation
= sbi
->s_next_generation
++;
526 spin_unlock(&sbi
->s_next_gen_lock
);
528 ei
->i_state_flags
= 0;
529 ext3_set_inode_state(inode
, EXT3_STATE_NEW
);
531 /* See comment in ext3_iget for explanation */
532 if (ino
>= EXT3_FIRST_INO(sb
) + 1 &&
533 EXT3_INODE_SIZE(sb
) > EXT3_GOOD_OLD_INODE_SIZE
) {
535 sizeof(struct ext3_inode
) - EXT3_GOOD_OLD_INODE_SIZE
;
537 ei
->i_extra_isize
= 0;
541 dquot_initialize(inode
);
542 err
= dquot_alloc_inode(inode
);
546 err
= ext3_init_acl(handle
, inode
, dir
);
550 err
= ext3_init_security(handle
, inode
, dir
, qstr
);
554 err
= ext3_mark_inode_dirty(handle
, inode
);
556 ext3_std_error(sb
, err
);
560 ext3_debug("allocating inode %lu\n", inode
->i_ino
);
561 trace_ext3_allocate_inode(inode
, dir
, mode
);
564 ext3_std_error(sb
, err
);
573 dquot_free_inode(inode
);
577 inode
->i_flags
|= S_NOQUOTA
;
579 unlock_new_inode(inode
);
585 /* Verify that we are loading a valid orphan from disk */
586 struct inode
*ext3_orphan_get(struct super_block
*sb
, unsigned long ino
)
588 unsigned long max_ino
= le32_to_cpu(EXT3_SB(sb
)->s_es
->s_inodes_count
);
589 unsigned long block_group
;
591 struct buffer_head
*bitmap_bh
;
592 struct inode
*inode
= NULL
;
595 /* Error cases - e2fsck has already cleaned up for us */
597 ext3_warning(sb
, __func__
,
598 "bad orphan ino %lu! e2fsck was run?", ino
);
602 block_group
= (ino
- 1) / EXT3_INODES_PER_GROUP(sb
);
603 bit
= (ino
- 1) % EXT3_INODES_PER_GROUP(sb
);
604 bitmap_bh
= read_inode_bitmap(sb
, block_group
);
606 ext3_warning(sb
, __func__
,
607 "inode bitmap error for orphan %lu", ino
);
611 /* Having the inode bit set should be a 100% indicator that this
612 * is a valid orphan (no e2fsck run on fs). Orphans also include
613 * inodes that were being truncated, so we can't check i_nlink==0.
615 if (!ext3_test_bit(bit
, bitmap_bh
->b_data
))
618 inode
= ext3_iget(sb
, ino
);
623 * If the orphans has i_nlinks > 0 then it should be able to be
624 * truncated, otherwise it won't be removed from the orphan list
625 * during processing and an infinite loop will result.
627 if (inode
->i_nlink
&& !ext3_can_truncate(inode
))
630 if (NEXT_ORPHAN(inode
) > max_ino
)
636 err
= PTR_ERR(inode
);
639 ext3_warning(sb
, __func__
,
640 "bad orphan inode %lu! e2fsck was run?", ino
);
641 printk(KERN_NOTICE
"ext3_test_bit(bit=%d, block=%llu) = %d\n",
642 bit
, (unsigned long long)bitmap_bh
->b_blocknr
,
643 ext3_test_bit(bit
, bitmap_bh
->b_data
));
644 printk(KERN_NOTICE
"inode=%p\n", inode
);
646 printk(KERN_NOTICE
"is_bad_inode(inode)=%d\n",
647 is_bad_inode(inode
));
648 printk(KERN_NOTICE
"NEXT_ORPHAN(inode)=%u\n",
650 printk(KERN_NOTICE
"max_ino=%lu\n", max_ino
);
651 printk(KERN_NOTICE
"i_nlink=%u\n", inode
->i_nlink
);
652 /* Avoid freeing blocks if we got a bad deleted inode */
653 if (inode
->i_nlink
== 0)
662 unsigned long ext3_count_free_inodes (struct super_block
* sb
)
664 unsigned long desc_count
;
665 struct ext3_group_desc
*gdp
;
668 struct ext3_super_block
*es
;
669 unsigned long bitmap_count
, x
;
670 struct buffer_head
*bitmap_bh
= NULL
;
672 es
= EXT3_SB(sb
)->s_es
;
676 for (i
= 0; i
< EXT3_SB(sb
)->s_groups_count
; i
++) {
677 gdp
= ext3_get_group_desc (sb
, i
, NULL
);
680 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
682 bitmap_bh
= read_inode_bitmap(sb
, i
);
686 x
= ext3_count_free(bitmap_bh
, EXT3_INODES_PER_GROUP(sb
) / 8);
687 printk("group %d: stored = %d, counted = %lu\n",
688 i
, le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
692 printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
693 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
697 for (i
= 0; i
< EXT3_SB(sb
)->s_groups_count
; i
++) {
698 gdp
= ext3_get_group_desc (sb
, i
, NULL
);
701 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
708 /* Called at mount-time, super-block is locked */
709 unsigned long ext3_count_dirs (struct super_block
* sb
)
711 unsigned long count
= 0;
714 for (i
= 0; i
< EXT3_SB(sb
)->s_groups_count
; i
++) {
715 struct ext3_group_desc
*gdp
= ext3_get_group_desc (sb
, i
, NULL
);
718 count
+= le16_to_cpu(gdp
->bg_used_dirs_count
);