super.c 39.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  linux/fs/ext2/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/module.h>
#include <linux/string.h>
21
#include <linux/fs.h>
L
Linus Torvalds 已提交
22 23 24 25 26 27
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/parser.h>
#include <linux/random.h>
#include <linux/buffer_head.h>
28
#include <linux/exportfs.h>
L
Linus Torvalds 已提交
29 30
#include <linux/smp_lock.h>
#include <linux/vfs.h>
31 32
#include <linux/seq_file.h>
#include <linux/mount.h>
V
vignesh babu 已提交
33
#include <linux/log2.h>
34
#include <linux/quotaops.h>
L
Linus Torvalds 已提交
35 36 37 38
#include <asm/uaccess.h>
#include "ext2.h"
#include "xattr.h"
#include "acl.h"
39
#include "xip.h"
L
Linus Torvalds 已提交
40 41 42 43

static void ext2_sync_super(struct super_block *sb,
			    struct ext2_super_block *es);
static int ext2_remount (struct super_block * sb, int * flags, char * data);
44
static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
C
Christoph Hellwig 已提交
45
static int ext2_sync_fs(struct super_block *sb, int wait);
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53 54 55

void ext2_error (struct super_block * sb, const char * function,
		 const char * fmt, ...)
{
	va_list args;
	struct ext2_sb_info *sbi = EXT2_SB(sb);
	struct ext2_super_block *es = sbi->s_es;

	if (!(sb->s_flags & MS_RDONLY)) {
		sbi->s_mount_state |= EXT2_ERROR_FS;
56
		es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
L
Linus Torvalds 已提交
57 58 59 60
		ext2_sync_super(sb, es);
	}

	va_start(args, fmt);
A
Alexey Fisher 已提交
61
	printk(KERN_CRIT "EXT2-fs (%s): error: %s: ", sb->s_id, function);
L
Linus Torvalds 已提交
62 63 64 65 66
	vprintk(fmt, args);
	printk("\n");
	va_end(args);

	if (test_opt(sb, ERRORS_PANIC))
A
Alexey Fisher 已提交
67
		panic("EXT2-fs: panic from previous error\n");
L
Linus Torvalds 已提交
68
	if (test_opt(sb, ERRORS_RO)) {
A
Alexey Fisher 已提交
69 70
		ext2_msg(sb, KERN_CRIT,
			     "error: remounting filesystem read-only");
L
Linus Torvalds 已提交
71 72 73 74
		sb->s_flags |= MS_RDONLY;
	}
}

A
Alexey Fisher 已提交
75 76
void ext2_msg(struct super_block *sb, const char *prefix,
		const char *fmt, ...)
L
Linus Torvalds 已提交
77 78 79 80
{
	va_list args;

	va_start(args, fmt);
A
Alexey Fisher 已提交
81
	printk("%sEXT2-fs (%s): ", prefix, sb->s_id);
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92 93
	vprintk(fmt, args);
	printk("\n");
	va_end(args);
}

void ext2_update_dynamic_rev(struct super_block *sb)
{
	struct ext2_super_block *es = EXT2_SB(sb)->s_es;

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

A
Alexey Fisher 已提交
94 95 96
	ext2_msg(sb, KERN_WARNING,
		     "warning: updating to rev %d because of "
		     "new feature flag, running e2fsck is recommended",
L
Linus Torvalds 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		     EXT2_DYNAMIC_REV);

	es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
	es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
	es->s_rev_level = cpu_to_le32(EXT2_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.
	 */
}

static void ext2_put_super (struct super_block * sb)
{
	int db_count;
	int i;
	struct ext2_sb_info *sbi = EXT2_SB(sb);

118 119
	lock_kernel();

120 121 122
	if (sb->s_dirt)
		ext2_write_super(sb);

L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	ext2_xattr_put_super(sb);
	if (!(sb->s_flags & MS_RDONLY)) {
		struct ext2_super_block *es = sbi->s_es;

		es->s_state = cpu_to_le16(sbi->s_mount_state);
		ext2_sync_super(sb, es);
	}
	db_count = sbi->s_gdb_count;
	for (i = 0; i < db_count; i++)
		if (sbi->s_group_desc[i])
			brelse (sbi->s_group_desc[i]);
	kfree(sbi->s_group_desc);
	kfree(sbi->s_debts);
	percpu_counter_destroy(&sbi->s_freeblocks_counter);
	percpu_counter_destroy(&sbi->s_freeinodes_counter);
	percpu_counter_destroy(&sbi->s_dirs_counter);
	brelse (sbi->s_sbh);
	sb->s_fs_info = NULL;
141
	kfree(sbi->s_blockgroup_lock);
L
Linus Torvalds 已提交
142 143
	kfree(sbi);

144
	unlock_kernel();
L
Linus Torvalds 已提交
145 146
}

147
static struct kmem_cache * ext2_inode_cachep;
L
Linus Torvalds 已提交
148 149 150 151

static struct inode *ext2_alloc_inode(struct super_block *sb)
{
	struct ext2_inode_info *ei;
152
	ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
L
Linus Torvalds 已提交
153 154
	if (!ei)
		return NULL;
M
Martin J. Bligh 已提交
155
	ei->i_block_alloc_info = NULL;
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164
	ei->vfs_inode.i_version = 1;
	return &ei->vfs_inode;
}

static void ext2_destroy_inode(struct inode *inode)
{
	kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
}

165
static void init_once(void *foo)
L
Linus Torvalds 已提交
166 167 168
{
	struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;

C
Christoph Lameter 已提交
169
	rwlock_init(&ei->i_meta_lock);
L
Linus Torvalds 已提交
170
#ifdef CONFIG_EXT2_FS_XATTR
C
Christoph Lameter 已提交
171
	init_rwsem(&ei->xattr_sem);
L
Linus Torvalds 已提交
172
#endif
M
Martin J. Bligh 已提交
173
	mutex_init(&ei->truncate_mutex);
C
Christoph Lameter 已提交
174
	inode_init_once(&ei->vfs_inode);
L
Linus Torvalds 已提交
175
}
176

L
Linus Torvalds 已提交
177 178 179 180
static int init_inodecache(void)
{
	ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
					     sizeof(struct ext2_inode_info),
181 182
					     0, (SLAB_RECLAIM_ACCOUNT|
						SLAB_MEM_SPREAD),
183
					     init_once);
L
Linus Torvalds 已提交
184 185 186 187 188 189 190
	if (ext2_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
191
	kmem_cache_destroy(ext2_inode_cachep);
L
Linus Torvalds 已提交
192 193 194 195
}

static void ext2_clear_inode(struct inode *inode)
{
M
Martin J. Bligh 已提交
196 197 198 199 200
	struct ext2_block_alloc_info *rsv = EXT2_I(inode)->i_block_alloc_info;
	ext2_discard_reservation(inode);
	EXT2_I(inode)->i_block_alloc_info = NULL;
	if (unlikely(rsv))
		kfree(rsv);
L
Linus Torvalds 已提交
201 202
}

203 204
static int ext2_show_options(struct seq_file *seq, struct vfsmount *vfs)
{
M
Miklos Szeredi 已提交
205 206 207 208
	struct super_block *sb = vfs->mnt_sb;
	struct ext2_sb_info *sbi = EXT2_SB(sb);
	struct ext2_super_block *es = sbi->s_es;
	unsigned long def_mount_opts;
209

M
Miklos Szeredi 已提交
210 211 212 213 214 215 216
	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);

	if (sbi->s_sb_block != 1)
		seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
	if (test_opt(sb, MINIX_DF))
		seq_puts(seq, ",minixdf");
	if (test_opt(sb, GRPID))
217
		seq_puts(seq, ",grpid");
M
Miklos Szeredi 已提交
218 219 220 221 222 223 224 225 226 227
	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
		seq_puts(seq, ",nogrpid");
	if (sbi->s_resuid != EXT2_DEF_RESUID ||
	    le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
		seq_printf(seq, ",resuid=%u", sbi->s_resuid);
	}
	if (sbi->s_resgid != EXT2_DEF_RESGID ||
	    le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
		seq_printf(seq, ",resgid=%u", sbi->s_resgid);
	}
228
	if (test_opt(sb, ERRORS_RO)) {
M
Miklos Szeredi 已提交
229 230 231
		int def_errors = le16_to_cpu(es->s_errors);

		if (def_errors == EXT2_ERRORS_PANIC ||
232 233
		    def_errors == EXT2_ERRORS_CONTINUE) {
			seq_puts(seq, ",errors=remount-ro");
M
Miklos Szeredi 已提交
234 235
		}
	}
236 237
	if (test_opt(sb, ERRORS_CONT))
		seq_puts(seq, ",errors=continue");
M
Miklos Szeredi 已提交
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 263 264
	if (test_opt(sb, ERRORS_PANIC))
		seq_puts(seq, ",errors=panic");
	if (test_opt(sb, NO_UID32))
		seq_puts(seq, ",nouid32");
	if (test_opt(sb, DEBUG))
		seq_puts(seq, ",debug");
	if (test_opt(sb, OLDALLOC))
		seq_puts(seq, ",oldalloc");

#ifdef CONFIG_EXT2_FS_XATTR
	if (test_opt(sb, XATTR_USER))
		seq_puts(seq, ",user_xattr");
	if (!test_opt(sb, XATTR_USER) &&
	    (def_mount_opts & EXT2_DEFM_XATTR_USER)) {
		seq_puts(seq, ",nouser_xattr");
	}
#endif

#ifdef CONFIG_EXT2_FS_POSIX_ACL
	if (test_opt(sb, POSIX_ACL))
		seq_puts(seq, ",acl");
	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT2_DEFM_ACL))
		seq_puts(seq, ",noacl");
