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 <linux/vs_dlimit.h>
28 #include <linux/vs_tag.h>
29 #include <asm/byteorder.h>
35 * ialloc.c contains the inodes allocation and deallocation routines
39 * The free inodes are managed by bitmaps. A file system contains several
40 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
41 * block for inodes, N blocks for the inode table and data blocks.
43 * The file system contains group descriptors which are located after the
44 * super block. Each descriptor contains the number of the bitmap block and
45 * the free blocks count in the block.
50 * Read the inode allocation bitmap for a given block_group, reading
51 * into the specified slot in the superblock's bitmap cache.
53 * Return buffer_head of bitmap on success or NULL.
55 static struct buffer_head *
56 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
58 struct ext4_group_desc *desc;
59 struct buffer_head *bh = NULL;
61 desc = ext4_get_group_desc(sb, block_group, NULL);
65 bh = sb_bread(sb, ext4_inode_bitmap(sb, desc));
67 ext4_error(sb, "read_inode_bitmap",
68 "Cannot read inode bitmap - "
69 "block_group = %lu, inode_bitmap = %llu",
70 block_group, ext4_inode_bitmap(sb, desc));
76 * NOTE! When we get the inode, we're the only people
77 * that have access to it, and as such there are no
78 * race conditions we have to worry about. The inode
79 * is not on the hash-lists, and it cannot be reached
80 * through the filesystem because the directory entry
81 * has been deleted earlier.
83 * HOWEVER: we must make sure that we get no aliases,
84 * which means that we have to call "clear_inode()"
85 * _before_ we mark the inode not in use in the inode
86 * bitmaps. Otherwise a newly created file might use
87 * the same inode number (not actually the same pointer
88 * though), and then we'd have two inodes sharing the
89 * same inode number and space on the harddisk.
91 void ext4_free_inode (handle_t *handle, struct inode * inode)
93 struct super_block * sb = inode->i_sb;
96 struct buffer_head *bitmap_bh = NULL;
97 struct buffer_head *bh2;
98 unsigned long block_group;
100 struct ext4_group_desc * gdp;
101 struct ext4_super_block * es;
102 struct ext4_sb_info *sbi;
105 if (atomic_read(&inode->i_count) > 1) {
106 printk ("ext4_free_inode: inode has count=%d\n",
107 atomic_read(&inode->i_count));
110 if (inode->i_nlink) {
111 printk ("ext4_free_inode: inode has nlink=%d\n",
116 printk("ext4_free_inode: inode on nonexistent device\n");
122 ext4_debug ("freeing inode %lu\n", ino);
125 * Note: we must free any quota before locking the superblock,
126 * as writing the quota to disk may need the lock as well.
129 ext4_xattr_delete_inode(handle, inode);
130 DQUOT_FREE_INODE(inode);
132 DLIMIT_FREE_INODE(inode);
134 is_directory = S_ISDIR(inode->i_mode);
136 /* Do this BEFORE marking the inode not in use or returning an error */
139 es = EXT4_SB(sb)->s_es;
140 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
141 ext4_error (sb, "ext4_free_inode",
142 "reserved or nonexistent inode %lu", ino);
145 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
146 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
147 bitmap_bh = read_inode_bitmap(sb, block_group);
151 BUFFER_TRACE(bitmap_bh, "get_write_access");
152 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
156 /* Ok, now we can actually update the inode bitmaps.. */
157 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
158 bit, bitmap_bh->b_data))
159 ext4_error (sb, "ext4_free_inode",
160 "bit already cleared for inode %lu", ino);
162 gdp = ext4_get_group_desc (sb, block_group, &bh2);
164 BUFFER_TRACE(bh2, "get_write_access");
165 fatal = ext4_journal_get_write_access(handle, bh2);
166 if (fatal) goto error_return;
169 spin_lock(sb_bgl_lock(sbi, block_group));
170 gdp->bg_free_inodes_count = cpu_to_le16(
171 le16_to_cpu(gdp->bg_free_inodes_count) + 1);
173 gdp->bg_used_dirs_count = cpu_to_le16(
174 le16_to_cpu(gdp->bg_used_dirs_count) - 1);
175 spin_unlock(sb_bgl_lock(sbi, block_group));
176 percpu_counter_inc(&sbi->s_freeinodes_counter);
178 percpu_counter_dec(&sbi->s_dirs_counter);
181 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
182 err = ext4_journal_dirty_metadata(handle, bh2);
183 if (!fatal) fatal = err;
185 BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
186 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
192 ext4_std_error(sb, fatal);
196 * There are two policies for allocating an inode. If the new inode is
197 * a directory, then a forward search is made for a block group with both
198 * free space and a low directory-to-inode ratio; if that fails, then of
199 * the groups with above-average free space, that group with the fewest
200 * directories already is chosen.
202 * For other inodes, search forward from the parent directory\'s block
203 * group to find a free inode.
205 static int find_group_dir(struct super_block *sb, struct inode *parent)
207 int ngroups = EXT4_SB(sb)->s_groups_count;
208 unsigned int freei, avefreei;
209 struct ext4_group_desc *desc, *best_desc = NULL;
210 struct buffer_head *bh;
211 int group, best_group = -1;
213 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
214 avefreei = freei / ngroups;
216 for (group = 0; group < ngroups; group++) {
217 desc = ext4_get_group_desc (sb, group, &bh);
218 if (!desc || !desc->bg_free_inodes_count)
220 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
223 (le16_to_cpu(desc->bg_free_blocks_count) >
224 le16_to_cpu(best_desc->bg_free_blocks_count))) {
233 * Orlov's allocator for directories.
235 * We always try to spread first-level directories.
237 * If there are blockgroups with both free inodes and free blocks counts
238 * not worse than average we return one with smallest directory count.
239 * Otherwise we simply return a random group.
241 * For the rest rules look so:
243 * It's OK to put directory into a group unless
244 * it has too many directories already (max_dirs) or
245 * it has too few free inodes left (min_inodes) or
246 * it has too few free blocks left (min_blocks) or
247 * it's already running too large debt (max_debt).
248 * Parent's group is prefered, if it doesn't satisfy these
249 * conditions we search cyclically through the rest. If none
250 * of the groups look good we just look for a group with more
251 * free inodes than average (starting at parent's group).
253 * Debt is incremented each time we allocate a directory and decremented
254 * when we allocate an inode, within 0--255.
257 #define INODE_COST 64
258 #define BLOCK_COST 256
260 static int find_group_orlov(struct super_block *sb, struct inode *parent)
262 int parent_group = EXT4_I(parent)->i_block_group;
263 struct ext4_sb_info *sbi = EXT4_SB(sb);
264 struct ext4_super_block *es = sbi->s_es;
265 int ngroups = sbi->s_groups_count;
266 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
267 unsigned int freei, avefreei;
268 ext4_fsblk_t freeb, avefreeb;
269 ext4_fsblk_t blocks_per_dir;
271 int max_debt, max_dirs, min_inodes;
272 ext4_grpblk_t min_blocks;
274 struct ext4_group_desc *desc;
275 struct buffer_head *bh;
277 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
278 avefreei = freei / ngroups;
279 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
281 do_div(avefreeb, ngroups);
282 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
284 if ((parent == sb->s_root->d_inode) ||
285 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
286 int best_ndir = inodes_per_group;
289 get_random_bytes(&group, sizeof(group));
290 parent_group = (unsigned)group % ngroups;
291 for (i = 0; i < ngroups; i++) {
292 group = (parent_group + i) % ngroups;
293 desc = ext4_get_group_desc (sb, group, &bh);
294 if (!desc || !desc->bg_free_inodes_count)
296 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
298 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
300 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
303 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
310 blocks_per_dir = ext4_blocks_count(es) - freeb;
311 do_div(blocks_per_dir, ndirs);
313 max_dirs = ndirs / ngroups + inodes_per_group / 16;
314 min_inodes = avefreei - inodes_per_group / 4;
315 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
317 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
318 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
319 if (max_debt * INODE_COST > inodes_per_group)
320 max_debt = inodes_per_group / INODE_COST;
326 for (i = 0; i < ngroups; i++) {
327 group = (parent_group + i) % ngroups;
328 desc = ext4_get_group_desc (sb, group, &bh);
329 if (!desc || !desc->bg_free_inodes_count)
331 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
333 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
335 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
341 for (i = 0; i < ngroups; i++) {
342 group = (parent_group + i) % ngroups;
343 desc = ext4_get_group_desc (sb, group, &bh);
344 if (!desc || !desc->bg_free_inodes_count)
346 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
352 * The free-inodes counter is approximate, and for really small
353 * filesystems the above test can fail to find any blockgroups
362 static int find_group_other(struct super_block *sb, struct inode *parent)
364 int parent_group = EXT4_I(parent)->i_block_group;
365 int ngroups = EXT4_SB(sb)->s_groups_count;
366 struct ext4_group_desc *desc;
367 struct buffer_head *bh;
371 * Try to place the inode in its parent directory
373 group = parent_group;
374 desc = ext4_get_group_desc (sb, group, &bh);
375 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
376 le16_to_cpu(desc->bg_free_blocks_count))
380 * We're going to place this inode in a different blockgroup from its
381 * parent. We want to cause files in a common directory to all land in
382 * the same blockgroup. But we want files which are in a different
383 * directory which shares a blockgroup with our parent to land in a
384 * different blockgroup.
386 * So add our directory's i_ino into the starting point for the hash.
388 group = (group + parent->i_ino) % ngroups;
391 * Use a quadratic hash to find a group with a free inode and some free
394 for (i = 1; i < ngroups; i <<= 1) {
396 if (group >= ngroups)
398 desc = ext4_get_group_desc (sb, group, &bh);
399 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
400 le16_to_cpu(desc->bg_free_blocks_count))
405 * That failed: try linear search for a free inode, even if that group
406 * has no free blocks.
408 group = parent_group;
409 for (i = 0; i < ngroups; i++) {
410 if (++group >= ngroups)
412 desc = ext4_get_group_desc (sb, group, &bh);
413 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
421 * There are two policies for allocating an inode. If the new inode is
422 * a directory, then a forward search is made for a block group with both
423 * free space and a low directory-to-inode ratio; if that fails, then of
424 * the groups with above-average free space, that group with the fewest
425 * directories already is chosen.
427 * For other inodes, search forward from the parent directory's block
428 * group to find a free inode.
430 struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
432 struct super_block *sb;
433 struct buffer_head *bitmap_bh = NULL;
434 struct buffer_head *bh2;
436 unsigned long ino = 0;
437 struct inode * inode;
438 struct ext4_group_desc * gdp = NULL;
439 struct ext4_super_block * es;
440 struct ext4_inode_info *ei;
441 struct ext4_sb_info *sbi;
446 /* Cannot create files in a deleted directory */
447 if (!dir || !dir->i_nlink)
448 return ERR_PTR(-EPERM);
451 inode = new_inode(sb);
453 return ERR_PTR(-ENOMEM);
455 inode->i_tag = dx_current_fstag(sb);
456 if (DLIMIT_ALLOC_INODE(inode)) {
465 if (test_opt (sb, OLDALLOC))
466 group = find_group_dir(sb, dir);
468 group = find_group_orlov(sb, dir);
470 group = find_group_other(sb, dir);
476 for (i = 0; i < sbi->s_groups_count; i++) {
479 gdp = ext4_get_group_desc(sb, group, &bh2);
484 bitmap_bh = read_inode_bitmap(sb, group);
490 repeat_in_this_group:
491 ino = ext4_find_next_zero_bit((unsigned long *)
492 bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
493 if (ino < EXT4_INODES_PER_GROUP(sb)) {
495 BUFFER_TRACE(bitmap_bh, "get_write_access");
496 err = ext4_journal_get_write_access(handle, bitmap_bh);
500 if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
501 ino, bitmap_bh->b_data)) {
503 BUFFER_TRACE(bitmap_bh,
504 "call ext4_journal_dirty_metadata");
505 err = ext4_journal_dirty_metadata(handle,
512 jbd2_journal_release_buffer(handle, bitmap_bh);
514 if (++ino < EXT4_INODES_PER_GROUP(sb))
515 goto repeat_in_this_group;
519 * This case is possible in concurrent environment. It is very
520 * rare. We cannot repeat the find_group_xxx() call because
521 * that will simply return the same blockgroup, because the
522 * group descriptor metadata has not yet been updated.
523 * So we just go onto the next blockgroup.
525 if (++group == sbi->s_groups_count)
532 ino += group * EXT4_INODES_PER_GROUP(sb) + 1;
533 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
534 ext4_error (sb, "ext4_new_inode",
535 "reserved inode or inode > inodes count - "
536 "block_group = %d, inode=%lu", group, ino);
541 BUFFER_TRACE(bh2, "get_write_access");
542 err = ext4_journal_get_write_access(handle, bh2);
544 spin_lock(sb_bgl_lock(sbi, group));
545 gdp->bg_free_inodes_count =
546 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
548 gdp->bg_used_dirs_count =
549 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
551 spin_unlock(sb_bgl_lock(sbi, group));
552 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
553 err = ext4_journal_dirty_metadata(handle, bh2);
556 percpu_counter_dec(&sbi->s_freeinodes_counter);
558 percpu_counter_inc(&sbi->s_dirs_counter);
561 inode->i_uid = current->fsuid;
562 if (test_opt (sb, GRPID))
563 inode->i_gid = dir->i_gid;
564 else if (dir->i_mode & S_ISGID) {
565 inode->i_gid = dir->i_gid;
569 inode->i_gid = current->fsgid;
570 inode->i_mode = mode;
573 /* This is the optimal IO size (for stat), not the fs block size */
575 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
577 memset(ei->i_data, 0, sizeof(ei->i_data));
578 ei->i_dir_start_lookup = 0;
581 ei->i_flags = EXT4_I(dir)->i_flags &
582 ~(EXT4_INDEX_FL|EXT4_IUNLINK_FL|EXT4_BARRIER_FL);
584 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
585 /* dirsync only applies to directories */
587 ei->i_flags &= ~EXT4_DIRSYNC_FL;
588 #ifdef EXT4_FRAGMENTS
596 ei->i_block_alloc_info = NULL;
597 ei->i_block_group = group;
599 ext4_set_inode_flags(inode);
600 if (IS_DIRSYNC(inode))
602 insert_inode_hash(inode);
603 spin_lock(&sbi->s_next_gen_lock);
604 inode->i_generation = sbi->s_next_generation++;
605 spin_unlock(&sbi->s_next_gen_lock);
607 ei->i_state = EXT4_STATE_NEW;
609 (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) ?
610 sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE : 0;
613 if(DQUOT_ALLOC_INODE(inode)) {
618 err = ext4_init_acl(handle, inode, dir);
622 err = ext4_init_security(handle,inode, dir);
626 err = ext4_mark_inode_dirty(handle, inode);
628 ext4_std_error(sb, err);
631 if (test_opt(sb, EXTENTS)) {
632 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
633 ext4_ext_tree_init(handle, inode);
634 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
635 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
637 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS);
638 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "call ext4_journal_dirty_metadata");
639 err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
643 ext4_debug("allocating inode %lu\n", inode->i_ino);
646 ext4_std_error(sb, err);
648 DLIMIT_FREE_INODE(inode);
657 DQUOT_FREE_INODE(inode);
661 DLIMIT_FREE_INODE(inode);
662 inode->i_flags |= S_NOQUOTA;
669 /* Verify that we are loading a valid orphan from disk */
670 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
672 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
673 unsigned long block_group;
675 struct buffer_head *bitmap_bh = NULL;
676 struct inode *inode = NULL;
678 /* Error cases - e2fsck has already cleaned up for us */
680 ext4_warning(sb, __FUNCTION__,
681 "bad orphan ino %lu! e2fsck was run?", ino);
685 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
686 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
687 bitmap_bh = read_inode_bitmap(sb, block_group);
689 ext4_warning(sb, __FUNCTION__,
690 "inode bitmap error for orphan %lu", ino);
694 /* Having the inode bit set should be a 100% indicator that this
695 * is a valid orphan (no e2fsck run on fs). Orphans also include
696 * inodes that were being truncated, so we can't check i_nlink==0.
698 if (!ext4_test_bit(bit, bitmap_bh->b_data) ||
699 !(inode = iget(sb, ino)) || is_bad_inode(inode) ||
700 NEXT_ORPHAN(inode) > max_ino) {
701 ext4_warning(sb, __FUNCTION__,
702 "bad orphan inode %lu! e2fsck was run?", ino);
703 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
704 bit, (unsigned long long)bitmap_bh->b_blocknr,
705 ext4_test_bit(bit, bitmap_bh->b_data));
706 printk(KERN_NOTICE "inode=%p\n", inode);
708 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
709 is_bad_inode(inode));
710 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
712 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
714 /* Avoid freeing blocks if we got a bad deleted inode */
715 if (inode && inode->i_nlink == 0)
725 unsigned long ext4_count_free_inodes (struct super_block * sb)
727 unsigned long desc_count;
728 struct ext4_group_desc *gdp;
731 struct ext4_super_block *es;
732 unsigned long bitmap_count, x;
733 struct buffer_head *bitmap_bh = NULL;
735 es = EXT4_SB(sb)->s_es;
739 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
740 gdp = ext4_get_group_desc (sb, i, NULL);
743 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
745 bitmap_bh = read_inode_bitmap(sb, i);
749 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
750 printk("group %d: stored = %d, counted = %lu\n",
751 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
755 printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
756 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
760 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
761 gdp = ext4_get_group_desc (sb, i, NULL);
764 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
771 /* Called at mount-time, super-block is locked */
772 unsigned long ext4_count_dirs (struct super_block * sb)
774 unsigned long count = 0;
777 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
778 struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
781 count += le16_to_cpu(gdp->bg_used_dirs_count);