ialloc.c 22 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  linux/fs/ext3/ialloc.c
 *
 * Copyright (C) 1992, 1993, 1994, 1995
 * Remy Card (card@masi.ibp.fr)
 * Laboratoire MASI - Institut Blaise Pascal
 * Universite Pierre et Marie Curie (Paris VI)
 *
 *  BSD ufs-inspired inode and directory allocation by
 *  Stephen Tweedie (sct@redhat.com), 1993
 *  Big-endian to little-endian byte-swapping/bitmaps by
 *        David S. Miller (davem@caip.rutgers.edu), 1995
 */

15
#include <linux/time.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19 20 21 22
#include <linux/fs.h>
#include <linux/jbd.h>
#include <linux/ext3_fs.h>
#include <linux/ext3_jbd.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/quotaops.h>
23
#include <linux/buffer_head.h>
24
#include <linux/random.h>
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27 28

#include <asm/bitops.h>
#include <asm/byteorder.h>

29
#include "xattr.h"
30
#include "acl.h"
31

Linus Torvalds's avatar
Linus Torvalds committed
32 33 34 35 36 37 38 39 40 41 42
/*
 * ialloc.c contains the inodes allocation and deallocation routines
 */

/*
 * The free inodes are managed by bitmaps.  A file system contains several
 * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
 * block for inodes, N blocks for the inode table and data blocks.
 *
 * The file system contains group descriptors which are located after the
 * super block.  Each descriptor contains the number of the bitmap block and
43
 * the free blocks count in the block.
Linus Torvalds's avatar
Linus Torvalds committed
44 45 46 47 48 49 50
 */


/*
 * Read the inode allocation bitmap for a given block_group, reading
 * into the specified slot in the superblock's bitmap cache.
 *
51
 * Return buffer_head of bitmap on success or NULL.
Linus Torvalds's avatar
Linus Torvalds committed
52
 */
53 54
static struct buffer_head *
read_inode_bitmap(struct super_block * sb, unsigned long block_group)
Linus Torvalds's avatar
Linus Torvalds committed
55
{
56 57
	struct ext3_group_desc *desc;
	struct buffer_head *bh = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
58

59 60
	desc = ext3_get_group_desc(sb, block_group, NULL);
	if (!desc)
Linus Torvalds's avatar
Linus Torvalds committed
61
		goto error_out;
62 63 64 65

	bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
	if (!bh)
		ext3_error(sb, "read_inode_bitmap",
Linus Torvalds's avatar
Linus Torvalds committed
66 67
			    "Cannot read inode bitmap - "
			    "block_group = %lu, inode_bitmap = %lu",
68
			    block_group, (unsigned long) desc->bg_inode_bitmap);
Linus Torvalds's avatar
Linus Torvalds committed
69
error_out:
70
	return bh;
Linus Torvalds's avatar
Linus Torvalds committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

/*
 * NOTE! When we get the inode, we're the only people
 * that have access to it, and as such there are no
 * race conditions we have to worry about. The inode
 * is not on the hash-lists, and it cannot be reached
 * through the filesystem because the directory entry
 * has been deleted earlier.
 *
 * HOWEVER: we must make sure that we get no aliases,
 * which means that we have to call "clear_inode()"
 * _before_ we mark the inode not in use in the inode
 * bitmaps. Otherwise a newly created file might use
 * the same inode number (not actually the same pointer
 * though), and then we'd have two inodes sharing the
 * same inode number and space on the harddisk.
 */
void ext3_free_inode (handle_t *handle, struct inode * inode)
{
	struct super_block * sb = inode->i_sb;
	int is_directory;
	unsigned long ino;
94 95
	struct buffer_head *bitmap_bh = NULL;
	struct buffer_head *bh2;
Linus Torvalds's avatar
Linus Torvalds committed
96 97 98 99
	unsigned long block_group;
	unsigned long bit;
	struct ext3_group_desc * gdp;
	struct ext3_super_block * es;
100
	struct ext3_sb_info *sbi = EXT3_SB(sb);
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	int fatal = 0, err;

	if (atomic_read(&inode->i_count) > 1) {
		printk ("ext3_free_inode: inode has count=%d\n",
					atomic_read(&inode->i_count));
		return;
	}
	if (inode->i_nlink) {
		printk ("ext3_free_inode: inode has nlink=%d\n",
			inode->i_nlink);
		return;
	}
	if (!sb) {
		printk("ext3_free_inode: inode on nonexistent device\n");
		return;
	}

	ino = inode->i_ino;
	ext3_debug ("freeing inode %lu\n", ino);

	/*
	 * Note: we must free any quota before locking the superblock,
	 * as writing the quota to disk may need the lock as well.
	 */
	DQUOT_INIT(inode);
126
	ext3_xattr_delete_inode(handle, inode);
Linus Torvalds's avatar
Linus Torvalds committed
127 128 129 130 131 132 133 134
	DQUOT_FREE_INODE(inode);
	DQUOT_DROP(inode);

	is_directory = S_ISDIR(inode->i_mode);

	/* Do this BEFORE marking the inode not in use or returning an error */
	clear_inode (inode);

135
	es = EXT3_SB(sb)->s_es;
Linus Torvalds's avatar
Linus Torvalds committed
136 137 138 139 140 141 142
	if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
		ext3_error (sb, "ext3_free_inode",
			    "reserved or nonexistent inode %lu", ino);
		goto error_return;
	}
	block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
	bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
143 144
	bitmap_bh = read_inode_bitmap(sb, block_group);
	if (!bitmap_bh)
Linus Torvalds's avatar
Linus Torvalds committed
145 146
		goto error_return;

147 148
	BUFFER_TRACE(bitmap_bh, "get_write_access");
	fatal = ext3_journal_get_write_access(handle, bitmap_bh);
Linus Torvalds's avatar
Linus Torvalds committed
149 150 151 152
	if (fatal)
		goto error_return;

	/* Ok, now we can actually update the inode bitmaps.. */
153 154
	if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
					bit, bitmap_bh->b_data))