#endif

	if (test_opt(sb, NOBH))
		seq_puts(seq, ",nobh");
265 266 267 268 269 270 271 272 273

#if defined(CONFIG_QUOTA)
	if (sbi->s_mount_opt & EXT2_MOUNT_USRQUOTA)
		seq_puts(seq, ",usrquota");

	if (sbi->s_mount_opt & EXT2_MOUNT_GRPQUOTA)
		seq_puts(seq, ",grpquota");
#endif

274 275 276 277 278
#if defined(CONFIG_EXT2_FS_XIP)
	if (sbi->s_mount_opt & EXT2_MOUNT_XIP)
		seq_puts(seq, ",xip");
#endif

M
Miklos Szeredi 已提交
279 280 281
	if (!test_opt(sb, RESERVATION))
		seq_puts(seq, ",noreservation");

282 283 284
	return 0;
}

L
Linus Torvalds 已提交
285 286 287 288 289
#ifdef CONFIG_QUOTA
static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
#endif

290
static const struct super_operations ext2_sops = {
L
Linus Torvalds 已提交
291 292 293 294 295 296
	.alloc_inode	= ext2_alloc_inode,
	.destroy_inode	= ext2_destroy_inode,
	.write_inode	= ext2_write_inode,
	.delete_inode	= ext2_delete_inode,
	.put_super	= ext2_put_super,
	.write_super	= ext2_write_super,
C
Christoph Hellwig 已提交
297
	.sync_fs	= ext2_sync_fs,
L
Linus Torvalds 已提交
298 299 300
	.statfs		= ext2_statfs,
	.remount_fs	= ext2_remount,
	.clear_inode	= ext2_clear_inode,
301
	.show_options	= ext2_show_options,
L
Linus Torvalds 已提交
302 303 304 305 306 307
#ifdef CONFIG_QUOTA
	.quota_read	= ext2_quota_read,
	.quota_write	= ext2_quota_write,
#endif
};

C
Christoph Hellwig 已提交
308 309
static struct inode *ext2_nfs_get_inode(struct super_block *sb,
		u64 ino, u32 generation)
310 311 312 313 314 315 316 317 318 319 320 321 322
{
	struct inode *inode;

	if (ino < EXT2_FIRST_INO(sb) && ino != EXT2_ROOT_INO)
		return ERR_PTR(-ESTALE);
	if (ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
		return ERR_PTR(-ESTALE);

	/* iget isn't really right if the inode is currently unallocated!!
	 * ext2_read_inode currently does appropriate checks, but
	 * it might be "neater" to call ext2_get_inode first and check
	 * if the inode is valid.....
	 */
323 324 325 326
	inode = ext2_iget(sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
	if (generation && inode->i_generation != generation) {
327 328 329 330
		/* we didn't find the right inode.. */
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
C
Christoph Hellwig 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	return inode;
}

static struct dentry *ext2_fh_to_dentry(struct super_block *sb, struct fid *fid,
		int fh_len, int fh_type)
{
	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
				    ext2_nfs_get_inode);
}

static struct dentry *ext2_fh_to_parent(struct super_block *sb, struct fid *fid,
		int fh_len, int fh_type)
{
	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
				    ext2_nfs_get_inode);
346 347
}

L
Linus Torvalds 已提交
348 349 350 351 352
/* Yes, most of these are left as NULL!!
 * A NULL value implies the default, which works with ext2-like file
 * systems, but can be improved upon.
 * Currently only get_parent is required.
 */
353
static const struct export_operations ext2_export_ops = {
C
Christoph Hellwig 已提交
354 355
	.fh_to_dentry = ext2_fh_to_dentry,
	.fh_to_parent = ext2_fh_to_parent,
L
Linus Torvalds 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	.get_parent = ext2_get_parent,
};

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("EXT2-fs: Invalid sb specification: %s\n",
		       (char *) *data);
		return 1;
	}
	if (*options == ',')
		options++;
	*data = (void *) options;
	return sb_block;
}

