blob: 246ff3024e546e1f005ff73c4ff6eea041610e2c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/fs/ext4/ialloc.c
4 *
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)
9 *
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
14 */
15
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>
26
27#include <asm/byteorder.h>
28
29#include "ext4.h"
30#include "ext4_jbd2.h"
31#include "xattr.h"
32#include "acl.h"
33
34#include <trace/events/ext4.h>
35
36/*
37 * ialloc.c contains the inodes allocation and deallocation routines
38 */
39
40/*
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.
44 *
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.
48 */
49
50/*
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.
54 */
55void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
56{
57 int i;
58
59 if (start_bit >= end_bit)
60 return;
61
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);
67}
68
69void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate)
70{
71 if (uptodate) {
72 set_buffer_uptodate(bh);
73 set_bitmap_uptodate(bh);
74 }
75 unlock_buffer(bh);
76 put_bh(bh);
77}
78
79static int ext4_validate_inode_bitmap(struct super_block *sb,
80 struct ext4_group_desc *desc,
81 ext4_group_t block_group,
82 struct buffer_head *bh)
83{
84 ext4_fsblk_t blk;
85 struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
86
87 if (buffer_verified(bh))
88 return 0;
89 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
90 return -EFSCORRUPTED;
91
92 ext4_lock_group(sb, block_group);
93 if (buffer_verified(bh))
94 goto verified;
95 blk = ext4_inode_bitmap(sb, desc);
96 if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
97 EXT4_INODES_PER_GROUP(sb) / 8)) {
98 ext4_unlock_group(sb, block_group);
99 ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
100 "inode_bitmap = %llu", block_group, blk);
101 ext4_mark_group_bitmap_corrupted(sb, block_group,
102 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
103 return -EFSBADCRC;
104 }
105 set_buffer_verified(bh);
106verified:
107 ext4_unlock_group(sb, block_group);
108 return 0;
109}
110
111/*
112 * Read the inode allocation bitmap for a given block_group, reading
113 * into the specified slot in the superblock's bitmap cache.
114 *
115 * Return buffer_head of bitmap on success or NULL.
116 */
117static struct buffer_head *
118ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
119{
120 struct ext4_group_desc *desc;
121 struct ext4_sb_info *sbi = EXT4_SB(sb);
122 struct buffer_head *bh = NULL;
123 ext4_fsblk_t bitmap_blk;
124 int err;
125
126 desc = ext4_get_group_desc(sb, block_group, NULL);
127 if (!desc)
128 return ERR_PTR(-EFSCORRUPTED);
129
130 bitmap_blk = ext4_inode_bitmap(sb, desc);
131 if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
132 (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
133 ext4_error(sb, "Invalid inode bitmap blk %llu in "
134 "block_group %u", bitmap_blk, block_group);
135 ext4_mark_group_bitmap_corrupted(sb, block_group,
136 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
137 return ERR_PTR(-EFSCORRUPTED);
138 }
139 bh = sb_getblk(sb, bitmap_blk);
140 if (unlikely(!bh)) {
141 ext4_warning(sb, "Cannot read inode bitmap - "
142 "block_group = %u, inode_bitmap = %llu",
143 block_group, bitmap_blk);
144 return ERR_PTR(-ENOMEM);
145 }
146 if (bitmap_uptodate(bh))
147 goto verify;
148
149 lock_buffer(bh);
150 if (bitmap_uptodate(bh)) {
151 unlock_buffer(bh);
152 goto verify;
153 }
154
155 ext4_lock_group(sb, block_group);
156 if (ext4_has_group_desc_csum(sb) &&
157 (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
158 if (block_group == 0) {
159 ext4_unlock_group(sb, block_group);
160 unlock_buffer(bh);
161 ext4_error(sb, "Inode bitmap for bg 0 marked "
162 "uninitialized");
163 err = -EFSCORRUPTED;
164 goto out;
165 }
166 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
167 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
168 sb->s_blocksize * 8, bh->b_data);
169 set_bitmap_uptodate(bh);
170 set_buffer_uptodate(bh);
171 set_buffer_verified(bh);
172 ext4_unlock_group(sb, block_group);
173 unlock_buffer(bh);
174 return bh;
175 }
176 ext4_unlock_group(sb, block_group);
177
178 if (buffer_uptodate(bh)) {
179 /*
180 * if not uninit if bh is uptodate,
181 * bitmap is also uptodate
182 */
183 set_bitmap_uptodate(bh);
184 unlock_buffer(bh);
185 goto verify;
186 }
187 /*
188 * submit the buffer_head for reading
189 */
190 trace_ext4_load_inode_bitmap(sb, block_group);
191 bh->b_end_io = ext4_end_bitmap_read;
192 get_bh(bh);
193 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
194 wait_on_buffer(bh);
195 if (!buffer_uptodate(bh)) {
196 put_bh(bh);
197 ext4_error(sb, "Cannot read inode bitmap - "
198 "block_group = %u, inode_bitmap = %llu",
199 block_group, bitmap_blk);
200 ext4_mark_group_bitmap_corrupted(sb, block_group,
201 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
202 return ERR_PTR(-EIO);
203 }
204
205verify:
206 err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
207 if (err)
208 goto out;
209 return bh;
210out:
211 put_bh(bh);
212 return ERR_PTR(err);
213}
214
215/*
216 * NOTE! When we get the inode, we're the only people
217 * that have access to it, and as such there are no
218 * race conditions we have to worry about. The inode
219 * is not on the hash-lists, and it cannot be reached
220 * through the filesystem because the directory entry
221 * has been deleted earlier.
222 *
223 * HOWEVER: we must make sure that we get no aliases,
224 * which means that we have to call "clear_inode()"
225 * _before_ we mark the inode not in use in the inode
226 * bitmaps. Otherwise a newly created file might use
227 * the same inode number (not actually the same pointer
228 * though), and then we'd have two inodes sharing the
229 * same inode number and space on the harddisk.
230 */
231void ext4_free_inode(handle_t *handle, struct inode *inode)
232{
233 struct super_block *sb = inode->i_sb;
234 int is_directory;
235 unsigned long ino;
236 struct buffer_head *bitmap_bh = NULL;
237 struct buffer_head *bh2;
238 ext4_group_t block_group;
239 unsigned long bit;
240 struct ext4_group_desc *gdp;
241 struct ext4_super_block *es;
242 struct ext4_sb_info *sbi;
243 int fatal = 0, err, count, cleared;
244 struct ext4_group_info *grp;
245
246 if (!sb) {
247 printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
248 "nonexistent device\n", __func__, __LINE__);
249 return;
250 }
251 if (atomic_read(&inode->i_count) > 1) {
252 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
253 __func__, __LINE__, inode->i_ino,
254 atomic_read(&inode->i_count));
255 return;
256 }
257 if (inode->i_nlink) {
258 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
259 __func__, __LINE__, inode->i_ino, inode->i_nlink);
260 return;
261 }
262 sbi = EXT4_SB(sb);
263
264 ino = inode->i_ino;
265 ext4_debug("freeing inode %lu\n", ino);
266 trace_ext4_free_inode(inode);
267
268 dquot_initialize(inode);
269 dquot_free_inode(inode);
270
271 is_directory = S_ISDIR(inode->i_mode);
272
273 /* Do this BEFORE marking the inode not in use or returning an error */
274 ext4_clear_inode(inode);
275
276 es = sbi->s_es;
277 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
278 ext4_error(sb, "reserved or nonexistent inode %lu", ino);
279 goto error_return;
280 }
281 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
282 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
283 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
284 /* Don't bother if the inode bitmap is corrupt. */
285 grp = ext4_get_group_info(sb, block_group);
286 if (IS_ERR(bitmap_bh)) {
287 fatal = PTR_ERR(bitmap_bh);
288 bitmap_bh = NULL;
289 goto error_return;
290 }
291 if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
292 fatal = -EFSCORRUPTED;
293 goto error_return;
294 }
295
296 BUFFER_TRACE(bitmap_bh, "get_write_access");
297 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
298 if (fatal)
299 goto error_return;
300
301 fatal = -ESRCH;
302 gdp = ext4_get_group_desc(sb, block_group, &bh2);
303 if (gdp) {
304 BUFFER_TRACE(bh2, "get_write_access");
305 fatal = ext4_journal_get_write_access(handle, bh2);
306 }
307 ext4_lock_group(sb, block_group);
308 cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
309 if (fatal || !cleared) {
310 ext4_unlock_group(sb, block_group);
311 goto out;
312 }
313
314 count = ext4_free_inodes_count(sb, gdp) + 1;
315 ext4_free_inodes_set(sb, gdp, count);
316 if (is_directory) {
317 count = ext4_used_dirs_count(sb, gdp) - 1;
318 ext4_used_dirs_set(sb, gdp, count);
319 percpu_counter_dec(&sbi->s_dirs_counter);
320 }
321 ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
322 EXT4_INODES_PER_GROUP(sb) / 8);
323 ext4_group_desc_csum_set(sb, block_group, gdp);
324 ext4_unlock_group(sb, block_group);
325
326 percpu_counter_inc(&sbi->s_freeinodes_counter);
327 if (sbi->s_log_groups_per_flex) {
328 struct flex_groups *fg;
329
330 fg = sbi_array_rcu_deref(sbi, s_flex_groups,
331 ext4_flex_group(sbi, block_group));
332 atomic_inc(&fg->free_inodes);
333 if (is_directory)
334 atomic_dec(&fg->used_dirs);
335 }
336 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
337 fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
338out:
339 if (cleared) {
340 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
341 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
342 if (!fatal)
343 fatal = err;
344 } else {
345 ext4_error(sb, "bit already cleared for inode %lu", ino);
346 ext4_mark_group_bitmap_corrupted(sb, block_group,
347 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
348 }
349
350error_return:
351 brelse(bitmap_bh);
352 ext4_std_error(sb, fatal);
353}
354
355struct orlov_stats {
356 __u64 free_clusters;
357 __u32 free_inodes;
358 __u32 used_dirs;
359};
360
361/*
362 * Helper function for Orlov's allocator; returns critical information
363 * for a particular block group or flex_bg. If flex_size is 1, then g
364 * is a block group number; otherwise it is flex_bg number.
365 */
366static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
367 int flex_size, struct orlov_stats *stats)
368{
369 struct ext4_group_desc *desc;
370
371 if (flex_size > 1) {
372 struct flex_groups *fg = sbi_array_rcu_deref(EXT4_SB(sb),
373 s_flex_groups, g);
374 stats->free_inodes = atomic_read(&fg->free_inodes);
375 stats->free_clusters = atomic64_read(&fg->free_clusters);
376 stats->used_dirs = atomic_read(&fg->used_dirs);
377 return;
378 }
379
380 desc = ext4_get_group_desc(sb, g, NULL);
381 if (desc) {
382 stats->free_inodes = ext4_free_inodes_count(sb, desc);
383 stats->free_clusters = ext4_free_group_clusters(sb, desc);
384 stats->used_dirs = ext4_used_dirs_count(sb, desc);
385 } else {
386 stats->free_inodes = 0;
387 stats->free_clusters = 0;
388 stats->used_dirs = 0;
389 }
390}
391
392/*
393 * Orlov's allocator for directories.
394 *
395 * We always try to spread first-level directories.
396 *
397 * If there are blockgroups with both free inodes and free clusters counts
398 * not worse than average we return one with smallest directory count.
399 * Otherwise we simply return a random group.
400 *
401 * For the rest rules look so:
402 *
403 * It's OK to put directory into a group unless
404 * it has too many directories already (max_dirs) or
405 * it has too few free inodes left (min_inodes) or
406 * it has too few free clusters left (min_clusters) or
407 * Parent's group is preferred, if it doesn't satisfy these
408 * conditions we search cyclically through the rest. If none
409 * of the groups look good we just look for a group with more
410 * free inodes than average (starting at parent's group).
411 */
412
413static int find_group_orlov(struct super_block *sb, struct inode *parent,
414 ext4_group_t *group, umode_t mode,
415 const struct qstr *qstr)
416{
417 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
418 struct ext4_sb_info *sbi = EXT4_SB(sb);
419 ext4_group_t real_ngroups = ext4_get_groups_count(sb);
420 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
421 unsigned int freei, avefreei, grp_free;
422 ext4_fsblk_t freec, avefreec;
423 unsigned int ndirs;
424 int max_dirs, min_inodes;
425 ext4_grpblk_t min_clusters;
426 ext4_group_t i, grp, g, ngroups;
427 struct ext4_group_desc *desc;
428 struct orlov_stats stats;
429 int flex_size = ext4_flex_bg_size(sbi);
430 struct dx_hash_info hinfo;
431
432 ngroups = real_ngroups;
433 if (flex_size > 1) {
434 ngroups = (real_ngroups + flex_size - 1) >>
435 sbi->s_log_groups_per_flex;
436 parent_group >>= sbi->s_log_groups_per_flex;
437 }
438
439 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
440 avefreei = freei / ngroups;
441 freec = percpu_counter_read_positive(&sbi->s_freeclusters_counter);
442 avefreec = freec;
443 do_div(avefreec, ngroups);
444 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
445
446 if (S_ISDIR(mode) &&
447 ((parent == d_inode(sb->s_root)) ||
448 (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
449 int best_ndir = inodes_per_group;
450 int ret = -1;
451
452 if (qstr) {
453 hinfo.hash_version = DX_HASH_HALF_MD4;
454 hinfo.seed = sbi->s_hash_seed;
455 ext4fs_dirhash(parent, qstr->name, qstr->len, &hinfo);
456 grp = hinfo.hash;
457 } else
458 grp = prandom_u32();
459 parent_group = (unsigned)grp % ngroups;
460 for (i = 0; i < ngroups; i++) {
461 g = (parent_group + i) % ngroups;
462 get_orlov_stats(sb, g, flex_size, &stats);
463 if (!stats.free_inodes)
464 continue;
465 if (stats.used_dirs >= best_ndir)
466 continue;
467 if (stats.free_inodes < avefreei)
468 continue;
469 if (stats.free_clusters < avefreec)
470 continue;
471 grp = g;
472 ret = 0;
473 best_ndir = stats.used_dirs;
474 }
475 if (ret)
476 goto fallback;
477 found_flex_bg:
478 if (flex_size == 1) {
479 *group = grp;
480 return 0;
481 }
482
483 /*
484 * We pack inodes at the beginning of the flexgroup's
485 * inode tables. Block allocation decisions will do
486 * something similar, although regular files will
487 * start at 2nd block group of the flexgroup. See
488 * ext4_ext_find_goal() and ext4_find_near().
489 */
490 grp *= flex_size;
491 for (i = 0; i < flex_size; i++) {
492 if (grp+i >= real_ngroups)
493 break;
494 desc = ext4_get_group_desc(sb, grp+i, NULL);
495 if (desc && ext4_free_inodes_count(sb, desc)) {
496 *group = grp+i;
497 return 0;
498 }
499 }
500 goto fallback;
501 }
502
503 max_dirs = ndirs / ngroups + inodes_per_group*flex_size / 16;
504 min_inodes = avefreei - inodes_per_group*flex_size / 4;
505 if (min_inodes < 1)
506 min_inodes = 1;
507 min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
508 if (min_clusters < 0)
509 min_clusters = 0;
510
511 /*
512 * Start looking in the flex group where we last allocated an
513 * inode for this parent directory
514 */
515 if (EXT4_I(parent)->i_last_alloc_group != ~0) {
516 parent_group = EXT4_I(parent)->i_last_alloc_group;
517 if (flex_size > 1)
518 parent_group >>= sbi->s_log_groups_per_flex;
519 }
520
521 for (i = 0; i < ngroups; i++) {
522 grp = (parent_group + i) % ngroups;
523 get_orlov_stats(sb, grp, flex_size, &stats);
524 if (stats.used_dirs >= max_dirs)
525 continue;
526 if (stats.free_inodes < min_inodes)
527 continue;
528 if (stats.free_clusters < min_clusters)
529 continue;
530 goto found_flex_bg;
531 }
532
533fallback:
534 ngroups = real_ngroups;
535 avefreei = freei / ngroups;
536fallback_retry:
537 parent_group = EXT4_I(parent)->i_block_group;
538 for (i = 0; i < ngroups; i++) {
539 grp = (parent_group + i) % ngroups;
540 desc = ext4_get_group_desc(sb, grp, NULL);
541 if (desc) {
542 grp_free = ext4_free_inodes_count(sb, desc);
543 if (grp_free && grp_free >= avefreei) {
544 *group = grp;
545 return 0;
546 }
547 }
548 }
549
550 if (avefreei) {
551 /*
552 * The free-inodes counter is approximate, and for really small
553 * filesystems the above test can fail to find any blockgroups
554 */
555 avefreei = 0;
556 goto fallback_retry;
557 }
558
559 return -1;
560}
561
562static int find_group_other(struct super_block *sb, struct inode *parent,
563 ext4_group_t *group, umode_t mode)
564{
565 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
566 ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
567 struct ext4_group_desc *desc;
568 int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
569
570 /*
571 * Try to place the inode is the same flex group as its
572 * parent. If we can't find space, use the Orlov algorithm to
573 * find another flex group, and store that information in the
574 * parent directory's inode information so that use that flex
575 * group for future allocations.
576 */
577 if (flex_size > 1) {
578 int retry = 0;
579
580 try_again:
581 parent_group &= ~(flex_size-1);
582 last = parent_group + flex_size;
583 if (last > ngroups)
584 last = ngroups;
585 for (i = parent_group; i < last; i++) {
586 desc = ext4_get_group_desc(sb, i, NULL);
587 if (desc && ext4_free_inodes_count(sb, desc)) {
588 *group = i;
589 return 0;
590 }
591 }
592 if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
593 retry = 1;
594 parent_group = EXT4_I(parent)->i_last_alloc_group;
595 goto try_again;
596 }
597 /*
598 * If this didn't work, use the Orlov search algorithm
599 * to find a new flex group; we pass in the mode to
600 * avoid the topdir algorithms.
601 */
602 *group = parent_group + flex_size;
603 if (*group > ngroups)
604 *group = 0;
605 return find_group_orlov(sb, parent, group, mode, NULL);
606 }
607
608 /*
609 * Try to place the inode in its parent directory
610 */
611 *group = parent_group;
612 desc = ext4_get_group_desc(sb, *group, NULL);
613 if (desc && ext4_free_inodes_count(sb, desc) &&
614 ext4_free_group_clusters(sb, desc))
615 return 0;
616
617 /*
618 * We're going to place this inode in a different blockgroup from its
619 * parent. We want to cause files in a common directory to all land in
620 * the same blockgroup. But we want files which are in a different
621 * directory which shares a blockgroup with our parent to land in a
622 * different blockgroup.
623 *
624 * So add our directory's i_ino into the starting point for the hash.
625 */
626 *group = (*group + parent->i_ino) % ngroups;
627
628 /*
629 * Use a quadratic hash to find a group with a free inode and some free
630 * blocks.
631 */
632 for (i = 1; i < ngroups; i <<= 1) {
633 *group += i;
634 if (*group >= ngroups)
635 *group -= ngroups;
636 desc = ext4_get_group_desc(sb, *group, NULL);
637 if (desc && ext4_free_inodes_count(sb, desc) &&
638 ext4_free_group_clusters(sb, desc))
639 return 0;
640 }
641
642 /*
643 * That failed: try linear search for a free inode, even if that group
644 * has no free blocks.
645 */
646 *group = parent_group;
647 for (i = 0; i < ngroups; i++) {
648 if (++*group >= ngroups)
649 *group = 0;
650 desc = ext4_get_group_desc(sb, *group, NULL);
651 if (desc && ext4_free_inodes_count(sb, desc))
652 return 0;
653 }
654
655 return -1;
656}
657
658/*
659 * In no journal mode, if an inode has recently been deleted, we want
660 * to avoid reusing it until we're reasonably sure the inode table
661 * block has been written back to disk. (Yes, these values are
662 * somewhat arbitrary...)
663 */
664#define RECENTCY_MIN 60
665#define RECENTCY_DIRTY 300
666
667static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
668{
669 struct ext4_group_desc *gdp;
670 struct ext4_inode *raw_inode;
671 struct buffer_head *bh;
672 int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
673 int offset, ret = 0;
674 int recentcy = RECENTCY_MIN;
675 u32 dtime, now;
676
677 gdp = ext4_get_group_desc(sb, group, NULL);
678 if (unlikely(!gdp))
679 return 0;
680
681 bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
682 (ino / inodes_per_block));
683 if (!bh || !buffer_uptodate(bh))
684 /*
685 * If the block is not in the buffer cache, then it
686 * must have been written out.
687 */
688 goto out;
689
690 offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
691 raw_inode = (struct ext4_inode *) (bh->b_data + offset);
692
693 /* i_dtime is only 32 bits on disk, but we only care about relative
694 * times in the range of a few minutes (i.e. long enough to sync a
695 * recently-deleted inode to disk), so using the low 32 bits of the
696 * clock (a 68 year range) is enough, see time_before32() */
697 dtime = le32_to_cpu(raw_inode->i_dtime);
698 now = ktime_get_real_seconds();
699 if (buffer_dirty(bh))
700 recentcy += RECENTCY_DIRTY;
701
702 if (dtime && time_before32(dtime, now) &&
703 time_before32(now, dtime + recentcy))
704 ret = 1;
705out:
706 brelse(bh);
707 return ret;
708}
709
710static int find_inode_bit(struct super_block *sb, ext4_group_t group,
711 struct buffer_head *bitmap, unsigned long *ino)
712{
713next:
714 *ino = ext4_find_next_zero_bit((unsigned long *)
715 bitmap->b_data,
716 EXT4_INODES_PER_GROUP(sb), *ino);
717 if (*ino >= EXT4_INODES_PER_GROUP(sb))
718 return 0;
719
720 if ((EXT4_SB(sb)->s_journal == NULL) &&
721 recently_deleted(sb, group, *ino)) {
722 *ino = *ino + 1;
723 if (*ino < EXT4_INODES_PER_GROUP(sb))
724 goto next;
725 return 0;
726 }
727
728 return 1;
729}
730
731static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
732 bool encrypt)
733{
734 struct super_block *sb = dir->i_sb;
735 int nblocks = 0;
736#ifdef CONFIG_EXT4_FS_POSIX_ACL
737 struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
738
739 if (IS_ERR(p))
740 return PTR_ERR(p);
741 if (p) {
742 int acl_size = p->a_count * sizeof(ext4_acl_entry);
743
744 nblocks += (S_ISDIR(mode) ? 2 : 1) *
745 __ext4_xattr_set_credits(sb, NULL /* inode */,
746 NULL /* block_bh */, acl_size,
747 true /* is_create */);
748 posix_acl_release(p);
749 }
750#endif
751
752#ifdef CONFIG_SECURITY
753 {
754 int num_security_xattrs = 1;
755
756#ifdef CONFIG_INTEGRITY
757 num_security_xattrs++;
758#endif
759 /*
760 * We assume that security xattrs are never more than 1k.
761 * In practice they are under 128 bytes.
762 */
763 nblocks += num_security_xattrs *
764 __ext4_xattr_set_credits(sb, NULL /* inode */,
765 NULL /* block_bh */, 1024,
766 true /* is_create */);
767 }
768#endif
769 if (encrypt)
770 nblocks += __ext4_xattr_set_credits(sb,
771 NULL /* inode */,
772 NULL /* block_bh */,
773 FSCRYPT_SET_CONTEXT_MAX_SIZE,
774 true /* is_create */);
775 return nblocks;
776}
777
778/*
779 * There are two policies for allocating an inode. If the new inode is
780 * a directory, then a forward search is made for a block group with both
781 * free space and a low directory-to-inode ratio; if that fails, then of
782 * the groups with above-average free space, that group with the fewest
783 * directories already is chosen.
784 *
785 * For other inodes, search forward from the parent directory's block
786 * group to find a free inode.
787 */
788struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
789 umode_t mode, const struct qstr *qstr,
790 __u32 goal, uid_t *owner, __u32 i_flags,
791 int handle_type, unsigned int line_no,
792 int nblocks)
793{
794 struct super_block *sb;
795 struct buffer_head *inode_bitmap_bh = NULL;
796 struct buffer_head *group_desc_bh;
797 ext4_group_t ngroups, group = 0;
798 unsigned long ino = 0;
799 struct inode *inode;
800 struct ext4_group_desc *gdp = NULL;
801 struct ext4_inode_info *ei;
802 struct ext4_sb_info *sbi;
803 int ret2, err;
804 struct inode *ret;
805 ext4_group_t i;
806 ext4_group_t flex_group;
807 struct ext4_group_info *grp;
808 bool encrypt = false;
809
810 /* Cannot create files in a deleted directory */
811 if (!dir || !dir->i_nlink)
812 return ERR_PTR(-EPERM);
813
814 sb = dir->i_sb;
815 sbi = EXT4_SB(sb);
816
817 if (unlikely(ext4_forced_shutdown(sbi)))
818 return ERR_PTR(-EIO);
819
820 ngroups = ext4_get_groups_count(sb);
821 trace_ext4_request_inode(dir, mode);
822 inode = new_inode(sb);
823 if (!inode)
824 return ERR_PTR(-ENOMEM);
825 ei = EXT4_I(inode);
826
827 /*
828 * Initialize owners and quota early so that we don't have to account
829 * for quota initialization worst case in standard inode creating
830 * transaction
831 */
832 if (owner) {
833 inode->i_mode = mode;
834 i_uid_write(inode, owner[0]);
835 i_gid_write(inode, owner[1]);
836 } else if (test_opt(sb, GRPID)) {
837 inode->i_mode = mode;
838 inode->i_uid = current_fsuid();
839 inode->i_gid = dir->i_gid;
840 } else
841 inode_init_owner(inode, dir, mode);
842
843 if (ext4_has_feature_project(sb) &&
844 ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
845 ei->i_projid = EXT4_I(dir)->i_projid;
846 else
847 ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
848
849 if (!(i_flags & EXT4_EA_INODE_FL)) {
850 err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
851 if (err)
852 goto out;
853 }
854
855 err = dquot_initialize(inode);
856 if (err)
857 goto out;
858
859 if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
860 ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
861 if (ret2 < 0) {
862 err = ret2;
863 goto out;
864 }
865 nblocks += ret2;
866 }
867
868 if (!goal)
869 goal = sbi->s_inode_goal;
870
871 if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
872 group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
873 ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
874 ret2 = 0;
875 goto got_group;
876 }
877
878 if (S_ISDIR(mode))
879 ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
880 else
881 ret2 = find_group_other(sb, dir, &group, mode);
882
883got_group:
884 EXT4_I(dir)->i_last_alloc_group = group;
885 err = -ENOSPC;
886 if (ret2 == -1)
887 goto out;
888
889 /*
890 * Normally we will only go through one pass of this loop,
891 * unless we get unlucky and it turns out the group we selected
892 * had its last inode grabbed by someone else.
893 */
894 for (i = 0; i < ngroups; i++, ino = 0) {
895 err = -EIO;
896
897 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
898 if (!gdp)
899 goto out;
900
901 /*
902 * Check free inodes count before loading bitmap.
903 */
904 if (ext4_free_inodes_count(sb, gdp) == 0)
905 goto next_group;
906
907 grp = ext4_get_group_info(sb, group);
908 /* Skip groups with already-known suspicious inode tables */
909 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
910 goto next_group;
911
912 brelse(inode_bitmap_bh);
913 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
914 /* Skip groups with suspicious inode tables */
915 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
916 IS_ERR(inode_bitmap_bh)) {
917 inode_bitmap_bh = NULL;
918 goto next_group;
919 }
920
921repeat_in_this_group:
922 ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
923 if (!ret2)
924 goto next_group;
925
926 if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
927 ext4_error(sb, "reserved inode found cleared - "
928 "inode=%lu", ino + 1);
929 ext4_mark_group_bitmap_corrupted(sb, group,
930 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
931 goto next_group;
932 }
933
934 if (!handle) {
935 BUG_ON(nblocks <= 0);
936 handle = __ext4_journal_start_sb(dir->i_sb, line_no,
937 handle_type, nblocks,
938 0);
939 if (IS_ERR(handle)) {
940 err = PTR_ERR(handle);
941 ext4_std_error(sb, err);
942 goto out;
943 }
944 }
945 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
946 err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
947 if (err) {
948 ext4_std_error(sb, err);
949 goto out;
950 }
951 ext4_lock_group(sb, group);
952 ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
953 if (ret2) {
954 /* Someone already took the bit. Repeat the search
955 * with lock held.
956 */
957 ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
958 if (ret2) {
959 ext4_set_bit(ino, inode_bitmap_bh->b_data);
960 ret2 = 0;
961 } else {
962 ret2 = 1; /* we didn't grab the inode */
963 }
964 }
965 ext4_unlock_group(sb, group);
966 ino++; /* the inode bitmap is zero-based */
967 if (!ret2)
968 goto got; /* we grabbed the inode! */
969
970 if (ino < EXT4_INODES_PER_GROUP(sb))
971 goto repeat_in_this_group;
972next_group:
973 if (++group == ngroups)
974 group = 0;
975 }
976 err = -ENOSPC;
977 goto out;
978
979got:
980 BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
981 err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
982 if (err) {
983 ext4_std_error(sb, err);
984 goto out;
985 }
986
987 BUFFER_TRACE(group_desc_bh, "get_write_access");
988 err = ext4_journal_get_write_access(handle, group_desc_bh);
989 if (err) {
990 ext4_std_error(sb, err);
991 goto out;
992 }
993
994 /* We may have to initialize the block bitmap if it isn't already */
995 if (ext4_has_group_desc_csum(sb) &&
996 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
997 struct buffer_head *block_bitmap_bh;
998
999 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
1000 if (IS_ERR(block_bitmap_bh)) {
1001 err = PTR_ERR(block_bitmap_bh);
1002 goto out;
1003 }
1004 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
1005 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
1006 if (err) {
1007 brelse(block_bitmap_bh);
1008 ext4_std_error(sb, err);
1009 goto out;
1010 }
1011
1012 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
1013 err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
1014
1015 /* recheck and clear flag under lock if we still need to */
1016 ext4_lock_group(sb, group);
1017 if (ext4_has_group_desc_csum(sb) &&
1018 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
1019 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
1020 ext4_free_group_clusters_set(sb, gdp,
1021 ext4_free_clusters_after_init(sb, group, gdp));
1022 ext4_block_bitmap_csum_set(sb, group, gdp,
1023 block_bitmap_bh);
1024 ext4_group_desc_csum_set(sb, group, gdp);
1025 }
1026 ext4_unlock_group(sb, group);
1027 brelse(block_bitmap_bh);
1028
1029 if (err) {
1030 ext4_std_error(sb, err);
1031 goto out;
1032 }
1033 }
1034
1035 /* Update the relevant bg descriptor fields */
1036 if (ext4_has_group_desc_csum(sb)) {
1037 int free;
1038 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1039
1040 down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
1041 ext4_lock_group(sb, group); /* while we modify the bg desc */
1042 free = EXT4_INODES_PER_GROUP(sb) -
1043 ext4_itable_unused_count(sb, gdp);
1044 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1045 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
1046 free = 0;
1047 }
1048 /*
1049 * Check the relative inode number against the last used
1050 * relative inode number in this group. if it is greater
1051 * we need to update the bg_itable_unused count
1052 */
1053 if (ino > free)
1054 ext4_itable_unused_set(sb, gdp,
1055 (EXT4_INODES_PER_GROUP(sb) - ino));
1056 up_read(&grp->alloc_sem);
1057 } else {
1058 ext4_lock_group(sb, group);
1059 }
1060
1061 ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
1062 if (S_ISDIR(mode)) {
1063 ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
1064 if (sbi->s_log_groups_per_flex) {
1065 ext4_group_t f = ext4_flex_group(sbi, group);
1066
1067 atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
1068 f)->used_dirs);
1069 }
1070 }
1071 if (ext4_has_group_desc_csum(sb)) {
1072 ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
1073 EXT4_INODES_PER_GROUP(sb) / 8);
1074 ext4_group_desc_csum_set(sb, group, gdp);
1075 }
1076 ext4_unlock_group(sb, group);
1077
1078 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
1079 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
1080 if (err) {
1081 ext4_std_error(sb, err);
1082 goto out;
1083 }
1084
1085 percpu_counter_dec(&sbi->s_freeinodes_counter);
1086 if (S_ISDIR(mode))
1087 percpu_counter_inc(&sbi->s_dirs_counter);
1088
1089 if (sbi->s_log_groups_per_flex) {
1090 flex_group = ext4_flex_group(sbi, group);
1091 atomic_dec(&sbi_array_rcu_deref(sbi, s_flex_groups,
1092 flex_group)->free_inodes);
1093 }
1094
1095 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1096 /* This is the optimal IO size (for stat), not the fs block size */
1097 inode->i_blocks = 0;
1098 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1099 ei->i_crtime = inode->i_mtime;
1100
1101 memset(ei->i_data, 0, sizeof(ei->i_data));
1102 ei->i_dir_start_lookup = 0;
1103 ei->i_disksize = 0;
1104
1105 /* Don't inherit extent flag from directory, amongst others. */
1106 ei->i_flags =
1107 ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
1108 ei->i_flags |= i_flags;
1109 ei->i_file_acl = 0;
1110 ei->i_dtime = 0;
1111 ei->i_block_group = group;
1112 ei->i_last_alloc_group = ~0;
1113
1114 ext4_set_inode_flags(inode);
1115 if (IS_DIRSYNC(inode))
1116 ext4_handle_sync(handle);
1117 if (insert_inode_locked(inode) < 0) {
1118 /*
1119 * Likely a bitmap corruption causing inode to be allocated
1120 * twice.
1121 */
1122 err = -EIO;
1123 ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
1124 inode->i_ino);
1125 ext4_mark_group_bitmap_corrupted(sb, group,
1126 EXT4_GROUP_INFO_IBITMAP_CORRUPT);
1127 goto out;
1128 }
1129 inode->i_generation = prandom_u32();
1130
1131 /* Precompute checksum seed for inode metadata */
1132 if (ext4_has_metadata_csum(sb)) {
1133 __u32 csum;
1134 __le32 inum = cpu_to_le32(inode->i_ino);
1135 __le32 gen = cpu_to_le32(inode->i_generation);
1136 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
1137 sizeof(inum));
1138 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
1139 sizeof(gen));
1140 }
1141
1142 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
1143 ext4_set_inode_state(inode, EXT4_STATE_NEW);
1144
1145 ei->i_extra_isize = sbi->s_want_extra_isize;
1146 ei->i_inline_off = 0;
1147 if (ext4_has_feature_inline_data(sb))
1148 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1149 ret = inode;
1150 err = dquot_alloc_inode(inode);
1151 if (err)
1152 goto fail_drop;
1153
1154 /*
1155 * Since the encryption xattr will always be unique, create it first so
1156 * that it's less likely to end up in an external xattr block and
1157 * prevent its deduplication.
1158 */
1159 if (encrypt) {
1160 err = fscrypt_set_context(inode, handle);
1161 if (err)
1162 goto fail_free_drop;
1163 }
1164
1165 if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
1166 err = ext4_init_acl(handle, inode, dir);
1167 if (err)
1168 goto fail_free_drop;
1169
1170 err = ext4_init_security(handle, inode, dir, qstr);
1171 if (err)
1172 goto fail_free_drop;
1173 }
1174
1175 if (ext4_has_feature_extents(sb)) {
1176 /* set extent flag only for directory, file and normal symlink*/
1177 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
1178 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
1179 ext4_ext_tree_init(handle, inode);
1180 }
1181 }
1182
1183 if (ext4_handle_valid(handle)) {
1184 ei->i_sync_tid = handle->h_transaction->t_tid;
1185 ei->i_datasync_tid = handle->h_transaction->t_tid;
1186 }
1187
1188 err = ext4_mark_inode_dirty(handle, inode);
1189 if (err) {
1190 ext4_std_error(sb, err);
1191 goto fail_free_drop;
1192 }
1193
1194 ext4_debug("allocating inode %lu\n", inode->i_ino);
1195 trace_ext4_allocate_inode(inode, dir, mode);
1196 brelse(inode_bitmap_bh);
1197 return ret;
1198
1199fail_free_drop:
1200 dquot_free_inode(inode);
1201fail_drop:
1202 clear_nlink(inode);
1203 unlock_new_inode(inode);
1204out:
1205 dquot_drop(inode);
1206 inode->i_flags |= S_NOQUOTA;
1207 iput(inode);
1208 brelse(inode_bitmap_bh);
1209 return ERR_PTR(err);
1210}
1211
1212/* Verify that we are loading a valid orphan from disk */
1213struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1214{
1215 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1216 ext4_group_t block_group;
1217 int bit;
1218 struct buffer_head *bitmap_bh = NULL;
1219 struct inode *inode = NULL;
1220 int err = -EFSCORRUPTED;
1221
1222 if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
1223 goto bad_orphan;
1224
1225 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1226 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1227 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
1228 if (IS_ERR(bitmap_bh))
1229 return ERR_CAST(bitmap_bh);
1230
1231 /* Having the inode bit set should be a 100% indicator that this
1232 * is a valid orphan (no e2fsck run on fs). Orphans also include
1233 * inodes that were being truncated, so we can't check i_nlink==0.
1234 */
1235 if (!ext4_test_bit(bit, bitmap_bh->b_data))
1236 goto bad_orphan;
1237
1238 inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
1239 if (IS_ERR(inode)) {
1240 err = PTR_ERR(inode);
1241 ext4_error(sb, "couldn't read orphan inode %lu (err %d)",
1242 ino, err);
1243 return inode;
1244 }
1245
1246 /*
1247 * If the orphans has i_nlinks > 0 then it should be able to
1248 * be truncated, otherwise it won't be removed from the orphan
1249 * list during processing and an infinite loop will result.
1250 * Similarly, it must not be a bad inode.
1251 */
1252 if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1253 is_bad_inode(inode))
1254 goto bad_orphan;
1255
1256 if (NEXT_ORPHAN(inode) > max_ino)
1257 goto bad_orphan;
1258 brelse(bitmap_bh);
1259 return inode;
1260
1261bad_orphan:
1262 ext4_error(sb, "bad orphan inode %lu", ino);
1263 if (bitmap_bh)
1264 printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1265 bit, (unsigned long long)bitmap_bh->b_blocknr,
1266 ext4_test_bit(bit, bitmap_bh->b_data));
1267 if (inode) {
1268 printk(KERN_ERR "is_bad_inode(inode)=%d\n",
1269 is_bad_inode(inode));
1270 printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
1271 NEXT_ORPHAN(inode));
1272 printk(KERN_ERR "max_ino=%lu\n", max_ino);
1273 printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
1274 /* Avoid freeing blocks if we got a bad deleted inode */
1275 if (inode->i_nlink == 0)
1276 inode->i_blocks = 0;
1277 iput(inode);
1278 }
1279 brelse(bitmap_bh);
1280 return ERR_PTR(err);
1281}
1282
1283unsigned long ext4_count_free_inodes(struct super_block *sb)
1284{
1285 unsigned long desc_count;
1286 struct ext4_group_desc *gdp;
1287 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1288#ifdef EXT4FS_DEBUG
1289 struct ext4_super_block *es;
1290 unsigned long bitmap_count, x;
1291 struct buffer_head *bitmap_bh = NULL;
1292
1293 es = EXT4_SB(sb)->s_es;
1294 desc_count = 0;
1295 bitmap_count = 0;
1296 gdp = NULL;
1297 for (i = 0; i < ngroups; i++) {
1298 gdp = ext4_get_group_desc(sb, i, NULL);
1299 if (!gdp)
1300 continue;
1301 desc_count += ext4_free_inodes_count(sb, gdp);
1302 brelse(bitmap_bh);
1303 bitmap_bh = ext4_read_inode_bitmap(sb, i);
1304 if (IS_ERR(bitmap_bh)) {
1305 bitmap_bh = NULL;
1306 continue;
1307 }
1308
1309 x = ext4_count_free(bitmap_bh->b_data,
1310 EXT4_INODES_PER_GROUP(sb) / 8);
1311 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1312 (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1313 bitmap_count += x;
1314 }
1315 brelse(bitmap_bh);
1316 printk(KERN_DEBUG "ext4_count_free_inodes: "
1317 "stored = %u, computed = %lu, %lu\n",
1318 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1319 return desc_count;
1320#else
1321 desc_count = 0;
1322 for (i = 0; i < ngroups; i++) {
1323 gdp = ext4_get_group_desc(sb, i, NULL);
1324 if (!gdp)
1325 continue;
1326 desc_count += ext4_free_inodes_count(sb, gdp);
1327 cond_resched();
1328 }
1329 return desc_count;
1330#endif
1331}
1332
1333/* Called at mount-time, super-block is locked */
1334unsigned long ext4_count_dirs(struct super_block * sb)
1335{
1336 unsigned long count = 0;
1337 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1338
1339 for (i = 0; i < ngroups; i++) {
1340 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1341 if (!gdp)
1342 continue;
1343 count += ext4_used_dirs_count(sb, gdp);
1344 }
1345 return count;
1346}
1347
1348/*
1349 * Zeroes not yet zeroed inode table - just write zeroes through the whole
1350 * inode table. Must be called without any spinlock held. The only place
1351 * where it is called from on active part of filesystem is ext4lazyinit
1352 * thread, so we do not need any special locks, however we have to prevent
1353 * inode allocation from the current group, so we take alloc_sem lock, to
1354 * block ext4_new_inode() until we are finished.
1355 */
1356int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1357 int barrier)
1358{
1359 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1360 struct ext4_sb_info *sbi = EXT4_SB(sb);
1361 struct ext4_group_desc *gdp = NULL;
1362 struct buffer_head *group_desc_bh;
1363 handle_t *handle;
1364 ext4_fsblk_t blk;
1365 int num, ret = 0, used_blks = 0;
1366 unsigned long used_inos = 0;
1367
1368 /* This should not happen, but just to be sure check this */
1369 if (sb_rdonly(sb)) {
1370 ret = 1;
1371 goto out;
1372 }
1373
1374 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1375 if (!gdp)
1376 goto out;
1377
1378 /*
1379 * We do not need to lock this, because we are the only one
1380 * handling this flag.
1381 */
1382 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1383 goto out;
1384
1385 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1386 if (IS_ERR(handle)) {
1387 ret = PTR_ERR(handle);
1388 goto out;
1389 }
1390
1391 down_write(&grp->alloc_sem);
1392 /*
1393 * If inode bitmap was already initialized there may be some
1394 * used inodes so we need to skip blocks with used inodes in
1395 * inode table.
1396 */
1397 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
1398 used_inos = EXT4_INODES_PER_GROUP(sb) -
1399 ext4_itable_unused_count(sb, gdp);
1400 used_blks = DIV_ROUND_UP(used_inos, sbi->s_inodes_per_block);
1401
1402 /* Bogus inode unused count? */
1403 if (used_blks < 0 || used_blks > sbi->s_itb_per_group) {
1404 ext4_error(sb, "Something is wrong with group %u: "
1405 "used itable blocks: %d; "
1406 "itable unused count: %u",
1407 group, used_blks,
1408 ext4_itable_unused_count(sb, gdp));
1409 ret = 1;
1410 goto err_out;
1411 }
1412
1413 used_inos += group * EXT4_INODES_PER_GROUP(sb);
1414 /*
1415 * Are there some uninitialized inodes in the inode table
1416 * before the first normal inode?
1417 */
1418 if ((used_blks != sbi->s_itb_per_group) &&
1419 (used_inos < EXT4_FIRST_INO(sb))) {
1420 ext4_error(sb, "Something is wrong with group %u: "
1421 "itable unused count: %u; "
1422 "itables initialized count: %ld",
1423 group, ext4_itable_unused_count(sb, gdp),
1424 used_inos);
1425 ret = 1;
1426 goto err_out;
1427 }
1428 }
1429
1430 blk = ext4_inode_table(sb, gdp) + used_blks;
1431 num = sbi->s_itb_per_group - used_blks;
1432
1433 BUFFER_TRACE(group_desc_bh, "get_write_access");
1434 ret = ext4_journal_get_write_access(handle,
1435 group_desc_bh);
1436 if (ret)
1437 goto err_out;
1438
1439 /*
1440 * Skip zeroout if the inode table is full. But we set the ZEROED
1441 * flag anyway, because obviously, when it is full it does not need
1442 * further zeroing.
1443 */
1444 if (unlikely(num == 0))
1445 goto skip_zeroout;
1446
1447 ext4_debug("going to zero out inode table in group %d\n",
1448 group);
1449 ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1450 if (ret < 0)
1451 goto err_out;
1452 if (barrier)
1453 blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
1454
1455skip_zeroout:
1456 ext4_lock_group(sb, group);
1457 gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1458 ext4_group_desc_csum_set(sb, group, gdp);
1459 ext4_unlock_group(sb, group);
1460
1461 BUFFER_TRACE(group_desc_bh,
1462 "call ext4_handle_dirty_metadata");
1463 ret = ext4_handle_dirty_metadata(handle, NULL,
1464 group_desc_bh);
1465
1466err_out:
1467 up_write(&grp->alloc_sem);
1468 ext4_journal_stop(handle);
1469out:
1470 return ret;
1471}