Linus Torvalds's avatar
Linus Torvalds committed
155 156 157 158 159 160 161 162 163 164
		ext3_error (sb, "ext3_free_inode",
			      "bit already cleared for inode %lu", ino);
	else {
		gdp = ext3_get_group_desc (sb, block_group, &bh2);

		BUFFER_TRACE(bh2, "get_write_access");
		fatal = ext3_journal_get_write_access(handle, bh2);
		if (fatal) goto error_return;

		if (gdp) {
165
			spin_lock(sb_bgl_lock(sbi, block_group));
Linus Torvalds's avatar
Linus Torvalds committed
166 167
			gdp->bg_free_inodes_count = cpu_to_le16(
				le16_to_cpu(gdp->bg_free_inodes_count) + 1);
168
			if (is_directory)
Linus Torvalds's avatar
Linus Torvalds committed
169 170
				gdp->bg_used_dirs_count = cpu_to_le16(
				  le16_to_cpu(gdp->bg_used_dirs_count) - 1);
171 172 173 174 175
			spin_unlock(sb_bgl_lock(sbi, block_group));
			percpu_counter_inc(&sbi->s_freeinodes_counter);
			if (is_directory)
				percpu_counter_dec(&sbi->s_dirs_counter);

Linus Torvalds's avatar
Linus Torvalds committed
176 177 178 179 180
		}
		BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
		err = ext3_journal_dirty_metadata(handle, bh2);
		if (!fatal) fatal = err;
	}
181 182
	BUFFER_TRACE(bitmap_bh, "call ext3_journal_dirty_metadata");
	err = ext3_journal_dirty_metadata(handle, bitmap_bh);
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186
	if (!fatal)
		fatal = err;
	sb->s_dirt = 1;
error_return:
187
	brelse(bitmap_bh);
Linus Torvalds's avatar
Linus Torvalds committed
188 189 190
	ext3_std_error(sb, fatal);
}

191 192 193 194 195 196 197 198 199 200 201 202 203
/*
 * There are two policies for allocating an inode.  If the new inode is
 * a directory, then a forward search is made for a block group with both
 * free space and a low directory-to-inode ratio; if that fails, then of
 * the groups with above-average free space, that group with the fewest
 * directories already is chosen.
 *
 * For other inodes, search forward from the parent directory\'s block
 * group to find a free inode.
 */
