super.c 44.2 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
#include <linux/vfs.h>
30 31
#include <linux/seq_file.h>
#include <linux/mount.h>
V
vignesh babu 已提交
32
#include <linux/log2.h>
33
#include <linux/quotaops.h>
34
#include <linux/uaccess.h>
35
#include <linux/dax.h>
L
Linus Torvalds 已提交
36 37 38 39
#include "ext2.h"
#include "xattr.h"
#include "acl.h"

40
static void ext2_write_super(struct super_block *sb);
L
Linus Torvalds 已提交
41
static int ext2_remount (struct super_block * sb, int * flags, char * data);
42
static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
C
Christoph Hellwig 已提交
43
static int ext2_sync_fs(struct super_block *sb, int wait);
J
Jan Kara 已提交
44 45
static int ext2_freeze(struct super_block *sb);
static int ext2_unfreeze(struct super_block *sb);
L
Linus Torvalds 已提交
46

47 48
void ext2_error(struct super_block *sb, const char *function,
		const char *fmt, ...)
L
Linus Torvalds 已提交
49
{
50
	struct va_format vaf;
L
Linus Torvalds 已提交
51 52 53 54
	va_list args;
	struct ext2_sb_info *sbi = EXT2_SB(sb);
	struct ext2_super_block *es = sbi->s_es;

55
	if (!sb_rdonly(sb)) {
56
		spin_lock(&sbi->s_lock);
L
Linus Torvalds 已提交
57
		sbi->s_mount_state |= EXT2_ERROR_FS;
58
		es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
59
		spin_unlock(&sbi->s_lock);
60
		ext2_sync_super(sb, es, 1);
L
Linus Torvalds 已提交
61 62 63
	}

	va_start(args, fmt);
64 65 66 67 68 69 70

	vaf.fmt = fmt;
	vaf.va = &args;

	printk(KERN_CRIT "EXT2-fs (%s): error: %s: %pV\n",
	       sb->s_id, function, &vaf);

L
Linus Torvalds 已提交
71 72 73
	va_end(args);

	if (test_opt(sb, ERRORS_PANIC))
A
Alexey Fisher 已提交
74
		panic("EXT2-fs: panic from previous error\n");
L
Linus Torvalds 已提交
75
	if (test_opt(sb, ERRORS_RO)) {
A
Alexey Fisher 已提交
76 77
		ext2_msg(sb, KERN_CRIT,
			     "error: remounting filesystem read-only");
78
		sb->s_flags |= SB_RDONLY;
L
Linus Torvalds 已提交
79 80 81
	}
}

A
Alexey Fisher 已提交
82 83
void ext2_msg(struct super_block *sb, const char *prefix,
		const char *fmt, ...)
L
Linus Torvalds 已提交
84
{
85
	struct va_format vaf;
L
Linus Torvalds 已提交
86 87 88
	va_list args;

	va_start(args, fmt);
89 90 91 92 93 94

	vaf.fmt = fmt;
	vaf.va = &args;

	printk("%sEXT2-fs (%s): %pV\n", prefix, sb->s_id, &vaf);

L
Linus Torvalds 已提交
95 96 97
	va_end(args);
}

98 99 100
/*
 * This must be called with sbi->s_lock held.
 */
L
Linus Torvalds 已提交
101 102 103 104 105 106 107
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 已提交
108 109 110
	ext2_msg(sb, KERN_WARNING,
		     "warning: updating to rev %d because of "
		     "new feature flag, running e2fsck is recommended",
L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		     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.
	 */
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#ifdef CONFIG_QUOTA
static int ext2_quota_off(struct super_block *sb, int type);

static void ext2_quota_off_umount(struct super_block *sb)
{
	int type;

	for (type = 0; type < MAXQUOTAS; type++)
		ext2_quota_off(sb, type);
}
#else
static inline void ext2_quota_off_umount(struct super_block *sb)
{
}
#endif

L
Linus Torvalds 已提交
142 143 144 145 146 147
static void ext2_put_super (struct super_block * sb)
{
	int db_count;
	int i;
	struct ext2_sb_info *sbi = EXT2_SB(sb);

148
	ext2_quota_off_umount(sb);
149

150 151 152
	if (sbi->s_ea_block_cache) {
		ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
		sbi->s_ea_block_cache = NULL;
J
Jan Kara 已提交
153
	}
154
	if (!sb_rdonly(sb)) {
L
Linus Torvalds 已提交
155 156
		struct ext2_super_block *es = sbi->s_es;

157
		spin_lock(&sbi->s_lock);
L
Linus Torvalds 已提交
158
		es->s_state = cpu_to_le16(sbi->s_mount_state);
159
		spin_unlock(&sbi->s_lock);
160
		ext2_sync_super(sb, es, 1);
L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168 169 170 171 172
	}
	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;
173
	kfree(sbi->s_blockgroup_lock);
174
	fs_put_dax(sbi->s_daxdev);
L
Linus Torvalds 已提交
175 176 177
	kfree(sbi);
}

178
static struct kmem_cache * ext2_inode_cachep;
L
Linus Torvalds 已提交
179 180 181 182

static struct inode *ext2_alloc_inode(struct super_block *sb)
{
	struct ext2_inode_info *ei;
183
	ei = kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
L
Linus Torvalds 已提交
184 185
	if (!ei)
		return NULL;
M
Martin J. Bligh 已提交
186
	ei->i_block_alloc_info = NULL;
L
Linus Torvalds 已提交
187
	ei->vfs_inode.i_version = 1;
J
Jan Kara 已提交
188 189 190 191
#ifdef CONFIG_QUOTA
	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
#endif

L
Linus Torvalds 已提交
192 193 194
	return &ei->vfs_inode;
}

N
Nick Piggin 已提交
195
static void ext2_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
196
{
N
Nick Piggin 已提交
197
	struct inode *inode = container_of(head, struct inode, i_rcu);
L
Linus Torvalds 已提交
198 199 200
	kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
}

N
Nick Piggin 已提交
201 202 203 204 205
static void ext2_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, ext2_i_callback);
}

