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>
28 #include "ext4_jbd2.h"
32 #include <trace/events/ext4.h>
35 * ialloc.c contains the inodes allocation and deallocation routines
39 * The free inodes are managed by bitmaps. A file system contains several
40 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
41 * block for inodes, N blocks for the inode table and data blocks.
43 * The file system contains group descriptors which are located after the
44 * super block. Each descriptor contains the number of the bitmap block and
45 * the free blocks count in the block.
49 * To avoid calling the atomic setbit hundreds or thousands of times, we only
50 * need to use it within a single byte (to ensure we get endianness right).
51 * We can use memset for the rest of the bitmap as there are no other users.
53 void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
57 if (start_bit >= end_bit)
60 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
61 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
62 ext4_set_bit(i, bitmap);
64 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
67 /* Initializes an uninitialized inode bitmap */
68 static unsigned ext4_init_inode_bitmap(struct super_block *sb,
69 struct buffer_head *bh,
70 ext4_group_t block_group,
71 struct ext4_group_desc *gdp)
73 struct ext4_sb_info *sbi = EXT4_SB(sb);
75 J_ASSERT_BH(bh, buffer_locked(bh));
77 /* If checksum is bad mark all blocks and inodes use to prevent
78 * allocation, essentially implementing a per-group read-only flag. */
79 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
80 ext4_error(sb, "Checksum bad for group %u", block_group);
81 ext4_free_group_clusters_set(sb, gdp, 0);
82 ext4_free_inodes_set(sb, gdp, 0);
83 ext4_itable_unused_set(sb, gdp, 0);
84 memset(bh->b_data, 0xff, sb->s_blocksize);
88 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
89 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
92 return EXT4_INODES_PER_GROUP(sb);
96 * Read the inode allocation bitmap for a given block_group, reading
97 * into the specified slot in the superblock's bitmap cache.
99 * Return buffer_head of bitmap on success or NULL.
101 static struct buffer_head *
102 ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
104 struct ext4_group_desc *desc;
105 struct ext4_sb_info *sbi = EXT4_SB(sb);
106 struct buffer_head *bh = NULL;
107 ext4_fsblk_t bitmap_blk;
109 desc = ext4_get_group_desc(sb, block_group, NULL);
113 bitmap_blk = ext4_inode_bitmap(sb, desc);
114 if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
115 (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
116 ext4_error(sb, "Invalid inode bitmap blk %llu in "
117 "block_group %u", bitmap_blk, block_group);
120 bh = sb_getblk(sb, bitmap_blk);
122 ext4_error(sb, "Cannot read inode bitmap - "
123 "block_group = %u, inode_bitmap = %llu",
124 block_group, bitmap_blk);
127 if (bitmap_uptodate(bh))
131 if (bitmap_uptodate(bh)) {
136 ext4_lock_group(sb, block_group);
137 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
138 ext4_init_inode_bitmap(sb, bh, block_group, desc);
139 set_bitmap_uptodate(bh);
140 set_buffer_uptodate(bh);
141 ext4_unlock_group(sb, block_group);
145 ext4_unlock_group(sb, block_group);
147 if (buffer_uptodate(bh)) {
149 * if not uninit if bh is uptodate,
150 * bitmap is also uptodate
152 set_bitmap_uptodate(bh);
157 * submit the buffer_head for read. We can
158 * safely mark the bitmap as uptodate now.
159 * We do it here so the bitmap uptodate bit
160 * get set with buffer lock held.
162 trace_ext4_load_inode_bitmap(sb, block_group);
163 set_bitmap_uptodate(bh);
164 if (bh_submit_read(bh) < 0) {
166 ext4_error(sb, "Cannot read inode bitmap - "
167 "block_group = %u, inode_bitmap = %llu",
168 block_group, bitmap_blk);
175 * NOTE! When we get the inode, we're the only people
176 * that have access to it, and as such there are no
177 * race conditions we have to worry about. The inode
178 * is not on the hash-lists, and it cannot be reached
179 * through the filesystem because the directory entry
180 * has been deleted earlier.
182 * HOWEVER: we must make sure that we get no aliases,
183 * which means that we have to call "clear_inode()"
184 * _before_ we mark the inode not in use in the inode
185 * bitmaps. Otherwise a newly created file might use
186 * the same inode number (not actually the same pointer
187 * though), and then we'd have two inodes sharing the
188 * same inode number and space on the harddisk.
190 void ext4_free_inode(handle_t *handle, struct inode *inode)
192 struct super_block *sb = inode->i_sb;
195 struct buffer_head *bitmap_bh = NULL;
196 struct buffer_head *bh2;
197 ext4_group_t block_group;
199 struct ext4_group_desc *gdp;
200 struct ext4_super_block *es;
201 struct ext4_sb_info *sbi;
202 int fatal = 0, err, count, cleared;
204 if (atomic_read(&inode->i_count) > 1) {
205 printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
206 atomic_read(&inode->i_count));
209 if (inode->i_nlink) {
210 printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
215 printk(KERN_ERR "ext4_free_inode: inode on "
216 "nonexistent device\n");
222 ext4_debug("freeing inode %lu\n", ino);
223 trace_ext4_free_inode(inode);
226 * Note: we must free any quota before locking the superblock,
227 * as writing the quota to disk may need the lock as well.
229 dquot_initialize(inode);
230 ext4_xattr_delete_inode(handle, inode);
231 dquot_free_inode(inode);
234 is_directory = S_ISDIR(inode->i_mode);
236 /* Do this BEFORE marking the inode not in use or returning an error */
237 ext4_clear_inode(inode);
239 es = EXT4_SB(sb)->s_es;
240 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
241 ext4_error(sb, "reserved or nonexistent inode %lu", ino);
244 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
245 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
246 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
250 BUFFER_TRACE(bitmap_bh, "get_write_access");
251 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
256 gdp = ext4_get_group_desc(sb, block_group, &bh2);
258 BUFFER_TRACE(bh2, "get_write_access");
259 fatal = ext4_journal_get_write_access(handle, bh2);
261 ext4_lock_group(sb, block_group);
262 cleared = ext4_clear_bit(bit, bitmap_bh->b_data);
263 if (fatal || !cleared) {
264 ext4_unlock_group(sb, block_group);
268 count = ext4_free_inodes_count(sb, gdp) + 1;
269 ext4_free_inodes_set(sb, gdp, count);
271 count = ext4_used_dirs_count(sb, gdp) - 1;
272 ext4_used_dirs_set(sb, gdp, count);
273 percpu_counter_dec(&sbi->s_dirs_counter);
275 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
276 ext4_unlock_group(sb, block_group);
278 percpu_counter_inc(&sbi->s_freeinodes_counter);
279 if (sbi->s_log_groups_per_flex) {
280 ext4_group_t f = ext4_flex_group(sbi, block_group);
282 atomic_inc(&sbi->s_flex_groups[f].free_inodes);
284 atomic_dec(&sbi->s_flex_groups[f].used_dirs);
286 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
287 fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
290 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
291 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
294 ext4_mark_super_dirty(sb);
296 ext4_error(sb, "bit already cleared for inode %lu", ino);
300 ext4_std_error(sb, fatal);
310 * Helper function for Orlov's allocator; returns critical information
311 * for a particular block group or flex_bg. If flex_size is 1, then g
312 * is a block group number; otherwise it is flex_bg number.
314 static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
315 int flex_size, struct orlov_stats *stats)
317 struct ext4_group_desc *desc;
318 struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
321 stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
322 stats->free_clusters = atomic64_read(&flex_group[g].free_clusters);
323 stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
327 desc = ext4_get_group_desc(sb, g, NULL);
329 stats->free_inodes = ext4_free_inodes_count(sb, desc);
330 stats->free_clusters = ext4_free_group_clusters(sb, desc);
331 stats->used_dirs = ext4_used_dirs_count(sb, desc);
333 stats->free_inodes = 0;
334 stats->free_clusters = 0;
335 stats->used_dirs = 0;
340 * Orlov's allocator for directories.
342 * We always try to spread first-level directories.
344 * If there are blockgroups with both free inodes and free blocks counts
345 * not worse than average we return one with smallest directory count.
346 * Otherwise we simply return a random group.
348 * For the rest rules look so:
350 * It's OK to put directory into a group unless
351 * it has too many directories already (max_dirs) or
352 * it has too few free inodes left (min_inodes) or
353 * it has too few free blocks left (min_blocks) or
354 * Parent's group is preferred, if it doesn't satisfy these
355 * conditions we search cyclically through the rest. If none
356 * of the groups look good we just look for a group with more
357 * free inodes than average (starting at parent's group).
360 static int find_group_orlov(struct super_block *sb, struct inode *parent,
361 ext4_group_t *group, int mode,
362 const struct qstr *qstr)
364 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
365 struct ext4_sb_info *sbi = EXT4_SB(sb);
366 ext4_group_t real_ngroups = ext4_get_groups_count(sb);
367 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
368 unsigned int freei, avefreei;
369 ext4_fsblk_t freeb, avefreec;
371 int max_dirs, min_inodes;
372 ext4_grpblk_t min_clusters;
373 ext4_group_t i, grp, g, ngroups;
374 struct ext4_group_desc *desc;
375 struct orlov_stats stats;
376 int flex_size = ext4_flex_bg_size(sbi);
377 struct dx_hash_info hinfo;
379 ngroups = real_ngroups;
381 ngroups = (real_ngroups + flex_size - 1) >>
382 sbi->s_log_groups_per_flex;
383 parent_group >>= sbi->s_log_groups_per_flex;
386 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
387 avefreei = freei / ngroups;
388 freeb = EXT4_C2B(sbi,
389 percpu_counter_read_positive(&sbi->s_freeclusters_counter));
391 do_div(avefreec, ngroups);
392 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
395 ((parent == sb->s_root->d_inode) ||
396 (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
397 int best_ndir = inodes_per_group;
401 hinfo.hash_version = DX_HASH_HALF_MD4;
402 hinfo.seed = sbi->s_hash_seed;
403 ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
406 get_random_bytes(&grp, sizeof(grp));
407 parent_group = (unsigned)grp % ngroups;
408 for (i = 0; i < ngroups; i++) {
409 g = (parent_group + i) % ngroups;
410 get_orlov_stats(sb, g, flex_size, &stats);
411 if (!stats.free_inodes)
413 if (stats.used_dirs >= best_ndir)
415 if (stats.free_inodes < avefreei)
417 if (stats.free_clusters < avefreec)
421 best_ndir = stats.used_dirs;
426 if (flex_size == 1) {
432 * We pack inodes at the beginning of the flexgroup's
433 * inode tables. Block allocation decisions will do
434 * something similar, although regular files will
435 * start at 2nd block group of the flexgroup. See
436 * ext4_ext_find_goal() and ext4_find_near().
439 for (i = 0; i < flex_size; i++) {
440 if (grp+i >= real_ngroups)
442 desc = ext4_get_group_desc(sb, grp+i, NULL);
443 if (desc && ext4_free_inodes_count(sb, desc)) {
451 max_dirs = ndirs / ngroups + inodes_per_group / 16;
452 min_inodes = avefreei - inodes_per_group*flex_size / 4;
455 min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
458 * Start looking in the flex group where we last allocated an
459 * inode for this parent directory
461 if (EXT4_I(parent)->i_last_alloc_group != ~0) {
462 parent_group = EXT4_I(parent)->i_last_alloc_group;
464 parent_group >>= sbi->s_log_groups_per_flex;
467 for (i = 0; i < ngroups; i++) {
468 grp = (parent_group + i) % ngroups;
469 get_orlov_stats(sb, grp, flex_size, &stats);
470 if (stats.used_dirs >= max_dirs)
472 if (stats.free_inodes < min_inodes)
474 if (stats.free_clusters < min_clusters)
480 ngroups = real_ngroups;
481 avefreei = freei / ngroups;
483 parent_group = EXT4_I(parent)->i_block_group;
484 for (i = 0; i < ngroups; i++) {
485 grp = (parent_group + i) % ngroups;
486 desc = ext4_get_group_desc(sb, grp, NULL);
487 if (desc && ext4_free_inodes_count(sb, desc) &&
488 ext4_free_inodes_count(sb, desc) >= avefreei) {
496 * The free-inodes counter is approximate, and for really small
497 * filesystems the above test can fail to find any blockgroups
506 static int find_group_other(struct super_block *sb, struct inode *parent,
507 ext4_group_t *group, int mode)
509 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
510 ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
511 struct ext4_group_desc *desc;
512 int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
515 * Try to place the inode is the same flex group as its
516 * parent. If we can't find space, use the Orlov algorithm to
517 * find another flex group, and store that information in the
518 * parent directory's inode information so that use that flex
519 * group for future allocations.
525 parent_group &= ~(flex_size-1);
526 last = parent_group + flex_size;
529 for (i = parent_group; i < last; i++) {
530 desc = ext4_get_group_desc(sb, i, NULL);
531 if (desc && ext4_free_inodes_count(sb, desc)) {
536 if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
538 parent_group = EXT4_I(parent)->i_last_alloc_group;
542 * If this didn't work, use the Orlov search algorithm
543 * to find a new flex group; we pass in the mode to
544 * avoid the topdir algorithms.
546 *group = parent_group + flex_size;
547 if (*group > ngroups)
549 return find_group_orlov(sb, parent, group, mode, NULL);
553 * Try to place the inode in its parent directory
555 *group = parent_group;
556 desc = ext4_get_group_desc(sb, *group, NULL);
557 if (desc && ext4_free_inodes_count(sb, desc) &&
558 ext4_free_group_clusters(sb, desc))
562 * We're going to place this inode in a different blockgroup from its
563 * parent. We want to cause files in a common directory to all land in
564 * the same blockgroup. But we want files which are in a different
565 * directory which shares a blockgroup with our parent to land in a
566 * different blockgroup.
568 * So add our directory's i_ino into the starting point for the hash.
570 *group = (*group + parent->i_ino) % ngroups;
573 * Use a quadratic hash to find a group with a free inode and some free
576 for (i = 1; i < ngroups; i <<= 1) {
578 if (*group >= ngroups)
580 desc = ext4_get_group_desc(sb, *group, NULL);
581 if (desc && ext4_free_inodes_count(sb, desc) &&
582 ext4_free_group_clusters(sb, desc))
587 * That failed: try linear search for a free inode, even if that group
588 * has no free blocks.
590 *group = parent_group;
591 for (i = 0; i < ngroups; i++) {
592 if (++*group >= ngroups)
594 desc = ext4_get_group_desc(sb, *group, NULL);
595 if (desc && ext4_free_inodes_count(sb, desc))
603 * claim the inode from the inode bitmap. If the group
604 * is uninit we need to take the groups's ext4_group_lock
605 * and clear the uninit flag. The inode bitmap update
606 * and group desc uninit flag clear should be done
607 * after holding ext4_group_lock so that ext4_read_inode_bitmap
608 * doesn't race with the ext4_claim_inode
610 static int ext4_claim_inode(struct super_block *sb,
611 struct buffer_head *inode_bitmap_bh,
612 unsigned long ino, ext4_group_t group, int mode)
614 int free = 0, retval = 0, count;
615 struct ext4_sb_info *sbi = EXT4_SB(sb);
616 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
617 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
620 * We have to be sure that new inode allocation does not race with
621 * inode table initialization, because otherwise we may end up
622 * allocating and writing new inode right before sb_issue_zeroout
623 * takes place and overwriting our new inode with zeroes. So we
624 * take alloc_sem to prevent it.
626 down_read(&grp->alloc_sem);
627 ext4_lock_group(sb, group);
628 if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
629 /* not a free inode */
634 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
635 ino > EXT4_INODES_PER_GROUP(sb)) {
636 ext4_unlock_group(sb, group);
637 up_read(&grp->alloc_sem);
638 ext4_error(sb, "reserved inode or inode > inodes count - "
639 "block_group = %u, inode=%lu", group,
640 ino + group * EXT4_INODES_PER_GROUP(sb));
643 /* If we didn't allocate from within the initialized part of the inode
644 * table then we need to initialize up to this inode. */
645 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
647 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
648 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
649 /* When marking the block group with
650 * ~EXT4_BG_INODE_UNINIT we don't want to depend
651 * on the value of bg_itable_unused even though
652 * mke2fs could have initialized the same for us.
653 * Instead we calculated the value below
658 free = EXT4_INODES_PER_GROUP(sb) -
659 ext4_itable_unused_count(sb, gdp);
663 * Check the relative inode number against the last used
664 * relative inode number in this group. if it is greater
665 * we need to update the bg_itable_unused count
669 ext4_itable_unused_set(sb, gdp,
670 (EXT4_INODES_PER_GROUP(sb) - ino));
672 count = ext4_free_inodes_count(sb, gdp) - 1;
673 ext4_free_inodes_set(sb, gdp, count);
675 count = ext4_used_dirs_count(sb, gdp) + 1;
676 ext4_used_dirs_set(sb, gdp, count);
677 if (sbi->s_log_groups_per_flex) {
678 ext4_group_t f = ext4_flex_group(sbi, group);
680 atomic_inc(&sbi->s_flex_groups[f].used_dirs);
683 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
685 ext4_unlock_group(sb, group);
686 up_read(&grp->alloc_sem);
691 * There are two policies for allocating an inode. If the new inode is
692 * a directory, then a forward search is made for a block group with both
693 * free space and a low directory-to-inode ratio; if that fails, then of
694 * the groups with above-average free space, that group with the fewest
695 * directories already is chosen.
697 * For other inodes, search forward from the parent directory's block
698 * group to find a free inode.
700 struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode,
701 const struct qstr *qstr, __u32 goal, uid_t *owner)
703 struct super_block *sb;
704 struct buffer_head *inode_bitmap_bh = NULL;
705 struct buffer_head *group_desc_bh;
706 ext4_group_t ngroups, group = 0;
707 unsigned long ino = 0;
709 struct ext4_group_desc *gdp = NULL;
710 struct ext4_inode_info *ei;
711 struct ext4_sb_info *sbi;
715 ext4_group_t flex_group;
717 /* Cannot create files in a deleted directory */
718 if (!dir || !dir->i_nlink)
719 return ERR_PTR(-EPERM);
722 ngroups = ext4_get_groups_count(sb);
723 trace_ext4_request_inode(dir, mode);
724 inode = new_inode(sb);
726 return ERR_PTR(-ENOMEM);
731 goal = sbi->s_inode_goal;
733 if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
734 group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
735 ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
741 ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
743 ret2 = find_group_other(sb, dir, &group, mode);
746 EXT4_I(dir)->i_last_alloc_group = group;
751 for (i = 0; i < ngroups; i++, ino = 0) {
754 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
758 brelse(inode_bitmap_bh);
759 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
760 if (!inode_bitmap_bh)
763 repeat_in_this_group:
764 ino = ext4_find_next_zero_bit((unsigned long *)
765 inode_bitmap_bh->b_data,
766 EXT4_INODES_PER_GROUP(sb), ino);
768 if (ino < EXT4_INODES_PER_GROUP(sb)) {
770 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
771 err = ext4_journal_get_write_access(handle,
776 BUFFER_TRACE(group_desc_bh, "get_write_access");
777 err = ext4_journal_get_write_access(handle,
781 if (!ext4_claim_inode(sb, inode_bitmap_bh,
784 BUFFER_TRACE(inode_bitmap_bh,
785 "call ext4_handle_dirty_metadata");
786 err = ext4_handle_dirty_metadata(handle,
791 /* zero bit is inode number 1*/
796 ext4_handle_release_buffer(handle, inode_bitmap_bh);
797 ext4_handle_release_buffer(handle, group_desc_bh);
799 if (++ino < EXT4_INODES_PER_GROUP(sb))
800 goto repeat_in_this_group;
804 * This case is possible in concurrent environment. It is very
805 * rare. We cannot repeat the find_group_xxx() call because
806 * that will simply return the same blockgroup, because the
807 * group descriptor metadata has not yet been updated.
808 * So we just go onto the next blockgroup.
810 if (++group == ngroups)
817 /* We may have to initialize the block bitmap if it isn't already */
818 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
819 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
820 struct buffer_head *block_bitmap_bh;
822 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
823 if (!block_bitmap_bh) {
827 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
828 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
830 brelse(block_bitmap_bh);
834 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
835 err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
836 brelse(block_bitmap_bh);
838 /* recheck and clear flag under lock if we still need to */
839 ext4_lock_group(sb, group);
840 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
841 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
842 ext4_free_group_clusters_set(sb, gdp,
843 ext4_free_clusters_after_init(sb, group, gdp));
844 gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
847 ext4_unlock_group(sb, group);
852 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
853 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
857 percpu_counter_dec(&sbi->s_freeinodes_counter);
859 percpu_counter_inc(&sbi->s_dirs_counter);
860 ext4_mark_super_dirty(sb);
862 if (sbi->s_log_groups_per_flex) {
863 flex_group = ext4_flex_group(sbi, group);
864 atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
867 inode->i_mode = mode;
868 inode->i_uid = owner[0];
869 inode->i_gid = owner[1];
870 } else if (test_opt(sb, GRPID)) {
871 inode->i_mode = mode;
872 inode->i_uid = current_fsuid();
873 inode->i_gid = dir->i_gid;
875 inode_init_owner(inode, dir, mode);
877 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
878 /* This is the optimal IO size (for stat), not the fs block size */
880 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
881 ext4_current_time(inode);
883 memset(ei->i_data, 0, sizeof(ei->i_data));
884 ei->i_dir_start_lookup = 0;
887 /* Don't inherit extent flag from directory, amongst others. */
889 ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
892 ei->i_block_group = group;
893 ei->i_last_alloc_group = ~0;
895 ext4_set_inode_flags(inode);
896 if (IS_DIRSYNC(inode))
897 ext4_handle_sync(handle);
898 if (insert_inode_locked(inode) < 0) {
900 * Likely a bitmap corruption causing inode to be allocated
906 spin_lock(&sbi->s_next_gen_lock);
907 inode->i_generation = sbi->s_next_generation++;
908 spin_unlock(&sbi->s_next_gen_lock);
910 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
911 ext4_set_inode_state(inode, EXT4_STATE_NEW);
913 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
916 dquot_initialize(inode);
917 err = dquot_alloc_inode(inode);
921 err = ext4_init_acl(handle, inode, dir);
925 err = ext4_init_security(handle, inode, dir, qstr);
929 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
930 /* set extent flag only for directory, file and normal symlink*/
931 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
932 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
933 ext4_ext_tree_init(handle, inode);
937 if (ext4_handle_valid(handle)) {
938 ei->i_sync_tid = handle->h_transaction->t_tid;
939 ei->i_datasync_tid = handle->h_transaction->t_tid;
942 err = ext4_mark_inode_dirty(handle, inode);
944 ext4_std_error(sb, err);
948 ext4_debug("allocating inode %lu\n", inode->i_ino);
949 trace_ext4_allocate_inode(inode, dir, mode);
952 ext4_std_error(sb, err);
957 brelse(inode_bitmap_bh);
961 dquot_free_inode(inode);
965 inode->i_flags |= S_NOQUOTA;
967 unlock_new_inode(inode);
969 brelse(inode_bitmap_bh);
973 /* Verify that we are loading a valid orphan from disk */
974 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
976 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
977 ext4_group_t block_group;
979 struct buffer_head *bitmap_bh;
980 struct inode *inode = NULL;
983 /* Error cases - e2fsck has already cleaned up for us */
985 ext4_warning(sb, "bad orphan ino %lu! e2fsck was run?", ino);
989 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
990 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
991 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
993 ext4_warning(sb, "inode bitmap error for orphan %lu", ino);
997 /* Having the inode bit set should be a 100% indicator that this
998 * is a valid orphan (no e2fsck run on fs). Orphans also include
999 * inodes that were being truncated, so we can't check i_nlink==0.
1001 if (!ext4_test_bit(bit, bitmap_bh->b_data))
1004 inode = ext4_iget(sb, ino);
1009 * If the orphans has i_nlinks > 0 then it should be able to
1010 * be truncated, otherwise it won't be removed from the orphan
1011 * list during processing and an infinite loop will result.
1012 * Similarly, it must not be a bad inode.
1014 if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1015 is_bad_inode(inode))
1018 if (NEXT_ORPHAN(inode) > max_ino)
1024 err = PTR_ERR(inode);
1027 ext4_warning(sb, "bad orphan inode %lu! e2fsck was run?", ino);
1028 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1029 bit, (unsigned long long)bitmap_bh->b_blocknr,
1030 ext4_test_bit(bit, bitmap_bh->b_data));
1031 printk(KERN_NOTICE "inode=%p\n", inode);
1033 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
1034 is_bad_inode(inode));
1035 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
1036 NEXT_ORPHAN(inode));
1037 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
1038 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
1039 /* Avoid freeing blocks if we got a bad deleted inode */
1040 if (inode->i_nlink == 0)
1041 inode->i_blocks = 0;
1046 return ERR_PTR(err);
1049 unsigned long ext4_count_free_inodes(struct super_block *sb)
1051 unsigned long desc_count;
1052 struct ext4_group_desc *gdp;
1053 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1055 struct ext4_super_block *es;
1056 unsigned long bitmap_count, x;
1057 struct buffer_head *bitmap_bh = NULL;
1059 es = EXT4_SB(sb)->s_es;
1063 for (i = 0; i < ngroups; i++) {
1064 gdp = ext4_get_group_desc(sb, i, NULL);
1067 desc_count += ext4_free_inodes_count(sb, gdp);
1069 bitmap_bh = ext4_read_inode_bitmap(sb, i);
1073 x = ext4_count_free(bitmap_bh->b_data,
1074 EXT4_INODES_PER_GROUP(sb) / 8);
1075 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1076 (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1080 printk(KERN_DEBUG "ext4_count_free_inodes: "
1081 "stored = %u, computed = %lu, %lu\n",
1082 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1086 for (i = 0; i < ngroups; i++) {
1087 gdp = ext4_get_group_desc(sb, i, NULL);
1090 desc_count += ext4_free_inodes_count(sb, gdp);
1097 /* Called at mount-time, super-block is locked */
1098 unsigned long ext4_count_dirs(struct super_block * sb)
1100 unsigned long count = 0;
1101 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1103 for (i = 0; i < ngroups; i++) {
1104 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1107 count += ext4_used_dirs_count(sb, gdp);
1113 * Zeroes not yet zeroed inode table - just write zeroes through the whole
1114 * inode table. Must be called without any spinlock held. The only place
1115 * where it is called from on active part of filesystem is ext4lazyinit
1116 * thread, so we do not need any special locks, however we have to prevent
1117 * inode allocation from the current group, so we take alloc_sem lock, to
1118 * block ext4_claim_inode until we are finished.
1120 int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1123 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1124 struct ext4_sb_info *sbi = EXT4_SB(sb);
1125 struct ext4_group_desc *gdp = NULL;
1126 struct buffer_head *group_desc_bh;
1129 int num, ret = 0, used_blks = 0;
1131 /* This should not happen, but just to be sure check this */
1132 if (sb->s_flags & MS_RDONLY) {
1137 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1142 * We do not need to lock this, because we are the only one
1143 * handling this flag.
1145 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1148 handle = ext4_journal_start_sb(sb, 1);
1149 if (IS_ERR(handle)) {
1150 ret = PTR_ERR(handle);
1154 down_write(&grp->alloc_sem);
1156 * If inode bitmap was already initialized there may be some
1157 * used inodes so we need to skip blocks with used inodes in
1160 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
1161 used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
1162 ext4_itable_unused_count(sb, gdp)),
1163 sbi->s_inodes_per_block);
1165 if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
1166 ext4_error(sb, "Something is wrong with group %u\n"
1167 "Used itable blocks: %d"
1168 "itable unused count: %u\n",
1170 ext4_itable_unused_count(sb, gdp));
1175 blk = ext4_inode_table(sb, gdp) + used_blks;
1176 num = sbi->s_itb_per_group - used_blks;
1178 BUFFER_TRACE(group_desc_bh, "get_write_access");
1179 ret = ext4_journal_get_write_access(handle,
1185 * Skip zeroout if the inode table is full. But we set the ZEROED
1186 * flag anyway, because obviously, when it is full it does not need
1189 if (unlikely(num == 0))
1192 ext4_debug("going to zero out inode table in group %d\n",
1194 ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1198 blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
1201 ext4_lock_group(sb, group);
1202 gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1203 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
1204 ext4_unlock_group(sb, group);
1206 BUFFER_TRACE(group_desc_bh,
1207 "call ext4_handle_dirty_metadata");
1208 ret = ext4_handle_dirty_metadata(handle, NULL,
1212 up_write(&grp->alloc_sem);
1213 ext4_journal_stop(handle);