static int find_group_dir(struct super_block *sb, struct inode *parent)
{
	int ngroups = EXT3_SB(sb)->s_groups_count;
204
	int freei, avefreei;
205
	struct ext3_group_desc *desc, *best_desc = NULL;
206
	struct buffer_head *bh;
207 208
	int group, best_group = -1;

209 210 211
	freei = percpu_counter_read_positive(&EXT3_SB(sb)->s_freeinodes_counter);
	avefreei = freei / ngroups;

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	for (group = 0; group < ngroups; group++) {
		desc = ext3_get_group_desc (sb, group, &bh);
		if (!desc || !desc->bg_free_inodes_count)
			continue;
		if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
			continue;
		if (!best_desc || 
		    (le16_to_cpu(desc->bg_free_blocks_count) >
		     le16_to_cpu(best_desc->bg_free_blocks_count))) {
			best_group = group;
			best_desc = desc;
		}
	}
	return best_group;
}

/* 
 * Orlov's allocator for directories. 
 * 
 * We always try to spread first-level directories.
 *
 * If there are blockgroups with both free inodes and free blocks counts 
 * not worse than average we return one with smallest directory count. 
 * Otherwise we simply return a random group. 
 * 
 * For the rest rules look so: 
 * 
 * It's OK to put directory into a group unless 
 * it has too many directories already (max_dirs) or 
 * it has too few free inodes left (min_inodes) or 
 * it has too few free blocks left (min_blocks) or 
 * it's already running too large debt (max_debt). 
 * Parent's group is prefered, if it doesn't satisfy these 
 * conditions we search cyclically through the rest. If none 
 * of the groups look good we just look for a group with more 
 * free inodes than average (starting at parent's group). 
 * 
 * Debt is incremented each time we allocate a directory and decremented 
 * when we allocate an inode, within 0--255. 
 */ 

#define INODE_COST 64
#define BLOCK_COST 256

static int find_group_orlov(struct super_block *sb, struct inode *parent)
{
	int parent_group = EXT3_I(parent)->i_block_group;
	struct ext3_sb_info *sbi = EXT3_SB(sb);
	struct ext3_super_block *es = sbi->s_es;
	int ngroups = sbi->s_groups_count;
	int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
263 264 265
	int freei, avefreei;
	int freeb, avefreeb;
	int blocks_per_dir, ndirs;
266 267 268 269 270
	int max_debt, max_dirs, min_blocks, min_inodes;
	int group = -1, i;
	struct ext3_group_desc *desc;
	struct buffer_head *bh;

271 272 273 274 275 276
	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
	avefreei = freei / ngroups;
	freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
	avefreeb = freeb / ngroups;
	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	if ((parent == sb->s_root->d_inode) ||
	    (parent->i_flags & EXT3_TOPDIR_FL)) {
		int best_ndir = inodes_per_group;
		int best_group = -1;

		get_random_bytes(&group, sizeof(group));
		parent_group = (unsigned)group % ngroups;
		for (i = 0; i < ngroups; i++) {
			group = (parent_group + i) % ngroups;
			desc = ext3_get_group_desc (sb, group, &bh);
			if (!desc || !desc->bg_free_inodes_count)
				continue;
			if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
				continue;
			if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
				continue;
			if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
				continue;
			best_group = group;
			best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
		}
298 299
		if (best_group >= 0)
			return best_group;
300 301 302
		goto fallback;
	}

303
	blocks_per_dir = (le32_to_cpu(es->s_blocks_count) - freeb) / ndirs;
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

	max_dirs = ndirs / ngroups + inodes_per_group / 16;
	min_inodes = avefreei - inodes_per_group / 4;
	min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;

	max_debt = EXT3_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
	if (max_debt * INODE_COST > inodes_per_group)
		max_debt = inodes_per_group / INODE_COST;
	if (max_debt > 255)
		max_debt = 255;
	if (max_debt == 0)
		max_debt = 1;

	for (i = 0; i < ngroups; i++) {
		group = (parent_group + i) % ngroups;
		desc = ext3_get_group_desc (sb, group, &bh);
		if (!desc || !desc->bg_free_inodes_count)
			continue;
322
		if (sbi->s_debts[group] >= max_debt)
323 324 325 326 327 328 329
			continue;
		if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
			continue;
		if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
			continue;
		if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
			continue;
330
		return group;
331 332 333 334 335 336 337 338 339
	}

fallback:
	for (i = 0; i < ngroups; i++) {
		group = (parent_group + i) % ngroups;
		desc = ext3_get_group_desc (sb, group, &bh);
		if (!desc || !desc->bg_free_inodes_count)
			continue;
		if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
340
			return group;
341 342
	}

343 344 345 346 347 348 349 350 351
	if (avefreei) {
		/*
		 * The free-inodes counter is approximate, and for really small
		 * filesystems the above test can fail to find any blockgroups
		 */
		avefreei = 0;
		goto fallback;
	}

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	return -1;
}

