super.c 56.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  linux/fs/ext3/super.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)
 *
 *  from
 *
 *  linux/fs/minix/inode.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  Big-endian to little-endian byte-swapping/bitmaps by
 *        David S. Miller (davem@caip.rutgers.edu), 1995
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
23
#include <linux/time.h>
Linus Torvalds's avatar
Linus Torvalds committed
24 25 26 27 28 29
#include <linux/jbd.h>
#include <linux/ext3_fs.h>
#include <linux/ext3_jbd.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/blkdev.h>
30
#include <linux/parser.h>
Linus Torvalds's avatar
Linus Torvalds committed
31
#include <linux/smp_lock.h>
32
#include <linux/buffer_head.h>
33
#include <linux/vfs.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
#include <asm/uaccess.h>
35
#include "xattr.h"
36
#include "acl.h"
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39 40 41 42 43 44 45 46 47

static int ext3_load_journal(struct super_block *, struct ext3_super_block *);
static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
			       int);
static void ext3_commit_super (struct super_block * sb,
			       struct ext3_super_block * es,
			       int sync);
static void ext3_mark_recovery_complete(struct super_block * sb,
					struct ext3_super_block * es);
static void ext3_clear_journal_err(struct super_block * sb,
				   struct ext3_super_block * es);
48
static int ext3_sync_fs(struct super_block *sb, int wait);
Linus Torvalds's avatar
Linus Torvalds committed
49

50 51 52 53 54 55 56 57 58 59 60
/* 
 * Wrappers for journal_start/end.
 *
 * The only special thing we need to do here is to make sure that all
 * journal_end calls result in the superblock being marked dirty, so
 * that sync() will call the filesystem's write_super callback if
 * appropriate. 
 */
handle_t *ext3_journal_start(struct inode *inode, int nblocks)
{
	journal_t *journal;
61

62 63 64 65 66 67 68 69 70 71 72 73
	if (inode->i_sb->s_flags & MS_RDONLY)
		return ERR_PTR(-EROFS);

	/* Special case here: if the journal has aborted behind our
	 * backs (eg. EIO in the commit thread), then we still need to
	 * take the FS itself readonly cleanly. */
	journal = EXT3_JOURNAL(inode);
	if (is_journal_aborted(journal)) {
		ext3_abort(inode->i_sb, __FUNCTION__,
			   "Detected aborted journal");
		return ERR_PTR(-EROFS);
	}
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	return journal_start(journal, nblocks);
}

/* 
 * The only special thing we need to do here is to make sure that all
 * journal_stop calls result in the superblock being marked dirty, so
 * that sync() will call the filesystem's write_super callback if
 * appropriate. 
 */
int __ext3_journal_stop(const char *where, handle_t *handle)
{
	struct super_block *sb;
	int err;
	int rc;

	sb = handle->h_transaction->t_journal->j_private;
	err = handle->h_err;
	rc = journal_stop(handle);

	if (!err)
		err = rc;
	if (err)
		__ext3_std_error(sb, where, err);
	return err;
}

void ext3_journal_abort_handle(const char *caller, const char *err_fn,
		struct buffer_head *bh, handle_t *handle, int err)
{
	char nbuf[16];
	const char *errstr = ext3_decode_error(NULL, err, nbuf);
106

107 108 109 110 111 112 113 114 115
	printk(KERN_ERR "%s: aborting transaction: %s in %s", 
	       caller, errstr, err_fn);

	if (bh)
		BUFFER_TRACE(bh, "abort");
	journal_abort_handle(handle);
	if (!handle->h_err)
		handle->h_err = err;
}
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

static char error_buf[1024];

/* Deal with the reporting of failure conditions on a filesystem such as
 * inconsistencies detected or read IO failures.
 *
 * On ext2, we can store the error state of the filesystem in the
 * superblock.  That is not possible on ext3, because we may have other
 * write ordering constraints on the superblock which prevent us from
 * writing it out straight away; and given that the journal is about to
 * be aborted, we can't rely on the current, or future, transactions to
 * write out the superblock safely.
 *
 * We'll just use the journal_abort() error code to record an error in
 * the journal instead.  On recovery, the journal will compain about
 * that error until we've noted it down and cleared it.
 */

static void ext3_handle_error(struct super_block *sb)
{
	struct ext3_super_block *es = EXT3_SB(sb)->s_es;

	EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
	es->s_state |= cpu_to_le32(EXT3_ERROR_FS);

	if (sb->s_flags & MS_RDONLY)
		return;

144
	if (test_opt (sb, ERRORS_PANIC))
Linus Torvalds's avatar
Linus Torvalds committed
145
		panic ("EXT3-fs (device %s): panic forced after error\n",
Linus Torvalds's avatar
Linus Torvalds committed
146
		       sb->s_id);
147
	if (test_opt (sb, ERRORS_RO)) {
Linus Torvalds's avatar
Linus Torvalds committed
148 149
		printk (KERN_CRIT "Remounting filesystem read-only\n");
		sb->s_flags |= MS_RDONLY;
150
	} else {
151 152
		journal_t *journal = EXT3_SB(sb)->s_journal;

153
		EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
154 155
		if (journal)
			journal_abort(journal, -EIO);
Linus Torvalds's avatar
Linus Torvalds committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169
	}
	ext3_commit_super(sb, es, 1);
}

void ext3_error (struct super_block * sb, const char * function,
		 const char * fmt, ...)
{
	va_list args;

	va_start (args, fmt);
	vsprintf (error_buf, fmt, args);
	va_end (args);

	printk (KERN_CRIT "EXT3-fs error (device %s): %s: %s\n",
Linus Torvalds's avatar
Linus Torvalds committed
170
		sb->s_id, function, error_buf);
Linus Torvalds's avatar
Linus Torvalds committed
171 172 173 174 175 176 177

	ext3_handle_error(sb);
}

const char *ext3_decode_error(struct super_block * sb, int errno, char nbuf[16])
{
	char *errstr = NULL;
178

Linus Torvalds's avatar
Linus Torvalds committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	switch (errno) {
	case -EIO:
		errstr = "IO failure";
		break;
	case -ENOMEM:
		errstr = "Out of memory";
		break;
	case -EROFS:
		if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
			errstr = "Journal has aborted";
		else
			errstr = "Readonly filesystem";
		break;
	default:
		/* If the caller passed in an extra buffer for unknown
		 * errors, textualise them now.  Else we just return
		 * NULL. */
		if (nbuf) {
			/* Check for truncated error codes... */
			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
				errstr = nbuf;
		}
		break;
	}

	return errstr;
}

/* __ext3_std_error decodes expected errors from journaling functions
 * automatically and invokes the appropriate error response.  */

void __ext3_std_error (struct super_block * sb, const char * function,
		       int errno)
{
	char nbuf[16];
	const char *errstr = ext3_decode_error(sb, errno, nbuf);

	printk (KERN_CRIT "EXT3-fs error (device %s) in %s: %s\n",
Linus Torvalds's avatar
Linus Torvalds committed
217
		sb->s_id, function, errstr);
218

Linus Torvalds's avatar
Linus Torvalds committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	ext3_handle_error(sb);
}

/*
 * ext3_abort is a much stronger failure handler than ext3_error.  The
 * abort function may be used to deal with unrecoverable failures such
 * as journal IO errors or ENOMEM at a critical moment in log management.
 *
 * We unconditionally force the filesystem into an ABORT|READONLY state,
 * unless the error response on the fs has been set to panic in which
 * case we take the easy way out and panic immediately.
 */

void ext3_abort (struct super_block * sb, const char * function,
		 const char * fmt, ...)
{
	va_list args;

	printk (KERN_CRIT "ext3_abort called.\n");

	va_start (args, fmt);
	vsprintf (error_buf, fmt, args);
	va_end (args);

243
	if (test_opt (sb, ERRORS_PANIC))
Linus Torvalds's avatar
Linus Torvalds committed
244
		panic ("EXT3-fs panic (device %s): %s: %s\n",
Linus Torvalds's avatar
Linus Torvalds committed
245
		       sb->s_id, function, error_buf);
Linus Torvalds's avatar
Linus Torvalds committed
246 247

	printk (KERN_CRIT "EXT3-fs abort (device %s): %s: %s\n",
Linus Torvalds's avatar
Linus Torvalds committed
248
		sb->s_id, function, error_buf);
Linus Torvalds's avatar
Linus Torvalds committed
249 250 251

	if (sb->s_flags & MS_RDONLY)
		return;
252

Linus Torvalds's avatar
Linus Torvalds committed
253
	printk (KERN_CRIT "Remounting filesystem read-only\n");
254
	EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
Linus Torvalds's avatar
Linus Torvalds committed
255
	sb->s_flags |= MS_RDONLY;
256
	EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	journal_abort(EXT3_SB(sb)->s_journal, -EIO);
}

/* Deal with the reporting of failure conditions while running, such as
 * inconsistencies in operation or invalid system states.
 *
 * Use ext3_error() for cases of invalid filesystem states, as that will
 * record an error on disk and force a filesystem check on the next boot.
 */
NORET_TYPE void ext3_panic (struct super_block * sb, const char * function,
			    const char * fmt, ...)
{
	va_list args;

	va_start (args, fmt);
	vsprintf (error_buf, fmt, args);
	va_end (args);

	/* this is to prevent panic from syncing this filesystem */
	/* AKPM: is this sufficient? */
	sb->s_flags |= MS_RDONLY;
	panic ("EXT3-fs panic (device %s): %s: %s\n",
Linus Torvalds's avatar
Linus Torvalds committed
279
	       sb->s_id, function, error_buf);
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282 283 284 285 286 287 288 289 290
}

