blob: a4e1d7a9c5445261f1a80a0bbefe88d0a8fedd1e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/ext2/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@dcs.ed.ac.uk), 1993
12 * Big-endian to little-endian byte-swapping/bitmaps by
13 * David S. Miller (davem@caip.rutgers.edu), 1995
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/quotaops.h>
17#include <linux/sched.h>
18#include <linux/backing-dev.h>
19#include <linux/buffer_head.h>
20#include <linux/random.h>
21#include "ext2.h"
22#include "xattr.h"
23#include "acl.h"
24
25/*
26 * ialloc.c contains the inodes allocation and deallocation routines
27 */
28
29/*
30 * The free inodes are managed by bitmaps. A file system contains several
31 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
32 * block for inodes, N blocks for the inode table and data blocks.
33 *
34 * The file system contains group descriptors which are located after the
35 * super block. Each descriptor contains the number of the bitmap block and
36 * the free blocks count in the block.
37 */
38
39
40/*
41 * Read the inode allocation bitmap for a given block_group, reading
42 * into the specified slot in the superblock's bitmap cache.
43 *
44 * Return buffer_head of bitmap on success or NULL.
45 */
46static struct buffer_head *
47read_inode_bitmap(struct super_block * sb, unsigned long block_group)
48{
49 struct ext2_group_desc *desc;
50 struct buffer_head *bh = NULL;
51
52 desc = ext2_get_group_desc(sb, block_group, NULL);
53 if (!desc)
54 goto error_out;
55
56 bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
57 if (!bh)
58 ext2_error(sb, "read_inode_bitmap",
59 "Cannot read inode bitmap - "
60 "block_group = %lu, inode_bitmap = %u",
61 block_group, le32_to_cpu(desc->bg_inode_bitmap));
62error_out:
63 return bh;
64}
65
66static void ext2_release_inode(struct super_block *sb, int group, int dir)
67{
68 struct ext2_group_desc * desc;
69 struct buffer_head *bh;
70
71 desc = ext2_get_group_desc(sb, group, &bh);
72 if (!desc) {
73 ext2_error(sb, "ext2_release_inode",
74 "can't get descriptor for group %d", group);
75 return;
76 }
77
78 spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
Marcin Slusarzfba4d392008-04-28 02:15:59 -070079 le16_add_cpu(&desc->bg_free_inodes_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (dir)
Marcin Slusarzfba4d392008-04-28 02:15:59 -070081 le16_add_cpu(&desc->bg_used_dirs_count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
Mikulas Patockabc2fbaa2020-04-20 16:02:21 -040083 percpu_counter_inc(&EXT2_SB(sb)->s_freeinodes_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (dir)
85 percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 mark_buffer_dirty(bh);
87}
88
89/*
90 * NOTE! When we get the inode, we're the only people
91 * that have access to it, and as such there are no
92 * race conditions we have to worry about. The inode
93 * is not on the hash-lists, and it cannot be reached
94 * through the filesystem because the directory entry
95 * has been deleted earlier.
96 *
97 * HOWEVER: we must make sure that we get no aliases,
98 * which means that we have to call "clear_inode()"
99 * _before_ we mark the inode not in use in the inode
100 * bitmaps. Otherwise a newly created file might use
101 * the same inode number (not actually the same pointer
102 * though), and then we'd have two inodes sharing the
103 * same inode number and space on the harddisk.
104 */
105void ext2_free_inode (struct inode * inode)
106{
107 struct super_block * sb = inode->i_sb;
108 int is_directory;
109 unsigned long ino;
Francis Moreau524e4a12010-04-08 11:35:17 +0200110 struct buffer_head *bitmap_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long block_group;
112 unsigned long bit;
113 struct ext2_super_block * es;
114
115 ino = inode->i_ino;
116 ext2_debug ("freeing inode %lu\n", ino);
117
118 /*
119 * Note: we must free any quota before locking the superblock,
120 * as writing the quota to disk may need the lock as well.
121 */
Al Viro72edc4d2010-06-04 23:32:28 -0400122 /* Quota is already initialized in iput() */
Al Viro72edc4d2010-06-04 23:32:28 -0400123 dquot_free_inode(inode);
124 dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 es = EXT2_SB(sb)->s_es;
127 is_directory = S_ISDIR(inode->i_mode);
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (ino < EXT2_FIRST_INO(sb) ||
130 ino > le32_to_cpu(es->s_inodes_count)) {
131 ext2_error (sb, "ext2_free_inode",
132 "reserved or nonexistent inode %lu", ino);
Francis Moreau524e4a12010-04-08 11:35:17 +0200133 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
136 bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 bitmap_bh = read_inode_bitmap(sb, block_group);
138 if (!bitmap_bh)
Francis Moreau524e4a12010-04-08 11:35:17 +0200139 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* Ok, now we can actually update the inode bitmaps.. */
142 if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
143 bit, (void *) bitmap_bh->b_data))
144 ext2_error (sb, "ext2_free_inode",
145 "bit already cleared for inode %lu", ino);
146 else
147 ext2_release_inode(sb, block_group, is_directory);
148 mark_buffer_dirty(bitmap_bh);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800149 if (sb->s_flags & SB_SYNCHRONOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sync_dirty_buffer(bitmap_bh);
Francis Moreau524e4a12010-04-08 11:35:17 +0200151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 brelse(bitmap_bh);
153}
154
155/*
156 * We perform asynchronous prereading of the new inode's inode block when
157 * we create the inode, in the expectation that the inode will be written
158 * back soon. There are two reasons:
159 *
160 * - When creating a large number of files, the async prereads will be
161 * nicely merged into large reads
162 * - When writing out a large number of inodes, we don't need to keep on
163 * stalling the writes while we read the inode block.
164 *
165 * FIXME: ext2_get_group_desc() needs to be simplified.
166 */
167static void ext2_preread_inode(struct inode *inode)
168{
169 unsigned long block_group;
170 unsigned long offset;
171 unsigned long block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct ext2_group_desc * gdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
Eric Sandeenef2fb672007-10-16 23:26:30 -0700175 gdp = ext2_get_group_desc(inode->i_sb, block_group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (gdp == NULL)
177 return;
178
179 /*
180 * Figure out the offset within the block group inode table
181 */
182 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
183 EXT2_INODE_SIZE(inode->i_sb);
184 block = le32_to_cpu(gdp->bg_inode_table) +
185 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
186 sb_breadahead(inode->i_sb, block);
187}
188
189/*
190 * There are two policies for allocating an inode. If the new inode is
191 * a directory, then a forward search is made for a block group with both
192 * free space and a low directory-to-inode ratio; if that fails, then of
193 * the groups with above-average free space, that group with the fewest
194 * directories already is chosen.
195 *
196 * For other inodes, search forward from the parent directory\'s block
197 * group to find a free inode.
198 */
199static int find_group_dir(struct super_block *sb, struct inode *parent)
200{
201 int ngroups = EXT2_SB(sb)->s_groups_count;
202 int avefreei = ext2_count_free_inodes(sb) / ngroups;
203 struct ext2_group_desc *desc, *best_desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int group, best_group = -1;
205
206 for (group = 0; group < ngroups; group++) {
Eric Sandeenef2fb672007-10-16 23:26:30 -0700207 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!desc || !desc->bg_free_inodes_count)
209 continue;
210 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
211 continue;
212 if (!best_desc ||
213 (le16_to_cpu(desc->bg_free_blocks_count) >
214 le16_to_cpu(best_desc->bg_free_blocks_count))) {
215 best_group = group;
216 best_desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 return best_group;
221}
222
223/*
224 * Orlov's allocator for directories.
225 *
226 * We always try to spread first-level directories.
227 *
228 * If there are blockgroups with both free inodes and free blocks counts
229 * not worse than average we return one with smallest directory count.
230 * Otherwise we simply return a random group.
231 *
232 * For the rest rules look so:
233 *
234 * It's OK to put directory into a group unless
235 * it has too many directories already (max_dirs) or
236 * it has too few free inodes left (min_inodes) or
237 * it has too few free blocks left (min_blocks) or
238 * it's already running too large debt (max_debt).
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000239 * Parent's group is preferred, if it doesn't satisfy these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * conditions we search cyclically through the rest. If none
241 * of the groups look good we just look for a group with more
242 * free inodes than average (starting at parent's group).
243 *
244 * Debt is incremented each time we allocate a directory and decremented
245 * when we allocate an inode, within 0--255.
246 */
247
248#define INODE_COST 64
249#define BLOCK_COST 256
250
251static int find_group_orlov(struct super_block *sb, struct inode *parent)
252{
253 int parent_group = EXT2_I(parent)->i_block_group;
254 struct ext2_sb_info *sbi = EXT2_SB(sb);
255 struct ext2_super_block *es = sbi->s_es;
256 int ngroups = sbi->s_groups_count;
257 int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
258 int freei;
259 int avefreei;
260 int free_blocks;
261 int avefreeb;
262 int blocks_per_dir;
263 int ndirs;
264 int max_debt, max_dirs, min_blocks, min_inodes;
265 int group = -1, i;
266 struct ext2_group_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
269 avefreei = freei / ngroups;
270 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
271 avefreeb = free_blocks / ngroups;
272 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
273
David Howells2b0143b2015-03-17 22:25:59 +0000274 if ((parent == d_inode(sb->s_root)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
276 struct ext2_group_desc *best_desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int best_ndir = inodes_per_group;
278 int best_group = -1;
279
Jason A. Donenfeld8032bf12022-10-09 20:44:02 -0600280 parent_group = get_random_u32_below(ngroups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 for (i = 0; i < ngroups; i++) {
282 group = (parent_group + i) % ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700283 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!desc || !desc->bg_free_inodes_count)
285 continue;
286 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
287 continue;
288 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
289 continue;
290 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
291 continue;
292 best_group = group;
293 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
294 best_desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296 if (best_group >= 0) {
297 desc = best_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 group = best_group;
299 goto found;
300 }
301 goto fallback;
302 }
303
304 if (ndirs == 0)
305 ndirs = 1; /* percpu_counters are approximate... */
306
307 blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
308
309 max_dirs = ndirs / ngroups + inodes_per_group / 16;
310 min_inodes = avefreei - inodes_per_group / 4;
311 min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
312
313 max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
314 if (max_debt * INODE_COST > inodes_per_group)
315 max_debt = inodes_per_group / INODE_COST;
316 if (max_debt > 255)
317 max_debt = 255;
318 if (max_debt == 0)
319 max_debt = 1;
320
321 for (i = 0; i < ngroups; i++) {
322 group = (parent_group + i) % ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700323 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!desc || !desc->bg_free_inodes_count)
325 continue;
326 if (sbi->s_debts[group] >= max_debt)
327 continue;
328 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
329 continue;
330 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
331 continue;
332 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
333 continue;
334 goto found;
335 }
336
337fallback:
338 for (i = 0; i < ngroups; i++) {
339 group = (parent_group + i) % ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700340 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!desc || !desc->bg_free_inodes_count)
342 continue;
343 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
344 goto found;
345 }
346
347 if (avefreei) {
348 /*
349 * The free-inodes counter is approximate, and for really small
350 * filesystems the above test can fail to find any blockgroups
351 */
352 avefreei = 0;
353 goto fallback;
354 }
355
356 return -1;
357
358found:
359 return group;
360}
361
362static int find_group_other(struct super_block *sb, struct inode *parent)
363{
364 int parent_group = EXT2_I(parent)->i_block_group;
365 int ngroups = EXT2_SB(sb)->s_groups_count;
366 struct ext2_group_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int group, i;
368
369 /*
370 * Try to place the inode in its parent directory
371 */
372 group = parent_group;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700373 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
375 le16_to_cpu(desc->bg_free_blocks_count))
376 goto found;
377
378 /*
379 * We're going to place this inode in a different blockgroup from its
380 * parent. We want to cause files in a common directory to all land in
381 * the same blockgroup. But we want files which are in a different
382 * directory which shares a blockgroup with our parent to land in a
383 * different blockgroup.
384 *
385 * So add our directory's i_ino into the starting point for the hash.
386 */
387 group = (group + parent->i_ino) % ngroups;
388
389 /*
390 * Use a quadratic hash to find a group with a free inode and some
391 * free blocks.
392 */
393 for (i = 1; i < ngroups; i <<= 1) {
394 group += i;
395 if (group >= ngroups)
396 group -= ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700397 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
399 le16_to_cpu(desc->bg_free_blocks_count))
400 goto found;
401 }
402
403 /*
404 * That failed: try linear search for a free inode, even if that group
405 * has no free blocks.
406 */
407 group = parent_group;
408 for (i = 0; i < ngroups; i++) {
409 if (++group >= ngroups)
410 group = 0;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700411 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
413 goto found;
414 }
415
416 return -1;
417
418found:
419 return group;
420}
421
Al Viro3ea40bc2011-07-26 02:46:18 -0400422struct inode *ext2_new_inode(struct inode *dir, umode_t mode,
Eric Paris2a7dba32011-02-01 11:05:39 -0500423 const struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct super_block *sb;
426 struct buffer_head *bitmap_bh = NULL;
427 struct buffer_head *bh2;
428 int group, i;
429 ino_t ino = 0;
430 struct inode * inode;
431 struct ext2_group_desc *gdp;
432 struct ext2_super_block *es;
433 struct ext2_inode_info *ei;
434 struct ext2_sb_info *sbi;
435 int err;
436
437 sb = dir->i_sb;
438 inode = new_inode(sb);
439 if (!inode)
440 return ERR_PTR(-ENOMEM);
441
442 ei = EXT2_I(inode);
443 sbi = EXT2_SB(sb);
444 es = sbi->s_es;
445 if (S_ISDIR(mode)) {
446 if (test_opt(sb, OLDALLOC))
447 group = find_group_dir(sb, dir);
448 else
449 group = find_group_orlov(sb, dir);
450 } else
451 group = find_group_other(sb, dir);
452
453 if (group == -1) {
454 err = -ENOSPC;
455 goto fail;
456 }
457
458 for (i = 0; i < sbi->s_groups_count; i++) {
459 gdp = ext2_get_group_desc(sb, group, &bh2);
Jan Karaf7a1c352016-08-09 10:07:52 +0200460 if (!gdp) {
461 if (++group == sbi->s_groups_count)
462 group = 0;
463 continue;
464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 brelse(bitmap_bh);
466 bitmap_bh = read_inode_bitmap(sb, group);
467 if (!bitmap_bh) {
468 err = -EIO;
469 goto fail;
470 }
471 ino = 0;
472
473repeat_in_this_group:
474 ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
475 EXT2_INODES_PER_GROUP(sb), ino);
476 if (ino >= EXT2_INODES_PER_GROUP(sb)) {
477 /*
478 * Rare race: find_group_xx() decided that there were
479 * free inodes in this group, but by the time we tried
480 * to allocate one, they're all gone. This can also
481 * occur because the counters which find_group_orlov()
482 * uses are approximate. So just go and search the
483 * next block group.
484 */
485 if (++group == sbi->s_groups_count)
486 group = 0;
487 continue;
488 }
489 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
490 ino, bitmap_bh->b_data)) {
491 /* we lost this inode */
492 if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
493 /* this group is exhausted, try next group */
494 if (++group == sbi->s_groups_count)
495 group = 0;
496 continue;
497 }
498 /* try to find free inode in the same group */
499 goto repeat_in_this_group;
500 }
501 goto got;
502 }
503
504 /*
505 * Scanned all blockgroups.
506 */
Chengguang Xudc1f7382019-05-30 18:10:42 +0800507 brelse(bitmap_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 err = -ENOSPC;
509 goto fail;
510got:
511 mark_buffer_dirty(bitmap_bh);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800512 if (sb->s_flags & SB_SYNCHRONOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 sync_dirty_buffer(bitmap_bh);
514 brelse(bitmap_bh);
515
516 ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
517 if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
518 ext2_error (sb, "ext2_new_inode",
519 "reserved inode or inode > inodes count - "
520 "block_group = %d,inode=%lu", group,
521 (unsigned long) ino);
522 err = -EIO;
523 goto fail;
524 }
525
Mikulas Patockabc2fbaa2020-04-20 16:02:21 -0400526 percpu_counter_dec(&sbi->s_freeinodes_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (S_ISDIR(mode))
528 percpu_counter_inc(&sbi->s_dirs_counter);
529
530 spin_lock(sb_bgl_lock(sbi, group));
Marcin Slusarzfba4d392008-04-28 02:15:59 -0700531 le16_add_cpu(&gdp->bg_free_inodes_count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (S_ISDIR(mode)) {
533 if (sbi->s_debts[group] < 255)
534 sbi->s_debts[group]++;
Marcin Slusarzfba4d392008-04-28 02:15:59 -0700535 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } else {
537 if (sbi->s_debts[group])
538 sbi->s_debts[group]--;
539 }
540 spin_unlock(sb_bgl_lock(sbi, group));
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 mark_buffer_dirty(bh2);
Dmitry Monakhovffba1022010-03-04 17:31:49 +0300543 if (test_opt(sb, GRPID)) {
544 inode->i_mode = mode;
545 inode->i_uid = current_fsuid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 inode->i_gid = dir->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 } else
Christian Braunerf2d40142023-01-13 12:49:25 +0100548 inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 inode->i_ino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 inode->i_blocks = 0;
Deepa Dinamani02027d42016-09-14 07:48:05 -0700552 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 memset(ei->i_data, 0, sizeof(ei->i_data));
Duane Griffinef8b6462009-01-07 18:07:21 -0800554 ei->i_flags =
555 ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ei->i_faddr = 0;
557 ei->i_frag_no = 0;
558 ei->i_frag_size = 0;
559 ei->i_file_acl = 0;
560 ei->i_dir_acl = 0;
561 ei->i_dtime = 0;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700562 ei->i_block_alloc_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ei->i_block_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 ei->i_dir_start_lookup = 0;
565 ei->i_state = EXT2_STATE_NEW;
566 ext2_set_inode_flags(inode);
567 spin_lock(&sbi->s_next_gen_lock);
568 inode->i_generation = sbi->s_next_generation++;
569 spin_unlock(&sbi->s_next_gen_lock);
Al Viro41080b52008-12-30 01:52:35 -0500570 if (insert_inode_locked(inode) < 0) {
Jan Karaef6919c2011-12-09 00:08:58 +0100571 ext2_error(sb, "ext2_new_inode",
572 "inode number already in use - inode=%lu",
573 (unsigned long) ino);
574 err = -EIO;
575 goto fail;
Al Viro41080b52008-12-30 01:52:35 -0500576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Jan Karac2edb302015-06-29 16:08:45 +0200578 err = dquot_initialize(inode);
579 if (err)
580 goto fail_drop;
581
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500582 err = dquot_alloc_inode(inode);
583 if (err)
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700584 goto fail_drop;
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 err = ext2_init_acl(inode, dir);
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700587 if (err)
588 goto fail_free_drop;
589
Eric Paris2a7dba32011-02-01 11:05:39 -0500590 err = ext2_init_security(inode, dir, qstr);
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700591 if (err)
592 goto fail_free_drop;
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 mark_inode_dirty(inode);
595 ext2_debug("allocating inode %lu\n", inode->i_ino);
596 ext2_preread_inode(inode);
597 return inode;
598
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700599fail_free_drop:
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500600 dquot_free_inode(inode);
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700601
602fail_drop:
Christoph Hellwig9f754752010-03-03 09:05:05 -0500603 dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 inode->i_flags |= S_NOQUOTA;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200605 clear_nlink(inode);
Al Viro2e5afe52018-05-16 18:29:56 -0400606 discard_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return ERR_PTR(err);
608
609fail:
610 make_bad_inode(inode);
611 iput(inode);
612 return ERR_PTR(err);
613}
614
615unsigned long ext2_count_free_inodes (struct super_block * sb)
616{
617 struct ext2_group_desc *desc;
618 unsigned long desc_count = 0;
619 int i;
620
621#ifdef EXT2FS_DEBUG
622 struct ext2_super_block *es;
623 unsigned long bitmap_count = 0;
624 struct buffer_head *bitmap_bh = NULL;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 es = EXT2_SB(sb)->s_es;
627 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
628 unsigned x;
629
630 desc = ext2_get_group_desc (sb, i, NULL);
631 if (!desc)
632 continue;
633 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
634 brelse(bitmap_bh);
635 bitmap_bh = read_inode_bitmap(sb, i);
636 if (!bitmap_bh)
637 continue;
638
639 x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
640 printk("group %d: stored = %d, counted = %u\n",
641 i, le16_to_cpu(desc->bg_free_inodes_count), x);
642 bitmap_count += x;
643 }
644 brelse(bitmap_bh);
645 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
Akinobu Mitaecd0afa2012-07-30 14:41:05 -0700646 (unsigned long)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
648 desc_count, bitmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return desc_count;
650#else
651 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
652 desc = ext2_get_group_desc (sb, i, NULL);
653 if (!desc)
654 continue;
655 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
656 }
657 return desc_count;
658#endif
659}
660
661/* Called at mount-time, super-block is locked */
662unsigned long ext2_count_dirs (struct super_block * sb)
663{
664 unsigned long count = 0;
665 int i;
666
667 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
668 struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
669 if (!gdp)
670 continue;
671 count += le16_to_cpu(gdp->bg_used_dirs_count);
672 }
673 return count;
674}
675