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), sb->s_blocksize * 8,
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, cleared;
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);
213 trace_mark(ext4_free_inode,
214 "dev %s ino %lu mode %d uid %lu gid %lu bocks %llu",
215 sb->s_id, inode->i_ino, inode->i_mode,
216 (unsigned long) inode->i_uid, (unsigned long) inode->i_gid,
217 (unsigned long long) inode->i_blocks);
220 * Note: we must free any quota before locking the superblock,
221 * as writing the quota to disk may need the lock as well.
224 ext4_xattr_delete_inode(handle, inode);
225 vfs_dq_free_inode(inode);
228 is_directory = S_ISDIR(inode->i_mode);
230 /* Do this BEFORE marking the inode not in use or returning an error */
233 es = EXT4_SB(sb)->s_es;
234 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
235 ext4_error(sb, "ext4_free_inode",
236 "reserved or nonexistent inode %lu", ino);
239 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
240 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
241 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
245 BUFFER_TRACE(bitmap_bh, "get_write_access");
246 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
250 /* Ok, now we can actually update the inode bitmaps.. */
251 spin_lock(sb_bgl_lock(sbi, block_group));
252 cleared = ext4_clear_bit(bit, bitmap_bh->b_data);
253 spin_unlock(sb_bgl_lock(sbi, block_group));
255 ext4_error(sb, "ext4_free_inode",
256 "bit already cleared for inode %lu", ino);
258 gdp = ext4_get_group_desc(sb, block_group, &bh2);
260 BUFFER_TRACE(bh2, "get_write_access");
261 fatal = ext4_journal_get_write_access(handle, bh2);
262 if (fatal) goto error_return;
265 spin_lock(sb_bgl_lock(sbi, block_group));
266 count = ext4_free_inodes_count(sb, gdp) + 1;
267 ext4_free_inodes_set(sb, gdp, count);
269 count = ext4_used_dirs_count(sb, gdp) - 1;
270 ext4_used_dirs_set(sb, gdp, count);
272 gdp->bg_checksum = ext4_group_desc_csum(sbi,
274 spin_unlock(sb_bgl_lock(sbi, block_group));
275 percpu_counter_inc(&sbi->s_freeinodes_counter);
277 percpu_counter_dec(&sbi->s_dirs_counter);
279 if (sbi->s_log_groups_per_flex) {
280 flex_group = ext4_flex_group(sbi, block_group);
281 spin_lock(sb_bgl_lock(sbi, flex_group));
282 sbi->s_flex_groups[flex_group].free_inodes++;
283 spin_unlock(sb_bgl_lock(sbi, flex_group));
286 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
287 err = ext4_handle_dirty_metadata(handle, NULL, bh2);
288 if (!fatal) fatal = err;
290 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
291 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
297 ext4_std_error(sb, fatal);
301 * There are two policies for allocating an inode. If the new inode is
302 * a directory, then a forward search is made for a block group with both
303 * free space and a low directory-to-inode ratio; if that fails, then of
304 * the groups with above-average free space, that group with the fewest
305 * directories already is chosen.
307 * For other inodes, search forward from the parent directory\'s block
308 * group to find a free inode.
310 static int find_group_dir(struct super_block *sb, struct inode *parent,
311 ext4_group_t *best_group)
313 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
314 unsigned int freei, avefreei;
315 struct ext4_group_desc *desc, *best_desc = NULL;
319 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
320 avefreei = freei / ngroups;
322 for (group = 0; group < ngroups; group++) {
323 desc = ext4_get_group_desc(sb, group, NULL);
324 if (!desc || !ext4_free_inodes_count(sb, desc))
326 if (ext4_free_inodes_count(sb, desc) < avefreei)
329 (ext4_free_blks_count(sb, desc) >
330 ext4_free_blks_count(sb, best_desc))) {
339 #define free_block_ratio 10
341 static int find_group_flex(struct super_block *sb, struct inode *parent,
342 ext4_group_t *best_group)
344 struct ext4_sb_info *sbi = EXT4_SB(sb);
345 struct ext4_group_desc *desc;
346 struct buffer_head *bh;
347 struct flex_groups *flex_group = sbi->s_flex_groups;
348 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
349 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
350 ext4_group_t ngroups = sbi->s_groups_count;
351 int flex_size = ext4_flex_bg_size(sbi);
352 ext4_group_t best_flex = parent_fbg_group;
353 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
354 int flexbg_free_blocks;
355 int flex_freeb_ratio;
356 ext4_group_t n_fbg_groups;
359 n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
360 sbi->s_log_groups_per_flex;
362 find_close_to_parent:
363 flexbg_free_blocks = flex_group[best_flex].free_blocks;
364 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
365 if (flex_group[best_flex].free_inodes &&
366 flex_freeb_ratio > free_block_ratio)
369 if (best_flex && best_flex == parent_fbg_group) {
371 goto find_close_to_parent;
374 for (i = 0; i < n_fbg_groups; i++) {
375 if (i == parent_fbg_group || i == parent_fbg_group - 1)
378 flexbg_free_blocks = flex_group[i].free_blocks;
379 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
381 if (flex_freeb_ratio > free_block_ratio &&
382 flex_group[i].free_inodes) {
387 if (flex_group[best_flex].free_inodes == 0 ||
388 (flex_group[i].free_blocks >
389 flex_group[best_flex].free_blocks &&
390 flex_group[i].free_inodes))
394 if (!flex_group[best_flex].free_inodes ||
395 !flex_group[best_flex].free_blocks)
399 for (i = best_flex * flex_size; i < ngroups &&
400 i < (best_flex + 1) * flex_size; i++) {
401 desc = ext4_get_group_desc(sb, i, &bh);
402 if (ext4_free_inodes_count(sb, desc)) {
414 * Orlov's allocator for directories.
416 * We always try to spread first-level directories.
418 * If there are blockgroups with both free inodes and free blocks counts
419 * not worse than average we return one with smallest directory count.
420 * Otherwise we simply return a random group.
422 * For the rest rules look so:
424 * It's OK to put directory into a group unless
425 * it has too many directories already (max_dirs) or
426 * it has too few free inodes left (min_inodes) or
427 * it has too few free blocks left (min_blocks) or
428 * it's already running too large debt (max_debt).
429 * Parent's group is preferred, if it doesn't satisfy these
430 * conditions we search cyclically through the rest. If none
431 * of the groups look good we just look for a group with more
432 * free inodes than average (starting at parent's group).
434 * Debt is incremented each time we allocate a directory and decremented
435 * when we allocate an inode, within 0--255.
438 #define INODE_COST 64
439 #define BLOCK_COST 256
441 static int find_group_orlov(struct super_block *sb, struct inode *parent,
444 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
445 struct ext4_sb_info *sbi = EXT4_SB(sb);
446 struct ext4_super_block *es = sbi->s_es;
447 ext4_group_t ngroups = sbi->s_groups_count;
448 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
449 unsigned int freei, avefreei;
450 ext4_fsblk_t freeb, avefreeb;
451 ext4_fsblk_t blocks_per_dir;
453 int max_debt, max_dirs, min_inodes;
454 ext4_grpblk_t min_blocks;
456 struct ext4_group_desc *desc;
458 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
459 avefreei = freei / ngroups;
460 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
462 do_div(avefreeb, ngroups);
463 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
465 if ((parent == sb->s_root->d_inode) ||
466 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
467 int best_ndir = inodes_per_group;
471 get_random_bytes(&grp, sizeof(grp));
472 parent_group = (unsigned)grp % ngroups;
473 for (i = 0; i < ngroups; i++) {
474 grp = (parent_group + i) % ngroups;
475 desc = ext4_get_group_desc(sb, grp, NULL);
476 if (!desc || !ext4_free_inodes_count(sb, desc))
478 if (ext4_used_dirs_count(sb, desc) >= best_ndir)
480 if (ext4_free_inodes_count(sb, desc) < avefreei)
482 if (ext4_free_blks_count(sb, desc) < avefreeb)
486 best_ndir = ext4_used_dirs_count(sb, desc);
493 blocks_per_dir = ext4_blocks_count(es) - freeb;
494 do_div(blocks_per_dir, ndirs);
496 max_dirs = ndirs / ngroups + inodes_per_group / 16;
497 min_inodes = avefreei - inodes_per_group / 4;
498 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
500 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
501 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
502 if (max_debt * INODE_COST > inodes_per_group)
503 max_debt = inodes_per_group / INODE_COST;
509 for (i = 0; i < ngroups; i++) {
510 *group = (parent_group + i) % ngroups;
511 desc = ext4_get_group_desc(sb, *group, NULL);
512 if (!desc || !ext4_free_inodes_count(sb, desc))
514 if (ext4_used_dirs_count(sb, desc) >= max_dirs)
516 if (ext4_free_inodes_count(sb, desc) < min_inodes)
518 if (ext4_free_blks_count(sb, desc) < min_blocks)
524 for (i = 0; i < ngroups; i++) {
525 *group = (parent_group + i) % ngroups;
526 desc = ext4_get_group_desc(sb, *group, NULL);
527 if (desc && ext4_free_inodes_count(sb, desc) &&
528 ext4_free_inodes_count(sb, desc) >= avefreei)
534 * The free-inodes counter is approximate, and for really small
535 * filesystems the above test can fail to find any blockgroups
544 static int find_group_other(struct super_block *sb, struct inode *parent,
547 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
548 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
549 struct ext4_group_desc *desc;
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_blks_count(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_blks_count(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 sb_bgl_lock
605 * and clear the uninit flag. The inode bitmap update
606 * and group desc uninit flag clear should be done
607 * after holding sb_bgl_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_desc *gdp = ext4_get_group_desc(sb, group, NULL);
618 spin_lock(sb_bgl_lock(sbi, group));
619 if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
620 /* not a free inode */
625 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
626 ino > EXT4_INODES_PER_GROUP(sb)) {
627 spin_unlock(sb_bgl_lock(sbi, group));
628 ext4_error(sb, __func__,
629 "reserved inode or inode > inodes count - "
630 "block_group = %u, inode=%lu", group,
631 ino + group * EXT4_INODES_PER_GROUP(sb));
634 /* If we didn't allocate from within the initialized part of the inode
635 * table then we need to initialize up to this inode. */
636 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
638 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
639 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
640 /* When marking the block group with
641 * ~EXT4_BG_INODE_UNINIT we don't want to depend
642 * on the value of bg_itable_unused even though
643 * mke2fs could have initialized the same for us.
644 * Instead we calculated the value below
649 free = EXT4_INODES_PER_GROUP(sb) -
650 ext4_itable_unused_count(sb, gdp);
654 * Check the relative inode number against the last used
655 * relative inode number in this group. if it is greater
656 * we need to update the bg_itable_unused count
660 ext4_itable_unused_set(sb, gdp,
661 (EXT4_INODES_PER_GROUP(sb) - ino));
663 count = ext4_free_inodes_count(sb, gdp) - 1;
664 ext4_free_inodes_set(sb, gdp, count);
666 count = ext4_used_dirs_count(sb, gdp) + 1;
667 ext4_used_dirs_set(sb, gdp, count);
669 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
671 spin_unlock(sb_bgl_lock(sbi, group));
676 * There are two policies for allocating an inode. If the new inode is
677 * a directory, then a forward search is made for a block group with both
678 * free space and a low directory-to-inode ratio; if that fails, then of
679 * the groups with above-average free space, that group with the fewest
680 * directories already is chosen.
682 * For other inodes, search forward from the parent directory's block
683 * group to find a free inode.
685 struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
687 struct super_block *sb;
688 struct buffer_head *inode_bitmap_bh = NULL;
689 struct buffer_head *group_desc_bh;
690 ext4_group_t group = 0;
691 unsigned long ino = 0;
693 struct ext4_group_desc *gdp = NULL;
694 struct ext4_super_block *es;
695 struct ext4_inode_info *ei;
696 struct ext4_sb_info *sbi;
702 ext4_group_t flex_group;
704 /* Cannot create files in a deleted directory */
705 if (!dir || !dir->i_nlink)
706 return ERR_PTR(-EPERM);
709 trace_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id,
711 inode = new_inode(sb);
713 return ERR_PTR(-ENOMEM);
719 if (sbi->s_log_groups_per_flex) {
720 ret2 = find_group_flex(sb, dir, &group);
722 ret2 = find_group_other(sb, dir, &group);
723 if (ret2 == 0 && once)
725 printk(KERN_NOTICE "ext4: find_group_flex "
726 "failed, fallback succeeded dir %lu\n",
733 if (test_opt(sb, OLDALLOC))
734 ret2 = find_group_dir(sb, dir, &group);
736 ret2 = find_group_orlov(sb, dir, &group);
738 ret2 = find_group_other(sb, dir, &group);
745 for (i = 0; i < sbi->s_groups_count; i++) {
748 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
752 brelse(inode_bitmap_bh);
753 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
754 if (!inode_bitmap_bh)
759 repeat_in_this_group:
760 ino = ext4_find_next_zero_bit((unsigned long *)
761 inode_bitmap_bh->b_data,
762 EXT4_INODES_PER_GROUP(sb), ino);
764 if (ino < EXT4_INODES_PER_GROUP(sb)) {
766 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
767 err = ext4_journal_get_write_access(handle,
772 BUFFER_TRACE(group_desc_bh, "get_write_access");
773 err = ext4_journal_get_write_access(handle,
777 if (!ext4_claim_inode(sb, inode_bitmap_bh,
780 BUFFER_TRACE(inode_bitmap_bh,
781 "call ext4_handle_dirty_metadata");
782 err = ext4_handle_dirty_metadata(handle,
787 /* zero bit is inode number 1*/
792 ext4_handle_release_buffer(handle, inode_bitmap_bh);
793 ext4_handle_release_buffer(handle, group_desc_bh);
795 if (++ino < EXT4_INODES_PER_GROUP(sb))
796 goto repeat_in_this_group;
800 * This case is possible in concurrent environment. It is very
801 * rare. We cannot repeat the find_group_xxx() call because
802 * that will simply return the same blockgroup, because the
803 * group descriptor metadata has not yet been updated.
804 * So we just go onto the next blockgroup.
806 if (++group == sbi->s_groups_count)
813 /* We may have to initialize the block bitmap if it isn't already */
814 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
815 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
816 struct buffer_head *block_bitmap_bh;
818 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
819 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
820 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
822 brelse(block_bitmap_bh);
827 spin_lock(sb_bgl_lock(sbi, group));
828 /* recheck and clear flag under lock if we still need to */
829 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
830 free = ext4_free_blocks_after_init(sb, group, gdp);
831 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
832 ext4_free_blks_set(sb, gdp, free);
833 gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
836 spin_unlock(sb_bgl_lock(sbi, group));
838 /* Don't need to dirty bitmap block if we didn't change it */
840 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
841 err = ext4_handle_dirty_metadata(handle,
842 NULL, block_bitmap_bh);
845 brelse(block_bitmap_bh);
849 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
850 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
854 percpu_counter_dec(&sbi->s_freeinodes_counter);
856 percpu_counter_inc(&sbi->s_dirs_counter);
859 if (sbi->s_log_groups_per_flex) {
860 flex_group = ext4_flex_group(sbi, group);
861 spin_lock(sb_bgl_lock(sbi, flex_group));
862 sbi->s_flex_groups[flex_group].free_inodes--;
863 spin_unlock(sb_bgl_lock(sbi, flex_group));
866 inode->i_uid = current_fsuid();
867 if (test_opt(sb, GRPID))
868 inode->i_gid = dir->i_gid;
869 else if (dir->i_mode & S_ISGID) {
870 inode->i_gid = dir->i_gid;
874 inode->i_gid = current_fsgid();
875 inode->i_mode = 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;
888 * Don't inherit extent flag from directory, amongst others. We set
889 * extent flag on newly created directory and file only if -o extent
890 * mount option is specified
893 ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
896 ei->i_block_group = group;
898 ext4_set_inode_flags(inode);
899 if (IS_DIRSYNC(inode))
900 ext4_handle_sync(handle);
901 if (insert_inode_locked(inode) < 0) {
905 spin_lock(&sbi->s_next_gen_lock);
906 inode->i_generation = sbi->s_next_generation++;
907 spin_unlock(&sbi->s_next_gen_lock);
909 ei->i_state = EXT4_STATE_NEW;
911 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
914 if (vfs_dq_alloc_inode(inode)) {
919 err = ext4_init_acl(handle, inode, dir);
923 err = ext4_init_security(handle, inode, dir);
927 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
928 /* set extent flag only for directory, file and normal symlink*/
929 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
930 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
931 ext4_ext_tree_init(handle, inode);
935 err = ext4_mark_inode_dirty(handle, inode);
937 ext4_std_error(sb, err);
941 ext4_debug("allocating inode %lu\n", inode->i_ino);
942 trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d",
943 sb->s_id, inode->i_ino, dir->i_ino, mode);
946 ext4_std_error(sb, err);
951 brelse(inode_bitmap_bh);
955 vfs_dq_free_inode(inode);
959 inode->i_flags |= S_NOQUOTA;
961 unlock_new_inode(inode);
963 brelse(inode_bitmap_bh);
967 /* Verify that we are loading a valid orphan from disk */
968 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
970 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
971 ext4_group_t block_group;
973 struct buffer_head *bitmap_bh;
974 struct inode *inode = NULL;
977 /* Error cases - e2fsck has already cleaned up for us */
979 ext4_warning(sb, __func__,
980 "bad orphan ino %lu! e2fsck was run?", ino);
984 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
985 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
986 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
988 ext4_warning(sb, __func__,
989 "inode bitmap error for orphan %lu", ino);
993 /* Having the inode bit set should be a 100% indicator that this
994 * is a valid orphan (no e2fsck run on fs). Orphans also include
995 * inodes that were being truncated, so we can't check i_nlink==0.
997 if (!ext4_test_bit(bit, bitmap_bh->b_data))
1000 inode = ext4_iget(sb, ino);
1005 * If the orphans has i_nlinks > 0 then it should be able to be
1006 * truncated, otherwise it won't be removed from the orphan list
1007 * during processing and an infinite loop will result.
1009 if (inode->i_nlink && !ext4_can_truncate(inode))
1012 if (NEXT_ORPHAN(inode) > max_ino)
1018 err = PTR_ERR(inode);
1021 ext4_warning(sb, __func__,
1022 "bad orphan inode %lu! e2fsck was run?", ino);
1023 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1024 bit, (unsigned long long)bitmap_bh->b_blocknr,
1025 ext4_test_bit(bit, bitmap_bh->b_data));
1026 printk(KERN_NOTICE "inode=%p\n", inode);
1028 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
1029 is_bad_inode(inode));
1030 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
1031 NEXT_ORPHAN(inode));
1032 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
1033 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
1034 /* Avoid freeing blocks if we got a bad deleted inode */
1035 if (inode->i_nlink == 0)
1036 inode->i_blocks = 0;
1041 return ERR_PTR(err);
1044 unsigned long ext4_count_free_inodes(struct super_block *sb)
1046 unsigned long desc_count;
1047 struct ext4_group_desc *gdp;
1050 struct ext4_super_block *es;
1051 unsigned long bitmap_count, x;
1052 struct buffer_head *bitmap_bh = NULL;
1054 es = EXT4_SB(sb)->s_es;
1058 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1059 gdp = ext4_get_group_desc(sb, i, NULL);
1062 desc_count += ext4_free_inodes_count(sb, gdp);
1064 bitmap_bh = ext4_read_inode_bitmap(sb, i);
1068 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
1069 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1070 i, ext4_free_inodes_count(sb, gdp), x);
1074 printk(KERN_DEBUG "ext4_count_free_inodes: "
1075 "stored = %u, computed = %lu, %lu\n",
1076 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1080 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1081 gdp = ext4_get_group_desc(sb, i, NULL);
1084 desc_count += ext4_free_inodes_count(sb, gdp);
1091 /* Called at mount-time, super-block is locked */
1092 unsigned long ext4_count_dirs(struct super_block * sb)
1094 unsigned long count = 0;
1097 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1098 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1101 count += ext4_used_dirs_count(sb, gdp);