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 (buffer_uptodate(bh) &&
119 !(desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
123 spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
124 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
125 ext4_init_inode_bitmap(sb, bh, block_group, desc);
126 set_buffer_uptodate(bh);
128 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
131 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
132 if (bh_submit_read(bh) < 0) {
134 ext4_error(sb, __func__,
135 "Cannot read inode bitmap - "
136 "block_group = %lu, inode_bitmap = %llu",
137 block_group, bitmap_blk);
144 * NOTE! When we get the inode, we're the only people
145 * that have access to it, and as such there are no
146 * race conditions we have to worry about. The inode
147 * is not on the hash-lists, and it cannot be reached
148 * through the filesystem because the directory entry
149 * has been deleted earlier.
151 * HOWEVER: we must make sure that we get no aliases,
152 * which means that we have to call "clear_inode()"
153 * _before_ we mark the inode not in use in the inode
154 * bitmaps. Otherwise a newly created file might use
155 * the same inode number (not actually the same pointer
156 * though), and then we'd have two inodes sharing the
157 * same inode number and space on the harddisk.
159 void ext4_free_inode(handle_t *handle, struct inode *inode)
161 struct super_block *sb = inode->i_sb;
164 struct buffer_head *bitmap_bh = NULL;
165 struct buffer_head *bh2;
166 ext4_group_t block_group;
168 struct ext4_group_desc *gdp;
169 struct ext4_super_block *es;
170 struct ext4_sb_info *sbi;
172 ext4_group_t flex_group;
174 if (atomic_read(&inode->i_count) > 1) {
175 printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
176 atomic_read(&inode->i_count));
179 if (inode->i_nlink) {
180 printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
185 printk(KERN_ERR "ext4_free_inode: inode on "
186 "nonexistent device\n");
192 ext4_debug("freeing inode %lu\n", ino);
195 * Note: we must free any quota before locking the superblock,
196 * as writing the quota to disk may need the lock as well.
199 ext4_xattr_delete_inode(handle, inode);
200 DQUOT_FREE_INODE(inode);
203 is_directory = S_ISDIR(inode->i_mode);
205 /* Do this BEFORE marking the inode not in use or returning an error */
208 es = EXT4_SB(sb)->s_es;
209 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
210 ext4_error(sb, "ext4_free_inode",
211 "reserved or nonexistent inode %lu", ino);
214 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
215 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
216 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
220 BUFFER_TRACE(bitmap_bh, "get_write_access");
221 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
225 /* Ok, now we can actually update the inode bitmaps.. */
226 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
227 bit, bitmap_bh->b_data))
228 ext4_error(sb, "ext4_free_inode",
229 "bit already cleared for inode %lu", ino);
231 gdp = ext4_get_group_desc(sb, block_group, &bh2);
233 BUFFER_TRACE(bh2, "get_write_access");
234 fatal = ext4_journal_get_write_access(handle, bh2);
235 if (fatal) goto error_return;
238 spin_lock(sb_bgl_lock(sbi, block_group));
239 le16_add_cpu(&gdp->bg_free_inodes_count, 1);
241 le16_add_cpu(&gdp->bg_used_dirs_count, -1);
242 gdp->bg_checksum = ext4_group_desc_csum(sbi,
244 spin_unlock(sb_bgl_lock(sbi, block_group));
245 percpu_counter_inc(&sbi->s_freeinodes_counter);
247 percpu_counter_dec(&sbi->s_dirs_counter);
249 if (sbi->s_log_groups_per_flex) {
250 flex_group = ext4_flex_group(sbi, block_group);
251 spin_lock(sb_bgl_lock(sbi, flex_group));
252 sbi->s_flex_groups[flex_group].free_inodes++;
253 spin_unlock(sb_bgl_lock(sbi, flex_group));
256 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
257 err = ext4_journal_dirty_metadata(handle, bh2);
258 if (!fatal) fatal = err;
260 BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
261 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
267 ext4_std_error(sb, fatal);
271 * There are two policies for allocating an inode. If the new inode is
272 * a directory, then a forward search is made for a block group with both
273 * free space and a low directory-to-inode ratio; if that fails, then of
274 * the groups with above-average free space, that group with the fewest
275 * directories already is chosen.
277 * For other inodes, search forward from the parent directory\'s block
278 * group to find a free inode.
280 static int find_group_dir(struct super_block *sb, struct inode *parent,
281 ext4_group_t *best_group)
283 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
284 unsigned int freei, avefreei;
285 struct ext4_group_desc *desc, *best_desc = NULL;
289 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
290 avefreei = freei / ngroups;
292 for (group = 0; group < ngroups; group++) {
293 desc = ext4_get_group_desc(sb, group, NULL);
294 if (!desc || !desc->bg_free_inodes_count)
296 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
299 (le16_to_cpu(desc->bg_free_blocks_count) >
300 le16_to_cpu(best_desc->bg_free_blocks_count))) {
309 #define free_block_ratio 10
311 static int find_group_flex(struct super_block *sb, struct inode *parent,
312 ext4_group_t *best_group)
314 struct ext4_sb_info *sbi = EXT4_SB(sb);
315 struct ext4_group_desc *desc;
316 struct buffer_head *bh;
317 struct flex_groups *flex_group = sbi->s_flex_groups;
318 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
319 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
320 ext4_group_t ngroups = sbi->s_groups_count;
321 int flex_size = ext4_flex_bg_size(sbi);
322 ext4_group_t best_flex = parent_fbg_group;
323 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
324 int flexbg_free_blocks;
325 int flex_freeb_ratio;
326 ext4_group_t n_fbg_groups;
329 n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
330 sbi->s_log_groups_per_flex;
332 find_close_to_parent:
333 flexbg_free_blocks = flex_group[best_flex].free_blocks;
334 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
335 if (flex_group[best_flex].free_inodes &&
336 flex_freeb_ratio > free_block_ratio)
339 if (best_flex && best_flex == parent_fbg_group) {
341 goto find_close_to_parent;
344 for (i = 0; i < n_fbg_groups; i++) {
345 if (i == parent_fbg_group || i == parent_fbg_group - 1)
348 flexbg_free_blocks = flex_group[i].free_blocks;
349 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
351 if (flex_freeb_ratio > free_block_ratio &&
352 flex_group[i].free_inodes) {
357 if (flex_group[best_flex].free_inodes == 0 ||
358 (flex_group[i].free_blocks >
359 flex_group[best_flex].free_blocks &&
360 flex_group[i].free_inodes))
364 if (!flex_group[best_flex].free_inodes ||
365 !flex_group[best_flex].free_blocks)
369 for (i = best_flex * flex_size; i < ngroups &&
370 i < (best_flex + 1) * flex_size; i++) {
371 desc = ext4_get_group_desc(sb, i, &bh);
372 if (le16_to_cpu(desc->bg_free_inodes_count)) {
384 * Orlov's allocator for directories.
386 * We always try to spread first-level directories.
388 * If there are blockgroups with both free inodes and free blocks counts
389 * not worse than average we return one with smallest directory count.
390 * Otherwise we simply return a random group.
392 * For the rest rules look so:
394 * It's OK to put directory into a group unless
395 * it has too many directories already (max_dirs) or
396 * it has too few free inodes left (min_inodes) or
397 * it has too few free blocks left (min_blocks) or
398 * it's already running too large debt (max_debt).
399 * Parent's group is preferred, if it doesn't satisfy these
400 * conditions we search cyclically through the rest. If none
401 * of the groups look good we just look for a group with more
402 * free inodes than average (starting at parent's group).
404 * Debt is incremented each time we allocate a directory and decremented
405 * when we allocate an inode, within 0--255.
408 #define INODE_COST 64
409 #define BLOCK_COST 256
411 static int find_group_orlov(struct super_block *sb, struct inode *parent,
414 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
415 struct ext4_sb_info *sbi = EXT4_SB(sb);
416 struct ext4_super_block *es = sbi->s_es;
417 ext4_group_t ngroups = sbi->s_groups_count;
418 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
419 unsigned int freei, avefreei;
420 ext4_fsblk_t freeb, avefreeb;
421 ext4_fsblk_t blocks_per_dir;
423 int max_debt, max_dirs, min_inodes;
424 ext4_grpblk_t min_blocks;
426 struct ext4_group_desc *desc;
428 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
429 avefreei = freei / ngroups;
430 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
432 do_div(avefreeb, ngroups);
433 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
435 if ((parent == sb->s_root->d_inode) ||
436 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
437 int best_ndir = inodes_per_group;
441 get_random_bytes(&grp, sizeof(grp));
442 parent_group = (unsigned)grp % ngroups;
443 for (i = 0; i < ngroups; i++) {
444 grp = (parent_group + i) % ngroups;
445 desc = ext4_get_group_desc(sb, grp, NULL);
446 if (!desc || !desc->bg_free_inodes_count)
448 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
450 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
452 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
456 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
463 blocks_per_dir = ext4_blocks_count(es) - freeb;
464 do_div(blocks_per_dir, ndirs);
466 max_dirs = ndirs / ngroups + inodes_per_group / 16;
467 min_inodes = avefreei - inodes_per_group / 4;
468 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
470 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
471 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
472 if (max_debt * INODE_COST > inodes_per_group)
473 max_debt = inodes_per_group / INODE_COST;
479 for (i = 0; i < ngroups; i++) {
480 *group = (parent_group + i) % ngroups;
481 desc = ext4_get_group_desc(sb, *group, NULL);
482 if (!desc || !desc->bg_free_inodes_count)
484 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
486 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
488 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
494 for (i = 0; i < ngroups; i++) {
495 *group = (parent_group + i) % ngroups;
496 desc = ext4_get_group_desc(sb, *group, NULL);
497 if (desc && desc->bg_free_inodes_count &&
498 le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
504 * The free-inodes counter is approximate, and for really small
505 * filesystems the above test can fail to find any blockgroups
514 static int find_group_other(struct super_block *sb, struct inode *parent,
517 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
518 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
519 struct ext4_group_desc *desc;
523 * Try to place the inode in its parent directory
525 *group = parent_group;
526 desc = ext4_get_group_desc(sb, *group, NULL);
527 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
528 le16_to_cpu(desc->bg_free_blocks_count))
532 * We're going to place this inode in a different blockgroup from its
533 * parent. We want to cause files in a common directory to all land in
534 * the same blockgroup. But we want files which are in a different
535 * directory which shares a blockgroup with our parent to land in a
536 * different blockgroup.
538 * So add our directory's i_ino into the starting point for the hash.
540 *group = (*group + parent->i_ino) % ngroups;
543 * Use a quadratic hash to find a group with a free inode and some free
546 for (i = 1; i < ngroups; i <<= 1) {
548 if (*group >= ngroups)
550 desc = ext4_get_group_desc(sb, *group, NULL);
551 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
552 le16_to_cpu(desc->bg_free_blocks_count))
557 * That failed: try linear search for a free inode, even if that group
558 * has no free blocks.
560 *group = parent_group;
561 for (i = 0; i < ngroups; i++) {
562 if (++*group >= ngroups)
564 desc = ext4_get_group_desc(sb, *group, NULL);
565 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
573 * There are two policies for allocating an inode. If the new inode is
574 * a directory, then a forward search is made for a block group with both
575 * free space and a low directory-to-inode ratio; if that fails, then of
576 * the groups with above-average free space, that group with the fewest
577 * directories already is chosen.
579 * For other inodes, search forward from the parent directory's block
580 * group to find a free inode.
582 struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
584 struct super_block *sb;
585 struct buffer_head *bitmap_bh = NULL;
586 struct buffer_head *bh2;
587 ext4_group_t group = 0;
588 unsigned long ino = 0;
590 struct ext4_group_desc *gdp = NULL;
591 struct ext4_super_block *es;
592 struct ext4_inode_info *ei;
593 struct ext4_sb_info *sbi;
598 ext4_group_t flex_group;
600 /* Cannot create files in a deleted directory */
601 if (!dir || !dir->i_nlink)
602 return ERR_PTR(-EPERM);
605 inode = new_inode(sb);
607 return ERR_PTR(-ENOMEM);
613 if (sbi->s_log_groups_per_flex) {
614 ret2 = find_group_flex(sb, dir, &group);
619 if (test_opt(sb, OLDALLOC))
620 ret2 = find_group_dir(sb, dir, &group);
622 ret2 = find_group_orlov(sb, dir, &group);
624 ret2 = find_group_other(sb, dir, &group);
631 for (i = 0; i < sbi->s_groups_count; i++) {
634 gdp = ext4_get_group_desc(sb, group, &bh2);
639 bitmap_bh = ext4_read_inode_bitmap(sb, group);
645 repeat_in_this_group:
646 ino = ext4_find_next_zero_bit((unsigned long *)
647 bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
648 if (ino < EXT4_INODES_PER_GROUP(sb)) {
650 BUFFER_TRACE(bitmap_bh, "get_write_access");
651 err = ext4_journal_get_write_access(handle, bitmap_bh);
655 if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
656 ino, bitmap_bh->b_data)) {
658 BUFFER_TRACE(bitmap_bh,
659 "call ext4_journal_dirty_metadata");
660 err = ext4_journal_dirty_metadata(handle,
667 jbd2_journal_release_buffer(handle, bitmap_bh);
669 if (++ino < EXT4_INODES_PER_GROUP(sb))
670 goto repeat_in_this_group;
674 * This case is possible in concurrent environment. It is very
675 * rare. We cannot repeat the find_group_xxx() call because
676 * that will simply return the same blockgroup, because the
677 * group descriptor metadata has not yet been updated.
678 * So we just go onto the next blockgroup.
680 if (++group == sbi->s_groups_count)
688 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
689 ino > EXT4_INODES_PER_GROUP(sb)) {
690 ext4_error(sb, __func__,
691 "reserved inode or inode > inodes count - "
692 "block_group = %lu, inode=%lu", group,
693 ino + group * EXT4_INODES_PER_GROUP(sb));
698 BUFFER_TRACE(bh2, "get_write_access");
699 err = ext4_journal_get_write_access(handle, bh2);
702 /* We may have to initialize the block bitmap if it isn't already */
703 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
704 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
705 struct buffer_head *block_bh = ext4_read_block_bitmap(sb, group);
707 BUFFER_TRACE(block_bh, "get block bitmap access");
708 err = ext4_journal_get_write_access(handle, block_bh);
715 spin_lock(sb_bgl_lock(sbi, group));
716 /* recheck and clear flag under lock if we still need to */
717 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
718 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
719 free = ext4_free_blocks_after_init(sb, group, gdp);
720 gdp->bg_free_blocks_count = cpu_to_le16(free);
721 gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
724 spin_unlock(sb_bgl_lock(sbi, group));
726 /* Don't need to dirty bitmap block if we didn't change it */
728 BUFFER_TRACE(block_bh, "dirty block bitmap");
729 err = ext4_journal_dirty_metadata(handle, block_bh);
737 spin_lock(sb_bgl_lock(sbi, group));
738 /* If we didn't allocate from within the initialized part of the inode
739 * table then we need to initialize up to this inode. */
740 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
741 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
742 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
744 /* When marking the block group with
745 * ~EXT4_BG_INODE_UNINIT we don't want to depend
746 * on the value of bg_itable_unused even though
747 * mke2fs could have initialized the same for us.
748 * Instead we calculated the value below
753 free = EXT4_INODES_PER_GROUP(sb) -
754 le16_to_cpu(gdp->bg_itable_unused);
758 * Check the relative inode number against the last used
759 * relative inode number in this group. if it is greater
760 * we need to update the bg_itable_unused count
764 gdp->bg_itable_unused =
765 cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
768 le16_add_cpu(&gdp->bg_free_inodes_count, -1);
770 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
772 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
773 spin_unlock(sb_bgl_lock(sbi, group));
774 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
775 err = ext4_journal_dirty_metadata(handle, bh2);
778 percpu_counter_dec(&sbi->s_freeinodes_counter);
780 percpu_counter_inc(&sbi->s_dirs_counter);
783 if (sbi->s_log_groups_per_flex) {
784 flex_group = ext4_flex_group(sbi, group);
785 spin_lock(sb_bgl_lock(sbi, flex_group));
786 sbi->s_flex_groups[flex_group].free_inodes--;
787 spin_unlock(sb_bgl_lock(sbi, flex_group));
790 inode->i_uid = current_fsuid();
791 if (test_opt(sb, GRPID))
792 inode->i_gid = dir->i_gid;
793 else if (dir->i_mode & S_ISGID) {
794 inode->i_gid = dir->i_gid;
798 inode->i_gid = current_fsgid();
799 inode->i_mode = mode;
801 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
802 /* This is the optimal IO size (for stat), not the fs block size */
804 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
805 ext4_current_time(inode);
807 memset(ei->i_data, 0, sizeof(ei->i_data));
808 ei->i_dir_start_lookup = 0;
812 * Don't inherit extent flag from directory. We set extent flag on
813 * newly created directory and file only if -o extent mount option is
816 ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
818 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
819 /* dirsync only applies to directories */
821 ei->i_flags &= ~EXT4_DIRSYNC_FL;
824 ei->i_block_group = group;
826 ext4_set_inode_flags(inode);
827 if (IS_DIRSYNC(inode))
829 if (insert_inode_locked(inode) < 0) {
833 spin_lock(&sbi->s_next_gen_lock);
834 inode->i_generation = sbi->s_next_generation++;
835 spin_unlock(&sbi->s_next_gen_lock);
837 ei->i_state = EXT4_STATE_NEW;
839 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
842 if (DQUOT_ALLOC_INODE(inode)) {
847 err = ext4_init_acl(handle, inode, dir);
851 err = ext4_init_security(handle, inode, dir);
855 if (test_opt(sb, EXTENTS)) {
856 /* set extent flag only for directory, file and normal symlink*/
857 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
858 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
859 ext4_ext_tree_init(handle, inode);
863 err = ext4_mark_inode_dirty(handle, inode);
865 ext4_std_error(sb, err);
869 ext4_debug("allocating inode %lu\n", inode->i_ino);
872 ext4_std_error(sb, err);
881 DQUOT_FREE_INODE(inode);
885 inode->i_flags |= S_NOQUOTA;
887 unlock_new_inode(inode);
893 /* Verify that we are loading a valid orphan from disk */
894 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
896 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
897 ext4_group_t block_group;
899 struct buffer_head *bitmap_bh;
900 struct inode *inode = NULL;
903 /* Error cases - e2fsck has already cleaned up for us */
905 ext4_warning(sb, __func__,
906 "bad orphan ino %lu! e2fsck was run?", ino);
910 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
911 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
912 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
914 ext4_warning(sb, __func__,
915 "inode bitmap error for orphan %lu", ino);
919 /* Having the inode bit set should be a 100% indicator that this
920 * is a valid orphan (no e2fsck run on fs). Orphans also include
921 * inodes that were being truncated, so we can't check i_nlink==0.
923 if (!ext4_test_bit(bit, bitmap_bh->b_data))
926 inode = ext4_iget(sb, ino);
931 * If the orphans has i_nlinks > 0 then it should be able to be
932 * truncated, otherwise it won't be removed from the orphan list
933 * during processing and an infinite loop will result.
935 if (inode->i_nlink && !ext4_can_truncate(inode))
938 if (NEXT_ORPHAN(inode) > max_ino)
944 err = PTR_ERR(inode);
947 ext4_warning(sb, __func__,
948 "bad orphan inode %lu! e2fsck was run?", ino);
949 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
950 bit, (unsigned long long)bitmap_bh->b_blocknr,
951 ext4_test_bit(bit, bitmap_bh->b_data));
952 printk(KERN_NOTICE "inode=%p\n", inode);
954 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
955 is_bad_inode(inode));
956 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
958 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
959 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
960 /* Avoid freeing blocks if we got a bad deleted inode */
961 if (inode->i_nlink == 0)
970 unsigned long ext4_count_free_inodes(struct super_block *sb)
972 unsigned long desc_count;
973 struct ext4_group_desc *gdp;
976 struct ext4_super_block *es;
977 unsigned long bitmap_count, x;
978 struct buffer_head *bitmap_bh = NULL;
980 es = EXT4_SB(sb)->s_es;
984 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
985 gdp = ext4_get_group_desc(sb, i, NULL);
988 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
990 bitmap_bh = ext4_read_inode_bitmap(sb, i);
994 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
995 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
996 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
1000 printk(KERN_DEBUG "ext4_count_free_inodes: "
1001 "stored = %u, computed = %lu, %lu\n",
1002 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1006 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1007 gdp = ext4_get_group_desc(sb, i, NULL);
1010 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
1017 /* Called at mount-time, super-block is locked */
1018 unsigned long ext4_count_dirs(struct super_block * sb)
1020 unsigned long count = 0;
1023 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1024 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1027 count += le16_to_cpu(gdp->bg_used_dirs_count);