enum {
	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
381
	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
A
Adrian Bunk 已提交
382
	Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
383 384
	Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
	Opt_acl, Opt_noacl, Opt_xip, Opt_ignore, Opt_err, Opt_quota,
M
Martin J. Bligh 已提交
385
	Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
L
Linus Torvalds 已提交
386 387
};

388
static const match_table_t tokens = {
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	{Opt_bsd_df, "bsddf"},
	{Opt_minix_df, "minixdf"},
	{Opt_grpid, "grpid"},
	{Opt_grpid, "bsdgroups"},
	{Opt_nogrpid, "nogrpid"},
	{Opt_nogrpid, "sysvgroups"},
	{Opt_resgid, "resgid=%u"},
	{Opt_resuid, "resuid=%u"},
	{Opt_sb, "sb=%u"},
	{Opt_err_cont, "errors=continue"},
	{Opt_err_panic, "errors=panic"},
	{Opt_err_ro, "errors=remount-ro"},
	{Opt_nouid32, "nouid32"},
	{Opt_nocheck, "check=none"},
	{Opt_nocheck, "nocheck"},
	{Opt_debug, "debug"},
	{Opt_oldalloc, "oldalloc"},
	{Opt_orlov, "orlov"},
	{Opt_nobh, "nobh"},
	{Opt_user_xattr, "user_xattr"},
	{Opt_nouser_xattr, "nouser_xattr"},
	{Opt_acl, "acl"},
	{Opt_noacl, "noacl"},
412
	{Opt_xip, "xip"},
413
	{Opt_grpquota, "grpquota"},
L
Linus Torvalds 已提交
414
	{Opt_ignore, "noquota"},
415 416
	{Opt_quota, "quota"},
	{Opt_usrquota, "usrquota"},
M
Martin J. Bligh 已提交
417 418
	{Opt_reservation, "reservation"},
	{Opt_noreservation, "noreservation"},
L
Linus Torvalds 已提交
419 420 421
	{Opt_err, NULL}
};

A
Alexey Fisher 已提交
422
static int parse_options(char *options, struct super_block *sb)
L
Linus Torvalds 已提交
423
{
A
Alexey Fisher 已提交
424 425
	char *p;
	struct ext2_sb_info *sbi = EXT2_SB(sb);
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	substring_t args[MAX_OPT_ARGS];
	int option;

	if (!options)
		return 1;

	while ((p = strsep (&options, ",")) != NULL) {
		int token;
		if (!*p)
			continue;

		token = match_token(p, tokens, args);
		switch (token) {
		case Opt_bsd_df:
			clear_opt (sbi->s_mount_opt, MINIX_DF);
			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:
V
Vasily Averin 已提交
466 467 468
			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
			clear_opt (sbi->s_mount_opt, ERRORS_RO);
			set_opt (sbi->s_mount_opt, ERRORS_PANIC);
L
Linus Torvalds 已提交
469 470
			break;
		case Opt_err_ro:
V
Vasily Averin 已提交
471 472 473
			clear_opt (sbi->s_mount_opt, ERRORS_CONT);
			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
			set_opt (sbi->s_mount_opt, ERRORS_RO);
L
Linus Torvalds 已提交
474 475
			break;
		case Opt_err_cont:
V
Vasily Averin 已提交
476 477 478
			clear_opt (sbi->s_mount_opt, ERRORS_RO);
			clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
			set_opt (sbi->s_mount_opt, ERRORS_CONT);
L
Linus Torvalds 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
			break;
		case Opt_nouid32:
			set_opt (sbi->s_mount_opt, NO_UID32);
			break;
		case Opt_nocheck:
			clear_opt (sbi->s_mount_opt, CHECK);
			break;
		case Opt_debug:
			set_opt (sbi->s_mount_opt, DEBUG);
			break;
		case Opt_oldalloc:
			set_opt (sbi->s_mount_opt, OLDALLOC);
			break;
		case Opt_orlov:
			clear_opt (sbi->s_mount_opt, OLDALLOC);
			break;
		case Opt_nobh:
			set_opt (sbi->s_mount_opt, NOBH);
			break;
#ifdef CONFIG_EXT2_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:
A
Alexey Fisher 已提交
508 509
			ext2_msg(sb, KERN_INFO, "(no)user_xattr options"
				"not supported");
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518 519 520 521
			break;
#endif
#ifdef CONFIG_EXT2_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:
A
Alexey Fisher 已提交
522 523
			ext2_msg(sb, KERN_INFO,
				"(no)acl options not supported");
L
Linus Torvalds 已提交
524 525
			break;
#endif
526 527 528 529
		case Opt_xip:
#ifdef CONFIG_EXT2_FS_XIP
			set_opt (sbi->s_mount_opt, XIP);
#else
A
Alexey Fisher 已提交
530
			ext2_msg(sb, KERN_INFO, "xip option not supported");
531 532
#endif
			break;
533 534 535 536 537 538 539 540 541 542 543 544 545 546

#if defined(CONFIG_QUOTA)
		case Opt_quota:
		case Opt_usrquota:
			set_opt(sbi->s_mount_opt, USRQUOTA);
			break;

		case Opt_grpquota:
			set_opt(sbi->s_mount_opt, GRPQUOTA);
			break;
#else
		case Opt_quota:
		case Opt_usrquota:
		case Opt_grpquota:
A
Alexey Fisher 已提交
547 548
			ext2_msg(sb, KERN_INFO,
				"quota operations not supported");
549 550 551
			break;
#endif

M
Martin J. Bligh 已提交
552 553
		case Opt_reservation:
			set_opt(sbi->s_mount_opt, RESERVATION);
A
Alexey Fisher 已提交
554
			ext2_msg(sb, KERN_INFO, "reservations ON");
M
Martin J. Bligh 已提交
555 556 557
			break;
		case Opt_noreservation:
			clear_opt(sbi->s_mount_opt, RESERVATION);
A
Alexey Fisher 已提交
558
			ext2_msg(sb, KERN_INFO, "reservations OFF");
M
Martin J. Bligh 已提交
559
			break;
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
		case Opt_ignore:
			break;
		default:
			return 0;
		}
	}
	return 1;
}

static int ext2_setup_super (struct super_block * sb,
			      struct ext2_super_block * es,
			      int read_only)
{
	int res = 0;
	struct ext2_sb_info *sbi = EXT2_SB(sb);

	if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
A
Alexey Fisher 已提交
577 578 579
		ext2_msg(sb, KERN_ERR,
			"error: revision level too high, "
			"forcing read-only mode");
L
Linus Torvalds 已提交
580 581 582 583 584
		res = MS_RDONLY;
	}
	if (read_only)
		return res;
	if (!(sbi->s_mount_state & EXT2_VALID_FS))
A
Alexey Fisher 已提交
585 586 587
		ext2_msg(sb, KERN_WARNING,
			"warning: mounting unchecked fs, "
			"running e2fsck is recommended");