void ext3_warning (struct super_block * sb, const char * function,
		   const char * fmt, ...)
{
	va_list args;

	va_start (args, fmt);
	vsprintf (error_buf, fmt, args);
	va_end (args);
	printk (KERN_WARNING "EXT3-fs warning (device %s): %s: %s\n",
Linus Torvalds's avatar
Linus Torvalds committed
291
		sb->s_id, function, error_buf);
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
}

void ext3_update_dynamic_rev(struct super_block *sb)
{
	struct ext3_super_block *es = EXT3_SB(sb)->s_es;

	if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
		return;

	ext3_warning(sb, __FUNCTION__,
		     "updating to rev %d because of new feature flag, "
		     "running e2fsck is recommended",
		     EXT3_DYNAMIC_REV);

	es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
	es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
	es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
	/* leave es->s_feature_*compat flags alone */
	/* es->s_uuid will be set by e2fsck if empty */

	/*
	 * The rest of the superblock fields should be zero, and if not it
	 * means they are likely already in use, so leave them alone.  We
	 * can leave it up to e2fsck to clean up any inconsistencies there.
	 */
}

/*
 * Open the external journal device
 */
Alexander Viro's avatar
Alexander Viro committed
322
static struct block_device *ext3_blkdev_get(dev_t dev)
Linus Torvalds's avatar
Linus Torvalds committed
323 324
{
	struct block_device *bdev;
325
	char b[BDEVNAME_SIZE];
Linus Torvalds's avatar
Linus Torvalds committed
326

Alexander Viro's avatar
Alexander Viro committed
327 328
	bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE, BDEV_FS);
	if (IS_ERR(bdev))
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332
		goto fail;
	return bdev;

fail:
Alexander Viro's avatar
Alexander Viro committed
333 334
	printk(KERN_ERR "EXT3: failed to open journal device %s: %ld\n",
			__bdevname(dev, b), PTR_ERR(bdev));
Linus Torvalds's avatar
Linus Torvalds committed
335 336 337 338 339 340 341 342
	return NULL;
}

/*
 * Release the journal device
 */
static int ext3_blkdev_put(struct block_device *bdev)
{
343
	bd_release(bdev);
Linus Torvalds's avatar
Linus Torvalds committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	return blkdev_put(bdev, BDEV_FS);
}

static int ext3_blkdev_remove(struct ext3_sb_info *sbi)
{
	struct block_device *bdev;
	int ret = -ENODEV;

	bdev = sbi->journal_bdev;
	if (bdev) {
		ret = ext3_blkdev_put(bdev);
		sbi->journal_bdev = 0;
	}
	return ret;
}

Linus Torvalds's avatar
Linus Torvalds committed
360 361 362 363
static inline struct inode *orphan_list_entry(struct list_head *l)
{
	return &list_entry(l, struct ext3_inode_info, i_orphan)->vfs_inode;
}
Linus Torvalds's avatar
Linus Torvalds committed
364 365 366 367

static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
{
	struct list_head *l;
368

Linus Torvalds's avatar
Linus Torvalds committed
369 370
	printk(KERN_ERR "sb orphan head is %d\n", 
	       le32_to_cpu(sbi->s_es->s_last_orphan));
371

Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375
	printk(KERN_ERR "sb_info orphan list:\n");
	list_for_each(l, &sbi->s_orphan) {
		struct inode *inode = orphan_list_entry(l);
		printk(KERN_ERR "  "
Linus Torvalds's avatar
Linus Torvalds committed
376 377
		       "inode %s:%ld at %p: mode %o, nlink %d, next %d\n",
		       inode->i_sb->s_id, inode->i_ino, inode,
Linus Torvalds's avatar
Linus Torvalds committed
378 379 380 381 382 383 384 385 386 387 388
		       inode->i_mode, inode->i_nlink, 
		       le32_to_cpu(NEXT_ORPHAN(inode)));
	}
}

void ext3_put_super (struct super_block * sb)
{
	struct ext3_sb_info *sbi = EXT3_SB(sb);
	struct ext3_super_block *es = sbi->s_es;
	int i;

389
	ext3_xattr_put_super(sb);
Linus Torvalds's avatar
Linus Torvalds committed
390 391 392 393 394 395 396 397 398 399 400 401
	journal_destroy(sbi->s_journal);
	if (!(sb->s_flags & MS_RDONLY)) {
		EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
		es->s_state = le16_to_cpu(sbi->s_mount_state);
		BUFFER_TRACE(sbi->s_sbh, "marking dirty");
		mark_buffer_dirty(sbi->s_sbh);
		ext3_commit_super(sb, es, 1);
	}

	for (i = 0; i < sbi->s_gdb_count; i++)
		brelse(sbi->s_group_desc[i]);
	kfree(sbi->s_group_desc);
402
	kfree(sbi->s_debts);
Linus Torvalds's avatar
Linus Torvalds committed
403 404 405 406 407 408 409 410 411 412
	brelse(sbi->s_sbh);

	/* Debugging code just in case the in-memory inode orphan list
	 * isn't empty.  The on-disk one can be non-empty if we've
	 * detected an error and taken the fs readonly, but the
	 * in-memory list had better be clean by this point. */
	if (!list_empty(&sbi->s_orphan))
		dump_orphan_list(sb, sbi);
	J_ASSERT(list_empty(&sbi->s_orphan));

Linus Torvalds's avatar
Linus Torvalds committed
413
	invalidate_bdev(sb->s_bdev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
414
	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
Linus Torvalds's avatar
Linus Torvalds committed
415 416 417 418 419
		/*
		 * Invalidate the journal device's buffers.  We don't want them
		 * floating about in memory - the physical journal device may
		 * hotswapped, and it breaks the `ro-after' testing code.
		 */
420
		sync_blockdev(sbi->journal_bdev);
Linus Torvalds's avatar
Linus Torvalds committed
421
		invalidate_bdev(sbi->journal_bdev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
422 423
		ext3_blkdev_remove(sbi);
	}
424
	sb->s_fs_info = NULL;
425
	kfree(sbi);
Linus Torvalds's avatar
Linus Torvalds committed
426 427 428
	return;
}

Andrew Morton's avatar
Andrew Morton committed
429
static kmem_cache_t *ext3_inode_cachep;
Linus Torvalds's avatar
Linus Torvalds committed
430

Andrew Morton's avatar
Andrew Morton committed
431 432 433
/*
 * Called inside transaction, so use GFP_NOFS
 */
Linus Torvalds's avatar
Linus Torvalds committed
434 435 436
static struct inode *ext3_alloc_inode(struct super_block *sb)
{
	struct ext3_inode_info *ei;
Andrew Morton's avatar
Andrew Morton committed
437 438

	ei = kmem_cache_alloc(ext3_inode_cachep, SLAB_NOFS);
Linus Torvalds's avatar
Linus Torvalds committed
439 440
	if (!ei)
		return NULL;
441 442 443 444
#ifdef CONFIG_EXT3_FS_POSIX_ACL
	ei->i_acl = EXT3_ACL_NOT_CACHED;
	ei->i_default_acl = EXT3_ACL_NOT_CACHED;
#endif
445
	ei->vfs_inode.i_version = 1;
Linus Torvalds's avatar
Linus Torvalds committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
	return &ei->vfs_inode;
}

static void ext3_destroy_inode(struct inode *inode)
{
	kmem_cache_free(ext3_inode_cachep, EXT3_I(inode));
}

static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
{
	struct ext3_inode_info *ei = (struct ext3_inode_info *) foo;

	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
	    SLAB_CTOR_CONSTRUCTOR) {
		INIT_LIST_HEAD(&ei->i_orphan);
461 462 463
#ifdef CONFIG_EXT3_FS_XATTR
		init_rwsem(&ei->xattr_sem);
#endif
464
		init_MUTEX(&ei->truncate_sem);
Linus Torvalds's avatar
Linus Torvalds committed
465 466 467 468 469 470 471 472
		inode_init_once(&ei->vfs_inode);
	}
}
 
