2 * linux/fs/ext4/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/jbd2.h>
18 #include <linux/ext4_fs.h>
19 #include <linux/ext4_jbd2.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 <linux/blkdev.h>
27 #include <asm/byteorder.h>
33 * ialloc.c contains the inodes allocation and deallocation routines
37 * The free inodes are managed by bitmaps. A file system contains several
38 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
39 * block for inodes, N blocks for the inode table and data blocks.
41 * The file system contains group descriptors which are located after the
42 * super block. Each descriptor contains the number of the bitmap block and
43 * the free blocks count in the block.
48 * Read the inode allocation bitmap for a given block_group, reading
49 * into the specified slot in the superblock's bitmap cache.
51 * Return buffer_head of bitmap on success or NULL.
53 static struct buffer_head *
54 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
56 struct ext4_group_desc *desc;
57 struct buffer_head *bh = NULL;
59 desc = ext4_get_group_desc(sb, block_group, NULL);
63 bh = sb_bread(sb, ext4_inode_bitmap(sb, desc));
65 ext4_error(sb, "read_inode_bitmap",
66 "Cannot read inode bitmap - "
67 "block_group = %lu, inode_bitmap = %llu",
68 block_group, ext4_inode_bitmap(sb, desc));
74 * NOTE! When we get the inode, we're the only people
75 * that have access to it, and as such there are no
76 * race conditions we have to worry about. The inode
77 * is not on the hash-lists, and it cannot be reached
78 * through the filesystem because the directory entry
79 * has been deleted earlier.
81 * HOWEVER: we must make sure that we get no aliases,
82 * which means that we have to call "clear_inode()"
83 * _before_ we mark the inode not in use in the inode
84 * bitmaps. Otherwise a newly created file might use
85 * the same inode number (not actually the same pointer
86 * though), and then we'd have two inodes sharing the
87 * same inode number and space on the harddisk.
89 void ext4_free_inode (handle_t *handle, struct inode * inode)
91 struct super_block * sb = inode->i_sb;
94 struct buffer_head *bitmap_bh = NULL;
95 struct buffer_head *bh2;
96 unsigned long block_group;
98 struct ext4_group_desc * gdp;
99 struct ext4_super_block * es;
100 struct ext4_sb_info *sbi;
103 if (atomic_read(&inode->i_count) > 1) {
104 printk ("ext4_free_inode: inode has count=%d\n",
105 atomic_read(&inode->i_count));
108 if (inode->i_nlink) {
109 printk ("ext4_free_inode: inode has nlink=%d\n",
114 printk("ext4_free_inode: inode on nonexistent device\n");
120 ext4_debug ("freeing inode %lu\n", ino);
123 * Note: we must free any quota before locking the superblock,
124 * as writing the quota to disk may need the lock as well.
127 ext4_xattr_delete_inode(handle, inode);
128 DQUOT_FREE_INODE(inode);
131 is_directory = S_ISDIR(inode->i_mode);
133 /* Do this BEFORE marking the inode not in use or returning an error */
136 es = EXT4_SB(sb)->s_es;
137 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
138 ext4_error (sb, "ext4_free_inode",
139 "reserved or nonexistent inode %lu", ino);
142 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
143 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
144 bitmap_bh = read_inode_bitmap(sb, block_group);
148 BUFFER_TRACE(bitmap_bh, "get_write_access");
149 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
153 /* Ok, now we can actually update the inode bitmaps.. */
154 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
155 bit, bitmap_bh->b_data))
156 ext4_error (sb, "ext4_free_inode",
157 "bit already cleared for inode %lu", ino);
159 gdp = ext4_get_group_desc (sb, block_group, &bh2);
161 BUFFER_TRACE(bh2, "get_write_access");
162 fatal = ext4_journal_get_write_access(handle, bh2);
163 if (fatal) goto error_return;
166 spin_lock(sb_bgl_lock(sbi, block_group));
167 gdp->bg_free_inodes_count = cpu_to_le16(
168 le16_to_cpu(gdp->bg_free_inodes_count) + 1);
170 gdp->bg_used_dirs_count = cpu_to_le16(
171 le16_to_cpu(gdp->bg_used_dirs_count) - 1);
172 spin_unlock(sb_bgl_lock(sbi, block_group));
173 percpu_counter_inc(&sbi->s_freeinodes_counter);
175 percpu_counter_dec(&sbi->s_dirs_counter);
178 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
179 err = ext4_journal_dirty_metadata(handle, bh2);
180 if (!fatal) fatal = err;
182 BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
183 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
189 ext4_std_error(sb, fatal);
193 * There are two policies for allocating an inode. If the new inode is
194 * a directory, then a forward search is made for a block group with both
195 * free space and a low directory-to-inode ratio; if that fails, then of
196 * the groups with above-average free space, that group with the fewest
197 * directories already is chosen.
199 * For other inodes, search forward from the parent directory\'s block
200 * group to find a free inode.
202 static int find_group_dir(struct super_block *sb, struct inode *parent)
204 int ngroups = EXT4_SB(sb)->s_groups_count;
205 unsigned int freei, avefreei;
206 struct ext4_group_desc *desc, *best_desc = NULL;
207 struct buffer_head *bh;
208 int group, best_group = -1;
210 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
211 avefreei = freei / ngroups;
213 for (group = 0; group < ngroups; group++) {
214 desc = ext4_get_group_desc (sb, group, &bh);
215 if (!desc || !desc->bg_free_inodes_count)
217 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
220 (le16_to_cpu(desc->bg_free_blocks_count) >
221 le16_to_cpu(best_desc->bg_free_blocks_count))) {
230 * Orlov's allocator for directories.
232 * We always try to spread first-level directories.
234 * If there are blockgroups with both free inodes and free blocks counts
235 * not worse than average we return one with smallest directory count.
236 * Otherwise we simply return a random group.
238 * For the rest rules look so:
240 * It's OK to put directory into a group unless
241 * it has too many directories already (max_dirs) or
242 * it has too few free inodes left (min_inodes) or
243 * it has too few free blocks left (min_blocks) or
244 * it's already running too large debt (max_debt).
245 * Parent's group is prefered, if it doesn't satisfy these
246 * conditions we search cyclically through the rest. If none
247 * of the groups look good we just look for a group with more
248 * free inodes than average (starting at parent's group).
250 * Debt is incremented each time we allocate a directory and decremented
251 * when we allocate an inode, within 0--255.
254 #define INODE_COST 64
255 #define BLOCK_COST 256
257 static int find_group_orlov(struct super_block *sb, struct inode *parent)
259 int parent_group = EXT4_I(parent)->i_block_group;
260 struct ext4_sb_info *sbi = EXT4_SB(sb);
261 struct ext4_super_block *es = sbi->s_es;
262 int ngroups = sbi->s_groups_count;
263 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
264 unsigned int freei, avefreei;
265 ext4_fsblk_t freeb, avefreeb;
266 ext4_fsblk_t blocks_per_dir;
268 int max_debt, max_dirs, min_inodes;
269 ext4_grpblk_t min_blocks;
271 struct ext4_group_desc *desc;
272 struct buffer_head *bh;
274 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
275 avefreei = freei / ngroups;
276 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
278 do_div(avefreeb, ngroups);
279 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
281 if ((parent == sb->s_root->d_inode) ||
282 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
283 int best_ndir = inodes_per_group;
286 get_random_bytes(&group, sizeof(group));
287 parent_group = (unsigned)group % ngroups;
288 for (i = 0; i < ngroups; i++) {
289 group = (parent_group + i) % ngroups;
290 desc = ext4_get_group_desc (sb, group, &bh);
291 if (!desc || !desc->bg_free_inodes_count)
293 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
295 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
297 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
300 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
307 blocks_per_dir = ext4_blocks_count(es) - freeb;
308 do_div(blocks_per_dir, ndirs);
310 max_dirs = ndirs / ngroups + inodes_per_group / 16;
311 min_inodes = avefreei - inodes_per_group / 4;
312 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
314 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
315 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
316 if (max_debt * INODE_COST > inodes_per_group)
317 max_debt = inodes_per_group / INODE_COST;
323 for (i = 0; i < ngroups; i++) {
324 group = (parent_group + i) % ngroups;
325 desc = ext4_get_group_desc (sb, group, &bh);
326 if (!desc || !desc->bg_free_inodes_count)
328 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
330 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
332 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
338 for (i = 0; i < ngroups; i++) {
339 group = (parent_group + i) % ngroups;
340 desc = ext4_get_group_desc (sb, group, &bh);
341 if (!desc || !desc->bg_free_inodes_count)
343 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
349 * The free-inodes counter is approximate, and for really small
350 * filesystems the above test can fail to find any blockgroups
359 static int find_group_other(struct super_block *sb, struct inode *parent)
361 int parent_group = EXT4_I(parent)->i_block_group;
362 int ngroups = EXT4_SB(sb)->s_groups_count;
363 struct ext4_group_desc *desc;
364 struct buffer_head *bh;
368 * Try to place the inode in its parent directory
370 group = parent_group;
371 desc = ext4_get_group_desc (sb, group, &bh);
372 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
373 le16_to_cpu(desc->bg_free_blocks_count))
377 * We're going to place this inode in a different blockgroup from its
378 * parent. We want to cause files in a common directory to all land in
379 * the same blockgroup. But we want files which are in a different
380 * directory which shares a blockgroup with our parent to land in a
381 * different blockgroup.
383 * So add our directory's i_ino into the starting point for the hash.
385 group = (group + parent->i_ino) % ngroups;
388 * Use a quadratic hash to find a group with a free inode and some free
391 for (i = 1; i < ngroups; i <<= 1) {
393 if (group >= ngroups)
395 desc = ext4_get_group_desc (sb, group, &bh);
396 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
397 le16_to_cpu(desc->bg_free_blocks_count))
402 * That failed: try linear search for a free inode, even if that group
403 * has no free blocks.
405 group = parent_group;
406 for (i = 0; i < ngroups; i++) {
407 if (++group >= ngroups)
409 desc = ext4_get_group_desc (sb, group, &bh);
410 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
418 * There are two policies for allocating an inode. If the new inode is
419 * a directory, then a forward search is made for a block group with both
420 * free space and a low directory-to-inode ratio; if that fails, then of
421 * the groups with above-average free space, that group with the fewest
422 * directories already is chosen.
424 * For other inodes, search forward from the parent directory's block
425 * group to find a free inode.
427 struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
429 struct super_block *sb;
430 struct buffer_head *bitmap_bh = NULL;
431 struct buffer_head *bh2;
433 unsigned long ino = 0;
434 struct inode * inode;
435 struct ext4_group_desc * gdp = NULL;
436 struct ext4_super_block * es;
437 struct ext4_inode_info *ei;
438 struct ext4_sb_info *sbi;
443 /* Cannot create files in a deleted directory */
444 if (!dir || !dir->i_nlink)
445 return ERR_PTR(-EPERM);
448 inode = new_inode(sb);
450 return ERR_PTR(-ENOMEM);
456 if (test_opt (sb, OLDALLOC))
457 group = find_group_dir(sb, dir);
459 group = find_group_orlov(sb, dir);
461 group = find_group_other(sb, dir);
467 for (i = 0; i < sbi->s_groups_count; i++) {
470 gdp = ext4_get_group_desc(sb, group, &bh2);
475 bitmap_bh = read_inode_bitmap(sb, group);
481 repeat_in_this_group:
482 ino = ext4_find_next_zero_bit((unsigned long *)
483 bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
484 if (ino < EXT4_INODES_PER_GROUP(sb)) {
486 BUFFER_TRACE(bitmap_bh, "get_write_access");
487 err = ext4_journal_get_write_access(handle, bitmap_bh);
491 if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
492 ino, bitmap_bh->b_data)) {
494 BUFFER_TRACE(bitmap_bh,
495 "call ext4_journal_dirty_metadata");
496 err = ext4_journal_dirty_metadata(handle,
503 jbd2_journal_release_buffer(handle, bitmap_bh);
505 if (++ino < EXT4_INODES_PER_GROUP(sb))
506 goto repeat_in_this_group;
510 * This case is possible in concurrent environment. It is very
511 * rare. We cannot repeat the find_group_xxx() call because
512 * that will simply return the same blockgroup, because the
513 * group descriptor metadata has not yet been updated.
514 * So we just go onto the next blockgroup.
516 if (++group == sbi->s_groups_count)
523 ino += group * EXT4_INODES_PER_GROUP(sb) + 1;
524 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
525 ext4_error (sb, "ext4_new_inode",
526 "reserved inode or inode > inodes count - "
527 "block_group = %d, inode=%lu", group, ino);
532 BUFFER_TRACE(bh2, "get_write_access");
533 err = ext4_journal_get_write_access(handle, bh2);
535 spin_lock(sb_bgl_lock(sbi, group));
536 gdp->bg_free_inodes_count =
537 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
539 gdp->bg_used_dirs_count =
540 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
542 spin_unlock(sb_bgl_lock(sbi, group));
543 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
544 err = ext4_journal_dirty_metadata(handle, bh2);
547 percpu_counter_dec(&sbi->s_freeinodes_counter);
549 percpu_counter_inc(&sbi->s_dirs_counter);
552 inode->i_uid = current->fsuid;
553 if (test_opt (sb, GRPID))
554 inode->i_gid = dir->i_gid;
555 else if (dir->i_mode & S_ISGID) {
556 inode->i_gid = dir->i_gid;
560 inode->i_gid = current->fsgid;
561 inode->i_mode = mode;
564 /* This is the optimal IO size (for stat), not the fs block size */
566 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
567 ext4_current_time(inode);
569 memset(ei->i_data, 0, sizeof(ei->i_data));
570 ei->i_dir_start_lookup = 0;
573 ei->i_flags = EXT4_I(dir)->i_flags & ~EXT4_INDEX_FL;
575 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
576 /* dirsync only applies to directories */
578 ei->i_flags &= ~EXT4_DIRSYNC_FL;
579 #ifdef EXT4_FRAGMENTS
587 ei->i_block_alloc_info = NULL;
588 ei->i_block_group = group;
590 ext4_set_inode_flags(inode);
591 if (IS_DIRSYNC(inode))
593 insert_inode_hash(inode);
594 spin_lock(&sbi->s_next_gen_lock);
595 inode->i_generation = sbi->s_next_generation++;
596 spin_unlock(&sbi->s_next_gen_lock);
598 ei->i_state = EXT4_STATE_NEW;
600 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
603 if(DQUOT_ALLOC_INODE(inode)) {
608 err = ext4_init_acl(handle, inode, dir);
612 err = ext4_init_security(handle,inode, dir);
616 err = ext4_mark_inode_dirty(handle, inode);
618 ext4_std_error(sb, err);
621 if (test_opt(sb, EXTENTS)) {
622 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
623 ext4_ext_tree_init(handle, inode);
624 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
625 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
627 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS);
628 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "call ext4_journal_dirty_metadata");
629 err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
633 ext4_debug("allocating inode %lu\n", inode->i_ino);
636 ext4_std_error(sb, err);
645 DQUOT_FREE_INODE(inode);
649 inode->i_flags |= S_NOQUOTA;
656 /* Verify that we are loading a valid orphan from disk */
657 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
659 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
660 unsigned long block_group;
662 struct buffer_head *bitmap_bh = NULL;
663 struct inode *inode = NULL;
665 /* Error cases - e2fsck has already cleaned up for us */
667 ext4_warning(sb, __FUNCTION__,
668 "bad orphan ino %lu! e2fsck was run?", ino);
672 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
673 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
674 bitmap_bh = read_inode_bitmap(sb, block_group);
676 ext4_warning(sb, __FUNCTION__,
677 "inode bitmap error for orphan %lu", ino);
681 /* Having the inode bit set should be a 100% indicator that this
682 * is a valid orphan (no e2fsck run on fs). Orphans also include
683 * inodes that were being truncated, so we can't check i_nlink==0.
685 if (!ext4_test_bit(bit, bitmap_bh->b_data) ||
686 !(inode = iget(sb, ino)) || is_bad_inode(inode) ||
687 NEXT_ORPHAN(inode) > max_ino) {
688 ext4_warning(sb, __FUNCTION__,
689 "bad orphan inode %lu! e2fsck was run?", ino);
690 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
691 bit, (unsigned long long)bitmap_bh->b_blocknr,
692 ext4_test_bit(bit, bitmap_bh->b_data));
693 printk(KERN_NOTICE "inode=%p\n", inode);
695 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
696 is_bad_inode(inode));
697 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
699 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
701 /* Avoid freeing blocks if we got a bad deleted inode */
702 if (inode && inode->i_nlink == 0)
712 unsigned long ext4_count_free_inodes (struct super_block * sb)
714 unsigned long desc_count;
715 struct ext4_group_desc *gdp;
718 struct ext4_super_block *es;
719 unsigned long bitmap_count, x;
720 struct buffer_head *bitmap_bh = NULL;
722 es = EXT4_SB(sb)->s_es;
726 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
727 gdp = ext4_get_group_desc (sb, i, NULL);
730 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
732 bitmap_bh = read_inode_bitmap(sb, i);
736 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
737 printk("group %d: stored = %d, counted = %lu\n",
738 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
742 printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
743 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
747 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
748 gdp = ext4_get_group_desc (sb, i, NULL);
751 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
758 /* Called at mount-time, super-block is locked */
759 unsigned long ext4_count_dirs (struct super_block * sb)
761 unsigned long count = 0;
764 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
765 struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
768 count += le16_to_cpu(gdp->bg_used_dirs_count);