L
Linus Torvalds 已提交
588
	else if ((sbi->s_mount_state & EXT2_ERROR_FS))
A
Alexey Fisher 已提交
589 590 591
		ext2_msg(sb, KERN_WARNING,
			"warning: mounting fs with errors, "
			"running e2fsck is recommended");
L
Linus Torvalds 已提交
592 593 594
	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))
A
Alexey Fisher 已提交
595 596 597
		ext2_msg(sb, KERN_WARNING,
			"warning: maximal mount count reached, "
			"running e2fsck is recommended");
L
Linus Torvalds 已提交
598
	else if (le32_to_cpu(es->s_checkinterval) &&
A
Alexey Fisher 已提交
599 600 601 602 603
		(le32_to_cpu(es->s_lastcheck) +
			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
		ext2_msg(sb, KERN_WARNING,
			"warning: checktime reached, "
			"running e2fsck is recommended");
L
Linus Torvalds 已提交
604 605
	if (!le16_to_cpu(es->s_max_mnt_count))
		es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
M
Marcin Slusarz 已提交
606
	le16_add_cpu(&es->s_mnt_count, 1);
L
Linus Torvalds 已提交
607 608
	ext2_write_super(sb);
	if (test_opt (sb, DEBUG))
A
Alexey Fisher 已提交
609 610
		ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, fs=%lu, gc=%lu, "
			"bpg=%lu, ipg=%lu, mo=%04lx]",
L
Linus Torvalds 已提交
611 612 613 614 615 616 617 618 619
			EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
			sbi->s_frag_size,
			sbi->s_groups_count,
			EXT2_BLOCKS_PER_GROUP(sb),
			EXT2_INODES_PER_GROUP(sb),
			sbi->s_mount_opt);
	return res;
}

620
static int ext2_check_descriptors(struct super_block *sb)
L
Linus Torvalds 已提交
621 622 623 624 625 626
{
	int i;
	struct ext2_sb_info *sbi = EXT2_SB(sb);

	ext2_debug ("Checking group descriptors");

627 628
	for (i = 0; i < sbi->s_groups_count; i++) {
		struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
629 630
		ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
		ext2_fsblk_t last_block;
631

E
Eric Sandeen 已提交
632 633 634 635 636 637 638 639
		if (i == sbi->s_groups_count - 1)
			last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
		else
			last_block = first_block +
				(EXT2_BLOCKS_PER_GROUP(sb) - 1);

		if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
		    le32_to_cpu(gdp->bg_block_bitmap) > last_block)
L
Linus Torvalds 已提交
640 641 642 643 644 645 646
		{
			ext2_error (sb, "ext2_check_descriptors",
				    "Block bitmap for group %d"
				    " not in group (block %lu)!",
				    i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
			return 0;
		}
E
Eric Sandeen 已提交
647 648
		if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
		    le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
L
Linus Torvalds 已提交
649 650 651 652 653 654 655
		{
			ext2_error (sb, "ext2_check_descriptors",
				    "Inode bitmap for group %d"
				    " not in group (block %lu)!",
				    i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
			return 0;
		}
E
Eric Sandeen 已提交
656
		if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
657
		    le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
E
Eric Sandeen 已提交
658
		    last_block)
L
Linus Torvalds 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
		{
			ext2_error (sb, "ext2_check_descriptors",
				    "Inode table for group %d"
				    " not in group (block %lu)!",
				    i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
			return 0;
		}
	}
	return 1;
}

/*
 * 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 ext2_max_size(int bits)
{
	loff_t res = EXT2_NDIR_BLOCKS;
678 679 680 681 682
	int meta_blocks;
	loff_t upper_limit;

	/* This is calculated to be the largest file size for a
	 * dense, file such that the total number of
L
Linus Torvalds 已提交
683
	 * sectors in the file, including data and all indirect blocks,
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
	 * does not exceed 2^32 -1
	 * __u32 i_blocks representing the total number of
	 * 512 bytes blocks of the file
	 */
	upper_limit = (1LL << 32) - 1;

	/* total blocks in file system block size */
	upper_limit >>= (bits - 9);


	/* indirect blocks */
	meta_blocks = 1;
	/* double indirect blocks */
	meta_blocks += 1 + (1LL << (bits-2));
	/* tripple indirect blocks */
	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));

	upper_limit -= meta_blocks;
	upper_limit <<= bits;
L
Linus Torvalds 已提交
703 704 705 706 707 708 709

	res += 1LL << (bits-2);
	res += 1LL << (2*(bits-2));
	res += 1LL << (3*(bits-2));
	res <<= bits;
	if (res > upper_limit)
		res = upper_limit;
710 711 712 713

	if (res > MAX_LFS_FILESIZE)
		res = MAX_LFS_FILESIZE;

L
Linus Torvalds 已提交
714 715 716 717 718 719 720 721
	return res;
}

static unsigned long descriptor_loc(struct super_block *sb,
				    unsigned long logic_sb_block,
				    int nr)
{
	struct ext2_sb_info *sbi = EXT2_SB(sb);
722
	unsigned long bg, first_meta_bg;
L
Linus Torvalds 已提交
723 724 725 726 727 728 729 730 731 732
	int has_super = 0;
	
	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);

	if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
	    nr < first_meta_bg)
		return (logic_sb_block + nr + 1);
	bg = sbi->s_desc_per_block * nr;
	if (ext2_bg_has_super(sb, bg))
		has_super = 1;
733 734

	return ext2_group_first_block_no(sb, bg) + has_super;
L
Linus Torvalds 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747
}