206
static void init_once(void *foo)
L
Linus Torvalds 已提交
207 208 209
{
	struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;

C
Christoph Lameter 已提交
210
	rwlock_init(&ei->i_meta_lock);
L
Linus Torvalds 已提交
211
#ifdef CONFIG_EXT2_FS_XATTR
C
Christoph Lameter 已提交
212
	init_rwsem(&ei->xattr_sem);
L
Linus Torvalds 已提交
213
#endif
M
Martin J. Bligh 已提交
214
	mutex_init(&ei->truncate_mutex);
R
Ross Zwisler 已提交
215 216 217
#ifdef CONFIG_FS_DAX
	init_rwsem(&ei->dax_sem);
#endif
C
Christoph Lameter 已提交
218
	inode_init_once(&ei->vfs_inode);
L
Linus Torvalds 已提交
219
}
220

221
static int __init init_inodecache(void)
L
Linus Torvalds 已提交
222
{
223 224 225 226 227 228 229
	ext2_inode_cachep = kmem_cache_create_usercopy("ext2_inode_cache",
				sizeof(struct ext2_inode_info), 0,
				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
					SLAB_ACCOUNT),
				offsetof(struct ext2_inode_info, i_data),
				sizeof_field(struct ext2_inode_info, i_data),
				init_once);
L
Linus Torvalds 已提交
230 231 232 233 234 235 236
	if (ext2_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
237 238 239 240 241
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
242
	kmem_cache_destroy(ext2_inode_cachep);
L
Linus Torvalds 已提交
243 244
}

245
static int ext2_show_options(struct seq_file *seq, struct dentry *root)
246
{
247
	struct super_block *sb = root->d_sb;
M
Miklos Szeredi 已提交
248 249 250
	struct ext2_sb_info *sbi = EXT2_SB(sb);
	struct ext2_super_block *es = sbi->s_es;
	unsigned long def_mount_opts;
251

252
	spin_lock(&sbi->s_lock);
M
Miklos Szeredi 已提交
253 254 255 256 257 258 259
	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))
260
		seq_puts(seq, ",grpid");
M
Miklos Szeredi 已提交
261 262
	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
		seq_puts(seq, ",nogrpid");
263
	if (!uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT2_DEF_RESUID)) ||
M
Miklos Szeredi 已提交
264
	    le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
265 266
		seq_printf(seq, ",resuid=%u",
				from_kuid_munged(&init_user_ns, sbi->s_resuid));
M
Miklos Szeredi 已提交
267
	}
268
	if (!gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT2_DEF_RESGID)) ||
M
Miklos Szeredi 已提交
269
	    le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
270 271
		seq_printf(seq, ",resgid=%u",
				from_kgid_munged(&init_user_ns, sbi->s_resgid));
M
Miklos Szeredi 已提交
272
	}
273
	if (test_opt(sb, ERRORS_RO)) {
M
Miklos Szeredi 已提交
274 275 276
		int def_errors = le16_to_cpu(es->s_errors);

		if (def_errors == EXT2_ERRORS_PANIC ||
277 278
		    def_errors == EXT2_ERRORS_CONTINUE) {
			seq_puts(seq, ",errors=remount-ro");
M
Miklos Szeredi 已提交
279 280
		}
	}
281 282
	if (test_opt(sb, ERRORS_CONT))
		seq_puts(seq, ",errors=continue");
M
Miklos Szeredi 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	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");
310 311 312 313 314 315 316 317 318

#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

319
#ifdef CONFIG_FS_DAX
320 321
	if (sbi->s_mount_opt & EXT2_MOUNT_XIP)
		seq_puts(seq, ",xip");
322 323
	if (sbi->s_mount_opt & EXT2_MOUNT_DAX)
		seq_puts(seq, ",dax");
324 325
#endif

M
Miklos Szeredi 已提交
326 327 328
	if (!test_opt(sb, RESERVATION))
		seq_puts(seq, ",noreservation");

329
	spin_unlock(&sbi->s_lock);
330 331 332
	return 0;
}

L
Linus Torvalds 已提交
333 334 335
#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);
336 337
static int ext2_quota_on(struct super_block *sb, int type, int format_id,
			 const struct path *path);
J
Jan Kara 已提交
338 339 340 341
static struct dquot **ext2_get_dquots(struct inode *inode)
{
	return EXT2_I(inode)->i_dquot;
}
342 343 344 345 346 347 348 349 350 351 352

static const struct quotactl_ops ext2_quotactl_ops = {
	.quota_on	= ext2_quota_on,
	.quota_off	= ext2_quota_off,
	.quota_sync	= dquot_quota_sync,
	.get_state	= dquot_get_state,
	.set_info	= dquot_set_dqinfo,
	.get_dqblk	= dquot_get_dqblk,
	.set_dqblk	= dquot_set_dqblk,
	.get_nextdqblk	= dquot_get_next_dqblk,
};
L
Linus Torvalds 已提交
353 354
#endif

355
static const struct super_operations ext2_sops = {
L
Linus Torvalds 已提交
356 357 358
	.alloc_inode	= ext2_alloc_inode,
	.destroy_inode	= ext2_destroy_inode,
	.write_inode	= ext2_write_inode,
359
	.evict_inode	= ext2_evict_inode,
L
Linus Torvalds 已提交
360
	.put_super	= ext2_put_super,
C
Christoph Hellwig 已提交
361
	.sync_fs	= ext2_sync_fs,
J
Jan Kara 已提交
362 363
	.freeze_fs	= ext2_freeze,
	.unfreeze_fs	= ext2_unfreeze,
L
Linus Torvalds 已提交
364 365
	.statfs		= ext2_statfs,
	.remount_fs	= ext2_remount,
366
	.show_options	= ext2_show_options,
L
Linus Torvalds 已提交
367 368 369
#ifdef CONFIG_QUOTA
	.quota_read	= ext2_quota_read,
	.quota_write	= ext2_quota_write,
J
Jan Kara 已提交
370
	.get_dquots	= ext2_get_dquots,
L
Linus Torvalds 已提交
371 372 373
#endif
};

C
Christoph Hellwig 已提交
374 375
static struct inode *ext2_nfs_get_inode(struct super_block *sb,
		u64 ino, u32 generation)
376 377 378 379 380 381 382 383
{
	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);

384 385 386 387
	/*
	 * ext2_iget isn't quite right if the inode is currently unallocated!
	 * However ext2_iget currently does appropriate checks to handle stale
	 * inodes so everything is OK.
388
	 */