static int find_group_other(struct super_block *sb, struct inode *parent)
{
	int parent_group = EXT3_I(parent)->i_block_group;
	int ngroups = EXT3_SB(sb)->s_groups_count;
	struct ext3_group_desc *desc;
	struct buffer_head *bh;
	int group, i;

	/*
	 * Try to place the inode in its parent directory
	 */
	group = parent_group;
	desc = ext3_get_group_desc (sb, group, &bh);
368 369
	if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
			le16_to_cpu(desc->bg_free_blocks_count))
370
		return group;
371 372

	/*
373 374 375 376 377 378 379 380 381 382 383 384 385
	 * We're going to place this inode in a different blockgroup from its
	 * parent.  We want to cause files in a common directory to all land in
	 * the same blockgroup.  But we want files which are in a different
	 * directory which shares a blockgroup with our parent to land in a
	 * different blockgroup.
	 *
	 * So add our directory's i_ino into the starting point for the hash.
	 */
	group = (group + parent->i_ino) % ngroups;

	/*
	 * Use a quadratic hash to find a group with a free inode and some free
	 * blocks.
386 387 388 389 390 391
	 */
	for (i = 1; i < ngroups; i <<= 1) {
		group += i;
		if (group >= ngroups)
			group -= ngroups;
		desc = ext3_get_group_desc (sb, group, &bh);
392 393
		if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
				le16_to_cpu(desc->bg_free_blocks_count))
394
			return group;
395 396 397
	}

	/*
398 399
	 * That failed: try linear search for a free inode, even if that group
	 * has no free blocks.
400 401 402 403 404 405 406
	 */
	group = parent_group + 1;
	for (i = 2; i < ngroups; i++) {
		if (++group >= ngroups)
			group = 0;
		desc = ext3_get_group_desc (sb, group, &bh);
		if (desc && le16_to_cpu(desc->bg_free_inodes_count))
407
			return group;
408 409 410 411 412
	}

	return -1;
}

Linus Torvalds's avatar
Linus Torvalds committed
413 414 415 416 417 418 419 420 421 422
/*
 * There are two policies for allocating an inode.  If the new inode is
 * a directory, then a forward search is made for a block group with both
 * free space and a low directory-to-inode ratio; if that fails, then of
 * the groups with above-average free space, that group with the fewest
 * directories already is chosen.
 *
 * For other inodes, search forward from the parent directory's block
 * group to find a free inode.
 */