static int init_inodecache(void)
{
	ext3_inode_cachep = kmem_cache_create("ext3_inode_cache",
					     sizeof(struct ext3_inode_info),
473
					     0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
Linus Torvalds's avatar
Linus Torvalds committed
474 475 476 477 478 479 480 481 482 483 484 485
					     init_once, NULL);
	if (ext3_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
	if (kmem_cache_destroy(ext3_inode_cachep))
		printk(KERN_INFO "ext3_inode_cache: not all structures were freed\n");
}

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
#ifdef CONFIG_EXT3_FS_POSIX_ACL

static void ext3_clear_inode(struct inode *inode)
{
       if (EXT3_I(inode)->i_acl &&
           EXT3_I(inode)->i_acl != EXT3_ACL_NOT_CACHED) {
               posix_acl_release(EXT3_I(inode)->i_acl);
               EXT3_I(inode)->i_acl = EXT3_ACL_NOT_CACHED;
       }
       if (EXT3_I(inode)->i_default_acl &&
           EXT3_I(inode)->i_default_acl != EXT3_ACL_NOT_CACHED) {
               posix_acl_release(EXT3_I(inode)->i_default_acl);
               EXT3_I(inode)->i_default_acl = EXT3_ACL_NOT_CACHED;
       }
}

#else
# define ext3_clear_inode NULL
#endif

506 507
static struct dquot_operations ext3_qops;

Linus Torvalds's avatar
Linus Torvalds committed
508
static struct super_operations ext3_sops = {
509 510
	.alloc_inode	= ext3_alloc_inode,
	.destroy_inode	= ext3_destroy_inode,
511 512 513 514 515 516 517 518 519 520 521 522 523
	.read_inode	= ext3_read_inode,
	.write_inode	= ext3_write_inode,
	.dirty_inode	= ext3_dirty_inode,
	.put_inode	= ext3_put_inode,
	.delete_inode	= ext3_delete_inode,
	.put_super	= ext3_put_super,
	.write_super	= ext3_write_super,
	.sync_fs	= ext3_sync_fs,
	.write_super_lockfs = ext3_write_super_lockfs,
	.unlockfs	= ext3_unlockfs,
	.statfs		= ext3_statfs,
	.remount_fs	= ext3_remount,
	.clear_inode	= ext3_clear_inode,
Linus Torvalds's avatar
Linus Torvalds committed
524 525
};

526 527
struct dentry *ext3_get_parent(struct dentry *child);
static struct export_operations ext3_export_ops = {
528
	.get_parent = ext3_get_parent,
529 530
};

531 532 533 534 535 536 537 538 539
enum {
	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
	Opt_nouid32, Opt_check, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl, Opt_noload,
	Opt_commit, Opt_journal_update, Opt_journal_inum,
	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
	Opt_ignore, Opt_err,
};
540

541 542 543 544 545 546 547
static match_table_t tokens = {
	{Opt_bsd_df, "bsddf"},
	{Opt_minix_df, "minixdf"},
	{Opt_grpid, "grpid"},
	{Opt_grpid, "bsdgroups"},
	{Opt_nogrpid, "nogrpid"},
	{Opt_nogrpid, "sysvgroups"},
548 549 550
	{Opt_resgid, "resgid=%u"},
	{Opt_resuid, "resuid=%u"},
	{Opt_sb, "sb=%u"},
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
	{Opt_err_cont, "errors=continue"},
	{Opt_err_panic, "errors=panic"},
	{Opt_err_ro, "errors=remount-ro"},
	{Opt_nouid32, "nouid32"},
	{Opt_nocheck, "nocheck"},
	{Opt_nocheck, "check=none"},
	{Opt_check, "check"},
	{Opt_debug, "debug"},
	{Opt_oldalloc, "oldalloc"},
	{Opt_orlov, "orlov"},
	{Opt_user_xattr, "user_xattr"},
	{Opt_nouser_xattr, "nouser_xattr"},
	{Opt_acl, "acl"},
	{Opt_noacl, "noacl"},
	{Opt_noload, "noload"},
	{Opt_commit, "commit=%u"},
	{Opt_journal_update, "journal=update"},
	{Opt_journal_inum, "journal=%u"},
	{Opt_abort, "abort"},
	{Opt_data_journal, "data=journal"},
	{Opt_data_ordered, "data=ordered"},
	{Opt_data_writeback, "data=writeback"},
	{Opt_ignore, "grpquota"},
	{Opt_ignore, "noquota"},
	{Opt_ignore, "quota"},
	{Opt_ignore, "usrquota"},
	{Opt_err, NULL}
};
Linus Torvalds's avatar
Linus Torvalds committed
579

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
static unsigned long get_sb_block(void **data)
{
	unsigned long 	sb_block;
	char 		*options = (char *) *data;

	if (!options || strncmp(options, "sb=", 3) != 0)
		return 1;	/* Default location */
	options += 3;
	sb_block = simple_strtoul(options, &options, 0);
	if (*options && *options != ',') {
		printk("EXT3-fs: Invalid sb specification: %s\n",
		       (char *) *data);
		return 1;
	}
	if (*options == ',')
		options++;
	*data = (void *) options;
	return sb_block;
}

static int parse_options (char * options, struct ext3_sb_info *sbi,
			  unsigned long * inum, int is_remount)
Linus Torvalds's avatar
Linus Torvalds committed
602
{
603 604 605 606
	char * p;
	substring_t args[MAX_OPT_ARGS];
	int data_opt = 0;
	int option;
Linus Torvalds's avatar
Linus Torvalds committed
607 608 609

	if (!options)
		return 1;
610 611 612 613

	while ((p = strsep (&options, ",")) != NULL) {
		int token;
		if (!*p)
Linus Torvalds's avatar
Linus Torvalds committed
614
			continue;
615 616 617 618

		token = match_token(p, tokens, args);
		switch (token) {
		case Opt_bsd_df:
619
			clear_opt (sbi->s_mount_opt, MINIX_DF);
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
			break;
		case Opt_minix_df:
			set_opt (sbi->s_mount_opt, MINIX_DF);
			break;
		case Opt_grpid:
			set_opt (sbi->s_mount_opt, GRPID);
			break;
		case Opt_nogrpid:
			clear_opt (sbi->s_mount_opt, GRPID);
			break;
		case Opt_resuid:
			if (match_int(&args[0], &option))
				return 0;
			sbi->s_resuid = option;
			break;
		case Opt_resgid:
			if (match_int(&args[0], &option))
				return 0;
			sbi->s_resgid = option;
			break;
		case Opt_sb:
			/* handled by get_sb_block() instead of here */
			/* *sb_block = match_int(&args[0]); */
			break;
		case Opt_err_panic:
			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
			clear_opt (sbi->s_mount_opt, ERRORS_RO);
			set_opt (sbi->s_mount_opt, ERRORS_PANIC);
			break;
		case Opt_err_ro:
			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
			set_opt (sbi->s_mount_opt, ERRORS_RO);
			break;
		case Opt_err_cont:
			clear_opt (sbi->s_mount_opt, ERRORS_RO);
			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
			set_opt (sbi->s_mount_opt, ERRORS_CONT);
			break;
		case Opt_nouid32:
660
			set_opt (sbi->s_mount_opt, NO_UID32);
661 662
			break;
		case Opt_check:
Linus Torvalds's avatar
Linus Torvalds committed
663
#ifdef CONFIG_EXT3_CHECK
664
			set_opt (sbi->s_mount_opt, CHECK);
Linus Torvalds's avatar
Linus Torvalds committed
665
#else
666 667
			printk(KERN_ERR
			       "EXT3 Check option not supported\n");
Linus Torvalds's avatar
Linus Torvalds committed
668
#endif
669 670
			break;
		case Opt_nocheck:
671
			clear_opt (sbi->s_mount_opt, CHECK);
672 673 674 675 676
			break;
		case Opt_debug:
			set_opt (sbi->s_mount_opt, DEBUG);
			break;
		case Opt_oldalloc:
677
			set_opt (sbi->s_mount_opt, OLDALLOC);
678 679
			break;
		case Opt_orlov:
680
			clear_opt (sbi->s_mount_opt, OLDALLOC);
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
			break;
#ifdef CONFIG_EXT3_FS_XATTR
		case Opt_user_xattr:
			set_opt (sbi->s_mount_opt, XATTR_USER);
			break;
		case Opt_nouser_xattr:
			clear_opt (sbi->s_mount_opt, XATTR_USER);
			break;
#else
		case Opt_user_xattr:
		case Opt_nouser_xattr:
			printk("EXT3 (no)user_xattr options not supported\n");
			break;
#endif
#ifdef CONFIG_EXT3_FS_POSIX_ACL
		case Opt_acl:
			set_opt(sbi->s_mount_opt, POSIX_ACL);
			break;
		case Opt_noacl:
			clear_opt(sbi->s_mount_opt, POSIX_ACL);
			break;
#else
		case Opt_acl:
		case Opt_noacl:
			printk("EXT3 (no)acl options not supported\n");
			break;
#endif
		case Opt_journal_update:
Linus Torvalds's avatar
Linus Torvalds committed
709 710
			/* @@@ FIXME */
			/* Eventually we will want to be able to create
711 712 713
			   a journal file here.  For now, only allow the
			   user to specify an existing inode to be the
			   journal file. */
Linus Torvalds's avatar
Linus Torvalds committed
714 715 716 717 718
			if (is_remount) {
				printk(KERN_ERR "EXT3-fs: cannot specify "
				       "journal on remount\n");
				return 0;
			}
719 720 721 722 723 724
			set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
			break;
		case Opt_journal_inum:
			if (is_remount) {
				printk(KERN_ERR "EXT3-fs: cannot specify "
				       "journal on remount\n");
Linus Torvalds's avatar
Linus Torvalds committed
725
				return 0;
726 727
			}
			if (match_int(&args[0], &option))
Linus Torvalds's avatar
Linus Torvalds committed
728
				return 0;
729 730 731
			*inum = option;
			break;
		case Opt_noload:
732
			set_opt (sbi->s_mount_opt, NOLOAD);
733 734 735
			break;
		case Opt_commit:
			if (match_int(&args[0], &option))
Linus Torvalds's avatar
Linus Torvalds committed
736
				return 0;
737 738 739 740 741 742 743 744 745 746 747
			sbi->s_commit_interval = HZ * option;
			break;
		case Opt_data_journal:
			data_opt = EXT3_MOUNT_JOURNAL_DATA;
			goto datacheck;
		case Opt_data_ordered:
			data_opt = EXT3_MOUNT_ORDERED_DATA;
			goto datacheck;
		case Opt_data_writeback:
			data_opt = EXT3_MOUNT_WRITEBACK_DATA;
		datacheck:
Linus Torvalds's avatar
Linus Torvalds committed
748
			if (is_remount) {
749 750
				if ((sbi->s_mount_opt & EXT3_MOUNT_DATA_FLAGS)
						!= data_opt) {
Linus Torvalds's avatar
Linus Torvalds committed
751
					printk(KERN_ERR
752 753
						"EXT3-fs: cannot change data "
						"mode on remount\n");
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756
					return 0;
				}
			} else {
757 758
				sbi->s_mount_opt &= ~EXT3_MOUNT_DATA_FLAGS;
				sbi->s_mount_opt |= data_opt;
Linus Torvalds's avatar
Linus Torvalds committed
759
			}
760 761 762 763 764 765 766 767 768 769
			break;
		case Opt_abort:
			set_opt(sbi->s_mount_opt, ABORT);
			break;
		case Opt_ignore:
			break;
		default:
			printk (KERN_ERR
				"EXT3-fs: Unrecognized mount option \"%s\" "
				"or missing value\n", p);
Linus Torvalds's avatar
Linus Torvalds committed
770 771 772
			return 0;
		}
	}
773

Linus Torvalds's avatar
Linus Torvalds committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	return 1;
}

static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
			    int read_only)
{
	struct ext3_sb_info *sbi = EXT3_SB(sb);
	int res = 0;

	if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
		printk (KERN_ERR "EXT3-fs warning: revision level too high, "
			"forcing read-only mode\n");
		res = MS_RDONLY;
	}
	if (read_only)
		return res;
	if (!(sbi->s_mount_state & EXT3_VALID_FS))
		printk (KERN_WARNING "EXT3-fs warning: mounting unchecked fs, "
			"running e2fsck is recommended\n");
	else if ((sbi->s_mount_state & EXT3_ERROR_FS))
		printk (KERN_WARNING
			"EXT3-fs warning: mounting fs with errors, "
			"running e2fsck is recommended\n");
	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
		 le16_to_cpu(es->s_mnt_count) >=
		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
		printk (KERN_WARNING
			"EXT3-fs warning: maximal mount count reached, "
			"running e2fsck is recommended\n");
	else if (le32_to_cpu(es->s_checkinterval) &&
		(le32_to_cpu(es->s_lastcheck) +
805
			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Linus Torvalds's avatar
Linus Torvalds committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819
		printk (KERN_WARNING
			"EXT3-fs warning: checktime reached, "
			"running e2fsck is recommended\n");
#if 0
		/* @@@ We _will_ want to clear the valid bit if we find
                   inconsistencies, to force a fsck at reboot.  But for
                   a plain journaled filesystem we can keep it set as
                   valid forever! :) */
	es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT3_VALID_FS);
#endif
	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
		es->s_max_mnt_count =
			(__s16) cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
	es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
820
	es->s_mtime = cpu_to_le32(get_seconds());
Linus Torvalds's avatar
Linus Torvalds committed
821 822
	ext3_update_dynamic_rev(sb);
	EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
823

824 825 826 827 828
	ext3_commit_super(sb, es, 1);
	if (test_opt(sb, DEBUG))
		printk(KERN_INFO "[EXT3 FS bs=%lu, gc=%lu, "
				"bpg=%lu, ipg=%lu, mo=%04lx]\n",
			sb->s_blocksize,
Linus Torvalds's avatar
Linus Torvalds committed
829 830 831 832
			sbi->s_groups_count,
			EXT3_BLOCKS_PER_GROUP(sb),
			EXT3_INODES_PER_GROUP(sb),
			sbi->s_mount_opt);
833

834
	printk(KERN_INFO "EXT3 FS on %s, ", sb->s_id);
Linus Torvalds's avatar
Linus Torvalds committed
835
	if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
836 837
		char b[BDEVNAME_SIZE];

Linus Torvalds's avatar
Linus Torvalds committed
838
		printk("external journal on %s\n",
839
			bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
Linus Torvalds's avatar
Linus Torvalds committed
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
	} else {
		printk("internal journal\n");
	}
#ifdef CONFIG_EXT3_CHECK
	if (test_opt (sb, CHECK)) {
		ext3_check_blocks_bitmap (sb);
		ext3_check_inodes_bitmap (sb);
	}
#endif
	return res;
}

