2 * linux/fs/ext4/ialloc.c
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
9 * BSD ufs-inspired inode and directory allocation by
10 * Stephen Tweedie (sct@redhat.com), 1993
11 * Big-endian to little-endian byte-swapping/bitmaps by
12 * David S. Miller (davem@caip.rutgers.edu), 1995
15 #include <linux/time.h>
17 #include <linux/jbd2.h>
18 #include <linux/stat.h>
19 #include <linux/string.h>
20 #include <linux/quotaops.h>
21 #include <linux/buffer_head.h>
22 #include <linux/random.h>
23 #include <linux/bitops.h>
24 #include <linux/blkdev.h>
25 #include <asm/byteorder.h>
27 #include "ext4_jbd2.h"
33 * ialloc.c contains the inodes allocation and deallocation routines
37 * The free inodes are managed by bitmaps. A file system contains several
38 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
39 * block for inodes, N blocks for the inode table and data blocks.
41 * The file system contains group descriptors which are located after the
42 * super block. Each descriptor contains the number of the bitmap block and
43 * the free blocks count in the block.
47 * To avoid calling the atomic setbit hundreds or thousands of times, we only
48 * need to use it within a single byte (to ensure we get endianness right).
49 * We can use memset for the rest of the bitmap as there are no other users.
51 void mark_bitmap_end(int start_bit
, int end_bit
, char *bitmap
)
55 if (start_bit
>= end_bit
)
58 ext4_debug("mark end bits +%d through +%d used\n", start_bit
, end_bit
);
59 for (i
= start_bit
; i
< ((start_bit
+ 7) & ~7UL); i
++)
60 ext4_set_bit(i
, bitmap
);
62 memset(bitmap
+ (i
>> 3), 0xff, (end_bit
- i
) >> 3);
65 /* Initializes an uninitialized inode bitmap */
66 unsigned ext4_init_inode_bitmap(struct super_block
*sb
, struct buffer_head
*bh
,
67 ext4_group_t block_group
,
68 struct ext4_group_desc
*gdp
)
70 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
72 J_ASSERT_BH(bh
, buffer_locked(bh
));
74 /* If checksum is bad mark all blocks and inodes use to prevent
75 * allocation, essentially implementing a per-group read-only flag. */
76 if (!ext4_group_desc_csum_verify(sbi
, block_group
, gdp
)) {
77 ext4_error(sb
, __func__
, "Checksum bad for group %lu\n",
79 gdp
->bg_free_blocks_count
= 0;
80 gdp
->bg_free_inodes_count
= 0;
81 gdp
->bg_itable_unused
= 0;
82 memset(bh
->b_data
, 0xff, sb
->s_blocksize
);
86 memset(bh
->b_data
, 0, (EXT4_INODES_PER_GROUP(sb
) + 7) / 8);
87 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb
), EXT4_BLOCKS_PER_GROUP(sb
),
90 return EXT4_INODES_PER_GROUP(sb
);
94 * Read the inode allocation bitmap for a given block_group, reading
95 * into the specified slot in the superblock's bitmap cache.
97 * Return buffer_head of bitmap on success or NULL.
99 static struct buffer_head
*
100 ext4_read_inode_bitmap(struct super_block
*sb
, ext4_group_t block_group
)
102 struct ext4_group_desc
*desc
;
103 struct buffer_head
*bh
= NULL
;
104 ext4_fsblk_t bitmap_blk
;
106 desc
= ext4_get_group_desc(sb
, block_group
, NULL
);
109 bitmap_blk
= ext4_inode_bitmap(sb
, desc
);
110 bh
= sb_getblk(sb
, bitmap_blk
);
112 ext4_error(sb
, __func__
,
113 "Cannot read inode bitmap - "
114 "block_group = %lu, inode_bitmap = %llu",
115 block_group
, bitmap_blk
);
118 if (bh_uptodate_or_lock(bh
))
121 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_INODE_UNINIT
)) {
122 ext4_init_inode_bitmap(sb
, bh
, block_group
, desc
);
123 set_buffer_uptodate(bh
);
127 if (bh_submit_read(bh
) < 0) {
129 ext4_error(sb
, __func__
,
130 "Cannot read inode bitmap - "
131 "block_group = %lu, inode_bitmap = %llu",
132 block_group
, bitmap_blk
);
139 * NOTE! When we get the inode, we're the only people
140 * that have access to it, and as such there are no
141 * race conditions we have to worry about. The inode
142 * is not on the hash-lists, and it cannot be reached
143 * through the filesystem because the directory entry
144 * has been deleted earlier.
146 * HOWEVER: we must make sure that we get no aliases,
147 * which means that we have to call "clear_inode()"
148 * _before_ we mark the inode not in use in the inode
149 * bitmaps. Otherwise a newly created file might use
150 * the same inode number (not actually the same pointer
151 * though), and then we'd have two inodes sharing the
152 * same inode number and space on the harddisk.
154 void ext4_free_inode (handle_t
*handle
, struct inode
* inode
)
156 struct super_block
* sb
= inode
->i_sb
;
159 struct buffer_head
*bitmap_bh
= NULL
;
160 struct buffer_head
*bh2
;
161 ext4_group_t block_group
;
163 struct ext4_group_desc
* gdp
;
164 struct ext4_super_block
* es
;
165 struct ext4_sb_info
*sbi
;
167 ext4_group_t flex_group
;
169 if (atomic_read(&inode
->i_count
) > 1) {
170 printk ("ext4_free_inode: inode has count=%d\n",
171 atomic_read(&inode
->i_count
));
174 if (inode
->i_nlink
) {
175 printk ("ext4_free_inode: inode has nlink=%d\n",
180 printk("ext4_free_inode: inode on nonexistent device\n");
186 ext4_debug ("freeing inode %lu\n", ino
);
189 * Note: we must free any quota before locking the superblock,
190 * as writing the quota to disk may need the lock as well.
193 ext4_xattr_delete_inode(handle
, inode
);
194 DQUOT_FREE_INODE(inode
);
197 is_directory
= S_ISDIR(inode
->i_mode
);
199 /* Do this BEFORE marking the inode not in use or returning an error */
202 es
= EXT4_SB(sb
)->s_es
;
203 if (ino
< EXT4_FIRST_INO(sb
) || ino
> le32_to_cpu(es
->s_inodes_count
)) {
204 ext4_error (sb
, "ext4_free_inode",
205 "reserved or nonexistent inode %lu", ino
);
208 block_group
= (ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
209 bit
= (ino
- 1) % EXT4_INODES_PER_GROUP(sb
);
210 bitmap_bh
= ext4_read_inode_bitmap(sb
, block_group
);
214 BUFFER_TRACE(bitmap_bh
, "get_write_access");
215 fatal
= ext4_journal_get_write_access(handle
, bitmap_bh
);
219 /* Ok, now we can actually update the inode bitmaps.. */
220 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi
, block_group
),
221 bit
, bitmap_bh
->b_data
))
222 ext4_error (sb
, "ext4_free_inode",
223 "bit already cleared for inode %lu", ino
);
225 gdp
= ext4_get_group_desc (sb
, block_group
, &bh2
);
227 BUFFER_TRACE(bh2
, "get_write_access");
228 fatal
= ext4_journal_get_write_access(handle
, bh2
);
229 if (fatal
) goto error_return
;
232 spin_lock(sb_bgl_lock(sbi
, block_group
));
233 le16_add_cpu(&gdp
->bg_free_inodes_count
, 1);
235 le16_add_cpu(&gdp
->bg_used_dirs_count
, -1);
236 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
,
238 spin_unlock(sb_bgl_lock(sbi
, block_group
));
239 percpu_counter_inc(&sbi
->s_freeinodes_counter
);
241 percpu_counter_dec(&sbi
->s_dirs_counter
);
243 if (sbi
->s_log_groups_per_flex
) {
244 flex_group
= ext4_flex_group(sbi
, block_group
);
245 spin_lock(sb_bgl_lock(sbi
, flex_group
));
246 sbi
->s_flex_groups
[flex_group
].free_inodes
++;
247 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
250 BUFFER_TRACE(bh2
, "call ext4_journal_dirty_metadata");
251 err
= ext4_journal_dirty_metadata(handle
, bh2
);
252 if (!fatal
) fatal
= err
;
254 BUFFER_TRACE(bitmap_bh
, "call ext4_journal_dirty_metadata");
255 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
261 ext4_std_error(sb
, fatal
);
265 * There are two policies for allocating an inode. If the new inode is
266 * a directory, then a forward search is made for a block group with both
267 * free space and a low directory-to-inode ratio; if that fails, then of
268 * the groups with above-average free space, that group with the fewest
269 * directories already is chosen.
271 * For other inodes, search forward from the parent directory\'s block
272 * group to find a free inode.
274 static int find_group_dir(struct super_block
*sb
, struct inode
*parent
,
275 ext4_group_t
*best_group
)
277 ext4_group_t ngroups
= EXT4_SB(sb
)->s_groups_count
;
278 unsigned int freei
, avefreei
;
279 struct ext4_group_desc
*desc
, *best_desc
= NULL
;
283 freei
= percpu_counter_read_positive(&EXT4_SB(sb
)->s_freeinodes_counter
);
284 avefreei
= freei
/ ngroups
;
286 for (group
= 0; group
< ngroups
; group
++) {
287 desc
= ext4_get_group_desc (sb
, group
, NULL
);
288 if (!desc
|| !desc
->bg_free_inodes_count
)
290 if (le16_to_cpu(desc
->bg_free_inodes_count
) < avefreei
)
293 (le16_to_cpu(desc
->bg_free_blocks_count
) >
294 le16_to_cpu(best_desc
->bg_free_blocks_count
))) {
303 #define free_block_ratio 10
305 static int find_group_flex(struct super_block
*sb
, struct inode
*parent
,
306 ext4_group_t
*best_group
)
308 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
309 struct ext4_group_desc
*desc
;
310 struct buffer_head
*bh
;
311 struct flex_groups
*flex_group
= sbi
->s_flex_groups
;
312 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
313 ext4_group_t parent_fbg_group
= ext4_flex_group(sbi
, parent_group
);
314 ext4_group_t ngroups
= sbi
->s_groups_count
;
315 int flex_size
= ext4_flex_bg_size(sbi
);
316 ext4_group_t best_flex
= parent_fbg_group
;
317 int blocks_per_flex
= sbi
->s_blocks_per_group
* flex_size
;
318 int flexbg_free_blocks
;
319 int flex_freeb_ratio
;
320 ext4_group_t n_fbg_groups
;
323 n_fbg_groups
= (sbi
->s_groups_count
+ flex_size
- 1) >>
324 sbi
->s_log_groups_per_flex
;
326 find_close_to_parent
:
327 flexbg_free_blocks
= flex_group
[best_flex
].free_blocks
;
328 flex_freeb_ratio
= flexbg_free_blocks
* 100 / blocks_per_flex
;
329 if (flex_group
[best_flex
].free_inodes
&&
330 flex_freeb_ratio
> free_block_ratio
)
333 if (best_flex
&& best_flex
== parent_fbg_group
) {
335 goto find_close_to_parent
;
338 for (i
= 0; i
< n_fbg_groups
; i
++) {
339 if (i
== parent_fbg_group
|| i
== parent_fbg_group
- 1)
342 flexbg_free_blocks
= flex_group
[i
].free_blocks
;
343 flex_freeb_ratio
= flexbg_free_blocks
* 100 / blocks_per_flex
;
345 if (flex_freeb_ratio
> free_block_ratio
&&
346 flex_group
[i
].free_inodes
) {
352 (flex_group
[i
].free_blocks
>
353 flex_group
[best_flex
].free_blocks
&&
354 flex_group
[i
].free_inodes
))
358 if (!flex_group
[best_flex
].free_inodes
||
359 !flex_group
[best_flex
].free_blocks
)
363 for (i
= best_flex
* flex_size
; i
< ngroups
&&
364 i
< (best_flex
+ 1) * flex_size
; i
++) {
365 desc
= ext4_get_group_desc(sb
, i
, &bh
);
366 if (le16_to_cpu(desc
->bg_free_inodes_count
)) {
378 * Orlov's allocator for directories.
380 * We always try to spread first-level directories.
382 * If there are blockgroups with both free inodes and free blocks counts
383 * not worse than average we return one with smallest directory count.
384 * Otherwise we simply return a random group.
386 * For the rest rules look so:
388 * It's OK to put directory into a group unless
389 * it has too many directories already (max_dirs) or
390 * it has too few free inodes left (min_inodes) or
391 * it has too few free blocks left (min_blocks) or
392 * it's already running too large debt (max_debt).
393 * Parent's group is preferred, if it doesn't satisfy these
394 * conditions we search cyclically through the rest. If none
395 * of the groups look good we just look for a group with more
396 * free inodes than average (starting at parent's group).
398 * Debt is incremented each time we allocate a directory and decremented
399 * when we allocate an inode, within 0--255.
402 #define INODE_COST 64
403 #define BLOCK_COST 256
405 static int find_group_orlov(struct super_block
*sb
, struct inode
*parent
,
408 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
409 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
410 struct ext4_super_block
*es
= sbi
->s_es
;
411 ext4_group_t ngroups
= sbi
->s_groups_count
;
412 int inodes_per_group
= EXT4_INODES_PER_GROUP(sb
);
413 unsigned int freei
, avefreei
;
414 ext4_fsblk_t freeb
, avefreeb
;
415 ext4_fsblk_t blocks_per_dir
;
417 int max_debt
, max_dirs
, min_inodes
;
418 ext4_grpblk_t min_blocks
;
420 struct ext4_group_desc
*desc
;
422 freei
= percpu_counter_read_positive(&sbi
->s_freeinodes_counter
);
423 avefreei
= freei
/ ngroups
;
424 freeb
= percpu_counter_read_positive(&sbi
->s_freeblocks_counter
);
426 do_div(avefreeb
, ngroups
);
427 ndirs
= percpu_counter_read_positive(&sbi
->s_dirs_counter
);
429 if ((parent
== sb
->s_root
->d_inode
) ||
430 (EXT4_I(parent
)->i_flags
& EXT4_TOPDIR_FL
)) {
431 int best_ndir
= inodes_per_group
;
435 get_random_bytes(&grp
, sizeof(grp
));
436 parent_group
= (unsigned)grp
% ngroups
;
437 for (i
= 0; i
< ngroups
; i
++) {
438 grp
= (parent_group
+ i
) % ngroups
;
439 desc
= ext4_get_group_desc(sb
, grp
, NULL
);
440 if (!desc
|| !desc
->bg_free_inodes_count
)
442 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= best_ndir
)
444 if (le16_to_cpu(desc
->bg_free_inodes_count
) < avefreei
)
446 if (le16_to_cpu(desc
->bg_free_blocks_count
) < avefreeb
)
450 best_ndir
= le16_to_cpu(desc
->bg_used_dirs_count
);
457 blocks_per_dir
= ext4_blocks_count(es
) - freeb
;
458 do_div(blocks_per_dir
, ndirs
);
460 max_dirs
= ndirs
/ ngroups
+ inodes_per_group
/ 16;
461 min_inodes
= avefreei
- inodes_per_group
/ 4;
462 min_blocks
= avefreeb
- EXT4_BLOCKS_PER_GROUP(sb
) / 4;
464 max_debt
= EXT4_BLOCKS_PER_GROUP(sb
);
465 max_debt
/= max_t(int, blocks_per_dir
, BLOCK_COST
);
466 if (max_debt
* INODE_COST
> inodes_per_group
)
467 max_debt
= inodes_per_group
/ INODE_COST
;
473 for (i
= 0; i
< ngroups
; i
++) {
474 *group
= (parent_group
+ i
) % ngroups
;
475 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
476 if (!desc
|| !desc
->bg_free_inodes_count
)
478 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= max_dirs
)
480 if (le16_to_cpu(desc
->bg_free_inodes_count
) < min_inodes
)
482 if (le16_to_cpu(desc
->bg_free_blocks_count
) < min_blocks
)
488 for (i
= 0; i
< ngroups
; i
++) {
489 *group
= (parent_group
+ i
) % ngroups
;
490 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
491 if (desc
&& desc
->bg_free_inodes_count
&&
492 le16_to_cpu(desc
->bg_free_inodes_count
) >= avefreei
)
498 * The free-inodes counter is approximate, and for really small
499 * filesystems the above test can fail to find any blockgroups
508 static int find_group_other(struct super_block
*sb
, struct inode
*parent
,
511 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
512 ext4_group_t ngroups
= EXT4_SB(sb
)->s_groups_count
;
513 struct ext4_group_desc
*desc
;
517 * Try to place the inode in its parent directory
519 *group
= parent_group
;
520 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
521 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
522 le16_to_cpu(desc
->bg_free_blocks_count
))
526 * We're going to place this inode in a different blockgroup from its
527 * parent. We want to cause files in a common directory to all land in
528 * the same blockgroup. But we want files which are in a different
529 * directory which shares a blockgroup with our parent to land in a
530 * different blockgroup.
532 * So add our directory's i_ino into the starting point for the hash.
534 *group
= (*group
+ parent
->i_ino
) % ngroups
;
537 * Use a quadratic hash to find a group with a free inode and some free
540 for (i
= 1; i
< ngroups
; i
<<= 1) {
542 if (*group
>= ngroups
)
544 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
545 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
546 le16_to_cpu(desc
->bg_free_blocks_count
))
551 * That failed: try linear search for a free inode, even if that group
552 * has no free blocks.
554 *group
= parent_group
;
555 for (i
= 0; i
< ngroups
; i
++) {
556 if (++*group
>= ngroups
)
558 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
559 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
))
567 * There are two policies for allocating an inode. If the new inode is
568 * a directory, then a forward search is made for a block group with both
569 * free space and a low directory-to-inode ratio; if that fails, then of
570 * the groups with above-average free space, that group with the fewest
571 * directories already is chosen.
573 * For other inodes, search forward from the parent directory's block
574 * group to find a free inode.
576 struct inode
*ext4_new_inode(handle_t
*handle
, struct inode
* dir
, int mode
)
578 struct super_block
*sb
;
579 struct buffer_head
*bitmap_bh
= NULL
;
580 struct buffer_head
*bh2
;
581 ext4_group_t group
= 0;
582 unsigned long ino
= 0;
583 struct inode
* inode
;
584 struct ext4_group_desc
* gdp
= NULL
;
585 struct ext4_super_block
* es
;
586 struct ext4_inode_info
*ei
;
587 struct ext4_sb_info
*sbi
;
592 ext4_group_t flex_group
;
594 /* Cannot create files in a deleted directory */
595 if (!dir
|| !dir
->i_nlink
)
596 return ERR_PTR(-EPERM
);
599 inode
= new_inode(sb
);
601 return ERR_PTR(-ENOMEM
);
607 if (sbi
->s_log_groups_per_flex
) {
608 ret2
= find_group_flex(sb
, dir
, &group
);
613 if (test_opt (sb
, OLDALLOC
))
614 ret2
= find_group_dir(sb
, dir
, &group
);
616 ret2
= find_group_orlov(sb
, dir
, &group
);
618 ret2
= find_group_other(sb
, dir
, &group
);
625 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
628 gdp
= ext4_get_group_desc(sb
, group
, &bh2
);
633 bitmap_bh
= ext4_read_inode_bitmap(sb
, group
);
639 repeat_in_this_group
:
640 ino
= ext4_find_next_zero_bit((unsigned long *)
641 bitmap_bh
->b_data
, EXT4_INODES_PER_GROUP(sb
), ino
);
642 if (ino
< EXT4_INODES_PER_GROUP(sb
)) {
644 BUFFER_TRACE(bitmap_bh
, "get_write_access");
645 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
649 if (!ext4_set_bit_atomic(sb_bgl_lock(sbi
, group
),
650 ino
, bitmap_bh
->b_data
)) {
652 BUFFER_TRACE(bitmap_bh
,
653 "call ext4_journal_dirty_metadata");
654 err
= ext4_journal_dirty_metadata(handle
,
661 jbd2_journal_release_buffer(handle
, bitmap_bh
);
663 if (++ino
< EXT4_INODES_PER_GROUP(sb
))
664 goto repeat_in_this_group
;
668 * This case is possible in concurrent environment. It is very
669 * rare. We cannot repeat the find_group_xxx() call because
670 * that will simply return the same blockgroup, because the
671 * group descriptor metadata has not yet been updated.
672 * So we just go onto the next blockgroup.
674 if (++group
== sbi
->s_groups_count
)
682 if ((group
== 0 && ino
< EXT4_FIRST_INO(sb
)) ||
683 ino
> EXT4_INODES_PER_GROUP(sb
)) {
684 ext4_error(sb
, __func__
,
685 "reserved inode or inode > inodes count - "
686 "block_group = %lu, inode=%lu", group
,
687 ino
+ group
* EXT4_INODES_PER_GROUP(sb
));
692 BUFFER_TRACE(bh2
, "get_write_access");
693 err
= ext4_journal_get_write_access(handle
, bh2
);
696 /* We may have to initialize the block bitmap if it isn't already */
697 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
, EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) &&
698 gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
699 struct buffer_head
*block_bh
= ext4_read_block_bitmap(sb
, group
);
701 BUFFER_TRACE(block_bh
, "get block bitmap access");
702 err
= ext4_journal_get_write_access(handle
, block_bh
);
709 spin_lock(sb_bgl_lock(sbi
, group
));
710 /* recheck and clear flag under lock if we still need to */
711 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
712 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
713 free
= ext4_free_blocks_after_init(sb
, group
, gdp
);
714 gdp
->bg_free_blocks_count
= cpu_to_le16(free
);
716 spin_unlock(sb_bgl_lock(sbi
, group
));
718 /* Don't need to dirty bitmap block if we didn't change it */
720 BUFFER_TRACE(block_bh
, "dirty block bitmap");
721 err
= ext4_journal_dirty_metadata(handle
, block_bh
);
729 spin_lock(sb_bgl_lock(sbi
, group
));
730 /* If we didn't allocate from within the initialized part of the inode
731 * table then we need to initialize up to this inode. */
732 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
, EXT4_FEATURE_RO_COMPAT_GDT_CSUM
)) {
733 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_INODE_UNINIT
)) {
734 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_INODE_UNINIT
);
736 /* When marking the block group with
737 * ~EXT4_BG_INODE_UNINIT we don't want to depend
738 * on the value of bg_itable_unsed even though
739 * mke2fs could have initialized the same for us.
740 * Instead we calculated the value below
745 free
= EXT4_INODES_PER_GROUP(sb
) -
746 le16_to_cpu(gdp
->bg_itable_unused
);
750 * Check the relative inode number against the last used
751 * relative inode number in this group. if it is greater
752 * we need to update the bg_itable_unused count
756 gdp
->bg_itable_unused
=
757 cpu_to_le16(EXT4_INODES_PER_GROUP(sb
) - ino
);
760 le16_add_cpu(&gdp
->bg_free_inodes_count
, -1);
762 le16_add_cpu(&gdp
->bg_used_dirs_count
, 1);
764 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, group
, gdp
);
765 spin_unlock(sb_bgl_lock(sbi
, group
));
766 BUFFER_TRACE(bh2
, "call ext4_journal_dirty_metadata");
767 err
= ext4_journal_dirty_metadata(handle
, bh2
);
770 percpu_counter_dec(&sbi
->s_freeinodes_counter
);
772 percpu_counter_inc(&sbi
->s_dirs_counter
);
775 if (sbi
->s_log_groups_per_flex
) {
776 flex_group
= ext4_flex_group(sbi
, group
);
777 spin_lock(sb_bgl_lock(sbi
, flex_group
));
778 sbi
->s_flex_groups
[flex_group
].free_inodes
--;
779 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
782 inode
->i_uid
= current
->fsuid
;
783 if (test_opt (sb
, GRPID
))
784 inode
->i_gid
= dir
->i_gid
;
785 else if (dir
->i_mode
& S_ISGID
) {
786 inode
->i_gid
= dir
->i_gid
;
790 inode
->i_gid
= current
->fsgid
;
791 inode
->i_mode
= mode
;
793 inode
->i_ino
= ino
+ group
* EXT4_INODES_PER_GROUP(sb
);
794 /* This is the optimal IO size (for stat), not the fs block size */
796 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= ei
->i_crtime
=
797 ext4_current_time(inode
);
799 memset(ei
->i_data
, 0, sizeof(ei
->i_data
));
800 ei
->i_dir_start_lookup
= 0;
804 * Don't inherit extent flag from directory. We set extent flag on
805 * newly created directory and file only if -o extent mount option is
808 ei
->i_flags
= EXT4_I(dir
)->i_flags
& ~(EXT4_INDEX_FL
|EXT4_EXTENTS_FL
);
810 ei
->i_flags
&= ~(EXT4_IMMUTABLE_FL
|EXT4_APPEND_FL
);
811 /* dirsync only applies to directories */
813 ei
->i_flags
&= ~EXT4_DIRSYNC_FL
;
816 ei
->i_block_alloc_info
= NULL
;
817 ei
->i_block_group
= group
;
819 ext4_set_inode_flags(inode
);
820 if (IS_DIRSYNC(inode
))
822 insert_inode_hash(inode
);
823 spin_lock(&sbi
->s_next_gen_lock
);
824 inode
->i_generation
= sbi
->s_next_generation
++;
825 spin_unlock(&sbi
->s_next_gen_lock
);
827 ei
->i_state
= EXT4_STATE_NEW
;
829 ei
->i_extra_isize
= EXT4_SB(sb
)->s_want_extra_isize
;
832 if(DQUOT_ALLOC_INODE(inode
)) {
837 err
= ext4_init_acl(handle
, inode
, dir
);
841 err
= ext4_init_security(handle
,inode
, dir
);
845 if (test_opt(sb
, EXTENTS
)) {
846 /* set extent flag only for directory, file and normal symlink*/
847 if (S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
)) {
848 EXT4_I(inode
)->i_flags
|= EXT4_EXTENTS_FL
;
849 ext4_ext_tree_init(handle
, inode
);
853 err
= ext4_mark_inode_dirty(handle
, inode
);
855 ext4_std_error(sb
, err
);
859 ext4_debug("allocating inode %lu\n", inode
->i_ino
);
862 ext4_std_error(sb
, err
);
871 DQUOT_FREE_INODE(inode
);
875 inode
->i_flags
|= S_NOQUOTA
;
882 /* Verify that we are loading a valid orphan from disk */
883 struct inode
*ext4_orphan_get(struct super_block
*sb
, unsigned long ino
)
885 unsigned long max_ino
= le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
);
886 ext4_group_t block_group
;
888 struct buffer_head
*bitmap_bh
;
889 struct inode
*inode
= NULL
;
892 /* Error cases - e2fsck has already cleaned up for us */
894 ext4_warning(sb
, __func__
,
895 "bad orphan ino %lu! e2fsck was run?", ino
);
899 block_group
= (ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
900 bit
= (ino
- 1) % EXT4_INODES_PER_GROUP(sb
);
901 bitmap_bh
= ext4_read_inode_bitmap(sb
, block_group
);
903 ext4_warning(sb
, __func__
,
904 "inode bitmap error for orphan %lu", ino
);
908 /* Having the inode bit set should be a 100% indicator that this
909 * is a valid orphan (no e2fsck run on fs). Orphans also include
910 * inodes that were being truncated, so we can't check i_nlink==0.
912 if (!ext4_test_bit(bit
, bitmap_bh
->b_data
))
915 inode
= ext4_iget(sb
, ino
);
920 * If the orphans has i_nlinks > 0 then it should be able to be
921 * truncated, otherwise it won't be removed from the orphan list
922 * during processing and an infinite loop will result.
924 if (inode
->i_nlink
&& !ext4_can_truncate(inode
))
927 if (NEXT_ORPHAN(inode
) > max_ino
)
933 err
= PTR_ERR(inode
);
936 ext4_warning(sb
, __func__
,
937 "bad orphan inode %lu! e2fsck was run?", ino
);
938 printk(KERN_NOTICE
"ext4_test_bit(bit=%d, block=%llu) = %d\n",
939 bit
, (unsigned long long)bitmap_bh
->b_blocknr
,
940 ext4_test_bit(bit
, bitmap_bh
->b_data
));
941 printk(KERN_NOTICE
"inode=%p\n", inode
);
943 printk(KERN_NOTICE
"is_bad_inode(inode)=%d\n",
944 is_bad_inode(inode
));
945 printk(KERN_NOTICE
"NEXT_ORPHAN(inode)=%u\n",
947 printk(KERN_NOTICE
"max_ino=%lu\n", max_ino
);
948 printk(KERN_NOTICE
"i_nlink=%u\n", inode
->i_nlink
);
949 /* Avoid freeing blocks if we got a bad deleted inode */
950 if (inode
->i_nlink
== 0)
959 unsigned long ext4_count_free_inodes (struct super_block
* sb
)
961 unsigned long desc_count
;
962 struct ext4_group_desc
*gdp
;
965 struct ext4_super_block
*es
;
966 unsigned long bitmap_count
, x
;
967 struct buffer_head
*bitmap_bh
= NULL
;
969 es
= EXT4_SB(sb
)->s_es
;
973 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
974 gdp
= ext4_get_group_desc (sb
, i
, NULL
);
977 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
979 bitmap_bh
= ext4_read_inode_bitmap(sb
, i
);
983 x
= ext4_count_free(bitmap_bh
, EXT4_INODES_PER_GROUP(sb
) / 8);
984 printk(KERN_DEBUG
"group %lu: stored = %d, counted = %lu\n",
985 i
, le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
989 printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
990 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
994 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
995 gdp
= ext4_get_group_desc (sb
, i
, NULL
);
998 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
1005 /* Called at mount-time, super-block is locked */
1006 unsigned long ext4_count_dirs (struct super_block
* sb
)
1008 unsigned long count
= 0;
1011 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
1012 struct ext4_group_desc
*gdp
= ext4_get_group_desc (sb
, i
, NULL
);
1015 count
+= le16_to_cpu(gdp
->bg_used_dirs_count
);