423
struct inode *ext3_new_inode(handle_t *handle, struct inode * dir, int mode)
Linus Torvalds's avatar
Linus Torvalds committed
424
{
425 426 427
	struct super_block *sb;
	struct buffer_head *bitmap_bh = NULL;
	struct buffer_head *bh2;
428
	int group;
429
	unsigned long ino = 0;
Linus Torvalds's avatar
Linus Torvalds committed
430
	struct inode * inode;
431
	struct ext3_group_desc * gdp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
432
	struct ext3_super_block * es;
Linus Torvalds's avatar
Linus Torvalds committed
433
	struct ext3_inode_info *ei;
434
	struct ext3_sb_info *sbi;
Linus Torvalds's avatar
Linus Torvalds committed
435
	int err = 0;
436
	struct inode *ret;
437
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
438 439 440 441 442 443 444 445 446

	/* Cannot create files in a deleted directory */
	if (!dir || !dir->i_nlink)
		return ERR_PTR(-EPERM);

	sb = dir->i_sb;
	inode = new_inode(sb);
	if (!inode)
		return ERR_PTR(-ENOMEM);
Linus Torvalds's avatar
Linus Torvalds committed
447
	ei = EXT3_I(inode);
Linus Torvalds's avatar
Linus Torvalds committed
448

449
	es = EXT3_SB(sb)->s_es;
450
	sbi = EXT3_SB(sb);
Linus Torvalds's avatar
Linus Torvalds committed
451
	if (S_ISDIR(mode)) {
452 453
		if (test_opt (sb, OLDALLOC))
			group = find_group_dir(sb, dir);
Linus Torvalds's avatar
Linus Torvalds committed
454
		else
455 456 457
			group = find_group_orlov(sb, dir);
	} else 
		group = find_group_other(sb, dir);
Linus Torvalds's avatar
Linus Torvalds committed
458 459

	err = -ENOSPC;
460
	if (group == -1)
461
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
462

463 464
	for (i = 0; i < sbi->s_groups_count; i++) {
		gdp = ext3_get_group_desc(sb, group, &bh2);
465

466 467 468 469 470
		err = -EIO;
		brelse(bitmap_bh);
		bitmap_bh = read_inode_bitmap(sb, group);
		if (!bitmap_bh)
			goto fail;
471

472 473 474
		ino = ext3_find_first_zero_bit((unsigned long *)
				bitmap_bh->b_data, EXT3_INODES_PER_GROUP(sb));
		if (ino < EXT3_INODES_PER_GROUP(sb)) {
475 476
			int credits = 0;

477
			BUFFER_TRACE(bitmap_bh, "get_write_access");
478 479
			err = ext3_journal_get_write_access_credits(handle,
							bitmap_bh, &credits);
480
			if (err)
Linus Torvalds's avatar
Linus Torvalds committed
481 482
				goto fail;

483 484 485 486 487 488 489 490 491 492 493 494
			if (!ext3_set_bit_atomic(sb_bgl_lock(sbi, group),
						ino, bitmap_bh->b_data)) {
				/* we won it */
				BUFFER_TRACE(bitmap_bh,
					"call ext3_journal_dirty_metadata");
				err = ext3_journal_dirty_metadata(handle,
								bitmap_bh);
				if (err)
					goto fail;
				goto got;
			}
			/* we lost it */
495
			journal_release_buffer(handle, bitmap_bh, credits);
Linus Torvalds's avatar
Linus Torvalds committed
496
		}
497 498 499 500 501 502 503 504 505 506

		/*
		 * This case is possible in concurrent environment.  It is very
		 * rare.  We cannot repeat the find_group_xxx() call because
		 * that will simply return the same blockgroup, because the
		 * group descriptor metadata has not yet been updated.
		 * So we just go onto the next blockgroup.
		 */
		if (++group == sbi->s_groups_count)
			group = 0;
Linus Torvalds's avatar
Linus Torvalds committed
507
	}
508 509 510 511
	err = -ENOSPC;
	goto out;

got:
512 513
	ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
	if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
Linus Torvalds's avatar
Linus Torvalds committed
514 515
		ext3_error (sb, "ext3_new_inode",
			    "reserved inode or inode > inodes count - "
516
			    "block_group = %d, inode=%lu", group, ino);
Linus Torvalds's avatar
Linus Torvalds committed
517 518 519 520 521 522 523
		err = -EIO;
		goto fail;
	}

	BUFFER_TRACE(bh2, "get_write_access");
	err = ext3_journal_get_write_access(handle, bh2);
	if (err) goto fail;
524
	spin_lock(sb_bgl_lock(sbi, group));
Linus Torvalds's avatar
Linus Torvalds committed
525 526
	gdp->bg_free_inodes_count =
		cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
527
	if (S_ISDIR(mode)) {
Linus Torvalds's avatar
Linus Torvalds committed
528 529
		gdp->bg_used_dirs_count =
			cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
530
	}