static int ext3_check_descriptors (struct super_block * sb)
{
	struct ext3_sb_info *sbi = EXT3_SB(sb);
	unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
	struct ext3_group_desc * gdp = NULL;
	int desc_block = 0;
	int i;

	ext3_debug ("Checking group descriptors");

	for (i = 0; i < sbi->s_groups_count; i++)
	{
		if ((i % EXT3_DESC_PER_BLOCK(sb)) == 0)
			gdp = (struct ext3_group_desc *)
					sbi->s_group_desc[desc_block++]->b_data;
		if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
		    le32_to_cpu(gdp->bg_block_bitmap) >=
				block + EXT3_BLOCKS_PER_GROUP(sb))
		{
			ext3_error (sb, "ext3_check_descriptors",
				    "Block bitmap for group %d"
				    " not in group (block %lu)!",
				    i, (unsigned long)
					le32_to_cpu(gdp->bg_block_bitmap));
			return 0;
		}
		if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
		    le32_to_cpu(gdp->bg_inode_bitmap) >=
				block + EXT3_BLOCKS_PER_GROUP(sb))
		{
			ext3_error (sb, "ext3_check_descriptors",
				    "Inode bitmap for group %d"
				    " not in group (block %lu)!",
				    i, (unsigned long)
					le32_to_cpu(gdp->bg_inode_bitmap));
			return 0;
		}
		if (le32_to_cpu(gdp->bg_inode_table) < block ||
		    le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
		    block + EXT3_BLOCKS_PER_GROUP(sb))
		{
			ext3_error (sb, "ext3_check_descriptors",
				    "Inode table for group %d"
				    " not in group (block %lu)!",
				    i, (unsigned long)
					le32_to_cpu(gdp->bg_inode_table));
			return 0;
		}
		block += EXT3_BLOCKS_PER_GROUP(sb);
		gdp++;
	}
903

904 905
	sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
	sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
Linus Torvalds's avatar
Linus Torvalds committed
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
	return 1;
}


/* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
 * the superblock) which were deleted from all directories, but held open by
 * a process at the time of a crash.  We walk the list and try to delete these
 * inodes at recovery time (only with a read-write filesystem).
 *
 * In order to keep the orphan inode chain consistent during traversal (in
 * case of crash during recovery), we link each inode into the superblock
 * orphan list_head and handle it the same way as an inode deletion during
 * normal operation (which journals the operations for us).
 *
 * We only do an iget() and an iput() on each inode, which is very safe if we
 * accidentally point at an in-use or already deleted inode.  The worst that
 * can happen in this case is that we get a "bit already cleared" message from
 * ext3_free_inode().  The only reason we would point at a wrong inode is if
 * e2fsck was run on this filesystem, and it must have already done the orphan
 * inode cleanup for us, so we can safely abort without any further action.
 */
static void ext3_orphan_cleanup (struct super_block * sb,
				 struct ext3_super_block * es)
{
	unsigned int s_flags = sb->s_flags;
	int nr_orphans = 0, nr_truncates = 0;
	if (!es->s_last_orphan) {
		jbd_debug(4, "no orphan inodes to clean up\n");
		return;
	}

937
	if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
Linus Torvalds's avatar
Linus Torvalds committed
938 939 940 941 942 943 944 945
		if (es->s_last_orphan)
			jbd_debug(1, "Errors on filesystem, "
				  "clearing orphan list.\n");
		es->s_last_orphan = 0;
		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
		return;
	}

946 947 948 949 950 951
	if (s_flags & MS_RDONLY) {
		printk(KERN_INFO "EXT3-fs: %s: orphan cleanup on readonly fs\n",
		       sb->s_id);
		sb->s_flags &= ~MS_RDONLY;
	}

Linus Torvalds's avatar
Linus Torvalds committed
952 953 954 955 956 957 958 959 960 961 962
	while (es->s_last_orphan) {
		struct inode *inode;

		if (!(inode =
		      ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
			es->s_last_orphan = 0;
			break;
		}

		list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
		if (inode->i_nlink) {
963 964 965
			printk(KERN_DEBUG
				"%s: truncating inode %ld to %Ld bytes\n",
				__FUNCTION__, inode->i_ino, inode->i_size);
Linus Torvalds's avatar
Linus Torvalds committed
966 967 968 969 970
			jbd_debug(2, "truncating inode %ld to %Ld bytes\n",
				  inode->i_ino, inode->i_size);
			ext3_truncate(inode);
			nr_truncates++;
		} else {
971 972 973
			printk(KERN_DEBUG
				"%s: deleting unreferenced inode %ld\n",
				__FUNCTION__, inode->i_ino);
Linus Torvalds's avatar
Linus Torvalds committed
974 975 976 977 978 979 980 981 982 983 984
			jbd_debug(2, "deleting unreferenced inode %ld\n",
				  inode->i_ino);
			nr_orphans++;
		}
		iput(inode);  /* The delete magic happens here! */
	}

#define PLURAL(x) (x), ((x)==1) ? "" : "s"

	if (nr_orphans)
		printk(KERN_INFO "EXT3-fs: %s: %d orphan inode%s deleted\n",
Linus Torvalds's avatar
Linus Torvalds committed
985
		       sb->s_id, PLURAL(nr_orphans));
Linus Torvalds's avatar
Linus Torvalds committed
986 987
	if (nr_truncates)
		printk(KERN_INFO "EXT3-fs: %s: %d truncate%s cleaned up\n",
Linus Torvalds's avatar
Linus Torvalds committed
988
		       sb->s_id, PLURAL(nr_truncates));
Linus Torvalds's avatar
Linus Torvalds committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
}

#define log2(n) ffz(~(n))

/*
 * Maximal file size.  There is a direct, and {,double-,triple-}indirect
 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
 * We need to be 1 filesystem block less than the 2^32 sector limit.
 */
static loff_t ext3_max_size(int bits)
{
	loff_t res = EXT3_NDIR_BLOCKS;
	res += 1LL << (bits-2);
	res += 1LL << (2*(bits-2));
	res += 1LL << (3*(bits-2));
	res <<= bits;
	if (res > (512LL << 32) - (1 << bits))
		res = (512LL << 32) - (1 << bits);
	return res;
}