389 390 391 392
	inode = ext2_iget(sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
	if (generation && inode->i_generation != generation) {
393 394 395 396
		/* we didn't find the right inode.. */
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
C
Christoph Hellwig 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	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);
412 413
}

414
static const struct export_operations ext2_export_ops = {
C
Christoph Hellwig 已提交
415 416
	.fh_to_dentry = ext2_fh_to_dentry,
	.fh_to_parent = ext2_fh_to_parent,
L
Linus Torvalds 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	.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,
442
	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
A
Adrian Bunk 已提交
443
	Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
444
	Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
445
	Opt_acl, Opt_noacl, Opt_xip, Opt_dax, Opt_ignore, Opt_err, Opt_quota,
M
Martin J. Bligh 已提交
446
	Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
L
Linus Torvalds 已提交
447 448
};

449
static const match_table_t tokens = {
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
	{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"},
473
	{Opt_xip, "xip"},
474
	{Opt_dax, "dax"},
475
	{Opt_grpquota, "grpquota"},
L
Linus Torvalds 已提交
476
	{Opt_ignore, "noquota"},
477 478
	{Opt_quota, "quota"},
	{Opt_usrquota, "usrquota"},
M
Martin J. Bligh 已提交
479 480
	{Opt_reservation, "reservation"},
	{Opt_noreservation, "noreservation"},
L
Linus Torvalds 已提交
481 482 483
	{Opt_err, NULL}
};

484 485
static int parse_options(char *options, struct super_block *sb,
			 struct ext2_mount_options *opts)
L
Linus Torvalds 已提交
486
{
A
Alexey Fisher 已提交
487
	char *p;
L
Linus Torvalds 已提交
488 489
	substring_t args[MAX_OPT_ARGS];
	int option;
490 491
	kuid_t uid;
	kgid_t gid;
L
Linus Torvalds 已提交
492 493 494 495 496 497 498 499 500 501 502 503

	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:
504
			clear_opt (opts->s_mount_opt, MINIX_DF);
L
Linus Torvalds 已提交
505 506
			break;
		case Opt_minix_df:
507
			set_opt (opts->s_mount_opt, MINIX_DF);
L
Linus Torvalds 已提交
508 509
			break;
		case Opt_grpid:
510
			set_opt (opts->s_mount_opt, GRPID);
L
Linus Torvalds 已提交
511 512
			break;
		case Opt_nogrpid:
513
			clear_opt (opts->s_mount_opt, GRPID);
L
Linus Torvalds 已提交
514 515 516 517
			break;
		case Opt_resuid:
			if (match_int(&args[0], &option))
				return 0;
518 519 520
			uid = make_kuid(current_user_ns(), option);
			if (!uid_valid(uid)) {
				ext2_msg(sb, KERN_ERR, "Invalid uid value %d", option);
521
				return 0;
522 523

			}
524
			opts->s_resuid = uid;
L
Linus Torvalds 已提交
525 526 527 528
			break;
		case Opt_resgid:
			if (match_int(&args[0], &option))
				return 0;
529 530 531
			gid = make_kgid(current_user_ns(), option);
			if (!gid_valid(gid)) {
				ext2_msg(sb, KERN_ERR, "Invalid gid value %d", option);
532
				return 0;
533
			}
534
			opts->s_resgid = gid;
L
Linus Torvalds 已提交
535 536 537 538 539 540
			break;
		case Opt_sb:
			/* handled by get_sb_block() instead of here */
			/* *sb_block = match_int(&args[0]); */
			break;
		case Opt_err_panic:
541 542 543
			clear_opt (opts->s_mount_opt, ERRORS_CONT);
			clear_opt (opts->s_mount_opt, ERRORS_RO);
			set_opt (opts->s_mount_opt, ERRORS_PANIC);
L
Linus Torvalds 已提交
544 545
			break;
		case Opt_err_ro:
546 547 548
			clear_opt (opts->s_mount_opt, ERRORS_CONT);
			clear_opt (opts->s_mount_opt, ERRORS_PANIC);
			set_opt (opts->s_mount_opt, ERRORS_RO);
L
Linus Torvalds 已提交
549 550
			break;
		case Opt_err_cont:
551 552 553
			clear_opt (opts->s_mount_opt, ERRORS_RO);
			clear_opt (opts->s_mount_opt, ERRORS_PANIC);
			set_opt (opts->s_mount_opt, ERRORS_CONT);
L
Linus Torvalds 已提交
554 555
			break;
		case Opt_nouid32:
556
			set_opt (opts->s_mount_opt, NO_UID32);
L
Linus Torvalds 已提交
557 558
			break;
		case Opt_nocheck:
559
			clear_opt (opts->s_mount_opt, CHECK);
L
Linus Torvalds 已提交
560 561
			break;
		case Opt_debug:
562
			set_opt (opts->s_mount_opt, DEBUG);
L
Linus Torvalds 已提交
563 564
			break;
		case Opt_oldalloc:
565
			set_opt (opts->s_mount_opt, OLDALLOC);
L
Linus Torvalds 已提交
566 567
			break;
		case Opt_orlov:
568
			clear_opt (opts->s_mount_opt, OLDALLOC);
L
Linus Torvalds 已提交
569 570
			break;
		case Opt_nobh:
571
			set_opt (opts->s_mount_opt, NOBH);
L
Linus Torvalds 已提交
572 573 574
			break;
#ifdef CONFIG_EXT2_FS_XATTR
		case Opt_user_xattr:
575
			set_opt (opts->s_mount_opt, XATTR_USER);
L
Linus Torvalds 已提交
576 577
			break;
		case Opt_nouser_xattr:
578
			clear_opt (opts->s_mount_opt, XATTR_USER);
L
Linus Torvalds 已提交
579 580 581 582
			break;
#else
		case Opt_user_xattr:
		case Opt_nouser_xattr:
A
Alexey Fisher 已提交
583 584
			ext2_msg(sb, KERN_INFO, "(no)user_xattr options"
				"not supported");
L
Linus Torvalds 已提交
585 586 587 588
			break;
#endif
#ifdef CONFIG_EXT2_FS_POSIX_ACL
		case Opt_acl:
589
			set_opt(opts->s_mount_opt, POSIX_ACL);
L
Linus Torvalds 已提交
590 591
			break;
		case Opt_noacl:
592
			clear_opt(opts->s_mount_opt, POSIX_ACL);
L
Linus Torvalds 已提交
593 594 595 596
			break;
#else
		case Opt_acl:
		case Opt_noacl:
A
Alexey Fisher 已提交
597 598
			ext2_msg(sb, KERN_INFO,
				"(no)acl options not supported");
L
Linus Torvalds 已提交
599 600
			break;
#endif
601
		case Opt_xip:
602
			ext2_msg(sb, KERN_INFO, "use dax instead of xip");
603
			set_opt(opts->s_mount_opt, XIP);
604 605
			/* Fall through */
		case Opt_dax:
606
#ifdef CONFIG_FS_DAX
607 608
			ext2_msg(sb, KERN_WARNING,
		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
609
			set_opt(opts->s_mount_opt, DAX);
610
#else
611
			ext2_msg(sb, KERN_INFO, "dax option not supported");
612 613
#endif
			break;
614 615 616 617

#if defined(CONFIG_QUOTA)
		case Opt_quota:
		case Opt_usrquota:
618
			set_opt(opts->s_mount_opt, USRQUOTA);
619 620 621
			break;

		case Opt_grpquota:
622
			set_opt(opts->s_mount_opt, GRPQUOTA);
623 624 625 626 627
			break;
#else
		case Opt_quota:
		case Opt_usrquota:
		case Opt_grpquota:
A
Alexey Fisher 已提交
628 629
			ext2_msg(sb, KERN_INFO,
				"quota operations not supported");
630 631 632
			break;
#endif

M
Martin J. Bligh 已提交
633
		case Opt_reservation:
634
			set_opt(opts->s_mount_opt, RESERVATION);
A
Alexey Fisher 已提交
635
			ext2_msg(sb, KERN_INFO, "reservations ON");
M
Martin J. Bligh 已提交
636 637
			break;
		case Opt_noreservation:
638
			clear_opt(opts->s_mount_opt, RESERVATION);
A
Alexey Fisher 已提交
639
			ext2_msg(sb, KERN_INFO, "reservations OFF");
M
Martin J. Bligh 已提交
640
			break;
L
Linus Torvalds 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
		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 已提交
658 659 660
		ext2_msg(sb, KERN_ERR,
			"error: revision level too high, "
			"forcing read-only mode");
661
		res = SB_RDONLY;
L
Linus Torvalds 已提交
662 663 664 665
	}
	if (read_only)
		return res;
	if (!(sbi->s_mount_state & EXT2_VALID_FS))
A
Alexey Fisher 已提交
666 667 668
		ext2_msg(sb, KERN_WARNING,
			"warning: mounting unchecked fs, "
			"running e2fsck is recommended");
L
Linus Torvalds 已提交
669
	else if ((sbi->s_mount_state & EXT2_ERROR_FS))
A
Alexey Fisher 已提交
670 671 672
		ext2_msg(sb, KERN_WARNING,
			"warning: mounting fs with errors, "
			"running e2fsck is recommended");
L
Linus Torvalds 已提交
673 674 675
	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 已提交
676 677 678
		ext2_msg(sb, KERN_WARNING,
			"warning: maximal mount count reached, "
			"running e2fsck is recommended");
L
Linus Torvalds 已提交
679
	else if (le32_to_cpu(es->s_checkinterval) &&
A
Alexey Fisher 已提交
680 681 682 683 684
		(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 已提交
685 686
	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 已提交
687
	le16_add_cpu(&es->s_mnt_count, 1);
L
Linus Torvalds 已提交
688
	if (test_opt (sb, DEBUG))
A
Alexey Fisher 已提交
689 690
		ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, fs=%lu, gc=%lu, "
			"bpg=%lu, ipg=%lu, mo=%04lx]",
L
Linus Torvalds 已提交
691 692 693 694 695 696 697 698 699
			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;
}

700
static int ext2_check_descriptors(struct super_block *sb)
L
Linus Torvalds 已提交
701 702 703 704 705 706
{
	int i;
	struct ext2_sb_info *sbi = EXT2_SB(sb);

	ext2_debug ("Checking group descriptors");

707 708
	for (i = 0; i < sbi->s_groups_count; i++) {
		struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
709 710
		ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
		ext2_fsblk_t last_block;
711

E
Eric Sandeen 已提交
712 713 714 715 716 717 718 719
		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 已提交
720 721 722 723 724 725 726
		{
			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 已提交
727 728
		if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
		    le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
L
Linus Torvalds 已提交
729 730 731 732 733 734 735
		{
			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 已提交
736
		if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
737
		    le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
E
Eric Sandeen 已提交
738
		    last_block)
L
Linus Torvalds 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
		{
			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;
758 759 760 761 762
	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 已提交
763
	 * sectors in the file, including data and all indirect blocks,
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	 * 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 已提交
783 784 785 786 787 788 789

	res += 1LL << (bits-2);
	res += 1LL << (2*(bits-2));
	res += 1LL << (3*(bits-2));
	res <<= bits;
	if (res > upper_limit)
		res = upper_limit;
790 791 792 793

	if (res > MAX_LFS_FILESIZE)
		res = MAX_LFS_FILESIZE;

L
Linus Torvalds 已提交
794 795 796 797 798 799 800 801
	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);
802
	unsigned long bg, first_meta_bg;
L
Linus Torvalds 已提交
803 804 805 806 807 808 809 810 811 812
	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;
813 814

	return ext2_group_first_block_no(sb, bg) + has_super;
L
Linus Torvalds 已提交
815 816 817 818
}

static int ext2_fill_super(struct super_block *sb, void *data, int silent)
{
819
	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827 828
	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;
829
	long ret = -EINVAL;
L
Linus Torvalds 已提交
830 831 832 833
	int blocksize = BLOCK_SIZE;
	int db_count;
	int i, j;
	__le32 features;
834
	int err;
835
	struct ext2_mount_options opts;
L
Linus Torvalds 已提交
836

837
	err = -ENOMEM;
838
	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
L
Linus Torvalds 已提交
839
	if (!sbi)
840
		goto failed;
841 842 843 844 845

	sbi->s_blockgroup_lock =
		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
	if (!sbi->s_blockgroup_lock) {
		kfree(sbi);
846
		goto failed;
847
	}
L
Linus Torvalds 已提交
848
	sb->s_fs_info = sbi;
M
Miklos Szeredi 已提交
849
	sbi->s_sb_block = sb_block;
850
	sbi->s_daxdev = dax_dev;
L
Linus Torvalds 已提交
851

852 853
	spin_lock_init(&sbi->s_lock);

L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862
	/*
	 * 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 已提交
863
		ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
L
Linus Torvalds 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
		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 已提交
879
		ext2_msg(sb, KERN_ERR, "error: unable to read superblock");
L
Linus Torvalds 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
		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)
896
		set_opt(opts.s_mount_opt, DEBUG);
L
Linus Torvalds 已提交
897
	if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
898
		set_opt(opts.s_mount_opt, GRPID);
L
Linus Torvalds 已提交
899
	if (def_mount_opts & EXT2_DEFM_UID16)
900
		set_opt(opts.s_mount_opt, NO_UID32);
901
#ifdef CONFIG_EXT2_FS_XATTR
L
Linus Torvalds 已提交
902
	if (def_mount_opts & EXT2_DEFM_XATTR_USER)
903
		set_opt(opts.s_mount_opt, XATTR_USER);
904 905
#endif
#ifdef CONFIG_EXT2_FS_POSIX_ACL
L
Linus Torvalds 已提交
906
	if (def_mount_opts & EXT2_DEFM_ACL)
907
		set_opt(opts.s_mount_opt, POSIX_ACL);
908
#endif
L
Linus Torvalds 已提交
909 910
	
	if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
911
		set_opt(opts.s_mount_opt, ERRORS_PANIC);
912
	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
913
		set_opt(opts.s_mount_opt, ERRORS_CONT);
914
	else
915
		set_opt(opts.s_mount_opt, ERRORS_RO);
L
Linus Torvalds 已提交
916

917 918
	opts.s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
	opts.s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
L
Linus Torvalds 已提交
919
	
920
	set_opt(opts.s_mount_opt, RESERVATION);
M
Martin J. Bligh 已提交
921

922
	if (!parse_options((char *) data, sb, &opts))
L
Linus Torvalds 已提交
923 924
		goto failed_mount;

925 926 927 928
	sbi->s_mount_opt = opts.s_mount_opt;
	sbi->s_resuid = opts.s_resuid;
	sbi->s_resgid = opts.s_resgid;

929
	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
L
Linus Torvalds 已提交
930
		((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
931
		 SB_POSIXACL : 0);
932
	sb->s_iflags |= SB_I_CGROUPWB;
L
Linus Torvalds 已提交
933 934 935 936 937

	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 已提交
938 939 940
		ext2_msg(sb, KERN_WARNING,
			"warning: feature flags set on rev 0 fs, "
			"running e2fsck is recommended");
L
Linus Torvalds 已提交
941 942 943 944 945 946 947
	/*
	 * 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 已提交
948 949 950
		ext2_msg(sb, KERN_ERR,	"error: couldn't mount because of "
		       "unsupported optional features (%x)",
			le32_to_cpu(features));
L
Linus Torvalds 已提交
951 952
		goto failed_mount;
	}
953
	if (!sb_rdonly(sb) && (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
A
Alexey Fisher 已提交
954 955 956
		ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of "
		       "unsupported optional features (%x)",
		       le32_to_cpu(features));
L
Linus Torvalds 已提交
957 958 959 960 961
		goto failed_mount;
	}

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

962
	if (sbi->s_mount_opt & EXT2_MOUNT_DAX) {
963 964
		err = bdev_dax_supported(sb, blocksize);
		if (err)
965
			goto failed_mount;
966 967
	}

L
Linus Torvalds 已提交
968 969 970 971 972
	/* If the blocksize doesn't match, re-read the thing.. */
	if (sb->s_blocksize != blocksize) {
		brelse(bh);

		if (!sb_set_blocksize(sb, blocksize)) {
973 974
			ext2_msg(sb, KERN_ERR,
				"error: bad blocksize %d", blocksize);
L
Linus Torvalds 已提交
975 976 977 978 979 980 981
			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 已提交
982 983
			ext2_msg(sb, KERN_ERR, "error: couldn't read"
				"superblock on 2nd try");
L
Linus Torvalds 已提交
984 985 986 987 988
			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 已提交
989
			ext2_msg(sb, KERN_ERR, "error: magic mismatch");
L
Linus Torvalds 已提交
990 991 992 993 994
			goto failed_mount;
		}
	}

	sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
995
	sb->s_max_links = EXT2_LINK_MAX;
L
Linus Torvalds 已提交
996 997 998 999 1000 1001 1002 1003

	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 已提交
1004
		    !is_power_of_2(sbi->s_inode_size) ||
L
Linus Torvalds 已提交
1005
		    (sbi->s_inode_size > blocksize)) {
A
Alexey Fisher 已提交
1006 1007
			ext2_msg(sb, KERN_ERR,
				"error: unsupported inode size: %d",
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
				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);
1026
	if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
L
Linus Torvalds 已提交
1027 1028 1029 1030 1031 1032 1033 1034
		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 =
1035
		ilog2 (EXT2_ADDR_PER_BLOCK(sb));
L
Linus Torvalds 已提交
1036
	sbi->s_desc_per_block_bits =
1037
		ilog2 (EXT2_DESC_PER_BLOCK(sb));
L
Linus Torvalds 已提交
1038 1039 1040 1041 1042 1043

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

	if (sb->s_blocksize != bh->b_size) {
		if (!silent)
A
Alexey Fisher 已提交
1044
			ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
L
Linus Torvalds 已提交
1045 1046 1047 1048
		goto failed_mount;
	}

	if (sb->s_blocksize != sbi->s_frag_size) {
A
Alexey Fisher 已提交
1049 1050 1051
		ext2_msg(sb, KERN_ERR,
			"error: fragsize %lu != blocksize %lu"
			"(not supported yet)",
L
Linus Torvalds 已提交
1052 1053 1054 1055 1056
			sbi->s_frag_size, sb->s_blocksize);
		goto failed_mount;
	}

	if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
A
Alexey Fisher 已提交
1057 1058
		ext2_msg(sb, KERN_ERR,
			"error: #blocks per group too big: %lu",
L
Linus Torvalds 已提交
1059 1060 1061 1062
			sbi->s_blocks_per_group);
		goto failed_mount;
	}
	if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
A
Alexey Fisher 已提交
1063 1064
		ext2_msg(sb, KERN_ERR,
			"error: #fragments per group too big: %lu",
L
Linus Torvalds 已提交
1065 1066 1067 1068
			sbi->s_frags_per_group);
		goto failed_mount;
	}
	if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
A
Alexey Fisher 已提交
1069 1070
		ext2_msg(sb, KERN_ERR,
			"error: #inodes per group too big: %lu",
L
Linus Torvalds 已提交
1071 1072 1073 1074 1075 1076
			sbi->s_inodes_per_group);
		goto failed_mount;
	}

	if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
		goto cantfind_ext2;
E
Eric Sandeen 已提交
1077 1078 1079
 	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 已提交
1080 1081 1082 1083
	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 已提交
1084
		ext2_msg(sb, KERN_ERR, "error: not enough memory");
L
Linus Torvalds 已提交
1085 1086
		goto failed_mount;
	}
1087
	bgl_lock_init(sbi->s_blockgroup_lock);
1088
	sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
L
Linus Torvalds 已提交
1089
	if (!sbi->s_debts) {
A
Alexey Fisher 已提交
1090
		ext2_msg(sb, KERN_ERR, "error: not enough memory");
L
Linus Torvalds 已提交
1091 1092 1093 1094 1095 1096 1097 1098
		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 已提交
1099 1100
			ext2_msg(sb, KERN_ERR,
				"error: unable to read group descriptors");
L
Linus Torvalds 已提交
1101 1102 1103 1104
			goto failed_mount_group_desc;
		}
	}
	if (!ext2_check_descriptors (sb)) {
A
Alexey Fisher 已提交
1105
		ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110
		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);
1111

M
Martin J. Bligh 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	/* 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);

1127
	err = percpu_counter_init(&sbi->s_freeblocks_counter,
1128
				ext2_count_free_blocks(sb), GFP_KERNEL);
1129 1130
	if (!err) {
		err = percpu_counter_init(&sbi->s_freeinodes_counter,
1131
				ext2_count_free_inodes(sb), GFP_KERNEL);
1132 1133 1134
	}
	if (!err) {
		err = percpu_counter_init(&sbi->s_dirs_counter,
1135
				ext2_count_dirs(sb), GFP_KERNEL);
1136 1137
	}
	if (err) {
A
Alexey Fisher 已提交
1138
		ext2_msg(sb, KERN_ERR, "error: insufficient memory");
1139 1140
		goto failed_mount3;
	}
J
Jan Kara 已提交
1141 1142

#ifdef CONFIG_EXT2_FS_XATTR
1143 1144 1145
	sbi->s_ea_block_cache = ext2_xattr_create_cache();
	if (!sbi->s_ea_block_cache) {
		ext2_msg(sb, KERN_ERR, "Failed to create ea_block_cache");
J
Jan Kara 已提交
1146 1147 1148
		goto failed_mount3;
	}
#endif
L
Linus Torvalds 已提交
1149 1150 1151 1152 1153 1154
	/*
	 * 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;
1155 1156 1157

#ifdef CONFIG_QUOTA
	sb->dq_op = &dquot_operations;
1158
	sb->s_qcop = &ext2_quotactl_ops;
J
Jan Kara 已提交
1159
	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
1160 1161
#endif

1162 1163 1164
	root = ext2_iget(sb, EXT2_ROOT_INO);
	if (IS_ERR(root)) {
		ret = PTR_ERR(root);
1165
		goto failed_mount3;
L
Linus Torvalds 已提交
1166 1167
	}
	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1168
		iput(root);
A
Alexey Fisher 已提交
1169
		ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
1170
		goto failed_mount3;
L
Linus Torvalds 已提交
1171
	}
1172

1173
	sb->s_root = d_make_root(root);
1174
	if (!sb->s_root) {
A
Alexey Fisher 已提交
1175
		ext2_msg(sb, KERN_ERR, "error: get root inode failed");
1176 1177 1178
		ret = -ENOMEM;
		goto failed_mount3;
	}
L
Linus Torvalds 已提交
1179
	if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
A
Alexey Fisher 已提交
1180 1181
		ext2_msg(sb, KERN_WARNING,
			"warning: mounting ext3 filesystem as ext2");
1182
	if (ext2_setup_super (sb, es, sb_rdonly(sb)))
1183
		sb->s_flags |= SB_RDONLY;
1184
	ext2_write_super(sb);
L
Linus Torvalds 已提交
1185 1186 1187 1188
	return 0;

cantfind_ext2:
	if (!silent)
A
Alexey Fisher 已提交
1189 1190 1191
		ext2_msg(sb, KERN_ERR,
			"error: can't find an ext2 filesystem on dev %s.",
			sb->s_id);
L
Linus Torvalds 已提交
1192
	goto failed_mount;
1193
failed_mount3:
1194 1195
	if (sbi->s_ea_block_cache)
		ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
1196 1197 1198
	percpu_counter_destroy(&sbi->s_freeblocks_counter);
	percpu_counter_destroy(&sbi->s_freeinodes_counter);
	percpu_counter_destroy(&sbi->s_dirs_counter);
L
Linus Torvalds 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
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;
1209
	kfree(sbi->s_blockgroup_lock);
L
Linus Torvalds 已提交
1210
	kfree(sbi);
1211
failed:
1212
	fs_put_dax(dax_dev);
1213
	return ret;
L
Linus Torvalds 已提交
1214 1215
}

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
static void ext2_clear_super_error(struct super_block *sb)
{
	struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;

	if (buffer_write_io_error(sbh)) {
		/*
		 * Oh, dear.  A previous attempt to write the
		 * superblock failed.  This could happen because the
		 * USB device was yanked out.  Or it could happen to
		 * be a transient write error and maybe the block will
		 * be remapped.  Nothing we can do but to retry the
		 * write and hope for the best.
		 */
1229 1230
		ext2_msg(sb, KERN_ERR,
		       "previous I/O error to superblock detected\n");
1231 1232 1233 1234 1235
		clear_buffer_write_io_error(sbh);
		set_buffer_uptodate(sbh);
	}
}

