ext4: Fix the race between read_inode_bitmap() and ext4_new_inode()
[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"
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);
87 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
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 }
c806e68f
FB
118 if (buffer_uptodate(bh) &&
119 !(desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
e29d1cde
ES
120 return bh;
121
c806e68f 122 lock_buffer(bh);
b5f10eed 123 spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
717d50e4 124 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
e29d1cde
ES
125 ext4_init_inode_bitmap(sb, bh, block_group, desc);
126 set_buffer_uptodate(bh);
b5f10eed 127 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
3300beda 128 unlock_buffer(bh);
e29d1cde 129 return bh;
717d50e4 130 }
b5f10eed 131 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
e29d1cde
ES
132 if (bh_submit_read(bh) < 0) {
133 put_bh(bh);
134 ext4_error(sb, __func__,
ac27a0ec 135 "Cannot read inode bitmap - "
a9df9a49 136 "block_group = %u, inode_bitmap = %llu",
e29d1cde
ES
137 block_group, bitmap_blk);
138 return NULL;
139 }
ac27a0ec
DK
140 return bh;
141}
142
143/*
144 * NOTE! When we get the inode, we're the only people
145 * that have access to it, and as such there are no
146 * race conditions we have to worry about. The inode
147 * is not on the hash-lists, and it cannot be reached
148 * through the filesystem because the directory entry
149 * has been deleted earlier.
150 *
151 * HOWEVER: we must make sure that we get no aliases,
152 * which means that we have to call "clear_inode()"
153 * _before_ we mark the inode not in use in the inode
154 * bitmaps. Otherwise a newly created file might use
155 * the same inode number (not actually the same pointer
156 * though), and then we'd have two inodes sharing the
157 * same inode number and space on the harddisk.
158 */
af5bc92d 159void ext4_free_inode(handle_t *handle, struct inode *inode)
ac27a0ec 160{
af5bc92d 161 struct super_block *sb = inode->i_sb;
ac27a0ec
DK
162 int is_directory;
163 unsigned long ino;
164 struct buffer_head *bitmap_bh = NULL;
165 struct buffer_head *bh2;
fd2d4291 166 ext4_group_t block_group;
ac27a0ec 167 unsigned long bit;
af5bc92d
TT
168 struct ext4_group_desc *gdp;
169 struct ext4_super_block *es;
617ba13b 170 struct ext4_sb_info *sbi;
560671a0 171 int fatal = 0, err, count;
772cb7c8 172 ext4_group_t flex_group;
ac27a0ec
DK
173
174 if (atomic_read(&inode->i_count) > 1) {
4776004f
TT
175 printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
176 atomic_read(&inode->i_count));
ac27a0ec
DK
177 return;
178 }
179 if (inode->i_nlink) {
4776004f
TT
180 printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
181 inode->i_nlink);
ac27a0ec
DK
182 return;
183 }
184 if (!sb) {
4776004f
TT
185 printk(KERN_ERR "ext4_free_inode: inode on "
186 "nonexistent device\n");
ac27a0ec
DK
187 return;
188 }
617ba13b 189 sbi = EXT4_SB(sb);
ac27a0ec
DK
190
191 ino = inode->i_ino;
af5bc92d 192 ext4_debug("freeing inode %lu\n", ino);
ac27a0ec
DK
193
194 /*
195 * Note: we must free any quota before locking the superblock,
196 * as writing the quota to disk may need the lock as well.
197 */
198 DQUOT_INIT(inode);
617ba13b 199 ext4_xattr_delete_inode(handle, inode);
ac27a0ec
DK
200 DQUOT_FREE_INODE(inode);
201 DQUOT_DROP(inode);
202
203 is_directory = S_ISDIR(inode->i_mode);
204
205 /* Do this BEFORE marking the inode not in use or returning an error */
af5bc92d 206 clear_inode(inode);
ac27a0ec 207
617ba13b
MC
208 es = EXT4_SB(sb)->s_es;
209 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
af5bc92d
TT
210 ext4_error(sb, "ext4_free_inode",
211 "reserved or nonexistent inode %lu", ino);
ac27a0ec
DK
212 goto error_return;
213 }
617ba13b
MC
214 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
215 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
e29d1cde 216 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
ac27a0ec
DK
217 if (!bitmap_bh)
218 goto error_return;
219
220 BUFFER_TRACE(bitmap_bh, "get_write_access");
617ba13b 221 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
ac27a0ec
DK
222 if (fatal)
223 goto error_return;
224
225 /* Ok, now we can actually update the inode bitmaps.. */
617ba13b 226 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
ac27a0ec 227 bit, bitmap_bh->b_data))
af5bc92d
TT
228 ext4_error(sb, "ext4_free_inode",
229 "bit already cleared for inode %lu", ino);
ac27a0ec 230 else {
af5bc92d 231 gdp = ext4_get_group_desc(sb, block_group, &bh2);
ac27a0ec
DK
232
233 BUFFER_TRACE(bh2, "get_write_access");
617ba13b 234 fatal = ext4_journal_get_write_access(handle, bh2);
ac27a0ec
DK
235 if (fatal) goto error_return;
236
237 if (gdp) {
238 spin_lock(sb_bgl_lock(sbi, block_group));
560671a0
AK
239 count = ext4_free_inodes_count(sb, gdp) + 1;
240 ext4_free_inodes_set(sb, gdp, count);
241 if (is_directory) {
242 count = ext4_used_dirs_count(sb, gdp) - 1;
243 ext4_used_dirs_set(sb, gdp, count);
244 }
717d50e4
AD
245 gdp->bg_checksum = ext4_group_desc_csum(sbi,
246 block_group, gdp);
ac27a0ec
DK
247 spin_unlock(sb_bgl_lock(sbi, block_group));
248 percpu_counter_inc(&sbi->s_freeinodes_counter);
249 if (is_directory)
250 percpu_counter_dec(&sbi->s_dirs_counter);
251
772cb7c8
JS
252 if (sbi->s_log_groups_per_flex) {
253 flex_group = ext4_flex_group(sbi, block_group);
254 spin_lock(sb_bgl_lock(sbi, flex_group));
255 sbi->s_flex_groups[flex_group].free_inodes++;
256 spin_unlock(sb_bgl_lock(sbi, flex_group));
257 }
ac27a0ec 258 }
0390131b
FM
259 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
260 err = ext4_handle_dirty_metadata(handle, NULL, bh2);
ac27a0ec
DK
261 if (!fatal) fatal = err;
262 }
0390131b
FM
263 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
264 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
ac27a0ec
DK
265 if (!fatal)
266 fatal = err;
267 sb->s_dirt = 1;
268error_return:
269 brelse(bitmap_bh);
617ba13b 270 ext4_std_error(sb, fatal);
ac27a0ec
DK
271}
272
273/*
274 * There are two policies for allocating an inode. If the new inode is
275 * a directory, then a forward search is made for a block group with both
276 * free space and a low directory-to-inode ratio; if that fails, then of
277 * the groups with above-average free space, that group with the fewest
278 * directories already is chosen.
279 *
280 * For other inodes, search forward from the parent directory\'s block
281 * group to find a free inode.
282 */
2aa9fc4c
AM
283static int find_group_dir(struct super_block *sb, struct inode *parent,
284 ext4_group_t *best_group)
ac27a0ec 285{
fd2d4291 286 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
ac27a0ec 287 unsigned int freei, avefreei;
617ba13b 288 struct ext4_group_desc *desc, *best_desc = NULL;
2aa9fc4c
AM
289 ext4_group_t group;
290 int ret = -1;
ac27a0ec 291
617ba13b 292 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
ac27a0ec
DK
293 avefreei = freei / ngroups;
294
295 for (group = 0; group < ngroups; group++) {
af5bc92d 296 desc = ext4_get_group_desc(sb, group, NULL);
560671a0 297 if (!desc || !ext4_free_inodes_count(sb, desc))
ac27a0ec 298 continue;
560671a0 299 if (ext4_free_inodes_count(sb, desc) < avefreei)
ac27a0ec
DK
300 continue;
301 if (!best_desc ||
560671a0
AK
302 (ext4_free_blks_count(sb, desc) >
303 ext4_free_blks_count(sb, best_desc))) {
2aa9fc4c 304 *best_group = group;
ac27a0ec 305 best_desc = desc;
2aa9fc4c 306 ret = 0;
ac27a0ec
DK
307 }
308 }
2aa9fc4c 309 return ret;
ac27a0ec
DK
310}
311
772cb7c8
JS
312#define free_block_ratio 10
313
314static int find_group_flex(struct super_block *sb, struct inode *parent,
315 ext4_group_t *best_group)
316{
317 struct ext4_sb_info *sbi = EXT4_SB(sb);
318 struct ext4_group_desc *desc;
319 struct buffer_head *bh;
320 struct flex_groups *flex_group = sbi->s_flex_groups;
321 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
322 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
323 ext4_group_t ngroups = sbi->s_groups_count;
324 int flex_size = ext4_flex_bg_size(sbi);
325 ext4_group_t best_flex = parent_fbg_group;
326 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
327 int flexbg_free_blocks;
328 int flex_freeb_ratio;
329 ext4_group_t n_fbg_groups;
330 ext4_group_t i;
331
332 n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
333 sbi->s_log_groups_per_flex;
334
335find_close_to_parent:
336 flexbg_free_blocks = flex_group[best_flex].free_blocks;
337 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
338 if (flex_group[best_flex].free_inodes &&
339 flex_freeb_ratio > free_block_ratio)
340 goto found_flexbg;
341
342 if (best_flex && best_flex == parent_fbg_group) {
343 best_flex--;
344 goto find_close_to_parent;
345 }
346
347 for (i = 0; i < n_fbg_groups; i++) {
348 if (i == parent_fbg_group || i == parent_fbg_group - 1)
349 continue;
350
351 flexbg_free_blocks = flex_group[i].free_blocks;
352 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
353
354 if (flex_freeb_ratio > free_block_ratio &&
355 flex_group[i].free_inodes) {
356 best_flex = i;
357 goto found_flexbg;
358 }
359
c001077f 360 if (flex_group[best_flex].free_inodes == 0 ||
772cb7c8
JS
361 (flex_group[i].free_blocks >
362 flex_group[best_flex].free_blocks &&
363 flex_group[i].free_inodes))
364 best_flex = i;
365 }
366
367 if (!flex_group[best_flex].free_inodes ||
368 !flex_group[best_flex].free_blocks)
369 return -1;
370
371found_flexbg:
372 for (i = best_flex * flex_size; i < ngroups &&
373 i < (best_flex + 1) * flex_size; i++) {
374 desc = ext4_get_group_desc(sb, i, &bh);
560671a0 375 if (ext4_free_inodes_count(sb, desc)) {
772cb7c8
JS
376 *best_group = i;
377 goto out;
378 }
379 }
380
381 return -1;
382out:
383 return 0;
384}
385
ac27a0ec
DK
386/*
387 * Orlov's allocator for directories.
388 *
389 * We always try to spread first-level directories.
390 *
391 * If there are blockgroups with both free inodes and free blocks counts
392 * not worse than average we return one with smallest directory count.
393 * Otherwise we simply return a random group.
394 *
395 * For the rest rules look so:
396 *
397 * It's OK to put directory into a group unless
398 * it has too many directories already (max_dirs) or
399 * it has too few free inodes left (min_inodes) or
400 * it has too few free blocks left (min_blocks) or
401 * it's already running too large debt (max_debt).
1cc8dcf5 402 * Parent's group is preferred, if it doesn't satisfy these
ac27a0ec
DK
403 * conditions we search cyclically through the rest. If none
404 * of the groups look good we just look for a group with more
405 * free inodes than average (starting at parent's group).
406 *
407 * Debt is incremented each time we allocate a directory and decremented
408 * when we allocate an inode, within 0--255.
409 */
410
411#define INODE_COST 64
412#define BLOCK_COST 256
413
2aa9fc4c
AM
414static int find_group_orlov(struct super_block *sb, struct inode *parent,
415 ext4_group_t *group)
ac27a0ec 416{
fd2d4291 417 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
617ba13b
MC
418 struct ext4_sb_info *sbi = EXT4_SB(sb);
419 struct ext4_super_block *es = sbi->s_es;
fd2d4291 420 ext4_group_t ngroups = sbi->s_groups_count;
617ba13b 421 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
ac27a0ec 422 unsigned int freei, avefreei;
617ba13b
MC
423 ext4_fsblk_t freeb, avefreeb;
424 ext4_fsblk_t blocks_per_dir;
ac27a0ec
DK
425 unsigned int ndirs;
426 int max_debt, max_dirs, min_inodes;
617ba13b 427 ext4_grpblk_t min_blocks;
2aa9fc4c 428 ext4_group_t i;
617ba13b 429 struct ext4_group_desc *desc;
ac27a0ec
DK
430
431 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
432 avefreei = freei / ngroups;
433 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
3a5b2ecd 434 avefreeb = freeb;
f4e5bc24 435 do_div(avefreeb, ngroups);
ac27a0ec
DK
436 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
437
438 if ((parent == sb->s_root->d_inode) ||
617ba13b 439 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
ac27a0ec 440 int best_ndir = inodes_per_group;
2aa9fc4c
AM
441 ext4_group_t grp;
442 int ret = -1;
ac27a0ec 443
2aa9fc4c
AM
444 get_random_bytes(&grp, sizeof(grp));
445 parent_group = (unsigned)grp % ngroups;
ac27a0ec 446 for (i = 0; i < ngroups; i++) {
2aa9fc4c
AM
447 grp = (parent_group + i) % ngroups;
448 desc = ext4_get_group_desc(sb, grp, NULL);
560671a0 449 if (!desc || !ext4_free_inodes_count(sb, desc))
ac27a0ec 450 continue;
560671a0 451 if (ext4_used_dirs_count(sb, desc) >= best_ndir)
ac27a0ec 452 continue;
560671a0 453 if (ext4_free_inodes_count(sb, desc) < avefreei)
ac27a0ec 454 continue;
560671a0 455 if (ext4_free_blks_count(sb, desc) < avefreeb)
ac27a0ec 456 continue;
2aa9fc4c
AM
457 *group = grp;
458 ret = 0;
560671a0 459 best_ndir = ext4_used_dirs_count(sb, desc);
ac27a0ec 460 }
2aa9fc4c
AM
461 if (ret == 0)
462 return ret;
ac27a0ec
DK
463 goto fallback;
464 }
465
bd81d8ee 466 blocks_per_dir = ext4_blocks_count(es) - freeb;
f4e5bc24 467 do_div(blocks_per_dir, ndirs);
ac27a0ec
DK
468
469 max_dirs = ndirs / ngroups + inodes_per_group / 16;
470 min_inodes = avefreei - inodes_per_group / 4;
617ba13b 471 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
ac27a0ec 472
3a5b2ecd 473 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
f4e5bc24 474 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
ac27a0ec
DK
475 if (max_debt * INODE_COST > inodes_per_group)
476 max_debt = inodes_per_group / INODE_COST;
477 if (max_debt > 255)
478 max_debt = 255;
479 if (max_debt == 0)
480 max_debt = 1;
481
482 for (i = 0; i < ngroups; i++) {
2aa9fc4c
AM
483 *group = (parent_group + i) % ngroups;
484 desc = ext4_get_group_desc(sb, *group, NULL);
560671a0 485 if (!desc || !ext4_free_inodes_count(sb, desc))
ac27a0ec 486 continue;
560671a0 487 if (ext4_used_dirs_count(sb, desc) >= max_dirs)
ac27a0ec 488 continue;
560671a0 489 if (ext4_free_inodes_count(sb, desc) < min_inodes)
ac27a0ec 490 continue;
560671a0 491 if (ext4_free_blks_count(sb, desc) < min_blocks)
ac27a0ec 492 continue;
2aa9fc4c 493 return 0;
ac27a0ec
DK
494 }
495
496fallback:
497 for (i = 0; i < ngroups; i++) {
2aa9fc4c
AM
498 *group = (parent_group + i) % ngroups;
499 desc = ext4_get_group_desc(sb, *group, NULL);
560671a0
AK
500 if (desc && ext4_free_inodes_count(sb, desc) &&
501 ext4_free_inodes_count(sb, desc) >= avefreei)
2aa9fc4c 502 return 0;
ac27a0ec
DK
503 }
504
505 if (avefreei) {
506 /*
507 * The free-inodes counter is approximate, and for really small
508 * filesystems the above test can fail to find any blockgroups
509 */
510 avefreei = 0;
511 goto fallback;
512 }
513
514 return -1;
515}
516
2aa9fc4c
AM
517static int find_group_other(struct super_block *sb, struct inode *parent,
518 ext4_group_t *group)
ac27a0ec 519{
fd2d4291
AM
520 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
521 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
617ba13b 522 struct ext4_group_desc *desc;
2aa9fc4c 523 ext4_group_t i;
ac27a0ec
DK
524
525 /*
526 * Try to place the inode in its parent directory
527 */
2aa9fc4c
AM
528 *group = parent_group;
529 desc = ext4_get_group_desc(sb, *group, NULL);
560671a0
AK
530 if (desc && ext4_free_inodes_count(sb, desc) &&
531 ext4_free_blks_count(sb, desc))
2aa9fc4c 532 return 0;
ac27a0ec
DK
533
534 /*
535 * We're going to place this inode in a different blockgroup from its
536 * parent. We want to cause files in a common directory to all land in
537 * the same blockgroup. But we want files which are in a different
538 * directory which shares a blockgroup with our parent to land in a
539 * different blockgroup.
540 *
541 * So add our directory's i_ino into the starting point for the hash.
542 */
2aa9fc4c 543 *group = (*group + parent->i_ino) % ngroups;
ac27a0ec
DK
544
545 /*
546 * Use a quadratic hash to find a group with a free inode and some free
547 * blocks.
548 */
549 for (i = 1; i < ngroups; i <<= 1) {
2aa9fc4c
AM
550 *group += i;
551 if (*group >= ngroups)
552 *group -= ngroups;
553 desc = ext4_get_group_desc(sb, *group, NULL);
560671a0
AK
554 if (desc && ext4_free_inodes_count(sb, desc) &&
555 ext4_free_blks_count(sb, desc))
2aa9fc4c 556 return 0;
ac27a0ec
DK
557 }
558
559 /*
560 * That failed: try linear search for a free inode, even if that group
561 * has no free blocks.
562 */
2aa9fc4c 563 *group = parent_group;
ac27a0ec 564 for (i = 0; i < ngroups; i++) {
2aa9fc4c
AM
565 if (++*group >= ngroups)
566 *group = 0;
567 desc = ext4_get_group_desc(sb, *group, NULL);
560671a0 568 if (desc && ext4_free_inodes_count(sb, desc))
2aa9fc4c 569 return 0;
ac27a0ec
DK
570 }
571
572 return -1;
573}
574
39341867
AK
575/*
576 * claim the inode from the inode bitmap. If the group
577 * is uninit we need to take the groups's sb_bgl_lock
578 * and clear the uninit flag. The inode bitmap update
579 * and group desc uninit flag clear should be done
580 * after holding sb_bgl_lock so that ext4_read_inode_bitmap
581 * doesn't race with the ext4_claim_inode
582 */
583static int ext4_claim_inode(struct super_block *sb,
584 struct buffer_head *inode_bitmap_bh,
585 unsigned long ino, ext4_group_t group, int mode)
586{
587 int free = 0, retval = 0, count;
588 struct ext4_sb_info *sbi = EXT4_SB(sb);
589 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
590
591 spin_lock(sb_bgl_lock(sbi, group));
592 if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
593 /* not a free inode */
594 retval = 1;
595 goto err_ret;
596 }
597 ino++;
598 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
599 ino > EXT4_INODES_PER_GROUP(sb)) {
600 spin_unlock(sb_bgl_lock(sbi, group));
601 ext4_error(sb, __func__,
602 "reserved inode or inode > inodes count - "
603 "block_group = %u, inode=%lu", group,
604 ino + group * EXT4_INODES_PER_GROUP(sb));
605 return 1;
606 }
607 /* If we didn't allocate from within the initialized part of the inode
608 * table then we need to initialize up to this inode. */
609 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
610
611 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
612 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
613 /* When marking the block group with
614 * ~EXT4_BG_INODE_UNINIT we don't want to depend
615 * on the value of bg_itable_unused even though
616 * mke2fs could have initialized the same for us.
617 * Instead we calculated the value below
618 */
619
620 free = 0;
621 } else {
622 free = EXT4_INODES_PER_GROUP(sb) -
623 ext4_itable_unused_count(sb, gdp);
624 }
625
626 /*
627 * Check the relative inode number against the last used
628 * relative inode number in this group. if it is greater
629 * we need to update the bg_itable_unused count
630 *
631 */
632 if (ino > free)
633 ext4_itable_unused_set(sb, gdp,
634 (EXT4_INODES_PER_GROUP(sb) - ino));
635 }
636 count = ext4_free_inodes_count(sb, gdp) - 1;
637 ext4_free_inodes_set(sb, gdp, count);
638 if (S_ISDIR(mode)) {
639 count = ext4_used_dirs_count(sb, gdp) + 1;
640 ext4_used_dirs_set(sb, gdp, count);
641 }
642 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
643err_ret:
644 spin_unlock(sb_bgl_lock(sbi, group));
645 return retval;
646}
647
ac27a0ec
DK
648/*
649 * There are two policies for allocating an inode. If the new inode is
650 * a directory, then a forward search is made for a block group with both
651 * free space and a low directory-to-inode ratio; if that fails, then of
652 * the groups with above-average free space, that group with the fewest
653 * directories already is chosen.
654 *
655 * For other inodes, search forward from the parent directory's block
656 * group to find a free inode.
657 */
af5bc92d 658struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
ac27a0ec
DK
659{
660 struct super_block *sb;
3300beda
AK
661 struct buffer_head *inode_bitmap_bh = NULL;
662 struct buffer_head *group_desc_bh;
2aa9fc4c 663 ext4_group_t group = 0;
ac27a0ec 664 unsigned long ino = 0;
af5bc92d
TT
665 struct inode *inode;
666 struct ext4_group_desc *gdp = NULL;
667 struct ext4_super_block *es;
617ba13b
MC
668 struct ext4_inode_info *ei;
669 struct ext4_sb_info *sbi;
39341867 670 int ret2, err = 0;
ac27a0ec 671 struct inode *ret;
2aa9fc4c
AM
672 ext4_group_t i;
673 int free = 0;
772cb7c8 674 ext4_group_t flex_group;
ac27a0ec
DK
675
676 /* Cannot create files in a deleted directory */
677 if (!dir || !dir->i_nlink)
678 return ERR_PTR(-EPERM);
679
680 sb = dir->i_sb;
681 inode = new_inode(sb);
682 if (!inode)
683 return ERR_PTR(-ENOMEM);
617ba13b 684 ei = EXT4_I(inode);
ac27a0ec 685
617ba13b 686 sbi = EXT4_SB(sb);
ac27a0ec 687 es = sbi->s_es;
772cb7c8
JS
688
689 if (sbi->s_log_groups_per_flex) {
690 ret2 = find_group_flex(sb, dir, &group);
691 goto got_group;
692 }
693
ac27a0ec 694 if (S_ISDIR(mode)) {
af5bc92d 695 if (test_opt(sb, OLDALLOC))
2aa9fc4c 696 ret2 = find_group_dir(sb, dir, &group);
ac27a0ec 697 else
2aa9fc4c 698 ret2 = find_group_orlov(sb, dir, &group);
ac27a0ec 699 } else
2aa9fc4c 700 ret2 = find_group_other(sb, dir, &group);
ac27a0ec 701
772cb7c8 702got_group:
ac27a0ec 703 err = -ENOSPC;
2aa9fc4c 704 if (ret2 == -1)
ac27a0ec
DK
705 goto out;
706
707 for (i = 0; i < sbi->s_groups_count; i++) {
708 err = -EIO;
709
3300beda 710 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
ac27a0ec
DK
711 if (!gdp)
712 goto fail;
713
3300beda
AK
714 brelse(inode_bitmap_bh);
715 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
716 if (!inode_bitmap_bh)
ac27a0ec
DK
717 goto fail;
718
719 ino = 0;
720
721repeat_in_this_group:
617ba13b 722 ino = ext4_find_next_zero_bit((unsigned long *)
3300beda
AK
723 inode_bitmap_bh->b_data,
724 EXT4_INODES_PER_GROUP(sb), ino);
725
617ba13b 726 if (ino < EXT4_INODES_PER_GROUP(sb)) {
ac27a0ec 727
3300beda
AK
728 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
729 err = ext4_journal_get_write_access(handle,
730 inode_bitmap_bh);
ac27a0ec
DK
731 if (err)
732 goto fail;
733
39341867
AK
734 BUFFER_TRACE(group_desc_bh, "get_write_access");
735 err = ext4_journal_get_write_access(handle,
736 group_desc_bh);
737 if (err)
738 goto fail;
739 if (!ext4_claim_inode(sb, inode_bitmap_bh,
740 ino, group, mode)) {
ac27a0ec 741 /* we won it */
3300beda 742 BUFFER_TRACE(inode_bitmap_bh,
0390131b
FM
743 "call ext4_handle_dirty_metadata");
744 err = ext4_handle_dirty_metadata(handle,
3300beda
AK
745 inode,
746 inode_bitmap_bh);
ac27a0ec
DK
747 if (err)
748 goto fail;
39341867
AK
749 /* zero bit is inode number 1*/
750 ino++;
ac27a0ec
DK
751 goto got;
752 }
753 /* we lost it */
3300beda 754 ext4_handle_release_buffer(handle, inode_bitmap_bh);
39341867 755 ext4_handle_release_buffer(handle, group_desc_bh);
ac27a0ec 756
617ba13b 757 if (++ino < EXT4_INODES_PER_GROUP(sb))
ac27a0ec
DK
758 goto repeat_in_this_group;
759 }
760
761 /*
762 * This case is possible in concurrent environment. It is very
763 * rare. We cannot repeat the find_group_xxx() call because
764 * that will simply return the same blockgroup, because the
765 * group descriptor metadata has not yet been updated.
766 * So we just go onto the next blockgroup.
767 */
768 if (++group == sbi->s_groups_count)
769 group = 0;
770 }
771 err = -ENOSPC;
772 goto out;
773
774got:
717d50e4
AD
775 /* We may have to initialize the block bitmap if it isn't already */
776 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
777 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3300beda 778 struct buffer_head *block_bitmap_bh;
717d50e4 779
3300beda
AK
780 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
781 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
782 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
717d50e4 783 if (err) {
3300beda 784 brelse(block_bitmap_bh);
717d50e4
AD
785 goto fail;
786 }
787
788 free = 0;
789 spin_lock(sb_bgl_lock(sbi, group));
790 /* recheck and clear flag under lock if we still need to */
791 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
717d50e4 792 free = ext4_free_blocks_after_init(sb, group, gdp);
3300beda 793 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
560671a0 794 ext4_free_blks_set(sb, gdp, free);
23712a9c
FB
795 gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
796 gdp);
717d50e4
AD
797 }
798 spin_unlock(sb_bgl_lock(sbi, group));
799
800 /* Don't need to dirty bitmap block if we didn't change it */
801 if (free) {
3300beda 802 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
0390131b 803 err = ext4_handle_dirty_metadata(handle,
3300beda 804 NULL, block_bitmap_bh);
717d50e4
AD
805 }
806
3300beda 807 brelse(block_bitmap_bh);
717d50e4
AD
808 if (err)
809 goto fail;
810 }
3300beda
AK
811 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
812 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
39341867
AK
813 if (err)
814 goto fail;
ac27a0ec
DK
815
816 percpu_counter_dec(&sbi->s_freeinodes_counter);
817 if (S_ISDIR(mode))
818 percpu_counter_inc(&sbi->s_dirs_counter);
819 sb->s_dirt = 1;
820
772cb7c8
JS
821 if (sbi->s_log_groups_per_flex) {
822 flex_group = ext4_flex_group(sbi, group);
823 spin_lock(sb_bgl_lock(sbi, flex_group));
824 sbi->s_flex_groups[flex_group].free_inodes--;
825 spin_unlock(sb_bgl_lock(sbi, flex_group));
826 }
827
4c9c544e 828 inode->i_uid = current_fsuid();
af5bc92d 829 if (test_opt(sb, GRPID))
ac27a0ec
DK
830 inode->i_gid = dir->i_gid;
831 else if (dir->i_mode & S_ISGID) {
832 inode->i_gid = dir->i_gid;
833 if (S_ISDIR(mode))
834 mode |= S_ISGID;
835 } else
4c9c544e 836 inode->i_gid = current_fsgid();
ac27a0ec
DK
837 inode->i_mode = mode;
838
717d50e4 839 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
ac27a0ec
DK
840 /* This is the optimal IO size (for stat), not the fs block size */
841 inode->i_blocks = 0;
ef7f3835
KS
842 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
843 ext4_current_time(inode);
ac27a0ec
DK
844
845 memset(ei->i_data, 0, sizeof(ei->i_data));
846 ei->i_dir_start_lookup = 0;
847 ei->i_disksize = 0;
848
42bf0383
AK
849 /*
850 * Don't inherit extent flag from directory. We set extent flag on
851 * newly created directory and file only if -o extent mount option is
852 * specified
853 */
854 ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
ac27a0ec 855 if (S_ISLNK(mode))
617ba13b 856 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
ac27a0ec
DK
857 /* dirsync only applies to directories */
858 if (!S_ISDIR(mode))
617ba13b 859 ei->i_flags &= ~EXT4_DIRSYNC_FL;
ac27a0ec 860 ei->i_file_acl = 0;
ac27a0ec 861 ei->i_dtime = 0;
ac27a0ec
DK
862 ei->i_block_group = group;
863
617ba13b 864 ext4_set_inode_flags(inode);
ac27a0ec 865 if (IS_DIRSYNC(inode))
0390131b 866 ext4_handle_sync(handle);
6b38e842
AV
867 if (insert_inode_locked(inode) < 0) {
868 err = -EINVAL;
869 goto fail_drop;
870 }
ac27a0ec
DK
871 spin_lock(&sbi->s_next_gen_lock);
872 inode->i_generation = sbi->s_next_generation++;
873 spin_unlock(&sbi->s_next_gen_lock);
874
617ba13b 875 ei->i_state = EXT4_STATE_NEW;
ef7f3835
KS
876
877 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
ac27a0ec
DK
878
879 ret = inode;
af5bc92d 880 if (DQUOT_ALLOC_INODE(inode)) {
ac27a0ec
DK
881 err = -EDQUOT;
882 goto fail_drop;
883 }
884
617ba13b 885 err = ext4_init_acl(handle, inode, dir);
ac27a0ec
DK
886 if (err)
887 goto fail_free_drop;
888
af5bc92d 889 err = ext4_init_security(handle, inode, dir);
ac27a0ec
DK
890 if (err)
891 goto fail_free_drop;
892
a86c6181 893 if (test_opt(sb, EXTENTS)) {
e4079a11 894 /* set extent flag only for directory, file and normal symlink*/
e65187e6 895 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
42bf0383
AK
896 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
897 ext4_ext_tree_init(handle, inode);
42bf0383 898 }
a86c6181 899 }
ac27a0ec 900
8753e88f
AK
901 err = ext4_mark_inode_dirty(handle, inode);
902 if (err) {
903 ext4_std_error(sb, err);
904 goto fail_free_drop;
905 }
906
617ba13b 907 ext4_debug("allocating inode %lu\n", inode->i_ino);
ac27a0ec
DK
908 goto really_out;
909fail:
617ba13b 910 ext4_std_error(sb, err);
ac27a0ec
DK
911out:
912 iput(inode);
913 ret = ERR_PTR(err);
914really_out:
3300beda 915 brelse(inode_bitmap_bh);
ac27a0ec
DK
916 return ret;
917
918fail_free_drop:
919 DQUOT_FREE_INODE(inode);
920
921fail_drop:
922 DQUOT_DROP(inode);
923 inode->i_flags |= S_NOQUOTA;
924 inode->i_nlink = 0;
6b38e842 925 unlock_new_inode(inode);
ac27a0ec 926 iput(inode);
3300beda 927 brelse(inode_bitmap_bh);
ac27a0ec
DK
928 return ERR_PTR(err);
929}
930
931/* Verify that we are loading a valid orphan from disk */
617ba13b 932struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
ac27a0ec 933{
617ba13b 934 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
fd2d4291 935 ext4_group_t block_group;
ac27a0ec 936 int bit;
1d1fe1ee 937 struct buffer_head *bitmap_bh;
ac27a0ec 938 struct inode *inode = NULL;
1d1fe1ee 939 long err = -EIO;
ac27a0ec
DK
940
941 /* Error cases - e2fsck has already cleaned up for us */
942 if (ino > max_ino) {
46e665e9 943 ext4_warning(sb, __func__,
ac27a0ec 944 "bad orphan ino %lu! e2fsck was run?", ino);
1d1fe1ee 945 goto error;
ac27a0ec
DK
946 }
947
617ba13b
MC
948 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
949 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
e29d1cde 950 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
ac27a0ec 951 if (!bitmap_bh) {
46e665e9 952 ext4_warning(sb, __func__,
ac27a0ec 953 "inode bitmap error for orphan %lu", ino);
1d1fe1ee 954 goto error;
ac27a0ec
DK
955 }
956
957 /* Having the inode bit set should be a 100% indicator that this
958 * is a valid orphan (no e2fsck run on fs). Orphans also include
959 * inodes that were being truncated, so we can't check i_nlink==0.
960 */
1d1fe1ee
DH
961 if (!ext4_test_bit(bit, bitmap_bh->b_data))
962 goto bad_orphan;
963
964 inode = ext4_iget(sb, ino);
965 if (IS_ERR(inode))
966 goto iget_failed;
967
91ef4caf
DG
968 /*
969 * If the orphans has i_nlinks > 0 then it should be able to be
970 * truncated, otherwise it won't be removed from the orphan list
971 * during processing and an infinite loop will result.
972 */
973 if (inode->i_nlink && !ext4_can_truncate(inode))
974 goto bad_orphan;
975
1d1fe1ee
DH
976 if (NEXT_ORPHAN(inode) > max_ino)
977 goto bad_orphan;
978 brelse(bitmap_bh);
979 return inode;
980
981iget_failed:
982 err = PTR_ERR(inode);
983 inode = NULL;
984bad_orphan:
46e665e9 985 ext4_warning(sb, __func__,
1d1fe1ee
DH
986 "bad orphan inode %lu! e2fsck was run?", ino);
987 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
988 bit, (unsigned long long)bitmap_bh->b_blocknr,
989 ext4_test_bit(bit, bitmap_bh->b_data));
990 printk(KERN_NOTICE "inode=%p\n", inode);
991 if (inode) {
992 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
993 is_bad_inode(inode));
994 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
995 NEXT_ORPHAN(inode));
996 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
91ef4caf 997 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
ac27a0ec 998 /* Avoid freeing blocks if we got a bad deleted inode */
1d1fe1ee 999 if (inode->i_nlink == 0)
ac27a0ec
DK
1000 inode->i_blocks = 0;
1001 iput(inode);
ac27a0ec 1002 }
ac27a0ec 1003 brelse(bitmap_bh);
1d1fe1ee
DH
1004error:
1005 return ERR_PTR(err);
ac27a0ec
DK
1006}
1007
af5bc92d 1008unsigned long ext4_count_free_inodes(struct super_block *sb)
ac27a0ec
DK
1009{
1010 unsigned long desc_count;
617ba13b 1011 struct ext4_group_desc *gdp;
fd2d4291 1012 ext4_group_t i;
617ba13b
MC
1013#ifdef EXT4FS_DEBUG
1014 struct ext4_super_block *es;
ac27a0ec
DK
1015 unsigned long bitmap_count, x;
1016 struct buffer_head *bitmap_bh = NULL;
1017
617ba13b 1018 es = EXT4_SB(sb)->s_es;
ac27a0ec
DK
1019 desc_count = 0;
1020 bitmap_count = 0;
1021 gdp = NULL;
617ba13b 1022 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
af5bc92d 1023 gdp = ext4_get_group_desc(sb, i, NULL);
ac27a0ec
DK
1024 if (!gdp)
1025 continue;
560671a0 1026 desc_count += ext4_free_inodes_count(sb, gdp);
ac27a0ec 1027 brelse(bitmap_bh);
e29d1cde 1028 bitmap_bh = ext4_read_inode_bitmap(sb, i);
ac27a0ec
DK
1029 if (!bitmap_bh)
1030 continue;
1031
617ba13b 1032 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
c549a95d 1033 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
560671a0 1034 i, ext4_free_inodes_count(sb, gdp), x);
ac27a0ec
DK
1035 bitmap_count += x;
1036 }
1037 brelse(bitmap_bh);
4776004f
TT
1038 printk(KERN_DEBUG "ext4_count_free_inodes: "
1039 "stored = %u, computed = %lu, %lu\n",
1040 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
ac27a0ec
DK
1041 return desc_count;
1042#else
1043 desc_count = 0;
617ba13b 1044 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
af5bc92d 1045 gdp = ext4_get_group_desc(sb, i, NULL);
ac27a0ec
DK
1046 if (!gdp)
1047 continue;
560671a0 1048 desc_count += ext4_free_inodes_count(sb, gdp);
ac27a0ec
DK
1049 cond_resched();
1050 }
1051 return desc_count;
1052#endif
1053}
1054
1055/* Called at mount-time, super-block is locked */
af5bc92d 1056unsigned long ext4_count_dirs(struct super_block * sb)
ac27a0ec
DK
1057{
1058 unsigned long count = 0;
fd2d4291 1059 ext4_group_t i;
ac27a0ec 1060
617ba13b 1061 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
af5bc92d 1062 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
ac27a0ec
DK
1063 if (!gdp)
1064 continue;
560671a0 1065 count += ext4_used_dirs_count(sb, gdp);
ac27a0ec
DK
1066 }
1067 return count;
1068}
This page took 0.289809 seconds and 5 git commands to generate.