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
), 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 = %lu, 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
);
132 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 = %lu, 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
, cleared
;
192 ext4_group_t flex_group
;
194 if (atomic_read(&inode
->i_count
) > 1) {
195 printk ("ext4_free_inode: inode has count=%d\n",
196 atomic_read(&inode
->i_count
));
199 if (inode
->i_nlink
) {
200 printk ("ext4_free_inode: inode has nlink=%d\n",
205 printk("ext4_free_inode: inode on nonexistent device\n");
211 ext4_debug ("freeing inode %lu\n", ino
);
214 * Note: we must free any quota before locking the superblock,
215 * as writing the quota to disk may need the lock as well.
218 ext4_xattr_delete_inode(handle
, inode
);
219 DQUOT_FREE_INODE(inode
);
222 is_directory
= S_ISDIR(inode
->i_mode
);
224 /* Do this BEFORE marking the inode not in use or returning an error */
227 es
= EXT4_SB(sb
)->s_es
;
228 if (ino
< EXT4_FIRST_INO(sb
) || ino
> le32_to_cpu(es
->s_inodes_count
)) {
229 ext4_error (sb
, "ext4_free_inode",
230 "reserved or nonexistent inode %lu", ino
);
233 block_group
= (ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
234 bit
= (ino
- 1) % EXT4_INODES_PER_GROUP(sb
);
235 bitmap_bh
= ext4_read_inode_bitmap(sb
, block_group
);
239 BUFFER_TRACE(bitmap_bh
, "get_write_access");
240 fatal
= ext4_journal_get_write_access(handle
, bitmap_bh
);
244 /* Ok, now we can actually update the inode bitmaps.. */
245 spin_lock(sb_bgl_lock(sbi
, block_group
));
246 cleared
= ext4_clear_bit(bit
, bitmap_bh
->b_data
);
247 spin_unlock(sb_bgl_lock(sbi
, block_group
));
249 ext4_error(sb
, "ext4_free_inode",
250 "bit already cleared for inode %lu", ino
);
252 gdp
= ext4_get_group_desc (sb
, block_group
, &bh2
);
254 BUFFER_TRACE(bh2
, "get_write_access");
255 fatal
= ext4_journal_get_write_access(handle
, bh2
);
256 if (fatal
) goto error_return
;
259 spin_lock(sb_bgl_lock(sbi
, block_group
));
260 le16_add_cpu(&gdp
->bg_free_inodes_count
, 1);
262 le16_add_cpu(&gdp
->bg_used_dirs_count
, -1);
263 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
,
265 spin_unlock(sb_bgl_lock(sbi
, block_group
));
266 percpu_counter_inc(&sbi
->s_freeinodes_counter
);
268 percpu_counter_dec(&sbi
->s_dirs_counter
);
270 if (sbi
->s_log_groups_per_flex
) {
271 flex_group
= ext4_flex_group(sbi
, block_group
);
272 spin_lock(sb_bgl_lock(sbi
, flex_group
));
273 sbi
->s_flex_groups
[flex_group
].free_inodes
++;
274 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
277 BUFFER_TRACE(bh2
, "call ext4_journal_dirty_metadata");
278 err
= ext4_journal_dirty_metadata(handle
, bh2
);
279 if (!fatal
) fatal
= err
;
281 BUFFER_TRACE(bitmap_bh
, "call ext4_journal_dirty_metadata");
282 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
288 ext4_std_error(sb
, fatal
);
292 * There are two policies for allocating an inode. If the new inode is
293 * a directory, then a forward search is made for a block group with both
294 * free space and a low directory-to-inode ratio; if that fails, then of
295 * the groups with above-average free space, that group with the fewest
296 * directories already is chosen.
298 * For other inodes, search forward from the parent directory\'s block
299 * group to find a free inode.
301 static int find_group_dir(struct super_block
*sb
, struct inode
*parent
,
302 ext4_group_t
*best_group
)
304 ext4_group_t ngroups
= EXT4_SB(sb
)->s_groups_count
;
305 unsigned int freei
, avefreei
;
306 struct ext4_group_desc
*desc
, *best_desc
= NULL
;
310 freei
= percpu_counter_read_positive(&EXT4_SB(sb
)->s_freeinodes_counter
);
311 avefreei
= freei
/ ngroups
;
313 for (group
= 0; group
< ngroups
; group
++) {
314 desc
= ext4_get_group_desc (sb
, group
, NULL
);
315 if (!desc
|| !desc
->bg_free_inodes_count
)
317 if (le16_to_cpu(desc
->bg_free_inodes_count
) < avefreei
)
320 (le16_to_cpu(desc
->bg_free_blocks_count
) >
321 le16_to_cpu(best_desc
->bg_free_blocks_count
))) {
330 #define free_block_ratio 10
332 static int find_group_flex(struct super_block
*sb
, struct inode
*parent
,
333 ext4_group_t
*best_group
)
335 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
336 struct ext4_group_desc
*desc
;
337 struct buffer_head
*bh
;
338 struct flex_groups
*flex_group
= sbi
->s_flex_groups
;
339 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
340 ext4_group_t parent_fbg_group
= ext4_flex_group(sbi
, parent_group
);
341 ext4_group_t ngroups
= sbi
->s_groups_count
;
342 int flex_size
= ext4_flex_bg_size(sbi
);
343 ext4_group_t best_flex
= parent_fbg_group
;
344 int blocks_per_flex
= sbi
->s_blocks_per_group
* flex_size
;
345 int flexbg_free_blocks
;
346 int flex_freeb_ratio
;
347 ext4_group_t n_fbg_groups
;
350 n_fbg_groups
= (sbi
->s_groups_count
+ flex_size
- 1) >>
351 sbi
->s_log_groups_per_flex
;
353 find_close_to_parent
:
354 flexbg_free_blocks
= flex_group
[best_flex
].free_blocks
;
355 flex_freeb_ratio
= flexbg_free_blocks
* 100 / blocks_per_flex
;
356 if (flex_group
[best_flex
].free_inodes
&&
357 flex_freeb_ratio
> free_block_ratio
)
360 if (best_flex
&& best_flex
== parent_fbg_group
) {
362 goto find_close_to_parent
;
365 for (i
= 0; i
< n_fbg_groups
; i
++) {
366 if (i
== parent_fbg_group
|| i
== parent_fbg_group
- 1)
369 flexbg_free_blocks
= flex_group
[i
].free_blocks
;
370 flex_freeb_ratio
= flexbg_free_blocks
* 100 / blocks_per_flex
;
372 if (flex_freeb_ratio
> free_block_ratio
&&
373 flex_group
[i
].free_inodes
) {
378 if (flex_group
[best_flex
].free_inodes
== 0 ||
379 (flex_group
[i
].free_blocks
>
380 flex_group
[best_flex
].free_blocks
&&
381 flex_group
[i
].free_inodes
))
385 if (!flex_group
[best_flex
].free_inodes
||
386 !flex_group
[best_flex
].free_blocks
)
390 for (i
= best_flex
* flex_size
; i
< ngroups
&&
391 i
< (best_flex
+ 1) * flex_size
; i
++) {
392 desc
= ext4_get_group_desc(sb
, i
, &bh
);
393 if (le16_to_cpu(desc
->bg_free_inodes_count
)) {
405 * Orlov's allocator for directories.
407 * We always try to spread first-level directories.
409 * If there are blockgroups with both free inodes and free blocks counts
410 * not worse than average we return one with smallest directory count.
411 * Otherwise we simply return a random group.
413 * For the rest rules look so:
415 * It's OK to put directory into a group unless
416 * it has too many directories already (max_dirs) or
417 * it has too few free inodes left (min_inodes) or
418 * it has too few free blocks left (min_blocks) or
419 * it's already running too large debt (max_debt).
420 * Parent's group is preferred, if it doesn't satisfy these
421 * conditions we search cyclically through the rest. If none
422 * of the groups look good we just look for a group with more
423 * free inodes than average (starting at parent's group).
425 * Debt is incremented each time we allocate a directory and decremented
426 * when we allocate an inode, within 0--255.
429 #define INODE_COST 64
430 #define BLOCK_COST 256
432 static int find_group_orlov(struct super_block
*sb
, struct inode
*parent
,
435 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
436 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
437 struct ext4_super_block
*es
= sbi
->s_es
;
438 ext4_group_t ngroups
= sbi
->s_groups_count
;
439 int inodes_per_group
= EXT4_INODES_PER_GROUP(sb
);
440 unsigned int freei
, avefreei
;
441 ext4_fsblk_t freeb
, avefreeb
;
442 ext4_fsblk_t blocks_per_dir
;
444 int max_debt
, max_dirs
, min_inodes
;
445 ext4_grpblk_t min_blocks
;
447 struct ext4_group_desc
*desc
;
449 freei
= percpu_counter_read_positive(&sbi
->s_freeinodes_counter
);
450 avefreei
= freei
/ ngroups
;
451 freeb
= percpu_counter_read_positive(&sbi
->s_freeblocks_counter
);
453 do_div(avefreeb
, ngroups
);
454 ndirs
= percpu_counter_read_positive(&sbi
->s_dirs_counter
);
456 if ((parent
== sb
->s_root
->d_inode
) ||
457 (EXT4_I(parent
)->i_flags
& EXT4_TOPDIR_FL
)) {
458 int best_ndir
= inodes_per_group
;
462 get_random_bytes(&grp
, sizeof(grp
));
463 parent_group
= (unsigned)grp
% ngroups
;
464 for (i
= 0; i
< ngroups
; i
++) {
465 grp
= (parent_group
+ i
) % ngroups
;
466 desc
= ext4_get_group_desc(sb
, grp
, NULL
);
467 if (!desc
|| !desc
->bg_free_inodes_count
)
469 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= best_ndir
)
471 if (le16_to_cpu(desc
->bg_free_inodes_count
) < avefreei
)
473 if (le16_to_cpu(desc
->bg_free_blocks_count
) < avefreeb
)
477 best_ndir
= le16_to_cpu(desc
->bg_used_dirs_count
);
484 blocks_per_dir
= ext4_blocks_count(es
) - freeb
;
485 do_div(blocks_per_dir
, ndirs
);
487 max_dirs
= ndirs
/ ngroups
+ inodes_per_group
/ 16;
488 min_inodes
= avefreei
- inodes_per_group
/ 4;
489 min_blocks
= avefreeb
- EXT4_BLOCKS_PER_GROUP(sb
) / 4;
491 max_debt
= EXT4_BLOCKS_PER_GROUP(sb
);
492 max_debt
/= max_t(int, blocks_per_dir
, BLOCK_COST
);
493 if (max_debt
* INODE_COST
> inodes_per_group
)
494 max_debt
= inodes_per_group
/ INODE_COST
;
500 for (i
= 0; i
< ngroups
; i
++) {
501 *group
= (parent_group
+ i
) % ngroups
;
502 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
503 if (!desc
|| !desc
->bg_free_inodes_count
)
505 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= max_dirs
)
507 if (le16_to_cpu(desc
->bg_free_inodes_count
) < min_inodes
)
509 if (le16_to_cpu(desc
->bg_free_blocks_count
) < min_blocks
)
515 for (i
= 0; i
< ngroups
; i
++) {
516 *group
= (parent_group
+ i
) % ngroups
;
517 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
518 if (desc
&& desc
->bg_free_inodes_count
&&
519 le16_to_cpu(desc
->bg_free_inodes_count
) >= avefreei
)
525 * The free-inodes counter is approximate, and for really small
526 * filesystems the above test can fail to find any blockgroups
535 static int find_group_other(struct super_block
*sb
, struct inode
*parent
,
538 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
539 ext4_group_t ngroups
= EXT4_SB(sb
)->s_groups_count
;
540 struct ext4_group_desc
*desc
;
544 * Try to place the inode in its parent directory
546 *group
= parent_group
;
547 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
548 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
549 le16_to_cpu(desc
->bg_free_blocks_count
))
553 * We're going to place this inode in a different blockgroup from its
554 * parent. We want to cause files in a common directory to all land in
555 * the same blockgroup. But we want files which are in a different
556 * directory which shares a blockgroup with our parent to land in a
557 * different blockgroup.
559 * So add our directory's i_ino into the starting point for the hash.
561 *group
= (*group
+ parent
->i_ino
) % ngroups
;
564 * Use a quadratic hash to find a group with a free inode and some free
567 for (i
= 1; i
< ngroups
; i
<<= 1) {
569 if (*group
>= ngroups
)
571 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
572 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
573 le16_to_cpu(desc
->bg_free_blocks_count
))
578 * That failed: try linear search for a free inode, even if that group
579 * has no free blocks.
581 *group
= parent_group
;
582 for (i
= 0; i
< ngroups
; i
++) {
583 if (++*group
>= ngroups
)
585 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
586 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
))
594 * claim the inode from the inode bitmap. If the group
595 * is uninit we need to take the groups's sb_bgl_lock
596 * and clear the uninit flag. The inode bitmap update
597 * and group desc uninit flag clear should be done
598 * after holding sb_bgl_lock so that ext4_read_inode_bitmap
599 * doesn't race with the ext4_claim_inode
601 static int ext4_claim_inode(struct super_block
*sb
,
602 struct buffer_head
*inode_bitmap_bh
,
603 unsigned long ino
, ext4_group_t group
, int mode
)
605 int free
= 0, retval
= 0;
606 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
607 struct ext4_group_desc
*gdp
= ext4_get_group_desc(sb
, group
, NULL
);
609 spin_lock(sb_bgl_lock(sbi
, group
));
610 if (ext4_set_bit(ino
, inode_bitmap_bh
->b_data
)) {
611 /* not a free inode */
616 if ((group
== 0 && ino
< EXT4_FIRST_INO(sb
)) ||
617 ino
> EXT4_INODES_PER_GROUP(sb
)) {
618 spin_unlock(sb_bgl_lock(sbi
, group
));
619 ext4_error(sb
, __func__
,
620 "reserved inode or inode > inodes count - "
621 "block_group = %lu, inode=%lu", group
,
622 ino
+ group
* EXT4_INODES_PER_GROUP(sb
));
625 /* If we didn't allocate from within the initialized part of the inode
626 * table then we need to initialize up to this inode. */
627 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
, EXT4_FEATURE_RO_COMPAT_GDT_CSUM
)) {
629 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_INODE_UNINIT
)) {
630 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_INODE_UNINIT
);
631 /* When marking the block group with
632 * ~EXT4_BG_INODE_UNINIT we don't want to depend
633 * on the value of bg_itable_unused even though
634 * mke2fs could have initialized the same for us.
635 * Instead we calculated the value below
640 free
= EXT4_INODES_PER_GROUP(sb
) -
641 le16_to_cpu(gdp
->bg_itable_unused
);
645 * Check the relative inode number against the last used
646 * relative inode number in this group. if it is greater
647 * we need to update the bg_itable_unused count
651 gdp
->bg_itable_unused
=
652 cpu_to_le16(EXT4_INODES_PER_GROUP(sb
) - ino
);
654 le16_add_cpu(&gdp
->bg_free_inodes_count
, -1);
656 le16_add_cpu(&gdp
->bg_used_dirs_count
, 1);
658 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, group
, gdp
);
660 spin_unlock(sb_bgl_lock(sbi
, group
));
665 * There are two policies for allocating an inode. If the new inode is
666 * a directory, then a forward search is made for a block group with both
667 * free space and a low directory-to-inode ratio; if that fails, then of
668 * the groups with above-average free space, that group with the fewest
669 * directories already is chosen.
671 * For other inodes, search forward from the parent directory's block
672 * group to find a free inode.
674 struct inode
*ext4_new_inode(handle_t
*handle
, struct inode
* dir
, int mode
)
676 struct super_block
*sb
;
677 struct buffer_head
*bitmap_bh
= NULL
;
678 struct buffer_head
*bh2
;
679 ext4_group_t group
= 0;
680 unsigned long ino
= 0;
681 struct inode
* inode
;
682 struct ext4_group_desc
* gdp
= NULL
;
683 struct ext4_super_block
* es
;
684 struct ext4_inode_info
*ei
;
685 struct ext4_sb_info
*sbi
;
691 ext4_group_t flex_group
;
693 /* Cannot create files in a deleted directory */
694 if (!dir
|| !dir
->i_nlink
)
695 return ERR_PTR(-EPERM
);
698 inode
= new_inode(sb
);
700 return ERR_PTR(-ENOMEM
);
706 if (sbi
->s_log_groups_per_flex
) {
707 ret2
= find_group_flex(sb
, dir
, &group
);
709 ret2
= find_group_other(sb
, dir
, &group
);
710 if (ret2
== 0 && once
) {
712 printk(KERN_NOTICE
"ext4: find_group_flex "
713 "failed, fallback succeeded dir %lu\n",
721 if (test_opt (sb
, OLDALLOC
))
722 ret2
= find_group_dir(sb
, dir
, &group
);
724 ret2
= find_group_orlov(sb
, dir
, &group
);
726 ret2
= find_group_other(sb
, dir
, &group
);
733 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
736 gdp
= ext4_get_group_desc(sb
, group
, &bh2
);
741 bitmap_bh
= ext4_read_inode_bitmap(sb
, group
);
747 repeat_in_this_group
:
748 ino
= ext4_find_next_zero_bit((unsigned long *)
749 bitmap_bh
->b_data
, EXT4_INODES_PER_GROUP(sb
), ino
);
750 if (ino
< EXT4_INODES_PER_GROUP(sb
)) {
752 BUFFER_TRACE(bitmap_bh
, "get_write_access");
753 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
757 BUFFER_TRACE(bh2
, "get_write_access");
758 err
= ext4_journal_get_write_access(handle
, bh2
);
761 if (!ext4_claim_inode(sb
, bitmap_bh
,
764 BUFFER_TRACE(bitmap_bh
,
765 "call ext4_journal_dirty_metadata");
766 err
= ext4_journal_dirty_metadata(handle
,
770 /* zero bit is inode number 1*/
775 jbd2_journal_release_buffer(handle
, bitmap_bh
);
776 jbd2_journal_release_buffer(handle
, bh2
);
778 if (++ino
< EXT4_INODES_PER_GROUP(sb
))
779 goto repeat_in_this_group
;
783 * This case is possible in concurrent environment. It is very
784 * rare. We cannot repeat the find_group_xxx() call because
785 * that will simply return the same blockgroup, because the
786 * group descriptor metadata has not yet been updated.
787 * So we just go onto the next blockgroup.
789 if (++group
== sbi
->s_groups_count
)
796 /* We may have to initialize the block bitmap if it isn't already */
797 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
, EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) &&
798 gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
799 struct buffer_head
*block_bh
= ext4_read_block_bitmap(sb
, group
);
801 BUFFER_TRACE(block_bh
, "get block bitmap access");
802 err
= ext4_journal_get_write_access(handle
, block_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 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
813 free
= ext4_free_blocks_after_init(sb
, group
, gdp
);
814 gdp
->bg_free_blocks_count
= cpu_to_le16(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_bh
, "dirty block bitmap");
823 err
= ext4_journal_dirty_metadata(handle
, block_bh
);
830 BUFFER_TRACE(bh2
, "call ext4_handle_dirty_metadata");
831 err
= ext4_journal_dirty_metadata(handle
, bh2
);
835 percpu_counter_dec(&sbi
->s_freeinodes_counter
);
837 percpu_counter_inc(&sbi
->s_dirs_counter
);
840 if (sbi
->s_log_groups_per_flex
) {
841 flex_group
= ext4_flex_group(sbi
, group
);
842 spin_lock(sb_bgl_lock(sbi
, flex_group
));
843 sbi
->s_flex_groups
[flex_group
].free_inodes
--;
844 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
847 inode
->i_uid
= current
->fsuid
;
848 if (test_opt (sb
, GRPID
))
849 inode
->i_gid
= dir
->i_gid
;
850 else if (dir
->i_mode
& S_ISGID
) {
851 inode
->i_gid
= dir
->i_gid
;
855 inode
->i_gid
= current
->fsgid
;
856 inode
->i_mode
= mode
;
858 inode
->i_ino
= ino
+ group
* EXT4_INODES_PER_GROUP(sb
);
859 /* This is the optimal IO size (for stat), not the fs block size */
861 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= ei
->i_crtime
=
862 ext4_current_time(inode
);
864 memset(ei
->i_data
, 0, sizeof(ei
->i_data
));
865 ei
->i_dir_start_lookup
= 0;
869 * Don't inherit extent flag from directory, amongst others. We set
870 * extent flag on newly created directory and file only if -o extent
871 * mount option is specified
874 ext4_mask_flags(mode
, EXT4_I(dir
)->i_flags
& EXT4_FL_INHERITED
);
877 ei
->i_block_alloc_info
= NULL
;
878 ei
->i_block_group
= group
;
880 ext4_set_inode_flags(inode
);
881 if (IS_DIRSYNC(inode
))
883 insert_inode_hash(inode
);
884 spin_lock(&sbi
->s_next_gen_lock
);
885 inode
->i_generation
= sbi
->s_next_generation
++;
886 spin_unlock(&sbi
->s_next_gen_lock
);
888 ei
->i_state
= EXT4_STATE_NEW
;
890 ei
->i_extra_isize
= EXT4_SB(sb
)->s_want_extra_isize
;
893 if(DQUOT_ALLOC_INODE(inode
)) {
898 err
= ext4_init_acl(handle
, inode
, dir
);
902 err
= ext4_init_security(handle
,inode
, dir
);
906 if (test_opt(sb
, EXTENTS
)) {
907 /* set extent flag only for directory, file and normal symlink*/
908 if (S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
)) {
909 EXT4_I(inode
)->i_flags
|= EXT4_EXTENTS_FL
;
910 ext4_ext_tree_init(handle
, inode
);
914 err
= ext4_mark_inode_dirty(handle
, inode
);
916 ext4_std_error(sb
, err
);
920 ext4_debug("allocating inode %lu\n", inode
->i_ino
);
923 ext4_std_error(sb
, err
);
932 DQUOT_FREE_INODE(inode
);
936 inode
->i_flags
|= S_NOQUOTA
;
943 /* Verify that we are loading a valid orphan from disk */
944 struct inode
*ext4_orphan_get(struct super_block
*sb
, unsigned long ino
)
946 unsigned long max_ino
= le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
);
947 ext4_group_t block_group
;
949 struct buffer_head
*bitmap_bh
;
950 struct inode
*inode
= NULL
;
953 /* Error cases - e2fsck has already cleaned up for us */
955 ext4_warning(sb
, __func__
,
956 "bad orphan ino %lu! e2fsck was run?", ino
);
960 block_group
= (ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
961 bit
= (ino
- 1) % EXT4_INODES_PER_GROUP(sb
);
962 bitmap_bh
= ext4_read_inode_bitmap(sb
, block_group
);
964 ext4_warning(sb
, __func__
,
965 "inode bitmap error for orphan %lu", ino
);
969 /* Having the inode bit set should be a 100% indicator that this
970 * is a valid orphan (no e2fsck run on fs). Orphans also include
971 * inodes that were being truncated, so we can't check i_nlink==0.
973 if (!ext4_test_bit(bit
, bitmap_bh
->b_data
))
976 inode
= ext4_iget(sb
, ino
);
981 * If the orphans has i_nlinks > 0 then it should be able to be
982 * truncated, otherwise it won't be removed from the orphan list
983 * during processing and an infinite loop will result.
985 if (inode
->i_nlink
&& !ext4_can_truncate(inode
))
988 if (NEXT_ORPHAN(inode
) > max_ino
)
994 err
= PTR_ERR(inode
);
997 ext4_warning(sb
, __func__
,
998 "bad orphan inode %lu! e2fsck was run?", ino
);
999 printk(KERN_NOTICE
"ext4_test_bit(bit=%d, block=%llu) = %d\n",
1000 bit
, (unsigned long long)bitmap_bh
->b_blocknr
,
1001 ext4_test_bit(bit
, bitmap_bh
->b_data
));
1002 printk(KERN_NOTICE
"inode=%p\n", inode
);
1004 printk(KERN_NOTICE
"is_bad_inode(inode)=%d\n",
1005 is_bad_inode(inode
));
1006 printk(KERN_NOTICE
"NEXT_ORPHAN(inode)=%u\n",
1007 NEXT_ORPHAN(inode
));
1008 printk(KERN_NOTICE
"max_ino=%lu\n", max_ino
);
1009 printk(KERN_NOTICE
"i_nlink=%u\n", inode
->i_nlink
);
1010 /* Avoid freeing blocks if we got a bad deleted inode */
1011 if (inode
->i_nlink
== 0)
1012 inode
->i_blocks
= 0;
1017 return ERR_PTR(err
);
1020 unsigned long ext4_count_free_inodes (struct super_block
* sb
)
1022 unsigned long desc_count
;
1023 struct ext4_group_desc
*gdp
;
1026 struct ext4_super_block
*es
;
1027 unsigned long bitmap_count
, x
;
1028 struct buffer_head
*bitmap_bh
= NULL
;
1030 es
= EXT4_SB(sb
)->s_es
;
1034 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
1035 gdp
= ext4_get_group_desc (sb
, i
, NULL
);
1038 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
1040 bitmap_bh
= ext4_read_inode_bitmap(sb
, i
);
1044 x
= ext4_count_free(bitmap_bh
, EXT4_INODES_PER_GROUP(sb
) / 8);
1045 printk(KERN_DEBUG
"group %lu: stored = %d, counted = %lu\n",
1046 i
, le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
1050 printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
1051 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
1055 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
1056 gdp
= ext4_get_group_desc (sb
, i
, NULL
);
1059 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
1066 /* Called at mount-time, super-block is locked */
1067 unsigned long ext4_count_dirs (struct super_block
* sb
)
1069 unsigned long count
= 0;
1072 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
1073 struct ext4_group_desc
*gdp
= ext4_get_group_desc (sb
, i
, NULL
);
1076 count
+= le16_to_cpu(gdp
->bg_used_dirs_count
);