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 spin_lock(sb_bgl_lock(EXT4_SB(sb
), block_group
));
122 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_INODE_UNINIT
)) {
123 ext4_init_inode_bitmap(sb
, bh
, block_group
, desc
);
124 set_buffer_uptodate(bh
);
126 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), block_group
));
129 spin_unlock(sb_bgl_lock(EXT4_SB(sb
), block_group
));
130 if (bh_submit_read(bh
) < 0) {
132 ext4_error(sb
, __func__
,
133 "Cannot read inode bitmap - "
134 "block_group = %lu, inode_bitmap = %llu",
135 block_group
, bitmap_blk
);
142 * NOTE! When we get the inode, we're the only people
143 * that have access to it, and as such there are no
144 * race conditions we have to worry about. The inode
145 * is not on the hash-lists, and it cannot be reached
146 * through the filesystem because the directory entry
147 * has been deleted earlier.
149 * HOWEVER: we must make sure that we get no aliases,
150 * which means that we have to call "clear_inode()"
151 * _before_ we mark the inode not in use in the inode
152 * bitmaps. Otherwise a newly created file might use
153 * the same inode number (not actually the same pointer
154 * though), and then we'd have two inodes sharing the
155 * same inode number and space on the harddisk.
157 void ext4_free_inode (handle_t
*handle
, struct inode
* inode
)
159 struct super_block
* sb
= inode
->i_sb
;
162 struct buffer_head
*bitmap_bh
= NULL
;
163 struct buffer_head
*bh2
;
164 ext4_group_t block_group
;
166 struct ext4_group_desc
* gdp
;
167 struct ext4_super_block
* es
;
168 struct ext4_sb_info
*sbi
;
170 ext4_group_t flex_group
;
172 if (atomic_read(&inode
->i_count
) > 1) {
173 printk(KERN_ERR
"ext4_free_inode: inode has count=%d\n",
174 atomic_read(&inode
->i_count
));
177 if (inode
->i_nlink
) {
178 printk(KERN_ERR
"ext4_free_inode: inode has nlink=%d\n",
183 printk(KERN_ERR
"ext4_free_inode: inode on "
184 "nonexistent device\n");
190 ext4_debug ("freeing inode %lu\n", ino
);
193 * Note: we must free any quota before locking the superblock,
194 * as writing the quota to disk may need the lock as well.
197 ext4_xattr_delete_inode(handle
, inode
);
198 DQUOT_FREE_INODE(inode
);
201 is_directory
= S_ISDIR(inode
->i_mode
);
203 /* Do this BEFORE marking the inode not in use or returning an error */
206 es
= EXT4_SB(sb
)->s_es
;
207 if (ino
< EXT4_FIRST_INO(sb
) || ino
> le32_to_cpu(es
->s_inodes_count
)) {
208 ext4_error (sb
, "ext4_free_inode",
209 "reserved or nonexistent inode %lu", ino
);
212 block_group
= (ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
213 bit
= (ino
- 1) % EXT4_INODES_PER_GROUP(sb
);
214 bitmap_bh
= ext4_read_inode_bitmap(sb
, block_group
);
218 BUFFER_TRACE(bitmap_bh
, "get_write_access");
219 fatal
= ext4_journal_get_write_access(handle
, bitmap_bh
);
223 /* Ok, now we can actually update the inode bitmaps.. */
224 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi
, block_group
),
225 bit
, bitmap_bh
->b_data
))
226 ext4_error (sb
, "ext4_free_inode",
227 "bit already cleared for inode %lu", ino
);
229 gdp
= ext4_get_group_desc (sb
, block_group
, &bh2
);
231 BUFFER_TRACE(bh2
, "get_write_access");
232 fatal
= ext4_journal_get_write_access(handle
, bh2
);
233 if (fatal
) goto error_return
;
236 spin_lock(sb_bgl_lock(sbi
, block_group
));
237 le16_add_cpu(&gdp
->bg_free_inodes_count
, 1);
239 le16_add_cpu(&gdp
->bg_used_dirs_count
, -1);
240 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
,
242 spin_unlock(sb_bgl_lock(sbi
, block_group
));
243 percpu_counter_inc(&sbi
->s_freeinodes_counter
);
245 percpu_counter_dec(&sbi
->s_dirs_counter
);
247 if (sbi
->s_log_groups_per_flex
) {
248 flex_group
= ext4_flex_group(sbi
, block_group
);
249 spin_lock(sb_bgl_lock(sbi
, flex_group
));
250 sbi
->s_flex_groups
[flex_group
].free_inodes
++;
251 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
254 BUFFER_TRACE(bh2
, "call ext4_journal_dirty_metadata");
255 err
= ext4_journal_dirty_metadata(handle
, bh2
);
256 if (!fatal
) fatal
= err
;
258 BUFFER_TRACE(bitmap_bh
, "call ext4_journal_dirty_metadata");
259 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
265 ext4_std_error(sb
, fatal
);
269 * There are two policies for allocating an inode. If the new inode is
270 * a directory, then a forward search is made for a block group with both
271 * free space and a low directory-to-inode ratio; if that fails, then of
272 * the groups with above-average free space, that group with the fewest
273 * directories already is chosen.
275 * For other inodes, search forward from the parent directory\'s block
276 * group to find a free inode.
278 static int find_group_dir(struct super_block
*sb
, struct inode
*parent
,
279 ext4_group_t
*best_group
)
281 ext4_group_t ngroups
= EXT4_SB(sb
)->s_groups_count
;
282 unsigned int freei
, avefreei
;
283 struct ext4_group_desc
*desc
, *best_desc
= NULL
;
287 freei
= percpu_counter_read_positive(&EXT4_SB(sb
)->s_freeinodes_counter
);
288 avefreei
= freei
/ ngroups
;
290 for (group
= 0; group
< ngroups
; group
++) {
291 desc
= ext4_get_group_desc (sb
, group
, NULL
);
292 if (!desc
|| !desc
->bg_free_inodes_count
)
294 if (le16_to_cpu(desc
->bg_free_inodes_count
) < avefreei
)
297 (le16_to_cpu(desc
->bg_free_blocks_count
) >
298 le16_to_cpu(best_desc
->bg_free_blocks_count
))) {
307 #define free_block_ratio 10
309 static int find_group_flex(struct super_block
*sb
, struct inode
*parent
,
310 ext4_group_t
*best_group
)
312 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
313 struct ext4_group_desc
*desc
;
314 struct buffer_head
*bh
;
315 struct flex_groups
*flex_group
= sbi
->s_flex_groups
;
316 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
317 ext4_group_t parent_fbg_group
= ext4_flex_group(sbi
, parent_group
);
318 ext4_group_t ngroups
= sbi
->s_groups_count
;
319 int flex_size
= ext4_flex_bg_size(sbi
);
320 ext4_group_t best_flex
= parent_fbg_group
;
321 int blocks_per_flex
= sbi
->s_blocks_per_group
* flex_size
;
322 int flexbg_free_blocks
;
323 int flex_freeb_ratio
;
324 ext4_group_t n_fbg_groups
;
327 n_fbg_groups
= (sbi
->s_groups_count
+ flex_size
- 1) >>
328 sbi
->s_log_groups_per_flex
;
330 find_close_to_parent
:
331 flexbg_free_blocks
= flex_group
[best_flex
].free_blocks
;
332 flex_freeb_ratio
= flexbg_free_blocks
* 100 / blocks_per_flex
;
333 if (flex_group
[best_flex
].free_inodes
&&
334 flex_freeb_ratio
> free_block_ratio
)
337 if (best_flex
&& best_flex
== parent_fbg_group
) {
339 goto find_close_to_parent
;
342 for (i
= 0; i
< n_fbg_groups
; i
++) {
343 if (i
== parent_fbg_group
|| i
== parent_fbg_group
- 1)
346 flexbg_free_blocks
= flex_group
[i
].free_blocks
;
347 flex_freeb_ratio
= flexbg_free_blocks
* 100 / blocks_per_flex
;
349 if (flex_freeb_ratio
> free_block_ratio
&&
350 flex_group
[i
].free_inodes
) {
355 if (flex_group
[best_flex
].free_inodes
== 0 ||
356 (flex_group
[i
].free_blocks
>
357 flex_group
[best_flex
].free_blocks
&&
358 flex_group
[i
].free_inodes
))
362 if (!flex_group
[best_flex
].free_inodes
||
363 !flex_group
[best_flex
].free_blocks
)
367 for (i
= best_flex
* flex_size
; i
< ngroups
&&
368 i
< (best_flex
+ 1) * flex_size
; i
++) {
369 desc
= ext4_get_group_desc(sb
, i
, &bh
);
370 if (le16_to_cpu(desc
->bg_free_inodes_count
)) {
382 * Orlov's allocator for directories.
384 * We always try to spread first-level directories.
386 * If there are blockgroups with both free inodes and free blocks counts
387 * not worse than average we return one with smallest directory count.
388 * Otherwise we simply return a random group.
390 * For the rest rules look so:
392 * It's OK to put directory into a group unless
393 * it has too many directories already (max_dirs) or
394 * it has too few free inodes left (min_inodes) or
395 * it has too few free blocks left (min_blocks) or
396 * it's already running too large debt (max_debt).
397 * Parent's group is preferred, if it doesn't satisfy these
398 * conditions we search cyclically through the rest. If none
399 * of the groups look good we just look for a group with more
400 * free inodes than average (starting at parent's group).
402 * Debt is incremented each time we allocate a directory and decremented
403 * when we allocate an inode, within 0--255.
406 #define INODE_COST 64
407 #define BLOCK_COST 256
409 static int find_group_orlov(struct super_block
*sb
, struct inode
*parent
,
412 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
413 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
414 struct ext4_super_block
*es
= sbi
->s_es
;
415 ext4_group_t ngroups
= sbi
->s_groups_count
;
416 int inodes_per_group
= EXT4_INODES_PER_GROUP(sb
);
417 unsigned int freei
, avefreei
;
418 ext4_fsblk_t freeb
, avefreeb
;
419 ext4_fsblk_t blocks_per_dir
;
421 int max_debt
, max_dirs
, min_inodes
;
422 ext4_grpblk_t min_blocks
;
424 struct ext4_group_desc
*desc
;
426 freei
= percpu_counter_read_positive(&sbi
->s_freeinodes_counter
);
427 avefreei
= freei
/ ngroups
;
428 freeb
= percpu_counter_read_positive(&sbi
->s_freeblocks_counter
);
430 do_div(avefreeb
, ngroups
);
431 ndirs
= percpu_counter_read_positive(&sbi
->s_dirs_counter
);
433 if ((parent
== sb
->s_root
->d_inode
) ||
434 (EXT4_I(parent
)->i_flags
& EXT4_TOPDIR_FL
)) {
435 int best_ndir
= inodes_per_group
;
439 get_random_bytes(&grp
, sizeof(grp
));
440 parent_group
= (unsigned)grp
% ngroups
;
441 for (i
= 0; i
< ngroups
; i
++) {
442 grp
= (parent_group
+ i
) % ngroups
;
443 desc
= ext4_get_group_desc(sb
, grp
, NULL
);
444 if (!desc
|| !desc
->bg_free_inodes_count
)
446 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= best_ndir
)
448 if (le16_to_cpu(desc
->bg_free_inodes_count
) < avefreei
)
450 if (le16_to_cpu(desc
->bg_free_blocks_count
) < avefreeb
)
454 best_ndir
= le16_to_cpu(desc
->bg_used_dirs_count
);
461 blocks_per_dir
= ext4_blocks_count(es
) - freeb
;
462 do_div(blocks_per_dir
, ndirs
);
464 max_dirs
= ndirs
/ ngroups
+ inodes_per_group
/ 16;
465 min_inodes
= avefreei
- inodes_per_group
/ 4;
466 min_blocks
= avefreeb
- EXT4_BLOCKS_PER_GROUP(sb
) / 4;
468 max_debt
= EXT4_BLOCKS_PER_GROUP(sb
);
469 max_debt
/= max_t(int, blocks_per_dir
, BLOCK_COST
);
470 if (max_debt
* INODE_COST
> inodes_per_group
)
471 max_debt
= inodes_per_group
/ INODE_COST
;
477 for (i
= 0; i
< ngroups
; i
++) {
478 *group
= (parent_group
+ i
) % ngroups
;
479 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
480 if (!desc
|| !desc
->bg_free_inodes_count
)
482 if (le16_to_cpu(desc
->bg_used_dirs_count
) >= max_dirs
)
484 if (le16_to_cpu(desc
->bg_free_inodes_count
) < min_inodes
)
486 if (le16_to_cpu(desc
->bg_free_blocks_count
) < min_blocks
)
492 for (i
= 0; i
< ngroups
; i
++) {
493 *group
= (parent_group
+ i
) % ngroups
;
494 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
495 if (desc
&& desc
->bg_free_inodes_count
&&
496 le16_to_cpu(desc
->bg_free_inodes_count
) >= avefreei
)
502 * The free-inodes counter is approximate, and for really small
503 * filesystems the above test can fail to find any blockgroups
512 static int find_group_other(struct super_block
*sb
, struct inode
*parent
,
515 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
516 ext4_group_t ngroups
= EXT4_SB(sb
)->s_groups_count
;
517 struct ext4_group_desc
*desc
;
521 * Try to place the inode in its parent directory
523 *group
= parent_group
;
524 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
525 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
526 le16_to_cpu(desc
->bg_free_blocks_count
))
530 * We're going to place this inode in a different blockgroup from its
531 * parent. We want to cause files in a common directory to all land in
532 * the same blockgroup. But we want files which are in a different
533 * directory which shares a blockgroup with our parent to land in a
534 * different blockgroup.
536 * So add our directory's i_ino into the starting point for the hash.
538 *group
= (*group
+ parent
->i_ino
) % ngroups
;
541 * Use a quadratic hash to find a group with a free inode and some free
544 for (i
= 1; i
< ngroups
; i
<<= 1) {
546 if (*group
>= ngroups
)
548 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
549 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
) &&
550 le16_to_cpu(desc
->bg_free_blocks_count
))
555 * That failed: try linear search for a free inode, even if that group
556 * has no free blocks.
558 *group
= parent_group
;
559 for (i
= 0; i
< ngroups
; i
++) {
560 if (++*group
>= ngroups
)
562 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
563 if (desc
&& le16_to_cpu(desc
->bg_free_inodes_count
))
571 * There are two policies for allocating an inode. If the new inode is
572 * a directory, then a forward search is made for a block group with both
573 * free space and a low directory-to-inode ratio; if that fails, then of
574 * the groups with above-average free space, that group with the fewest
575 * directories already is chosen.
577 * For other inodes, search forward from the parent directory's block
578 * group to find a free inode.
580 struct inode
*ext4_new_inode(handle_t
*handle
, struct inode
* dir
, int mode
)
582 struct super_block
*sb
;
583 struct buffer_head
*bitmap_bh
= NULL
;
584 struct buffer_head
*bh2
;
585 ext4_group_t group
= 0;
586 unsigned long ino
= 0;
587 struct inode
* inode
;
588 struct ext4_group_desc
* gdp
= NULL
;
589 struct ext4_super_block
* es
;
590 struct ext4_inode_info
*ei
;
591 struct ext4_sb_info
*sbi
;
596 ext4_group_t flex_group
;
598 /* Cannot create files in a deleted directory */
599 if (!dir
|| !dir
->i_nlink
)
600 return ERR_PTR(-EPERM
);
603 inode
= new_inode(sb
);
605 return ERR_PTR(-ENOMEM
);
611 if (sbi
->s_log_groups_per_flex
) {
612 ret2
= find_group_flex(sb
, dir
, &group
);
617 if (test_opt (sb
, OLDALLOC
))
618 ret2
= find_group_dir(sb
, dir
, &group
);
620 ret2
= find_group_orlov(sb
, dir
, &group
);
622 ret2
= find_group_other(sb
, dir
, &group
);
629 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
632 gdp
= ext4_get_group_desc(sb
, group
, &bh2
);
637 bitmap_bh
= ext4_read_inode_bitmap(sb
, group
);
643 repeat_in_this_group
:
644 ino
= ext4_find_next_zero_bit((unsigned long *)
645 bitmap_bh
->b_data
, EXT4_INODES_PER_GROUP(sb
), ino
);
646 if (ino
< EXT4_INODES_PER_GROUP(sb
)) {
648 BUFFER_TRACE(bitmap_bh
, "get_write_access");
649 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
653 if (!ext4_set_bit_atomic(sb_bgl_lock(sbi
, group
),
654 ino
, bitmap_bh
->b_data
)) {
656 BUFFER_TRACE(bitmap_bh
,
657 "call ext4_journal_dirty_metadata");
658 err
= ext4_journal_dirty_metadata(handle
,
665 jbd2_journal_release_buffer(handle
, bitmap_bh
);
667 if (++ino
< EXT4_INODES_PER_GROUP(sb
))
668 goto repeat_in_this_group
;
672 * This case is possible in concurrent environment. It is very
673 * rare. We cannot repeat the find_group_xxx() call because
674 * that will simply return the same blockgroup, because the
675 * group descriptor metadata has not yet been updated.
676 * So we just go onto the next blockgroup.
678 if (++group
== sbi
->s_groups_count
)
686 if ((group
== 0 && ino
< EXT4_FIRST_INO(sb
)) ||
687 ino
> EXT4_INODES_PER_GROUP(sb
)) {
688 ext4_error(sb
, __func__
,
689 "reserved inode or inode > inodes count - "
690 "block_group = %lu, inode=%lu", group
,
691 ino
+ group
* EXT4_INODES_PER_GROUP(sb
));
696 BUFFER_TRACE(bh2
, "get_write_access");
697 err
= ext4_journal_get_write_access(handle
, bh2
);
700 /* We may have to initialize the block bitmap if it isn't already */
701 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
, EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) &&
702 gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
703 struct buffer_head
*block_bh
= ext4_read_block_bitmap(sb
, group
);
705 BUFFER_TRACE(block_bh
, "get block bitmap access");
706 err
= ext4_journal_get_write_access(handle
, block_bh
);
713 spin_lock(sb_bgl_lock(sbi
, group
));
714 /* recheck and clear flag under lock if we still need to */
715 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
716 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
717 free
= ext4_free_blocks_after_init(sb
, group
, gdp
);
718 gdp
->bg_free_blocks_count
= cpu_to_le16(free
);
720 spin_unlock(sb_bgl_lock(sbi
, group
));
722 /* Don't need to dirty bitmap block if we didn't change it */
724 BUFFER_TRACE(block_bh
, "dirty block bitmap");
725 err
= ext4_journal_dirty_metadata(handle
, block_bh
);
733 spin_lock(sb_bgl_lock(sbi
, group
));
734 /* If we didn't allocate from within the initialized part of the inode
735 * table then we need to initialize up to this inode. */
736 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
, EXT4_FEATURE_RO_COMPAT_GDT_CSUM
)) {
737 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_INODE_UNINIT
)) {
738 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_INODE_UNINIT
);
740 /* When marking the block group with
741 * ~EXT4_BG_INODE_UNINIT we don't want to depend
742 * on the value of bg_itable_unused even though
743 * mke2fs could have initialized the same for us.
744 * Instead we calculated the value below
749 free
= EXT4_INODES_PER_GROUP(sb
) -
750 le16_to_cpu(gdp
->bg_itable_unused
);
754 * Check the relative inode number against the last used
755 * relative inode number in this group. if it is greater
756 * we need to update the bg_itable_unused count
760 gdp
->bg_itable_unused
=
761 cpu_to_le16(EXT4_INODES_PER_GROUP(sb
) - ino
);
764 le16_add_cpu(&gdp
->bg_free_inodes_count
, -1);
766 le16_add_cpu(&gdp
->bg_used_dirs_count
, 1);
768 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, group
, gdp
);
769 spin_unlock(sb_bgl_lock(sbi
, group
));
770 BUFFER_TRACE(bh2
, "call ext4_journal_dirty_metadata");
771 err
= ext4_journal_dirty_metadata(handle
, bh2
);
774 percpu_counter_dec(&sbi
->s_freeinodes_counter
);
776 percpu_counter_inc(&sbi
->s_dirs_counter
);
779 if (sbi
->s_log_groups_per_flex
) {
780 flex_group
= ext4_flex_group(sbi
, group
);
781 spin_lock(sb_bgl_lock(sbi
, flex_group
));
782 sbi
->s_flex_groups
[flex_group
].free_inodes
--;
783 spin_unlock(sb_bgl_lock(sbi
, flex_group
));
786 inode
->i_uid
= current
->fsuid
;
787 if (test_opt (sb
, GRPID
))
788 inode
->i_gid
= dir
->i_gid
;
789 else if (dir
->i_mode
& S_ISGID
) {
790 inode
->i_gid
= dir
->i_gid
;
794 inode
->i_gid
= current
->fsgid
;
795 inode
->i_mode
= mode
;
797 inode
->i_ino
= ino
+ group
* EXT4_INODES_PER_GROUP(sb
);
798 /* This is the optimal IO size (for stat), not the fs block size */
800 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= ei
->i_crtime
=
801 ext4_current_time(inode
);
803 memset(ei
->i_data
, 0, sizeof(ei
->i_data
));
804 ei
->i_dir_start_lookup
= 0;
808 * Don't inherit extent flag from directory. We set extent flag on
809 * newly created directory and file only if -o extent mount option is
812 ei
->i_flags
= EXT4_I(dir
)->i_flags
& ~(EXT4_INDEX_FL
|EXT4_EXTENTS_FL
);
814 ei
->i_flags
&= ~(EXT4_IMMUTABLE_FL
|EXT4_APPEND_FL
);
815 /* dirsync only applies to directories */
817 ei
->i_flags
&= ~EXT4_DIRSYNC_FL
;
820 ei
->i_block_alloc_info
= NULL
;
821 ei
->i_block_group
= group
;
823 ext4_set_inode_flags(inode
);
824 if (IS_DIRSYNC(inode
))
826 insert_inode_hash(inode
);
827 spin_lock(&sbi
->s_next_gen_lock
);
828 inode
->i_generation
= sbi
->s_next_generation
++;
829 spin_unlock(&sbi
->s_next_gen_lock
);
831 ei
->i_state
= EXT4_STATE_NEW
;
833 ei
->i_extra_isize
= EXT4_SB(sb
)->s_want_extra_isize
;
836 if(DQUOT_ALLOC_INODE(inode
)) {
841 err
= ext4_init_acl(handle
, inode
, dir
);
845 err
= ext4_init_security(handle
,inode
, dir
);
849 if (test_opt(sb
, EXTENTS
)) {
850 /* set extent flag only for directory, file and normal symlink*/
851 if (S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
)) {
852 EXT4_I(inode
)->i_flags
|= EXT4_EXTENTS_FL
;
853 ext4_ext_tree_init(handle
, inode
);
857 err
= ext4_mark_inode_dirty(handle
, inode
);
859 ext4_std_error(sb
, err
);
863 ext4_debug("allocating inode %lu\n", inode
->i_ino
);
866 ext4_std_error(sb
, err
);
875 DQUOT_FREE_INODE(inode
);
879 inode
->i_flags
|= S_NOQUOTA
;
886 /* Verify that we are loading a valid orphan from disk */
887 struct inode
*ext4_orphan_get(struct super_block
*sb
, unsigned long ino
)
889 unsigned long max_ino
= le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
);
890 ext4_group_t block_group
;
892 struct buffer_head
*bitmap_bh
;
893 struct inode
*inode
= NULL
;
896 /* Error cases - e2fsck has already cleaned up for us */
898 ext4_warning(sb
, __func__
,
899 "bad orphan ino %lu! e2fsck was run?", ino
);
903 block_group
= (ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
904 bit
= (ino
- 1) % EXT4_INODES_PER_GROUP(sb
);
905 bitmap_bh
= ext4_read_inode_bitmap(sb
, block_group
);
907 ext4_warning(sb
, __func__
,
908 "inode bitmap error for orphan %lu", ino
);
912 /* Having the inode bit set should be a 100% indicator that this
913 * is a valid orphan (no e2fsck run on fs). Orphans also include
914 * inodes that were being truncated, so we can't check i_nlink==0.
916 if (!ext4_test_bit(bit
, bitmap_bh
->b_data
))
919 inode
= ext4_iget(sb
, ino
);
924 * If the orphans has i_nlinks > 0 then it should be able to be
925 * truncated, otherwise it won't be removed from the orphan list
926 * during processing and an infinite loop will result.
928 if (inode
->i_nlink
&& !ext4_can_truncate(inode
))
931 if (NEXT_ORPHAN(inode
) > max_ino
)
937 err
= PTR_ERR(inode
);
940 ext4_warning(sb
, __func__
,
941 "bad orphan inode %lu! e2fsck was run?", ino
);
942 printk(KERN_NOTICE
"ext4_test_bit(bit=%d, block=%llu) = %d\n",
943 bit
, (unsigned long long)bitmap_bh
->b_blocknr
,
944 ext4_test_bit(bit
, bitmap_bh
->b_data
));
945 printk(KERN_NOTICE
"inode=%p\n", inode
);
947 printk(KERN_NOTICE
"is_bad_inode(inode)=%d\n",
948 is_bad_inode(inode
));
949 printk(KERN_NOTICE
"NEXT_ORPHAN(inode)=%u\n",
951 printk(KERN_NOTICE
"max_ino=%lu\n", max_ino
);
952 printk(KERN_NOTICE
"i_nlink=%u\n", inode
->i_nlink
);
953 /* Avoid freeing blocks if we got a bad deleted inode */
954 if (inode
->i_nlink
== 0)
963 unsigned long ext4_count_free_inodes (struct super_block
* sb
)
965 unsigned long desc_count
;
966 struct ext4_group_desc
*gdp
;
969 struct ext4_super_block
*es
;
970 unsigned long bitmap_count
, x
;
971 struct buffer_head
*bitmap_bh
= NULL
;
973 es
= EXT4_SB(sb
)->s_es
;
977 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
978 gdp
= ext4_get_group_desc (sb
, i
, NULL
);
981 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
983 bitmap_bh
= ext4_read_inode_bitmap(sb
, i
);
987 x
= ext4_count_free(bitmap_bh
, EXT4_INODES_PER_GROUP(sb
) / 8);
988 printk(KERN_DEBUG
"group %lu: stored = %d, counted = %lu\n",
989 i
, le16_to_cpu(gdp
->bg_free_inodes_count
), x
);
993 printk(KERN_DEBUG
"ext4_count_free_inodes: "
994 "stored = %u, computed = %lu, %lu\n",
995 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
999 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
1000 gdp
= ext4_get_group_desc (sb
, i
, NULL
);
1003 desc_count
+= le16_to_cpu(gdp
->bg_free_inodes_count
);
1010 /* Called at mount-time, super-block is locked */
1011 unsigned long ext4_count_dirs (struct super_block
* sb
)
1013 unsigned long count
= 0;
1016 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
1017 struct ext4_group_desc
*gdp
= ext4_get_group_desc (sb
, i
, NULL
);
1020 count
+= le16_to_cpu(gdp
->bg_used_dirs_count
);