1236 1237
void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es,
		     int wait)
L
Linus Torvalds 已提交
1238
{
1239
	ext2_clear_super_error(sb);
1240
	spin_lock(&EXT2_SB(sb)->s_lock);
L
Linus Torvalds 已提交
1241 1242 1243
	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());
1244 1245
	/* unlock before we do IO */
	spin_unlock(&EXT2_SB(sb)->s_lock);
L
Linus Torvalds 已提交
1246
	mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
1247 1248
	if (wait)
		sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
L
Linus Torvalds 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
}

/*
 * 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 已提交
1261
static int ext2_sync_fs(struct super_block *sb, int wait)
L
Linus Torvalds 已提交
1262
{
1263
	struct ext2_sb_info *sbi = EXT2_SB(sb);
C
Christoph Hellwig 已提交
1264 1265
	struct ext2_super_block *es = EXT2_SB(sb)->s_es;

1266 1267 1268 1269 1270 1271
	/*
	 * Write quota structures to quota file, sync_blockdev() will write
	 * them to disk later
	 */
	dquot_writeback_dquots(sb, -1);

1272
	spin_lock(&sbi->s_lock);
C
Christoph Hellwig 已提交
1273 1274 1275
	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);
L
Linus Torvalds 已提交
1276
	}
1277
	spin_unlock(&sbi->s_lock);