static int ext2_fill_super(struct super_block *sb, void *data, int silent)
{
	struct buffer_head * bh;
	struct ext2_sb_info * sbi;
	struct ext2_super_block * es;
	struct inode *root;
	unsigned long block;
	unsigned long sb_block = get_sb_block(&data);
	unsigned long logic_sb_block;
	unsigned long offset = 0;
	unsigned long def_mount_opts;
748
	long ret = -EINVAL;
L
Linus Torvalds 已提交
749 750 751 752
	int blocksize = BLOCK_SIZE;
	int db_count;
	int i, j;
	__le32 features;
753
	int err;
L
Linus Torvalds 已提交
754

755
	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
L
Linus Torvalds 已提交
756 757
	if (!sbi)
		return -ENOMEM;
758 759 760 761 762 763 764

	sbi->s_blockgroup_lock =
		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
	if (!sbi->s_blockgroup_lock) {
		kfree(sbi);
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
765
	sb->s_fs_info = sbi;
M
Miklos Szeredi 已提交
766
	sbi->s_sb_block = sb_block;
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774 775 776

	/*
	 * See what the current blocksize for the device is, and
	 * use that as the blocksize.  Otherwise (or if the blocksize
	 * is smaller than the default) use the default.
	 * This is important for devices that have a hardware
	 * sectorsize that is larger than the default.
	 */
	blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
	if (!blocksize) {
A
Alexey Fisher 已提交
777
		ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
		goto failed_sbi;
	}

	/*
	 * If the superblock doesn't start on a hardware sector boundary,
	 * calculate the offset.  
	 */
	if (blocksize != BLOCK_SIZE) {
		logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
		offset = (sb_block*BLOCK_SIZE) % blocksize;
	} else {
		logic_sb_block = sb_block;
	}

	if (!(bh = sb_bread(sb, logic_sb_block))) {
A
Alexey Fisher 已提交
793
		ext2_msg(sb, KERN_ERR, "error: unable to read superblock");
L
Linus Torvalds 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
		goto failed_sbi;
	}
	/*
	 * Note: s_es must be initialized as soon as possible because
	 *       some ext2 macro-instructions depend on its value
	 */
	es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
	sbi->s_es = es;
	sb->s_magic = le16_to_cpu(es->s_magic);

	if (sb->s_magic != EXT2_SUPER_MAGIC)
		goto cantfind_ext2;

	/* Set defaults before we parse the mount options */
	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
	if (def_mount_opts & EXT2_DEFM_DEBUG)
		set_opt(sbi->s_mount_opt, DEBUG);
	if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
		set_opt(sbi->s_mount_opt, GRPID);
	if (def_mount_opts & EXT2_DEFM_UID16)
		set_opt(sbi->s_mount_opt, NO_UID32);
815
#ifdef CONFIG_EXT2_FS_XATTR
L
Linus Torvalds 已提交
816 817
	if (def_mount_opts & EXT2_DEFM_XATTR_USER)
		set_opt(sbi->s_mount_opt, XATTR_USER);
818 819
#endif
#ifdef CONFIG_EXT2_FS_POSIX_ACL
L
Linus Torvalds 已提交
820 821
	if (def_mount_opts & EXT2_DEFM_ACL)
		set_opt(sbi->s_mount_opt, POSIX_ACL);
822
#endif
L
Linus Torvalds 已提交
823 824 825
	
	if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
826
	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
V
Vasily Averin 已提交
827
		set_opt(sbi->s_mount_opt, ERRORS_CONT);
828 829
	else
		set_opt(sbi->s_mount_opt, ERRORS_RO);
L
Linus Torvalds 已提交
830 831 832 833

	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
	
M
Martin J. Bligh 已提交
834 835
	set_opt(sbi->s_mount_opt, RESERVATION);

A
Alexey Fisher 已提交
836
	if (!parse_options((char *) data, sb))
L
Linus Torvalds 已提交
837 838 839 840 841 842
		goto failed_mount;

	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
		((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
		 MS_POSIXACL : 0);

843 844 845
	ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
				    EXT2_MOUNT_XIP if not */

L
Linus Torvalds 已提交
846 847 848 849
	if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
	    (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
	     EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
	     EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
A
Alexey Fisher 已提交
850 851 852
		ext2_msg(sb, KERN_WARNING,
			"warning: feature flags set on rev 0 fs, "
			"running e2fsck is recommended");
L
Linus Torvalds 已提交
853 854 855 856 857 858 859
	/*
	 * 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.
	 */
	features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
	if (features) {
A
Alexey Fisher 已提交
860 861 862
		ext2_msg(sb, KERN_ERR,	"error: couldn't mount because of "
		       "unsupported optional features (%x)",
			le32_to_cpu(features));
L
Linus Torvalds 已提交
863 864 865 866
		goto failed_mount;
	}
	if (!(sb->s_flags & MS_RDONLY) &&
	    (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
A
Alexey Fisher 已提交
867 868 869
		ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of "
		       "unsupported optional features (%x)",
		       le32_to_cpu(features));
L
Linus Torvalds 已提交
870 871 872 873 874
		goto failed_mount;
	}

	blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);

N
Nick Piggin 已提交
875
	if (ext2_use_xip(sb) && blocksize != PAGE_SIZE) {
876
		if (!silent)
A
Alexey Fisher 已提交
877 878
			ext2_msg(sb, KERN_ERR,
				"error: unsupported blocksize for xip");
879 880 881
		goto failed_mount;
	}

L
Linus Torvalds 已提交
882 883 884 885 886
	/* If the blocksize doesn't match, re-read the thing.. */
	if (sb->s_blocksize != blocksize) {
		brelse(bh);

		if (!sb_set_blocksize(sb, blocksize)) {
A
Alexey Fisher 已提交
887
			ext2_msg(sb, KERN_ERR, "error: blocksize is too small");
L
Linus Torvalds 已提交
888 889 890 891 892 893 894
			goto failed_sbi;
		}

		logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
		offset = (sb_block*BLOCK_SIZE) % blocksize;
		bh = sb_bread(sb, logic_sb_block);
		if(!bh) {
A
Alexey Fisher 已提交
895 896
			ext2_msg(sb, KERN_ERR, "error: couldn't read"
				"superblock on 2nd try");
L
Linus Torvalds 已提交
897 898 899 900 901
			goto failed_sbi;
		}
		es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
		sbi->s_es = es;
		if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
A
Alexey Fisher 已提交
902
			ext2_msg(sb, KERN_ERR, "error: magic mismatch");
L
Linus Torvalds 已提交
903 904 905 906 907 908 909 910 911 912 913 914 915
			goto failed_mount;
		}
	}

	sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);

	if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
		sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
		sbi->s_first_ino = EXT2_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);
		if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
V
vignesh babu 已提交
916
		    !is_power_of_2(sbi->s_inode_size) ||
L
Linus Torvalds 已提交
917
		    (sbi->s_inode_size > blocksize)) {
A
Alexey Fisher 已提交
918 919
			ext2_msg(sb, KERN_ERR,
				"error: unsupported inode size: %d",
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
				sbi->s_inode_size);
			goto failed_mount;
		}
	}

	sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
				   le32_to_cpu(es->s_log_frag_size);
	if (sbi->s_frag_size == 0)
		goto cantfind_ext2;
	sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;

	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);

	if (EXT2_INODE_SIZE(sb) == 0)
		goto cantfind_ext2;
	sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
938
	if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
L
Linus Torvalds 已提交
939 940 941 942 943 944 945 946
		goto cantfind_ext2;
	sbi->s_itb_per_group = sbi->s_inodes_per_group /
					sbi->s_inodes_per_block;
	sbi->s_desc_per_block = sb->s_blocksize /
					sizeof (struct ext2_group_desc);
	sbi->s_sbh = bh;
	sbi->s_mount_state = le16_to_cpu(es->s_state);
	sbi->s_addr_per_block_bits =
947
		ilog2 (EXT2_ADDR_PER_BLOCK(sb));
L
Linus Torvalds 已提交
948
	sbi->s_desc_per_block_bits =
949
		ilog2 (EXT2_DESC_PER_BLOCK(sb));
