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 %u",
79 ext4_free_blks_set(sb, gdp, 0);
80 ext4_free_inodes_set(sb, gdp, 0);
81 ext4_itable_unused_set(sb, gdp, 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 = %u, inode_bitmap = %llu",
115 block_group, bitmap_blk);
118 if (bitmap_uptodate(bh))
122 if (bitmap_uptodate(bh)) {
126 spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
127 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
128 ext4_init_inode_bitmap(sb, bh, block_group, desc);
129 set_bitmap_uptodate(bh);
130 set_buffer_uptodate(bh);
131 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
135 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
136 if (buffer_uptodate(bh)) {
138 * if not uninit if bh is uptodate,
139 * bitmap is also uptodate
141 set_bitmap_uptodate(bh);
146 * submit the buffer_head for read. We can
147 * safely mark the bitmap as uptodate now.
148 * We do it here so the bitmap uptodate bit
149 * get set with buffer lock held.
151 set_bitmap_uptodate(bh);
152 if (bh_submit_read(bh) < 0) {
154 ext4_error(sb, __func__,
155 "Cannot read inode bitmap - "
156 "block_group = %u, inode_bitmap = %llu",
157 block_group, bitmap_blk);
164 * NOTE! When we get the inode, we're the only people
165 * that have access to it, and as such there are no
166 * race conditions we have to worry about. The inode
167 * is not on the hash-lists, and it cannot be reached
168 * through the filesystem because the directory entry
169 * has been deleted earlier.
171 * HOWEVER: we must make sure that we get no aliases,
172 * which means that we have to call "clear_inode()"
173 * _before_ we mark the inode not in use in the inode
174 * bitmaps. Otherwise a newly created file might use
175 * the same inode number (not actually the same pointer
176 * though), and then we'd have two inodes sharing the
177 * same inode number and space on the harddisk.
179 void ext4_free_inode(handle_t *handle, struct inode *inode)
181 struct super_block *sb = inode->i_sb;
184 struct buffer_head *bitmap_bh = NULL;
185 struct buffer_head *bh2;
186 ext4_group_t block_group;
188 struct ext4_group_desc *gdp;
189 struct ext4_super_block *es;
190 struct ext4_sb_info *sbi;
191 int fatal = 0, err, count;
192 ext4_group_t flex_group;
194 if (atomic_read(&inode->i_count) > 1) {
195 printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
196 atomic_read(&inode->i_count));
199 if (inode->i_nlink) {
200 printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
205 printk(KERN_ERR "ext4_free_inode: inode on "
206 "nonexistent device\n");
212 ext4_debug("freeing inode %lu\n", ino);
215 * Note: we must free any quota before locking the superblock,
216 * as writing the quota to disk may need the lock as well.
219 ext4_xattr_delete_inode(handle, inode);
220 DQUOT_FREE_INODE(inode);
223 is_directory = S_ISDIR(inode->i_mode);
225 /* Do this BEFORE marking the inode not in use or returning an error */
228 es = EXT4_SB(sb)->s_es;
229 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
230 ext4_error(sb, "ext4_free_inode",
231 "reserved or nonexistent inode %lu", ino);
234 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
235 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
236 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
240 BUFFER_TRACE(bitmap_bh, "get_write_access");
241 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
245 /* Ok, now we can actually update the inode bitmaps.. */
246 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
247 bit, bitmap_bh->b_data))
248 ext4_error(sb, "ext4_free_inode",
249 "bit already cleared for inode %lu", ino);
251 gdp = ext4_get_group_desc(sb, block_group, &bh2);
253 BUFFER_TRACE(bh2, "get_write_access");
254 fatal = ext4_journal_get_write_access(handle, bh2);
255 if (fatal) goto error_return;
258 spin_lock(sb_bgl_lock(sbi, block_group));
259 count = ext4_free_inodes_count(sb, gdp) + 1;
260 ext4_free_inodes_set(sb, gdp, count);
262 count = ext4_used_dirs_count(sb, gdp) - 1;
263 ext4_used_dirs_set(sb, gdp, count);
265 gdp->bg_checksum = ext4_group_desc_csum(sbi,
267 spin_unlock(sb_bgl_lock(sbi, block_group));
268 percpu_counter_inc(&sbi->s_freeinodes_counter);
270 percpu_counter_dec(&sbi->s_dirs_counter);
272 if (sbi->s_log_groups_per_flex) {
273 flex_group = ext4_flex_group(sbi, block_group);
274 spin_lock(sb_bgl_lock(sbi, flex_group));
275 sbi->s_flex_groups[flex_group].free_inodes++;
276 spin_unlock(sb_bgl_lock(sbi, flex_group));
279 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
280 err = ext4_handle_dirty_metadata(handle, NULL, bh2);
281 if (!fatal) fatal = err;
283 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
284 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
290 ext4_std_error(sb, fatal);
294 * There are two policies for allocating an inode. If the new inode is
295 * a directory, then a forward search is made for a block group with both
296 * free space and a low directory-to-inode ratio; if that fails, then of
297 * the groups with above-average free space, that group with the fewest
298 * directories already is chosen.
300 * For other inodes, search forward from the parent directory\'s block
301 * group to find a free inode.
303 static int find_group_dir(struct super_block *sb, struct inode *parent,
304 ext4_group_t *best_group)
306 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
307 unsigned int freei, avefreei;
308 struct ext4_group_desc *desc, *best_desc = NULL;
312 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
313 avefreei = freei / ngroups;
315 for (group = 0; group < ngroups; group++) {
316 desc = ext4_get_group_desc(sb, group, NULL);
317 if (!desc || !ext4_free_inodes_count(sb, desc))
319 if (ext4_free_inodes_count(sb, desc) < avefreei)
322 (ext4_free_blks_count(sb, desc) >
323 ext4_free_blks_count(sb, best_desc))) {
332 #define free_block_ratio 10
334 static int find_group_flex(struct super_block *sb, struct inode *parent,
335 ext4_group_t *best_group)
337 struct ext4_sb_info *sbi = EXT4_SB(sb);
338 struct ext4_group_desc *desc;
339 struct buffer_head *bh;
340 struct flex_groups *flex_group = sbi->s_flex_groups;
341 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
342 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
343 ext4_group_t ngroups = sbi->s_groups_count;
344 int flex_size = ext4_flex_bg_size(sbi);
345 ext4_group_t best_flex = parent_fbg_group;
346 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
347 int flexbg_free_blocks;
348 int flex_freeb_ratio;
349 ext4_group_t n_fbg_groups;
352 n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
353 sbi->s_log_groups_per_flex;
355 find_close_to_parent:
356 flexbg_free_blocks = flex_group[best_flex].free_blocks;
357 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
358 if (flex_group[best_flex].free_inodes &&
359 flex_freeb_ratio > free_block_ratio)
362 if (best_flex && best_flex == parent_fbg_group) {
364 goto find_close_to_parent;
367 for (i = 0; i < n_fbg_groups; i++) {
368 if (i == parent_fbg_group || i == parent_fbg_group - 1)
371 flexbg_free_blocks = flex_group[i].free_blocks;
372 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
374 if (flex_freeb_ratio > free_block_ratio &&
375 flex_group[i].free_inodes) {
380 if (flex_group[best_flex].free_inodes == 0 ||
381 (flex_group[i].free_blocks >
382 flex_group[best_flex].free_blocks &&
383 flex_group[i].free_inodes))
387 if (!flex_group[best_flex].free_inodes ||
388 !flex_group[best_flex].free_blocks)
392 for (i = best_flex * flex_size; i < ngroups &&
393 i < (best_flex + 1) * flex_size; i++) {
394 desc = ext4_get_group_desc(sb, i, &bh);
395 if (ext4_free_inodes_count(sb, desc)) {
407 * Orlov's allocator for directories.
409 * We always try to spread first-level directories.
411 * If there are blockgroups with both free inodes and free blocks counts
412 * not worse than average we return one with smallest directory count.
413 * Otherwise we simply return a random group.
415 * For the rest rules look so:
417 * It's OK to put directory into a group unless
418 * it has too many directories already (max_dirs) or
419 * it has too few free inodes left (min_inodes) or
420 * it has too few free blocks left (min_blocks) or
421 * it's already running too large debt (max_debt).
422 * Parent's group is preferred, if it doesn't satisfy these
423 * conditions we search cyclically through the rest. If none
424 * of the groups look good we just look for a group with more
425 * free inodes than average (starting at parent's group).
427 * Debt is incremented each time we allocate a directory and decremented
428 * when we allocate an inode, within 0--255.
431 #define INODE_COST 64
432 #define BLOCK_COST 256
434 static int find_group_orlov(struct super_block *sb, struct inode *parent,
437 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
438 struct ext4_sb_info *sbi = EXT4_SB(sb);
439 struct ext4_super_block *es = sbi->s_es;
440 ext4_group_t ngroups = sbi->s_groups_count;
441 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
442 unsigned int freei, avefreei;
443 ext4_fsblk_t freeb, avefreeb;
444 ext4_fsblk_t blocks_per_dir;
446 int max_debt, max_dirs, min_inodes;
447 ext4_grpblk_t min_blocks;
449 struct ext4_group_desc *desc;
451 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
452 avefreei = freei / ngroups;
453 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
455 do_div(avefreeb, ngroups);
456 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
458 if ((parent == sb->s_root->d_inode) ||
459 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
460 int best_ndir = inodes_per_group;
464 get_random_bytes(&grp, sizeof(grp));
465 parent_group = (unsigned)grp % ngroups;
466 for (i = 0; i < ngroups; i++) {
467 grp = (parent_group + i) % ngroups;
468 desc = ext4_get_group_desc(sb, grp, NULL);
469 if (!desc || !ext4_free_inodes_count(sb, desc))
471 if (ext4_used_dirs_count(sb, desc) >= best_ndir)
473 if (ext4_free_inodes_count(sb, desc) < avefreei)
475 if (ext4_free_blks_count(sb, desc) < avefreeb)
479 best_ndir = ext4_used_dirs_count(sb, desc);
486 blocks_per_dir = ext4_blocks_count(es) - freeb;
487 do_div(blocks_per_dir, ndirs);
489 max_dirs = ndirs / ngroups + inodes_per_group / 16;
490 min_inodes = avefreei - inodes_per_group / 4;
491 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
493 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
494 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
495 if (max_debt * INODE_COST > inodes_per_group)
496 max_debt = inodes_per_group / INODE_COST;
502 for (i = 0; i < ngroups; i++) {
503 *group = (parent_group + i) % ngroups;
504 desc = ext4_get_group_desc(sb, *group, NULL);
505 if (!desc || !ext4_free_inodes_count(sb, desc))
507 if (ext4_used_dirs_count(sb, desc) >= max_dirs)
509 if (ext4_free_inodes_count(sb, desc) < min_inodes)
511 if (ext4_free_blks_count(sb, desc) < min_blocks)
517 for (i = 0; i < ngroups; i++) {
518 *group = (parent_group + i) % ngroups;
519 desc = ext4_get_group_desc(sb, *group, NULL);
520 if (desc && ext4_free_inodes_count(sb, desc) &&
521 ext4_free_inodes_count(sb, desc) >= avefreei)
527 * The free-inodes counter is approximate, and for really small
528 * filesystems the above test can fail to find any blockgroups
537 static int find_group_other(struct super_block *sb, struct inode *parent,
540 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
541 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
542 struct ext4_group_desc *desc;
546 * Try to place the inode in its parent directory
548 *group = parent_group;
549 desc = ext4_get_group_desc(sb, *group, NULL);
550 if (desc && ext4_free_inodes_count(sb, desc) &&
551 ext4_free_blks_count(sb, desc))
555 * We're going to place this inode in a different blockgroup from its
556 * parent. We want to cause files in a common directory to all land in
557 * the same blockgroup. But we want files which are in a different
558 * directory which shares a blockgroup with our parent to land in a
559 * different blockgroup.
561 * So add our directory's i_ino into the starting point for the hash.
563 *group = (*group + parent->i_ino) % ngroups;
566 * Use a quadratic hash to find a group with a free inode and some free
569 for (i = 1; i < ngroups; i <<= 1) {
571 if (*group >= ngroups)
573 desc = ext4_get_group_desc(sb, *group, NULL);
574 if (desc && ext4_free_inodes_count(sb, desc) &&
575 ext4_free_blks_count(sb, desc))
580 * That failed: try linear search for a free inode, even if that group
581 * has no free blocks.
583 *group = parent_group;
584 for (i = 0; i < ngroups; i++) {
585 if (++*group >= ngroups)
587 desc = ext4_get_group_desc(sb, *group, NULL);
588 if (desc && ext4_free_inodes_count(sb, desc))
596 * claim the inode from the inode bitmap. If the group
597 * is uninit we need to take the groups's sb_bgl_lock
598 * and clear the uninit flag. The inode bitmap update
599 * and group desc uninit flag clear should be done
600 * after holding sb_bgl_lock so that ext4_read_inode_bitmap
601 * doesn't race with the ext4_claim_inode
603 static int ext4_claim_inode(struct super_block *sb,
604 struct buffer_head *inode_bitmap_bh,
605 unsigned long ino, ext4_group_t group, int mode)
607 int free = 0, retval = 0, count;
608 struct ext4_sb_info *sbi = EXT4_SB(sb);
609 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
611 spin_lock(sb_bgl_lock(sbi, group));
612 if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
613 /* not a free inode */
618 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
619 ino > EXT4_INODES_PER_GROUP(sb)) {
620 spin_unlock(sb_bgl_lock(sbi, group));
621 ext4_error(sb, __func__,
622 "reserved inode or inode > inodes count - "
623 "block_group = %u, inode=%lu", group,
624 ino + group * EXT4_INODES_PER_GROUP(sb));
627 /* If we didn't allocate from within the initialized part of the inode
628 * table then we need to initialize up to this inode. */
629 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
631 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
632 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
633 /* When marking the block group with
634 * ~EXT4_BG_INODE_UNINIT we don't want to depend
635 * on the value of bg_itable_unused even though
636 * mke2fs could have initialized the same for us.
637 * Instead we calculated the value below
642 free = EXT4_INODES_PER_GROUP(sb) -
643 ext4_itable_unused_count(sb, gdp);
647 * Check the relative inode number against the last used
648 * relative inode number in this group. if it is greater
649 * we need to update the bg_itable_unused count
653 ext4_itable_unused_set(sb, gdp,
654 (EXT4_INODES_PER_GROUP(sb) - ino));
656 count = ext4_free_inodes_count(sb, gdp) - 1;
657 ext4_free_inodes_set(sb, gdp, count);
659 count = ext4_used_dirs_count(sb, gdp) + 1;
660 ext4_used_dirs_set(sb, gdp, count);
662 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
664 spin_unlock(sb_bgl_lock(sbi, group));
669 * There are two policies for allocating an inode. If the new inode is
670 * a directory, then a forward search is made for a block group with both
671 * free space and a low directory-to-inode ratio; if that fails, then of
672 * the groups with above-average free space, that group with the fewest
673 * directories already is chosen.
675 * For other inodes, search forward from the parent directory's block
676 * group to find a free inode.
678 struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
680 struct super_block *sb;
681 struct buffer_head *inode_bitmap_bh = NULL;
682 struct buffer_head *group_desc_bh;
683 ext4_group_t group = 0;
684 unsigned long ino = 0;
686 struct ext4_group_desc *gdp = NULL;
687 struct ext4_super_block *es;
688 struct ext4_inode_info *ei;
689 struct ext4_sb_info *sbi;
694 ext4_group_t flex_group;
696 /* Cannot create files in a deleted directory */
697 if (!dir || !dir->i_nlink)
698 return ERR_PTR(-EPERM);
701 inode = new_inode(sb);
703 return ERR_PTR(-ENOMEM);
709 if (sbi->s_log_groups_per_flex) {
710 ret2 = find_group_flex(sb, dir, &group);
715 if (test_opt(sb, OLDALLOC))
716 ret2 = find_group_dir(sb, dir, &group);
718 ret2 = find_group_orlov(sb, dir, &group);
720 ret2 = find_group_other(sb, dir, &group);
727 for (i = 0; i < sbi->s_groups_count; i++) {
730 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
734 brelse(inode_bitmap_bh);
735 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
736 if (!inode_bitmap_bh)
741 repeat_in_this_group:
742 ino = ext4_find_next_zero_bit((unsigned long *)
743 inode_bitmap_bh->b_data,
744 EXT4_INODES_PER_GROUP(sb), ino);
746 if (ino < EXT4_INODES_PER_GROUP(sb)) {
748 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
749 err = ext4_journal_get_write_access(handle,
754 BUFFER_TRACE(group_desc_bh, "get_write_access");
755 err = ext4_journal_get_write_access(handle,
759 if (!ext4_claim_inode(sb, inode_bitmap_bh,
762 BUFFER_TRACE(inode_bitmap_bh,
763 "call ext4_handle_dirty_metadata");
764 err = ext4_handle_dirty_metadata(handle,
769 /* zero bit is inode number 1*/
774 ext4_handle_release_buffer(handle, inode_bitmap_bh);
775 ext4_handle_release_buffer(handle, group_desc_bh);
777 if (++ino < EXT4_INODES_PER_GROUP(sb))
778 goto repeat_in_this_group;
782 * This case is possible in concurrent environment. It is very
783 * rare. We cannot repeat the find_group_xxx() call because
784 * that will simply return the same blockgroup, because the
785 * group descriptor metadata has not yet been updated.
786 * So we just go onto the next blockgroup.
788 if (++group == sbi->s_groups_count)
795 /* We may have to initialize the block bitmap if it isn't already */
796 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
797 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
798 struct buffer_head *block_bitmap_bh;
800 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
801 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
802 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
804 brelse(block_bitmap_bh);
809 spin_lock(sb_bgl_lock(sbi, group));
810 /* recheck and clear flag under lock if we still need to */
811 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
812 free = ext4_free_blocks_after_init(sb, group, gdp);
813 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
814 ext4_free_blks_set(sb, gdp, free);
815 gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
818 spin_unlock(sb_bgl_lock(sbi, group));
820 /* Don't need to dirty bitmap block if we didn't change it */
822 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
823 err = ext4_handle_dirty_metadata(handle,
824 NULL, block_bitmap_bh);
827 brelse(block_bitmap_bh);
831 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
832 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
836 percpu_counter_dec(&sbi->s_freeinodes_counter);
838 percpu_counter_inc(&sbi->s_dirs_counter);
841 if (sbi->s_log_groups_per_flex) {
842 flex_group = ext4_flex_group(sbi, group);
843 spin_lock(sb_bgl_lock(sbi, flex_group));
844 sbi->s_flex_groups[flex_group].free_inodes--;
845 spin_unlock(sb_bgl_lock(sbi, flex_group));
848 inode->i_uid = current_fsuid();
849 if (test_opt(sb, GRPID))
850 inode->i_gid = dir->i_gid;
851 else if (dir->i_mode & S_ISGID) {
852 inode->i_gid = dir->i_gid;
856 inode->i_gid = current_fsgid();
857 inode->i_mode = mode;
859 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
860 /* This is the optimal IO size (for stat), not the fs block size */
862 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
863 ext4_current_time(inode);
865 memset(ei->i_data, 0, sizeof(ei->i_data));
866 ei->i_dir_start_lookup = 0;
870 * Don't inherit extent flag from directory. We set extent flag on
871 * newly created directory and file only if -o extent mount option is
874 ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
876 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
877 /* dirsync only applies to directories */
879 ei->i_flags &= ~EXT4_DIRSYNC_FL;
882 ei->i_block_group = group;
884 ext4_set_inode_flags(inode);
885 if (IS_DIRSYNC(inode))
886 ext4_handle_sync(handle);
887 if (insert_inode_locked(inode) < 0) {
891 spin_lock(&sbi->s_next_gen_lock);
892 inode->i_generation = sbi->s_next_generation++;
893 spin_unlock(&sbi->s_next_gen_lock);
895 ei->i_state = EXT4_STATE_NEW;
897 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
900 if (DQUOT_ALLOC_INODE(inode)) {
905 err = ext4_init_acl(handle, inode, dir);
909 err = ext4_init_security(handle, inode, dir);
913 if (test_opt(sb, EXTENTS)) {
914 /* set extent flag only for directory, file and normal symlink*/
915 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
916 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
917 ext4_ext_tree_init(handle, inode);
921 err = ext4_mark_inode_dirty(handle, inode);
923 ext4_std_error(sb, err);
927 ext4_debug("allocating inode %lu\n", inode->i_ino);
930 ext4_std_error(sb, err);
935 brelse(inode_bitmap_bh);
939 DQUOT_FREE_INODE(inode);
943 inode->i_flags |= S_NOQUOTA;
945 unlock_new_inode(inode);
947 brelse(inode_bitmap_bh);
951 /* Verify that we are loading a valid orphan from disk */
952 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
954 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
955 ext4_group_t block_group;
957 struct buffer_head *bitmap_bh;
958 struct inode *inode = NULL;
961 /* Error cases - e2fsck has already cleaned up for us */
963 ext4_warning(sb, __func__,
964 "bad orphan ino %lu! e2fsck was run?", ino);
968 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
969 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
970 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
972 ext4_warning(sb, __func__,
973 "inode bitmap error for orphan %lu", ino);
977 /* Having the inode bit set should be a 100% indicator that this
978 * is a valid orphan (no e2fsck run on fs). Orphans also include
979 * inodes that were being truncated, so we can't check i_nlink==0.
981 if (!ext4_test_bit(bit, bitmap_bh->b_data))
984 inode = ext4_iget(sb, ino);
989 * If the orphans has i_nlinks > 0 then it should be able to be
990 * truncated, otherwise it won't be removed from the orphan list
991 * during processing and an infinite loop will result.
993 if (inode->i_nlink && !ext4_can_truncate(inode))
996 if (NEXT_ORPHAN(inode) > max_ino)
1002 err = PTR_ERR(inode);
1005 ext4_warning(sb, __func__,
1006 "bad orphan inode %lu! e2fsck was run?", ino);
1007 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1008 bit, (unsigned long long)bitmap_bh->b_blocknr,
1009 ext4_test_bit(bit, bitmap_bh->b_data));
1010 printk(KERN_NOTICE "inode=%p\n", inode);
1012 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
1013 is_bad_inode(inode));
1014 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
1015 NEXT_ORPHAN(inode));
1016 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
1017 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
1018 /* Avoid freeing blocks if we got a bad deleted inode */
1019 if (inode->i_nlink == 0)
1020 inode->i_blocks = 0;
1025 return ERR_PTR(err);
1028 unsigned long ext4_count_free_inodes(struct super_block *sb)
1030 unsigned long desc_count;
1031 struct ext4_group_desc *gdp;
1034 struct ext4_super_block *es;
1035 unsigned long bitmap_count, x;
1036 struct buffer_head *bitmap_bh = NULL;
1038 es = EXT4_SB(sb)->s_es;
1042 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1043 gdp = ext4_get_group_desc(sb, i, NULL);
1046 desc_count += ext4_free_inodes_count(sb, gdp);
1048 bitmap_bh = ext4_read_inode_bitmap(sb, i);
1052 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
1053 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1054 i, ext4_free_inodes_count(sb, gdp), x);
1058 printk(KERN_DEBUG "ext4_count_free_inodes: "
1059 "stored = %u, computed = %lu, %lu\n",
1060 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1064 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1065 gdp = ext4_get_group_desc(sb, i, NULL);
1068 desc_count += ext4_free_inodes_count(sb, gdp);
1075 /* Called at mount-time, super-block is locked */
1076 unsigned long ext4_count_dirs(struct super_block * sb)
1078 unsigned long count = 0;
1081 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1082 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1085 count += ext4_used_dirs_count(sb, gdp);