1278
	ext2_sync_super(sb, es, wait);
C
Christoph Hellwig 已提交
1279 1280 1281
	return 0;
}

J
Jan Kara 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
static int ext2_freeze(struct super_block *sb)
{
	struct ext2_sb_info *sbi = EXT2_SB(sb);

	/*
	 * Open but unlinked files present? Keep EXT2_VALID_FS flag cleared
	 * because we have unattached inodes and thus filesystem is not fully
	 * consistent.
	 */
	if (atomic_long_read(&sb->s_remove_count)) {
		ext2_sync_fs(sb, 1);
		return 0;
	}
	/* Set EXT2_FS_VALID flag */
	spin_lock(&sbi->s_lock);
	sbi->s_es->s_state = cpu_to_le16(sbi->s_mount_state);
	spin_unlock(&sbi->s_lock);
	ext2_sync_super(sb, sbi->s_es, 1);

	return 0;
}

static int ext2_unfreeze(struct super_block *sb)
{
	/* Just write sb to clear EXT2_VALID_FS flag */
	ext2_write_super(sb);

	return 0;
}
C
Christoph Hellwig 已提交
1311

1312
static void ext2_write_super(struct super_block *sb)
C
Christoph Hellwig 已提交
1313
{
1314
	if (!sb_rdonly(sb))
C
Christoph Hellwig 已提交
1315
		ext2_sync_fs(sb, 1);
L
Linus Torvalds 已提交
1316 1317 1318 1319 1320 1321
}

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;
1322
	struct ext2_mount_options new_opts;