L
Linus Torvalds 已提交
950 951 952 953 954 955

	if (sb->s_magic != EXT2_SUPER_MAGIC)
		goto cantfind_ext2;

	if (sb->s_blocksize != bh->b_size) {
		if (!silent)
A
Alexey Fisher 已提交
956
			ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
L
Linus Torvalds 已提交
957 958 959 960
		goto failed_mount;
	}

	if (sb->s_blocksize != sbi->s_frag_size) {
A
Alexey Fisher 已提交
961 962 963
		ext2_msg(sb, KERN_ERR,
			"error: fragsize %lu != blocksize %lu"
			"(not supported yet)",
L
Linus Torvalds 已提交
964 965 966 967 968
			sbi->s_frag_size, sb->s_blocksize);
		goto failed_mount;
	}

	if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
A
Alexey Fisher 已提交
969 970
		ext2_msg(sb, KERN_ERR,
			"error: #blocks per group too big: %lu",
L
Linus Torvalds 已提交
971 972 973 974
			sbi->s_blocks_per_group);
		goto failed_mount;
	}
	if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
A
Alexey Fisher 已提交
975 976
		ext2_msg(sb, KERN_ERR,
			"error: #fragments per group too big: %lu",
L
Linus Torvalds 已提交
977 978 979 980
			sbi->s_frags_per_group);
		goto failed_mount;
	}
	if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
A
Alexey Fisher 已提交
981 982
		ext2_msg(sb, KERN_ERR,
			"error: #inodes per group too big: %lu",
L
Linus Torvalds 已提交
983 984 985 986 987 988
			sbi->s_inodes_per_group);
		goto failed_mount;
	}

	if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
		goto cantfind_ext2;
E
Eric Sandeen 已提交
989 990 991
 	sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
 				le32_to_cpu(es->s_first_data_block) - 1)
 					/ EXT2_BLOCKS_PER_GROUP(sb)) + 1;
L
Linus Torvalds 已提交
992 993 994 995
	db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
		   EXT2_DESC_PER_BLOCK(sb);
	sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
	if (sbi->s_group_desc == NULL) {
A
Alexey Fisher 已提交
996
		ext2_msg(sb, KERN_ERR, "error: not enough memory");
L
Linus Torvalds 已提交
997 998
		goto failed_mount;
	}
999
	bgl_lock_init(sbi->s_blockgroup_lock);
1000
	sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
L
Linus Torvalds 已提交
1001
	if (!sbi->s_debts) {
A
Alexey Fisher 已提交
1002
		ext2_msg(sb, KERN_ERR, "error: not enough memory");
L
Linus Torvalds 已提交
1003 1004 1005 1006 1007 1008 1009 1010
		goto failed_mount_group_desc;
	}
	for (i = 0; i < db_count; i++) {
		block = descriptor_loc(sb, logic_sb_block, i);
		sbi->s_group_desc[i] = sb_bread(sb, block);
		if (!sbi->s_group_desc[i]) {
			for (j = 0; j < i; j++)
				brelse (sbi->s_group_desc[j]);
A
Alexey Fisher 已提交
1011 1012
			ext2_msg(sb, KERN_ERR,
				"error: unable to read group descriptors");
L
Linus Torvalds 已提交
1013 1014 1015 1016
			goto failed_mount_group_desc;
		}
	}
	if (!ext2_check_descriptors (sb)) {
A
Alexey Fisher 已提交
1017
		ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022
		goto failed_mount2;
	}
	sbi->s_gdb_count = db_count;
	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
	spin_lock_init(&sbi->s_next_gen_lock);
1023

M
Martin J. Bligh 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
	/* per fileystem reservation list head & lock */
	spin_lock_init(&sbi->s_rsv_window_lock);
	sbi->s_rsv_window_root = RB_ROOT;
	/*
	 * Add a single, static dummy reservation to the start of the
	 * reservation window list --- it gives us a placeholder for
	 * append-at-start-of-list which makes the allocation logic
	 * _much_ simpler.
	 */
	sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
	sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
	sbi->s_rsv_window_head.rsv_alloc_hit = 0;
	sbi->s_rsv_window_head.rsv_goal_size = 0;
	ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);

1039
	err = percpu_counter_init(&sbi->s_freeblocks_counter,
1040
				ext2_count_free_blocks(sb));
1041 1042
	if (!err) {
		err = percpu_counter_init(&sbi->s_freeinodes_counter,
1043
				ext2_count_free_inodes(sb));
1044 1045 1046
	}
	if (!err) {
		err = percpu_counter_init(&sbi->s_dirs_counter,
1047
				ext2_count_dirs(sb));
1048 1049
	}
	if (err) {
A
Alexey Fisher 已提交
1050
		ext2_msg(sb, KERN_ERR, "error: insufficient memory");
1051 1052
		goto failed_mount3;
	}
L
Linus Torvalds 已提交
1053 1054 1055 1056 1057 1058
	/*
	 * set up enough so that it can read an inode
	 */
	sb->s_op = &ext2_sops;
	sb->s_export_op = &ext2_export_ops;
	sb->s_xattr = ext2_xattr_handlers;
1059 1060 1061
	root = ext2_iget(sb, EXT2_ROOT_INO);
	if (IS_ERR(root)) {
		ret = PTR_ERR(root);
1062
		goto failed_mount3;
L
Linus Torvalds 已提交
1063 1064
	}
	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1065
		iput(root);
A
Alexey Fisher 已提交
1066
		ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
1067
		goto failed_mount3;
L
Linus Torvalds 已提交
1068
	}
1069 1070 1071 1072

	sb->s_root = d_alloc_root(root);
	if (!sb->s_root) {
		iput(root);
A
Alexey Fisher 已提交
1073
		ext2_msg(sb, KERN_ERR, "error: get root inode failed");
1074 1075 1076
		ret = -ENOMEM;
		goto failed_mount3;
	}
L
Linus Torvalds 已提交
1077
	if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
A
Alexey Fisher 已提交
1078 1079
		ext2_msg(sb, KERN_WARNING,
			"warning: mounting ext3 filesystem as ext2");
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084
	ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
	return 0;

cantfind_ext2:
	if (!silent)
A
Alexey Fisher 已提交
1085 1086 1087
		ext2_msg(sb, KERN_ERR,
			"error: can't find an ext2 filesystem on dev %s.",
			sb->s_id);
L
Linus Torvalds 已提交
1088
	goto failed_mount;
1089 1090 1091 1092
failed_mount3:
	percpu_counter_destroy(&sbi->s_freeblocks_counter);
	percpu_counter_destroy(&sbi->s_freeinodes_counter);
	percpu_counter_destroy(&sbi->s_dirs_counter);
L
Linus Torvalds 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
failed_mount2:
	for (i = 0; i < db_count; i++)
		brelse(sbi->s_group_desc[i]);
failed_mount_group_desc:
	kfree(sbi->s_group_desc);
	kfree(sbi->s_debts);
failed_mount:
	brelse(bh);
failed_sbi:
	sb->s_fs_info = NULL;