531
	spin_unlock(sb_bgl_lock(sbi, group));
Linus Torvalds's avatar
Linus Torvalds committed
532 533 534
	BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
	err = ext3_journal_dirty_metadata(handle, bh2);
	if (err) goto fail;
535

536 537 538
	percpu_counter_dec(&sbi->s_freeinodes_counter);
	if (S_ISDIR(mode))
		percpu_counter_inc(&sbi->s_dirs_counter);
Linus Torvalds's avatar
Linus Torvalds committed
539 540 541 542 543 544 545 546 547 548 549 550 551
	sb->s_dirt = 1;

	inode->i_uid = current->fsuid;
	if (test_opt (sb, GRPID))
		inode->i_gid = dir->i_gid;
	else if (dir->i_mode & S_ISGID) {
		inode->i_gid = dir->i_gid;
		if (S_ISDIR(mode))
			mode |= S_ISGID;
	} else
		inode->i_gid = current->fsgid;
	inode->i_mode = mode;

552
	inode->i_ino = ino;
Linus Torvalds's avatar
Linus Torvalds committed
553 554 555 556
	/* This is the optimal IO size (for stat), not the fs block size */
	inode->i_blksize = PAGE_SIZE;
	inode->i_blocks = 0;
	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Linus Torvalds's avatar
Linus Torvalds committed
557 558 559 560 561 562 563 564

	memset(ei->i_data, 0, sizeof(ei->i_data));
	ei->i_next_alloc_block = 0;
	ei->i_next_alloc_goal = 0;
	ei->i_dir_start_lookup = 0;
	ei->i_disksize = 0;

	ei->i_flags = EXT3_I(dir)->i_flags & ~EXT3_INDEX_FL;
Linus Torvalds's avatar
Linus Torvalds committed
565
	if (S_ISLNK(mode))
Linus Torvalds's avatar
Linus Torvalds committed
566
		ei->i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
Andrew Morton's avatar
Andrew Morton committed
567 568 569
	/* dirsync only applies to directories */
	if (!S_ISDIR(mode))
		ei->i_flags &= ~EXT3_DIRSYNC_FL;
Linus Torvalds's avatar
Linus Torvalds committed
570
#ifdef EXT3_FRAGMENTS
Linus Torvalds's avatar
Linus Torvalds committed
571 572 573
	ei->i_faddr = 0;
	ei->i_frag_no = 0;
	ei->i_frag_size = 0;
Linus Torvalds's avatar
Linus Torvalds committed
574
#endif
Linus Torvalds's avatar
Linus Torvalds committed
575 576 577
	ei->i_file_acl = 0;
	ei->i_dir_acl = 0;
	ei->i_dtime = 0;
Linus Torvalds's avatar
Linus Torvalds committed
578
#ifdef EXT3_PREALLOCATE
Linus Torvalds's avatar
Linus Torvalds committed
579
	ei->i_prealloc_block = 0;
Linus Torvalds's avatar
Linus Torvalds committed
580
	ei->i_prealloc_count = 0;
Linus Torvalds's avatar
Linus Torvalds committed
581
#endif
582
	ei->i_block_group = group;
583

584
	ext3_set_inode_flags(inode);
Andrew Morton's avatar
Andrew Morton committed
585
	if (IS_DIRSYNC(inode))
Linus Torvalds's avatar
Linus Torvalds committed
586 587
		handle->h_sync = 1;
	insert_inode_hash(inode);
588
	inode->i_generation = EXT3_SB(sb)->s_next_generation++;
Linus Torvalds's avatar
Linus Torvalds committed
589

Linus Torvalds's avatar
Linus Torvalds committed
590
	ei->i_state = EXT3_STATE_NEW;
591

592
	ret = inode;
Linus Torvalds's avatar
Linus Torvalds committed
593 594
	if(DQUOT_ALLOC_INODE(inode)) {
		DQUOT_DROP(inode);
595 596
		err = -EDQUOT;
		goto fail2;
Linus Torvalds's avatar
Linus Torvalds committed
597
	}
