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