1103
	kfree(sbi->s_blockgroup_lock);
L
Linus Torvalds 已提交
1104
	kfree(sbi);
1105
	return ret;
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
}

static void ext2_commit_super (struct super_block * sb,
			       struct ext2_super_block * es)
{
	es->s_wtime = cpu_to_le32(get_seconds());
	mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
	sb->s_dirt = 0;
}

static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
{
	es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
	es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
	es->s_wtime = cpu_to_le32(get_seconds());
	mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
	sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
	sb->s_dirt = 0;
}

/*
 * In the second extended file system, it is not necessary to
 * write the super block since we use a mapping of the
 * disk super block in a buffer.
 *
 * However, this function is still used to set the fs valid
 * flags to 0.  We need to set this flag to 0 since the fs
 * may have been checked while mounted and e2fsck may have
 * set s_state to EXT2_VALID_FS after some corrections.
 */

C
Christoph Hellwig 已提交
1137
static int ext2_sync_fs(struct super_block *sb, int wait)
L
Linus Torvalds 已提交
1138
{
C
Christoph Hellwig 已提交
1139 1140
	struct ext2_super_block *es = EXT2_SB(sb)->s_es;

L
Linus Torvalds 已提交
1141
	lock_kernel();
C
Christoph Hellwig 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
	if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) {
		ext2_debug("setting valid to 0\n");
		es->s_state &= cpu_to_le16(~EXT2_VALID_FS);
		es->s_free_blocks_count =
			cpu_to_le32(ext2_count_free_blocks(sb));
		es->s_free_inodes_count =
			cpu_to_le32(ext2_count_free_inodes(sb));
		es->s_mtime = cpu_to_le32(get_seconds());
		ext2_sync_super(sb, es);
	} else {
		ext2_commit_super(sb, es);
L
Linus Torvalds 已提交
1153 1154 1155
	}
	sb->s_dirt = 0;
	unlock_kernel();
C
Christoph Hellwig 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166

	return 0;
}


void ext2_write_super(struct super_block *sb)
{
	if (!(sb->s_flags & MS_RDONLY))
		ext2_sync_fs(sb, 1);
	else
		sb->s_dirt = 0;
L
Linus Torvalds 已提交
1167 1168 1169 1170 1171 1172
}

static int ext2_remount (struct super_block * sb, int * flags, char * data)
{
	struct ext2_sb_info * sbi = EXT2_SB(sb);
	struct ext2_super_block * es;
1173
	unsigned long old_mount_opt = sbi->s_mount_opt;
1174 1175 1176 1177
	struct ext2_mount_options old_opts;
	unsigned long old_sb_flags;
	int err;

1178 1179
	lock_kernel();

1180 1181 1182 1183 1184
	/* Store the old options */
	old_sb_flags = sb->s_flags;
	old_opts.s_mount_opt = sbi->s_mount_opt;
	old_opts.s_resuid = sbi->s_resuid;
	old_opts.s_resgid = sbi->s_resgid;
L
Linus Torvalds 已提交
1185 1186 1187 1188

	/*
	 * Allow the "check" option to be passed as a remount option.
	 */
A
Alexey Fisher 已提交
1189
	if (!parse_options(data, sb)) {
1190 1191 1192
		err = -EINVAL;
		goto restore_opts;
	}
L
Linus Torvalds 已提交
1193 1194 1195 1196

	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
		((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);

1197 1198 1199 1200
	ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
				    EXT2_MOUNT_XIP if not */

	if ((ext2_use_xip(sb)) && (sb->s_blocksize != PAGE_SIZE)) {
A
Alexey Fisher 已提交
1201 1202
		ext2_msg(sb, KERN_WARNING,
			"warning: unsupported blocksize for xip");
1203
		err = -EINVAL;
1204 1205 1206
		goto restore_opts;
	}

L
Linus Torvalds 已提交
1207
	es = sbi->s_es;
1208 1209
	if (((sbi->s_mount_opt & EXT2_MOUNT_XIP) !=
	    (old_mount_opt & EXT2_MOUNT_XIP)) &&
1210
	    invalidate_inodes(sb)) {
A
Alexey Fisher 已提交
1211 1212
		ext2_msg(sb, KERN_WARNING, "warning: refusing change of "
			 "xip flag with busy inodes while remounting");
1213 1214 1215
		sbi->s_mount_opt &= ~EXT2_MOUNT_XIP;
		sbi->s_mount_opt |= old_mount_opt & EXT2_MOUNT_XIP;
	}
1216 1217
	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
		unlock_kernel();
L
Linus Torvalds 已提交
1218
		return 0;
1219
	}
L
Linus Torvalds 已提交
1220 1221
	if (*flags & MS_RDONLY) {
		if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
1222 1223
		    !(sbi->s_mount_state & EXT2_VALID_FS)) {
			unlock_kernel();
L
Linus Torvalds 已提交
1224
			return 0;
1225
		}
L
Linus Torvalds 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
		/*
		 * OK, we are remounting a valid rw partition rdonly, so set
		 * the rdonly flag and then mark the partition as valid again.
		 */
		es->s_state = cpu_to_le16(sbi->s_mount_state);
		es->s_mtime = cpu_to_le32(get_seconds());
	} else {
		__le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
					       ~EXT2_FEATURE_RO_COMPAT_SUPP);
		if (ret) {
A
Alexey Fisher 已提交
1236 1237 1238 1239
			ext2_msg(sb, KERN_WARNING,
				"warning: couldn't remount RDWR because of "
				"unsupported optional features (%x).",
				le32_to_cpu(ret));
1240 1241
			err = -EROFS;
			goto restore_opts;
L
Linus Torvalds 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
		}
		/*
		 * 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.)
		 */
		sbi->s_mount_state = le16_to_cpu(es->s_state);
		if (!ext2_setup_super (sb, es, 0))
			sb->s_flags &= ~MS_RDONLY;
	}
	ext2_sync_super(sb, es);
1253
	unlock_kernel();
L
Linus Torvalds 已提交
1254
	return 0;
1255 1256 1257 1258 1259
restore_opts:
	sbi->s_mount_opt = old_opts.s_mount_opt;
	sbi->s_resuid = old_opts.s_resuid;
	sbi->s_resgid = old_opts.s_resgid;
	sb->s_flags = old_sb_flags;
1260
	unlock_kernel();
1261
	return err;
L
Linus Torvalds 已提交
1262 1263
}

