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/time.h>
17 #include <linux/jbd.h>
18 #include <linux/ext3_fs.h>
19 #include <linux/ext3_jbd.h>
20 #include <linux/stat.h>
21 #include <linux/string.h>
22 #include <linux/quotaops.h>
23 #include <linux/buffer_head.h>
24 #include <linux/random.h>
25 #include <linux/bitops.h>
26 #include <trace/events/ext3.h>
28 #include <asm/byteorder.h>
34 * ialloc.c contains the inodes allocation and deallocation routines
38 * The free inodes are managed by bitmaps. A file system contains several
39 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
40 * block for inodes, N blocks for the inode table and data blocks.
42 * The file system contains group descriptors which are located after the
43 * super block. Each descriptor contains the number of the bitmap block and
44 * the free blocks count in the block.
49 * Read the inode allocation bitmap for a given block_group, reading
50 * into the specified slot in the superblock's bitmap cache.
52 * Return buffer_head of bitmap on success or NULL.
54 static struct buffer_head
*
55 read_inode_bitmap(struct super_block
* sb
, unsigned long block_group
)
57 struct ext3_group_desc
*desc
;
58 struct buffer_head
*bh
= NULL
;
60 desc
= ext3_get_group_desc(sb
, block_group
, NULL
);
64 bh
= sb_bread(sb
, le32_to_cpu(desc
->bg_inode_bitmap
));
66 ext3_error(sb
, "read_inode_bitmap",
67 "Cannot read inode bitmap - "
68 "block_group = %lu, inode_bitmap = %u",
69 block_group
, le32_to_cpu(desc
->bg_inode_bitmap
));
75 * NOTE! When we get the inode, we're the only people
76 * that have access to it, and as such there are no
77 * race conditions we have to worry about. The inode
78 * is not on the hash-lists, and it cannot be reached
79 * through the filesystem because the directory entry
80 * has been deleted earlier.
82 * HOWEVER: we must make sure that we get no aliases,
83 * which means that we have to call "clear_inode()"
84 * _before_ we mark the inode not in use in the inode
85 * bitmaps. Otherwise a newly created file might use
86 * the same inode number (not actually the same pointer
87 * though), and then we'd have two inodes sharing the
88 * same inode number and space on the harddisk.
90 void ext3_free_inode (handle_t
*handle
, struct inode
* inode
)
92 struct super_block
* sb
= inode
->i_sb
;
95 struct buffer_head
*bitmap_bh
= NULL
;
96 struct buffer_head
*bh2
;
97 unsigned long block_group
;
99 struct ext3_group_desc
* gdp
;
100 struct ext3_super_block
* es
;
101 struct ext3_sb_info
*sbi
;
104 if (atomic_read(&inode
->i_count
) > 1) {
105 printk ("ext3_free_inode: inode has count=%d\n",
106 atomic_read(&inode
->i_count
));
109 if (inode
->i_nlink
) {
110 printk ("ext3_free_inode: inode has nlink=%d\n",
115 printk("ext3_free_inode: inode on nonexistent device\n");
121 ext3_debug ("freeing inode %lu\n", ino
);
122 trace_ext3_free_inode(inode
);
124 is_directory
= S_ISDIR(inode
->i_mode
);
126 es
= EXT3_SB(sb
)->s_es
;
127 if (ino
< EXT3_FIRST_INO(sb
) || ino
> le32_to_cpu(es
->s_inodes_count
)) {
128 ext3_error (sb
, "ext3_free_inode",
129 "reserved or nonexistent inode %lu", ino
);
132 block_group
= (ino
- 1) / EXT3_INODES_PER_GROUP(sb
);
133 bit
= (ino
- 1) % EXT3_INODES_PER_GROUP(sb
);
134 bitmap_bh
= read_inode_bitmap(sb
, block_group
);
138 BUFFER_TRACE(bitmap_bh
, "get_write_access");
139 fatal
= ext3_journal_get_write_access(handle
, bitmap_bh
);
143 /* Ok, now we can actually update the inode bitmaps.. */
144 if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi
, block_group
),
145 bit
, bitmap_bh
->b_data
))
146 ext3_error (sb
, "ext3_free_inode",
147 "bit already cleared for inode %lu", ino
);
149 gdp
= ext3_get_group_desc (sb
, block_group
, &bh2
);
151 BUFFER_TRACE(bh2
, "get_write_access");
152 fatal
= ext3_journal_get_write_access(handle
, bh2
);
153 if (fatal
) goto error_return
;
156 spin_lock(sb_bgl_lock(sbi
, block_group
));
157 le16_add_cpu(&gdp
->bg_free_inodes_count
, 1);
159 le16_add_cpu(&gdp
->bg_used_dirs_count
, -1);
160 spin_unlock(sb_bgl_lock(sbi
, block_group
));
161 percpu_counter_inc(&sbi
->s_freeinodes_counter
);
163 percpu_counter_dec(&sbi
->s_dirs_counter
);
166 BUFFER_TRACE(bh2
, "call ext3_journal_dirty_metadata");
167 err
= ext3_journal_dirty_metadata(handle
, bh2
);
168 if (!fatal
) fatal
= err
;
170 BUFFER_TRACE(bitmap_bh
, "call ext3_journal_dirty_metadata");
171 err
= ext3_journal_dirty_metadata(handle
, bitmap_bh
);
177 ext3_std_error(sb
, fatal
);
181 * There are two policies for allocating an inode. If the new inode is
182 * a directory, then a forward search is made for a block group with both
183 * free space and a low directory-to-inode ratio; if that fails, then of
184 * the groups with above-average free space, that group with the fewest
185 * directories already is chosen.
187 * For other inodes, search forward from the parent directory\'s block
188 * group to find a free inode.
190 static int find_group_dir(struct super_block
*sb
, struct inode
*parent
)
192 int ngroups
= EXT3_SB(sb
)->s_groups_count
;
193 unsigned int freei
, avefreei
;
194 struct ext3_group_desc
*desc
, *best_desc
= NULL
;
195 int group
, best_group
= -1;
197 freei
= percpu_counter_read_positive(&EXT3_SB(sb
)->s_freeinodes_counter
);
198 avefreei
= freei
/ ngroups
;
200 for (group
= 0; group
< ngroups
; group
++) {
201 desc
= ext3_get_group_desc (sb
, group
, NULL
);
202 if (!desc
|| !desc
->bg_free_inodes_count
)
204 if (le16_to_cpu(desc
->bg_free_inodes_count
) < avefreei
)
207 (le16_to_cpu(desc
->bg_free_blocks_count
) >
208 le16_to_cpu(best_desc
->bg_free_blocks_count
))) {
217 * Orlov's allocator for directories.
219 * We always try to spread first-level directories.
221 * If there are blockgroups with both free inodes and free blocks counts
222 * not worse than average we return one with smallest directory count.
223 * Otherwise we simply return a random group.
225 * For the rest rules look so:
227 * It's OK to put directory into a group unless
228 * it has too many directories already (max_dirs) or
229 * it has too few free inodes left (min_inodes) or
230 * it has too few free blocks left (min_blocks) or
231 * it's already running too large debt (max_debt).
232 * Parent's group is preferred, if it doesn't satisfy these
233 * conditions we search cyclically through the rest. If none
234 * of the groups look good we just look for a group with more
235 * free inodes than average (starting at parent's group).
237 * Debt is incremented each time we allocate a directory and decremented
238 * when we allocate an inode, within 0--255.
241 #define INODE_COST 64
242 #define BLOCK_COST 256
244 static int find_group_orlov(struct super_block
*sb
, struct inode
*parent
)
246 int parent_group
= EXT3_I(parent
)->i_block_group
;
247 struct ext3_sb_info
*sbi
= EXT3_SB(sb
);
248 struct ext3_super_block
*es
= sbi
->s_es
;
249 int ngroups
= sbi
->s_groups_count
;
250 int inodes_per_group
= EXT3_INODES_PER_GROUP(sb
);
251 unsigned int freei
, avefreei
;
252 ext3_fsblk_t freeb
, avefreeb
;
253 ext3_fsblk_t blocks_per_dir
;
255 int max_debt
, max_dirs
, min_inodes
;
256 ext3_grpblk_t min_blocks
;
258 struct ext3_group_desc
*desc
;
260 freei
= percpu_counter_read_positive(&sbi
->s_freeinodes_counter
);
261 avefreei
= freei
/ ngroups
;
262 freeb
= percpu_counter_read_positive(&sbi
->s_freeblocks_counter
);
263 avefreeb
= freeb
/ ngroups
;
264 ndirs
= percpu_counter_read_positive(&sbi
->s_dirs_counter
);
266 if ((parent
== sb
->s_root
->d_inode
) ||
267 (EXT3_I(parent
)->i_flags
& EXT3_TOPDIR_FL
)) {
268 int best_ndir
= inodes_per_group
;
271 get_random_bytes(&group
, sizeof(group
));
272 parent_group
= (unsigned)group
% ngroups
;
273 for (i
= 0; i
< ngroups
; i
++) {
274 group
= (parent_group
+ i
) % ngroups
;
275 desc
= ext3_get_group_desc (sb
, group
, NULL
);
276 if (!desc
|| !desc
->bg_free_inodes_count
)
278 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= best_ndir
)
280 if (le16_to_cpu(desc
->bg_free_inodes_count
) < avefreei
)
282 if (le16_to_cpu(desc
->bg_free_blocks_count
) < avefreeb
)
285 best_ndir
= le16_to_cpu(desc
->bg_used_dirs_count
);
292 blocks_per_dir
= (le32_to_cpu(es
->s_blocks_count
) - freeb
) / ndirs
;
294 max_dirs
= ndirs
/ ngroups
+ inodes_per_group
/ 16;
295 min_inodes
= avefreei
- inodes_per_group
/ 4;
296 min_blocks
= avefreeb
- EXT3_BLOCKS_PER_GROUP(sb
) / 4;
298 max_debt
= EXT3_BLOCKS_PER_GROUP(sb
) / max(blocks_per_dir
, (ext3_fsblk_t
)BLOCK_COST
);
299 if (max_debt
* INODE_COST
> inodes_per_group
)
300 max_debt
= inodes_per_group
/ INODE_COST
;
306 for (i
= 0; i
< ngroups
; i
++) {
307 group
= (parent_group
+ i
) % ngroups
;
308 desc
= ext3_get_group_desc (sb
, group
, NULL
);
309 if (!desc
|| !desc
->bg_free_inodes_count
)
311 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= max_dirs
)
313 if (le16_to_cpu(desc
->bg_free_inodes_count
) < min_inodes
)
315 if (le16_to_cpu(desc
->bg_free_blocks_count
) < min_blocks
)
321 for (i
= 0; i
< ngroups
; i
++) {
322 group
= (parent_group
+ i
) % ngroups
;
323 desc
= ext3_get_group_desc (sb
, group
, NULL
);
324 if (!desc
|| !desc
->bg_free_inodes_count
)
326 if (le16_to_cpu(desc
->bg_free_inodes_count
) >= avefreei
)
332 * The free-inodes counter is approximate, and for really small
333 * filesystems the above test can fail to find any blockgroups
342 static int find_group_other(struct super_block
*sb
, struct inode
*parent
)
344 int parent_group
= EXT3_I(parent
)->i_block_group
;
345 int ngroups
= EXT3_SB(sb
)->s_groups_count
;
346 struct ext3_group_desc
*desc
;
350 * Try to place the inode in its parent directory
352 group
= parent_group
;
353 desc
= ext3_get_group_desc (sb
, group
, NULL
);
354 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
355 le16_to_cpu(desc
->bg_free_blocks_count
))
359 * We're going to place this inode in a different blockgroup from its
360 * parent. We want to cause files in a common directory to all land in
361 * the same blockgroup. But we want files which are in a different
362 * directory which shares a blockgroup with our parent to land in a
363 * different blockgroup.
365 * So add our directory's i_ino into the starting point for the hash.
367 group
= (group
+ parent
->i_ino
) % ngroups
;
370 * Use a quadratic hash to find a group with a free inode and some free
373 for (i
= 1; i
< ngroups
; i
<<= 1) {
375 if (group
>= ngroups
)
377 desc
= ext3_get_group_desc (sb
, group
, NULL
);
378 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
379 le16_to_cpu(desc
->bg_free_blocks_count
))
384 * That failed: try linear search for a free inode, even if that group
385 * has no free blocks.
387 group
= parent_group
;
388 for (i
= 0; i
< ngroups
; i
++) {
389 if (++group
>= ngroups
)
391 desc
= ext3_get_group_desc (sb
, group
, NULL
);
392 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
))
400 * There are two policies for allocating an inode. If the new inode is
401 * a directory, then a forward search is made for a block group with both
402 * free space and a low directory-to-inode ratio; if that fails, then of
403 * the groups with above-average free space, that group with the fewest
404 * directories already is chosen.
406 * For other inodes, search forward from the parent directory's block
407 * group to find a free inode.
409 struct inode
*ext3_new_inode(handle_t
*handle
, struct inode
* dir
,
410 const struct qstr
*qstr
, int mode
)
412 struct super_block
*sb
;
413 struct buffer_head
*bitmap_bh
= NULL
;
414 struct buffer_head
*bh2
;
416 unsigned long ino
= 0;
417 struct inode
* inode
;
418 struct ext3_group_desc
* gdp
= NULL
;
419 struct ext3_super_block
* es
;
420 struct ext3_inode_info
*ei
;
421 struct ext3_sb_info
*sbi
;
426 /* Cannot create files in a deleted directory */
427 if (!dir
|| !dir
->i_nlink
)
428 return ERR_PTR(-EPERM
);
431 trace_ext3_request_inode(dir
, mode
);
432 inode
= new_inode(sb
);
434 return ERR_PTR(-ENOMEM
);
440 if (test_opt (sb
, OLDALLOC
))
441 group
= find_group_dir(sb
, dir
);
443 group
= find_group_orlov(sb
, dir
);
445 group
= find_group_other(sb
, dir
);
451 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
454 gdp
= ext3_get_group_desc(sb
, group
, &bh2
);
459 bitmap_bh
= read_inode_bitmap(sb
, group
);
465 repeat_in_this_group
:
466 ino
= ext3_find_next_zero_bit((unsigned long *)
467 bitmap_bh
->b_data
, EXT3_INODES_PER_GROUP(sb
), ino
);
468 if (ino
< EXT3_INODES_PER_GROUP(sb
)) {
470 BUFFER_TRACE(bitmap_bh
, "get_write_access");
471 err
= ext3_journal_get_write_access(handle
, bitmap_bh
);
475 if (!ext3_set_bit_atomic(sb_bgl_lock(sbi
, group
),
476 ino
, bitmap_bh
->b_data
)) {
478 BUFFER_TRACE(bitmap_bh
,
479 "call ext3_journal_dirty_metadata");
480 err
= ext3_journal_dirty_metadata(handle
,
487 journal_release_buffer(handle
, bitmap_bh
);
489 if (++ino
< EXT3_INODES_PER_GROUP(sb
))
490 goto repeat_in_this_group
;
494 * This case is possible in concurrent environment. It is very
495 * rare. We cannot repeat the find_group_xxx() call because
496 * that will simply return the same blockgroup, because the
497 * group descriptor metadata has not yet been updated.
498 * So we just go onto the next blockgroup.
500 if (++group
== sbi
->s_groups_count
)
507 ino
+= group
* EXT3_INODES_PER_GROUP(sb
) + 1;
508 if (ino
< EXT3_FIRST_INO(sb
) || ino
> le32_to_cpu(es
->s_inodes_count
)) {
509 ext3_error (sb
, "ext3_new_inode",
510 "reserved inode or inode > inodes count - "
511 "block_group = %d, inode=%lu", group
, ino
);
516 BUFFER_TRACE(bh2
, "get_write_access");
517 err
= ext3_journal_get_write_access(handle
, bh2
);
519 spin_lock(sb_bgl_lock(sbi
, group
));
520 le16_add_cpu(&gdp
->bg_free_inodes_count
, -1);
522 le16_add_cpu(&gdp
->bg_used_dirs_count
, 1);
524 spin_unlock(sb_bgl_lock(sbi
, group
));
525 BUFFER_TRACE(bh2
, "call ext3_journal_dirty_metadata");
526 err
= ext3_journal_dirty_metadata(handle
, bh2
);
529 percpu_counter_dec(&sbi
->s_freeinodes_counter
);
531 percpu_counter_inc(&sbi
->s_dirs_counter
);
534 if (test_opt(sb
, GRPID
)) {
535 inode
->i_mode
= mode
;
536 inode
->i_uid
= current_fsuid();
537 inode
->i_gid
= dir
->i_gid
;
539 inode_init_owner(inode
, dir
, mode
);
542 /* This is the optimal IO size (for stat), not the fs block size */
544 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
546 memset(ei
->i_data
, 0, sizeof(ei
->i_data
));
547 ei
->i_dir_start_lookup
= 0;
551 ext3_mask_flags(mode
, EXT3_I(dir
)->i_flags
& EXT3_FL_INHERITED
);
552 #ifdef EXT3_FRAGMENTS
560 ei
->i_block_alloc_info
= NULL
;
561 ei
->i_block_group
= group
;
563 ext3_set_inode_flags(inode
);
564 if (IS_DIRSYNC(inode
))
566 if (insert_inode_locked(inode
) < 0) {
570 spin_lock(&sbi
->s_next_gen_lock
);
571 inode
->i_generation
= sbi
->s_next_generation
++;
572 spin_unlock(&sbi
->s_next_gen_lock
);
574 ei
->i_state_flags
= 0;
575 ext3_set_inode_state(inode
, EXT3_STATE_NEW
);
577 /* See comment in ext3_iget for explanation */
578 if (ino
>= EXT3_FIRST_INO(sb
) + 1 &&
579 EXT3_INODE_SIZE(sb
) > EXT3_GOOD_OLD_INODE_SIZE
) {
581 sizeof(struct ext3_inode
) - EXT3_GOOD_OLD_INODE_SIZE
;
583 ei
->i_extra_isize
= 0;
587 dquot_initialize(inode
);
588 err
= dquot_alloc_inode(inode
);
592 err
= ext3_init_acl(handle
, inode
, dir
);
596 err
= ext3_init_security(handle
, inode
, dir
, qstr
);
600 err
= ext3_mark_inode_dirty(handle
, inode
);
602 ext3_std_error(sb
, err
);
606 ext3_debug("allocating inode %lu\n", inode
->i_ino
);
607 trace_ext3_allocate_inode(inode
, dir
, mode
);
610 ext3_std_error(sb
, err
);
619 dquot_free_inode(inode
);
623 inode
->i_flags
|= S_NOQUOTA
;
625 unlock_new_inode(inode
);
631 /* Verify that we are loading a valid orphan from disk */
632 struct inode
*ext3_orphan_get(struct super_block
*sb
, unsigned long ino
)
634 unsigned long max_ino
= le32_to_cpu(EXT3_SB(sb
)->s_es
->s_inodes_count
);
635 unsigned long block_group
;
637 struct buffer_head
*bitmap_bh
;
638 struct inode
*inode
= NULL
;
641 /* Error cases - e2fsck has already cleaned up for us */
643 ext3_warning(sb
, __func__
,
644 "bad orphan ino %lu! e2fsck was run?", ino
);
648 block_group
= (ino
- 1) / EXT3_INODES_PER_GROUP(sb
);
649 bit
= (ino
- 1) % EXT3_INODES_PER_GROUP(sb
);
650 bitmap_bh
= read_inode_bitmap(sb
, block_group
);
652 ext3_warning(sb
, __func__
,
653 "inode bitmap error for orphan %lu", ino
);
657 /* Having the inode bit set should be a 100% indicator that this
658 * is a valid orphan (no e2fsck run on fs). Orphans also include
659 * inodes that were being truncated, so we can't check i_nlink==0.
661 if (!ext3_test_bit(bit
, bitmap_bh
->b_data
))
664 inode
= ext3_iget(sb
, ino
);
669 * If the orphans has i_nlinks > 0 then it should be able to be
670 * truncated, otherwise it won't be removed from the orphan list
671 * during processing and an infinite loop will result.
673 if (inode
->i_nlink
&& !ext3_can_truncate(inode
))
676 if (NEXT_ORPHAN(inode
) > max_ino
)
682 err
= PTR_ERR(inode
);
685 ext3_warning(sb
, __func__
,
686 "bad orphan inode %lu! e2fsck was run?", ino
);
687 printk(KERN_NOTICE
"ext3_test_bit(bit=%d, block=%llu) = %d\n",
688 bit
, (unsigned long long)bitmap_bh
->b_blocknr
,
689 ext3_test_bit(bit
, bitmap_bh
->b_data
));
690 printk(KERN_NOTICE
"inode=%p\n", inode
);
692 printk(KERN_NOTICE
"is_bad_inode(inode)=%d\n",
693 is_bad_inode(inode
));
694 printk(KERN_NOTICE
"NEXT_ORPHAN(inode)=%u\n",
696 printk(KERN_NOTICE
"max_ino=%lu\n", max_ino
);
697 printk(KERN_NOTICE
"i_nlink=%u\n", inode
->i_nlink
);
698 /* Avoid freeing blocks if we got a bad deleted inode */
699 if (inode
->i_nlink
== 0)
708 unsigned long ext3_count_free_inodes (struct super_block
* sb
)
710 unsigned long desc_count
;
711 struct ext3_group_desc
*gdp
;
714 struct ext3_super_block
*es
;
715 unsigned long bitmap_count
, x
;
716 struct buffer_head
*bitmap_bh
= NULL
;
718 es
= EXT3_SB(sb
)->s_es
;
722 for (i
= 0; i
< EXT3_SB(sb
)->s_groups_count
; i
++) {
723 gdp
= ext3_get_group_desc (sb
, i
, NULL
);
726 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
728 bitmap_bh
= read_inode_bitmap(sb
, i
);
732 x
= ext3_count_free(bitmap_bh
, EXT3_INODES_PER_GROUP(sb
) / 8);
733 printk("group %d: stored = %d, counted = %lu\n",
734 i
, le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
738 printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
739 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
743 for (i
= 0; i
< EXT3_SB(sb
)->s_groups_count
; i
++) {
744 gdp
= ext3_get_group_desc (sb
, i
, NULL
);
747 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
754 /* Called at mount-time, super-block is locked */
755 unsigned long ext3_count_dirs (struct super_block
* sb
)
757 unsigned long count
= 0;
760 for (i
= 0; i
< EXT3_SB(sb
)->s_groups_count
; i
++) {
761 struct ext3_group_desc
*gdp
= ext3_get_group_desc (sb
, i
, NULL
);
764 count
+= le16_to_cpu(gdp
->bg_used_dirs_count
);