598 599 600 601 602 603 604 605 606 607 608 609 610
	err = ext3_init_acl(handle, inode, dir);
	if (err) {
		DQUOT_FREE_INODE(inode);
		goto fail2;
  	}
	err = ext3_mark_inode_dirty(handle, inode);
	if (err) {
		ext3_std_error(sb, err);
		DQUOT_FREE_INODE(inode);
		goto fail2;
	}

	ext3_debug("allocating inode %lu\n", inode->i_ino);
611
	goto really_out;
Linus Torvalds's avatar
Linus Torvalds committed
612
fail:
613 614
	ext3_std_error(sb, err);
out:
Linus Torvalds's avatar
Linus Torvalds committed
615
	iput(inode);
616 617 618 619
	ret = ERR_PTR(err);
really_out:
	brelse(bitmap_bh);
	return ret;
620 621 622 623 624

fail2:
	inode->i_flags |= S_NOQUOTA;
	inode->i_nlink = 0;
	iput(inode);
625
	brelse(bitmap_bh);
626
	return ERR_PTR(err);
Linus Torvalds's avatar
Linus Torvalds committed
627 628 629
}

/* Verify that we are loading a valid orphan from disk */
Andrew Morton's avatar
Andrew Morton committed
630
struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
Linus Torvalds's avatar
Linus Torvalds committed
631
{
Andrew Morton's avatar
Andrew Morton committed
632
	unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
Linus Torvalds's avatar
Linus Torvalds committed
633 634
	unsigned long block_group;
	int bit;
635
	struct buffer_head *bitmap_bh = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
636
	struct inode *inode = NULL;
Andrew Morton's avatar
Andrew Morton committed
637

Linus Torvalds's avatar
Linus Torvalds committed
638 639 640
	/* Error cases - e2fsck has already cleaned up for us */
	if (ino > max_ino) {
		ext3_warning(sb, __FUNCTION__,
Andrew Morton's avatar
Andrew Morton committed
641
			     "bad orphan ino %lu!  e2fsck was run?\n", ino);
642
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
643 644 645 646
	}

	block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
	bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
647 648
	bitmap_bh = read_inode_bitmap(sb, block_group);
	if (!bitmap_bh) {
Linus Torvalds's avatar
Linus Torvalds committed
649
		ext3_warning(sb, __FUNCTION__,
Andrew Morton's avatar
Andrew Morton committed
650
			     "inode bitmap error for orphan %lu\n", ino);
651
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
652 653 654 655 656 657
	}

	/* Having the inode bit set should be a 100% indicator that this
	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
	 * inodes that were being truncated, so we can't check i_nlink==0.
	 */
658 659 660
	if (!ext3_test_bit(bit, bitmap_bh->b_data) ||
			!(inode = iget(sb, ino)) || is_bad_inode(inode) ||
			NEXT_ORPHAN(inode) > max_ino) {
Linus Torvalds's avatar
Linus Torvalds committed
661
		ext3_warning(sb, __FUNCTION__,
Andrew Morton's avatar
Andrew Morton committed
662
			     "bad orphan inode %lu!  e2fsck was run?\n", ino);
663
		printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%llu) = %d\n",
Andrew Morton's avatar
Andrew Morton committed
664 665
		       bit, (unsigned long long)bitmap_bh->b_blocknr,
		       ext3_test_bit(bit, bitmap_bh->b_data));
Linus Torvalds's avatar
Linus Torvalds committed
666 667 668 669
		printk(KERN_NOTICE "inode=%p\n", inode);
		if (inode) {
			printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
			       is_bad_inode(inode));
Andrew Morton's avatar
Andrew Morton committed
670
			printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
Linus Torvalds's avatar
Linus Torvalds committed
671
			       NEXT_ORPHAN(inode));
Andrew Morton's avatar
Andrew Morton committed
672
			printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
Linus Torvalds's avatar
Linus Torvalds committed
673 674 675 676 677
		}
		/* Avoid freeing blocks if we got a bad deleted inode */
		if (inode && inode->i_nlink == 0)
			inode->i_blocks = 0;
		iput(inode);
678
		inode = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
679
	}
680 681
out:
	brelse(bitmap_bh);
Linus Torvalds's avatar
Linus Torvalds committed
682 683 684 685 686
	return inode;
}