1323 1324
	int err;

1325
	sync_filesystem(sb);
1326

1327
	spin_lock(&sbi->s_lock);
1328 1329 1330
	new_opts.s_mount_opt = sbi->s_mount_opt;
	new_opts.s_resuid = sbi->s_resuid;
	new_opts.s_resgid = sbi->s_resgid;
1331
	spin_unlock(&sbi->s_lock);
L
Linus Torvalds 已提交
1332 1333 1334 1335

	/*
	 * Allow the "check" option to be passed as a remount option.
	 */
1336
	if (!parse_options(data, sb, &new_opts))
1337
		return -EINVAL;
L
Linus Torvalds 已提交
1338

1339
	spin_lock(&sbi->s_lock);
L
Linus Torvalds 已提交
1340
	es = sbi->s_es;
1341
	if ((sbi->s_mount_opt ^ new_opts.s_mount_opt) & EXT2_MOUNT_DAX) {
A
Alexey Fisher 已提交
1342
		ext2_msg(sb, KERN_WARNING, "warning: refusing change of "
1343
			 "dax flag with busy inodes while remounting");
1344
		new_opts.s_mount_opt ^= EXT2_MOUNT_DAX;
1345
	}
1346
	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1347
		goto out_set;
1348
	if (*flags & SB_RDONLY) {
L
Linus Torvalds 已提交
1349
		if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
1350 1351
		    !(sbi->s_mount_state & EXT2_VALID_FS))
			goto out_set;