1011 1012 1013 1014 1015 1016
static unsigned long descriptor_loc(struct super_block *sb,
				    unsigned long logic_sb_block,
				    int nr)
{
	struct ext3_sb_info *sbi = EXT3_SB(sb);
	unsigned long bg, first_data_block, first_meta_bg;
1017
	int has_super = 0;
1018

1019 1020 1021 1022 1023 1024 1025
	first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);

	if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
	    nr < first_meta_bg)
		return (logic_sb_block + nr + 1);
	bg = sbi->s_desc_per_block * nr;
1026 1027 1028
	if (ext3_bg_has_super(sb, bg))
		has_super = 1;
	return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
1029 1030
}

1031

1032
static int ext3_fill_super (struct super_block *sb, void *data, int silent)
Linus Torvalds's avatar
Linus Torvalds committed
1033 1034 1035
{
	struct buffer_head * bh;
	struct ext3_super_block *es = 0;
1036
	struct ext3_sb_info *sbi;
1037
	unsigned long sb_block = get_sb_block(&data);
1038
	unsigned long block, logic_sb_block = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1039 1040
	unsigned long offset = 0;
	unsigned long journal_inum = 0;
1041
	unsigned long def_mount_opts;
Linus Torvalds's avatar
Linus Torvalds committed
1042 1043 1044 1045 1046 1047
	int blocksize;
	int hblock;
	int db_count;
	int i;
	int needs_recovery;

1048 1049 1050
	sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
	if (!sbi)
		return -ENOMEM;
1051
	sb->s_fs_info = sbi;
1052
	memset(sbi, 0, sizeof(*sbi));
Linus Torvalds's avatar
Linus Torvalds committed
1053 1054 1055 1056
	sbi->s_mount_opt = 0;
	sbi->s_resuid = EXT3_DEF_RESUID;
	sbi->s_resgid = EXT3_DEF_RESGID;

Linus Torvalds's avatar
Linus Torvalds committed
1057
	blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
Andrew Morton's avatar
Andrew Morton committed
1058 1059 1060 1061
	if (!blocksize) {
		printk(KERN_ERR "EXT3-fs: unable to set blocksize\n");
		goto out_fail;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

	/*
	 * The ext3 superblock will not be buffer aligned for other than 1kB
	 * block sizes.  We need to calculate the offset from buffer start.
	 */
	if (blocksize != EXT3_MIN_BLOCK_SIZE) {
		logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
		offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1072
	if (!(bh = sb_bread(sb, logic_sb_block))) {
Linus Torvalds's avatar
Linus Torvalds committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
		printk (KERN_ERR "EXT3-fs: unable to read superblock\n");
		goto out_fail;
	}
	/*
	 * Note: s_es must be initialized as soon as possible because
	 *       some ext3 macro-instructions depend on its value
	 */
	es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
	sbi->s_es = es;
	sb->s_magic = le16_to_cpu(es->s_magic);
	if (sb->s_magic != EXT3_SUPER_MAGIC) {
		if (!silent)
			printk(KERN_ERR 
			       "VFS: Can't find ext3 filesystem on dev %s.\n",
Linus Torvalds's avatar
Linus Torvalds committed
1087
			       sb->s_id);
Linus Torvalds's avatar
Linus Torvalds committed
1088 1089
		goto failed_mount;
	}
1090

1091 1092 1093 1094 1095 1096 1097 1098
	/* Set defaults before we parse the mount options */
	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
	if (def_mount_opts & EXT3_DEFM_DEBUG)
		set_opt(sbi->s_mount_opt, DEBUG);
	if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
		set_opt(sbi->s_mount_opt, GRPID);
	if (def_mount_opts & EXT3_DEFM_UID16)
		set_opt(sbi->s_mount_opt, NO_UID32);
1099 1100
	if (def_mount_opts & EXT3_DEFM_XATTR_USER)
		set_opt(sbi->s_mount_opt, XATTR_USER);
1101 1102
	if (def_mount_opts & EXT3_DEFM_ACL)
		set_opt(sbi->s_mount_opt, POSIX_ACL);
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
		sbi->s_mount_opt |= EXT3_MOUNT_JOURNAL_DATA;
	else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
		sbi->s_mount_opt |= EXT3_MOUNT_ORDERED_DATA;
	else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
		sbi->s_mount_opt |= EXT3_MOUNT_WRITEBACK_DATA;

	if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_RO)
		set_opt(sbi->s_mount_opt, ERRORS_RO);
1114

1115 1116 1117 1118 1119 1120
	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);

	if (!parse_options ((char *) data, sbi, &journal_inum, 0))
		goto failed_mount;

1121
	sb->s_flags |= MS_ONE_SECOND;
1122 1123 1124
	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
		((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);

Linus Torvalds's avatar
Linus Torvalds committed
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
	    (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
	     EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
	     EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
		printk(KERN_WARNING 
		       "EXT3-fs warning: feature flags set on rev 0 fs, "
		       "running e2fsck is recommended\n");
	/*
	 * Check feature flags regardless of the revision level, since we
	 * previously didn't change the revision level when setting the flags,
	 * so there is a chance incompat flags are set on a rev 0 filesystem.
	 */
	if ((i = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))) {
		printk(KERN_ERR "EXT3-fs: %s: couldn't mount because of "
		       "unsupported optional features (%x).\n",
Linus Torvalds's avatar
Linus Torvalds committed
1140
		       sb->s_id, i);
Linus Torvalds's avatar
Linus Torvalds committed
1141 1142 1143 1144 1145 1146
		goto failed_mount;
	}
	if (!(sb->s_flags & MS_RDONLY) &&
	    (i = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))){
		printk(KERN_ERR "EXT3-fs: %s: couldn't mount RDWR because of "
		       "unsupported optional features (%x).\n",
Linus Torvalds's avatar
Linus Torvalds committed
1147
		       sb->s_id, i);
Linus Torvalds's avatar
Linus Torvalds committed
1148 1149
		goto failed_mount;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1150
	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
Linus Torvalds's avatar
Linus Torvalds committed
1151

Linus Torvalds's avatar
Linus Torvalds committed
1152 1153
	if (blocksize < EXT3_MIN_BLOCK_SIZE ||
	    blocksize > EXT3_MAX_BLOCK_SIZE) {
Linus Torvalds's avatar
Linus Torvalds committed
1154 1155
		printk(KERN_ERR 
		       "EXT3-fs: Unsupported filesystem blocksize %d on %s.\n",
Linus Torvalds's avatar
Linus Torvalds committed
1156
		       blocksize, sb->s_id);
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158 1159
		goto failed_mount;
	}

1160
	hblock = bdev_hardsect_size(sb->s_bdev);
Linus Torvalds's avatar
Linus Torvalds committed
1161 1162 1163 1164 1165
	if (sb->s_blocksize != blocksize) {
		/*
		 * Make sure the blocksize for the filesystem is larger
		 * than the hardware sectorsize for the machine.
		 */
Linus Torvalds's avatar
Linus Torvalds committed
1166
		if (blocksize < hblock) {
Linus Torvalds's avatar
Linus Torvalds committed
1167 1168 1169 1170 1171 1172
			printk(KERN_ERR "EXT3-fs: blocksize %d too small for "
			       "device blocksize %d.\n", blocksize, hblock);
			goto failed_mount;
		}

		brelse (bh);
Linus Torvalds's avatar
Linus Torvalds committed
1173
		sb_set_blocksize(sb, blocksize);
Linus Torvalds's avatar
Linus Torvalds committed
1174 1175
		logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
		offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
Linus Torvalds's avatar
Linus Torvalds committed
1176
		bh = sb_bread(sb, logic_sb_block);
Linus Torvalds's avatar
Linus Torvalds committed
1177 1178 1179
		if (!bh) {
			printk(KERN_ERR 
			       "EXT3-fs: Can't read superblock on 2nd try.\n");
1180
			goto failed_mount;
Linus Torvalds's avatar
Linus Torvalds committed
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
		}
		es = (struct ext3_super_block *)(((char *)bh->b_data) + offset);
		sbi->s_es = es;
		if (es->s_magic != le16_to_cpu(EXT3_SUPER_MAGIC)) {
			printk (KERN_ERR 
				"EXT3-fs: Magic mismatch, very weird !\n");
			goto failed_mount;
		}
	}

1191 1192
	sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);

Linus Torvalds's avatar
Linus Torvalds committed
1193 1194 1195 1196 1197 1198
	if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
		sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
		sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
	} else {
		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1199 1200 1201
		if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
		    (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
		    (sbi->s_inode_size > blocksize)) {
Linus Torvalds's avatar
Linus Torvalds committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
			printk (KERN_ERR
				"EXT3-fs: unsupported inode size: %d\n",
				sbi->s_inode_size);
			goto failed_mount;
		}
	}
	sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
				   le32_to_cpu(es->s_log_frag_size);
	if (blocksize != sbi->s_frag_size) {
		printk(KERN_ERR
		       "EXT3-fs: fragsize %lu != blocksize %u (unsupported)\n",
		       sbi->s_frag_size, blocksize);
		goto failed_mount;
	}
	sbi->s_frags_per_block = 1;
	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
	sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
	sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
	sbi->s_itb_per_group = sbi->s_inodes_per_group /sbi->s_inodes_per_block;
	sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
	sbi->s_sbh = bh;
	sbi->s_mount_state = le16_to_cpu(es->s_state);
	sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
	sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
1227 1228 1229
	for (i=0; i < 4; i++)
		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
	sbi->s_def_hash_version = es->s_def_hash_version;
