blob: 6484199b35d1ec1bb63879593e5bb20f0af2bb77 [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));
83 if (dir)
84 percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 mark_buffer_dirty(bh);
86}
87
88/*
89 * NOTE! When we get the inode, we're the only people
90 * that have access to it, and as such there are no
91 * race conditions we have to worry about. The inode
92 * is not on the hash-lists, and it cannot be reached
93 * through the filesystem because the directory entry
94 * has been deleted earlier.
95 *
96 * HOWEVER: we must make sure that we get no aliases,
97 * which means that we have to call "clear_inode()"
98 * _before_ we mark the inode not in use in the inode
99 * bitmaps. Otherwise a newly created file might use
100 * the same inode number (not actually the same pointer
101 * though), and then we'd have two inodes sharing the
102 * same inode number and space on the harddisk.
103 */
104void ext2_free_inode (struct inode * inode)
105{
106 struct super_block * sb = inode->i_sb;
107 int is_directory;
108 unsigned long ino;
Francis Moreau524e4a12010-04-08 11:35:17 +0200109 struct buffer_head *bitmap_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unsigned long block_group;
111 unsigned long bit;
112 struct ext2_super_block * es;
113
114 ino = inode->i_ino;
115 ext2_debug ("freeing inode %lu\n", ino);
116
117 /*
118 * Note: we must free any quota before locking the superblock,
119 * as writing the quota to disk may need the lock as well.
120 */
Al Viro72edc4d2010-06-04 23:32:28 -0400121 /* Quota is already initialized in iput() */
Al Viro72edc4d2010-06-04 23:32:28 -0400122 dquot_free_inode(inode);
123 dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 es = EXT2_SB(sb)->s_es;
126 is_directory = S_ISDIR(inode->i_mode);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (ino < EXT2_FIRST_INO(sb) ||
129 ino > le32_to_cpu(es->s_inodes_count)) {
130 ext2_error (sb, "ext2_free_inode",
131 "reserved or nonexistent inode %lu", ino);
Francis Moreau524e4a12010-04-08 11:35:17 +0200132 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
135 bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 bitmap_bh = read_inode_bitmap(sb, block_group);
137 if (!bitmap_bh)
Francis Moreau524e4a12010-04-08 11:35:17 +0200138 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* Ok, now we can actually update the inode bitmaps.. */
141 if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
142 bit, (void *) bitmap_bh->b_data))
143 ext2_error (sb, "ext2_free_inode",
144 "bit already cleared for inode %lu", ino);
145 else
146 ext2_release_inode(sb, block_group, is_directory);
147 mark_buffer_dirty(bitmap_bh);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800148 if (sb->s_flags & SB_SYNCHRONOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 sync_dirty_buffer(bitmap_bh);
Francis Moreau524e4a12010-04-08 11:35:17 +0200150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 brelse(bitmap_bh);
152}
153
154/*
155 * We perform asynchronous prereading of the new inode's inode block when
156 * we create the inode, in the expectation that the inode will be written
157 * back soon. There are two reasons:
158 *
159 * - When creating a large number of files, the async prereads will be
160 * nicely merged into large reads
161 * - When writing out a large number of inodes, we don't need to keep on
162 * stalling the writes while we read the inode block.
163 *
164 * FIXME: ext2_get_group_desc() needs to be simplified.
165 */
166static void ext2_preread_inode(struct inode *inode)
167{
168 unsigned long block_group;
169 unsigned long offset;
170 unsigned long block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct ext2_group_desc * gdp;
172 struct backing_dev_info *bdi;
173
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100174 bdi = inode_to_bdi(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (bdi_read_congested(bdi))
176 return;
177 if (bdi_write_congested(bdi))
178 return;
179
180 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
Eric Sandeenef2fb672007-10-16 23:26:30 -0700181 gdp = ext2_get_group_desc(inode->i_sb, block_group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (gdp == NULL)
183 return;
184
185 /*
186 * Figure out the offset within the block group inode table
187 */
188 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
189 EXT2_INODE_SIZE(inode->i_sb);
190 block = le32_to_cpu(gdp->bg_inode_table) +
191 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
192 sb_breadahead(inode->i_sb, block);
193}
194
195/*
196 * There are two policies for allocating an inode. If the new inode is
197 * a directory, then a forward search is made for a block group with both
198 * free space and a low directory-to-inode ratio; if that fails, then of
199 * the groups with above-average free space, that group with the fewest
200 * directories already is chosen.
201 *
202 * For other inodes, search forward from the parent directory\'s block
203 * group to find a free inode.
204 */
205static int find_group_dir(struct super_block *sb, struct inode *parent)
206{
207 int ngroups = EXT2_SB(sb)->s_groups_count;
208 int avefreei = ext2_count_free_inodes(sb) / ngroups;
209 struct ext2_group_desc *desc, *best_desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int group, best_group = -1;
211
212 for (group = 0; group < ngroups; group++) {
Eric Sandeenef2fb672007-10-16 23:26:30 -0700213 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!desc || !desc->bg_free_inodes_count)
215 continue;
216 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
217 continue;
218 if (!best_desc ||
219 (le16_to_cpu(desc->bg_free_blocks_count) >
220 le16_to_cpu(best_desc->bg_free_blocks_count))) {
221 best_group = group;
222 best_desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
225 if (!best_desc)
226 return -1;
227
228 return best_group;
229}
230
231/*
232 * Orlov's allocator for directories.
233 *
234 * We always try to spread first-level directories.
235 *
236 * If there are blockgroups with both free inodes and free blocks counts
237 * not worse than average we return one with smallest directory count.
238 * Otherwise we simply return a random group.
239 *
240 * For the rest rules look so:
241 *
242 * It's OK to put directory into a group unless
243 * it has too many directories already (max_dirs) or
244 * it has too few free inodes left (min_inodes) or
245 * it has too few free blocks left (min_blocks) or
246 * it's already running too large debt (max_debt).
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000247 * Parent's group is preferred, if it doesn't satisfy these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * conditions we search cyclically through the rest. If none
249 * of the groups look good we just look for a group with more
250 * free inodes than average (starting at parent's group).
251 *
252 * Debt is incremented each time we allocate a directory and decremented
253 * when we allocate an inode, within 0--255.
254 */
255
256#define INODE_COST 64
257#define BLOCK_COST 256
258
259static int find_group_orlov(struct super_block *sb, struct inode *parent)
260{
261 int parent_group = EXT2_I(parent)->i_block_group;
262 struct ext2_sb_info *sbi = EXT2_SB(sb);
263 struct ext2_super_block *es = sbi->s_es;
264 int ngroups = sbi->s_groups_count;
265 int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
266 int freei;
267 int avefreei;
268 int free_blocks;
269 int avefreeb;
270 int blocks_per_dir;
271 int ndirs;
272 int max_debt, max_dirs, min_blocks, min_inodes;
273 int group = -1, i;
274 struct ext2_group_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
277 avefreei = freei / ngroups;
278 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
279 avefreeb = free_blocks / ngroups;
280 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
281
David Howells2b0143b2015-03-17 22:25:59 +0000282 if ((parent == d_inode(sb->s_root)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
284 struct ext2_group_desc *best_desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int best_ndir = inodes_per_group;
286 int best_group = -1;
287
ZhangZhene8781672014-02-26 10:32:41 +0800288 group = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 parent_group = (unsigned)group % ngroups;
290 for (i = 0; i < ngroups; i++) {
291 group = (parent_group + i) % ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700292 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (!desc || !desc->bg_free_inodes_count)
294 continue;
295 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
296 continue;
297 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
298 continue;
299 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
300 continue;
301 best_group = group;
302 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
303 best_desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 if (best_group >= 0) {
306 desc = best_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 group = best_group;
308 goto found;
309 }
310 goto fallback;
311 }
312
313 if (ndirs == 0)
314 ndirs = 1; /* percpu_counters are approximate... */
315
316 blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
317
318 max_dirs = ndirs / ngroups + inodes_per_group / 16;
319 min_inodes = avefreei - inodes_per_group / 4;
320 min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
321
322 max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
323 if (max_debt * INODE_COST > inodes_per_group)
324 max_debt = inodes_per_group / INODE_COST;
325 if (max_debt > 255)
326 max_debt = 255;
327 if (max_debt == 0)
328 max_debt = 1;
329
330 for (i = 0; i < ngroups; i++) {
331 group = (parent_group + i) % ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700332 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!desc || !desc->bg_free_inodes_count)
334 continue;
335 if (sbi->s_debts[group] >= max_debt)
336 continue;
337 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
338 continue;
339 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
340 continue;
341 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
342 continue;
343 goto found;
344 }
345
346fallback:
347 for (i = 0; i < ngroups; i++) {
348 group = (parent_group + i) % ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700349 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (!desc || !desc->bg_free_inodes_count)
351 continue;
352 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
353 goto found;
354 }
355
356 if (avefreei) {
357 /*
358 * The free-inodes counter is approximate, and for really small
359 * filesystems the above test can fail to find any blockgroups
360 */
361 avefreei = 0;
362 goto fallback;
363 }
364
365 return -1;
366
367found:
368 return group;
369}
370
371static int find_group_other(struct super_block *sb, struct inode *parent)
372{
373 int parent_group = EXT2_I(parent)->i_block_group;
374 int ngroups = EXT2_SB(sb)->s_groups_count;
375 struct ext2_group_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int group, i;
377
378 /*
379 * Try to place the inode in its parent directory
380 */
381 group = parent_group;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700382 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
384 le16_to_cpu(desc->bg_free_blocks_count))
385 goto found;
386
387 /*
388 * We're going to place this inode in a different blockgroup from its
389 * parent. We want to cause files in a common directory to all land in
390 * the same blockgroup. But we want files which are in a different
391 * directory which shares a blockgroup with our parent to land in a
392 * different blockgroup.
393 *
394 * So add our directory's i_ino into the starting point for the hash.
395 */
396 group = (group + parent->i_ino) % ngroups;
397
398 /*
399 * Use a quadratic hash to find a group with a free inode and some
400 * free blocks.
401 */
402 for (i = 1; i < ngroups; i <<= 1) {
403 group += i;
404 if (group >= ngroups)
405 group -= ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700406 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
408 le16_to_cpu(desc->bg_free_blocks_count))
409 goto found;
410 }
411
412 /*
413 * That failed: try linear search for a free inode, even if that group
414 * has no free blocks.
415 */
416 group = parent_group;
417 for (i = 0; i < ngroups; i++) {
418 if (++group >= ngroups)
419 group = 0;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700420 desc = ext2_get_group_desc (sb, group, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
422 goto found;
423 }
424
425 return -1;
426
427found:
428 return group;
429}
430
Al Viro3ea40bc2011-07-26 02:46:18 -0400431struct inode *ext2_new_inode(struct inode *dir, umode_t mode,
Eric Paris2a7dba32011-02-01 11:05:39 -0500432 const struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct super_block *sb;
435 struct buffer_head *bitmap_bh = NULL;
436 struct buffer_head *bh2;
437 int group, i;
438 ino_t ino = 0;
439 struct inode * inode;
440 struct ext2_group_desc *gdp;
441 struct ext2_super_block *es;
442 struct ext2_inode_info *ei;
443 struct ext2_sb_info *sbi;
444 int err;
445
446 sb = dir->i_sb;
447 inode = new_inode(sb);
448 if (!inode)
449 return ERR_PTR(-ENOMEM);
450
451 ei = EXT2_I(inode);
452 sbi = EXT2_SB(sb);
453 es = sbi->s_es;
454 if (S_ISDIR(mode)) {
455 if (test_opt(sb, OLDALLOC))
456 group = find_group_dir(sb, dir);
457 else
458 group = find_group_orlov(sb, dir);
459 } else
460 group = find_group_other(sb, dir);
461
462 if (group == -1) {
463 err = -ENOSPC;
464 goto fail;
465 }
466
467 for (i = 0; i < sbi->s_groups_count; i++) {
468 gdp = ext2_get_group_desc(sb, group, &bh2);
Jan Karaf7a1c352016-08-09 10:07:52 +0200469 if (!gdp) {
470 if (++group == sbi->s_groups_count)
471 group = 0;
472 continue;
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 brelse(bitmap_bh);
475 bitmap_bh = read_inode_bitmap(sb, group);
476 if (!bitmap_bh) {
477 err = -EIO;
478 goto fail;
479 }
480 ino = 0;
481
482repeat_in_this_group:
483 ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
484 EXT2_INODES_PER_GROUP(sb), ino);
485 if (ino >= EXT2_INODES_PER_GROUP(sb)) {
486 /*
487 * Rare race: find_group_xx() decided that there were
488 * free inodes in this group, but by the time we tried
489 * to allocate one, they're all gone. This can also
490 * occur because the counters which find_group_orlov()
491 * uses are approximate. So just go and search the
492 * next block group.
493 */
494 if (++group == sbi->s_groups_count)
495 group = 0;
496 continue;
497 }
498 if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
499 ino, bitmap_bh->b_data)) {
500 /* we lost this inode */
501 if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
502 /* this group is exhausted, try next group */
503 if (++group == sbi->s_groups_count)
504 group = 0;
505 continue;
506 }
507 /* try to find free inode in the same group */
508 goto repeat_in_this_group;
509 }
510 goto got;
511 }
512
513 /*
514 * Scanned all blockgroups.
515 */
516 err = -ENOSPC;
517 goto fail;
518got:
519 mark_buffer_dirty(bitmap_bh);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800520 if (sb->s_flags & SB_SYNCHRONOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 sync_dirty_buffer(bitmap_bh);
522 brelse(bitmap_bh);
523
524 ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
525 if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
526 ext2_error (sb, "ext2_new_inode",
527 "reserved inode or inode > inodes count - "
528 "block_group = %d,inode=%lu", group,
529 (unsigned long) ino);
530 err = -EIO;
531 goto fail;
532 }
533
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700534 percpu_counter_add(&sbi->s_freeinodes_counter, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (S_ISDIR(mode))
536 percpu_counter_inc(&sbi->s_dirs_counter);
537
538 spin_lock(sb_bgl_lock(sbi, group));
Marcin Slusarzfba4d392008-04-28 02:15:59 -0700539 le16_add_cpu(&gdp->bg_free_inodes_count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (S_ISDIR(mode)) {
541 if (sbi->s_debts[group] < 255)
542 sbi->s_debts[group]++;
Marcin Slusarzfba4d392008-04-28 02:15:59 -0700543 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else {
545 if (sbi->s_debts[group])
546 sbi->s_debts[group]--;
547 }
548 spin_unlock(sb_bgl_lock(sbi, group));
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 mark_buffer_dirty(bh2);
Dmitry Monakhovffba1022010-03-04 17:31:49 +0300551 if (test_opt(sb, GRPID)) {
552 inode->i_mode = mode;
553 inode->i_uid = current_fsuid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 inode->i_gid = dir->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 } else
Dmitry Monakhovffba1022010-03-04 17:31:49 +0300556 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 inode->i_ino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 inode->i_blocks = 0;
Deepa Dinamani02027d42016-09-14 07:48:05 -0700560 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 memset(ei->i_data, 0, sizeof(ei->i_data));
Duane Griffinef8b6462009-01-07 18:07:21 -0800562 ei->i_flags =
563 ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 ei->i_faddr = 0;
565 ei->i_frag_no = 0;
566 ei->i_frag_size = 0;
567 ei->i_file_acl = 0;
568 ei->i_dir_acl = 0;
569 ei->i_dtime = 0;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700570 ei->i_block_alloc_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ei->i_block_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 ei->i_dir_start_lookup = 0;
573 ei->i_state = EXT2_STATE_NEW;
574 ext2_set_inode_flags(inode);
575 spin_lock(&sbi->s_next_gen_lock);
576 inode->i_generation = sbi->s_next_generation++;
577 spin_unlock(&sbi->s_next_gen_lock);
Al Viro41080b52008-12-30 01:52:35 -0500578 if (insert_inode_locked(inode) < 0) {
Jan Karaef6919c2011-12-09 00:08:58 +0100579 ext2_error(sb, "ext2_new_inode",
580 "inode number already in use - inode=%lu",
581 (unsigned long) ino);
582 err = -EIO;
583 goto fail;
Al Viro41080b52008-12-30 01:52:35 -0500584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Jan Karac2edb302015-06-29 16:08:45 +0200586 err = dquot_initialize(inode);
587 if (err)
588 goto fail_drop;
589
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500590 err = dquot_alloc_inode(inode);
591 if (err)
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700592 goto fail_drop;
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 err = ext2_init_acl(inode, dir);
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700595 if (err)
596 goto fail_free_drop;
597
Eric Paris2a7dba32011-02-01 11:05:39 -0500598 err = ext2_init_security(inode, dir, qstr);
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700599 if (err)
600 goto fail_free_drop;
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 mark_inode_dirty(inode);
603 ext2_debug("allocating inode %lu\n", inode->i_ino);
604 ext2_preread_inode(inode);
605 return inode;
606
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700607fail_free_drop:
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500608 dquot_free_inode(inode);
Chris Sykes9ed6c2f2005-09-27 21:45:22 -0700609
610fail_drop:
Christoph Hellwig9f754752010-03-03 09:05:05 -0500611 dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 inode->i_flags |= S_NOQUOTA;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200613 clear_nlink(inode);
Al Viro41080b52008-12-30 01:52:35 -0500614 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 iput(inode);
616 return ERR_PTR(err);
617
618fail:
619 make_bad_inode(inode);
620 iput(inode);
621 return ERR_PTR(err);
622}
623
624unsigned long ext2_count_free_inodes (struct super_block * sb)
625{
626 struct ext2_group_desc *desc;
627 unsigned long desc_count = 0;
628 int i;
629
630#ifdef EXT2FS_DEBUG
631 struct ext2_super_block *es;
632 unsigned long bitmap_count = 0;
633 struct buffer_head *bitmap_bh = NULL;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 es = EXT2_SB(sb)->s_es;
636 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
637 unsigned x;
638
639 desc = ext2_get_group_desc (sb, i, NULL);
640 if (!desc)
641 continue;
642 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
643 brelse(bitmap_bh);
644 bitmap_bh = read_inode_bitmap(sb, i);
645 if (!bitmap_bh)
646 continue;
647
648 x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
649 printk("group %d: stored = %d, counted = %u\n",
650 i, le16_to_cpu(desc->bg_free_inodes_count), x);
651 bitmap_count += x;
652 }
653 brelse(bitmap_bh);
654 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
Akinobu Mitaecd0afa2012-07-30 14:41:05 -0700655 (unsigned long)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
657 desc_count, bitmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return desc_count;
659#else
660 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
661 desc = ext2_get_group_desc (sb, i, NULL);
662 if (!desc)
663 continue;
664 desc_count += le16_to_cpu(desc->bg_free_inodes_count);
665 }
666 return desc_count;
667#endif
668}
669
670/* Called at mount-time, super-block is locked */
671unsigned long ext2_count_dirs (struct super_block * sb)
672{
673 unsigned long count = 0;
674 int i;
675
676 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
677 struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
678 if (!gdp)
679 continue;
680 count += le16_to_cpu(gdp->bg_used_dirs_count);
681 }
682 return count;
683}
684