unsigned long ext3_count_free_inodes (struct super_block * sb)
{
687 688 689
	unsigned long desc_count;
	struct ext3_group_desc *gdp;
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
690
#ifdef EXT3FS_DEBUG
691
	struct ext3_super_block *es;
692
	unsigned long bitmap_count, x;
693
	struct buffer_head *bitmap_bh = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
694 695

	lock_super (sb);
696
	es = EXT3_SB(sb)->s_es;
Linus Torvalds's avatar
Linus Torvalds committed
697 698 699
	desc_count = 0;
	bitmap_count = 0;
	gdp = NULL;
700
	for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
701 702 703 704
		gdp = ext3_get_group_desc (sb, i, NULL);
		if (!gdp)
			continue;
		desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
705 706 707
		brelse(bitmap_bh);
		bitmap_bh = read_inode_bitmap(sb, i);
		if (!bitmap_bh)
Linus Torvalds's avatar
Linus Torvalds committed
708 709
			continue;

710 711
		x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
		printk("group %d: stored = %d, counted = %lu\n",
Linus Torvalds's avatar
Linus Torvalds committed
712 713 714
			i, le16_to_cpu(gdp->bg_free_inodes_count), x);
		bitmap_count += x;
	}
715
	brelse(bitmap_bh);
716
	printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
Linus Torvalds's avatar
Linus Torvalds committed
717
		le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
718
	unlock_super(sb);
Linus Torvalds's avatar
Linus Torvalds committed
719 720
	return desc_count;
#else
721 722 723 724 725 726 727 728
	desc_count = 0;
	for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
		gdp = ext3_get_group_desc (sb, i, NULL);
		if (!gdp)
			continue;
		desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
	}
	return desc_count;
Linus Torvalds's avatar
Linus Torvalds committed
729 730 731
#endif
}

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
/* Called at mount-time, super-block is locked */
unsigned long ext3_count_dirs (struct super_block * sb)
{
	unsigned long count = 0;
	int i;

	for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
		struct ext3_group_desc *gdp = ext3_get_group_desc (sb, i, NULL);
		if (!gdp)
			continue;
		count += le16_to_cpu(gdp->bg_used_dirs_count);
	}
	return count;
}

Linus Torvalds's avatar
Linus Torvalds committed
747 748 749 750 751 752
#ifdef CONFIG_EXT3_CHECK
/* Called at mount-time, super-block is locked */
void ext3_check_inodes_bitmap (struct super_block * sb)
{
	struct ext3_super_block * es;
	unsigned long desc_count, bitmap_count, x;
753
	struct buffer_head *bitmap_bh = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756
	struct ext3_group_desc * gdp;
	int i;

757
	es = EXT3_SB(sb)->s_es;
Linus Torvalds's avatar
Linus Torvalds committed
758 759 760
	desc_count = 0;
	bitmap_count = 0;
	gdp = NULL;
761
	for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
762 763 764 765
		gdp = ext3_get_group_desc (sb, i, NULL);
		if (!gdp)
			continue;
		desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
766 767 768
		brelse(bitmap_bh);
		bitmap_bh = read_inode_bitmap(sb, i);
		if (!bitmap_bh)
Linus Torvalds's avatar
Linus Torvalds committed
769 770
			continue;

771
		x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
Linus Torvalds's avatar
Linus Torvalds committed
772 773 774 775 776 777 778
		if (le16_to_cpu(gdp->bg_free_inodes_count) != x)
			ext3_error (sb, "ext3_check_inodes_bitmap",
				    "Wrong free inodes count in group %d, "
				    "stored = %d, counted = %lu", i,
				    le16_to_cpu(gdp->bg_free_inodes_count), x);
		bitmap_count += x;
	}
779
	brelse(bitmap_bh);
Linus Torvalds's avatar
Linus Torvalds committed
780 781 782 783 784 785 786 787
	if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
		ext3_error (sb, "ext3_check_inodes_bitmap",
			    "Wrong free inodes count in super block, "
			    "stored = %lu, counted = %lu",
			    (unsigned long)le32_to_cpu(es->s_free_inodes_count),
			    bitmap_count);
}
#endif