1264
static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
L
Linus Torvalds 已提交
1265
{
1266
	struct super_block *sb = dentry->d_sb;
L
Linus Torvalds 已提交
1267
	struct ext2_sb_info *sbi = EXT2_SB(sb);
P
Pekka Enberg 已提交
1268 1269
	struct ext2_super_block *es = sbi->s_es;
	u64 fsid;
L
Linus Torvalds 已提交
1270 1271

	if (test_opt (sb, MINIX_DF))
B
Badari Pulavarty 已提交
1272 1273 1274 1275 1276
		sbi->s_overhead_last = 0;
	else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
		unsigned long i, overhead = 0;
		smp_rmb();

L
Linus Torvalds 已提交
1277
		/*
B
Badari Pulavarty 已提交
1278 1279 1280
		 * Compute the overhead (FS structures). This is constant
		 * for a given filesystem unless the number of block groups
		 * changes so we cache the previous value until it does.
L
Linus Torvalds 已提交
1281 1282 1283 1284 1285 1286
		 */

		/*
		 * All of the blocks before first_data_block are
		 * overhead
		 */
P
Pekka Enberg 已提交
1287
		overhead = le32_to_cpu(es->s_first_data_block);
L
Linus Torvalds 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303

		/*
		 * 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 < sbi->s_groups_count; i++)
			overhead += ext2_bg_has_super(sb, i) +
				ext2_bg_num_gdb(sb, i);

		/*
		 * Every block group has an inode bitmap, a block
		 * bitmap, and an inode table.
		 */
		overhead += (sbi->s_groups_count *
			     (2 + sbi->s_itb_per_group));
B
Badari Pulavarty 已提交
1304 1305 1306
		sbi->s_overhead_last = overhead;
		smp_wmb();
		sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
L
Linus Torvalds 已提交
1307 1308 1309 1310
	}

	buf->f_type = EXT2_SUPER_MAGIC;
	buf->f_bsize = sb->s_blocksize;
B
Badari Pulavarty 已提交
1311
	buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
L
Linus Torvalds 已提交
1312
	buf->f_bfree = ext2_count_free_blocks(sb);
B
Badari Pulavarty 已提交
1313
	es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
P
Pekka Enberg 已提交
1314 1315
	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))
L
Linus Torvalds 已提交
1316
		buf->f_bavail = 0;
P
Pekka Enberg 已提交
1317 1318
	buf->f_files = le32_to_cpu(es->s_inodes_count);
	buf->f_ffree = ext2_count_free_inodes(sb);
B
Badari Pulavarty 已提交
1319
	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
L
Linus Torvalds 已提交
1320
	buf->f_namelen = EXT2_NAME_LEN;
P
Pekka Enberg 已提交
1321 1322 1323 1324
	fsid = le64_to_cpup((void *)es->s_uuid) ^
	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
L
Linus Torvalds 已提交
1325 1326 1327
	return 0;
}

1328 1329
static int ext2_get_sb(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
L
Linus Torvalds 已提交
1330
{
1331
	return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super, mnt);
L
Linus Torvalds 已提交
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
}

#ifdef CONFIG_QUOTA

/* Read data from quotafile - avoid pagecache and such because we cannot afford
 * acquiring the locks... As quota files are never truncated and quota code
 * itself serializes the operations (and noone else should touch the files)
 * we don't have to be afraid of races */
static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
			       size_t len, loff_t off)
{
	struct inode *inode = sb_dqopt(sb)->files[type];
	sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
	int err = 0;
	int offset = off & (sb->s_blocksize - 1);
	int tocopy;
	size_t toread;
	struct buffer_head tmp_bh;
	struct buffer_head *bh;
	loff_t i_size = i_size_read(inode);

	if (off > i_size)
		return 0;
	if (off+len > i_size)
		len = i_size-off;
	toread = len;
	while (toread > 0) {
		tocopy = sb->s_blocksize - offset < toread ?
				sb->s_blocksize - offset : toread;

		tmp_bh.b_state = 0;
1363
		tmp_bh.b_size = sb->s_blocksize;
L
Linus Torvalds 已提交
1364
		err = ext2_get_block(inode, blk, &tmp_bh, 0);
M
Martin J. Bligh 已提交
1365
		if (err < 0)
L
Linus Torvalds 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
			return err;
		if (!buffer_mapped(&tmp_bh))	/* A hole? */
			memset(data, 0, tocopy);
		else {
			bh = sb_bread(sb, tmp_bh.b_blocknr);
			if (!bh)
				return -EIO;
			memcpy(data, bh->b_data+offset, tocopy);
			brelse(bh);
		}
		offset = 0;
		toread -= tocopy;
		data += tocopy;
		blk++;
	}
	return len;
}

/* Write to quotafile */
static ssize_t ext2_quota_write(struct super_block *sb, int type,
				const char *data, size_t len, loff_t off)
{
	struct inode *inode = sb_dqopt(sb)->files[type];
	sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
	int err = 0;
	int offset = off & (sb->s_blocksize - 1);
	int tocopy;
	size_t towrite = len;
	struct buffer_head tmp_bh;
	struct buffer_head *bh;

1397
	mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
L
Linus Torvalds 已提交
1398 1399 1400 1401 1402 1403
	while (towrite > 0) {
		tocopy = sb->s_blocksize - offset < towrite ?
				sb->s_blocksize - offset : towrite;

		tmp_bh.b_state = 0;
		err = ext2_get_block(inode, blk, &tmp_bh, 1);
M
Martin J. Bligh 已提交
1404
		if (err < 0)
L
Linus Torvalds 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
			goto out;
		if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
			bh = sb_bread(sb, tmp_bh.b_blocknr);
		else
			bh = sb_getblk(sb, tmp_bh.b_blocknr);
		if (!bh) {
			err = -EIO;
			goto out;
		}
		lock_buffer(bh);
		memcpy(bh->b_data+offset, data, tocopy);
		flush_dcache_page(bh->b_page);
		set_buffer_uptodate(bh);
		mark_buffer_dirty(bh);
		unlock_buffer(bh);
		brelse(bh);
		offset = 0;
		towrite -= tocopy;
		data += tocopy;
		blk++;
	}
out:
1427 1428
	if (len == towrite) {
		mutex_unlock(&inode->i_mutex);
L
Linus Torvalds 已提交
1429
		return err;
1430
	}
L
Linus Torvalds 已提交
1431 1432 1433 1434 1435
	if (inode->i_size < off+len-towrite)
		i_size_write(inode, off+len-towrite);
	inode->i_version++;
	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
	mark_inode_dirty(inode);
1436
	mutex_unlock(&inode->i_mutex);
L
Linus Torvalds 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
	return len - towrite;
}

#endif

static struct file_system_type ext2_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "ext2",
	.get_sb		= ext2_get_sb,
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};

static int __init init_ext2_fs(void)
{
	int err = init_ext2_xattr();
	if (err)
		return err;
	err = init_inodecache();
	if (err)
		goto out1;
        err = register_filesystem(&ext2_fs_type);
	if (err)
		goto out;
	return 0;
out:
	destroy_inodecache();
out1:
	exit_ext2_xattr();
	return err;
}

static void __exit exit_ext2_fs(void)
{
	unregister_filesystem(&ext2_fs_type);
	destroy_inodecache();
	exit_ext2_xattr();
}

module_init(init_ext2_fs)
module_exit(exit_ext2_fs)