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
;
193 if (atomic_read(&inode
->i_count
) > 1) {
194 printk(KERN_ERR
"ext4_free_inode: inode has count=%d\n",
195 atomic_read(&inode
->i_count
));
198 if (inode
->i_nlink
) {
199 printk(KERN_ERR
"ext4_free_inode: inode has nlink=%d\n",
204 printk(KERN_ERR
"ext4_free_inode: inode on "
205 "nonexistent device\n");
211 ext4_debug("freeing inode %lu\n", ino
);
212 trace_mark(ext4_free_inode
,
213 "dev %s ino %lu mode %d uid %lu gid %lu bocks %llu",
214 sb
->s_id
, inode
->i_ino
, inode
->i_mode
,
215 (unsigned long) inode
->i_uid
, (unsigned long) inode
->i_gid
,
216 (unsigned long long) inode
->i_blocks
);
219 * Note: we must free any quota before locking the superblock,
220 * as writing the quota to disk may need the lock as well.
223 ext4_xattr_delete_inode(handle
, inode
);
224 vfs_dq_free_inode(inode
);
227 is_directory
= S_ISDIR(inode
->i_mode
);
229 /* Do this BEFORE marking the inode not in use or returning an error */
232 es
= EXT4_SB(sb
)->s_es
;
233 if (ino
< EXT4_FIRST_INO(sb
) || ino
> le32_to_cpu(es
->s_inodes_count
)) {
234 ext4_error(sb
, "ext4_free_inode",
235 "reserved or nonexistent inode %lu", ino
);
238 block_group
= (ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
239 bit
= (ino
- 1) % EXT4_INODES_PER_GROUP(sb
);
240 bitmap_bh
= ext4_read_inode_bitmap(sb
, block_group
);
244 BUFFER_TRACE(bitmap_bh
, "get_write_access");
245 fatal
= ext4_journal_get_write_access(handle
, bitmap_bh
);
249 /* Ok, now we can actually update the inode bitmaps.. */
250 spin_lock(sb_bgl_lock(sbi
, block_group
));
251 cleared
= ext4_clear_bit(bit
, bitmap_bh
->b_data
);
252 spin_unlock(sb_bgl_lock(sbi
, block_group
));
254 ext4_error(sb
, "ext4_free_inode",
255 "bit already cleared for inode %lu", ino
);
257 gdp
= ext4_get_group_desc(sb
, block_group
, &bh2
);
259 BUFFER_TRACE(bh2
, "get_write_access");
260 fatal
= ext4_journal_get_write_access(handle
, bh2
);
261 if (fatal
) goto error_return
;
264 spin_lock(sb_bgl_lock(sbi
, block_group
));
265 count
= ext4_free_inodes_count(sb
, gdp
) + 1;
266 ext4_free_inodes_set(sb
, gdp
, count
);
268 count
= ext4_used_dirs_count(sb
, gdp
) - 1;
269 ext4_used_dirs_set(sb
, gdp
, count
);
270 if (sbi
->s_log_groups_per_flex
) {
273 f
= ext4_flex_group(sbi
, block_group
);
274 atomic_dec(&sbi
->s_flex_groups
[f
].free_inodes
);
278 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
,
280 spin_unlock(sb_bgl_lock(sbi
, block_group
));
281 percpu_counter_inc(&sbi
->s_freeinodes_counter
);
283 percpu_counter_dec(&sbi
->s_dirs_counter
);
285 if (sbi
->s_log_groups_per_flex
) {
288 f
= ext4_flex_group(sbi
, block_group
);
289 atomic_inc(&sbi
->s_flex_groups
[f
].free_inodes
);
292 BUFFER_TRACE(bh2
, "call ext4_handle_dirty_metadata");
293 err
= ext4_handle_dirty_metadata(handle
, NULL
, bh2
);
294 if (!fatal
) fatal
= err
;
296 BUFFER_TRACE(bitmap_bh
, "call ext4_handle_dirty_metadata");
297 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
303 ext4_std_error(sb
, fatal
);
307 * There are two policies for allocating an inode. If the new inode is
308 * a directory, then a forward search is made for a block group with both
309 * free space and a low directory-to-inode ratio; if that fails, then of
310 * the groups with above-average free space, that group with the fewest
311 * directories already is chosen.
313 * For other inodes, search forward from the parent directory\'s block
314 * group to find a free inode.
316 static int find_group_dir(struct super_block
*sb
, struct inode
*parent
,
317 ext4_group_t
*best_group
)
319 ext4_group_t ngroups
= EXT4_SB(sb
)->s_groups_count
;
320 unsigned int freei
, avefreei
;
321 struct ext4_group_desc
*desc
, *best_desc
= NULL
;
325 freei
= percpu_counter_read_positive(&EXT4_SB(sb
)->s_freeinodes_counter
);
326 avefreei
= freei
/ ngroups
;
328 for (group
= 0; group
< ngroups
; group
++) {
329 desc
= ext4_get_group_desc(sb
, group
, NULL
);
330 if (!desc
|| !ext4_free_inodes_count(sb
, desc
))
332 if (ext4_free_inodes_count(sb
, desc
) < avefreei
)
335 (ext4_free_blks_count(sb
, desc
) >
336 ext4_free_blks_count(sb
, best_desc
))) {
345 #define free_block_ratio 10
347 static int find_group_flex(struct super_block
*sb
, struct inode
*parent
,
348 ext4_group_t
*best_group
)
350 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
351 struct ext4_group_desc
*desc
;
352 struct buffer_head
*bh
;
353 struct flex_groups
*flex_group
= sbi
->s_flex_groups
;
354 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
355 ext4_group_t parent_fbg_group
= ext4_flex_group(sbi
, parent_group
);
356 ext4_group_t ngroups
= sbi
->s_groups_count
;
357 int flex_size
= ext4_flex_bg_size(sbi
);
358 ext4_group_t best_flex
= parent_fbg_group
;
359 int blocks_per_flex
= sbi
->s_blocks_per_group
* flex_size
;
360 int flexbg_free_blocks
;
361 int flex_freeb_ratio
;
362 ext4_group_t n_fbg_groups
;
365 n_fbg_groups
= (sbi
->s_groups_count
+ flex_size
- 1) >>
366 sbi
->s_log_groups_per_flex
;
368 find_close_to_parent
:
369 flexbg_free_blocks
= atomic_read(&flex_group
[best_flex
].free_blocks
);
370 flex_freeb_ratio
= flexbg_free_blocks
* 100 / blocks_per_flex
;
371 if (atomic_read(&flex_group
[best_flex
].free_inodes
) &&
372 flex_freeb_ratio
> free_block_ratio
)
375 if (best_flex
&& best_flex
== parent_fbg_group
) {
377 goto find_close_to_parent
;
380 for (i
= 0; i
< n_fbg_groups
; i
++) {
381 if (i
== parent_fbg_group
|| i
== parent_fbg_group
- 1)
384 flexbg_free_blocks
= atomic_read(&flex_group
[i
].free_blocks
);
385 flex_freeb_ratio
= flexbg_free_blocks
* 100 / blocks_per_flex
;
387 if (flex_freeb_ratio
> free_block_ratio
&&
388 (atomic_read(&flex_group
[i
].free_inodes
))) {
393 if ((atomic_read(&flex_group
[best_flex
].free_inodes
) == 0) ||
394 ((atomic_read(&flex_group
[i
].free_blocks
) >
395 atomic_read(&flex_group
[best_flex
].free_blocks
)) &&
396 atomic_read(&flex_group
[i
].free_inodes
)))
400 if (!atomic_read(&flex_group
[best_flex
].free_inodes
) ||
401 !atomic_read(&flex_group
[best_flex
].free_blocks
))
405 for (i
= best_flex
* flex_size
; i
< ngroups
&&
406 i
< (best_flex
+ 1) * flex_size
; i
++) {
407 desc
= ext4_get_group_desc(sb
, i
, &bh
);
408 if (ext4_free_inodes_count(sb
, desc
)) {
426 * Helper function for Orlov's allocator; returns critical information
427 * for a particular block group or flex_bg. If flex_size is 1, then g
428 * is a block group number; otherwise it is flex_bg number.
430 void get_orlov_stats(struct super_block
*sb
, ext4_group_t g
,
431 int flex_size
, struct orlov_stats
*stats
)
433 struct ext4_group_desc
*desc
;
434 struct flex_groups
*flex_group
= EXT4_SB(sb
)->s_flex_groups
;
437 stats
->free_inodes
= atomic_read(&flex_group
[g
].free_inodes
);
438 stats
->free_blocks
= atomic_read(&flex_group
[g
].free_blocks
);
439 stats
->used_dirs
= atomic_read(&flex_group
[g
].used_dirs
);
443 desc
= ext4_get_group_desc(sb
, g
, NULL
);
445 stats
->free_inodes
= ext4_free_inodes_count(sb
, desc
);
446 stats
->free_blocks
= ext4_free_blks_count(sb
, desc
);
447 stats
->used_dirs
= ext4_used_dirs_count(sb
, desc
);
449 stats
->free_inodes
= 0;
450 stats
->free_blocks
= 0;
451 stats
->used_dirs
= 0;
456 * Orlov's allocator for directories.
458 * We always try to spread first-level directories.
460 * If there are blockgroups with both free inodes and free blocks counts
461 * not worse than average we return one with smallest directory count.
462 * Otherwise we simply return a random group.
464 * For the rest rules look so:
466 * It's OK to put directory into a group unless
467 * it has too many directories already (max_dirs) or
468 * it has too few free inodes left (min_inodes) or
469 * it has too few free blocks left (min_blocks) or
470 * Parent's group is preferred, if it doesn't satisfy these
471 * conditions we search cyclically through the rest. If none
472 * of the groups look good we just look for a group with more
473 * free inodes than average (starting at parent's group).
476 static int find_group_orlov(struct super_block
*sb
, struct inode
*parent
,
477 ext4_group_t
*group
, int mode
)
479 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
480 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
481 ext4_group_t ngroups
= sbi
->s_groups_count
;
482 int inodes_per_group
= EXT4_INODES_PER_GROUP(sb
);
483 unsigned int freei
, avefreei
;
484 ext4_fsblk_t freeb
, avefreeb
;
486 int max_dirs
, min_inodes
;
487 ext4_grpblk_t min_blocks
;
488 ext4_group_t i
, grp
, g
;
489 struct ext4_group_desc
*desc
;
490 struct orlov_stats stats
;
491 int flex_size
= ext4_flex_bg_size(sbi
);
494 ngroups
= (ngroups
+ flex_size
- 1) >>
495 sbi
->s_log_groups_per_flex
;
496 parent_group
>>= sbi
->s_log_groups_per_flex
;
499 freei
= percpu_counter_read_positive(&sbi
->s_freeinodes_counter
);
500 avefreei
= freei
/ ngroups
;
501 freeb
= percpu_counter_read_positive(&sbi
->s_freeblocks_counter
);
503 do_div(avefreeb
, ngroups
);
504 ndirs
= percpu_counter_read_positive(&sbi
->s_dirs_counter
);
507 ((parent
== sb
->s_root
->d_inode
) ||
508 (EXT4_I(parent
)->i_flags
& EXT4_TOPDIR_FL
))) {
509 int best_ndir
= inodes_per_group
;
512 get_random_bytes(&grp
, sizeof(grp
));
513 parent_group
= (unsigned)grp
% ngroups
;
514 for (i
= 0; i
< ngroups
; i
++) {
515 g
= (parent_group
+ i
) % ngroups
;
516 get_orlov_stats(sb
, g
, flex_size
, &stats
);
517 if (!stats
.free_inodes
)
519 if (stats
.used_dirs
>= best_ndir
)
521 if (stats
.free_inodes
< avefreei
)
523 if (stats
.free_blocks
< avefreeb
)
527 best_ndir
= stats
.used_dirs
;
532 if (flex_size
== 1) {
538 * We pack inodes at the beginning of the flexgroup's
539 * inode tables. Block allocation decisions will do
540 * something similar, although regular files will
541 * start at 2nd block group of the flexgroup. See
542 * ext4_ext_find_goal() and ext4_find_near().
545 for (i
= 0; i
< flex_size
; i
++) {
546 if (grp
+i
>= sbi
->s_groups_count
)
548 desc
= ext4_get_group_desc(sb
, grp
+i
, NULL
);
549 if (desc
&& ext4_free_inodes_count(sb
, desc
)) {
557 max_dirs
= ndirs
/ ngroups
+ inodes_per_group
/ 16;
558 min_inodes
= avefreei
- inodes_per_group
*flex_size
/ 4;
561 min_blocks
= avefreeb
- EXT4_BLOCKS_PER_GROUP(sb
)*flex_size
/ 4;
564 * Start looking in the flex group where we last allocated an
565 * inode for this parent directory
567 if (EXT4_I(parent
)->i_last_alloc_group
!= ~0) {
568 parent_group
= EXT4_I(parent
)->i_last_alloc_group
;
570 parent_group
>>= sbi
->s_log_groups_per_flex
;
573 for (i
= 0; i
< ngroups
; i
++) {
574 grp
= (parent_group
+ i
) % ngroups
;
575 get_orlov_stats(sb
, grp
, flex_size
, &stats
);
576 if (stats
.used_dirs
>= max_dirs
)
578 if (stats
.free_inodes
< min_inodes
)
580 if (stats
.free_blocks
< min_blocks
)
586 ngroups
= sbi
->s_groups_count
;
587 avefreei
= freei
/ ngroups
;
588 parent_group
= EXT4_I(parent
)->i_block_group
;
589 for (i
= 0; i
< ngroups
; i
++) {
590 grp
= (parent_group
+ i
) % ngroups
;
591 desc
= ext4_get_group_desc(sb
, grp
, NULL
);
592 if (desc
&& ext4_free_inodes_count(sb
, desc
) &&
593 ext4_free_inodes_count(sb
, desc
) >= avefreei
) {
601 * The free-inodes counter is approximate, and for really small
602 * filesystems the above test can fail to find any blockgroups
611 static int find_group_other(struct super_block
*sb
, struct inode
*parent
,
612 ext4_group_t
*group
, int mode
)
614 ext4_group_t parent_group
= EXT4_I(parent
)->i_block_group
;
615 ext4_group_t ngroups
= EXT4_SB(sb
)->s_groups_count
;
616 struct ext4_group_desc
*desc
;
617 ext4_group_t i
, last
;
618 int flex_size
= ext4_flex_bg_size(EXT4_SB(sb
));
621 * Try to place the inode is the same flex group as its
622 * parent. If we can't find space, use the Orlov algorithm to
623 * find another flex group, and store that information in the
624 * parent directory's inode information so that use that flex
625 * group for future allocations.
631 parent_group
&= ~(flex_size
-1);
632 last
= parent_group
+ flex_size
;
635 for (i
= parent_group
; i
< last
; i
++) {
636 desc
= ext4_get_group_desc(sb
, i
, NULL
);
637 if (desc
&& ext4_free_inodes_count(sb
, desc
)) {
642 if (!retry
&& EXT4_I(parent
)->i_last_alloc_group
!= ~0) {
644 parent_group
= EXT4_I(parent
)->i_last_alloc_group
;
648 * If this didn't work, use the Orlov search algorithm
649 * to find a new flex group; we pass in the mode to
650 * avoid the topdir algorithms.
652 *group
= parent_group
+ flex_size
;
653 if (*group
> ngroups
)
655 return find_group_orlov(sb
, parent
, group
, mode
);
659 * Try to place the inode in its parent directory
661 *group
= parent_group
;
662 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
663 if (desc
&& ext4_free_inodes_count(sb
, desc
) &&
664 ext4_free_blks_count(sb
, desc
))
668 * We're going to place this inode in a different blockgroup from its
669 * parent. We want to cause files in a common directory to all land in
670 * the same blockgroup. But we want files which are in a different
671 * directory which shares a blockgroup with our parent to land in a
672 * different blockgroup.
674 * So add our directory's i_ino into the starting point for the hash.
676 *group
= (*group
+ parent
->i_ino
) % ngroups
;
679 * Use a quadratic hash to find a group with a free inode and some free
682 for (i
= 1; i
< ngroups
; i
<<= 1) {
684 if (*group
>= ngroups
)
686 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
687 if (desc
&& ext4_free_inodes_count(sb
, desc
) &&
688 ext4_free_blks_count(sb
, desc
))
693 * That failed: try linear search for a free inode, even if that group
694 * has no free blocks.
696 *group
= parent_group
;
697 for (i
= 0; i
< ngroups
; i
++) {
698 if (++*group
>= ngroups
)
700 desc
= ext4_get_group_desc(sb
, *group
, NULL
);
701 if (desc
&& ext4_free_inodes_count(sb
, desc
))
709 * claim the inode from the inode bitmap. If the group
710 * is uninit we need to take the groups's sb_bgl_lock
711 * and clear the uninit flag. The inode bitmap update
712 * and group desc uninit flag clear should be done
713 * after holding sb_bgl_lock so that ext4_read_inode_bitmap
714 * doesn't race with the ext4_claim_inode
716 static int ext4_claim_inode(struct super_block
*sb
,
717 struct buffer_head
*inode_bitmap_bh
,
718 unsigned long ino
, ext4_group_t group
, int mode
)
720 int free
= 0, retval
= 0, count
;
721 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
722 struct ext4_group_desc
*gdp
= ext4_get_group_desc(sb
, group
, NULL
);
724 spin_lock(sb_bgl_lock(sbi
, group
));
725 if (ext4_set_bit(ino
, inode_bitmap_bh
->b_data
)) {
726 /* not a free inode */
731 if ((group
== 0 && ino
< EXT4_FIRST_INO(sb
)) ||
732 ino
> EXT4_INODES_PER_GROUP(sb
)) {
733 spin_unlock(sb_bgl_lock(sbi
, group
));
734 ext4_error(sb
, __func__
,
735 "reserved inode or inode > inodes count - "
736 "block_group = %u, inode=%lu", group
,
737 ino
+ group
* EXT4_INODES_PER_GROUP(sb
));
740 /* If we didn't allocate from within the initialized part of the inode
741 * table then we need to initialize up to this inode. */
742 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
, EXT4_FEATURE_RO_COMPAT_GDT_CSUM
)) {
744 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_INODE_UNINIT
)) {
745 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_INODE_UNINIT
);
746 /* When marking the block group with
747 * ~EXT4_BG_INODE_UNINIT we don't want to depend
748 * on the value of bg_itable_unused even though
749 * mke2fs could have initialized the same for us.
750 * Instead we calculated the value below
755 free
= EXT4_INODES_PER_GROUP(sb
) -
756 ext4_itable_unused_count(sb
, gdp
);
760 * Check the relative inode number against the last used
761 * relative inode number in this group. if it is greater
762 * we need to update the bg_itable_unused count
766 ext4_itable_unused_set(sb
, gdp
,
767 (EXT4_INODES_PER_GROUP(sb
) - ino
));
769 count
= ext4_free_inodes_count(sb
, gdp
) - 1;
770 ext4_free_inodes_set(sb
, gdp
, count
);
772 count
= ext4_used_dirs_count(sb
, gdp
) + 1;
773 ext4_used_dirs_set(sb
, gdp
, count
);
774 if (sbi
->s_log_groups_per_flex
) {
775 ext4_group_t f
= ext4_flex_group(sbi
, group
);
777 atomic_inc(&sbi
->s_flex_groups
[f
].free_inodes
);
780 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, group
, gdp
);
782 spin_unlock(sb_bgl_lock(sbi
, group
));
787 * There are two policies for allocating an inode. If the new inode is
788 * a directory, then a forward search is made for a block group with both
789 * free space and a low directory-to-inode ratio; if that fails, then of
790 * the groups with above-average free space, that group with the fewest
791 * directories already is chosen.
793 * For other inodes, search forward from the parent directory's block
794 * group to find a free inode.
796 struct inode
*ext4_new_inode(handle_t
*handle
, struct inode
*dir
, int mode
)
798 struct super_block
*sb
;
799 struct buffer_head
*inode_bitmap_bh
= NULL
;
800 struct buffer_head
*group_desc_bh
;
801 ext4_group_t group
= 0;
802 unsigned long ino
= 0;
804 struct ext4_group_desc
*gdp
= NULL
;
805 struct ext4_super_block
*es
;
806 struct ext4_inode_info
*ei
;
807 struct ext4_sb_info
*sbi
;
813 ext4_group_t flex_group
;
815 /* Cannot create files in a deleted directory */
816 if (!dir
|| !dir
->i_nlink
)
817 return ERR_PTR(-EPERM
);
820 trace_mark(ext4_request_inode
, "dev %s dir %lu mode %d", sb
->s_id
,
822 inode
= new_inode(sb
);
824 return ERR_PTR(-ENOMEM
);
830 if (sbi
->s_log_groups_per_flex
&& test_opt(sb
, OLDALLOC
)) {
831 ret2
= find_group_flex(sb
, dir
, &group
);
833 ret2
= find_group_other(sb
, dir
, &group
, mode
);
834 if (ret2
== 0 && once
)
836 printk(KERN_NOTICE
"ext4: find_group_flex "
837 "failed, fallback succeeded dir %lu\n",
844 if (test_opt(sb
, OLDALLOC
))
845 ret2
= find_group_dir(sb
, dir
, &group
);
847 ret2
= find_group_orlov(sb
, dir
, &group
, mode
);
849 ret2
= find_group_other(sb
, dir
, &group
, mode
);
852 EXT4_I(dir
)->i_last_alloc_group
= group
;
857 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
860 gdp
= ext4_get_group_desc(sb
, group
, &group_desc_bh
);
864 brelse(inode_bitmap_bh
);
865 inode_bitmap_bh
= ext4_read_inode_bitmap(sb
, group
);
866 if (!inode_bitmap_bh
)
871 repeat_in_this_group
:
872 ino
= ext4_find_next_zero_bit((unsigned long *)
873 inode_bitmap_bh
->b_data
,
874 EXT4_INODES_PER_GROUP(sb
), ino
);
876 if (ino
< EXT4_INODES_PER_GROUP(sb
)) {
878 BUFFER_TRACE(inode_bitmap_bh
, "get_write_access");
879 err
= ext4_journal_get_write_access(handle
,
884 BUFFER_TRACE(group_desc_bh
, "get_write_access");
885 err
= ext4_journal_get_write_access(handle
,
889 if (!ext4_claim_inode(sb
, inode_bitmap_bh
,
892 BUFFER_TRACE(inode_bitmap_bh
,
893 "call ext4_handle_dirty_metadata");
894 err
= ext4_handle_dirty_metadata(handle
,
899 /* zero bit is inode number 1*/
904 ext4_handle_release_buffer(handle
, inode_bitmap_bh
);
905 ext4_handle_release_buffer(handle
, group_desc_bh
);
907 if (++ino
< EXT4_INODES_PER_GROUP(sb
))
908 goto repeat_in_this_group
;
912 * This case is possible in concurrent environment. It is very
913 * rare. We cannot repeat the find_group_xxx() call because
914 * that will simply return the same blockgroup, because the
915 * group descriptor metadata has not yet been updated.
916 * So we just go onto the next blockgroup.
918 if (++group
== sbi
->s_groups_count
)
925 /* We may have to initialize the block bitmap if it isn't already */
926 if (EXT4_HAS_RO_COMPAT_FEATURE(sb
, EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) &&
927 gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
928 struct buffer_head
*block_bitmap_bh
;
930 block_bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
931 BUFFER_TRACE(block_bitmap_bh
, "get block bitmap access");
932 err
= ext4_journal_get_write_access(handle
, block_bitmap_bh
);
934 brelse(block_bitmap_bh
);
939 spin_lock(sb_bgl_lock(sbi
, group
));
940 /* recheck and clear flag under lock if we still need to */
941 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
942 free
= ext4_free_blocks_after_init(sb
, group
, gdp
);
943 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
944 ext4_free_blks_set(sb
, gdp
, free
);
945 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, group
,
948 spin_unlock(sb_bgl_lock(sbi
, group
));
950 /* Don't need to dirty bitmap block if we didn't change it */
952 BUFFER_TRACE(block_bitmap_bh
, "dirty block bitmap");
953 err
= ext4_handle_dirty_metadata(handle
,
954 NULL
, block_bitmap_bh
);
957 brelse(block_bitmap_bh
);
961 BUFFER_TRACE(group_desc_bh
, "call ext4_handle_dirty_metadata");
962 err
= ext4_handle_dirty_metadata(handle
, NULL
, group_desc_bh
);
966 percpu_counter_dec(&sbi
->s_freeinodes_counter
);
968 percpu_counter_inc(&sbi
->s_dirs_counter
);
971 if (sbi
->s_log_groups_per_flex
) {
972 flex_group
= ext4_flex_group(sbi
, group
);
973 atomic_dec(&sbi
->s_flex_groups
[flex_group
].free_inodes
);
976 inode
->i_uid
= current_fsuid();
977 if (test_opt(sb
, GRPID
))
978 inode
->i_gid
= dir
->i_gid
;
979 else if (dir
->i_mode
& S_ISGID
) {
980 inode
->i_gid
= dir
->i_gid
;
984 inode
->i_gid
= current_fsgid();
985 inode
->i_mode
= mode
;
987 inode
->i_ino
= ino
+ group
* EXT4_INODES_PER_GROUP(sb
);
988 /* This is the optimal IO size (for stat), not the fs block size */
990 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= ei
->i_crtime
=
991 ext4_current_time(inode
);
993 memset(ei
->i_data
, 0, sizeof(ei
->i_data
));
994 ei
->i_dir_start_lookup
= 0;
998 * Don't inherit extent flag from directory, amongst others. We set
999 * extent flag on newly created directory and file only if -o extent
1000 * mount option is specified
1003 ext4_mask_flags(mode
, EXT4_I(dir
)->i_flags
& EXT4_FL_INHERITED
);
1006 ei
->i_block_group
= group
;
1007 ei
->i_last_alloc_group
= ~0;
1009 ext4_set_inode_flags(inode
);
1010 if (IS_DIRSYNC(inode
))
1011 ext4_handle_sync(handle
);
1012 if (insert_inode_locked(inode
) < 0) {
1016 spin_lock(&sbi
->s_next_gen_lock
);
1017 inode
->i_generation
= sbi
->s_next_generation
++;
1018 spin_unlock(&sbi
->s_next_gen_lock
);
1020 ei
->i_state
= EXT4_STATE_NEW
;
1022 ei
->i_extra_isize
= EXT4_SB(sb
)->s_want_extra_isize
;
1025 if (vfs_dq_alloc_inode(inode
)) {
1030 err
= ext4_init_acl(handle
, inode
, dir
);
1032 goto fail_free_drop
;
1034 err
= ext4_init_security(handle
, inode
, dir
);
1036 goto fail_free_drop
;
1038 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
1039 /* set extent flag only for directory, file and normal symlink*/
1040 if (S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
)) {
1041 EXT4_I(inode
)->i_flags
|= EXT4_EXTENTS_FL
;
1042 ext4_ext_tree_init(handle
, inode
);
1046 err
= ext4_mark_inode_dirty(handle
, inode
);
1048 ext4_std_error(sb
, err
);
1049 goto fail_free_drop
;
1052 ext4_debug("allocating inode %lu\n", inode
->i_ino
);
1053 trace_mark(ext4_allocate_inode
, "dev %s ino %lu dir %lu mode %d",
1054 sb
->s_id
, inode
->i_ino
, dir
->i_ino
, mode
);
1057 ext4_std_error(sb
, err
);
1062 brelse(inode_bitmap_bh
);
1066 vfs_dq_free_inode(inode
);
1070 inode
->i_flags
|= S_NOQUOTA
;
1072 unlock_new_inode(inode
);
1074 brelse(inode_bitmap_bh
);
1075 return ERR_PTR(err
);
1078 /* Verify that we are loading a valid orphan from disk */
1079 struct inode
*ext4_orphan_get(struct super_block
*sb
, unsigned long ino
)
1081 unsigned long max_ino
= le32_to_cpu(EXT4_SB(sb
)->s_es
->s_inodes_count
);
1082 ext4_group_t block_group
;
1084 struct buffer_head
*bitmap_bh
;
1085 struct inode
*inode
= NULL
;
1088 /* Error cases - e2fsck has already cleaned up for us */
1089 if (ino
> max_ino
) {
1090 ext4_warning(sb
, __func__
,
1091 "bad orphan ino %lu! e2fsck was run?", ino
);
1095 block_group
= (ino
- 1) / EXT4_INODES_PER_GROUP(sb
);
1096 bit
= (ino
- 1) % EXT4_INODES_PER_GROUP(sb
);
1097 bitmap_bh
= ext4_read_inode_bitmap(sb
, block_group
);
1099 ext4_warning(sb
, __func__
,
1100 "inode bitmap error for orphan %lu", ino
);
1104 /* Having the inode bit set should be a 100% indicator that this
1105 * is a valid orphan (no e2fsck run on fs). Orphans also include
1106 * inodes that were being truncated, so we can't check i_nlink==0.
1108 if (!ext4_test_bit(bit
, bitmap_bh
->b_data
))
1111 inode
= ext4_iget(sb
, ino
);
1116 * If the orphans has i_nlinks > 0 then it should be able to be
1117 * truncated, otherwise it won't be removed from the orphan list
1118 * during processing and an infinite loop will result.
1120 if (inode
->i_nlink
&& !ext4_can_truncate(inode
))
1123 if (NEXT_ORPHAN(inode
) > max_ino
)
1129 err
= PTR_ERR(inode
);
1132 ext4_warning(sb
, __func__
,
1133 "bad orphan inode %lu! e2fsck was run?", ino
);
1134 printk(KERN_NOTICE
"ext4_test_bit(bit=%d, block=%llu) = %d\n",
1135 bit
, (unsigned long long)bitmap_bh
->b_blocknr
,
1136 ext4_test_bit(bit
, bitmap_bh
->b_data
));
1137 printk(KERN_NOTICE
"inode=%p\n", inode
);
1139 printk(KERN_NOTICE
"is_bad_inode(inode)=%d\n",
1140 is_bad_inode(inode
));
1141 printk(KERN_NOTICE
"NEXT_ORPHAN(inode)=%u\n",
1142 NEXT_ORPHAN(inode
));
1143 printk(KERN_NOTICE
"max_ino=%lu\n", max_ino
);
1144 printk(KERN_NOTICE
"i_nlink=%u\n", inode
->i_nlink
);
1145 /* Avoid freeing blocks if we got a bad deleted inode */
1146 if (inode
->i_nlink
== 0)
1147 inode
->i_blocks
= 0;
1152 return ERR_PTR(err
);
1155 unsigned long ext4_count_free_inodes(struct super_block
*sb
)
1157 unsigned long desc_count
;
1158 struct ext4_group_desc
*gdp
;
1161 struct ext4_super_block
*es
;
1162 unsigned long bitmap_count
, x
;
1163 struct buffer_head
*bitmap_bh
= NULL
;
1165 es
= EXT4_SB(sb
)->s_es
;
1169 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
1170 gdp
= ext4_get_group_desc(sb
, i
, NULL
);
1173 desc_count
+= ext4_free_inodes_count(sb
, gdp
);
1175 bitmap_bh
= ext4_read_inode_bitmap(sb
, i
);
1179 x
= ext4_count_free(bitmap_bh
, EXT4_INODES_PER_GROUP(sb
) / 8);
1180 printk(KERN_DEBUG
"group %lu: stored = %d, counted = %lu\n",
1181 i
, ext4_free_inodes_count(sb
, gdp
), x
);
1185 printk(KERN_DEBUG
"ext4_count_free_inodes: "
1186 "stored = %u, computed = %lu, %lu\n",
1187 le32_to_cpu(es
->s_free_inodes_count
), desc_count
, bitmap_count
);
1191 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
1192 gdp
= ext4_get_group_desc(sb
, i
, NULL
);
1195 desc_count
+= ext4_free_inodes_count(sb
, gdp
);
1202 /* Called at mount-time, super-block is locked */
1203 unsigned long ext4_count_dirs(struct super_block
* sb
)
1205 unsigned long count
= 0;
1208 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
1209 struct ext4_group_desc
*gdp
= ext4_get_group_desc(sb
, i
, NULL
);
1212 count
+= ext4_used_dirs_count(sb
, gdp
);