Linus Torvalds's avatar
Linus Torvalds committed
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

	if (sbi->s_blocks_per_group > blocksize * 8) {
		printk (KERN_ERR
			"EXT3-fs: #blocks per group too big: %lu\n",
			sbi->s_blocks_per_group);
		goto failed_mount;
	}
	if (sbi->s_frags_per_group > blocksize * 8) {
		printk (KERN_ERR
			"EXT3-fs: #fragments per group too big: %lu\n",
			sbi->s_frags_per_group);
		goto failed_mount;
	}
	if (sbi->s_inodes_per_group > blocksize * 8) {
		printk (KERN_ERR
			"EXT3-fs: #inodes per group too big: %lu\n",
			sbi->s_inodes_per_group);
		goto failed_mount;
	}

	sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
			       le32_to_cpu(es->s_first_data_block) +
			       EXT3_BLOCKS_PER_GROUP(sb) - 1) /
			      EXT3_BLOCKS_PER_GROUP(sb);
	db_count = (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) /
		   EXT3_DESC_PER_BLOCK(sb);
	sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
				    GFP_KERNEL);
	if (sbi->s_group_desc == NULL) {
		printk (KERN_ERR "EXT3-fs: not enough memory\n");
		goto failed_mount;
	}
1262
	sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(u8),
1263
			GFP_KERNEL);
1264
	if (!sbi->s_debts) {
1265
		printk("EXT3-fs: not enough memory to allocate s_bgi\n");
1266 1267
		goto failed_mount2;
	}
1268 1269 1270 1271 1272 1273 1274
	memset(sbi->s_debts, 0,  sbi->s_groups_count * sizeof(u8));

	percpu_counter_init(&sbi->s_freeblocks_counter);
	percpu_counter_init(&sbi->s_freeinodes_counter);
	percpu_counter_init(&sbi->s_dirs_counter);
	bgl_lock_init(&sbi->s_blockgroup_lock);

Linus Torvalds's avatar
Linus Torvalds committed
1275
	for (i = 0; i < db_count; i++) {
1276 1277
		block = descriptor_loc(sb, logic_sb_block, i);
		sbi->s_group_desc[i] = sb_bread(sb, block);
Linus Torvalds's avatar
Linus Torvalds committed
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
		if (!sbi->s_group_desc[i]) {
			printk (KERN_ERR "EXT3-fs: "
				"can't read group descriptor %d\n", i);
			db_count = i;
			goto failed_mount2;
		}
	}
	if (!ext3_check_descriptors (sb)) {
		printk (KERN_ERR "EXT3-fs: group descriptors corrupted !\n");
		goto failed_mount2;
	}
	sbi->s_gdb_count = db_count;
	/*
	 * set up enough so that it can read an inode
	 */
	sb->s_op = &ext3_sops;
1294
	sb->s_export_op = &ext3_export_ops;
1295
	sb->dq_op = &ext3_qops;
Linus Torvalds's avatar
Linus Torvalds committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */

	sb->s_root = 0;

	needs_recovery = (es->s_last_orphan != 0 ||
			  EXT3_HAS_INCOMPAT_FEATURE(sb,
				    EXT3_FEATURE_INCOMPAT_RECOVER));

	/*
	 * The first inode we look at is the journal inode.  Don't try
	 * root first: it may be modified in the journal!
	 */
	if (!test_opt(sb, NOLOAD) &&
	    EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
		if (ext3_load_journal(sb, es))
			goto failed_mount2;
	} else if (journal_inum) {
		if (ext3_create_journal(sb, es, journal_inum))
			goto failed_mount2;
	} else {
		if (!silent)
			printk (KERN_ERR
				"ext3: No journal on filesystem on %s\n",
Linus Torvalds's avatar
Linus Torvalds committed
1319
				sb->s_id);
Linus Torvalds's avatar
Linus Torvalds committed
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
		goto failed_mount2;
	}

	/* We have now updated the journal if required, so we can
	 * validate the data journaling mode. */
	switch (test_opt(sb, DATA_FLAGS)) {
	case 0:
		/* No mode set, assume a default based on the journal
                   capabilities: ORDERED_DATA if the journal can
                   cope, else JOURNAL_DATA */
		if (journal_check_available_features
		    (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
			set_opt(sbi->s_mount_opt, ORDERED_DATA);
		else
			set_opt(sbi->s_mount_opt, JOURNAL_DATA);
		break;

	case EXT3_MOUNT_ORDERED_DATA:
	case EXT3_MOUNT_WRITEBACK_DATA:
		if (!journal_check_available_features
		    (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
			printk(KERN_ERR "EXT3-fs: Journal does not support "
			       "requested data journaling mode\n");
			goto failed_mount3;
		}
	default:
		break;
	}

	/*
	 * The journal_load will have done any necessary log recovery,
	 * so we can safely mount the rest of the filesystem now.
	 */

	sb->s_root = d_alloc_root(iget(sb, EXT3_ROOT_INO));
	if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
	    !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
		if (sb->s_root) {
			dput(sb->s_root);
			sb->s_root = NULL;
			printk(KERN_ERR
			       "EXT3-fs: corrupt root inode, run e2fsck\n");
		} else
			printk(KERN_ERR "EXT3-fs: get root inode failed\n");
		goto failed_mount3;
	}

	ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
	/*
	 * akpm: core read_super() calls in here with the superblock locked.
	 * That deadlocks, because orphan cleanup needs to lock the superblock
	 * in numerous places.  Here we just pop the lock - it's relatively
	 * harmless, because we are now ready to accept write_super() requests,
	 * and aviro says that's the only reason for hanging onto the
	 * superblock lock.
	 */
	EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
	ext3_orphan_cleanup(sb, es);
	EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
	if (needs_recovery)
		printk (KERN_INFO "EXT3-fs: recovery complete.\n");
	ext3_mark_recovery_complete(sb, es);
	printk (KERN_INFO "EXT3-fs: mounted filesystem with %s data mode.\n",
		test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
		test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
		"writeback");

1387 1388 1389 1390 1391 1392 1393
	percpu_counter_mod(&sbi->s_freeblocks_counter,
		ext3_count_free_blocks(sb));
	percpu_counter_mod(&sbi->s_freeinodes_counter,
		ext3_count_free_inodes(sb));
	percpu_counter_mod(&sbi->s_dirs_counter,
		ext3_count_dirs(sb));

1394
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1395 1396 1397 1398

failed_mount3:
	journal_destroy(sbi->s_journal);
failed_mount2:
1399
	kfree(sbi->s_debts);
Linus Torvalds's avatar
Linus Torvalds committed
1400 1401 1402 1403 1404 1405 1406
	for (i = 0; i < db_count; i++)
		brelse(sbi->s_group_desc[i]);
	kfree(sbi->s_group_desc);
failed_mount:
	ext3_blkdev_remove(sbi);
	brelse(bh);
out_fail:
1407
	sb->s_fs_info = NULL;
1408
	kfree(sbi);
1409
	return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1410 1411
}

1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
/*
 * Setup any per-fs journal parameters now.  We'll do this both on
 * initial mount, once the journal has been initialised but before we've
 * done any recovery; and again on any subsequent remount. 
 */
static void ext3_init_journal_params(struct ext3_sb_info *sbi, 
				     journal_t *journal)
{
	if (sbi->s_commit_interval)
		journal->j_commit_interval = sbi->s_commit_interval;
	/* We could also set up an ext3-specific default for the commit
	 * interval here, but for now we'll just fall back to the jbd
	 * default. */
}


Linus Torvalds's avatar
Linus Torvalds committed
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
static journal_t *ext3_get_journal(struct super_block *sb, int journal_inum)
{
	struct inode *journal_inode;
	journal_t *journal;

	/* First, test for the existence of a valid inode on disk.  Bad
	 * things happen if we iget() an unused inode, as the subsequent
	 * iput() will try to delete it. */

	journal_inode = iget(sb, journal_inum);
	if (!journal_inode) {
		printk(KERN_ERR "EXT3-fs: no journal found.\n");
		return NULL;
	}
	if (!journal_inode->i_nlink) {
		make_bad_inode(journal_inode);
		iput(journal_inode);
		printk(KERN_ERR "EXT3-fs: journal inode is deleted.\n");
		return NULL;
	}

	jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
		  journal_inode, journal_inode->i_size);
	if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
		printk(KERN_ERR "EXT3-fs: invalid journal inode.\n");
		iput(journal_inode);
		return NULL;
	}

	journal = journal_init_inode(journal_inode);
Linus Torvalds's avatar
Linus Torvalds committed
1458 1459
	if (!journal) {
		printk(KERN_ERR "EXT3-fs: Could not load journal inode\n");
Linus Torvalds's avatar
Linus Torvalds committed
1460
		iput(journal_inode);
Linus Torvalds's avatar
Linus Torvalds committed
1461
	}
1462
	journal->j_private = sb;
1463
	ext3_init_journal_params(EXT3_SB(sb), journal);
Linus Torvalds's avatar
Linus Torvalds committed
1464 1465 1466 1467
	return journal;
}

static journal_t *ext3_get_dev_journal(struct super_block *sb,
Alexander Viro's avatar
Alexander Viro committed
1468
				       dev_t j_dev)
