2 * linux/fs/ext2/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@dcs.ed.ac.uk), 1993
11 * Big-endian to little-endian byte-swapping/bitmaps by
12 * David S. Miller (davem@caip.rutgers.edu), 1995
15 #include <linux/config.h>
16 #include <linux/quotaops.h>
17 #include <linux/sched.h>
18 #include <linux/backing-dev.h>
19 #include <linux/buffer_head.h>
20 #include <linux/random.h>
21 #include <linux/vs_dlimit.h>
28 * ialloc.c contains the inodes allocation and deallocation routines
32 * The free inodes are managed by bitmaps. A file system contains several
33 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
34 * block for inodes, N blocks for the inode table and data blocks.
36 * The file system contains group descriptors which are located after the
37 * super block. Each descriptor contains the number of the bitmap block and
38 * the free blocks count in the block.
43 * Read the inode allocation bitmap for a given block_group, reading
44 * into the specified slot in the superblock's bitmap cache.
46 * Return buffer_head of bitmap on success or NULL.
48 static struct buffer_head *
49 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
51 struct ext2_group_desc *desc;
52 struct buffer_head *bh = NULL;
54 desc = ext2_get_group_desc(sb, block_group, NULL);
58 bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
60 ext2_error(sb, "read_inode_bitmap",
61 "Cannot read inode bitmap - "
62 "block_group = %lu, inode_bitmap = %u",
63 block_group, le32_to_cpu(desc->bg_inode_bitmap));
68 static void ext2_release_inode(struct super_block *sb, int group, int dir)
70 struct ext2_group_desc * desc;
71 struct buffer_head *bh;
73 desc = ext2_get_group_desc(sb, group, &bh);
75 ext2_error(sb, "ext2_release_inode",
76 "can't get descriptor for group %d", group);
80 spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
81 desc->bg_free_inodes_count =
82 cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
84 desc->bg_used_dirs_count =
85 cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
86 spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
88 percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
90 mark_buffer_dirty(bh);
94 * NOTE! When we get the inode, we're the only people
95 * that have access to it, and as such there are no
96 * race conditions we have to worry about. The inode
97 * is not on the hash-lists, and it cannot be reached
98 * through the filesystem because the directory entry
99 * has been deleted earlier.
101 * HOWEVER: we must make sure that we get no aliases,
102 * which means that we have to call "clear_inode()"
103 * _before_ we mark the inode not in use in the inode
104 * bitmaps. Otherwise a newly created file might use
105 * the same inode number (not actually the same pointer
106 * though), and then we'd have two inodes sharing the
107 * same inode number and space on the harddisk.
109 void ext2_free_inode (struct inode * inode)
111 struct super_block * sb = inode->i_sb;
114 struct buffer_head *bitmap_bh = NULL;
115 unsigned long block_group;
117 struct ext2_super_block * es;
120 ext2_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.
126 if (!is_bad_inode(inode)) {
127 /* Quota is already initialized in iput() */
128 ext2_xattr_delete_inode(inode);
129 DLIMIT_FREE_INODE(sb, inode->i_xid);
130 DQUOT_FREE_INODE(inode);
134 es = EXT2_SB(sb)->s_es;
135 is_directory = S_ISDIR(inode->i_mode);
137 /* Do this BEFORE marking the inode not in use or returning an error */
140 if (ino < EXT2_FIRST_INO(sb) ||
141 ino > le32_to_cpu(es->s_inodes_count)) {
142 ext2_error (sb, "ext2_free_inode",
143 "reserved or nonexistent inode %lu", ino);
146 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
147 bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
149 bitmap_bh = read_inode_bitmap(sb, block_group);
153 /* Ok, now we can actually update the inode bitmaps.. */
154 if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
155 bit, (void *) bitmap_bh->b_data))
156 ext2_error (sb, "ext2_free_inode",
157 "bit already cleared for inode %lu", ino);
159 ext2_release_inode(sb, block_group, is_directory);
160 mark_buffer_dirty(bitmap_bh);
161 if (sb->s_flags & MS_SYNCHRONOUS)
162 sync_dirty_buffer(bitmap_bh);
168 * We perform asynchronous prereading of the new inode's inode block when
169 * we create the inode, in the expectation that the inode will be written
170 * back soon. There are two reasons:
172 * - When creating a large number of files, the async prereads will be
173 * nicely merged into large reads
174 * - When writing out a large number of inodes, we don't need to keep on
175 * stalling the writes while we read the inode block.
177 * FIXME: ext2_get_group_desc() needs to be simplified.
179 static void ext2_preread_inode(struct inode *inode)
181 unsigned long block_group;
182 unsigned long offset;
184 struct buffer_head *bh;
185 struct ext2_group_desc * gdp;
186 struct backing_dev_info *bdi;
188 bdi = inode->i_mapping->backing_dev_info;
189 if (bdi_read_congested(bdi))
191 if (bdi_write_congested(bdi))
194 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
195 gdp = ext2_get_group_desc(inode->i_sb, block_group, &bh);
200 * Figure out the offset within the block group inode table
202 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
203 EXT2_INODE_SIZE(inode->i_sb);
204 block = le32_to_cpu(gdp->bg_inode_table) +
205 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
206 sb_breadahead(inode->i_sb, block);
210 * There are two policies for allocating an inode. If the new inode is
211 * a directory, then a forward search is made for a block group with both
212 * free space and a low directory-to-inode ratio; if that fails, then of
213 * the groups with above-average free space, that group with the fewest
214 * directories already is chosen.
216 * For other inodes, search forward from the parent directory\'s block
217 * group to find a free inode.
219 static int find_group_dir(struct super_block *sb, struct inode *parent)
221 int ngroups = EXT2_SB(sb)->s_groups_count;
222 int avefreei = ext2_count_free_inodes(sb) / ngroups;
223 struct ext2_group_desc *desc, *best_desc = NULL;
224 struct buffer_head *bh, *best_bh = NULL;
225 int group, best_group = -1;
227 for (group = 0; group < ngroups; group++) {
228 desc = ext2_get_group_desc (sb, group, &bh);
229 if (!desc || !desc->bg_free_inodes_count)
231 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
234 (le16_to_cpu(desc->bg_free_blocks_count) >
235 le16_to_cpu(best_desc->bg_free_blocks_count))) {
248 * Orlov's allocator for directories.
250 * We always try to spread first-level directories.
252 * If there are blockgroups with both free inodes and free blocks counts
253 * not worse than average we return one with smallest directory count.
254 * Otherwise we simply return a random group.
256 * For the rest rules look so:
258 * It's OK to put directory into a group unless
259 * it has too many directories already (max_dirs) or
260 * it has too few free inodes left (min_inodes) or
261 * it has too few free blocks left (min_blocks) or
262 * it's already running too large debt (max_debt).
263 * Parent's group is prefered, if it doesn't satisfy these
264 * conditions we search cyclically through the rest. If none
265 * of the groups look good we just look for a group with more
266 * free inodes than average (starting at parent's group).
268 * Debt is incremented each time we allocate a directory and decremented
269 * when we allocate an inode, within 0--255.
272 #define INODE_COST 64
273 #define BLOCK_COST 256
275 static int find_group_orlov(struct super_block *sb, struct inode *parent)
277 int parent_group = EXT2_I(parent)->i_block_group;
278 struct ext2_sb_info *sbi = EXT2_SB(sb);
279 struct ext2_super_block *es = sbi->s_es;
280 int ngroups = sbi->s_groups_count;
281 int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
288 int max_debt, max_dirs, min_blocks, min_inodes;
290 struct ext2_group_desc *desc;
291 struct buffer_head *bh;
293 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
294 avefreei = freei / ngroups;
295 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
296 avefreeb = free_blocks / ngroups;
297 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
299 if ((parent == sb->s_root->d_inode) ||
300 (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
301 struct ext2_group_desc *best_desc = NULL;
302 struct buffer_head *best_bh = NULL;
303 int best_ndir = inodes_per_group;
306 get_random_bytes(&group, sizeof(group));
307 parent_group = (unsigned)group % ngroups;
308 for (i = 0; i < ngroups; i++) {
309 group = (parent_group + i) % ngroups;
310 desc = ext2_get_group_desc (sb, group, &bh);
311 if (!desc || !desc->bg_free_inodes_count)
313 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
315 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
317 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
320 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
324 if (best_group >= 0) {
334 ndirs = 1; /* percpu_counters are approximate... */
336 blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
338 max_dirs = ndirs / ngroups + inodes_per_group / 16;
339 min_inodes = avefreei - inodes_per_group / 4;
340 min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
342 max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
343 if (max_debt * INODE_COST > inodes_per_group)
344 max_debt = inodes_per_group / INODE_COST;
350 for (i = 0; i < ngroups; i++) {
351 group = (parent_group + i) % ngroups;
352 desc = ext2_get_group_desc (sb, group, &bh);
353 if (!desc || !desc->bg_free_inodes_count)
355 if (sbi->s_debts[group] >= max_debt)
357 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
359 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
361 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
367 for (i = 0; i < ngroups; i++) {
368 group = (parent_group + i) % ngroups;
369 desc = ext2_get_group_desc (sb, group, &bh);
370 if (!desc || !desc->bg_free_inodes_count)
372 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
378 * The free-inodes counter is approximate, and for really small
379 * filesystems the above test can fail to find any blockgroups
391 static int find_group_other(struct super_block *sb, struct inode *parent)
393 int parent_group = EXT2_I(parent)->i_block_group;
394 int ngroups = EXT2_SB(sb)->s_groups_count;
395 struct ext2_group_desc *desc;
396 struct buffer_head *bh;
400 * Try to place the inode in its parent directory
402 group = parent_group;
403 desc = ext2_get_group_desc (sb, group, &bh);
404 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
405 le16_to_cpu(desc->bg_free_blocks_count))
409 * We're going to place this inode in a different blockgroup from its
410 * parent. We want to cause files in a common directory to all land in
411 * the same blockgroup. But we want files which are in a different
412 * directory which shares a blockgroup with our parent to land in a
413 * different blockgroup.
415 * So add our directory's i_ino into the starting point for the hash.
417 group = (group + parent->i_ino) % ngroups;
420 * Use a quadratic hash to find a group with a free inode and some
423 for (i = 1; i < ngroups; i <<= 1) {
425 if (group >= ngroups)
427 desc = ext2_get_group_desc (sb, group, &bh);
428 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
429 le16_to_cpu(desc->bg_free_blocks_count))
434 * That failed: try linear search for a free inode, even if that group
435 * has no free blocks.
437 group = parent_group;
438 for (i = 0; i < ngroups; i++) {
439 if (++group >= ngroups)
441 desc = ext2_get_group_desc (sb, group, &bh);
442 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
452 struct inode *ext2_new_inode(struct inode *dir, int mode)
454 struct super_block *sb;
455 struct buffer_head *bitmap_bh = NULL;
456 struct buffer_head *bh2;
459 struct inode * inode;
460 struct ext2_group_desc *gdp;
461 struct ext2_super_block *es;
462 struct ext2_inode_info *ei;
463 struct ext2_sb_info *sbi;
467 inode = new_inode(sb);
469 return ERR_PTR(-ENOMEM);
471 if (sb->s_flags & MS_TAGXID)
472 inode->i_xid = vx_current_xid();
476 if (DLIMIT_ALLOC_INODE(sb, inode->i_xid)) {
484 if (test_opt(sb, OLDALLOC))
485 group = find_group_dir(sb, dir);
487 group = find_group_orlov(sb, dir);
489 group = find_group_other(sb, dir);
496 for (i = 0; i < sbi->s_groups_count; i++) {
497 gdp = ext2_get_group_desc(sb, group, &bh2);
499 bitmap_bh = read_inode_bitmap(sb, group);
506 repeat_in_this_group:
507 ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
508 EXT2_INODES_PER_GROUP(sb), ino);
509 if (ino >= EXT2_INODES_PER_GROUP(sb)) {
511 * Rare race: find_group_xx() decided that there were
512 * free inodes in this group, but by the time we tried
513 * to allocate one, they're all gone. This can also
514 * occur because the counters which find_group_orlov()
515 * uses are approximate. So just go and search the
518 if (++group == sbi->s_groups_count)
522 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
523 ino, bitmap_bh->b_data)) {
524 /* we lost this inode */
525 if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
526 /* this group is exhausted, try next group */
527 if (++group == sbi->s_groups_count)
531 /* try to find free inode in the same group */
532 goto repeat_in_this_group;
538 * Scanned all blockgroups.
543 mark_buffer_dirty(bitmap_bh);
544 if (sb->s_flags & MS_SYNCHRONOUS)
545 sync_dirty_buffer(bitmap_bh);
548 ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
549 if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
550 ext2_error (sb, "ext2_new_inode",
551 "reserved inode or inode > inodes count - "
552 "block_group = %d,inode=%lu", group,
553 (unsigned long) ino);
558 percpu_counter_mod(&sbi->s_freeinodes_counter, -1);
560 percpu_counter_inc(&sbi->s_dirs_counter);
562 spin_lock(sb_bgl_lock(sbi, group));
563 gdp->bg_free_inodes_count =
564 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
566 if (sbi->s_debts[group] < 255)
567 sbi->s_debts[group]++;
568 gdp->bg_used_dirs_count =
569 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
571 if (sbi->s_debts[group])
572 sbi->s_debts[group]--;
574 spin_unlock(sb_bgl_lock(sbi, group));
577 mark_buffer_dirty(bh2);
578 inode->i_uid = current->fsuid;
579 if (test_opt (sb, GRPID))
580 inode->i_gid = dir->i_gid;
581 else if (dir->i_mode & S_ISGID) {
582 inode->i_gid = dir->i_gid;
586 inode->i_gid = current->fsgid;
587 inode->i_mode = mode;
590 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
592 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
593 memset(ei->i_data, 0, sizeof(ei->i_data));
594 ei->i_flags = EXT2_I(dir)->i_flags &
595 ~(EXT2_BTREE_FL|EXT2_IUNLINK_FL|EXT2_BARRIER_FL);
597 ei->i_flags &= ~(EXT2_IMMUTABLE_FL|EXT2_APPEND_FL);
598 /* dirsync is only applied to directories */
600 ei->i_flags &= ~EXT2_DIRSYNC_FL;
607 ei->i_block_group = group;
608 ei->i_next_alloc_block = 0;
609 ei->i_next_alloc_goal = 0;
610 ei->i_prealloc_block = 0;
611 ei->i_prealloc_count = 0;
612 ei->i_dir_start_lookup = 0;
613 ei->i_state = EXT2_STATE_NEW;
614 ext2_set_inode_flags(inode);
615 spin_lock(&sbi->s_next_gen_lock);
616 inode->i_generation = sbi->s_next_generation++;
617 spin_unlock(&sbi->s_next_gen_lock);
618 insert_inode_hash(inode);
620 if (DQUOT_ALLOC_INODE(inode)) {
625 err = ext2_init_acl(inode, dir);
627 DQUOT_FREE_INODE(inode);
630 mark_inode_dirty(inode);
631 ext2_debug("allocating inode %lu\n", inode->i_ino);
632 ext2_preread_inode(inode);
636 DLIMIT_FREE_INODE(sb, inode->i_xid);
637 inode->i_flags |= S_NOQUOTA;
643 DLIMIT_FREE_INODE(sb, inode->i_xid);
645 make_bad_inode(inode);
650 unsigned long ext2_count_free_inodes (struct super_block * sb)
652 struct ext2_group_desc *desc;
653 unsigned long desc_count = 0;
657 struct ext2_super_block *es;
658 unsigned long bitmap_count = 0;
659 struct buffer_head *bitmap_bh = NULL;
662 es = EXT2_SB(sb)->s_es;
663 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
666 desc = ext2_get_group_desc (sb, i, NULL);
669 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
671 bitmap_bh = read_inode_bitmap(sb, i);
675 x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
676 printk("group %d: stored = %d, counted = %u\n",
677 i, le16_to_cpu(desc->bg_free_inodes_count), x);
681 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
682 percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
683 desc_count, bitmap_count);
687 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
688 desc = ext2_get_group_desc (sb, i, NULL);
691 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
697 /* Called at mount-time, super-block is locked */
698 unsigned long ext2_count_dirs (struct super_block * sb)
700 unsigned long count = 0;
703 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
704 struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
707 count += le16_to_cpu(gdp->bg_used_dirs_count);
712 #ifdef CONFIG_EXT2_CHECK
713 /* Called at mount-time, super-block is locked */
714 void ext2_check_inodes_bitmap (struct super_block * sb)
716 struct ext2_super_block * es = EXT2_SB(sb)->s_es;
717 unsigned long desc_count = 0, bitmap_count = 0;
718 struct buffer_head *bitmap_bh = NULL;
721 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
722 struct ext2_group_desc *desc;
725 desc = ext2_get_group_desc(sb, i, NULL);
728 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
730 bitmap_bh = read_inode_bitmap(sb, i);
734 x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
735 if (le16_to_cpu(desc->bg_free_inodes_count) != x)
736 ext2_error (sb, "ext2_check_inodes_bitmap",
737 "Wrong free inodes count in group %d, "
738 "stored = %d, counted = %lu", i,
739 le16_to_cpu(desc->bg_free_inodes_count), x);
743 if (percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter) !=
745 ext2_error(sb, "ext2_check_inodes_bitmap",
746 "Wrong free inodes count in super block, "
747 "stored = %lu, counted = %lu",
748 (unsigned long)le32_to_cpu(es->s_free_inodes_count),