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/vs_dlimit.h>
26 #include <linux/bitops.h>
27 #include <linux/vs_dlimit.h>
28 #include <linux/vserver/xid.h>
30 #include <asm/byteorder.h>
36 * ialloc.c contains the inodes allocation and deallocation routines
40 * The free inodes are managed by bitmaps. A file system contains several
41 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
42 * block for inodes, N blocks for the inode table and data blocks.
44 * The file system contains group descriptors which are located after the
45 * super block. Each descriptor contains the number of the bitmap block and
46 * the free blocks count in the block.
51 * Read the inode allocation bitmap for a given block_group, reading
52 * into the specified slot in the superblock's bitmap cache.
54 * Return buffer_head of bitmap on success or NULL.
56 static struct buffer_head *
57 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
59 struct ext3_group_desc *desc;
60 struct buffer_head *bh = NULL;
62 desc = ext3_get_group_desc(sb, block_group, NULL);
66 bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
68 ext3_error(sb, "read_inode_bitmap",
69 "Cannot read inode bitmap - "
70 "block_group = %lu, inode_bitmap = %u",
71 block_group, le32_to_cpu(desc->bg_inode_bitmap));
77 * NOTE! When we get the inode, we're the only people
78 * that have access to it, and as such there are no
79 * race conditions we have to worry about. The inode
80 * is not on the hash-lists, and it cannot be reached
81 * through the filesystem because the directory entry
82 * has been deleted earlier.
84 * HOWEVER: we must make sure that we get no aliases,
85 * which means that we have to call "clear_inode()"
86 * _before_ we mark the inode not in use in the inode
87 * bitmaps. Otherwise a newly created file might use
88 * the same inode number (not actually the same pointer
89 * though), and then we'd have two inodes sharing the
90 * same inode number and space on the harddisk.
92 void ext3_free_inode (handle_t *handle, struct inode * inode)
94 struct super_block * sb = inode->i_sb;
97 struct buffer_head *bitmap_bh = NULL;
98 struct buffer_head *bh2;
99 unsigned long block_group;
101 struct ext3_group_desc * gdp;
102 struct ext3_super_block * es;
103 struct ext3_sb_info *sbi;
106 if (atomic_read(&inode->i_count) > 1) {
107 printk ("ext3_free_inode: inode has count=%d\n",
108 atomic_read(&inode->i_count));
111 if (inode->i_nlink) {
112 printk ("ext3_free_inode: inode has nlink=%d\n",
117 printk("ext3_free_inode: inode on nonexistent device\n");
123 ext3_debug ("freeing inode %lu\n", ino);
126 * Note: we must free any quota before locking the superblock,
127 * as writing the quota to disk may need the lock as well.
130 ext3_xattr_delete_inode(handle, inode);
131 DLIMIT_FREE_INODE(sb, inode->i_xid);
132 DQUOT_FREE_INODE(inode);
135 is_directory = S_ISDIR(inode->i_mode);
137 /* Do this BEFORE marking the inode not in use or returning an error */
140 es = EXT3_SB(sb)->s_es;
141 if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
142 ext3_error (sb, "ext3_free_inode",
143 "reserved or nonexistent inode %lu", ino);
146 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
147 bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
148 bitmap_bh = read_inode_bitmap(sb, block_group);
152 BUFFER_TRACE(bitmap_bh, "get_write_access");
153 fatal = ext3_journal_get_write_access(handle, bitmap_bh);
157 /* Ok, now we can actually update the inode bitmaps.. */
158 if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
159 bit, bitmap_bh->b_data))
160 ext3_error (sb, "ext3_free_inode",
161 "bit already cleared for inode %lu", ino);
163 gdp = ext3_get_group_desc (sb, block_group, &bh2);
165 BUFFER_TRACE(bh2, "get_write_access");
166 fatal = ext3_journal_get_write_access(handle, bh2);
167 if (fatal) goto error_return;
170 spin_lock(sb_bgl_lock(sbi, block_group));
171 gdp->bg_free_inodes_count = cpu_to_le16(
172 le16_to_cpu(gdp->bg_free_inodes_count) + 1);
174 gdp->bg_used_dirs_count = cpu_to_le16(
175 le16_to_cpu(gdp->bg_used_dirs_count) - 1);
176 spin_unlock(sb_bgl_lock(sbi, block_group));
177 percpu_counter_inc(&sbi->s_freeinodes_counter);
179 percpu_counter_dec(&sbi->s_dirs_counter);
182 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
183 err = ext3_journal_dirty_metadata(handle, bh2);
184 if (!fatal) fatal = err;
186 BUFFER_TRACE(bitmap_bh, "call ext3_journal_dirty_metadata");
187 err = ext3_journal_dirty_metadata(handle, bitmap_bh);
193 ext3_std_error(sb, fatal);
197 * There are two policies for allocating an inode. If the new inode is
198 * a directory, then a forward search is made for a block group with both
199 * free space and a low directory-to-inode ratio; if that fails, then of
200 * the groups with above-average free space, that group with the fewest
201 * directories already is chosen.
203 * For other inodes, search forward from the parent directory\'s block
204 * group to find a free inode.
206 static int find_group_dir(struct super_block *sb, struct inode *parent)
208 int ngroups = EXT3_SB(sb)->s_groups_count;
210 struct ext3_group_desc *desc, *best_desc = NULL;
211 struct buffer_head *bh;
212 int group, best_group = -1;
214 freei = percpu_counter_read_positive(&EXT3_SB(sb)->s_freeinodes_counter);
215 avefreei = freei / ngroups;
217 for (group = 0; group < ngroups; group++) {
218 desc = ext3_get_group_desc (sb, group, &bh);
219 if (!desc || !desc->bg_free_inodes_count)
221 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
224 (le16_to_cpu(desc->bg_free_blocks_count) >
225 le16_to_cpu(best_desc->bg_free_blocks_count))) {
234 * Orlov's allocator for directories.
236 * We always try to spread first-level directories.
238 * If there are blockgroups with both free inodes and free blocks counts
239 * not worse than average we return one with smallest directory count.
240 * Otherwise we simply return a random group.
242 * For the rest rules look so:
244 * It's OK to put directory into a group unless
245 * it has too many directories already (max_dirs) or
246 * it has too few free inodes left (min_inodes) or
247 * it has too few free blocks left (min_blocks) or
248 * it's already running too large debt (max_debt).
249 * Parent's group is prefered, if it doesn't satisfy these
250 * conditions we search cyclically through the rest. If none
251 * of the groups look good we just look for a group with more
252 * free inodes than average (starting at parent's group).
254 * Debt is incremented each time we allocate a directory and decremented
255 * when we allocate an inode, within 0--255.
258 #define INODE_COST 64
259 #define BLOCK_COST 256
261 static int find_group_orlov(struct super_block *sb, struct inode *parent)
263 int parent_group = EXT3_I(parent)->i_block_group;
264 struct ext3_sb_info *sbi = EXT3_SB(sb);
265 struct ext3_super_block *es = sbi->s_es;
266 int ngroups = sbi->s_groups_count;
267 int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
270 int blocks_per_dir, ndirs;
271 int max_debt, max_dirs, min_blocks, min_inodes;
273 struct ext3_group_desc *desc;
274 struct buffer_head *bh;
276 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
277 avefreei = freei / ngroups;
278 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
279 avefreeb = freeb / ngroups;
280 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
282 if ((parent == sb->s_root->d_inode) ||
283 (EXT3_I(parent)->i_flags & EXT3_TOPDIR_FL)) {
284 int best_ndir = inodes_per_group;
287 get_random_bytes(&group, sizeof(group));
288 parent_group = (unsigned)group % ngroups;
289 for (i = 0; i < ngroups; i++) {
290 group = (parent_group + i) % ngroups;
291 desc = ext3_get_group_desc (sb, group, &bh);
292 if (!desc || !desc->bg_free_inodes_count)
294 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
296 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
298 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
301 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
308 blocks_per_dir = (le32_to_cpu(es->s_blocks_count) - freeb) / ndirs;
310 max_dirs = ndirs / ngroups + inodes_per_group / 16;
311 min_inodes = avefreei - inodes_per_group / 4;
312 min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;
314 max_debt = EXT3_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
315 if (max_debt * INODE_COST > inodes_per_group)
316 max_debt = inodes_per_group / INODE_COST;
322 for (i = 0; i < ngroups; i++) {
323 group = (parent_group + i) % ngroups;
324 desc = ext3_get_group_desc (sb, group, &bh);
325 if (!desc || !desc->bg_free_inodes_count)
327 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
329 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
331 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
337 for (i = 0; i < ngroups; i++) {
338 group = (parent_group + i) % ngroups;
339 desc = ext3_get_group_desc (sb, group, &bh);
340 if (!desc || !desc->bg_free_inodes_count)
342 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
348 * The free-inodes counter is approximate, and for really small
349 * filesystems the above test can fail to find any blockgroups
358 static int find_group_other(struct super_block *sb, struct inode *parent)
360 int parent_group = EXT3_I(parent)->i_block_group;
361 int ngroups = EXT3_SB(sb)->s_groups_count;
362 struct ext3_group_desc *desc;
363 struct buffer_head *bh;
367 * Try to place the inode in its parent directory
369 group = parent_group;
370 desc = ext3_get_group_desc (sb, group, &bh);
371 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
372 le16_to_cpu(desc->bg_free_blocks_count))
376 * We're going to place this inode in a different blockgroup from its
377 * parent. We want to cause files in a common directory to all land in
378 * the same blockgroup. But we want files which are in a different
379 * directory which shares a blockgroup with our parent to land in a
380 * different blockgroup.
382 * So add our directory's i_ino into the starting point for the hash.
384 group = (group + parent->i_ino) % ngroups;
387 * Use a quadratic hash to find a group with a free inode and some free
390 for (i = 1; i < ngroups; i <<= 1) {
392 if (group >= ngroups)
394 desc = ext3_get_group_desc (sb, group, &bh);
395 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
396 le16_to_cpu(desc->bg_free_blocks_count))
401 * That failed: try linear search for a free inode, even if that group
402 * has no free blocks.
404 group = parent_group;
405 for (i = 0; i < ngroups; i++) {
406 if (++group >= ngroups)
408 desc = ext3_get_group_desc (sb, group, &bh);
409 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
417 * There are two policies for allocating an inode. If the new inode is
418 * a directory, then a forward search is made for a block group with both
419 * free space and a low directory-to-inode ratio; if that fails, then of
420 * the groups with above-average free space, that group with the fewest
421 * directories already is chosen.
423 * For other inodes, search forward from the parent directory's block
424 * group to find a free inode.
426 struct inode *ext3_new_inode(handle_t *handle, struct inode * dir, int mode)
428 struct super_block *sb;
429 struct buffer_head *bitmap_bh = NULL;
430 struct buffer_head *bh2;
432 unsigned long ino = 0;
433 struct inode * inode;
434 struct ext3_group_desc * gdp = NULL;
435 struct ext3_super_block * es;
436 struct ext3_inode_info *ei;
437 struct ext3_sb_info *sbi;
442 /* Cannot create files in a deleted directory */
443 if (!dir || !dir->i_nlink)
444 return ERR_PTR(-EPERM);
447 inode = new_inode(sb);
449 return ERR_PTR(-ENOMEM);
451 inode->i_xid = vx_current_fsxid(sb);
452 if (DLIMIT_ALLOC_INODE(sb, inode->i_xid)) {
461 if (test_opt (sb, OLDALLOC))
462 group = find_group_dir(sb, dir);
464 group = find_group_orlov(sb, dir);
466 group = find_group_other(sb, dir);
472 for (i = 0; i < sbi->s_groups_count; i++) {
475 gdp = ext3_get_group_desc(sb, group, &bh2);
480 bitmap_bh = read_inode_bitmap(sb, group);
486 repeat_in_this_group:
487 ino = ext3_find_next_zero_bit((unsigned long *)
488 bitmap_bh->b_data, EXT3_INODES_PER_GROUP(sb), ino);
489 if (ino < EXT3_INODES_PER_GROUP(sb)) {
491 BUFFER_TRACE(bitmap_bh, "get_write_access");
492 err = ext3_journal_get_write_access(handle, bitmap_bh);
496 if (!ext3_set_bit_atomic(sb_bgl_lock(sbi, group),
497 ino, bitmap_bh->b_data)) {
499 BUFFER_TRACE(bitmap_bh,
500 "call ext3_journal_dirty_metadata");
501 err = ext3_journal_dirty_metadata(handle,
508 journal_release_buffer(handle, bitmap_bh);
510 if (++ino < EXT3_INODES_PER_GROUP(sb))
511 goto repeat_in_this_group;
515 * This case is possible in concurrent environment. It is very
516 * rare. We cannot repeat the find_group_xxx() call because
517 * that will simply return the same blockgroup, because the
518 * group descriptor metadata has not yet been updated.
519 * So we just go onto the next blockgroup.
521 if (++group == sbi->s_groups_count)
528 ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
529 if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
530 ext3_error (sb, "ext3_new_inode",
531 "reserved inode or inode > inodes count - "
532 "block_group = %d, inode=%lu", group, ino);
537 BUFFER_TRACE(bh2, "get_write_access");
538 err = ext3_journal_get_write_access(handle, bh2);
540 spin_lock(sb_bgl_lock(sbi, group));
541 gdp->bg_free_inodes_count =
542 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
544 gdp->bg_used_dirs_count =
545 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
547 spin_unlock(sb_bgl_lock(sbi, group));
548 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
549 err = ext3_journal_dirty_metadata(handle, bh2);
552 percpu_counter_dec(&sbi->s_freeinodes_counter);
554 percpu_counter_inc(&sbi->s_dirs_counter);
557 inode->i_uid = current->fsuid;
558 if (test_opt (sb, GRPID))
559 inode->i_gid = dir->i_gid;
560 else if (dir->i_mode & S_ISGID) {
561 inode->i_gid = dir->i_gid;
565 inode->i_gid = current->fsgid;
566 inode->i_mode = mode;
569 /* This is the optimal IO size (for stat), not the fs block size */
570 inode->i_blksize = PAGE_SIZE;
572 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
574 memset(ei->i_data, 0, sizeof(ei->i_data));
575 ei->i_dir_start_lookup = 0;
578 ei->i_flags = EXT3_I(dir)->i_flags &
579 ~(EXT3_INDEX_FL|EXT3_IUNLINK_FL|EXT3_BARRIER_FL);
581 ei->i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
582 /* dirsync only applies to directories */
584 ei->i_flags &= ~EXT3_DIRSYNC_FL;
585 #ifdef EXT3_FRAGMENTS
593 ei->i_block_alloc_info = NULL;
594 ei->i_block_group = group;
596 ext3_set_inode_flags(inode);
597 if (IS_DIRSYNC(inode))
599 insert_inode_hash(inode);
600 spin_lock(&sbi->s_next_gen_lock);
601 inode->i_generation = sbi->s_next_generation++;
602 spin_unlock(&sbi->s_next_gen_lock);
604 ei->i_state = EXT3_STATE_NEW;
606 (EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) ?
607 sizeof(struct ext3_inode) - EXT3_GOOD_OLD_INODE_SIZE : 0;
610 if(DQUOT_ALLOC_INODE(inode)) {
615 err = ext3_init_acl(handle, inode, dir);
617 DQUOT_FREE_INODE(inode);
620 err = ext3_mark_inode_dirty(handle, inode);
622 ext3_std_error(sb, err);
623 DQUOT_FREE_INODE(inode);
627 ext3_debug("allocating inode %lu\n", inode->i_ino);
630 DLIMIT_FREE_INODE(sb, inode->i_xid);
631 ext3_std_error(sb, err);
640 DLIMIT_FREE_INODE(sb, inode->i_xid);
641 inode->i_flags |= S_NOQUOTA;
648 /* Verify that we are loading a valid orphan from disk */
649 struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
651 unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
652 unsigned long block_group;
654 struct buffer_head *bitmap_bh = NULL;
655 struct inode *inode = NULL;
657 /* Error cases - e2fsck has already cleaned up for us */
659 ext3_warning(sb, __FUNCTION__,
660 "bad orphan ino %lu! e2fsck was run?\n", ino);
664 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
665 bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
666 bitmap_bh = read_inode_bitmap(sb, block_group);
668 ext3_warning(sb, __FUNCTION__,
669 "inode bitmap error for orphan %lu\n", ino);
673 /* Having the inode bit set should be a 100% indicator that this
674 * is a valid orphan (no e2fsck run on fs). Orphans also include
675 * inodes that were being truncated, so we can't check i_nlink==0.
677 if (!ext3_test_bit(bit, bitmap_bh->b_data) ||
678 !(inode = iget(sb, ino)) || is_bad_inode(inode) ||
679 NEXT_ORPHAN(inode) > max_ino) {
680 ext3_warning(sb, __FUNCTION__,
681 "bad orphan inode %lu! e2fsck was run?\n", ino);
682 printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%llu) = %d\n",
683 bit, (unsigned long long)bitmap_bh->b_blocknr,
684 ext3_test_bit(bit, bitmap_bh->b_data));
685 printk(KERN_NOTICE "inode=%p\n", inode);
687 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
688 is_bad_inode(inode));
689 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
691 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
693 /* Avoid freeing blocks if we got a bad deleted inode */
694 if (inode && inode->i_nlink == 0)
704 unsigned long ext3_count_free_inodes (struct super_block * sb)
706 unsigned long desc_count;
707 struct ext3_group_desc *gdp;
710 struct ext3_super_block *es;
711 unsigned long bitmap_count, x;
712 struct buffer_head *bitmap_bh = NULL;
715 es = EXT3_SB(sb)->s_es;
719 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
720 gdp = ext3_get_group_desc (sb, i, NULL);
723 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
725 bitmap_bh = read_inode_bitmap(sb, i);
729 x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
730 printk("group %d: stored = %d, counted = %lu\n",
731 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
735 printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
736 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
741 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
742 gdp = ext3_get_group_desc (sb, i, NULL);
745 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
752 /* Called at mount-time, super-block is locked */
753 unsigned long ext3_count_dirs (struct super_block * sb)
755 unsigned long count = 0;
758 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
759 struct ext3_group_desc *gdp = ext3_get_group_desc (sb, i, NULL);
762 count += le16_to_cpu(gdp->bg_used_dirs_count);
767 #ifdef CONFIG_EXT3_CHECK
768 /* Called at mount-time, super-block is locked */
769 void ext3_check_inodes_bitmap (struct super_block * sb)
771 struct ext3_super_block * es;
772 unsigned long desc_count, bitmap_count, x;
773 struct buffer_head *bitmap_bh = NULL;
774 struct ext3_group_desc * gdp;
777 es = EXT3_SB(sb)->s_es;
781 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
782 gdp = ext3_get_group_desc (sb, i, NULL);
785 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
787 bitmap_bh = read_inode_bitmap(sb, i);
791 x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
792 if (le16_to_cpu(gdp->bg_free_inodes_count) != x)
793 ext3_error (sb, "ext3_check_inodes_bitmap",
794 "Wrong free inodes count in group %d, "
795 "stored = %d, counted = %lu", i,
796 le16_to_cpu(gdp->bg_free_inodes_count), x);
800 if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
801 ext3_error (sb, "ext3_check_inodes_bitmap",
802 "Wrong free inodes count in super block, "
803 "stored = %lu, counted = %lu",
804 (unsigned long)le32_to_cpu(es->s_free_inodes_count),