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