staging: most: video: rename module
[linux/fpc-iii.git] / fs / ext4 / ialloc.c
blobb4267d72f24955c314d78f350c4247e6c7373cc2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/ext4/ialloc.c
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
10 * BSD ufs-inspired inode and directory allocation by
11 * Stephen Tweedie (sct@redhat.com), 1993
12 * Big-endian to little-endian byte-swapping/bitmaps by
13 * David S. Miller (davem@caip.rutgers.edu), 1995
16 #include <linux/time.h>
17 #include <linux/fs.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 <linux/cred.h>
27 #include <asm/byteorder.h>
29 #include "ext4.h"
30 #include "ext4_jbd2.h"
31 #include "xattr.h"
32 #include "acl.h"
34 #include <trace/events/ext4.h>
37 * ialloc.c contains the inodes allocation and deallocation routines
41 * The free inodes are managed by bitmaps. A file system contains several
42 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
43 * block for inodes, N blocks for the inode table and data blocks.
45 * The file system contains group descriptors which are located after the
46 * super block. Each descriptor contains the number of the bitmap block and
47 * the free blocks count in the block.
51 * To avoid calling the atomic setbit hundreds or thousands of times, we only
52 * need to use it within a single byte (to ensure we get endianness right).
53 * We can use memset for the rest of the bitmap as there are no other users.
55 void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
57 int i;
59 if (start_bit >= end_bit)
60 return;
62 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
63 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
64 ext4_set_bit(i, bitmap);
65 if (i < end_bit)
66 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
69 /* Initializes an uninitialized inode bitmap */
70 static int ext4_init_inode_bitmap(struct super_block *sb,
71 struct buffer_head *bh,
72 ext4_group_t block_group,
73 struct ext4_group_desc *gdp)
75 struct ext4_group_info *grp;
76 struct ext4_sb_info *sbi = EXT4_SB(sb);
77 J_ASSERT_BH(bh, buffer_locked(bh));
79 /* If checksum is bad mark all blocks and inodes use to prevent
80 * allocation, essentially implementing a per-group read-only flag. */
81 if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
82 grp = ext4_get_group_info(sb, block_group);
83 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
84 percpu_counter_sub(&sbi->s_freeclusters_counter,
85 grp->bb_free);
86 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
87 if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
88 int count;
89 count = ext4_free_inodes_count(sb, gdp);
90 percpu_counter_sub(&sbi->s_freeinodes_counter,
91 count);
93 set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
94 return -EFSBADCRC;
97 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
98 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
99 bh->b_data);
100 ext4_inode_bitmap_csum_set(sb, block_group, gdp, bh,
101 EXT4_INODES_PER_GROUP(sb) / 8);
102 ext4_group_desc_csum_set(sb, block_group, gdp);
104 return 0;
107 void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate)
109 if (uptodate) {
110 set_buffer_uptodate(bh);
111 set_bitmap_uptodate(bh);
113 unlock_buffer(bh);
114 put_bh(bh);
117 static int ext4_validate_inode_bitmap(struct super_block *sb,
118 struct ext4_group_desc *desc,
119 ext4_group_t block_group,
120 struct buffer_head *bh)
122 ext4_fsblk_t blk;
123 struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
124 struct ext4_sb_info *sbi = EXT4_SB(sb);
126 if (buffer_verified(bh))
127 return 0;
128 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
129 return -EFSCORRUPTED;
131 ext4_lock_group(sb, block_group);
132 blk = ext4_inode_bitmap(sb, desc);
133 if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
134 EXT4_INODES_PER_GROUP(sb) / 8)) {
135 ext4_unlock_group(sb, block_group);
136 ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
137 "inode_bitmap = %llu", block_group, blk);
138 grp = ext4_get_group_info(sb, block_group);
139 if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
140 int count;
141 count = ext4_free_inodes_count(sb, desc);
142 percpu_counter_sub(&sbi->s_freeinodes_counter,
143 count);
145 set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
146 return -EFSBADCRC;
148 set_buffer_verified(bh);
149 ext4_unlock_group(sb, block_group);
150 return 0;
154 * Read the inode allocation bitmap for a given block_group, reading
155 * into the specified slot in the superblock's bitmap cache.
157 * Return buffer_head of bitmap on success or NULL.
159 static struct buffer_head *
160 ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
162 struct ext4_group_desc *desc;
163 struct buffer_head *bh = NULL;
164 ext4_fsblk_t bitmap_blk;
165 int err;
167 desc = ext4_get_group_desc(sb, block_group, NULL);
168 if (!desc)
169 return ERR_PTR(-EFSCORRUPTED);
171 bitmap_blk = ext4_inode_bitmap(sb, desc);
172 bh = sb_getblk(sb, bitmap_blk);
173 if (unlikely(!bh)) {
174 ext4_error(sb, "Cannot read inode bitmap - "
175 "block_group = %u, inode_bitmap = %llu",
176 block_group, bitmap_blk);
177 return ERR_PTR(-EIO);
179 if (bitmap_uptodate(bh))
180 goto verify;
182 lock_buffer(bh);
183 if (bitmap_uptodate(bh)) {
184 unlock_buffer(bh);
185 goto verify;
188 ext4_lock_group(sb, block_group);
189 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
190 err = ext4_init_inode_bitmap(sb, bh, block_group, desc);
191 set_bitmap_uptodate(bh);
192 set_buffer_uptodate(bh);
193 set_buffer_verified(bh);
194 ext4_unlock_group(sb, block_group);
195 unlock_buffer(bh);
196 if (err) {
197 ext4_error(sb, "Failed to init inode bitmap for group "
198 "%u: %d", block_group, err);
199 goto out;
201 return bh;
203 ext4_unlock_group(sb, block_group);
205 if (buffer_uptodate(bh)) {
207 * if not uninit if bh is uptodate,
208 * bitmap is also uptodate
210 set_bitmap_uptodate(bh);
211 unlock_buffer(bh);
212 goto verify;
215 * submit the buffer_head for reading
217 trace_ext4_load_inode_bitmap(sb, block_group);
218 bh->b_end_io = ext4_end_bitmap_read;
219 get_bh(bh);
220 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
221 wait_on_buffer(bh);
222 if (!buffer_uptodate(bh)) {
223 put_bh(bh);
224 ext4_error(sb, "Cannot read inode bitmap - "
225 "block_group = %u, inode_bitmap = %llu",
226 block_group, bitmap_blk);
227 return ERR_PTR(-EIO);
230 verify:
231 err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
232 if (err)
233 goto out;
234 return bh;
235 out:
236 put_bh(bh);
237 return ERR_PTR(err);
241 * NOTE! When we get the inode, we're the only people
242 * that have access to it, and as such there are no
243 * race conditions we have to worry about. The inode
244 * is not on the hash-lists, and it cannot be reached
245 * through the filesystem because the directory entry
246 * has been deleted earlier.
248 * HOWEVER: we must make sure that we get no aliases,
249 * which means that we have to call "clear_inode()"
250 * _before_ we mark the inode not in use in the inode
251 * bitmaps. Otherwise a newly created file might use
252 * the same inode number (not actually the same pointer
253 * though), and then we'd have two inodes sharing the
254 * same inode number and space on the harddisk.
256 void ext4_free_inode(handle_t *handle, struct inode *inode)
258 struct super_block *sb = inode->i_sb;
259 int is_directory;
260 unsigned long ino;
261 struct buffer_head *bitmap_bh = NULL;
262 struct buffer_head *bh2;
263 ext4_group_t block_group;
264 unsigned long bit;
265 struct ext4_group_desc *gdp;
266 struct ext4_super_block *es;
267 struct ext4_sb_info *sbi;
268 int fatal = 0, err, count, cleared;
269 struct ext4_group_info *grp;
271 if (!sb) {
272 printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
273 "nonexistent device\n", __func__, __LINE__);
274 return;
276 if (atomic_read(&inode->i_count) > 1) {
277 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
278 __func__, __LINE__, inode->i_ino,
279 atomic_read(&inode->i_count));
280 return;
282 if (inode->i_nlink) {
283 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
284 __func__, __LINE__, inode->i_ino, inode->i_nlink);
285 return;
287 sbi = EXT4_SB(sb);
289 ino = inode->i_ino;
290 ext4_debug("freeing inode %lu\n", ino);
291 trace_ext4_free_inode(inode);
294 * Note: we must free any quota before locking the superblock,
295 * as writing the quota to disk may need the lock as well.
297 dquot_initialize(inode);
298 dquot_free_inode(inode);
299 dquot_drop(inode);
301 is_directory = S_ISDIR(inode->i_mode);
303 /* Do this BEFORE marking the inode not in use or returning an error */
304 ext4_clear_inode(inode);
306 es = EXT4_SB(sb)->s_es;
307 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
308 ext4_error(sb, "reserved or nonexistent inode %lu", ino);
309 goto error_return;
311 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
312 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
313 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
314 /* Don't bother if the inode bitmap is corrupt. */
315 grp = ext4_get_group_info(sb, block_group);
316 if (IS_ERR(bitmap_bh)) {
317 fatal = PTR_ERR(bitmap_bh);
318 bitmap_bh = NULL;
319 goto error_return;
321 if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
322 fatal = -EFSCORRUPTED;
323 goto error_return;
326 BUFFER_TRACE(bitmap_bh, "get_write_access");
327 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
328 if (fatal)
329 goto error_return;
331 fatal = -ESRCH;
332 gdp = ext4_get_group_desc(sb, block_group, &bh2);
333 if (gdp) {
334 BUFFER_TRACE(bh2, "get_write_access");
335 fatal = ext4_journal_get_write_access(handle, bh2);
337 ext4_lock_group(sb, block_group);
338 cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
339 if (fatal || !cleared) {
340 ext4_unlock_group(sb, block_group);
341 goto out;
344 count = ext4_free_inodes_count(sb, gdp) + 1;
345 ext4_free_inodes_set(sb, gdp, count);
346 if (is_directory) {
347 count = ext4_used_dirs_count(sb, gdp) - 1;
348 ext4_used_dirs_set(sb, gdp, count);
349 percpu_counter_dec(&sbi->s_dirs_counter);
351 ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
352 EXT4_INODES_PER_GROUP(sb) / 8);
353 ext4_group_desc_csum_set(sb, block_group, gdp);
354 ext4_unlock_group(sb, block_group);
356 percpu_counter_inc(&sbi->s_freeinodes_counter);
357 if (sbi->s_log_groups_per_flex) {
358 ext4_group_t f = ext4_flex_group(sbi, block_group);
360 atomic_inc(&sbi->s_flex_groups[f].free_inodes);
361 if (is_directory)
362 atomic_dec(&sbi->s_flex_groups[f].used_dirs);
364 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
365 fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
366 out:
367 if (cleared) {
368 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
369 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
370 if (!fatal)
371 fatal = err;
372 } else {
373 ext4_error(sb, "bit already cleared for inode %lu", ino);
374 if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
375 int count;
376 count = ext4_free_inodes_count(sb, gdp);
377 percpu_counter_sub(&sbi->s_freeinodes_counter,
378 count);
380 set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
383 error_return:
384 brelse(bitmap_bh);
385 ext4_std_error(sb, fatal);
388 struct orlov_stats {
389 __u64 free_clusters;
390 __u32 free_inodes;
391 __u32 used_dirs;
395 * Helper function for Orlov's allocator; returns critical information
396 * for a particular block group or flex_bg. If flex_size is 1, then g
397 * is a block group number; otherwise it is flex_bg number.
399 static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
400 int flex_size, struct orlov_stats *stats)
402 struct ext4_group_desc *desc;
403 struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
405 if (flex_size > 1) {
406 stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
407 stats->free_clusters = atomic64_read(&flex_group[g].free_clusters);
408 stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
409 return;
412 desc = ext4_get_group_desc(sb, g, NULL);
413 if (desc) {
414 stats->free_inodes = ext4_free_inodes_count(sb, desc);
415 stats->free_clusters = ext4_free_group_clusters(sb, desc);
416 stats->used_dirs = ext4_used_dirs_count(sb, desc);
417 } else {
418 stats->free_inodes = 0;
419 stats->free_clusters = 0;
420 stats->used_dirs = 0;
425 * Orlov's allocator for directories.
427 * We always try to spread first-level directories.
429 * If there are blockgroups with both free inodes and free blocks counts
430 * not worse than average we return one with smallest directory count.
431 * Otherwise we simply return a random group.
433 * For the rest rules look so:
435 * It's OK to put directory into a group unless
436 * it has too many directories already (max_dirs) or
437 * it has too few free inodes left (min_inodes) or
438 * it has too few free blocks left (min_blocks) or
439 * Parent's group is preferred, if it doesn't satisfy these
440 * conditions we search cyclically through the rest. If none
441 * of the groups look good we just look for a group with more
442 * free inodes than average (starting at parent's group).
445 static int find_group_orlov(struct super_block *sb, struct inode *parent,
446 ext4_group_t *group, umode_t mode,
447 const struct qstr *qstr)
449 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
450 struct ext4_sb_info *sbi = EXT4_SB(sb);
451 ext4_group_t real_ngroups = ext4_get_groups_count(sb);
452 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
453 unsigned int freei, avefreei, grp_free;
454 ext4_fsblk_t freeb, avefreec;
455 unsigned int ndirs;
456 int max_dirs, min_inodes;
457 ext4_grpblk_t min_clusters;
458 ext4_group_t i, grp, g, ngroups;
459 struct ext4_group_desc *desc;
460 struct orlov_stats stats;
461 int flex_size = ext4_flex_bg_size(sbi);
462 struct dx_hash_info hinfo;
464 ngroups = real_ngroups;
465 if (flex_size > 1) {
466 ngroups = (real_ngroups + flex_size - 1) >>
467 sbi->s_log_groups_per_flex;
468 parent_group >>= sbi->s_log_groups_per_flex;
471 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
472 avefreei = freei / ngroups;
473 freeb = EXT4_C2B(sbi,
474 percpu_counter_read_positive(&sbi->s_freeclusters_counter));
475 avefreec = freeb;
476 do_div(avefreec, ngroups);
477 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
479 if (S_ISDIR(mode) &&
480 ((parent == d_inode(sb->s_root)) ||
481 (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
482 int best_ndir = inodes_per_group;
483 int ret = -1;
485 if (qstr) {
486 hinfo.hash_version = DX_HASH_HALF_MD4;
487 hinfo.seed = sbi->s_hash_seed;
488 ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
489 grp = hinfo.hash;
490 } else
491 grp = prandom_u32();
492 parent_group = (unsigned)grp % ngroups;
493 for (i = 0; i < ngroups; i++) {
494 g = (parent_group + i) % ngroups;
495 get_orlov_stats(sb, g, flex_size, &stats);
496 if (!stats.free_inodes)
497 continue;
498 if (stats.used_dirs >= best_ndir)
499 continue;
500 if (stats.free_inodes < avefreei)
501 continue;
502 if (stats.free_clusters < avefreec)
503 continue;
504 grp = g;
505 ret = 0;
506 best_ndir = stats.used_dirs;
508 if (ret)
509 goto fallback;
510 found_flex_bg:
511 if (flex_size == 1) {
512 *group = grp;
513 return 0;
517 * We pack inodes at the beginning of the flexgroup's
518 * inode tables. Block allocation decisions will do
519 * something similar, although regular files will
520 * start at 2nd block group of the flexgroup. See
521 * ext4_ext_find_goal() and ext4_find_near().
523 grp *= flex_size;
524 for (i = 0; i < flex_size; i++) {
525 if (grp+i >= real_ngroups)
526 break;
527 desc = ext4_get_group_desc(sb, grp+i, NULL);
528 if (desc && ext4_free_inodes_count(sb, desc)) {
529 *group = grp+i;
530 return 0;
533 goto fallback;
536 max_dirs = ndirs / ngroups + inodes_per_group / 16;
537 min_inodes = avefreei - inodes_per_group*flex_size / 4;
538 if (min_inodes < 1)
539 min_inodes = 1;
540 min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
543 * Start looking in the flex group where we last allocated an
544 * inode for this parent directory
546 if (EXT4_I(parent)->i_last_alloc_group != ~0) {
547 parent_group = EXT4_I(parent)->i_last_alloc_group;
548 if (flex_size > 1)
549 parent_group >>= sbi->s_log_groups_per_flex;
552 for (i = 0; i < ngroups; i++) {
553 grp = (parent_group + i) % ngroups;
554 get_orlov_stats(sb, grp, flex_size, &stats);
555 if (stats.used_dirs >= max_dirs)
556 continue;
557 if (stats.free_inodes < min_inodes)
558 continue;
559 if (stats.free_clusters < min_clusters)
560 continue;
561 goto found_flex_bg;
564 fallback:
565 ngroups = real_ngroups;
566 avefreei = freei / ngroups;
567 fallback_retry:
568 parent_group = EXT4_I(parent)->i_block_group;
569 for (i = 0; i < ngroups; i++) {
570 grp = (parent_group + i) % ngroups;
571 desc = ext4_get_group_desc(sb, grp, NULL);
572 if (desc) {
573 grp_free = ext4_free_inodes_count(sb, desc);
574 if (grp_free && grp_free >= avefreei) {
575 *group = grp;
576 return 0;
581 if (avefreei) {
583 * The free-inodes counter is approximate, and for really small
584 * filesystems the above test can fail to find any blockgroups
586 avefreei = 0;
587 goto fallback_retry;
590 return -1;
593 static int find_group_other(struct super_block *sb, struct inode *parent,
594 ext4_group_t *group, umode_t mode)
596 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
597 ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
598 struct ext4_group_desc *desc;
599 int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
602 * Try to place the inode is the same flex group as its
603 * parent. If we can't find space, use the Orlov algorithm to
604 * find another flex group, and store that information in the
605 * parent directory's inode information so that use that flex
606 * group for future allocations.
608 if (flex_size > 1) {
609 int retry = 0;
611 try_again:
612 parent_group &= ~(flex_size-1);
613 last = parent_group + flex_size;
614 if (last > ngroups)
615 last = ngroups;
616 for (i = parent_group; i < last; i++) {
617 desc = ext4_get_group_desc(sb, i, NULL);
618 if (desc && ext4_free_inodes_count(sb, desc)) {
619 *group = i;
620 return 0;
623 if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
624 retry = 1;
625 parent_group = EXT4_I(parent)->i_last_alloc_group;
626 goto try_again;
629 * If this didn't work, use the Orlov search algorithm
630 * to find a new flex group; we pass in the mode to
631 * avoid the topdir algorithms.
633 *group = parent_group + flex_size;
634 if (*group > ngroups)
635 *group = 0;
636 return find_group_orlov(sb, parent, group, mode, NULL);
640 * Try to place the inode in its parent directory
642 *group = parent_group;
643 desc = ext4_get_group_desc(sb, *group, NULL);
644 if (desc && ext4_free_inodes_count(sb, desc) &&
645 ext4_free_group_clusters(sb, desc))
646 return 0;
649 * We're going to place this inode in a different blockgroup from its
650 * parent. We want to cause files in a common directory to all land in
651 * the same blockgroup. But we want files which are in a different
652 * directory which shares a blockgroup with our parent to land in a
653 * different blockgroup.
655 * So add our directory's i_ino into the starting point for the hash.
657 *group = (*group + parent->i_ino) % ngroups;
660 * Use a quadratic hash to find a group with a free inode and some free
661 * blocks.
663 for (i = 1; i < ngroups; i <<= 1) {
664 *group += i;
665 if (*group >= ngroups)
666 *group -= ngroups;
667 desc = ext4_get_group_desc(sb, *group, NULL);
668 if (desc && ext4_free_inodes_count(sb, desc) &&
669 ext4_free_group_clusters(sb, desc))
670 return 0;
674 * That failed: try linear search for a free inode, even if that group
675 * has no free blocks.
677 *group = parent_group;
678 for (i = 0; i < ngroups; i++) {
679 if (++*group >= ngroups)
680 *group = 0;
681 desc = ext4_get_group_desc(sb, *group, NULL);
682 if (desc && ext4_free_inodes_count(sb, desc))
683 return 0;
686 return -1;
690 * In no journal mode, if an inode has recently been deleted, we want
691 * to avoid reusing it until we're reasonably sure the inode table
692 * block has been written back to disk. (Yes, these values are
693 * somewhat arbitrary...)
695 #define RECENTCY_MIN 5
696 #define RECENTCY_DIRTY 300
698 static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
700 struct ext4_group_desc *gdp;
701 struct ext4_inode *raw_inode;
702 struct buffer_head *bh;
703 int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
704 int offset, ret = 0;
705 int recentcy = RECENTCY_MIN;
706 u32 dtime, now;
708 gdp = ext4_get_group_desc(sb, group, NULL);
709 if (unlikely(!gdp))
710 return 0;
712 bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
713 (ino / inodes_per_block));
714 if (!bh || !buffer_uptodate(bh))
716 * If the block is not in the buffer cache, then it
717 * must have been written out.
719 goto out;
721 offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
722 raw_inode = (struct ext4_inode *) (bh->b_data + offset);
724 /* i_dtime is only 32 bits on disk, but we only care about relative
725 * times in the range of a few minutes (i.e. long enough to sync a
726 * recently-deleted inode to disk), so using the low 32 bits of the
727 * clock (a 68 year range) is enough, see time_before32() */
728 dtime = le32_to_cpu(raw_inode->i_dtime);
729 now = ktime_get_real_seconds();
730 if (buffer_dirty(bh))
731 recentcy += RECENTCY_DIRTY;
733 if (dtime && time_before32(dtime, now) &&
734 time_before32(now, dtime + recentcy))
735 ret = 1;
736 out:
737 brelse(bh);
738 return ret;
741 static int find_inode_bit(struct super_block *sb, ext4_group_t group,
742 struct buffer_head *bitmap, unsigned long *ino)
744 next:
745 *ino = ext4_find_next_zero_bit((unsigned long *)
746 bitmap->b_data,
747 EXT4_INODES_PER_GROUP(sb), *ino);
748 if (*ino >= EXT4_INODES_PER_GROUP(sb))
749 return 0;
751 if ((EXT4_SB(sb)->s_journal == NULL) &&
752 recently_deleted(sb, group, *ino)) {
753 *ino = *ino + 1;
754 if (*ino < EXT4_INODES_PER_GROUP(sb))
755 goto next;
756 return 0;
759 return 1;
763 * There are two policies for allocating an inode. If the new inode is
764 * a directory, then a forward search is made for a block group with both
765 * free space and a low directory-to-inode ratio; if that fails, then of
766 * the groups with above-average free space, that group with the fewest
767 * directories already is chosen.
769 * For other inodes, search forward from the parent directory's block
770 * group to find a free inode.
772 struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
773 umode_t mode, const struct qstr *qstr,
774 __u32 goal, uid_t *owner, __u32 i_flags,
775 int handle_type, unsigned int line_no,
776 int nblocks)
778 struct super_block *sb;
779 struct buffer_head *inode_bitmap_bh = NULL;
780 struct buffer_head *group_desc_bh;
781 ext4_group_t ngroups, group = 0;
782 unsigned long ino = 0;
783 struct inode *inode;
784 struct ext4_group_desc *gdp = NULL;
785 struct ext4_inode_info *ei;
786 struct ext4_sb_info *sbi;
787 int ret2, err;
788 struct inode *ret;
789 ext4_group_t i;
790 ext4_group_t flex_group;
791 struct ext4_group_info *grp;
792 int encrypt = 0;
794 /* Cannot create files in a deleted directory */
795 if (!dir || !dir->i_nlink)
796 return ERR_PTR(-EPERM);
798 sb = dir->i_sb;
799 sbi = EXT4_SB(sb);
801 if (unlikely(ext4_forced_shutdown(sbi)))
802 return ERR_PTR(-EIO);
804 if ((ext4_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
805 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
806 !(i_flags & EXT4_EA_INODE_FL)) {
807 err = fscrypt_get_encryption_info(dir);
808 if (err)
809 return ERR_PTR(err);
810 if (!fscrypt_has_encryption_key(dir))
811 return ERR_PTR(-ENOKEY);
812 encrypt = 1;
815 if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
816 #ifdef CONFIG_EXT4_FS_POSIX_ACL
817 struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
819 if (p) {
820 int acl_size = p->a_count * sizeof(ext4_acl_entry);
822 nblocks += (S_ISDIR(mode) ? 2 : 1) *
823 __ext4_xattr_set_credits(sb, NULL /* inode */,
824 NULL /* block_bh */, acl_size,
825 true /* is_create */);
826 posix_acl_release(p);
828 #endif
830 #ifdef CONFIG_SECURITY
832 int num_security_xattrs = 1;
834 #ifdef CONFIG_INTEGRITY
835 num_security_xattrs++;
836 #endif
838 * We assume that security xattrs are never
839 * more than 1k. In practice they are under
840 * 128 bytes.
842 nblocks += num_security_xattrs *
843 __ext4_xattr_set_credits(sb, NULL /* inode */,
844 NULL /* block_bh */, 1024,
845 true /* is_create */);
847 #endif
848 if (encrypt)
849 nblocks += __ext4_xattr_set_credits(sb,
850 NULL /* inode */, NULL /* block_bh */,
851 FSCRYPT_SET_CONTEXT_MAX_SIZE,
852 true /* is_create */);
855 ngroups = ext4_get_groups_count(sb);
856 trace_ext4_request_inode(dir, mode);
857 inode = new_inode(sb);
858 if (!inode)
859 return ERR_PTR(-ENOMEM);
860 ei = EXT4_I(inode);
863 * Initialize owners and quota early so that we don't have to account
864 * for quota initialization worst case in standard inode creating
865 * transaction
867 if (owner) {
868 inode->i_mode = mode;
869 i_uid_write(inode, owner[0]);
870 i_gid_write(inode, owner[1]);
871 } else if (test_opt(sb, GRPID)) {
872 inode->i_mode = mode;
873 inode->i_uid = current_fsuid();
874 inode->i_gid = dir->i_gid;
875 } else
876 inode_init_owner(inode, dir, mode);
878 if (ext4_has_feature_project(sb) &&
879 ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
880 ei->i_projid = EXT4_I(dir)->i_projid;
881 else
882 ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
884 err = dquot_initialize(inode);
885 if (err)
886 goto out;
888 if (!goal)
889 goal = sbi->s_inode_goal;
891 if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
892 group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
893 ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
894 ret2 = 0;
895 goto got_group;
898 if (S_ISDIR(mode))
899 ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
900 else
901 ret2 = find_group_other(sb, dir, &group, mode);
903 got_group:
904 EXT4_I(dir)->i_last_alloc_group = group;
905 err = -ENOSPC;
906 if (ret2 == -1)
907 goto out;
910 * Normally we will only go through one pass of this loop,
911 * unless we get unlucky and it turns out the group we selected
912 * had its last inode grabbed by someone else.
914 for (i = 0; i < ngroups; i++, ino = 0) {
915 err = -EIO;
917 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
918 if (!gdp)
919 goto out;
922 * Check free inodes count before loading bitmap.
924 if (ext4_free_inodes_count(sb, gdp) == 0)
925 goto next_group;
927 grp = ext4_get_group_info(sb, group);
928 /* Skip groups with already-known suspicious inode tables */
929 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
930 goto next_group;
932 brelse(inode_bitmap_bh);
933 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
934 /* Skip groups with suspicious inode tables */
935 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
936 IS_ERR(inode_bitmap_bh)) {
937 inode_bitmap_bh = NULL;
938 goto next_group;
941 repeat_in_this_group:
942 ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
943 if (!ret2)
944 goto next_group;
946 if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
947 ext4_error(sb, "reserved inode found cleared - "
948 "inode=%lu", ino + 1);
949 goto next_group;
952 if (!handle) {
953 BUG_ON(nblocks <= 0);
954 handle = __ext4_journal_start_sb(dir->i_sb, line_no,
955 handle_type, nblocks,
957 if (IS_ERR(handle)) {
958 err = PTR_ERR(handle);
959 ext4_std_error(sb, err);
960 goto out;
963 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
964 err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
965 if (err) {
966 ext4_std_error(sb, err);
967 goto out;
969 ext4_lock_group(sb, group);
970 ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
971 if (ret2) {
972 /* Someone already took the bit. Repeat the search
973 * with lock held.
975 ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
976 if (ret2) {
977 ext4_set_bit(ino, inode_bitmap_bh->b_data);
978 ret2 = 0;
979 } else {
980 ret2 = 1; /* we didn't grab the inode */
983 ext4_unlock_group(sb, group);
984 ino++; /* the inode bitmap is zero-based */
985 if (!ret2)
986 goto got; /* we grabbed the inode! */
988 if (ino < EXT4_INODES_PER_GROUP(sb))
989 goto repeat_in_this_group;
990 next_group:
991 if (++group == ngroups)
992 group = 0;
994 err = -ENOSPC;
995 goto out;
997 got:
998 BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
999 err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
1000 if (err) {
1001 ext4_std_error(sb, err);
1002 goto out;
1005 BUFFER_TRACE(group_desc_bh, "get_write_access");
1006 err = ext4_journal_get_write_access(handle, group_desc_bh);
1007 if (err) {
1008 ext4_std_error(sb, err);
1009 goto out;
1012 /* We may have to initialize the block bitmap if it isn't already */
1013 if (ext4_has_group_desc_csum(sb) &&
1014 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
1015 struct buffer_head *block_bitmap_bh;
1017 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
1018 if (IS_ERR(block_bitmap_bh)) {
1019 err = PTR_ERR(block_bitmap_bh);
1020 goto out;
1022 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
1023 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
1024 if (err) {
1025 brelse(block_bitmap_bh);
1026 ext4_std_error(sb, err);
1027 goto out;
1030 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
1031 err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
1033 /* recheck and clear flag under lock if we still need to */
1034 ext4_lock_group(sb, group);
1035 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
1036 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
1037 ext4_free_group_clusters_set(sb, gdp,
1038 ext4_free_clusters_after_init(sb, group, gdp));
1039 ext4_block_bitmap_csum_set(sb, group, gdp,
1040 block_bitmap_bh);
1041 ext4_group_desc_csum_set(sb, group, gdp);
1043 ext4_unlock_group(sb, group);
1044 brelse(block_bitmap_bh);
1046 if (err) {
1047 ext4_std_error(sb, err);
1048 goto out;
1052 /* Update the relevant bg descriptor fields */
1053 if (ext4_has_group_desc_csum(sb)) {
1054 int free;
1055 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1057 down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
1058 ext4_lock_group(sb, group); /* while we modify the bg desc */
1059 free = EXT4_INODES_PER_GROUP(sb) -
1060 ext4_itable_unused_count(sb, gdp);
1061 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1062 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
1063 free = 0;
1066 * Check the relative inode number against the last used
1067 * relative inode number in this group. if it is greater
1068 * we need to update the bg_itable_unused count
1070 if (ino > free)
1071 ext4_itable_unused_set(sb, gdp,
1072 (EXT4_INODES_PER_GROUP(sb) - ino));
1073 up_read(&grp->alloc_sem);
1074 } else {
1075 ext4_lock_group(sb, group);
1078 ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
1079 if (S_ISDIR(mode)) {
1080 ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
1081 if (sbi->s_log_groups_per_flex) {
1082 ext4_group_t f = ext4_flex_group(sbi, group);
1084 atomic_inc(&sbi->s_flex_groups[f].used_dirs);
1087 if (ext4_has_group_desc_csum(sb)) {
1088 ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
1089 EXT4_INODES_PER_GROUP(sb) / 8);
1090 ext4_group_desc_csum_set(sb, group, gdp);
1092 ext4_unlock_group(sb, group);
1094 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
1095 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
1096 if (err) {
1097 ext4_std_error(sb, err);
1098 goto out;
1101 percpu_counter_dec(&sbi->s_freeinodes_counter);
1102 if (S_ISDIR(mode))
1103 percpu_counter_inc(&sbi->s_dirs_counter);
1105 if (sbi->s_log_groups_per_flex) {
1106 flex_group = ext4_flex_group(sbi, group);
1107 atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
1110 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1111 /* This is the optimal IO size (for stat), not the fs block size */
1112 inode->i_blocks = 0;
1113 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
1114 current_time(inode);
1116 memset(ei->i_data, 0, sizeof(ei->i_data));
1117 ei->i_dir_start_lookup = 0;
1118 ei->i_disksize = 0;
1120 /* Don't inherit extent flag from directory, amongst others. */
1121 ei->i_flags =
1122 ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
1123 ei->i_flags |= i_flags;
1124 ei->i_file_acl = 0;
1125 ei->i_dtime = 0;
1126 ei->i_block_group = group;
1127 ei->i_last_alloc_group = ~0;
1129 ext4_set_inode_flags(inode);
1130 if (IS_DIRSYNC(inode))
1131 ext4_handle_sync(handle);
1132 if (insert_inode_locked(inode) < 0) {
1134 * Likely a bitmap corruption causing inode to be allocated
1135 * twice.
1137 err = -EIO;
1138 ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
1139 inode->i_ino);
1140 goto out;
1142 inode->i_generation = prandom_u32();
1144 /* Precompute checksum seed for inode metadata */
1145 if (ext4_has_metadata_csum(sb)) {
1146 __u32 csum;
1147 __le32 inum = cpu_to_le32(inode->i_ino);
1148 __le32 gen = cpu_to_le32(inode->i_generation);
1149 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
1150 sizeof(inum));
1151 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
1152 sizeof(gen));
1155 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
1156 ext4_set_inode_state(inode, EXT4_STATE_NEW);
1158 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
1159 ei->i_inline_off = 0;
1160 if (ext4_has_feature_inline_data(sb))
1161 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1162 ret = inode;
1163 err = dquot_alloc_inode(inode);
1164 if (err)
1165 goto fail_drop;
1168 * Since the encryption xattr will always be unique, create it first so
1169 * that it's less likely to end up in an external xattr block and
1170 * prevent its deduplication.
1172 if (encrypt) {
1173 err = fscrypt_inherit_context(dir, inode, handle, true);
1174 if (err)
1175 goto fail_free_drop;
1178 if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
1179 err = ext4_init_acl(handle, inode, dir);
1180 if (err)
1181 goto fail_free_drop;
1183 err = ext4_init_security(handle, inode, dir, qstr);
1184 if (err)
1185 goto fail_free_drop;
1188 if (ext4_has_feature_extents(sb)) {
1189 /* set extent flag only for directory, file and normal symlink*/
1190 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
1191 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
1192 ext4_ext_tree_init(handle, inode);
1196 if (ext4_handle_valid(handle)) {
1197 ei->i_sync_tid = handle->h_transaction->t_tid;
1198 ei->i_datasync_tid = handle->h_transaction->t_tid;
1201 err = ext4_mark_inode_dirty(handle, inode);
1202 if (err) {
1203 ext4_std_error(sb, err);
1204 goto fail_free_drop;
1207 ext4_debug("allocating inode %lu\n", inode->i_ino);
1208 trace_ext4_allocate_inode(inode, dir, mode);
1209 brelse(inode_bitmap_bh);
1210 return ret;
1212 fail_free_drop:
1213 dquot_free_inode(inode);
1214 fail_drop:
1215 clear_nlink(inode);
1216 unlock_new_inode(inode);
1217 out:
1218 dquot_drop(inode);
1219 inode->i_flags |= S_NOQUOTA;
1220 iput(inode);
1221 brelse(inode_bitmap_bh);
1222 return ERR_PTR(err);
1225 /* Verify that we are loading a valid orphan from disk */
1226 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1228 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1229 ext4_group_t block_group;
1230 int bit;
1231 struct buffer_head *bitmap_bh = NULL;
1232 struct inode *inode = NULL;
1233 int err = -EFSCORRUPTED;
1235 if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
1236 goto bad_orphan;
1238 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1239 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1240 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
1241 if (IS_ERR(bitmap_bh)) {
1242 ext4_error(sb, "inode bitmap error %ld for orphan %lu",
1243 ino, PTR_ERR(bitmap_bh));
1244 return (struct inode *) bitmap_bh;
1247 /* Having the inode bit set should be a 100% indicator that this
1248 * is a valid orphan (no e2fsck run on fs). Orphans also include
1249 * inodes that were being truncated, so we can't check i_nlink==0.
1251 if (!ext4_test_bit(bit, bitmap_bh->b_data))
1252 goto bad_orphan;
1254 inode = ext4_iget(sb, ino);
1255 if (IS_ERR(inode)) {
1256 err = PTR_ERR(inode);
1257 ext4_error(sb, "couldn't read orphan inode %lu (err %d)",
1258 ino, err);
1259 return inode;
1263 * If the orphans has i_nlinks > 0 then it should be able to
1264 * be truncated, otherwise it won't be removed from the orphan
1265 * list during processing and an infinite loop will result.
1266 * Similarly, it must not be a bad inode.
1268 if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1269 is_bad_inode(inode))
1270 goto bad_orphan;
1272 if (NEXT_ORPHAN(inode) > max_ino)
1273 goto bad_orphan;
1274 brelse(bitmap_bh);
1275 return inode;
1277 bad_orphan:
1278 ext4_error(sb, "bad orphan inode %lu", ino);
1279 if (bitmap_bh)
1280 printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1281 bit, (unsigned long long)bitmap_bh->b_blocknr,
1282 ext4_test_bit(bit, bitmap_bh->b_data));
1283 if (inode) {
1284 printk(KERN_ERR "is_bad_inode(inode)=%d\n",
1285 is_bad_inode(inode));
1286 printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
1287 NEXT_ORPHAN(inode));
1288 printk(KERN_ERR "max_ino=%lu\n", max_ino);
1289 printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
1290 /* Avoid freeing blocks if we got a bad deleted inode */
1291 if (inode->i_nlink == 0)
1292 inode->i_blocks = 0;
1293 iput(inode);
1295 brelse(bitmap_bh);
1296 return ERR_PTR(err);
1299 unsigned long ext4_count_free_inodes(struct super_block *sb)
1301 unsigned long desc_count;
1302 struct ext4_group_desc *gdp;
1303 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1304 #ifdef EXT4FS_DEBUG
1305 struct ext4_super_block *es;
1306 unsigned long bitmap_count, x;
1307 struct buffer_head *bitmap_bh = NULL;
1309 es = EXT4_SB(sb)->s_es;
1310 desc_count = 0;
1311 bitmap_count = 0;
1312 gdp = NULL;
1313 for (i = 0; i < ngroups; i++) {
1314 gdp = ext4_get_group_desc(sb, i, NULL);
1315 if (!gdp)
1316 continue;
1317 desc_count += ext4_free_inodes_count(sb, gdp);
1318 brelse(bitmap_bh);
1319 bitmap_bh = ext4_read_inode_bitmap(sb, i);
1320 if (IS_ERR(bitmap_bh)) {
1321 bitmap_bh = NULL;
1322 continue;
1325 x = ext4_count_free(bitmap_bh->b_data,
1326 EXT4_INODES_PER_GROUP(sb) / 8);
1327 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1328 (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1329 bitmap_count += x;
1331 brelse(bitmap_bh);
1332 printk(KERN_DEBUG "ext4_count_free_inodes: "
1333 "stored = %u, computed = %lu, %lu\n",
1334 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1335 return desc_count;
1336 #else
1337 desc_count = 0;
1338 for (i = 0; i < ngroups; i++) {
1339 gdp = ext4_get_group_desc(sb, i, NULL);
1340 if (!gdp)
1341 continue;
1342 desc_count += ext4_free_inodes_count(sb, gdp);
1343 cond_resched();
1345 return desc_count;
1346 #endif
1349 /* Called at mount-time, super-block is locked */
1350 unsigned long ext4_count_dirs(struct super_block * sb)
1352 unsigned long count = 0;
1353 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1355 for (i = 0; i < ngroups; i++) {
1356 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1357 if (!gdp)
1358 continue;
1359 count += ext4_used_dirs_count(sb, gdp);
1361 return count;
1365 * Zeroes not yet zeroed inode table - just write zeroes through the whole
1366 * inode table. Must be called without any spinlock held. The only place
1367 * where it is called from on active part of filesystem is ext4lazyinit
1368 * thread, so we do not need any special locks, however we have to prevent
1369 * inode allocation from the current group, so we take alloc_sem lock, to
1370 * block ext4_new_inode() until we are finished.
1372 int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1373 int barrier)
1375 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1376 struct ext4_sb_info *sbi = EXT4_SB(sb);
1377 struct ext4_group_desc *gdp = NULL;
1378 struct buffer_head *group_desc_bh;
1379 handle_t *handle;
1380 ext4_fsblk_t blk;
1381 int num, ret = 0, used_blks = 0;
1383 /* This should not happen, but just to be sure check this */
1384 if (sb_rdonly(sb)) {
1385 ret = 1;
1386 goto out;
1389 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1390 if (!gdp)
1391 goto out;
1394 * We do not need to lock this, because we are the only one
1395 * handling this flag.
1397 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1398 goto out;
1400 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1401 if (IS_ERR(handle)) {
1402 ret = PTR_ERR(handle);
1403 goto out;
1406 down_write(&grp->alloc_sem);
1408 * If inode bitmap was already initialized there may be some
1409 * used inodes so we need to skip blocks with used inodes in
1410 * inode table.
1412 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
1413 used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
1414 ext4_itable_unused_count(sb, gdp)),
1415 sbi->s_inodes_per_block);
1417 if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
1418 ext4_error(sb, "Something is wrong with group %u: "
1419 "used itable blocks: %d; "
1420 "itable unused count: %u",
1421 group, used_blks,
1422 ext4_itable_unused_count(sb, gdp));
1423 ret = 1;
1424 goto err_out;
1427 blk = ext4_inode_table(sb, gdp) + used_blks;
1428 num = sbi->s_itb_per_group - used_blks;
1430 BUFFER_TRACE(group_desc_bh, "get_write_access");
1431 ret = ext4_journal_get_write_access(handle,
1432 group_desc_bh);
1433 if (ret)
1434 goto err_out;
1437 * Skip zeroout if the inode table is full. But we set the ZEROED
1438 * flag anyway, because obviously, when it is full it does not need
1439 * further zeroing.
1441 if (unlikely(num == 0))
1442 goto skip_zeroout;
1444 ext4_debug("going to zero out inode table in group %d\n",
1445 group);
1446 ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1447 if (ret < 0)
1448 goto err_out;
1449 if (barrier)
1450 blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
1452 skip_zeroout:
1453 ext4_lock_group(sb, group);
1454 gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1455 ext4_group_desc_csum_set(sb, group, gdp);
1456 ext4_unlock_group(sb, group);
1458 BUFFER_TRACE(group_desc_bh,
1459 "call ext4_handle_dirty_metadata");
1460 ret = ext4_handle_dirty_metadata(handle, NULL,
1461 group_desc_bh);
1463 err_out:
1464 up_write(&grp->alloc_sem);
1465 ext4_journal_stop(handle);
1466 out:
1467 return ret;