1352

L
Linus Torvalds 已提交
1353 1354 1355 1356 1357 1358
		/*
		 * 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());
1359
		spin_unlock(&sbi->s_lock);
1360

1361
		err = dquot_suspend(sb, -1);
1362 1363
		if (err < 0)
			return err;
1364

1365
		ext2_sync_super(sb, es, 1);
L
Linus Torvalds 已提交
1366 1367 1368 1369
	} else {
		__le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
					       ~EXT2_FEATURE_RO_COMPAT_SUPP);
		if (ret) {
1370
			spin_unlock(&sbi->s_lock);
A
Alexey Fisher 已提交
1371 1372 1373 1374
			ext2_msg(sb, KERN_WARNING,
				"warning: couldn't remount RDWR because of "
				"unsupported optional features (%x).",
				le32_to_cpu(ret));
1375
			return -EROFS;
L
Linus Torvalds 已提交
1376 1377 1378 1379 1380 1381 1382 1383
		}
		/*
		 * 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))
1384
			sb->s_flags &= ~SB_RDONLY;
1385
		spin_unlock(&sbi->s_lock);
1386

1387
		ext2_write_super(sb);
1388

1389
		dquot_resume(sb, -1);
L
Linus Torvalds 已提交
1390
	}
1391

1392 1393 1394 1395 1396
	spin_lock(&sbi->s_lock);
out_set:
	sbi->s_mount_opt = new_opts.s_mount_opt;
	sbi->s_resuid = new_opts.s_resuid;
	sbi->s_resgid = new_opts.s_resgid;
1397 1398
	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
		((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? SB_POSIXACL : 0);
1399
	spin_unlock(&sbi->s_lock);
1400 1401

	return 0;
L
Linus Torvalds 已提交
1402 1403
}

1404
static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
L
Linus Torvalds 已提交
1405
{
1406
	struct super_block *sb = dentry->d_sb;
L
Linus Torvalds 已提交
1407
	struct ext2_sb_info *sbi = EXT2_SB(sb);
P
Pekka Enberg 已提交
1408 1409
	struct ext2_super_block *es = sbi->s_es;
	u64 fsid;
L
Linus Torvalds 已提交
1410

1411 1412
	spin_lock(&sbi->s_lock);

L
Linus Torvalds 已提交
1413
	if (test_opt (sb, MINIX_DF))
B
Badari Pulavarty 已提交
1414 1415 1416 1417 1418
		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 已提交
1419
		/*
B
Badari Pulavarty 已提交
1420 1421 1422
		 * 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 已提交
1423 1424 1425 1426 1427 1428
		 */

		/*
		 * All of the blocks before first_data_block are
		 * overhead
		 */