Linus Torvalds's avatar
Linus Torvalds committed
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
{
	struct buffer_head * bh;
	journal_t *journal;
	int start;
	int len;
	int hblock, blocksize;
	unsigned long sb_block;
	unsigned long offset;
	struct ext3_super_block * es;
	struct block_device *bdev;

Linus Torvalds's avatar
Linus Torvalds committed
1480
	bdev = ext3_blkdev_get(j_dev);
Linus Torvalds's avatar
Linus Torvalds committed
1481 1482 1483
	if (bdev == NULL)
		return NULL;

1484 1485 1486 1487 1488 1489 1490
	if (bd_claim(bdev, sb)) {
		printk(KERN_ERR
		        "EXT3: failed to claim external journal device.\n");
		blkdev_put(bdev, BDEV_FS);
		return NULL;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1491
	blocksize = sb->s_blocksize;
1492
	hblock = bdev_hardsect_size(bdev);
Linus Torvalds's avatar
Linus Torvalds committed
1493 1494 1495 1496 1497
	if (blocksize < hblock) {
		printk(KERN_ERR
			"EXT3-fs: blocksize too small for journal device.\n");
		goto out_bdev;
	}
1498

Linus Torvalds's avatar
Linus Torvalds committed
1499 1500
	sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
	offset = EXT3_MIN_BLOCK_SIZE % blocksize;
1501
	set_blocksize(bdev, blocksize);
Linus Torvalds's avatar
Linus Torvalds committed
1502
	if (!(bh = __bread(bdev, sb_block, blocksize))) {
Linus Torvalds's avatar
Linus Torvalds committed
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
		printk(KERN_ERR "EXT3-fs: couldn't read superblock of "
		       "external journal\n");
		goto out_bdev;
	}

	es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
	if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
	    !(le32_to_cpu(es->s_feature_incompat) &
	      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
		printk(KERN_ERR "EXT3-fs: external journal has "
					"bad superblock\n");
		brelse(bh);
		goto out_bdev;
	}

	if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
		printk(KERN_ERR "EXT3-fs: journal UUID does not match\n");
		brelse(bh);
		goto out_bdev;
	}

	len = le32_to_cpu(es->s_blocks_count);
	start = sb_block + 1;
	brelse(bh);	/* we're done with the superblock */

Linus Torvalds's avatar
Linus Torvalds committed
1528
	journal = journal_init_dev(bdev, sb->s_bdev,
Linus Torvalds's avatar
Linus Torvalds committed
1529 1530 1531 1532 1533
					start, len, blocksize);
	if (!journal) {
		printk(KERN_ERR "EXT3-fs: failed to create device journal\n");
		goto out_bdev;
	}
1534
	journal->j_private = sb;
Linus Torvalds's avatar
Linus Torvalds committed
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
	ll_rw_block(READ, 1, &journal->j_sb_buffer);
	wait_on_buffer(journal->j_sb_buffer);
	if (!buffer_uptodate(journal->j_sb_buffer)) {
		printk(KERN_ERR "EXT3-fs: I/O error on journal device\n");
		goto out_journal;
	}
	if (ntohl(journal->j_superblock->s_nr_users) != 1) {
		printk(KERN_ERR "EXT3-fs: External journal has more than one "
					"user (unsupported) - %d\n",
			ntohl(journal->j_superblock->s_nr_users));
		goto out_journal;
	}
	EXT3_SB(sb)->journal_bdev = bdev;
1548
	ext3_init_journal_params(EXT3_SB(sb), journal);
Linus Torvalds's avatar
Linus Torvalds committed
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
	return journal;
out_journal:
	journal_destroy(journal);
out_bdev:
	ext3_blkdev_put(bdev);
	return NULL;
}

static int ext3_load_journal(struct super_block * sb,
			     struct ext3_super_block * es)
{
	journal_t *journal;
	int journal_inum = le32_to_cpu(es->s_journal_inum);
1562
	dev_t journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
Linus Torvalds's avatar
Linus Torvalds committed
1563
	int err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1564 1565
	int really_read_only;

Alexander Viro's avatar
Alexander Viro committed
1566
	really_read_only = bdev_read_only(sb->s_bdev);
Linus Torvalds's avatar
Linus Torvalds committed
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587

	/*
	 * Are we loading a blank journal or performing recovery after a
	 * crash?  For recovery, we need to check in advance whether we
	 * can get read-write access to the device.
	 */

	if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
		if (sb->s_flags & MS_RDONLY) {
			printk(KERN_INFO "EXT3-fs: INFO: recovery "
					"required on readonly filesystem.\n");
			if (really_read_only) {
				printk(KERN_ERR "EXT3-fs: write access "
					"unavailable, cannot proceed.\n");
				return -EROFS;
			}
			printk (KERN_INFO "EXT3-fs: write access will "
					"be enabled during recovery.\n");
		}
	}

Alexander Viro's avatar
Alexander Viro committed
1588
	if (journal_inum && journal_dev) {
Linus Torvalds's avatar
Linus Torvalds committed
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
		printk(KERN_ERR "EXT3-fs: filesystem has both journal "
		       "and inode journals!\n");
		return -EINVAL;
	}

	if (journal_inum) {
		if (!(journal = ext3_get_journal(sb, journal_inum)))
			return -EINVAL;
	} else {
		if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
			return -EINVAL;
	}

	if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
		err = journal_update_format(journal);
		if (err)  {
			printk(KERN_ERR "EXT3-fs: error updating journal.\n");
			journal_destroy(journal);
			return err;
		}
	}

	if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
Linus Torvalds's avatar
Linus Torvalds committed
1612 1613 1614
		err = journal_wipe(journal, !really_read_only);
	if (!err)
		err = journal_load(journal);
Linus Torvalds's avatar
Linus Torvalds committed
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669

	if (err) {
		printk(KERN_ERR "EXT3-fs: error loading journal.\n");
		journal_destroy(journal);
		return err;
	}

	EXT3_SB(sb)->s_journal = journal;
	ext3_clear_journal_err(sb, es);
	return 0;
}

static int ext3_create_journal(struct super_block * sb,
			       struct ext3_super_block * es,
			       int journal_inum)
{
	journal_t *journal;

	if (sb->s_flags & MS_RDONLY) {
		printk(KERN_ERR "EXT3-fs: readonly filesystem when trying to "
				"create journal.\n");
		return -EROFS;
	}

	if (!(journal = ext3_get_journal(sb, journal_inum)))
		return -EINVAL;

	printk(KERN_INFO "EXT3-fs: creating new journal on inode %d\n",
	       journal_inum);

	if (journal_create(journal)) {
		printk(KERN_ERR "EXT3-fs: error creating journal.\n");
		journal_destroy(journal);
		return -EIO;
	}

	EXT3_SB(sb)->s_journal = journal;

	ext3_update_dynamic_rev(sb);
	EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
	EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);

	es->s_journal_inum = cpu_to_le32(journal_inum);
	sb->s_dirt = 1;

	/* Make sure we flush the recovery flag to disk. */
	ext3_commit_super(sb, es, 1);

	return 0;
}

static void ext3_commit_super (struct super_block * sb,
			       struct ext3_super_block * es,
			       int sync)
{
1670 1671 1672 1673
	struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;

	if (!sbh)
		return;
1674
	es->s_wtime = cpu_to_le32(get_seconds());
1675 1676
	es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
	es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
1677 1678
	BUFFER_TRACE(sbh, "marking dirty");
	mark_buffer_dirty(sbh);
1679
	if (sync)
1680
		sync_dirty_buffer(sbh);
Linus Torvalds's avatar
Linus Torvalds committed
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
}


/*
 * Have we just finished recovery?  If so, and if we are mounting (or
 * remounting) the filesystem readonly, then we will end up with a
 * consistent fs on disk.  Record that fact.
 */
static void ext3_mark_recovery_complete(struct super_block * sb,
					struct ext3_super_block * es)
{
	journal_flush(EXT3_SB(sb)->s_journal);
	if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
	    sb->s_flags & MS_RDONLY) {
		EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
		sb->s_dirt = 0;
		ext3_commit_super(sb, es, 1);
	}
}

/*
 * If we are mounting (or read-write remounting) a filesystem whose journal
 * has recorded an error from a previous lifetime, move that error to the
 * main filesystem now.
 */
static void ext3_clear_journal_err(struct super_block * sb,
				   struct ext3_super_block * es)
{
	journal_t *journal;
	int j_errno;
	const char *errstr;
1712

Linus Torvalds's avatar
Linus Torvalds committed
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
	journal = EXT3_SB(sb)->s_journal;

	/*
	 * Now check for any error status which may have been recorded in the
	 * journal by a prior ext3_error() or ext3_abort()
	 */

	j_errno = journal_errno(journal);
	if (j_errno) {
		char nbuf[16];
1723

Linus Torvalds's avatar
Linus Torvalds committed
1724 1725 1726 1727 1728
		errstr = ext3_decode_error(sb, j_errno, nbuf);
		ext3_warning(sb, __FUNCTION__, "Filesystem error recorded "
			     "from previous mount: %s", errstr);
		ext3_warning(sb, __FUNCTION__, "Marking fs in need of "
			     "filesystem check.");
1729

1730
		EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
Linus Torvalds's avatar
Linus Torvalds committed
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
		es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
		ext3_commit_super (sb, es, 1);

		journal_clear_err(journal);
	}
}

/*
 * Force the running and committing transactions to commit,
 * and wait on the commit.
 */
int ext3_force_commit(struct super_block *sb)
{
	journal_t *journal;
	int ret;

	if (sb->s_flags & MS_RDONLY)
		return 0;

	journal = EXT3_SB(sb)->s_journal;
	sb->s_dirt = 0;
	ret = ext3_journal_force_commit(journal);
	return ret;
}

/*
 * Ext3 always journals updates to the superblock itself, so we don't
 * have to propagate any other updates to the superblock on disk at this
 * point.  Just start an async writeback to get the buffers on their way
 * to the disk.
 *
 * This implicitly triggers the writebehind on sync().
 */

