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/stat.h>
19 #include <linux/string.h>
20 #include <linux/quotaops.h>
21 #include <linux/buffer_head.h>
22 #include <linux/random.h>
23 #include <linux/bitops.h>
24 #include <linux/blkdev.h>
25 #include <asm/byteorder.h>
27 #include "ext4_jbd2.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.
47 * To avoid calling the atomic setbit hundreds or thousands of times, we only
48 * need to use it within a single byte (to ensure we get endianness right).
49 * We can use memset for the rest of the bitmap as there are no other users.
51 void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
55 if (start_bit >= end_bit)
58 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
59 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
60 ext4_set_bit(i, bitmap);
62 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
65 /* Initializes an uninitialized inode bitmap */
66 unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
67 ext4_group_t block_group,
68 struct ext4_group_desc *gdp)
70 struct ext4_sb_info *sbi = EXT4_SB(sb);
72 J_ASSERT_BH(bh, buffer_locked(bh));
74 /* If checksum is bad mark all blocks and inodes use to prevent
75 * allocation, essentially implementing a per-group read-only flag. */
76 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
77 ext4_error(sb, __func__, "Checksum bad for group %lu\n",
79 gdp->bg_free_blocks_count = 0;
80 gdp->bg_free_inodes_count = 0;
81 gdp->bg_itable_unused = 0;
82 memset(bh->b_data, 0xff, sb->s_blocksize);
86 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
87 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
90 return EXT4_INODES_PER_GROUP(sb);
94 * Read the inode allocation bitmap for a given block_group, reading
95 * into the specified slot in the superblock's bitmap cache.
97 * Return buffer_head of bitmap on success or NULL.
99 static struct buffer_head *
100 ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
102 struct ext4_group_desc *desc;
103 struct buffer_head *bh = NULL;
104 ext4_fsblk_t bitmap_blk;
106 desc = ext4_get_group_desc(sb, block_group, NULL);
109 bitmap_blk = ext4_inode_bitmap(sb, desc);
110 bh = sb_getblk(sb, bitmap_blk);
112 ext4_error(sb, __func__,
113 "Cannot read inode bitmap - "
114 "block_group = %lu, inode_bitmap = %llu",
115 block_group, bitmap_blk);
118 if (bh_uptodate_or_lock(bh))
121 spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
122 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
123 ext4_init_inode_bitmap(sb, bh, block_group, desc);
124 set_buffer_uptodate(bh);
126 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
129 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
130 if (bh_submit_read(bh) < 0) {
132 ext4_error(sb, __func__,
133 "Cannot read inode bitmap - "
134 "block_group = %lu, inode_bitmap = %llu",
135 block_group, bitmap_blk);
142 * NOTE! When we get the inode, we're the only people
143 * that have access to it, and as such there are no
144 * race conditions we have to worry about. The inode
145 * is not on the hash-lists, and it cannot be reached
146 * through the filesystem because the directory entry
147 * has been deleted earlier.
149 * HOWEVER: we must make sure that we get no aliases,
150 * which means that we have to call "clear_inode()"
151 * _before_ we mark the inode not in use in the inode
152 * bitmaps. Otherwise a newly created file might use
153 * the same inode number (not actually the same pointer
154 * though), and then we'd have two inodes sharing the
155 * same inode number and space on the harddisk.
157 void ext4_free_inode (handle_t *handle, struct inode * inode)
159 struct super_block * sb = inode->i_sb;
162 struct buffer_head *bitmap_bh = NULL;
163 struct buffer_head *bh2;
164 ext4_group_t block_group;
166 struct ext4_group_desc * gdp;
167 struct ext4_super_block * es;
168 struct ext4_sb_info *sbi;
170 ext4_group_t flex_group;
172 if (atomic_read(&inode->i_count) > 1) {
173 printk ("ext4_free_inode: inode has count=%d\n",
174 atomic_read(&inode->i_count));
177 if (inode->i_nlink) {
178 printk ("ext4_free_inode: inode has nlink=%d\n",
183 printk("ext4_free_inode: inode on nonexistent device\n");
189 ext4_debug ("freeing inode %lu\n", ino);
192 * Note: we must free any quota before locking the superblock,
193 * as writing the quota to disk may need the lock as well.
196 ext4_xattr_delete_inode(handle, inode);
197 DQUOT_FREE_INODE(inode);
200 is_directory = S_ISDIR(inode->i_mode);
202 /* Do this BEFORE marking the inode not in use or returning an error */
205 es = EXT4_SB(sb)->s_es;
206 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
207 ext4_error (sb, "ext4_free_inode",
208 "reserved or nonexistent inode %lu", ino);
211 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
212 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
213 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
217 BUFFER_TRACE(bitmap_bh, "get_write_access");
218 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
222 /* Ok, now we can actually update the inode bitmaps.. */
223 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
224 bit, bitmap_bh->b_data))
225 ext4_error (sb, "ext4_free_inode",
226 "bit already cleared for inode %lu", ino);
228 gdp = ext4_get_group_desc (sb, block_group, &bh2);
230 BUFFER_TRACE(bh2, "get_write_access");
231 fatal = ext4_journal_get_write_access(handle, bh2);
232 if (fatal) goto error_return;
235 spin_lock(sb_bgl_lock(sbi, block_group));
236 le16_add_cpu(&gdp->bg_free_inodes_count, 1);
238 le16_add_cpu(&gdp->bg_used_dirs_count, -1);
239 gdp->bg_checksum = ext4_group_desc_csum(sbi,
241 spin_unlock(sb_bgl_lock(sbi, block_group));
242 percpu_counter_inc(&sbi->s_freeinodes_counter);
244 percpu_counter_dec(&sbi->s_dirs_counter);
246 if (sbi->s_log_groups_per_flex) {
247 flex_group = ext4_flex_group(sbi, block_group);
248 spin_lock(sb_bgl_lock(sbi, flex_group));
249 sbi->s_flex_groups[flex_group].free_inodes++;
250 spin_unlock(sb_bgl_lock(sbi, flex_group));
253 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
254 err = ext4_journal_dirty_metadata(handle, bh2);
255 if (!fatal) fatal = err;
257 BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
258 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
264 ext4_std_error(sb, fatal);
268 * There are two policies for allocating an inode. If the new inode is
269 * a directory, then a forward search is made for a block group with both
270 * free space and a low directory-to-inode ratio; if that fails, then of
271 * the groups with above-average free space, that group with the fewest
272 * directories already is chosen.
274 * For other inodes, search forward from the parent directory\'s block
275 * group to find a free inode.
277 static int find_group_dir(struct super_block *sb, struct inode *parent,
278 ext4_group_t *best_group)
280 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
281 unsigned int freei, avefreei;
282 struct ext4_group_desc *desc, *best_desc = NULL;
286 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
287 avefreei = freei / ngroups;
289 for (group = 0; group < ngroups; group++) {
290 desc = ext4_get_group_desc (sb, group, NULL);
291 if (!desc || !desc->bg_free_inodes_count)
293 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
296 (le16_to_cpu(desc->bg_free_blocks_count) >
297 le16_to_cpu(best_desc->bg_free_blocks_count))) {
306 #define free_block_ratio 10
308 static int find_group_flex(struct super_block *sb, struct inode *parent,
309 ext4_group_t *best_group)
311 struct ext4_sb_info *sbi = EXT4_SB(sb);
312 struct ext4_group_desc *desc;
313 struct buffer_head *bh;
314 struct flex_groups *flex_group = sbi->s_flex_groups;
315 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
316 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
317 ext4_group_t ngroups = sbi->s_groups_count;
318 int flex_size = ext4_flex_bg_size(sbi);
319 ext4_group_t best_flex = parent_fbg_group;
320 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
321 int flexbg_free_blocks;
322 int flex_freeb_ratio;
323 ext4_group_t n_fbg_groups;
326 n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
327 sbi->s_log_groups_per_flex;
329 find_close_to_parent:
330 flexbg_free_blocks = flex_group[best_flex].free_blocks;
331 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
332 if (flex_group[best_flex].free_inodes &&
333 flex_freeb_ratio > free_block_ratio)
336 if (best_flex && best_flex == parent_fbg_group) {
338 goto find_close_to_parent;
341 for (i = 0; i < n_fbg_groups; i++) {
342 if (i == parent_fbg_group || i == parent_fbg_group - 1)
345 flexbg_free_blocks = flex_group[i].free_blocks;
346 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
348 if (flex_freeb_ratio > free_block_ratio &&
349 flex_group[i].free_inodes) {
354 if (flex_group[best_flex].free_inodes == 0 ||
355 (flex_group[i].free_blocks >
356 flex_group[best_flex].free_blocks &&
357 flex_group[i].free_inodes))
361 if (!flex_group[best_flex].free_inodes ||
362 !flex_group[best_flex].free_blocks)
366 for (i = best_flex * flex_size; i < ngroups &&
367 i < (best_flex + 1) * flex_size; i++) {
368 desc = ext4_get_group_desc(sb, i, &bh);
369 if (le16_to_cpu(desc->bg_free_inodes_count)) {
381 * Orlov's allocator for directories.
383 * We always try to spread first-level directories.
385 * If there are blockgroups with both free inodes and free blocks counts
386 * not worse than average we return one with smallest directory count.
387 * Otherwise we simply return a random group.
389 * For the rest rules look so:
391 * It's OK to put directory into a group unless
392 * it has too many directories already (max_dirs) or
393 * it has too few free inodes left (min_inodes) or
394 * it has too few free blocks left (min_blocks) or
395 * it's already running too large debt (max_debt).
396 * Parent's group is preferred, if it doesn't satisfy these
397 * conditions we search cyclically through the rest. If none
398 * of the groups look good we just look for a group with more
399 * free inodes than average (starting at parent's group).
401 * Debt is incremented each time we allocate a directory and decremented
402 * when we allocate an inode, within 0--255.
405 #define INODE_COST 64
406 #define BLOCK_COST 256
408 static int find_group_orlov(struct super_block *sb, struct inode *parent,
411 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
412 struct ext4_sb_info *sbi = EXT4_SB(sb);
413 struct ext4_super_block *es = sbi->s_es;
414 ext4_group_t ngroups = sbi->s_groups_count;
415 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
416 unsigned int freei, avefreei;
417 ext4_fsblk_t freeb, avefreeb;
418 ext4_fsblk_t blocks_per_dir;
420 int max_debt, max_dirs, min_inodes;
421 ext4_grpblk_t min_blocks;
423 struct ext4_group_desc *desc;
425 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
426 avefreei = freei / ngroups;
427 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
429 do_div(avefreeb, ngroups);
430 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
432 if ((parent == sb->s_root->d_inode) ||
433 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
434 int best_ndir = inodes_per_group;
438 get_random_bytes(&grp, sizeof(grp));
439 parent_group = (unsigned)grp % ngroups;
440 for (i = 0; i < ngroups; i++) {
441 grp = (parent_group + i) % ngroups;
442 desc = ext4_get_group_desc(sb, grp, NULL);
443 if (!desc || !desc->bg_free_inodes_count)
445 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
447 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
449 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
453 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
460 blocks_per_dir = ext4_blocks_count(es) - freeb;
461 do_div(blocks_per_dir, ndirs);
463 max_dirs = ndirs / ngroups + inodes_per_group / 16;
464 min_inodes = avefreei - inodes_per_group / 4;
465 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
467 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
468 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
469 if (max_debt * INODE_COST > inodes_per_group)
470 max_debt = inodes_per_group / INODE_COST;
476 for (i = 0; i < ngroups; i++) {
477 *group = (parent_group + i) % ngroups;
478 desc = ext4_get_group_desc(sb, *group, NULL);
479 if (!desc || !desc->bg_free_inodes_count)
481 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
483 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
485 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
491 for (i = 0; i < ngroups; i++) {
492 *group = (parent_group + i) % ngroups;
493 desc = ext4_get_group_desc(sb, *group, NULL);
494 if (desc && desc->bg_free_inodes_count &&
495 le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
501 * The free-inodes counter is approximate, and for really small
502 * filesystems the above test can fail to find any blockgroups
511 static int find_group_other(struct super_block *sb, struct inode *parent,
514 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
515 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
516 struct ext4_group_desc *desc;
520 * Try to place the inode in its parent directory
522 *group = parent_group;
523 desc = ext4_get_group_desc(sb, *group, NULL);
524 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
525 le16_to_cpu(desc->bg_free_blocks_count))
529 * We're going to place this inode in a different blockgroup from its
530 * parent. We want to cause files in a common directory to all land in
531 * the same blockgroup. But we want files which are in a different
532 * directory which shares a blockgroup with our parent to land in a
533 * different blockgroup.
535 * So add our directory's i_ino into the starting point for the hash.
537 *group = (*group + parent->i_ino) % ngroups;
540 * Use a quadratic hash to find a group with a free inode and some free
543 for (i = 1; i < ngroups; i <<= 1) {
545 if (*group >= ngroups)
547 desc = ext4_get_group_desc(sb, *group, NULL);
548 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
549 le16_to_cpu(desc->bg_free_blocks_count))
554 * That failed: try linear search for a free inode, even if that group
555 * has no free blocks.
557 *group = parent_group;
558 for (i = 0; i < ngroups; i++) {
559 if (++*group >= ngroups)
561 desc = ext4_get_group_desc(sb, *group, NULL);
562 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
570 * There are two policies for allocating an inode. If the new inode is
571 * a directory, then a forward search is made for a block group with both
572 * free space and a low directory-to-inode ratio; if that fails, then of
573 * the groups with above-average free space, that group with the fewest
574 * directories already is chosen.
576 * For other inodes, search forward from the parent directory's block
577 * group to find a free inode.
579 struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
581 struct super_block *sb;
582 struct buffer_head *bitmap_bh = NULL;
583 struct buffer_head *bh2;
584 ext4_group_t group = 0;
585 unsigned long ino = 0;
586 struct inode * inode;
587 struct ext4_group_desc * gdp = NULL;
588 struct ext4_super_block * es;
589 struct ext4_inode_info *ei;
590 struct ext4_sb_info *sbi;
595 ext4_group_t flex_group;
597 /* Cannot create files in a deleted directory */
598 if (!dir || !dir->i_nlink)
599 return ERR_PTR(-EPERM);
602 inode = new_inode(sb);
604 return ERR_PTR(-ENOMEM);
610 if (sbi->s_log_groups_per_flex) {
611 ret2 = find_group_flex(sb, dir, &group);
616 if (test_opt (sb, OLDALLOC))
617 ret2 = find_group_dir(sb, dir, &group);
619 ret2 = find_group_orlov(sb, dir, &group);
621 ret2 = find_group_other(sb, dir, &group);
628 for (i = 0; i < sbi->s_groups_count; i++) {
631 gdp = ext4_get_group_desc(sb, group, &bh2);
636 bitmap_bh = ext4_read_inode_bitmap(sb, group);
642 repeat_in_this_group:
643 ino = ext4_find_next_zero_bit((unsigned long *)
644 bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
645 if (ino < EXT4_INODES_PER_GROUP(sb)) {
647 BUFFER_TRACE(bitmap_bh, "get_write_access");
648 err = ext4_journal_get_write_access(handle, bitmap_bh);
652 if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
653 ino, bitmap_bh->b_data)) {
655 BUFFER_TRACE(bitmap_bh,
656 "call ext4_journal_dirty_metadata");
657 err = ext4_journal_dirty_metadata(handle,
664 jbd2_journal_release_buffer(handle, bitmap_bh);
666 if (++ino < EXT4_INODES_PER_GROUP(sb))
667 goto repeat_in_this_group;
671 * This case is possible in concurrent environment. It is very
672 * rare. We cannot repeat the find_group_xxx() call because
673 * that will simply return the same blockgroup, because the
674 * group descriptor metadata has not yet been updated.
675 * So we just go onto the next blockgroup.
677 if (++group == sbi->s_groups_count)
685 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
686 ino > EXT4_INODES_PER_GROUP(sb)) {
687 ext4_error(sb, __func__,
688 "reserved inode or inode > inodes count - "
689 "block_group = %lu, inode=%lu", group,
690 ino + group * EXT4_INODES_PER_GROUP(sb));
695 BUFFER_TRACE(bh2, "get_write_access");
696 err = ext4_journal_get_write_access(handle, bh2);
699 /* We may have to initialize the block bitmap if it isn't already */
700 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
701 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
702 struct buffer_head *block_bh = ext4_read_block_bitmap(sb, group);
704 BUFFER_TRACE(block_bh, "get block bitmap access");
705 err = ext4_journal_get_write_access(handle, block_bh);
712 spin_lock(sb_bgl_lock(sbi, group));
713 /* recheck and clear flag under lock if we still need to */
714 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
715 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
716 free = ext4_free_blocks_after_init(sb, group, gdp);
717 gdp->bg_free_blocks_count = cpu_to_le16(free);
719 spin_unlock(sb_bgl_lock(sbi, group));
721 /* Don't need to dirty bitmap block if we didn't change it */
723 BUFFER_TRACE(block_bh, "dirty block bitmap");
724 err = ext4_journal_dirty_metadata(handle, block_bh);
732 spin_lock(sb_bgl_lock(sbi, group));
733 /* If we didn't allocate from within the initialized part of the inode
734 * table then we need to initialize up to this inode. */
735 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
736 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
737 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
739 /* When marking the block group with
740 * ~EXT4_BG_INODE_UNINIT we don't want to depend
741 * on the value of bg_itable_unused even though
742 * mke2fs could have initialized the same for us.
743 * Instead we calculated the value below
748 free = EXT4_INODES_PER_GROUP(sb) -
749 le16_to_cpu(gdp->bg_itable_unused);
753 * Check the relative inode number against the last used
754 * relative inode number in this group. if it is greater
755 * we need to update the bg_itable_unused count
759 gdp->bg_itable_unused =
760 cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
763 le16_add_cpu(&gdp->bg_free_inodes_count, -1);
765 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
767 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
768 spin_unlock(sb_bgl_lock(sbi, group));
769 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
770 err = ext4_journal_dirty_metadata(handle, bh2);
773 percpu_counter_dec(&sbi->s_freeinodes_counter);
775 percpu_counter_inc(&sbi->s_dirs_counter);
778 if (sbi->s_log_groups_per_flex) {
779 flex_group = ext4_flex_group(sbi, group);
780 spin_lock(sb_bgl_lock(sbi, flex_group));
781 sbi->s_flex_groups[flex_group].free_inodes--;
782 spin_unlock(sb_bgl_lock(sbi, flex_group));
785 inode->i_uid = current->fsuid;
786 if (test_opt (sb, GRPID))
787 inode->i_gid = dir->i_gid;
788 else if (dir->i_mode & S_ISGID) {
789 inode->i_gid = dir->i_gid;
793 inode->i_gid = current->fsgid;
794 inode->i_mode = mode;
796 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
797 /* This is the optimal IO size (for stat), not the fs block size */
799 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
800 ext4_current_time(inode);
802 memset(ei->i_data, 0, sizeof(ei->i_data));
803 ei->i_dir_start_lookup = 0;
807 * Don't inherit extent flag from directory. We set extent flag on
808 * newly created directory and file only if -o extent mount option is
811 ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
813 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
814 /* dirsync only applies to directories */
816 ei->i_flags &= ~EXT4_DIRSYNC_FL;
819 ei->i_block_alloc_info = NULL;
820 ei->i_block_group = group;
822 ext4_set_inode_flags(inode);
823 if (IS_DIRSYNC(inode))
825 insert_inode_hash(inode);
826 spin_lock(&sbi->s_next_gen_lock);
827 inode->i_generation = sbi->s_next_generation++;
828 spin_unlock(&sbi->s_next_gen_lock);
830 ei->i_state = EXT4_STATE_NEW;
832 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
835 if(DQUOT_ALLOC_INODE(inode)) {
840 err = ext4_init_acl(handle, inode, dir);
844 err = ext4_init_security(handle,inode, dir);
848 if (test_opt(sb, EXTENTS)) {
849 /* set extent flag only for directory, file and normal symlink*/
850 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
851 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
852 ext4_ext_tree_init(handle, inode);
856 err = ext4_mark_inode_dirty(handle, inode);
858 ext4_std_error(sb, err);
862 ext4_debug("allocating inode %lu\n", inode->i_ino);
865 ext4_std_error(sb, err);
874 DQUOT_FREE_INODE(inode);
878 inode->i_flags |= S_NOQUOTA;
885 /* Verify that we are loading a valid orphan from disk */
886 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
888 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
889 ext4_group_t block_group;
891 struct buffer_head *bitmap_bh;
892 struct inode *inode = NULL;
895 /* Error cases - e2fsck has already cleaned up for us */
897 ext4_warning(sb, __func__,
898 "bad orphan ino %lu! e2fsck was run?", ino);
902 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
903 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
904 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
906 ext4_warning(sb, __func__,
907 "inode bitmap error for orphan %lu", ino);
911 /* Having the inode bit set should be a 100% indicator that this
912 * is a valid orphan (no e2fsck run on fs). Orphans also include
913 * inodes that were being truncated, so we can't check i_nlink==0.
915 if (!ext4_test_bit(bit, bitmap_bh->b_data))
918 inode = ext4_iget(sb, ino);
923 * If the orphans has i_nlinks > 0 then it should be able to be
924 * truncated, otherwise it won't be removed from the orphan list
925 * during processing and an infinite loop will result.
927 if (inode->i_nlink && !ext4_can_truncate(inode))
930 if (NEXT_ORPHAN(inode) > max_ino)
936 err = PTR_ERR(inode);
939 ext4_warning(sb, __func__,
940 "bad orphan inode %lu! e2fsck was run?", ino);
941 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
942 bit, (unsigned long long)bitmap_bh->b_blocknr,
943 ext4_test_bit(bit, bitmap_bh->b_data));
944 printk(KERN_NOTICE "inode=%p\n", inode);
946 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
947 is_bad_inode(inode));
948 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
950 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
951 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
952 /* Avoid freeing blocks if we got a bad deleted inode */
953 if (inode->i_nlink == 0)
962 unsigned long ext4_count_free_inodes (struct super_block * sb)
964 unsigned long desc_count;
965 struct ext4_group_desc *gdp;
968 struct ext4_super_block *es;
969 unsigned long bitmap_count, x;
970 struct buffer_head *bitmap_bh = NULL;
972 es = EXT4_SB(sb)->s_es;
976 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
977 gdp = ext4_get_group_desc (sb, i, NULL);
980 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
982 bitmap_bh = ext4_read_inode_bitmap(sb, i);
986 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
987 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
988 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
992 printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
993 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
997 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
998 gdp = ext4_get_group_desc (sb, i, NULL);
1001 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
1008 /* Called at mount-time, super-block is locked */
1009 unsigned long ext4_count_dirs (struct super_block * sb)
1011 unsigned long count = 0;
1014 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1015 struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
1018 count += le16_to_cpu(gdp->bg_used_dirs_count);