P
Pekka Enberg 已提交
1429
		overhead = le32_to_cpu(es->s_first_data_block);
L
Linus Torvalds 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445

		/*
		 * 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 已提交
1446 1447 1448
		sbi->s_overhead_last = overhead;
		smp_wmb();
		sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
L
Linus Torvalds 已提交
1449 1450 1451 1452
	}

	buf->f_type = EXT2_SUPER_MAGIC;
	buf->f_bsize = sb->s_blocksize;
B
Badari Pulavarty 已提交
1453
	buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
L
Linus Torvalds 已提交
1454
	buf->f_bfree = ext2_count_free_blocks(sb);
B
Badari Pulavarty 已提交
1455
	es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
P
Pekka Enberg 已提交
1456 1457
	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 已提交
1458
		buf->f_bavail = 0;
P
Pekka Enberg 已提交
1459 1460
	buf->f_files = le32_to_cpu(es->s_inodes_count);
	buf->f_ffree = ext2_count_free_inodes(sb);
B
Badari Pulavarty 已提交
1461
	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
L
Linus Torvalds 已提交
1462
	buf->f_namelen = EXT2_NAME_LEN;
P
Pekka Enberg 已提交
1463 1464 1465 1466
	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;
1467
	spin_unlock(&sbi->s_lock);
L
Linus Torvalds 已提交
1468 1469 1470
	return 0;
}

A
Al Viro 已提交
1471 1472
static struct dentry *ext2_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
L
Linus Torvalds 已提交
1473
{
A
Al Viro 已提交
1474
	return mount_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
L
Linus Torvalds 已提交
1475 1476 1477 1478 1479 1480
}

#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
L
Lucas De Marchi 已提交
1481
 * itself serializes the operations (and no one else should touch the files)
L
Linus Torvalds 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
 * 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;
1506
		tmp_bh.b_size = sb->s_blocksize;
L
Linus Torvalds 已提交
1507
		err = ext2_get_block(inode, blk, &tmp_bh, 0);
M
Martin J. Bligh 已提交
1508
		if (err < 0)
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
			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;

	while (towrite > 0) {
		tocopy = sb->s_blocksize - offset < towrite ?
				sb->s_blocksize - offset : towrite;

		tmp_bh.b_state = 0;
1545
		tmp_bh.b_size = sb->s_blocksize;
L
Linus Torvalds 已提交
1546
		err = ext2_get_block(inode, blk, &tmp_bh, 1);
M
Martin J. Bligh 已提交
1547
		if (err < 0)
L
Linus Torvalds 已提交
1548 1549 1550 1551 1552
			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);
1553
		if (unlikely(!bh)) {
L
Linus Torvalds 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
			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:
1570
	if (len == towrite)
L
Linus Torvalds 已提交
1571 1572 1573 1574
		return err;
	if (inode->i_size < off+len-towrite)
		i_size_write(inode, off+len-towrite);
	inode->i_version++;
1575
	inode->i_mtime = inode->i_ctime = current_time(inode);
L
Linus Torvalds 已提交
1576 1577 1578 1579
	mark_inode_dirty(inode);
	return len - towrite;
}

1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
static int ext2_quota_on(struct super_block *sb, int type, int format_id,
			 const struct path *path)
{
	int err;
	struct inode *inode;

	err = dquot_quota_on(sb, type, format_id, path);
	if (err)
		return err;

	inode = d_inode(path->dentry);
	inode_lock(inode);
	EXT2_I(inode)->i_flags |= EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL;
	inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
			S_NOATIME | S_IMMUTABLE);
	inode_unlock(inode);
	mark_inode_dirty(inode);

	return 0;
}

static int ext2_quota_off(struct super_block *sb, int type)
{
	struct inode *inode = sb_dqopt(sb)->files[type];
	int err;

	if (!inode || !igrab(inode))
		goto out;

	err = dquot_quota_off(sb, type);
	if (err)
		goto out_put;

	inode_lock(inode);
	EXT2_I(inode)->i_flags &= ~(EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL);
	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
	inode_unlock(inode);
	mark_inode_dirty(inode);
out_put:
	iput(inode);
	return err;
out:
	return dquot_quota_off(sb, type);
}

L
Linus Torvalds 已提交
1625 1626 1627 1628 1629
#endif

static struct file_system_type ext2_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "ext2",
A
Al Viro 已提交
1630
	.mount		= ext2_mount,
L
Linus Torvalds 已提交
1631
	.kill_sb	= kill_block_super,
1632
	.fs_flags	= FS_REQUIRES_DEV,
L
Linus Torvalds 已提交
1633
};
1634
MODULE_ALIAS_FS("ext2");
L
Linus Torvalds 已提交
1635 1636 1637

static int __init init_ext2_fs(void)
{
J
Jan Kara 已提交
1638 1639
	int err;

L
Linus Torvalds 已提交
1640 1641
	err = init_inodecache();
	if (err)
J
Jan Kara 已提交
1642
		return err;
L
Linus Torvalds 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
        err = register_filesystem(&ext2_fs_type);
	if (err)
		goto out;
	return 0;
out:
	destroy_inodecache();
	return err;
}

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

1658 1659 1660
MODULE_AUTHOR("Remy Card and others");
MODULE_DESCRIPTION("Second Extended Filesystem");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
1661 1662
module_init(init_ext2_fs)
module_exit(exit_ext2_fs)