]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ext4/ialloc.c
ext4: Avoid races caused by on-line resizing and SMP memory reordering
[mirror_ubuntu-bionic-kernel.git] / fs / ext4 / ialloc.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/ialloc.c
ac27a0ec
DK
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * BSD ufs-inspired inode and directory allocation by
10 * Stephen Tweedie (sct@redhat.com), 1993
11 * Big-endian to little-endian byte-swapping/bitmaps by
12 * David S. Miller (davem@caip.rutgers.edu), 1995
13 */
14
15#include <linux/time.h>
16#include <linux/fs.h>
dab291af 17#include <linux/jbd2.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>
ac27a0ec 25#include <asm/byteorder.h>
3dcf5451
CH
26#include "ext4.h"
27#include "ext4_jbd2.h"
ac27a0ec
DK
28#include "xattr.h"
29#include "acl.h"
717d50e4 30#include "group.h"
ac27a0ec
DK
31
32/*
33 * ialloc.c contains the inodes allocation and deallocation routines
34 */
35
36/*
37 * The free inodes are managed by bitmaps. A file system contains several
38 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
39 * block for inodes, N blocks for the inode table and data blocks.
40 *
41 * The file system contains group descriptors which are located after the
42 * super block. Each descriptor contains the number of the bitmap block and
43 * the free blocks count in the block.
44 */
45
717d50e4
AD
46/*
47 * To avoid calling the atomic setbit hundreds or thousands of times, we only
48 * need to use it within a single byte (to ensure we get endianness right).
49 * We can use memset for the rest of the bitmap as there are no other users.
50 */
51void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
52{
53 int i;
54
55 if (start_bit >= end_bit)
56 return;
57
58 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
59 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
60 ext4_set_bit(i, bitmap);
61 if (i < end_bit)
62 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
63}
64
65/* Initializes an uninitialized inode bitmap */
fd2d4291
AM
66unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
67 ext4_group_t block_group,
717d50e4
AD
68 struct ext4_group_desc *gdp)
69{
70 struct ext4_sb_info *sbi = EXT4_SB(sb);
71
72 J_ASSERT_BH(bh, buffer_locked(bh));
73
74 /* If checksum is bad mark all blocks and inodes use to prevent
75 * allocation, essentially implementing a per-group read-only flag. */
76 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
a9df9a49 77 ext4_error(sb, __func__, "Checksum bad for group %u",
717d50e4 78 block_group);
560671a0
AK
79 ext4_free_blks_set(sb, gdp, 0);
80 ext4_free_inodes_set(sb, gdp, 0);
81 ext4_itable_unused_set(sb, gdp, 0);
717d50e4
AD
82 memset(bh->b_data, 0xff, sb->s_blocksize);
83 return 0;
84 }
85
86 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
648f5879 87 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
717d50e4
AD
88 bh->b_data);
89
90 return EXT4_INODES_PER_GROUP(sb);
91}
ac27a0ec
DK
92
93/*
94 * Read the inode allocation bitmap for a given block_group, reading
95 * into the specified slot in the superblock's bitmap cache.
96 *
97 * Return buffer_head of bitmap on success or NULL.
98 */
99static struct buffer_head *
e29d1cde 100ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
ac27a0ec 101{
617ba13b 102 struct ext4_group_desc *desc;
ac27a0ec 103 struct buffer_head *bh = NULL;
e29d1cde 104 ext4_fsblk_t bitmap_blk;
ac27a0ec 105
617ba13b 106 desc = ext4_get_group_desc(sb, block_group, NULL);
ac27a0ec 107 if (!desc)
e29d1cde
ES
108 return NULL;
109 bitmap_blk = ext4_inode_bitmap(sb, desc);
110 bh = sb_getblk(sb, bitmap_blk);
111 if (unlikely(!bh)) {
112 ext4_error(sb, __func__,
113 "Cannot read inode bitmap - "
a9df9a49 114 "block_group = %u, inode_bitmap = %llu",
e29d1cde
ES
115 block_group, bitmap_blk);
116 return NULL;
117 }
2ccb5fb9 118 if (bitmap_uptodate(bh))
e29d1cde
ES
119 return bh;
120
c806e68f 121 lock_buffer(bh);
2ccb5fb9
AK
122 if (bitmap_uptodate(bh)) {
123 unlock_buffer(bh);
124 return bh;
125 }
b5f10eed 126 spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
717d50e4 127 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
e29d1cde 128 ext4_init_inode_bitmap(sb, bh, block_group, desc);
2ccb5fb9 129 set_bitmap_uptodate(bh);
e29d1cde 130 set_buffer_uptodate(bh);
b5f10eed 131 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
3300beda 132 unlock_buffer(bh);
e29d1cde 133 return bh;
717d50e4 134 }
b5f10eed 135 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
2ccb5fb9
AK
136 if (buffer_uptodate(bh)) {
137 /*
138 * if not uninit if bh is uptodate,
139 * bitmap is also uptodate
140 */
141 set_bitmap_uptodate(bh);
142 unlock_buffer(bh);
143 return bh;
144 }
145 /*
146 * submit the buffer_head for read. We can
147 * safely mark the bitmap as uptodate now.
148 * We do it here so the bitmap uptodate bit
149 * get set with buffer lock held.
150 */
151 set_bitmap_uptodate(bh);
e29d1cde
ES
152 if (bh_submit_read(bh) < 0) {
153 put_bh(bh);
154 ext4_error(sb, __func__,
ac27a0ec 155 "Cannot read inode bitmap - "
a9df9a49 156 "block_group = %u, inode_bitmap = %llu",
e29d1cde
ES
157 block_group, bitmap_blk);
158 return NULL;
159 }
ac27a0ec
DK
160 return bh;
161}
162
163/*
164 * NOTE! When we get the inode, we're the only people
165 * that have access to it, and as such there are no
166 * race conditions we have to worry about. The inode
167 * is not on the hash-lists, and it cannot be reached
168 * through the filesystem because the directory entry
169 * has been deleted earlier.
170 *
171 * HOWEVER: we must make sure that we get no aliases,
172 * which means that we have to call "clear_inode()"
173 * _before_ we mark the inode not in use in the inode
174 * bitmaps. Otherwise a newly created file might use
175 * the same inode number (not actually the same pointer
176 * though), and then we'd have two inodes sharing the
177 * same inode number and space on the harddisk.
178 */
af5bc92d 179void ext4_free_inode(handle_t *handle, struct inode *inode)
ac27a0ec 180{
af5bc92d 181 struct super_block *sb = inode->i_sb;
ac27a0ec
DK
182 int is_directory;
183 unsigned long ino;
184 struct buffer_head *bitmap_bh = NULL;
185 struct buffer_head *bh2;
fd2d4291 186 ext4_group_t block_group;
ac27a0ec 187 unsigned long bit;
af5bc92d
TT
188 struct ext4_group_desc *gdp;
189 struct ext4_super_block *es;
617ba13b 190 struct ext4_sb_info *sbi;
7ce9d5d1 191 int fatal = 0, err, count, cleared;
ac27a0ec
DK
192
193 if (atomic_read(&inode->i_count) > 1) {
4776004f
TT
194 printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
195 atomic_read(&inode->i_count));
ac27a0ec
DK
196 return;
197 }
198 if (inode->i_nlink) {
4776004f
TT
199 printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
200 inode->i_nlink);
ac27a0ec
DK
201 return;
202 }
203 if (!sb) {
4776004f
TT
204 printk(KERN_ERR "ext4_free_inode: inode on "
205 "nonexistent device\n");
ac27a0ec
DK
206 return;
207 }
617ba13b 208 sbi = EXT4_SB(sb);
ac27a0ec
DK
209
210 ino = inode->i_ino;
af5bc92d 211 ext4_debug("freeing inode %lu\n", ino);
ba80b101
TT
212 trace_mark(ext4_free_inode,
213 "dev %s ino %lu mode %d uid %lu gid %lu bocks %llu",
214 sb->s_id, inode->i_ino, inode->i_mode,
215 (unsigned long) inode->i_uid, (unsigned long) inode->i_gid,
216 (unsigned long long) inode->i_blocks);
ac27a0ec
DK
217
218 /*
219 * Note: we must free any quota before locking the superblock,
220 * as writing the quota to disk may need the lock as well.
221 */
a269eb18 222 vfs_dq_init(inode);
617ba13b 223 ext4_xattr_delete_inode(handle, inode);
a269eb18
JK
224 vfs_dq_free_inode(inode);
225 vfs_dq_drop(inode);
ac27a0ec
DK
226
227 is_directory = S_ISDIR(inode->i_mode);
228
229 /* Do this BEFORE marking the inode not in use or returning an error */
af5bc92d 230 clear_inode(inode);
ac27a0ec 231
617ba13b
MC
232 es = EXT4_SB(sb)->s_es;
233 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
af5bc92d
TT
234 ext4_error(sb, "ext4_free_inode",
235 "reserved or nonexistent inode %lu", ino);
ac27a0ec
DK
236 goto error_return;
237 }
617ba13b
MC
238 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
239 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
e29d1cde 240 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
ac27a0ec
DK
241 if (!bitmap_bh)
242 goto error_return;
243
244 BUFFER_TRACE(bitmap_bh, "get_write_access");
617ba13b 245 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
ac27a0ec
DK
246 if (fatal)
247 goto error_return;
248
249 /* Ok, now we can actually update the inode bitmaps.. */
7ce9d5d1
ES
250 spin_lock(sb_bgl_lock(sbi, block_group));
251 cleared = ext4_clear_bit(bit, bitmap_bh->b_data);
252 spin_unlock(sb_bgl_lock(sbi, block_group));
253 if (!cleared)
af5bc92d
TT
254 ext4_error(sb, "ext4_free_inode",
255 "bit already cleared for inode %lu", ino);
ac27a0ec 256 else {
af5bc92d 257 gdp = ext4_get_group_desc(sb, block_group, &bh2);
ac27a0ec
DK
258
259 BUFFER_TRACE(bh2, "get_write_access");
617ba13b 260 fatal = ext4_journal_get_write_access(handle, bh2);
ac27a0ec
DK
261 if (fatal) goto error_return;
262
263 if (gdp) {
264 spin_lock(sb_bgl_lock(sbi, block_group));
560671a0
AK
265 count = ext4_free_inodes_count(sb, gdp) + 1;
266 ext4_free_inodes_set(sb, gdp, count);
267 if (is_directory) {
268 count = ext4_used_dirs_count(sb, gdp) - 1;
269 ext4_used_dirs_set(sb, gdp, count);
7d39db14
TT
270 if (sbi->s_log_groups_per_flex) {
271 ext4_group_t f;
272
273 f = ext4_flex_group(sbi, block_group);
274 atomic_dec(&sbi->s_flex_groups[f].free_inodes);
275 }
276
560671a0 277 }
717d50e4
AD
278 gdp->bg_checksum = ext4_group_desc_csum(sbi,
279 block_group, gdp);
ac27a0ec
DK
280 spin_unlock(sb_bgl_lock(sbi, block_group));
281 percpu_counter_inc(&sbi->s_freeinodes_counter);
282 if (is_directory)
283 percpu_counter_dec(&sbi->s_dirs_counter);
284
772cb7c8 285 if (sbi->s_log_groups_per_flex) {
9f24e420
TT
286 ext4_group_t f;
287
288 f = ext4_flex_group(sbi, block_group);
289 atomic_inc(&sbi->s_flex_groups[f].free_inodes);
772cb7c8 290 }
ac27a0ec 291 }
0390131b
FM
292 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
293 err = ext4_handle_dirty_metadata(handle, NULL, bh2);
ac27a0ec
DK
294 if (!fatal) fatal = err;
295 }
0390131b
FM
296 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
297 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
ac27a0ec
DK
298 if (!fatal)
299 fatal = err;
300 sb->s_dirt = 1;
301error_return:
302 brelse(bitmap_bh);
617ba13b 303 ext4_std_error(sb, fatal);
ac27a0ec
DK
304}
305
306/*
307 * There are two policies for allocating an inode. If the new inode is
308 * a directory, then a forward search is made for a block group with both
309 * free space and a low directory-to-inode ratio; if that fails, then of
310 * the groups with above-average free space, that group with the fewest
311 * directories already is chosen.
312 *
313 * For other inodes, search forward from the parent directory\'s block
314 * group to find a free inode.
315 */
2aa9fc4c
AM
316static int find_group_dir(struct super_block *sb, struct inode *parent,
317 ext4_group_t *best_group)
ac27a0ec 318{
8df9675f 319 ext4_group_t ngroups = ext4_get_groups_count(sb);
ac27a0ec 320 unsigned int freei, avefreei;
617ba13b 321 struct ext4_group_desc *desc, *best_desc = NULL;
2aa9fc4c
AM
322 ext4_group_t group;
323 int ret = -1;
ac27a0ec 324
617ba13b 325 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
ac27a0ec
DK
326 avefreei = freei / ngroups;
327
328 for (group = 0; group < ngroups; group++) {
af5bc92d 329 desc = ext4_get_group_desc(sb, group, NULL);
560671a0 330 if (!desc || !ext4_free_inodes_count(sb, desc))
ac27a0ec 331 continue;
560671a0 332 if (ext4_free_inodes_count(sb, desc) < avefreei)
ac27a0ec
DK
333 continue;
334 if (!best_desc ||
560671a0
AK
335 (ext4_free_blks_count(sb, desc) >
336 ext4_free_blks_count(sb, best_desc))) {
2aa9fc4c 337 *best_group = group;
ac27a0ec 338 best_desc = desc;
2aa9fc4c 339 ret = 0;
ac27a0ec
DK
340 }
341 }
2aa9fc4c 342 return ret;
ac27a0ec
DK
343}
344
772cb7c8
JS
345#define free_block_ratio 10
346
347static int find_group_flex(struct super_block *sb, struct inode *parent,
348 ext4_group_t *best_group)
349{
350 struct ext4_sb_info *sbi = EXT4_SB(sb);
351 struct ext4_group_desc *desc;
352 struct buffer_head *bh;
353 struct flex_groups *flex_group = sbi->s_flex_groups;
354 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
355 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
8df9675f 356 ext4_group_t ngroups = ext4_get_groups_count(sb);
772cb7c8
JS
357 int flex_size = ext4_flex_bg_size(sbi);
358 ext4_group_t best_flex = parent_fbg_group;
359 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
360 int flexbg_free_blocks;
361 int flex_freeb_ratio;
362 ext4_group_t n_fbg_groups;
363 ext4_group_t i;
364
8df9675f 365 n_fbg_groups = (ngroups + flex_size - 1) >>
772cb7c8
JS
366 sbi->s_log_groups_per_flex;
367
368find_close_to_parent:
9f24e420 369 flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks);
772cb7c8 370 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
9f24e420 371 if (atomic_read(&flex_group[best_flex].free_inodes) &&
772cb7c8
JS
372 flex_freeb_ratio > free_block_ratio)
373 goto found_flexbg;
374
375 if (best_flex && best_flex == parent_fbg_group) {
376 best_flex--;
377 goto find_close_to_parent;
378 }
379
380 for (i = 0; i < n_fbg_groups; i++) {
381 if (i == parent_fbg_group || i == parent_fbg_group - 1)
382 continue;
383
9f24e420 384 flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks);
772cb7c8
JS
385 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
386
387 if (flex_freeb_ratio > free_block_ratio &&
9f24e420 388 (atomic_read(&flex_group[i].free_inodes))) {
772cb7c8
JS
389 best_flex = i;
390 goto found_flexbg;
391 }
392
9f24e420
TT
393 if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) ||
394 ((atomic_read(&flex_group[i].free_blocks) >
395 atomic_read(&flex_group[best_flex].free_blocks)) &&
396 atomic_read(&flex_group[i].free_inodes)))
772cb7c8
JS
397 best_flex = i;
398 }
399
9f24e420
TT
400 if (!atomic_read(&flex_group[best_flex].free_inodes) ||
401 !atomic_read(&flex_group[best_flex].free_blocks))
772cb7c8
JS
402 return -1;
403
404found_flexbg:
405 for (i = best_flex * flex_size; i < ngroups &&
406 i < (best_flex + 1) * flex_size; i++) {
407 desc = ext4_get_group_desc(sb, i, &bh);
560671a0 408 if (ext4_free_inodes_count(sb, desc)) {
772cb7c8
JS
409 *best_group = i;
410 goto out;
411 }
412 }
413
414 return -1;
415out:
416 return 0;
417}
418
a4912123
TT
419struct orlov_stats {
420 __u32 free_inodes;
421 __u32 free_blocks;
422 __u32 used_dirs;
423};
424
425/*
426 * Helper function for Orlov's allocator; returns critical information
427 * for a particular block group or flex_bg. If flex_size is 1, then g
428 * is a block group number; otherwise it is flex_bg number.
429 */
430void get_orlov_stats(struct super_block *sb, ext4_group_t g,
431 int flex_size, struct orlov_stats *stats)
432{
433 struct ext4_group_desc *desc;
7d39db14 434 struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
a4912123 435
7d39db14
TT
436 if (flex_size > 1) {
437 stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
438 stats->free_blocks = atomic_read(&flex_group[g].free_blocks);
439 stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
440 return;
441 }
a4912123 442
7d39db14
TT
443 desc = ext4_get_group_desc(sb, g, NULL);
444 if (desc) {
445 stats->free_inodes = ext4_free_inodes_count(sb, desc);
446 stats->free_blocks = ext4_free_blks_count(sb, desc);
447 stats->used_dirs = ext4_used_dirs_count(sb, desc);
448 } else {
449 stats->free_inodes = 0;
450 stats->free_blocks = 0;
451 stats->used_dirs = 0;
a4912123
TT
452 }
453}
454
ac27a0ec
DK
455/*
456 * Orlov's allocator for directories.
457 *
458 * We always try to spread first-level directories.
459 *
460 * If there are blockgroups with both free inodes and free blocks counts
461 * not worse than average we return one with smallest directory count.
462 * Otherwise we simply return a random group.
463 *
464 * For the rest rules look so:
465 *
466 * It's OK to put directory into a group unless
467 * it has too many directories already (max_dirs) or
468 * it has too few free inodes left (min_inodes) or
469 * it has too few free blocks left (min_blocks) or
1cc8dcf5 470 * Parent's group is preferred, if it doesn't satisfy these
ac27a0ec
DK
471 * conditions we search cyclically through the rest. If none
472 * of the groups look good we just look for a group with more
473 * free inodes than average (starting at parent's group).
ac27a0ec
DK
474 */
475
2aa9fc4c 476static int find_group_orlov(struct super_block *sb, struct inode *parent,
a4912123 477 ext4_group_t *group, int mode)
ac27a0ec 478{
fd2d4291 479 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
617ba13b 480 struct ext4_sb_info *sbi = EXT4_SB(sb);
8df9675f 481 ext4_group_t real_ngroups = ext4_get_groups_count(sb);
617ba13b 482 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
ac27a0ec 483 unsigned int freei, avefreei;
617ba13b 484 ext4_fsblk_t freeb, avefreeb;
ac27a0ec 485 unsigned int ndirs;
a4912123 486 int max_dirs, min_inodes;
617ba13b 487 ext4_grpblk_t min_blocks;
8df9675f 488 ext4_group_t i, grp, g, ngroups;
617ba13b 489 struct ext4_group_desc *desc;
a4912123
TT
490 struct orlov_stats stats;
491 int flex_size = ext4_flex_bg_size(sbi);
492
8df9675f 493 ngroups = real_ngroups;
a4912123 494 if (flex_size > 1) {
8df9675f 495 ngroups = (real_ngroups + flex_size - 1) >>
a4912123
TT
496 sbi->s_log_groups_per_flex;
497 parent_group >>= sbi->s_log_groups_per_flex;
498 }
ac27a0ec
DK
499
500 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
501 avefreei = freei / ngroups;
502 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
3a5b2ecd 503 avefreeb = freeb;
f4e5bc24 504 do_div(avefreeb, ngroups);
ac27a0ec
DK
505 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
506
a4912123
TT
507 if (S_ISDIR(mode) &&
508 ((parent == sb->s_root->d_inode) ||
509 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL))) {
ac27a0ec 510 int best_ndir = inodes_per_group;
2aa9fc4c 511 int ret = -1;
ac27a0ec 512
2aa9fc4c
AM
513 get_random_bytes(&grp, sizeof(grp));
514 parent_group = (unsigned)grp % ngroups;
ac27a0ec 515 for (i = 0; i < ngroups; i++) {
a4912123
TT
516 g = (parent_group + i) % ngroups;
517 get_orlov_stats(sb, g, flex_size, &stats);
518 if (!stats.free_inodes)
ac27a0ec 519 continue;
a4912123 520 if (stats.used_dirs >= best_ndir)
ac27a0ec 521 continue;
a4912123 522 if (stats.free_inodes < avefreei)
ac27a0ec 523 continue;
a4912123 524 if (stats.free_blocks < avefreeb)
ac27a0ec 525 continue;
a4912123 526 grp = g;
2aa9fc4c 527 ret = 0;
a4912123
TT
528 best_ndir = stats.used_dirs;
529 }
530 if (ret)
531 goto fallback;
532 found_flex_bg:
533 if (flex_size == 1) {
534 *group = grp;
535 return 0;
536 }
537
538 /*
539 * We pack inodes at the beginning of the flexgroup's
540 * inode tables. Block allocation decisions will do
541 * something similar, although regular files will
542 * start at 2nd block group of the flexgroup. See
543 * ext4_ext_find_goal() and ext4_find_near().
544 */
545 grp *= flex_size;
546 for (i = 0; i < flex_size; i++) {
8df9675f 547 if (grp+i >= real_ngroups)
a4912123
TT
548 break;
549 desc = ext4_get_group_desc(sb, grp+i, NULL);
550 if (desc && ext4_free_inodes_count(sb, desc)) {
551 *group = grp+i;
552 return 0;
553 }
ac27a0ec 554 }
ac27a0ec
DK
555 goto fallback;
556 }
557
ac27a0ec 558 max_dirs = ndirs / ngroups + inodes_per_group / 16;
a4912123
TT
559 min_inodes = avefreei - inodes_per_group*flex_size / 4;
560 if (min_inodes < 1)
561 min_inodes = 1;
562 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb)*flex_size / 4;
563
564 /*
565 * Start looking in the flex group where we last allocated an
566 * inode for this parent directory
567 */
568 if (EXT4_I(parent)->i_last_alloc_group != ~0) {
569 parent_group = EXT4_I(parent)->i_last_alloc_group;
570 if (flex_size > 1)
571 parent_group >>= sbi->s_log_groups_per_flex;
572 }
ac27a0ec
DK
573
574 for (i = 0; i < ngroups; i++) {
a4912123
TT
575 grp = (parent_group + i) % ngroups;
576 get_orlov_stats(sb, grp, flex_size, &stats);
577 if (stats.used_dirs >= max_dirs)
ac27a0ec 578 continue;
a4912123 579 if (stats.free_inodes < min_inodes)
ac27a0ec 580 continue;
a4912123 581 if (stats.free_blocks < min_blocks)
ac27a0ec 582 continue;
a4912123 583 goto found_flex_bg;
ac27a0ec
DK
584 }
585
586fallback:
8df9675f 587 ngroups = real_ngroups;
a4912123 588 avefreei = freei / ngroups;
b5451f7b 589fallback_retry:
a4912123 590 parent_group = EXT4_I(parent)->i_block_group;
ac27a0ec 591 for (i = 0; i < ngroups; i++) {
a4912123
TT
592 grp = (parent_group + i) % ngroups;
593 desc = ext4_get_group_desc(sb, grp, NULL);
560671a0 594 if (desc && ext4_free_inodes_count(sb, desc) &&
a4912123
TT
595 ext4_free_inodes_count(sb, desc) >= avefreei) {
596 *group = grp;
2aa9fc4c 597 return 0;
a4912123 598 }
ac27a0ec
DK
599 }
600
601 if (avefreei) {
602 /*
603 * The free-inodes counter is approximate, and for really small
604 * filesystems the above test can fail to find any blockgroups
605 */
606 avefreei = 0;
b5451f7b 607 goto fallback_retry;
ac27a0ec
DK
608 }
609
610 return -1;
611}
612
2aa9fc4c 613static int find_group_other(struct super_block *sb, struct inode *parent,
a4912123 614 ext4_group_t *group, int mode)
ac27a0ec 615{
fd2d4291 616 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
8df9675f 617 ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
617ba13b 618 struct ext4_group_desc *desc;
a4912123
TT
619 int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
620
621 /*
622 * Try to place the inode is the same flex group as its
623 * parent. If we can't find space, use the Orlov algorithm to
624 * find another flex group, and store that information in the
625 * parent directory's inode information so that use that flex
626 * group for future allocations.
627 */
628 if (flex_size > 1) {
629 int retry = 0;
630
631 try_again:
632 parent_group &= ~(flex_size-1);
633 last = parent_group + flex_size;
634 if (last > ngroups)
635 last = ngroups;
636 for (i = parent_group; i < last; i++) {
637 desc = ext4_get_group_desc(sb, i, NULL);
638 if (desc && ext4_free_inodes_count(sb, desc)) {
639 *group = i;
640 return 0;
641 }
642 }
643 if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
644 retry = 1;
645 parent_group = EXT4_I(parent)->i_last_alloc_group;
646 goto try_again;
647 }
648 /*
649 * If this didn't work, use the Orlov search algorithm
650 * to find a new flex group; we pass in the mode to
651 * avoid the topdir algorithms.
652 */
653 *group = parent_group + flex_size;
654 if (*group > ngroups)
655 *group = 0;
656 return find_group_orlov(sb, parent, group, mode);
657 }
ac27a0ec
DK
658
659 /*
660 * Try to place the inode in its parent directory
661 */
2aa9fc4c
AM
662 *group = parent_group;
663 desc = ext4_get_group_desc(sb, *group, NULL);
560671a0
AK
664 if (desc && ext4_free_inodes_count(sb, desc) &&
665 ext4_free_blks_count(sb, desc))
2aa9fc4c 666 return 0;
ac27a0ec
DK
667
668 /*
669 * We're going to place this inode in a different blockgroup from its
670 * parent. We want to cause files in a common directory to all land in
671 * the same blockgroup. But we want files which are in a different
672 * directory which shares a blockgroup with our parent to land in a
673 * different blockgroup.
674 *
675 * So add our directory's i_ino into the starting point for the hash.
676 */
2aa9fc4c 677 *group = (*group + parent->i_ino) % ngroups;
ac27a0ec
DK
678
679 /*
680 * Use a quadratic hash to find a group with a free inode and some free
681 * blocks.
682 */
683 for (i = 1; i < ngroups; i <<= 1) {
2aa9fc4c
AM
684 *group += i;
685 if (*group >= ngroups)
686 *group -= ngroups;
687 desc = ext4_get_group_desc(sb, *group, NULL);
560671a0
AK
688 if (desc && ext4_free_inodes_count(sb, desc) &&
689 ext4_free_blks_count(sb, desc))
2aa9fc4c 690 return 0;
ac27a0ec
DK
691 }
692
693 /*
694 * That failed: try linear search for a free inode, even if that group
695 * has no free blocks.
696 */
2aa9fc4c 697 *group = parent_group;
ac27a0ec 698 for (i = 0; i < ngroups; i++) {
2aa9fc4c
AM
699 if (++*group >= ngroups)
700 *group = 0;
701 desc = ext4_get_group_desc(sb, *group, NULL);
560671a0 702 if (desc && ext4_free_inodes_count(sb, desc))
2aa9fc4c 703 return 0;
ac27a0ec
DK
704 }
705
706 return -1;
707}
708
39341867
AK
709/*
710 * claim the inode from the inode bitmap. If the group
711 * is uninit we need to take the groups's sb_bgl_lock
712 * and clear the uninit flag. The inode bitmap update
713 * and group desc uninit flag clear should be done
714 * after holding sb_bgl_lock so that ext4_read_inode_bitmap
715 * doesn't race with the ext4_claim_inode
716 */
717static int ext4_claim_inode(struct super_block *sb,
718 struct buffer_head *inode_bitmap_bh,
719 unsigned long ino, ext4_group_t group, int mode)
720{
721 int free = 0, retval = 0, count;
722 struct ext4_sb_info *sbi = EXT4_SB(sb);
723 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
724
725 spin_lock(sb_bgl_lock(sbi, group));
726 if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
727 /* not a free inode */
728 retval = 1;
729 goto err_ret;
730 }
731 ino++;
732 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
733 ino > EXT4_INODES_PER_GROUP(sb)) {
734 spin_unlock(sb_bgl_lock(sbi, group));
735 ext4_error(sb, __func__,
736 "reserved inode or inode > inodes count - "
737 "block_group = %u, inode=%lu", group,
738 ino + group * EXT4_INODES_PER_GROUP(sb));
739 return 1;
740 }
741 /* If we didn't allocate from within the initialized part of the inode
742 * table then we need to initialize up to this inode. */
743 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
744
745 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
746 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
747 /* When marking the block group with
748 * ~EXT4_BG_INODE_UNINIT we don't want to depend
749 * on the value of bg_itable_unused even though
750 * mke2fs could have initialized the same for us.
751 * Instead we calculated the value below
752 */
753
754 free = 0;
755 } else {
756 free = EXT4_INODES_PER_GROUP(sb) -
757 ext4_itable_unused_count(sb, gdp);
758 }
759
760 /*
761 * Check the relative inode number against the last used
762 * relative inode number in this group. if it is greater
763 * we need to update the bg_itable_unused count
764 *
765 */
766 if (ino > free)
767 ext4_itable_unused_set(sb, gdp,
768 (EXT4_INODES_PER_GROUP(sb) - ino));
769 }
770 count = ext4_free_inodes_count(sb, gdp) - 1;
771 ext4_free_inodes_set(sb, gdp, count);
772 if (S_ISDIR(mode)) {
773 count = ext4_used_dirs_count(sb, gdp) + 1;
774 ext4_used_dirs_set(sb, gdp, count);
7d39db14
TT
775 if (sbi->s_log_groups_per_flex) {
776 ext4_group_t f = ext4_flex_group(sbi, group);
777
778 atomic_inc(&sbi->s_flex_groups[f].free_inodes);
779 }
39341867
AK
780 }
781 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
782err_ret:
783 spin_unlock(sb_bgl_lock(sbi, group));
784 return retval;
785}
786
ac27a0ec
DK
787/*
788 * There are two policies for allocating an inode. If the new inode is
789 * a directory, then a forward search is made for a block group with both
790 * free space and a low directory-to-inode ratio; if that fails, then of
791 * the groups with above-average free space, that group with the fewest
792 * directories already is chosen.
793 *
794 * For other inodes, search forward from the parent directory's block
795 * group to find a free inode.
796 */
af5bc92d 797struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
ac27a0ec
DK
798{
799 struct super_block *sb;
3300beda
AK
800 struct buffer_head *inode_bitmap_bh = NULL;
801 struct buffer_head *group_desc_bh;
8df9675f 802 ext4_group_t ngroups, group = 0;
ac27a0ec 803 unsigned long ino = 0;
af5bc92d
TT
804 struct inode *inode;
805 struct ext4_group_desc *gdp = NULL;
617ba13b
MC
806 struct ext4_inode_info *ei;
807 struct ext4_sb_info *sbi;
39341867 808 int ret2, err = 0;
ac27a0ec 809 struct inode *ret;
2aa9fc4c
AM
810 ext4_group_t i;
811 int free = 0;
2842c3b5 812 static int once = 1;
772cb7c8 813 ext4_group_t flex_group;
ac27a0ec
DK
814
815 /* Cannot create files in a deleted directory */
816 if (!dir || !dir->i_nlink)
817 return ERR_PTR(-EPERM);
818
819 sb = dir->i_sb;
8df9675f 820 ngroups = ext4_get_groups_count(sb);
ba80b101
TT
821 trace_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id,
822 dir->i_ino, mode);
ac27a0ec
DK
823 inode = new_inode(sb);
824 if (!inode)
825 return ERR_PTR(-ENOMEM);
617ba13b 826 ei = EXT4_I(inode);
617ba13b 827 sbi = EXT4_SB(sb);
772cb7c8 828
a4912123 829 if (sbi->s_log_groups_per_flex && test_opt(sb, OLDALLOC)) {
772cb7c8 830 ret2 = find_group_flex(sb, dir, &group);
05bf9e83 831 if (ret2 == -1) {
a4912123 832 ret2 = find_group_other(sb, dir, &group, mode);
6b82f3cb 833 if (ret2 == 0 && once) {
2842c3b5 834 once = 0;
05bf9e83
TT
835 printk(KERN_NOTICE "ext4: find_group_flex "
836 "failed, fallback succeeded dir %lu\n",
837 dir->i_ino);
6b82f3cb 838 }
05bf9e83 839 }
772cb7c8
JS
840 goto got_group;
841 }
842
ac27a0ec 843 if (S_ISDIR(mode)) {
af5bc92d 844 if (test_opt(sb, OLDALLOC))
2aa9fc4c 845 ret2 = find_group_dir(sb, dir, &group);
ac27a0ec 846 else
a4912123 847 ret2 = find_group_orlov(sb, dir, &group, mode);
ac27a0ec 848 } else
a4912123 849 ret2 = find_group_other(sb, dir, &group, mode);
ac27a0ec 850
772cb7c8 851got_group:
a4912123 852 EXT4_I(dir)->i_last_alloc_group = group;
ac27a0ec 853 err = -ENOSPC;
2aa9fc4c 854 if (ret2 == -1)
ac27a0ec
DK
855 goto out;
856
8df9675f 857 for (i = 0; i < ngroups; i++) {
ac27a0ec
DK
858 err = -EIO;
859
3300beda 860 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
ac27a0ec
DK
861 if (!gdp)
862 goto fail;
863
3300beda
AK
864 brelse(inode_bitmap_bh);
865 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
866 if (!inode_bitmap_bh)
ac27a0ec
DK
867 goto fail;
868
869 ino = 0;
870
871repeat_in_this_group:
617ba13b 872 ino = ext4_find_next_zero_bit((unsigned long *)
3300beda
AK
873 inode_bitmap_bh->b_data,
874 EXT4_INODES_PER_GROUP(sb), ino);
875
617ba13b 876 if (ino < EXT4_INODES_PER_GROUP(sb)) {
ac27a0ec 877
3300beda
AK
878 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
879 err = ext4_journal_get_write_access(handle,
880 inode_bitmap_bh);
ac27a0ec
DK
881 if (err)
882 goto fail;
883
39341867
AK
884 BUFFER_TRACE(group_desc_bh, "get_write_access");
885 err = ext4_journal_get_write_access(handle,
886 group_desc_bh);
887 if (err)
888 goto fail;
889 if (!ext4_claim_inode(sb, inode_bitmap_bh,
890 ino, group, mode)) {
ac27a0ec 891 /* we won it */
3300beda 892 BUFFER_TRACE(inode_bitmap_bh,
0390131b
FM
893 "call ext4_handle_dirty_metadata");
894 err = ext4_handle_dirty_metadata(handle,
3300beda
AK
895 inode,
896 inode_bitmap_bh);
ac27a0ec
DK
897 if (err)
898 goto fail;
39341867
AK
899 /* zero bit is inode number 1*/
900 ino++;
ac27a0ec
DK
901 goto got;
902 }
903 /* we lost it */
3300beda 904 ext4_handle_release_buffer(handle, inode_bitmap_bh);
39341867 905 ext4_handle_release_buffer(handle, group_desc_bh);
ac27a0ec 906
617ba13b 907 if (++ino < EXT4_INODES_PER_GROUP(sb))
ac27a0ec
DK
908 goto repeat_in_this_group;
909 }
910
911 /*
912 * This case is possible in concurrent environment. It is very
913 * rare. We cannot repeat the find_group_xxx() call because
914 * that will simply return the same blockgroup, because the
915 * group descriptor metadata has not yet been updated.
916 * So we just go onto the next blockgroup.
917 */
8df9675f 918 if (++group == ngroups)
ac27a0ec
DK
919 group = 0;
920 }
921 err = -ENOSPC;
922 goto out;
923
924got:
717d50e4
AD
925 /* We may have to initialize the block bitmap if it isn't already */
926 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
927 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3300beda 928 struct buffer_head *block_bitmap_bh;
717d50e4 929
3300beda
AK
930 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
931 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
932 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
717d50e4 933 if (err) {
3300beda 934 brelse(block_bitmap_bh);
717d50e4
AD
935 goto fail;
936 }
937
938 free = 0;
939 spin_lock(sb_bgl_lock(sbi, group));
940 /* recheck and clear flag under lock if we still need to */
941 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
717d50e4 942 free = ext4_free_blocks_after_init(sb, group, gdp);
3300beda 943 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
560671a0 944 ext4_free_blks_set(sb, gdp, free);
23712a9c
FB
945 gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
946 gdp);
717d50e4
AD
947 }
948 spin_unlock(sb_bgl_lock(sbi, group));
949
950 /* Don't need to dirty bitmap block if we didn't change it */
951 if (free) {
3300beda 952 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
0390131b 953 err = ext4_handle_dirty_metadata(handle,
3300beda 954 NULL, block_bitmap_bh);
717d50e4
AD
955 }
956
3300beda 957 brelse(block_bitmap_bh);
717d50e4
AD
958 if (err)
959 goto fail;
960 }
3300beda
AK
961 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
962 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
39341867
AK
963 if (err)
964 goto fail;
ac27a0ec
DK
965
966 percpu_counter_dec(&sbi->s_freeinodes_counter);
967 if (S_ISDIR(mode))
968 percpu_counter_inc(&sbi->s_dirs_counter);
969 sb->s_dirt = 1;
970
772cb7c8
JS
971 if (sbi->s_log_groups_per_flex) {
972 flex_group = ext4_flex_group(sbi, group);
9f24e420 973 atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
772cb7c8
JS
974 }
975
4c9c544e 976 inode->i_uid = current_fsuid();
af5bc92d 977 if (test_opt(sb, GRPID))
ac27a0ec
DK
978 inode->i_gid = dir->i_gid;
979 else if (dir->i_mode & S_ISGID) {
980 inode->i_gid = dir->i_gid;
981 if (S_ISDIR(mode))
982 mode |= S_ISGID;
983 } else
4c9c544e 984 inode->i_gid = current_fsgid();
ac27a0ec
DK
985 inode->i_mode = mode;
986
717d50e4 987 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
ac27a0ec
DK
988 /* This is the optimal IO size (for stat), not the fs block size */
989 inode->i_blocks = 0;
ef7f3835
KS
990 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
991 ext4_current_time(inode);
ac27a0ec
DK
992
993 memset(ei->i_data, 0, sizeof(ei->i_data));
994 ei->i_dir_start_lookup = 0;
995 ei->i_disksize = 0;
996
42bf0383 997 /*
2dc6b0d4
DG
998 * Don't inherit extent flag from directory, amongst others. We set
999 * extent flag on newly created directory and file only if -o extent
1000 * mount option is specified
42bf0383 1001 */
2dc6b0d4
DG
1002 ei->i_flags =
1003 ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
ac27a0ec 1004 ei->i_file_acl = 0;
ac27a0ec 1005 ei->i_dtime = 0;
ac27a0ec 1006 ei->i_block_group = group;
a4912123 1007 ei->i_last_alloc_group = ~0;
ac27a0ec 1008
617ba13b 1009 ext4_set_inode_flags(inode);
ac27a0ec 1010 if (IS_DIRSYNC(inode))
0390131b 1011 ext4_handle_sync(handle);
6b38e842
AV
1012 if (insert_inode_locked(inode) < 0) {
1013 err = -EINVAL;
1014 goto fail_drop;
1015 }
ac27a0ec
DK
1016 spin_lock(&sbi->s_next_gen_lock);
1017 inode->i_generation = sbi->s_next_generation++;
1018 spin_unlock(&sbi->s_next_gen_lock);
1019
617ba13b 1020 ei->i_state = EXT4_STATE_NEW;
ef7f3835
KS
1021
1022 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
ac27a0ec
DK
1023
1024 ret = inode;
a269eb18 1025 if (vfs_dq_alloc_inode(inode)) {
ac27a0ec
DK
1026 err = -EDQUOT;
1027 goto fail_drop;
1028 }
1029
617ba13b 1030 err = ext4_init_acl(handle, inode, dir);
ac27a0ec
DK
1031 if (err)
1032 goto fail_free_drop;
1033
af5bc92d 1034 err = ext4_init_security(handle, inode, dir);
ac27a0ec
DK
1035 if (err)
1036 goto fail_free_drop;
1037
83982b6f 1038 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
e4079a11 1039 /* set extent flag only for directory, file and normal symlink*/
e65187e6 1040 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
42bf0383
AK
1041 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
1042 ext4_ext_tree_init(handle, inode);
42bf0383 1043 }
a86c6181 1044 }
ac27a0ec 1045
8753e88f
AK
1046 err = ext4_mark_inode_dirty(handle, inode);
1047 if (err) {
1048 ext4_std_error(sb, err);
1049 goto fail_free_drop;
1050 }
1051
617ba13b 1052 ext4_debug("allocating inode %lu\n", inode->i_ino);
ba80b101
TT
1053 trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d",
1054 sb->s_id, inode->i_ino, dir->i_ino, mode);
ac27a0ec
DK
1055 goto really_out;
1056fail:
617ba13b 1057 ext4_std_error(sb, err);
ac27a0ec
DK
1058out:
1059 iput(inode);
1060 ret = ERR_PTR(err);
1061really_out:
3300beda 1062 brelse(inode_bitmap_bh);
ac27a0ec
DK
1063 return ret;
1064
1065fail_free_drop:
a269eb18 1066 vfs_dq_free_inode(inode);
ac27a0ec
DK
1067
1068fail_drop:
a269eb18 1069 vfs_dq_drop(inode);
ac27a0ec
DK
1070 inode->i_flags |= S_NOQUOTA;
1071 inode->i_nlink = 0;
6b38e842 1072 unlock_new_inode(inode);
ac27a0ec 1073 iput(inode);
3300beda 1074 brelse(inode_bitmap_bh);
ac27a0ec
DK
1075 return ERR_PTR(err);
1076}
1077
1078/* Verify that we are loading a valid orphan from disk */
617ba13b 1079struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
ac27a0ec 1080{
617ba13b 1081 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
fd2d4291 1082 ext4_group_t block_group;
ac27a0ec 1083 int bit;
1d1fe1ee 1084 struct buffer_head *bitmap_bh;
ac27a0ec 1085 struct inode *inode = NULL;
1d1fe1ee 1086 long err = -EIO;
ac27a0ec
DK
1087
1088 /* Error cases - e2fsck has already cleaned up for us */
1089 if (ino > max_ino) {
46e665e9 1090 ext4_warning(sb, __func__,
ac27a0ec 1091 "bad orphan ino %lu! e2fsck was run?", ino);
1d1fe1ee 1092 goto error;
ac27a0ec
DK
1093 }
1094
617ba13b
MC
1095 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1096 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
e29d1cde 1097 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
ac27a0ec 1098 if (!bitmap_bh) {
46e665e9 1099 ext4_warning(sb, __func__,
ac27a0ec 1100 "inode bitmap error for orphan %lu", ino);
1d1fe1ee 1101 goto error;
ac27a0ec
DK
1102 }
1103
1104 /* Having the inode bit set should be a 100% indicator that this
1105 * is a valid orphan (no e2fsck run on fs). Orphans also include
1106 * inodes that were being truncated, so we can't check i_nlink==0.
1107 */
1d1fe1ee
DH
1108 if (!ext4_test_bit(bit, bitmap_bh->b_data))
1109 goto bad_orphan;
1110
1111 inode = ext4_iget(sb, ino);
1112 if (IS_ERR(inode))
1113 goto iget_failed;
1114
91ef4caf
DG
1115 /*
1116 * If the orphans has i_nlinks > 0 then it should be able to be
1117 * truncated, otherwise it won't be removed from the orphan list
1118 * during processing and an infinite loop will result.
1119 */
1120 if (inode->i_nlink && !ext4_can_truncate(inode))
1121 goto bad_orphan;
1122
1d1fe1ee
DH
1123 if (NEXT_ORPHAN(inode) > max_ino)
1124 goto bad_orphan;
1125 brelse(bitmap_bh);
1126 return inode;
1127
1128iget_failed:
1129 err = PTR_ERR(inode);
1130 inode = NULL;
1131bad_orphan:
46e665e9 1132 ext4_warning(sb, __func__,
1d1fe1ee
DH
1133 "bad orphan inode %lu! e2fsck was run?", ino);
1134 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1135 bit, (unsigned long long)bitmap_bh->b_blocknr,
1136 ext4_test_bit(bit, bitmap_bh->b_data));
1137 printk(KERN_NOTICE "inode=%p\n", inode);
1138 if (inode) {
1139 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
1140 is_bad_inode(inode));
1141 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
1142 NEXT_ORPHAN(inode));
1143 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
91ef4caf 1144 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
ac27a0ec 1145 /* Avoid freeing blocks if we got a bad deleted inode */
1d1fe1ee 1146 if (inode->i_nlink == 0)
ac27a0ec
DK
1147 inode->i_blocks = 0;
1148 iput(inode);
ac27a0ec 1149 }
ac27a0ec 1150 brelse(bitmap_bh);
1d1fe1ee
DH
1151error:
1152 return ERR_PTR(err);
ac27a0ec
DK
1153}
1154
af5bc92d 1155unsigned long ext4_count_free_inodes(struct super_block *sb)
ac27a0ec
DK
1156{
1157 unsigned long desc_count;
617ba13b 1158 struct ext4_group_desc *gdp;
8df9675f 1159 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
617ba13b
MC
1160#ifdef EXT4FS_DEBUG
1161 struct ext4_super_block *es;
ac27a0ec
DK
1162 unsigned long bitmap_count, x;
1163 struct buffer_head *bitmap_bh = NULL;
1164
617ba13b 1165 es = EXT4_SB(sb)->s_es;
ac27a0ec
DK
1166 desc_count = 0;
1167 bitmap_count = 0;
1168 gdp = NULL;
8df9675f 1169 for (i = 0; i < ngroups; i++) {
af5bc92d 1170 gdp = ext4_get_group_desc(sb, i, NULL);
ac27a0ec
DK
1171 if (!gdp)
1172 continue;
560671a0 1173 desc_count += ext4_free_inodes_count(sb, gdp);
ac27a0ec 1174 brelse(bitmap_bh);
e29d1cde 1175 bitmap_bh = ext4_read_inode_bitmap(sb, i);
ac27a0ec
DK
1176 if (!bitmap_bh)
1177 continue;
1178
617ba13b 1179 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
c549a95d 1180 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
560671a0 1181 i, ext4_free_inodes_count(sb, gdp), x);
ac27a0ec
DK
1182 bitmap_count += x;
1183 }
1184 brelse(bitmap_bh);
4776004f
TT
1185 printk(KERN_DEBUG "ext4_count_free_inodes: "
1186 "stored = %u, computed = %lu, %lu\n",
1187 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
ac27a0ec
DK
1188 return desc_count;
1189#else
1190 desc_count = 0;
8df9675f 1191 for (i = 0; i < ngroups; i++) {
af5bc92d 1192 gdp = ext4_get_group_desc(sb, i, NULL);
ac27a0ec
DK
1193 if (!gdp)
1194 continue;
560671a0 1195 desc_count += ext4_free_inodes_count(sb, gdp);
ac27a0ec
DK
1196 cond_resched();
1197 }
1198 return desc_count;
1199#endif
1200}
1201
1202/* Called at mount-time, super-block is locked */
af5bc92d 1203unsigned long ext4_count_dirs(struct super_block * sb)
ac27a0ec
DK
1204{
1205 unsigned long count = 0;
8df9675f 1206 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
ac27a0ec 1207
8df9675f 1208 for (i = 0; i < ngroups; i++) {
af5bc92d 1209 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
ac27a0ec
DK
1210 if (!gdp)
1211 continue;
560671a0 1212 count += ext4_used_dirs_count(sb, gdp);
ac27a0ec
DK
1213 }
1214 return count;
1215}