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/quotaops.h>
16 #include <linux/sched.h>
17 #include <linux/backing-dev.h>
18 #include <linux/buffer_head.h>
19 #include <linux/random.h>
20 #include <linux/vs_dlimit.h>
21 #include <linux/vs_tag.h>
27 * ialloc.c contains the inodes allocation and deallocation routines
31 * The free inodes are managed by bitmaps. A file system contains several
32 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
33 * block for inodes, N blocks for the inode table and data blocks.
35 * The file system contains group descriptors which are located after the
36 * super block. Each descriptor contains the number of the bitmap block and
37 * the free blocks count in the block.
42 * Read the inode allocation bitmap for a given block_group, reading
43 * into the specified slot in the superblock's bitmap cache.
45 * Return buffer_head of bitmap on success or NULL.
47 static struct buffer_head *
48 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
50 struct ext2_group_desc *desc;
51 struct buffer_head *bh = NULL;
53 desc = ext2_get_group_desc(sb, block_group, NULL);
57 bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
59 ext2_error(sb, "read_inode_bitmap",
60 "Cannot read inode bitmap - "
61 "block_group = %lu, inode_bitmap = %u",
62 block_group, le32_to_cpu(desc->bg_inode_bitmap));
67 static void ext2_release_inode(struct super_block *sb, int group, int dir)
69 struct ext2_group_desc * desc;
70 struct buffer_head *bh;
72 desc = ext2_get_group_desc(sb, group, &bh);
74 ext2_error(sb, "ext2_release_inode",
75 "can't get descriptor for group %d", group);
79 spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
80 desc->bg_free_inodes_count =
81 cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
83 desc->bg_used_dirs_count =
84 cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
85 spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
87 percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
89 mark_buffer_dirty(bh);
93 * NOTE! When we get the inode, we're the only people
94 * that have access to it, and as such there are no
95 * race conditions we have to worry about. The inode
96 * is not on the hash-lists, and it cannot be reached
97 * through the filesystem because the directory entry
98 * has been deleted earlier.
100 * HOWEVER: we must make sure that we get no aliases,
101 * which means that we have to call "clear_inode()"
102 * _before_ we mark the inode not in use in the inode
103 * bitmaps. Otherwise a newly created file might use
104 * the same inode number (not actually the same pointer
105 * though), and then we'd have two inodes sharing the
106 * same inode number and space on the harddisk.
108 void ext2_free_inode (struct inode * inode)
110 struct super_block * sb = inode->i_sb;
113 struct buffer_head *bitmap_bh = NULL;
114 unsigned long block_group;
116 struct ext2_super_block * es;
119 ext2_debug ("freeing inode %lu\n", ino);
122 * Note: we must free any quota before locking the superblock,
123 * as writing the quota to disk may need the lock as well.
125 if (!is_bad_inode(inode)) {
126 /* Quota is already initialized in iput() */
127 ext2_xattr_delete_inode(inode);
128 DQUOT_FREE_INODE(inode);
130 DLIMIT_FREE_INODE(inode);
133 es = EXT2_SB(sb)->s_es;
134 is_directory = S_ISDIR(inode->i_mode);
136 /* Do this BEFORE marking the inode not in use or returning an error */
139 if (ino < EXT2_FIRST_INO(sb) ||
140 ino > le32_to_cpu(es->s_inodes_count)) {
141 ext2_error (sb, "ext2_free_inode",
142 "reserved or nonexistent inode %lu", ino);
145 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
146 bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
148 bitmap_bh = read_inode_bitmap(sb, block_group);
152 /* Ok, now we can actually update the inode bitmaps.. */
153 if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
154 bit, (void *) bitmap_bh->b_data))
155 ext2_error (sb, "ext2_free_inode",
156 "bit already cleared for inode %lu", ino);
158 ext2_release_inode(sb, block_group, is_directory);
159 mark_buffer_dirty(bitmap_bh);
160 if (sb->s_flags & MS_SYNCHRONOUS)
161 sync_dirty_buffer(bitmap_bh);
167 * We perform asynchronous prereading of the new inode's inode block when
168 * we create the inode, in the expectation that the inode will be written
169 * back soon. There are two reasons:
171 * - When creating a large number of files, the async prereads will be
172 * nicely merged into large reads
173 * - When writing out a large number of inodes, we don't need to keep on
174 * stalling the writes while we read the inode block.
176 * FIXME: ext2_get_group_desc() needs to be simplified.
178 static void ext2_preread_inode(struct inode *inode)
180 unsigned long block_group;
181 unsigned long offset;
183 struct buffer_head *bh;
184 struct ext2_group_desc * gdp;
185 struct backing_dev_info *bdi;
187 bdi = inode->i_mapping->backing_dev_info;
188 if (bdi_read_congested(bdi))
190 if (bdi_write_congested(bdi))
193 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
194 gdp = ext2_get_group_desc(inode->i_sb, block_group, &bh);
199 * Figure out the offset within the block group inode table
201 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
202 EXT2_INODE_SIZE(inode->i_sb);
203 block = le32_to_cpu(gdp->bg_inode_table) +
204 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
205 sb_breadahead(inode->i_sb, block);
209 * There are two policies for allocating an inode. If the new inode is
210 * a directory, then a forward search is made for a block group with both
211 * free space and a low directory-to-inode ratio; if that fails, then of
212 * the groups with above-average free space, that group with the fewest
213 * directories already is chosen.
215 * For other inodes, search forward from the parent directory\'s block
216 * group to find a free inode.
218 static int find_group_dir(struct super_block *sb, struct inode *parent)
220 int ngroups = EXT2_SB(sb)->s_groups_count;
221 int avefreei = ext2_count_free_inodes(sb) / ngroups;
222 struct ext2_group_desc *desc, *best_desc = NULL;
223 struct buffer_head *bh, *best_bh = NULL;
224 int group, best_group = -1;
226 for (group = 0; group < ngroups; group++) {
227 desc = ext2_get_group_desc (sb, group, &bh);
228 if (!desc || !desc->bg_free_inodes_count)
230 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
233 (le16_to_cpu(desc->bg_free_blocks_count) >
234 le16_to_cpu(best_desc->bg_free_blocks_count))) {
247 * Orlov's allocator for directories.
249 * We always try to spread first-level directories.
251 * If there are blockgroups with both free inodes and free blocks counts
252 * not worse than average we return one with smallest directory count.
253 * Otherwise we simply return a random group.
255 * For the rest rules look so:
257 * It's OK to put directory into a group unless
258 * it has too many directories already (max_dirs) or
259 * it has too few free inodes left (min_inodes) or
260 * it has too few free blocks left (min_blocks) or
261 * it's already running too large debt (max_debt).
262 * Parent's group is prefered, if it doesn't satisfy these
263 * conditions we search cyclically through the rest. If none
264 * of the groups look good we just look for a group with more
265 * free inodes than average (starting at parent's group).
267 * Debt is incremented each time we allocate a directory and decremented
268 * when we allocate an inode, within 0--255.
271 #define INODE_COST 64
272 #define BLOCK_COST 256
274 static int find_group_orlov(struct super_block *sb, struct inode *parent)
276 int parent_group = EXT2_I(parent)->i_block_group;
277 struct ext2_sb_info *sbi = EXT2_SB(sb);
278 struct ext2_super_block *es = sbi->s_es;
279 int ngroups = sbi->s_groups_count;
280 int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
287 int max_debt, max_dirs, min_blocks, min_inodes;
289 struct ext2_group_desc *desc;
290 struct buffer_head *bh;
292 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
293 avefreei = freei / ngroups;
294 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
295 avefreeb = free_blocks / ngroups;
296 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
298 if ((parent == sb->s_root->d_inode) ||
299 (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
300 struct ext2_group_desc *best_desc = NULL;
301 struct buffer_head *best_bh = NULL;
302 int best_ndir = inodes_per_group;
305 get_random_bytes(&group, sizeof(group));
306 parent_group = (unsigned)group % ngroups;
307 for (i = 0; i < ngroups; i++) {
308 group = (parent_group + i) % ngroups;
309 desc = ext2_get_group_desc (sb, group, &bh);
310 if (!desc || !desc->bg_free_inodes_count)
312 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
314 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
316 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
319 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
323 if (best_group >= 0) {
333 ndirs = 1; /* percpu_counters are approximate... */
335 blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
337 max_dirs = ndirs / ngroups + inodes_per_group / 16;
338 min_inodes = avefreei - inodes_per_group / 4;
339 min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
341 max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
342 if (max_debt * INODE_COST > inodes_per_group)
343 max_debt = inodes_per_group / INODE_COST;
349 for (i = 0; i < ngroups; i++) {
350 group = (parent_group + i) % ngroups;
351 desc = ext2_get_group_desc (sb, group, &bh);
352 if (!desc || !desc->bg_free_inodes_count)
354 if (sbi->s_debts[group] >= max_debt)
356 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
358 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
360 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
366 for (i = 0; i < ngroups; i++) {
367 group = (parent_group + i) % ngroups;
368 desc = ext2_get_group_desc (sb, group, &bh);
369 if (!desc || !desc->bg_free_inodes_count)
371 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
377 * The free-inodes counter is approximate, and for really small
378 * filesystems the above test can fail to find any blockgroups
390 static int find_group_other(struct super_block *sb, struct inode *parent)
392 int parent_group = EXT2_I(parent)->i_block_group;
393 int ngroups = EXT2_SB(sb)->s_groups_count;
394 struct ext2_group_desc *desc;
395 struct buffer_head *bh;
399 * Try to place the inode in its parent directory
401 group = parent_group;
402 desc = ext2_get_group_desc (sb, group, &bh);
403 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
404 le16_to_cpu(desc->bg_free_blocks_count))
408 * We're going to place this inode in a different blockgroup from its
409 * parent. We want to cause files in a common directory to all land in
410 * the same blockgroup. But we want files which are in a different
411 * directory which shares a blockgroup with our parent to land in a
412 * different blockgroup.
414 * So add our directory's i_ino into the starting point for the hash.
416 group = (group + parent->i_ino) % ngroups;
419 * Use a quadratic hash to find a group with a free inode and some
422 for (i = 1; i < ngroups; i <<= 1) {
424 if (group >= ngroups)
426 desc = ext2_get_group_desc (sb, group, &bh);
427 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
428 le16_to_cpu(desc->bg_free_blocks_count))
433 * That failed: try linear search for a free inode, even if that group
434 * has no free blocks.
436 group = parent_group;
437 for (i = 0; i < ngroups; i++) {
438 if (++group >= ngroups)
440 desc = ext2_get_group_desc (sb, group, &bh);
441 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
451 struct inode *ext2_new_inode(struct inode *dir, int mode)
453 struct super_block *sb;
454 struct buffer_head *bitmap_bh = NULL;
455 struct buffer_head *bh2;
458 struct inode * inode;
459 struct ext2_group_desc *gdp;
460 struct ext2_super_block *es;
461 struct ext2_inode_info *ei;
462 struct ext2_sb_info *sbi;
466 inode = new_inode(sb);
468 return ERR_PTR(-ENOMEM);
470 inode->i_tag = dx_current_fstag(sb);
471 if (DLIMIT_ALLOC_INODE(inode)) {
479 if (test_opt(sb, OLDALLOC))
480 group = find_group_dir(sb, dir);
482 group = find_group_orlov(sb, dir);
484 group = find_group_other(sb, dir);
491 for (i = 0; i < sbi->s_groups_count; i++) {
492 gdp = ext2_get_group_desc(sb, group, &bh2);
494 bitmap_bh = read_inode_bitmap(sb, group);
501 repeat_in_this_group:
502 ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
503 EXT2_INODES_PER_GROUP(sb), ino);
504 if (ino >= EXT2_INODES_PER_GROUP(sb)) {
506 * Rare race: find_group_xx() decided that there were
507 * free inodes in this group, but by the time we tried
508 * to allocate one, they're all gone. This can also
509 * occur because the counters which find_group_orlov()
510 * uses are approximate. So just go and search the
513 if (++group == sbi->s_groups_count)
517 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
518 ino, bitmap_bh->b_data)) {
519 /* we lost this inode */
520 if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
521 /* this group is exhausted, try next group */
522 if (++group == sbi->s_groups_count)
526 /* try to find free inode in the same group */
527 goto repeat_in_this_group;
533 * Scanned all blockgroups.
538 mark_buffer_dirty(bitmap_bh);
539 if (sb->s_flags & MS_SYNCHRONOUS)
540 sync_dirty_buffer(bitmap_bh);
543 ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
544 if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
545 ext2_error (sb, "ext2_new_inode",
546 "reserved inode or inode > inodes count - "
547 "block_group = %d,inode=%lu", group,
548 (unsigned long) ino);
553 percpu_counter_mod(&sbi->s_freeinodes_counter, -1);
555 percpu_counter_inc(&sbi->s_dirs_counter);
557 spin_lock(sb_bgl_lock(sbi, group));
558 gdp->bg_free_inodes_count =
559 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
561 if (sbi->s_debts[group] < 255)
562 sbi->s_debts[group]++;
563 gdp->bg_used_dirs_count =
564 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
566 if (sbi->s_debts[group])
567 sbi->s_debts[group]--;
569 spin_unlock(sb_bgl_lock(sbi, group));
572 mark_buffer_dirty(bh2);
573 inode->i_uid = current->fsuid;
574 if (test_opt (sb, GRPID))
575 inode->i_gid = dir->i_gid;
576 else if (dir->i_mode & S_ISGID) {
577 inode->i_gid = dir->i_gid;
581 inode->i_gid = current->fsgid;
582 inode->i_mode = mode;
586 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
587 memset(ei->i_data, 0, sizeof(ei->i_data));
588 ei->i_flags = EXT2_I(dir)->i_flags &
589 ~(EXT2_BTREE_FL|EXT2_IUNLINK_FL|EXT2_BARRIER_FL);
591 ei->i_flags &= ~(EXT2_IMMUTABLE_FL|EXT2_APPEND_FL);
592 /* dirsync is only applied to directories */
594 ei->i_flags &= ~EXT2_DIRSYNC_FL;
601 ei->i_block_group = group;
602 ei->i_next_alloc_block = 0;
603 ei->i_next_alloc_goal = 0;
604 ei->i_prealloc_block = 0;
605 ei->i_prealloc_count = 0;
606 ei->i_dir_start_lookup = 0;
607 ei->i_state = EXT2_STATE_NEW;
608 ext2_set_inode_flags(inode);
609 spin_lock(&sbi->s_next_gen_lock);
610 inode->i_generation = sbi->s_next_generation++;
611 spin_unlock(&sbi->s_next_gen_lock);
612 insert_inode_hash(inode);
614 if (DQUOT_ALLOC_INODE(inode)) {
619 err = ext2_init_acl(inode, dir);
623 err = ext2_init_security(inode,dir);
627 mark_inode_dirty(inode);
628 ext2_debug("allocating inode %lu\n", inode->i_ino);
629 ext2_preread_inode(inode);
633 DQUOT_FREE_INODE(inode);
637 DLIMIT_FREE_INODE(inode);
638 inode->i_flags |= S_NOQUOTA;
644 DLIMIT_FREE_INODE(inode);
646 make_bad_inode(inode);
651 unsigned long ext2_count_free_inodes (struct super_block * sb)
653 struct ext2_group_desc *desc;
654 unsigned long desc_count = 0;
658 struct ext2_super_block *es;
659 unsigned long bitmap_count = 0;
660 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);
686 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
687 desc = ext2_get_group_desc (sb, i, NULL);
690 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
696 /* Called at mount-time, super-block is locked */
697 unsigned long ext2_count_dirs (struct super_block * sb)
699 unsigned long count = 0;
702 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
703 struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
706 count += le16_to_cpu(gdp->bg_used_dirs_count);