void ext3_write_super (struct super_block * sb)
{
	if (down_trylock(&sb->s_lock) == 0)
1768
		BUG();
Linus Torvalds's avatar
Linus Torvalds committed
1769
	sb->s_dirt = 0;
1770
}
Linus Torvalds's avatar
Linus Torvalds committed
1771

1772 1773 1774 1775 1776
static int ext3_sync_fs(struct super_block *sb, int wait)
{
	tid_t target;

	sb->s_dirt = 0;
1777 1778 1779 1780
	if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
		if (wait)
			log_wait_commit(EXT3_SB(sb)->s_journal, target);
	}
1781
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
}

/*
 * LVM calls this function before a (read-only) snapshot is created.  This
 * gives us a chance to flush the journal completely and mark the fs clean.
 */
void ext3_write_super_lockfs(struct super_block *sb)
{
	sb->s_dirt = 0;

	if (!(sb->s_flags & MS_RDONLY)) {
		journal_t *journal = EXT3_SB(sb)->s_journal;

		/* Now we set up the journal barrier. */
		journal_lock_updates(journal);
		journal_flush(journal);

		/* Journal blocked and flushed, clear needs_recovery flag. */
		EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
		ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
	}
}

/*
 * Called by LVM after the snapshot is done.  We need to reset the RECOVER
 * flag here, even though the filesystem is not technically dirty yet.
 */
void ext3_unlockfs(struct super_block *sb)
{
	if (!(sb->s_flags & MS_RDONLY)) {
		lock_super(sb);
		/* Reser the needs_recovery flag before the fs is unlocked. */
		EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
		ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
		unlock_super(sb);
		journal_unlock_updates(EXT3_SB(sb)->s_journal);
	}
}

int ext3_remount (struct super_block * sb, int * flags, char * data)
{
	struct ext3_super_block * es;
	struct ext3_sb_info *sbi = EXT3_SB(sb);
	unsigned long tmp;

	/*
	 * Allow the "check" option to be passed as a remount option.
	 */
1830
	if (!parse_options(data, sbi, &tmp, 1))
Linus Torvalds's avatar
Linus Torvalds committed
1831 1832 1833 1834 1835
		return -EINVAL;

	if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
		ext3_abort(sb, __FUNCTION__, "Abort forced by user");

1836 1837 1838
	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
		((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);

Linus Torvalds's avatar
Linus Torvalds committed
1839 1840
	es = sbi->s_es;

1841
	ext3_init_journal_params(sbi, sbi->s_journal);
1842

Linus Torvalds's avatar
Linus Torvalds committed
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
		if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
			return -EROFS;

		if (*flags & MS_RDONLY) {
			/*
			 * First of all, the unconditional stuff we have to do
			 * to disable replay of the journal when we next remount
			 */
			sb->s_flags |= MS_RDONLY;

			/*
			 * OK, test if we are remounting a valid rw partition
			 * readonly, and if so set the rdonly flag and then
			 * mark the partition as valid again.
			 */
			if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
			    (sbi->s_mount_state & EXT3_VALID_FS))
				es->s_state = cpu_to_le16(sbi->s_mount_state);

			ext3_mark_recovery_complete(sb, es);
		} else {
			int ret;
			if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
					~EXT3_FEATURE_RO_COMPAT_SUPP))) {
				printk(KERN_WARNING "EXT3-fs: %s: couldn't "
				       "remount RDWR because of unsupported "
				       "optional features (%x).\n",
Linus Torvalds's avatar
Linus Torvalds committed
1871
				       sb->s_id, ret);
Linus Torvalds's avatar
Linus Torvalds committed
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
				return -EROFS;
			}
			/*
			 * Mounting a RDONLY partition read-write, so reread
			 * and store the current valid flag.  (It may have
			 * been changed by e2fsck since we originally mounted
			 * the partition.)
			 */
			ext3_clear_journal_err(sb, es);
			sbi->s_mount_state = le16_to_cpu(es->s_state);
			if (!ext3_setup_super (sb, es, 0))
				sb->s_flags &= ~MS_RDONLY;
		}
	}
	return 0;
}

1889
int ext3_statfs (struct super_block * sb, struct kstatfs * buf)
Linus Torvalds's avatar
Linus Torvalds committed
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
{
	struct ext3_super_block *es = EXT3_SB(sb)->s_es;
	unsigned long overhead;
	int i;

	if (test_opt (sb, MINIX_DF))
		overhead = 0;
	else {
		/*
		 * Compute the overhead (FS structures)
		 */

		/*
		 * All of the blocks before first_data_block are
		 * overhead
		 */
		overhead = le32_to_cpu(es->s_first_data_block);

		/*
		 * Add the overhead attributed to the superblock and
		 * block group descriptors.  If the sparse superblocks
		 * feature is turned on, then not all groups have this.
		 */
		for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
			overhead += ext3_bg_has_super(sb, i) +
				ext3_bg_num_gdb(sb, i);

		/*
		 * Every block group has an inode bitmap, a block
		 * bitmap, and an inode table.
		 */
		overhead += (EXT3_SB(sb)->s_groups_count *
			     (2 + EXT3_SB(sb)->s_itb_per_group));
	}

	buf->f_type = EXT3_SUPER_MAGIC;
	buf->f_bsize = sb->s_blocksize;
	buf->f_blocks = le32_to_cpu(es->s_blocks_count) - overhead;
	buf->f_bfree = ext3_count_free_blocks (sb);
	buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
	if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
		buf->f_bavail = 0;
	buf->f_files = le32_to_cpu(es->s_inodes_count);
	buf->f_ffree = ext3_count_free_inodes (sb);
	buf->f_namelen = EXT3_NAME_LEN;
	return 0;
}

1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
/* Helper function for writing quotas on sync - we need to start transaction before quota file
 * is locked for write. Otherwise the are possible deadlocks:
 * Process 1                         Process 2
 * ext3_create()                     quota_sync()
 *   journal_start()                   write_dquot()
 *   DQUOT_INIT()                        down(dqio_sem)
 *     down(dqio_sem)                    journal_start()
 *
 */

#ifdef CONFIG_QUOTA

1950 1951 1952 1953
/* Blocks: (2 data blocks) * (3 indirect + 1 descriptor + 1 bitmap) + superblock */
#define EXT3_OLD_QFMT_BLOCKS 11
/* Blocks: quota info + (4 pointer blocks + 1 entry block) * (3 indirect + 1 descriptor + 1 bitmap) + superblock */
#define EXT3_V0_QFMT_BLOCKS 27
1954

1955
static int (*old_write_dquot)(struct dquot *dquot);
1956

1957
static int ext3_write_dquot(struct dquot *dquot)
1958
{
1959 1960 1961
	int nblocks;
	int ret;
	int err;
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
	handle_t *handle;
	struct quota_info *dqops = sb_dqopt(dquot->dq_sb);
	struct inode *qinode;

	switch (dqops->info[dquot->dq_type].dqi_format->qf_fmt_id) {
		case QFMT_VFS_OLD:
			nblocks = EXT3_OLD_QFMT_BLOCKS;
			break;
		case QFMT_VFS_V0:
			nblocks = EXT3_V0_QFMT_BLOCKS;
			break;
		default:
			nblocks = EXT3_MAX_TRANS_DATA;
	}
	qinode = dqops->files[dquot->dq_type]->f_dentry->d_inode;
	handle = ext3_journal_start(qinode, nblocks);
	if (IS_ERR(handle)) {
1979 1980
		ret = PTR_ERR(handle);
		goto out;
1981
	}
1982
	ret = old_write_dquot(dquot);
1983 1984 1985 1986
	err = ext3_journal_stop(handle);
	if (ret == 0)
		ret = err;
out:
1987 1988 1989 1990
	return ret;
}
#endif

1991
static struct super_block *ext3_get_sb(struct file_system_type *fs_type,
1992
	int flags, const char *dev_name, void *data)
1993 1994 1995 1996 1997
{
	return get_sb_bdev(fs_type, flags, dev_name, data, ext3_fill_super);
}

static struct file_system_type ext3_fs_type = {
1998 1999 2000 2001 2002
	.owner		= THIS_MODULE,
	.name		= "ext3",
	.get_sb		= ext3_get_sb,
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
2003
};
Linus Torvalds's avatar
Linus Torvalds committed
2004 2005 2006

static int __init init_ext3_fs(void)
{
2007 2008 2009 2010
	int err = init_ext3_xattr();
	if (err)
		return err;
	err = init_inodecache();
Linus Torvalds's avatar
Linus Torvalds committed
2011 2012
	if (err)
		goto out1;
2013 2014
#ifdef CONFIG_QUOTA
	init_dquot_operations(&ext3_qops);
2015 2016
	old_write_dquot = ext3_qops.write_dquot;
	ext3_qops.write_dquot = ext3_write_dquot;
2017
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2018 2019 2020 2021 2022 2023 2024
        err = register_filesystem(&ext3_fs_type);
	if (err)
		goto out;
	return 0;
out:
	destroy_inodecache();
out1:
2025
 	exit_ext3_xattr();
Linus Torvalds's avatar
Linus Torvalds committed
2026
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
2027 2028 2029 2030 2031
}

static void __exit exit_ext3_fs(void)
{
	unregister_filesystem(&ext3_fs_type);
Linus Torvalds's avatar
Linus Torvalds committed
2032
	destroy_inodecache();
2033
	exit_ext3_xattr();
Linus Torvalds's avatar
Linus Torvalds committed
2034 2035
}

Linus Torvalds's avatar
Linus Torvalds committed
2036 2037
MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
Linus Torvalds's avatar
Linus Torvalds committed
2038 2039 2040
MODULE_LICENSE("GPL");
module_init(init_ext3_fs)
module_exit(exit_ext3_fs)