ARM i.MX6Q: Use ENET_CLK_SEL defines in imx6q_1588_init
[deliverable/linux.git] / fs / ext3 / super.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/ext3/super.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/inode.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 */
18
1da177e4 19#include <linux/module.h>
1da177e4
LT
20#include <linux/blkdev.h>
21#include <linux/parser.h>
a5694255 22#include <linux/exportfs.h>
4613ad18 23#include <linux/statfs.h>
1da177e4
LT
24#include <linux/random.h>
25#include <linux/mount.h>
1da177e4 26#include <linux/quotaops.h>
8fc2751b 27#include <linux/seq_file.h>
3fc74269 28#include <linux/log2.h>
d71bc6db 29#include <linux/cleancache.h>
381be254 30
1da177e4 31#include <asm/uaccess.h>
381be254 32
4613ad18
AV
33#define CREATE_TRACE_POINTS
34
35#include "ext3.h"
1da177e4
LT
36#include "xattr.h"
37#include "acl.h"
381be254 38#include "namei.h"
1da177e4 39
bbae8bcc
LT
40#ifdef CONFIG_EXT3_DEFAULTS_TO_ORDERED
41 #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_ORDERED_DATA
42#else
43 #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_WRITEBACK_DATA
44#endif
45
71b96257
JL
46static int ext3_load_journal(struct super_block *, struct ext3_super_block *,
47 unsigned long journal_devnum);
1da177e4 48static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
eee194e7 49 unsigned int);
c4be0c1d
TS
50static int ext3_commit_super(struct super_block *sb,
51 struct ext3_super_block *es,
1da177e4
LT
52 int sync);
53static void ext3_mark_recovery_complete(struct super_block * sb,
54 struct ext3_super_block * es);
55static void ext3_clear_journal_err(struct super_block * sb,
56 struct ext3_super_block * es);
57static int ext3_sync_fs(struct super_block *sb, int wait);
58static const char *ext3_decode_error(struct super_block * sb, int errno,
59 char nbuf[16]);
60static int ext3_remount (struct super_block * sb, int * flags, char * data);
726c3342 61static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf);
c4be0c1d 62static int ext3_unfreeze(struct super_block *sb);
c4be0c1d 63static int ext3_freeze(struct super_block *sb);
1da177e4 64
ae6ddcc5 65/*
1da177e4 66 * Wrappers for journal_start/end.
1da177e4
LT
67 */
68handle_t *ext3_journal_start_sb(struct super_block *sb, int nblocks)
69{
70 journal_t *journal;
71
72 if (sb->s_flags & MS_RDONLY)
73 return ERR_PTR(-EROFS);
74
75 /* Special case here: if the journal has aborted behind our
76 * backs (eg. EIO in the commit thread), then we still need to
77 * take the FS itself readonly cleanly. */
78 journal = EXT3_SB(sb)->s_journal;
79 if (is_journal_aborted(journal)) {
e05b6b52 80 ext3_abort(sb, __func__,
1da177e4
LT
81 "Detected aborted journal");
82 return ERR_PTR(-EROFS);
83 }
84
85 return journal_start(journal, nblocks);
86}
87
1da177e4
LT
88int __ext3_journal_stop(const char *where, handle_t *handle)
89{
90 struct super_block *sb;
91 int err;
92 int rc;
93
94 sb = handle->h_transaction->t_journal->j_private;
95 err = handle->h_err;
96 rc = journal_stop(handle);
97
98 if (!err)
99 err = rc;
100 if (err)
101 __ext3_std_error(sb, where, err);
102 return err;
103}
104
105void ext3_journal_abort_handle(const char *caller, const char *err_fn,
106 struct buffer_head *bh, handle_t *handle, int err)
107{
108 char nbuf[16];
109 const char *errstr = ext3_decode_error(NULL, err, nbuf);
110
111 if (bh)
112 BUFFER_TRACE(bh, "abort");
113
114 if (!handle->h_err)
115 handle->h_err = err;
116
117 if (is_handle_aborted(handle))
118 return;
119
4cf46b67
AF
120 printk(KERN_ERR "EXT3-fs: %s: aborting transaction: %s in %s\n",
121 caller, errstr, err_fn);
1da177e4
LT
122
123 journal_abort_handle(handle);
124}
125
4cf46b67
AF
126void ext3_msg(struct super_block *sb, const char *prefix,
127 const char *fmt, ...)
128{
99fbb1e2 129 struct va_format vaf;
4cf46b67
AF
130 va_list args;
131
132 va_start(args, fmt);
99fbb1e2
JP
133
134 vaf.fmt = fmt;
135 vaf.va = &args;
136
137 printk("%sEXT3-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
138
4cf46b67
AF
139 va_end(args);
140}
141
1da177e4
LT
142/* Deal with the reporting of failure conditions on a filesystem such as
143 * inconsistencies detected or read IO failures.
144 *
145 * On ext2, we can store the error state of the filesystem in the
146 * superblock. That is not possible on ext3, because we may have other
147 * write ordering constraints on the superblock which prevent us from
148 * writing it out straight away; and given that the journal is about to
149 * be aborted, we can't rely on the current, or future, transactions to
150 * write out the superblock safely.
151 *
152 * We'll just use the journal_abort() error code to record an error in
d6b198bc 153 * the journal instead. On recovery, the journal will complain about
1da177e4
LT
154 * that error until we've noted it down and cleared it.
155 */
156
157static void ext3_handle_error(struct super_block *sb)
158{
159 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
160
161 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
162 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
163
164 if (sb->s_flags & MS_RDONLY)
165 return;
166
7543fc7b 167 if (!test_opt (sb, ERRORS_CONT)) {
1da177e4
LT
168 journal_t *journal = EXT3_SB(sb)->s_journal;
169
e3c96435 170 set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
1da177e4
LT
171 if (journal)
172 journal_abort(journal, -EIO);
173 }
7543fc7b 174 if (test_opt (sb, ERRORS_RO)) {
4cf46b67
AF
175 ext3_msg(sb, KERN_CRIT,
176 "error: remounting filesystem read-only");
3df32196
DM
177 /*
178 * Make sure updated value of ->s_mount_state will be visible
179 * before ->s_flags update.
180 */
181 smp_wmb();
7543fc7b
VA
182 sb->s_flags |= MS_RDONLY;
183 }
184 ext3_commit_super(sb, es, 1);
1da177e4 185 if (test_opt(sb, ERRORS_PANIC))
4cf46b67 186 panic("EXT3-fs (%s): panic forced after error\n",
1da177e4 187 sb->s_id);
1da177e4
LT
188}
189
99fbb1e2
JP
190void ext3_error(struct super_block *sb, const char *function,
191 const char *fmt, ...)
1da177e4 192{
99fbb1e2 193 struct va_format vaf;
1da177e4
LT
194 va_list args;
195
196 va_start(args, fmt);
99fbb1e2
JP
197
198 vaf.fmt = fmt;
199 vaf.va = &args;
200
201 printk(KERN_CRIT "EXT3-fs error (device %s): %s: %pV\n",
202 sb->s_id, function, &vaf);
203
1da177e4
LT
204 va_end(args);
205
206 ext3_handle_error(sb);
207}
208
209static const char *ext3_decode_error(struct super_block * sb, int errno,
210 char nbuf[16])
211{
212 char *errstr = NULL;
213
214 switch (errno) {
215 case -EIO:
216 errstr = "IO failure";
217 break;
218 case -ENOMEM:
219 errstr = "Out of memory";
220 break;
221 case -EROFS:
222 if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
223 errstr = "Journal has aborted";
224 else
225 errstr = "Readonly filesystem";
226 break;
227 default:
228 /* If the caller passed in an extra buffer for unknown
229 * errors, textualise them now. Else we just return
230 * NULL. */
231 if (nbuf) {
232 /* Check for truncated error codes... */
233 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
234 errstr = nbuf;
235 }
236 break;
237 }
238
239 return errstr;
240}
241
242/* __ext3_std_error decodes expected errors from journaling functions
243 * automatically and invokes the appropriate error response. */
244
245void __ext3_std_error (struct super_block * sb, const char * function,
246 int errno)
247{
248 char nbuf[16];
30121624 249 const char *errstr;
1da177e4 250
30121624
ST
251 /* Special case: if the error is EROFS, and we're not already
252 * inside a transaction, then there's really no point in logging
253 * an error. */
254 if (errno == -EROFS && journal_current_handle() == NULL &&
255 (sb->s_flags & MS_RDONLY))
256 return;
257
258 errstr = ext3_decode_error(sb, errno, nbuf);
4cf46b67 259 ext3_msg(sb, KERN_CRIT, "error in %s: %s", function, errstr);
1da177e4
LT
260
261 ext3_handle_error(sb);
262}
263
264/*
265 * ext3_abort is a much stronger failure handler than ext3_error. The
266 * abort function may be used to deal with unrecoverable failures such
267 * as journal IO errors or ENOMEM at a critical moment in log management.
268 *
269 * We unconditionally force the filesystem into an ABORT|READONLY state,
270 * unless the error response on the fs has been set to panic in which
271 * case we take the easy way out and panic immediately.
272 */
273
99fbb1e2
JP
274void ext3_abort(struct super_block *sb, const char *function,
275 const char *fmt, ...)
1da177e4 276{
99fbb1e2 277 struct va_format vaf;
1da177e4
LT
278 va_list args;
279
1da177e4 280 va_start(args, fmt);
99fbb1e2
JP
281
282 vaf.fmt = fmt;
283 vaf.va = &args;
284
285 printk(KERN_CRIT "EXT3-fs (%s): error: %s: %pV\n",
286 sb->s_id, function, &vaf);
287
1da177e4
LT
288 va_end(args);
289
290 if (test_opt(sb, ERRORS_PANIC))
4cf46b67 291 panic("EXT3-fs: panic from previous error\n");
1da177e4
LT
292
293 if (sb->s_flags & MS_RDONLY)
294 return;
295
4cf46b67
AF
296 ext3_msg(sb, KERN_CRIT,
297 "error: remounting filesystem read-only");
1da177e4 298 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
e3c96435 299 set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
3df32196
DM
300 /*
301 * Make sure updated value of ->s_mount_state will be visible
302 * before ->s_flags update.
303 */
304 smp_wmb();
305 sb->s_flags |= MS_RDONLY;
306
44d6f787
HK
307 if (EXT3_SB(sb)->s_journal)
308 journal_abort(EXT3_SB(sb)->s_journal, -EIO);
1da177e4
LT
309}
310
99fbb1e2
JP
311void ext3_warning(struct super_block *sb, const char *function,
312 const char *fmt, ...)
1da177e4 313{
99fbb1e2 314 struct va_format vaf;
1da177e4
LT
315 va_list args;
316
317 va_start(args, fmt);
99fbb1e2
JP
318
319 vaf.fmt = fmt;
320 vaf.va = &args;
321
322 printk(KERN_WARNING "EXT3-fs (%s): warning: %s: %pV\n",
323 sb->s_id, function, &vaf);
324
1da177e4
LT
325 va_end(args);
326}
327
328void ext3_update_dynamic_rev(struct super_block *sb)
329{
330 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
331
332 if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
333 return;
334
4cf46b67
AF
335 ext3_msg(sb, KERN_WARNING,
336 "warning: updating to rev %d because of "
337 "new feature flag, running e2fsck is recommended",
338 EXT3_DYNAMIC_REV);
1da177e4
LT
339
340 es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
341 es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
342 es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
343 /* leave es->s_feature_*compat flags alone */
344 /* es->s_uuid will be set by e2fsck if empty */
345
346 /*
347 * The rest of the superblock fields should be zero, and if not it
348 * means they are likely already in use, so leave them alone. We
349 * can leave it up to e2fsck to clean up any inconsistencies there.
350 */
351}
352
353/*
354 * Open the external journal device
355 */
4cf46b67 356static struct block_device *ext3_blkdev_get(dev_t dev, struct super_block *sb)
1da177e4
LT
357{
358 struct block_device *bdev;
359 char b[BDEVNAME_SIZE];
360
d4d77629 361 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
1da177e4
LT
362 if (IS_ERR(bdev))
363 goto fail;
364 return bdev;
365
366fail:
8d0c2d10 367 ext3_msg(sb, KERN_ERR, "error: failed to open journal device %s: %ld",
4cf46b67
AF
368 __bdevname(dev, b), PTR_ERR(bdev));
369
1da177e4
LT
370 return NULL;
371}
372
373/*
374 * Release the journal device
375 */
4385bab1 376static void ext3_blkdev_put(struct block_device *bdev)
1da177e4 377{
4385bab1 378 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1da177e4
LT
379}
380
4385bab1 381static void ext3_blkdev_remove(struct ext3_sb_info *sbi)
1da177e4
LT
382{
383 struct block_device *bdev;
1da177e4
LT
384 bdev = sbi->journal_bdev;
385 if (bdev) {
4385bab1 386 ext3_blkdev_put(bdev);
1da177e4
LT
387 sbi->journal_bdev = NULL;
388 }
1da177e4
LT
389}
390
391static inline struct inode *orphan_list_entry(struct list_head *l)
392{
393 return &list_entry(l, struct ext3_inode_info, i_orphan)->vfs_inode;
394}
395
396static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
397{
398 struct list_head *l;
399
4cf46b67 400 ext3_msg(sb, KERN_ERR, "error: sb orphan head is %d",
1da177e4
LT
401 le32_to_cpu(sbi->s_es->s_last_orphan));
402
4cf46b67 403 ext3_msg(sb, KERN_ERR, "sb_info orphan list:");
1da177e4
LT
404 list_for_each(l, &sbi->s_orphan) {
405 struct inode *inode = orphan_list_entry(l);
4cf46b67 406 ext3_msg(sb, KERN_ERR, " "
eee194e7 407 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1da177e4 408 inode->i_sb->s_id, inode->i_ino, inode,
ae6ddcc5 409 inode->i_mode, inode->i_nlink,
1da177e4
LT
410 NEXT_ORPHAN(inode));
411 }
412}
413
414static void ext3_put_super (struct super_block * sb)
415{
416 struct ext3_sb_info *sbi = EXT3_SB(sb);
417 struct ext3_super_block *es = sbi->s_es;
44d6f787 418 int i, err;
1da177e4 419
e0ccfd95 420 dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
1da177e4 421 ext3_xattr_put_super(sb);
44d6f787
HK
422 err = journal_destroy(sbi->s_journal);
423 sbi->s_journal = NULL;
424 if (err < 0)
2d7c820e 425 ext3_abort(sb, __func__, "Couldn't clean up the journal");
44d6f787 426
1da177e4
LT
427 if (!(sb->s_flags & MS_RDONLY)) {
428 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
429 es->s_state = cpu_to_le16(sbi->s_mount_state);
430 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
431 mark_buffer_dirty(sbi->s_sbh);
432 ext3_commit_super(sb, es, 1);
433 }
434
435 for (i = 0; i < sbi->s_gdb_count; i++)
436 brelse(sbi->s_group_desc[i]);
437 kfree(sbi->s_group_desc);
438 percpu_counter_destroy(&sbi->s_freeblocks_counter);
439 percpu_counter_destroy(&sbi->s_freeinodes_counter);
440 percpu_counter_destroy(&sbi->s_dirs_counter);
441 brelse(sbi->s_sbh);
442#ifdef CONFIG_QUOTA
443 for (i = 0; i < MAXQUOTAS; i++)
444 kfree(sbi->s_qf_names[i]);
445#endif
446
447 /* Debugging code just in case the in-memory inode orphan list
448 * isn't empty. The on-disk one can be non-empty if we've
449 * detected an error and taken the fs readonly, but the
450 * in-memory list had better be clean by this point. */
451 if (!list_empty(&sbi->s_orphan))
452 dump_orphan_list(sb, sbi);
453 J_ASSERT(list_empty(&sbi->s_orphan));
454
f98393a6 455 invalidate_bdev(sb->s_bdev);
1da177e4
LT
456 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
457 /*
458 * Invalidate the journal device's buffers. We don't want them
459 * floating about in memory - the physical journal device may
460 * hotswapped, and it breaks the `ro-after' testing code.
461 */
462 sync_blockdev(sbi->journal_bdev);
f98393a6 463 invalidate_bdev(sbi->journal_bdev);
1da177e4
LT
464 ext3_blkdev_remove(sbi);
465 }
466 sb->s_fs_info = NULL;
5df096d6 467 kfree(sbi->s_blockgroup_lock);
1da177e4 468 kfree(sbi);
1da177e4
LT
469}
470
e18b890b 471static struct kmem_cache *ext3_inode_cachep;
1da177e4
LT
472
473/*
474 * Called inside transaction, so use GFP_NOFS
475 */
476static struct inode *ext3_alloc_inode(struct super_block *sb)
477{
478 struct ext3_inode_info *ei;
479
e6b4f8da 480 ei = kmem_cache_alloc(ext3_inode_cachep, GFP_NOFS);
1da177e4
LT
481 if (!ei)
482 return NULL;
1da177e4
LT
483 ei->i_block_alloc_info = NULL;
484 ei->vfs_inode.i_version = 1;
fe8bc91c
JK
485 atomic_set(&ei->i_datasync_tid, 0);
486 atomic_set(&ei->i_sync_tid, 0);
1da177e4
LT
487 return &ei->vfs_inode;
488}
489
785c4bcc
LC
490static int ext3_drop_inode(struct inode *inode)
491{
492 int drop = generic_drop_inode(inode);
493
494 trace_ext3_drop_inode(inode, drop);
495 return drop;
496}
497
fa0d7e3d
NP
498static void ext3_i_callback(struct rcu_head *head)
499{
500 struct inode *inode = container_of(head, struct inode, i_rcu);
fa0d7e3d
NP
501 kmem_cache_free(ext3_inode_cachep, EXT3_I(inode));
502}
503
1da177e4
LT
504static void ext3_destroy_inode(struct inode *inode)
505{
9f7dd93d
VA
506 if (!list_empty(&(EXT3_I(inode)->i_orphan))) {
507 printk("EXT3 Inode %p: orphan list check failed!\n",
508 EXT3_I(inode));
509 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
510 EXT3_I(inode), sizeof(struct ext3_inode_info),
511 false);
512 dump_stack();
513 }
fa0d7e3d 514 call_rcu(&inode->i_rcu, ext3_i_callback);
1da177e4
LT
515}
516
51cc5068 517static void init_once(void *foo)
1da177e4
LT
518{
519 struct ext3_inode_info *ei = (struct ext3_inode_info *) foo;
520
a35afb83 521 INIT_LIST_HEAD(&ei->i_orphan);
1da177e4 522#ifdef CONFIG_EXT3_FS_XATTR
a35afb83 523 init_rwsem(&ei->xattr_sem);
1da177e4 524#endif
a35afb83
CL
525 mutex_init(&ei->truncate_mutex);
526 inode_init_once(&ei->vfs_inode);
1da177e4 527}
ae6ddcc5 528
1da177e4
LT
529static int init_inodecache(void)
530{
531 ext3_inode_cachep = kmem_cache_create("ext3_inode_cache",
532 sizeof(struct ext3_inode_info),
fffb60f9
PJ
533 0, (SLAB_RECLAIM_ACCOUNT|
534 SLAB_MEM_SPREAD),
20c2df83 535 init_once);
1da177e4
LT
536 if (ext3_inode_cachep == NULL)
537 return -ENOMEM;
538 return 0;
539}
540
541static void destroy_inodecache(void)
542{
8c0a8537
KS
543 /*
544 * Make sure all delayed rcu free inodes are flushed before we
545 * destroy cache.
546 */
547 rcu_barrier();
1a1d92c1 548 kmem_cache_destroy(ext3_inode_cachep);
1da177e4
LT
549}
550
6cd37cda 551static inline void ext3_show_quota_options(struct seq_file *seq, struct super_block *sb)
8fc2751b 552{
6cd37cda 553#if defined(CONFIG_QUOTA)
275abf5b 554 struct ext3_sb_info *sbi = EXT3_SB(sb);
8fc2751b 555
1aeec434
JK
556 if (sbi->s_jquota_fmt) {
557 char *fmtname = "";
558
559 switch (sbi->s_jquota_fmt) {
560 case QFMT_VFS_OLD:
561 fmtname = "vfsold";
562 break;
563 case QFMT_VFS_V0:
564 fmtname = "vfsv0";
565 break;
566 case QFMT_VFS_V1:
567 fmtname = "vfsv1";
568 break;
569 }
570 seq_printf(seq, ",jqfmt=%s", fmtname);
571 }
8fc2751b
MB
572
573 if (sbi->s_qf_names[USRQUOTA])
574 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
575
576 if (sbi->s_qf_names[GRPQUOTA])
577 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
578
e3c96435 579 if (test_opt(sb, USRQUOTA))
8fc2751b
MB
580 seq_puts(seq, ",usrquota");
581
e3c96435 582 if (test_opt(sb, GRPQUOTA))
8fc2751b
MB
583 seq_puts(seq, ",grpquota");
584#endif
6cd37cda
PO
585}
586
3c4cec65
JK
587static char *data_mode_string(unsigned long mode)
588{
589 switch (mode) {
590 case EXT3_MOUNT_JOURNAL_DATA:
591 return "journal";
592 case EXT3_MOUNT_ORDERED_DATA:
593 return "ordered";
594 case EXT3_MOUNT_WRITEBACK_DATA:
595 return "writeback";
596 }
597 return "unknown";
598}
599
571beed8
MS
600/*
601 * Show an option if
602 * - it's set to a non-default value OR
603 * - if the per-sb default is different from the global default
604 */
34c80b1d 605static int ext3_show_options(struct seq_file *seq, struct dentry *root)
6cd37cda 606{
34c80b1d 607 struct super_block *sb = root->d_sb;
571beed8
MS
608 struct ext3_sb_info *sbi = EXT3_SB(sb);
609 struct ext3_super_block *es = sbi->s_es;
610 unsigned long def_mount_opts;
611
612 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
613
614 if (sbi->s_sb_block != 1)
615 seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
616 if (test_opt(sb, MINIX_DF))
617 seq_puts(seq, ",minixdf");
618 if (test_opt(sb, GRPID))
619 seq_puts(seq, ",grpid");
620 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT3_DEFM_BSDGROUPS))
621 seq_puts(seq, ",nogrpid");
1523299d 622 if (!uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT3_DEF_RESUID)) ||
571beed8 623 le16_to_cpu(es->s_def_resuid) != EXT3_DEF_RESUID) {
1523299d
EB
624 seq_printf(seq, ",resuid=%u",
625 from_kuid_munged(&init_user_ns, sbi->s_resuid));
571beed8 626 }
1523299d 627 if (!gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT3_DEF_RESGID)) ||
571beed8 628 le16_to_cpu(es->s_def_resgid) != EXT3_DEF_RESGID) {
1523299d
EB
629 seq_printf(seq, ",resgid=%u",
630 from_kgid_munged(&init_user_ns, sbi->s_resgid));
571beed8 631 }
1eca93f9 632 if (test_opt(sb, ERRORS_RO)) {
571beed8
MS
633 int def_errors = le16_to_cpu(es->s_errors);
634
635 if (def_errors == EXT3_ERRORS_PANIC ||
1eca93f9
AK
636 def_errors == EXT3_ERRORS_CONTINUE) {
637 seq_puts(seq, ",errors=remount-ro");
571beed8
MS
638 }
639 }
1eca93f9
AK
640 if (test_opt(sb, ERRORS_CONT))
641 seq_puts(seq, ",errors=continue");
571beed8
MS
642 if (test_opt(sb, ERRORS_PANIC))
643 seq_puts(seq, ",errors=panic");
644 if (test_opt(sb, NO_UID32))
645 seq_puts(seq, ",nouid32");
646 if (test_opt(sb, DEBUG))
647 seq_puts(seq, ",debug");
571beed8
MS
648#ifdef CONFIG_EXT3_FS_XATTR
649 if (test_opt(sb, XATTR_USER))
650 seq_puts(seq, ",user_xattr");
651 if (!test_opt(sb, XATTR_USER) &&
652 (def_mount_opts & EXT3_DEFM_XATTR_USER)) {
653 seq_puts(seq, ",nouser_xattr");
654 }
655#endif
656#ifdef CONFIG_EXT3_FS_POSIX_ACL
657 if (test_opt(sb, POSIX_ACL))
658 seq_puts(seq, ",acl");
659 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT3_DEFM_ACL))
660 seq_puts(seq, ",noacl");
661#endif
662 if (!test_opt(sb, RESERVATION))
663 seq_puts(seq, ",noreservation");
664 if (sbi->s_commit_interval) {
665 seq_printf(seq, ",commit=%u",
666 (unsigned) (sbi->s_commit_interval / HZ));
667 }
0636c73e
ES
668
669 /*
670 * Always display barrier state so it's clear what the status is.
671 */
672 seq_puts(seq, ",barrier=");
673 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
e3c96435 674 seq_printf(seq, ",data=%s", data_mode_string(test_opt(sb, DATA_FLAGS)));
0e4fb5e2
HK
675 if (test_opt(sb, DATA_ERR_ABORT))
676 seq_puts(seq, ",data_err=abort");
677
dee1d3b6
ES
678 if (test_opt(sb, NOLOAD))
679 seq_puts(seq, ",norecovery");
680
6cd37cda 681 ext3_show_quota_options(seq, sb);
8fc2751b
MB
682
683 return 0;
684}
1da177e4 685
fdb36673 686
74af0baa
CH
687static struct inode *ext3_nfs_get_inode(struct super_block *sb,
688 u64 ino, u32 generation)
fdb36673 689{
fdb36673 690 struct inode *inode;
fdb36673
N
691
692 if (ino < EXT3_FIRST_INO(sb) && ino != EXT3_ROOT_INO)
693 return ERR_PTR(-ESTALE);
694 if (ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count))
695 return ERR_PTR(-ESTALE);
696
697 /* iget isn't really right if the inode is currently unallocated!!
698 *
699 * ext3_read_inode will return a bad_inode if the inode had been
700 * deleted, so we should be safe.
701 *
702 * Currently we don't know the generation for parent directory, so
703 * a generation of 0 means "accept any"
704 */
473043dc
DH
705 inode = ext3_iget(sb, ino);
706 if (IS_ERR(inode))
707 return ERR_CAST(inode);
708 if (generation && inode->i_generation != generation) {
fdb36673
N
709 iput(inode);
710 return ERR_PTR(-ESTALE);
711 }
74af0baa
CH
712
713 return inode;
714}
715
716static struct dentry *ext3_fh_to_dentry(struct super_block *sb, struct fid *fid,
717 int fh_len, int fh_type)
718{
719 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
720 ext3_nfs_get_inode);
721}
722
723static struct dentry *ext3_fh_to_parent(struct super_block *sb, struct fid *fid,
724 int fh_len, int fh_type)
725{
726 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
727 ext3_nfs_get_inode);
fdb36673
N
728}
729
6b082b53
TO
730/*
731 * Try to release metadata pages (indirect blocks, directories) which are
732 * mapped via the block device. Since these pages could have journal heads
733 * which would prevent try_to_free_buffers() from freeing them, we must use
734 * jbd layer's try_to_free_buffers() function to release them.
735 */
736static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
737 gfp_t wait)
738{
739 journal_t *journal = EXT3_SB(sb)->s_journal;
740
741 WARN_ON(PageChecked(page));
742 if (!page_has_buffers(page))
743 return 0;
744 if (journal)
745 return journal_try_to_free_buffers(journal, page,
746 wait & ~__GFP_WAIT);
747 return try_to_free_buffers(page);
748}
749
8fc2751b 750#ifdef CONFIG_QUOTA
1da177e4
LT
751#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
752#define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
753
1da177e4
LT
754static int ext3_write_dquot(struct dquot *dquot);
755static int ext3_acquire_dquot(struct dquot *dquot);
756static int ext3_release_dquot(struct dquot *dquot);
757static int ext3_mark_dquot_dirty(struct dquot *dquot);
758static int ext3_write_info(struct super_block *sb, int type);
2fd83a4f 759static int ext3_quota_on(struct super_block *sb, int type, int format_id,
f00c9e44 760 struct path *path);
1da177e4
LT
761static int ext3_quota_on_mount(struct super_block *sb, int type);
762static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
763 size_t len, loff_t off);
764static ssize_t ext3_quota_write(struct super_block *sb, int type,
765 const char *data, size_t len, loff_t off);
766
61e225dc 767static const struct dquot_operations ext3_quota_operations = {
1da177e4
LT
768 .write_dquot = ext3_write_dquot,
769 .acquire_dquot = ext3_acquire_dquot,
770 .release_dquot = ext3_release_dquot,
771 .mark_dirty = ext3_mark_dquot_dirty,
157091a2
JK
772 .write_info = ext3_write_info,
773 .alloc_dquot = dquot_alloc,
774 .destroy_dquot = dquot_destroy,
1da177e4
LT
775};
776
0d54b217 777static const struct quotactl_ops ext3_qctl_operations = {
1da177e4 778 .quota_on = ext3_quota_on,
287a8095
CH
779 .quota_off = dquot_quota_off,
780 .quota_sync = dquot_quota_sync,
781 .get_info = dquot_get_dqinfo,
782 .set_info = dquot_set_dqinfo,
783 .get_dqblk = dquot_get_dqblk,
784 .set_dqblk = dquot_set_dqblk
1da177e4
LT
785};
786#endif
787
ee9b6d61 788static const struct super_operations ext3_sops = {
1da177e4
LT
789 .alloc_inode = ext3_alloc_inode,
790 .destroy_inode = ext3_destroy_inode,
1da177e4
LT
791 .write_inode = ext3_write_inode,
792 .dirty_inode = ext3_dirty_inode,
785c4bcc 793 .drop_inode = ext3_drop_inode,
ac14a95b 794 .evict_inode = ext3_evict_inode,
1da177e4 795 .put_super = ext3_put_super,
1da177e4 796 .sync_fs = ext3_sync_fs,
c4be0c1d
TS
797 .freeze_fs = ext3_freeze,
798 .unfreeze_fs = ext3_unfreeze,
1da177e4
LT
799 .statfs = ext3_statfs,
800 .remount_fs = ext3_remount,
8fc2751b 801 .show_options = ext3_show_options,
1da177e4
LT
802#ifdef CONFIG_QUOTA
803 .quota_read = ext3_quota_read,
804 .quota_write = ext3_quota_write,
805#endif
6b082b53 806 .bdev_try_to_free_page = bdev_try_to_free_page,
1da177e4
LT
807};
808
39655164 809static const struct export_operations ext3_export_ops = {
74af0baa
CH
810 .fh_to_dentry = ext3_fh_to_dentry,
811 .fh_to_parent = ext3_fh_to_parent,
1da177e4
LT
812 .get_parent = ext3_get_parent,
813};
814
815enum {
816 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
817 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
2860b733 818 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
1da177e4 819 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
ade1a29e 820 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
71b96257 821 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
1da177e4 822 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
0e4fb5e2 823 Opt_data_err_abort, Opt_data_err_ignore,
1da177e4 824 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1aeec434 825 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
0636c73e
ES
826 Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
827 Opt_resize, Opt_usrquota, Opt_grpquota
1da177e4
LT
828};
829
a447c093 830static const match_table_t tokens = {
1da177e4
LT
831 {Opt_bsd_df, "bsddf"},
832 {Opt_minix_df, "minixdf"},
833 {Opt_grpid, "grpid"},
834 {Opt_grpid, "bsdgroups"},
835 {Opt_nogrpid, "nogrpid"},
836 {Opt_nogrpid, "sysvgroups"},
837 {Opt_resgid, "resgid=%u"},
838 {Opt_resuid, "resuid=%u"},
839 {Opt_sb, "sb=%u"},
840 {Opt_err_cont, "errors=continue"},
841 {Opt_err_panic, "errors=panic"},
842 {Opt_err_ro, "errors=remount-ro"},
843 {Opt_nouid32, "nouid32"},
844 {Opt_nocheck, "nocheck"},
845 {Opt_nocheck, "check=none"},
1da177e4
LT
846 {Opt_debug, "debug"},
847 {Opt_oldalloc, "oldalloc"},
848 {Opt_orlov, "orlov"},
849 {Opt_user_xattr, "user_xattr"},
850 {Opt_nouser_xattr, "nouser_xattr"},
851 {Opt_acl, "acl"},
852 {Opt_noacl, "noacl"},
853 {Opt_reservation, "reservation"},
854 {Opt_noreservation, "noreservation"},
855 {Opt_noload, "noload"},
dee1d3b6 856 {Opt_noload, "norecovery"},
1da177e4 857 {Opt_nobh, "nobh"},
ade1a29e 858 {Opt_bh, "bh"},
1da177e4
LT
859 {Opt_commit, "commit=%u"},
860 {Opt_journal_update, "journal=update"},
861 {Opt_journal_inum, "journal=%u"},
71b96257 862 {Opt_journal_dev, "journal_dev=%u"},
1da177e4
LT
863 {Opt_abort, "abort"},
864 {Opt_data_journal, "data=journal"},
865 {Opt_data_ordered, "data=ordered"},
866 {Opt_data_writeback, "data=writeback"},
0e4fb5e2
HK
867 {Opt_data_err_abort, "data_err=abort"},
868 {Opt_data_err_ignore, "data_err=ignore"},
1da177e4
LT
869 {Opt_offusrjquota, "usrjquota="},
870 {Opt_usrjquota, "usrjquota=%s"},
871 {Opt_offgrpjquota, "grpjquota="},
872 {Opt_grpjquota, "grpjquota=%s"},
873 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
874 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1aeec434 875 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
8fc2751b 876 {Opt_grpquota, "grpquota"},
1f54587b
JK
877 {Opt_noquota, "noquota"},
878 {Opt_quota, "quota"},
8fc2751b 879 {Opt_usrquota, "usrquota"},
1da177e4 880 {Opt_barrier, "barrier=%u"},
0636c73e
ES
881 {Opt_barrier, "barrier"},
882 {Opt_nobarrier, "nobarrier"},
1da177e4 883 {Opt_resize, "resize"},
92587216 884 {Opt_err, NULL},
1da177e4
LT
885};
886
4cf46b67 887static ext3_fsblk_t get_sb_block(void **data, struct super_block *sb)
1da177e4 888{
e9ad5620
DK
889 ext3_fsblk_t sb_block;
890 char *options = (char *) *data;
1da177e4
LT
891
892 if (!options || strncmp(options, "sb=", 3) != 0)
893 return 1; /* Default location */
894 options += 3;
43d23f90 895 /*todo: use simple_strtoll with >32bit ext3 */
1da177e4
LT
896 sb_block = simple_strtoul(options, &options, 0);
897 if (*options && *options != ',') {
8d0c2d10 898 ext3_msg(sb, KERN_ERR, "error: invalid sb specification: %s",
1da177e4
LT
899 (char *) *data);
900 return 1;
901 }
902 if (*options == ',')
903 options++;
904 *data = (void *) options;
905 return sb_block;
906}
907
e1f5c67a
DM
908#ifdef CONFIG_QUOTA
909static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
910{
911 struct ext3_sb_info *sbi = EXT3_SB(sb);
912 char *qname;
913
914 if (sb_any_quota_loaded(sb) &&
915 !sbi->s_qf_names[qtype]) {
916 ext3_msg(sb, KERN_ERR,
917 "Cannot change journaled "
918 "quota options when quota turned on");
919 return 0;
920 }
921 qname = match_strdup(args);
922 if (!qname) {
923 ext3_msg(sb, KERN_ERR,
924 "Not enough memory for storing quotafile name");
925 return 0;
926 }
f56426ae
JK
927 if (sbi->s_qf_names[qtype]) {
928 int same = !strcmp(sbi->s_qf_names[qtype], qname);
929
e1f5c67a 930 kfree(qname);
f56426ae
JK
931 if (!same) {
932 ext3_msg(sb, KERN_ERR,
933 "%s quota file already specified",
934 QTYPE2NAME(qtype));
935 }
936 return same;
e1f5c67a 937 }
f56426ae 938 if (strchr(qname, '/')) {
e1f5c67a
DM
939 ext3_msg(sb, KERN_ERR,
940 "quotafile must be on filesystem root");
f56426ae 941 kfree(qname);
e1f5c67a
DM
942 return 0;
943 }
f56426ae 944 sbi->s_qf_names[qtype] = qname;
e1f5c67a
DM
945 set_opt(sbi->s_mount_opt, QUOTA);
946 return 1;
947}
948
949static int clear_qf_name(struct super_block *sb, int qtype) {
950
951 struct ext3_sb_info *sbi = EXT3_SB(sb);
952
953 if (sb_any_quota_loaded(sb) &&
954 sbi->s_qf_names[qtype]) {
955 ext3_msg(sb, KERN_ERR, "Cannot change journaled quota options"
956 " when quota turned on");
957 return 0;
958 }
f56426ae
JK
959 if (sbi->s_qf_names[qtype]) {
960 kfree(sbi->s_qf_names[qtype]);
961 sbi->s_qf_names[qtype] = NULL;
962 }
e1f5c67a
DM
963 return 1;
964}
965#endif
966
71b96257 967static int parse_options (char *options, struct super_block *sb,
eee194e7 968 unsigned int *inum, unsigned long *journal_devnum,
43d23f90 969 ext3_fsblk_t *n_blocks_count, int is_remount)
1da177e4
LT
970{
971 struct ext3_sb_info *sbi = EXT3_SB(sb);
972 char * p;
973 substring_t args[MAX_OPT_ARGS];
974 int data_opt = 0;
975 int option;
1523299d
EB
976 kuid_t uid;
977 kgid_t gid;
1da177e4 978#ifdef CONFIG_QUOTA
e1f5c67a 979 int qfmt;
1da177e4
LT
980#endif
981
982 if (!options)
983 return 1;
984
985 while ((p = strsep (&options, ",")) != NULL) {
986 int token;
987 if (!*p)
988 continue;
0636c73e
ES
989 /*
990 * Initialize args struct so we know whether arg was
991 * found; some options take optional arguments.
992 */
76f59b3b 993 args[0].to = args[0].from = NULL;
1da177e4
LT
994 token = match_token(p, tokens, args);
995 switch (token) {
996 case Opt_bsd_df:
997 clear_opt (sbi->s_mount_opt, MINIX_DF);
998 break;
999 case Opt_minix_df:
1000 set_opt (sbi->s_mount_opt, MINIX_DF);
1001 break;
1002 case Opt_grpid:
1003 set_opt (sbi->s_mount_opt, GRPID);
1004 break;
1005 case Opt_nogrpid:
1006 clear_opt (sbi->s_mount_opt, GRPID);
1007 break;
1008 case Opt_resuid:
1009 if (match_int(&args[0], &option))
1010 return 0;
1523299d
EB
1011 uid = make_kuid(current_user_ns(), option);
1012 if (!uid_valid(uid)) {
1013 ext3_msg(sb, KERN_ERR, "Invalid uid value %d", option);
66415f6b 1014 return 0;
1523299d
EB
1015
1016 }
1017 sbi->s_resuid = uid;
1da177e4
LT
1018 break;
1019 case Opt_resgid:
1020 if (match_int(&args[0], &option))
1021 return 0;
1523299d
EB
1022 gid = make_kgid(current_user_ns(), option);
1023 if (!gid_valid(gid)) {
1024 ext3_msg(sb, KERN_ERR, "Invalid gid value %d", option);
66415f6b 1025 return 0;
1523299d
EB
1026 }
1027 sbi->s_resgid = gid;
1da177e4
LT
1028 break;
1029 case Opt_sb:
1030 /* handled by get_sb_block() instead of here */
1031 /* *sb_block = match_int(&args[0]); */
1032 break;
1033 case Opt_err_panic:
1034 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1035 clear_opt (sbi->s_mount_opt, ERRORS_RO);
1036 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
1037 break;
1038 case Opt_err_ro:
1039 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1040 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1041 set_opt (sbi->s_mount_opt, ERRORS_RO);
1042 break;
1043 case Opt_err_cont:
1044 clear_opt (sbi->s_mount_opt, ERRORS_RO);
1045 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1046 set_opt (sbi->s_mount_opt, ERRORS_CONT);
1047 break;
1048 case Opt_nouid32:
1049 set_opt (sbi->s_mount_opt, NO_UID32);
1050 break;
1da177e4
LT
1051 case Opt_nocheck:
1052 clear_opt (sbi->s_mount_opt, CHECK);
1053 break;
1054 case Opt_debug:
1055 set_opt (sbi->s_mount_opt, DEBUG);
1056 break;
1057 case Opt_oldalloc:
fbc85402
LC
1058 ext3_msg(sb, KERN_WARNING,
1059 "Ignoring deprecated oldalloc option");
1da177e4
LT
1060 break;
1061 case Opt_orlov:
fbc85402
LC
1062 ext3_msg(sb, KERN_WARNING,
1063 "Ignoring deprecated orlov option");
1da177e4
LT
1064 break;
1065#ifdef CONFIG_EXT3_FS_XATTR
1066 case Opt_user_xattr:
1067 set_opt (sbi->s_mount_opt, XATTR_USER);
1068 break;
1069 case Opt_nouser_xattr:
1070 clear_opt (sbi->s_mount_opt, XATTR_USER);
1071 break;
1072#else
1073 case Opt_user_xattr:
1074 case Opt_nouser_xattr:
4cf46b67
AF
1075 ext3_msg(sb, KERN_INFO,
1076 "(no)user_xattr options not supported");
1da177e4
LT
1077 break;
1078#endif
1079#ifdef CONFIG_EXT3_FS_POSIX_ACL
1080 case Opt_acl:
1081 set_opt(sbi->s_mount_opt, POSIX_ACL);
1082 break;
1083 case Opt_noacl:
1084 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1085 break;
1086#else
1087 case Opt_acl:
1088 case Opt_noacl:
4cf46b67
AF
1089 ext3_msg(sb, KERN_INFO,
1090 "(no)acl options not supported");
1da177e4
LT
1091 break;
1092#endif
1093 case Opt_reservation:
1094 set_opt(sbi->s_mount_opt, RESERVATION);
1095 break;
1096 case Opt_noreservation:
1097 clear_opt(sbi->s_mount_opt, RESERVATION);
1098 break;
1099 case Opt_journal_update:
1100 /* @@@ FIXME */
1101 /* Eventually we will want to be able to create
1102 a journal file here. For now, only allow the
1103 user to specify an existing inode to be the
1104 journal file. */
1105 if (is_remount) {
4cf46b67
AF
1106 ext3_msg(sb, KERN_ERR, "error: cannot specify "
1107 "journal on remount");
1da177e4
LT
1108 return 0;
1109 }
1110 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
1111 break;
1112 case Opt_journal_inum:
1113 if (is_remount) {
4cf46b67
AF
1114 ext3_msg(sb, KERN_ERR, "error: cannot specify "
1115 "journal on remount");
1da177e4
LT
1116 return 0;
1117 }
1118 if (match_int(&args[0], &option))
1119 return 0;
1120 *inum = option;
1121 break;
71b96257
JL
1122 case Opt_journal_dev:
1123 if (is_remount) {
4cf46b67
AF
1124 ext3_msg(sb, KERN_ERR, "error: cannot specify "
1125 "journal on remount");
71b96257
JL
1126 return 0;
1127 }
1128 if (match_int(&args[0], &option))
1129 return 0;
1130 *journal_devnum = option;
1131 break;
1da177e4
LT
1132 case Opt_noload:
1133 set_opt (sbi->s_mount_opt, NOLOAD);
1134 break;
1135 case Opt_commit:
1136 if (match_int(&args[0], &option))
1137 return 0;
1138 if (option < 0)
1139 return 0;
1140 if (option == 0)
1141 option = JBD_DEFAULT_MAX_COMMIT_AGE;
1142 sbi->s_commit_interval = HZ * option;
1143 break;
1144 case Opt_data_journal:
1145 data_opt = EXT3_MOUNT_JOURNAL_DATA;
1146 goto datacheck;
1147 case Opt_data_ordered:
1148 data_opt = EXT3_MOUNT_ORDERED_DATA;
1149 goto datacheck;
1150 case Opt_data_writeback:
1151 data_opt = EXT3_MOUNT_WRITEBACK_DATA;
1152 datacheck:
1153 if (is_remount) {
e3c96435 1154 if (test_opt(sb, DATA_FLAGS) == data_opt)
3c4cec65 1155 break;
4cf46b67
AF
1156 ext3_msg(sb, KERN_ERR,
1157 "error: cannot change "
3c4cec65
JK
1158 "data mode on remount. The filesystem "
1159 "is mounted in data=%s mode and you "
4cf46b67 1160 "try to remount it in data=%s mode.",
e3c96435
DM
1161 data_mode_string(test_opt(sb,
1162 DATA_FLAGS)),
3c4cec65
JK
1163 data_mode_string(data_opt));
1164 return 0;
1da177e4 1165 } else {
e3c96435 1166 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
1da177e4
LT
1167 sbi->s_mount_opt |= data_opt;
1168 }
1169 break;
0e4fb5e2
HK
1170 case Opt_data_err_abort:
1171 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1172 break;
1173 case Opt_data_err_ignore:
1174 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1175 break;
1da177e4
LT
1176#ifdef CONFIG_QUOTA
1177 case Opt_usrjquota:
e1f5c67a 1178 if (!set_qf_name(sb, USRQUOTA, &args[0]))
1da177e4 1179 return 0;
e1f5c67a
DM
1180 break;
1181 case Opt_grpjquota:
1182 if (!set_qf_name(sb, GRPQUOTA, &args[0]))
1da177e4 1183 return 0;
1da177e4
LT
1184 break;
1185 case Opt_offusrjquota:
e1f5c67a
DM
1186 if (!clear_qf_name(sb, USRQUOTA))
1187 return 0;
1188 break;
1da177e4 1189 case Opt_offgrpjquota:
e1f5c67a 1190 if (!clear_qf_name(sb, GRPQUOTA))
1da177e4 1191 return 0;
1da177e4
LT
1192 break;
1193 case Opt_jqfmt_vfsold:
d06bf1d2
JK
1194 qfmt = QFMT_VFS_OLD;
1195 goto set_qf_format;
1da177e4 1196 case Opt_jqfmt_vfsv0:
d06bf1d2 1197 qfmt = QFMT_VFS_V0;
1aeec434
JK
1198 goto set_qf_format;
1199 case Opt_jqfmt_vfsv1:
1200 qfmt = QFMT_VFS_V1;
d06bf1d2 1201set_qf_format:
ee0d5ffe 1202 if (sb_any_quota_loaded(sb) &&
d06bf1d2 1203 sbi->s_jquota_fmt != qfmt) {
4cf46b67 1204 ext3_msg(sb, KERN_ERR, "error: cannot change "
d06bf1d2 1205 "journaled quota options when "
4cf46b67 1206 "quota turned on.");
d06bf1d2
JK
1207 return 0;
1208 }
1209 sbi->s_jquota_fmt = qfmt;
1da177e4 1210 break;
1f54587b 1211 case Opt_quota:
8fc2751b
MB
1212 case Opt_usrquota:
1213 set_opt(sbi->s_mount_opt, QUOTA);
1214 set_opt(sbi->s_mount_opt, USRQUOTA);
1215 break;
1216 case Opt_grpquota:
1f54587b 1217 set_opt(sbi->s_mount_opt, QUOTA);
8fc2751b 1218 set_opt(sbi->s_mount_opt, GRPQUOTA);
1f54587b
JK
1219 break;
1220 case Opt_noquota:
ee0d5ffe 1221 if (sb_any_quota_loaded(sb)) {
4cf46b67
AF
1222 ext3_msg(sb, KERN_ERR, "error: cannot change "
1223 "quota options when quota turned on.");
1f54587b
JK
1224 return 0;
1225 }
1226 clear_opt(sbi->s_mount_opt, QUOTA);
8fc2751b
MB
1227 clear_opt(sbi->s_mount_opt, USRQUOTA);
1228 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1f54587b 1229 break;
1da177e4 1230#else
8fc2751b
MB
1231 case Opt_quota:
1232 case Opt_usrquota:
1233 case Opt_grpquota:
4cf46b67
AF
1234 ext3_msg(sb, KERN_ERR,
1235 "error: quota options not supported.");
fa1ff1e0 1236 break;
1da177e4
LT
1237 case Opt_usrjquota:
1238 case Opt_grpjquota:
1239 case Opt_offusrjquota:
1240 case Opt_offgrpjquota:
1241 case Opt_jqfmt_vfsold:
1242 case Opt_jqfmt_vfsv0:
1aeec434 1243 case Opt_jqfmt_vfsv1:
4cf46b67
AF
1244 ext3_msg(sb, KERN_ERR,
1245 "error: journaled quota options not "
1246 "supported.");
1da177e4 1247 break;
1f54587b
JK
1248 case Opt_noquota:
1249 break;
1da177e4
LT
1250#endif
1251 case Opt_abort:
1252 set_opt(sbi->s_mount_opt, ABORT);
1253 break;
0636c73e
ES
1254 case Opt_nobarrier:
1255 clear_opt(sbi->s_mount_opt, BARRIER);
1256 break;
1da177e4 1257 case Opt_barrier:
0636c73e
ES
1258 if (args[0].from) {
1259 if (match_int(&args[0], &option))
1260 return 0;
1261 } else
1262 option = 1; /* No argument, default to 1 */
1da177e4
LT
1263 if (option)
1264 set_opt(sbi->s_mount_opt, BARRIER);
1265 else
1266 clear_opt(sbi->s_mount_opt, BARRIER);
1267 break;
1268 case Opt_ignore:
1269 break;
1270 case Opt_resize:
08c6a96f 1271 if (!is_remount) {
4cf46b67
AF
1272 ext3_msg(sb, KERN_ERR,
1273 "error: resize option only available "
1274 "for remount");
1da177e4
LT
1275 return 0;
1276 }
c7f1721e
KZ
1277 if (match_int(&args[0], &option) != 0)
1278 return 0;
1da177e4
LT
1279 *n_blocks_count = option;
1280 break;
1281 case Opt_nobh:
4c4d3901
CH
1282 ext3_msg(sb, KERN_WARNING,
1283 "warning: ignoring deprecated nobh option");
1da177e4 1284 break;
ade1a29e 1285 case Opt_bh:
4c4d3901
CH
1286 ext3_msg(sb, KERN_WARNING,
1287 "warning: ignoring deprecated bh option");
ade1a29e 1288 break;
1da177e4 1289 default:
4cf46b67
AF
1290 ext3_msg(sb, KERN_ERR,
1291 "error: unrecognized mount option \"%s\" "
1292 "or missing value", p);
1da177e4
LT
1293 return 0;
1294 }
1295 }
1296#ifdef CONFIG_QUOTA
8fc2751b 1297 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
e3c96435 1298 if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
8fc2751b 1299 clear_opt(sbi->s_mount_opt, USRQUOTA);
e3c96435 1300 if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
8fc2751b
MB
1301 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1302
e1f5c67a 1303 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
4cf46b67
AF
1304 ext3_msg(sb, KERN_ERR, "error: old and new quota "
1305 "format mixing.");
8fc2751b
MB
1306 return 0;
1307 }
1308
1309 if (!sbi->s_jquota_fmt) {
4cf46b67
AF
1310 ext3_msg(sb, KERN_ERR, "error: journaled quota format "
1311 "not specified.");
8fc2751b
MB
1312 return 0;
1313 }
1314 } else {
1315 if (sbi->s_jquota_fmt) {
4cf46b67 1316 ext3_msg(sb, KERN_ERR, "error: journaled quota format "
99aeaf63 1317 "specified with no journaling "
4cf46b67 1318 "enabled.");
8fc2751b
MB
1319 return 0;
1320 }
1da177e4
LT
1321 }
1322#endif
1da177e4
LT
1323 return 1;
1324}
1325
1326static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
1327 int read_only)
1328{
1329 struct ext3_sb_info *sbi = EXT3_SB(sb);
1330 int res = 0;
1331
1332 if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
4cf46b67
AF
1333 ext3_msg(sb, KERN_ERR,
1334 "error: revision level too high, "
1335 "forcing read-only mode");
1da177e4
LT
1336 res = MS_RDONLY;
1337 }
1338 if (read_only)
1339 return res;
1340 if (!(sbi->s_mount_state & EXT3_VALID_FS))
4cf46b67
AF
1341 ext3_msg(sb, KERN_WARNING,
1342 "warning: mounting unchecked fs, "
1343 "running e2fsck is recommended");
1da177e4 1344 else if ((sbi->s_mount_state & EXT3_ERROR_FS))
4cf46b67
AF
1345 ext3_msg(sb, KERN_WARNING,
1346 "warning: mounting fs with errors, "
1347 "running e2fsck is recommended");
df0d6b8f 1348 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
1da177e4 1349 le16_to_cpu(es->s_mnt_count) >=
df0d6b8f 1350 le16_to_cpu(es->s_max_mnt_count))
4cf46b67
AF
1351 ext3_msg(sb, KERN_WARNING,
1352 "warning: maximal mount count reached, "
1353 "running e2fsck is recommended");
1da177e4
LT
1354 else if (le32_to_cpu(es->s_checkinterval) &&
1355 (le32_to_cpu(es->s_lastcheck) +
1356 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
4cf46b67
AF
1357 ext3_msg(sb, KERN_WARNING,
1358 "warning: checktime reached, "
1359 "running e2fsck is recommended");
1da177e4
LT
1360#if 0
1361 /* @@@ We _will_ want to clear the valid bit if we find
1362 inconsistencies, to force a fsck at reboot. But for
1363 a plain journaled filesystem we can keep it set as
1364 valid forever! :) */
e7f23ebd 1365 es->s_state &= cpu_to_le16(~EXT3_VALID_FS);
1da177e4 1366#endif
df0d6b8f 1367 if (!le16_to_cpu(es->s_max_mnt_count))
1da177e4 1368 es->s_max_mnt_count = cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
50e8a289 1369 le16_add_cpu(&es->s_mnt_count, 1);
1da177e4
LT
1370 es->s_mtime = cpu_to_le32(get_seconds());
1371 ext3_update_dynamic_rev(sb);
1372 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1373
1374 ext3_commit_super(sb, es, 1);
1375 if (test_opt(sb, DEBUG))
4cf46b67
AF
1376 ext3_msg(sb, KERN_INFO, "[bs=%lu, gc=%lu, "
1377 "bpg=%lu, ipg=%lu, mo=%04lx]",
1da177e4
LT
1378 sb->s_blocksize,
1379 sbi->s_groups_count,
1380 EXT3_BLOCKS_PER_GROUP(sb),
1381 EXT3_INODES_PER_GROUP(sb),
1382 sbi->s_mount_opt);
1383
1da177e4
LT
1384 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
1385 char b[BDEVNAME_SIZE];
4cf46b67 1386 ext3_msg(sb, KERN_INFO, "using external journal on %s",
1da177e4
LT
1387 bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
1388 } else {
4cf46b67 1389 ext3_msg(sb, KERN_INFO, "using internal journal");
1da177e4 1390 }
d71bc6db 1391 cleancache_init_fs(sb);
1da177e4
LT
1392 return res;
1393}
1394
1395/* Called at mount-time, super-block is locked */
197cd65a 1396static int ext3_check_descriptors(struct super_block *sb)
1da177e4
LT
1397{
1398 struct ext3_sb_info *sbi = EXT3_SB(sb);
1da177e4
LT
1399 int i;
1400
1401 ext3_debug ("Checking group descriptors");
1402
197cd65a
AM
1403 for (i = 0; i < sbi->s_groups_count; i++) {
1404 struct ext3_group_desc *gdp = ext3_get_group_desc(sb, i, NULL);
1eaafeae
AM
1405 ext3_fsblk_t first_block = ext3_group_first_block_no(sb, i);
1406 ext3_fsblk_t last_block;
197cd65a 1407
855565e8
ES
1408 if (i == sbi->s_groups_count - 1)
1409 last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
1410 else
1411 last_block = first_block +
1412 (EXT3_BLOCKS_PER_GROUP(sb) - 1);
1413
855565e8
ES
1414 if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
1415 le32_to_cpu(gdp->bg_block_bitmap) > last_block)
1da177e4
LT
1416 {
1417 ext3_error (sb, "ext3_check_descriptors",
1418 "Block bitmap for group %d"
1419 " not in group (block %lu)!",
1420 i, (unsigned long)
1421 le32_to_cpu(gdp->bg_block_bitmap));
1422 return 0;
1423 }
855565e8
ES
1424 if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
1425 le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
1da177e4
LT
1426 {
1427 ext3_error (sb, "ext3_check_descriptors",
1428 "Inode bitmap for group %d"
1429 " not in group (block %lu)!",
1430 i, (unsigned long)
1431 le32_to_cpu(gdp->bg_inode_bitmap));
1432 return 0;
1433 }
855565e8 1434 if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
780dcdb2 1435 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
855565e8 1436 last_block)
1da177e4
LT
1437 {
1438 ext3_error (sb, "ext3_check_descriptors",
1439 "Inode table for group %d"
1440 " not in group (block %lu)!",
1441 i, (unsigned long)
1442 le32_to_cpu(gdp->bg_inode_table));
1443 return 0;
1444 }
1da177e4
LT
1445 }
1446
1447 sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
1448 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
1449 return 1;
1450}
1451
1452
1453/* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
1454 * the superblock) which were deleted from all directories, but held open by
1455 * a process at the time of a crash. We walk the list and try to delete these
1456 * inodes at recovery time (only with a read-write filesystem).
1457 *
1458 * In order to keep the orphan inode chain consistent during traversal (in
1459 * case of crash during recovery), we link each inode into the superblock
1460 * orphan list_head and handle it the same way as an inode deletion during
1461 * normal operation (which journals the operations for us).
1462 *
1463 * We only do an iget() and an iput() on each inode, which is very safe if we
1464 * accidentally point at an in-use or already deleted inode. The worst that
1465 * can happen in this case is that we get a "bit already cleared" message from
1466 * ext3_free_inode(). The only reason we would point at a wrong inode is if
1467 * e2fsck was run on this filesystem, and it must have already done the orphan
1468 * inode cleanup for us, so we can safely abort without any further action.
1469 */
1470static void ext3_orphan_cleanup (struct super_block * sb,
1471 struct ext3_super_block * es)
1472{
1473 unsigned int s_flags = sb->s_flags;
1474 int nr_orphans = 0, nr_truncates = 0;
1475#ifdef CONFIG_QUOTA
1476 int i;
1477#endif
1478 if (!es->s_last_orphan) {
1479 jbd_debug(4, "no orphan inodes to clean up\n");
1480 return;
1481 }
1482
a8f48a95 1483 if (bdev_read_only(sb->s_bdev)) {
4cf46b67
AF
1484 ext3_msg(sb, KERN_ERR, "error: write access "
1485 "unavailable, skipping orphan cleanup.");
a8f48a95
ES
1486 return;
1487 }
1488
ce654b37
AG
1489 /* Check if feature set allows readwrite operations */
1490 if (EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP)) {
1491 ext3_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
1492 "unknown ROCOMPAT features");
1493 return;
a8f48a95
ES
1494 }
1495
1da177e4 1496 if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
e4230108
ES
1497 /* don't clear list on RO mount w/ errors */
1498 if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
1da177e4
LT
1499 jbd_debug(1, "Errors on filesystem, "
1500 "clearing orphan list.\n");
e4230108
ES
1501 es->s_last_orphan = 0;
1502 }
1da177e4
LT
1503 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1504 return;
1505 }
1506
1507 if (s_flags & MS_RDONLY) {
4cf46b67 1508 ext3_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
1da177e4
LT
1509 sb->s_flags &= ~MS_RDONLY;
1510 }
1511#ifdef CONFIG_QUOTA
1512 /* Needed for iput() to work correctly and not trash data */
1513 sb->s_flags |= MS_ACTIVE;
1514 /* Turn on quotas so that they are updated correctly */
1515 for (i = 0; i < MAXQUOTAS; i++) {
1516 if (EXT3_SB(sb)->s_qf_names[i]) {
1517 int ret = ext3_quota_on_mount(sb, i);
1518 if (ret < 0)
4cf46b67
AF
1519 ext3_msg(sb, KERN_ERR,
1520 "error: cannot turn on journaled "
1521 "quota: %d", ret);
1da177e4
LT
1522 }
1523 }
1524#endif
1525
1526 while (es->s_last_orphan) {
1527 struct inode *inode;
1528
473043dc
DH
1529 inode = ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1530 if (IS_ERR(inode)) {
1da177e4
LT
1531 es->s_last_orphan = 0;
1532 break;
1533 }
1534
1535 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
871a2931 1536 dquot_initialize(inode);
1da177e4
LT
1537 if (inode->i_nlink) {
1538 printk(KERN_DEBUG
eee194e7 1539 "%s: truncating inode %lu to %Ld bytes\n",
e05b6b52 1540 __func__, inode->i_ino, inode->i_size);
eee194e7 1541 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1da177e4
LT
1542 inode->i_ino, inode->i_size);
1543 ext3_truncate(inode);
1544 nr_truncates++;
1545 } else {
1546 printk(KERN_DEBUG
eee194e7 1547 "%s: deleting unreferenced inode %lu\n",
e05b6b52 1548 __func__, inode->i_ino);
eee194e7 1549 jbd_debug(2, "deleting unreferenced inode %lu\n",
1da177e4
LT
1550 inode->i_ino);
1551 nr_orphans++;
1552 }
1553 iput(inode); /* The delete magic happens here! */
1554 }
1555
1556#define PLURAL(x) (x), ((x)==1) ? "" : "s"
1557
1558 if (nr_orphans)
4cf46b67
AF
1559 ext3_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
1560 PLURAL(nr_orphans));
1da177e4 1561 if (nr_truncates)
4cf46b67
AF
1562 ext3_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
1563 PLURAL(nr_truncates));
1da177e4
LT
1564#ifdef CONFIG_QUOTA
1565 /* Turn quotas off */
1566 for (i = 0; i < MAXQUOTAS; i++) {
1567 if (sb_dqopt(sb)->files[i])
287a8095 1568 dquot_quota_off(sb, i);
1da177e4
LT
1569 }
1570#endif
1571 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1572}
1573
1da177e4
LT
1574/*
1575 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1576 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1577 * We need to be 1 filesystem block less than the 2^32 sector limit.
1578 */
1579static loff_t ext3_max_size(int bits)
1580{
1581 loff_t res = EXT3_NDIR_BLOCKS;
fe7fdc37
AK
1582 int meta_blocks;
1583 loff_t upper_limit;
1584
1585 /* This is calculated to be the largest file size for a
1586 * dense, file such that the total number of
1da177e4 1587 * sectors in the file, including data and all indirect blocks,
fe7fdc37
AK
1588 * does not exceed 2^32 -1
1589 * __u32 i_blocks representing the total number of
1590 * 512 bytes blocks of the file
1591 */
1592 upper_limit = (1LL << 32) - 1;
1593
1594 /* total blocks in file system block size */
1595 upper_limit >>= (bits - 9);
1596
1597
1598 /* indirect blocks */
1599 meta_blocks = 1;
1600 /* double indirect blocks */
1601 meta_blocks += 1 + (1LL << (bits-2));
1602 /* tripple indirect blocks */
1603 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1604
1605 upper_limit -= meta_blocks;
1606 upper_limit <<= bits;
1da177e4
LT
1607
1608 res += 1LL << (bits-2);
1609 res += 1LL << (2*(bits-2));
1610 res += 1LL << (3*(bits-2));
1611 res <<= bits;
1612 if (res > upper_limit)
1613 res = upper_limit;
fe7fdc37
AK
1614
1615 if (res > MAX_LFS_FILESIZE)
1616 res = MAX_LFS_FILESIZE;
1617
1da177e4
LT
1618 return res;
1619}
1620
43d23f90
MC
1621static ext3_fsblk_t descriptor_loc(struct super_block *sb,
1622 ext3_fsblk_t logic_sb_block,
1da177e4
LT
1623 int nr)
1624{
1625 struct ext3_sb_info *sbi = EXT3_SB(sb);
43d23f90 1626 unsigned long bg, first_meta_bg;
1da177e4
LT
1627 int has_super = 0;
1628
1da177e4
LT
1629 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1630
1631 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
1632 nr < first_meta_bg)
1633 return (logic_sb_block + nr + 1);
1634 bg = sbi->s_desc_per_block * nr;
1635 if (ext3_bg_has_super(sb, bg))
1636 has_super = 1;
43d23f90 1637 return (has_super + ext3_group_first_block_no(sb, bg));
1da177e4
LT
1638}
1639
1640
1641static int ext3_fill_super (struct super_block *sb, void *data, int silent)
1642{
1643 struct buffer_head * bh;
1644 struct ext3_super_block *es = NULL;
1645 struct ext3_sb_info *sbi;
43d23f90 1646 ext3_fsblk_t block;
4cf46b67 1647 ext3_fsblk_t sb_block = get_sb_block(&data, sb);
43d23f90 1648 ext3_fsblk_t logic_sb_block;
1da177e4 1649 unsigned long offset = 0;
eee194e7 1650 unsigned int journal_inum = 0;
71b96257 1651 unsigned long journal_devnum = 0;
1da177e4
LT
1652 unsigned long def_mount_opts;
1653 struct inode *root;
1654 int blocksize;
1655 int hblock;
1656 int db_count;
1657 int i;
1658 int needs_recovery;
473043dc 1659 int ret = -EINVAL;
1da177e4 1660 __le32 features;
833f4077 1661 int err;
1da177e4 1662
f8314dc6 1663 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1da177e4
LT
1664 if (!sbi)
1665 return -ENOMEM;
5df096d6
PE
1666
1667 sbi->s_blockgroup_lock =
1668 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
1669 if (!sbi->s_blockgroup_lock) {
1670 kfree(sbi);
1671 return -ENOMEM;
1672 }
1da177e4 1673 sb->s_fs_info = sbi;
571beed8 1674 sbi->s_sb_block = sb_block;
1da177e4 1675
1da177e4
LT
1676 blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
1677 if (!blocksize) {
4cf46b67 1678 ext3_msg(sb, KERN_ERR, "error: unable to set blocksize");
1da177e4
LT
1679 goto out_fail;
1680 }
1681
1682 /*
1683 * The ext3 superblock will not be buffer aligned for other than 1kB
1684 * block sizes. We need to calculate the offset from buffer start.
1685 */
1686 if (blocksize != EXT3_MIN_BLOCK_SIZE) {
1687 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1688 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1689 } else {
1690 logic_sb_block = sb_block;
1691 }
1692
1693 if (!(bh = sb_bread(sb, logic_sb_block))) {
4cf46b67 1694 ext3_msg(sb, KERN_ERR, "error: unable to read superblock");
1da177e4
LT
1695 goto out_fail;
1696 }
1697 /*
1698 * Note: s_es must be initialized as soon as possible because
1699 * some ext3 macro-instructions depend on its value
1700 */
57e94d86 1701 es = (struct ext3_super_block *) (bh->b_data + offset);
1da177e4
LT
1702 sbi->s_es = es;
1703 sb->s_magic = le16_to_cpu(es->s_magic);
1704 if (sb->s_magic != EXT3_SUPER_MAGIC)
1705 goto cantfind_ext3;
1706
1707 /* Set defaults before we parse the mount options */
1708 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1709 if (def_mount_opts & EXT3_DEFM_DEBUG)
1710 set_opt(sbi->s_mount_opt, DEBUG);
1711 if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
1712 set_opt(sbi->s_mount_opt, GRPID);
1713 if (def_mount_opts & EXT3_DEFM_UID16)
1714 set_opt(sbi->s_mount_opt, NO_UID32);
2e7842b8 1715#ifdef CONFIG_EXT3_FS_XATTR
1da177e4
LT
1716 if (def_mount_opts & EXT3_DEFM_XATTR_USER)
1717 set_opt(sbi->s_mount_opt, XATTR_USER);
2e7842b8
HD
1718#endif
1719#ifdef CONFIG_EXT3_FS_POSIX_ACL
1da177e4
LT
1720 if (def_mount_opts & EXT3_DEFM_ACL)
1721 set_opt(sbi->s_mount_opt, POSIX_ACL);
2e7842b8 1722#endif
1da177e4 1723 if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
e3c96435 1724 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1da177e4 1725 else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
e3c96435 1726 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1da177e4 1727 else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
e3c96435 1728 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
1da177e4
LT
1729
1730 if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
1731 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1eca93f9 1732 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_CONTINUE)
2245d7c2 1733 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1eca93f9
AK
1734 else
1735 set_opt(sbi->s_mount_opt, ERRORS_RO);
1da177e4 1736
1523299d
EB
1737 sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
1738 sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
1da177e4 1739
00eacd66
CH
1740 /* enable barriers by default */
1741 set_opt(sbi->s_mount_opt, BARRIER);
1da177e4
LT
1742 set_opt(sbi->s_mount_opt, RESERVATION);
1743
71b96257
JL
1744 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1745 NULL, 0))
1da177e4
LT
1746 goto failed_mount;
1747
1748 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
e3c96435 1749 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
1da177e4
LT
1750
1751 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
1752 (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
1753 EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1754 EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
4cf46b67
AF
1755 ext3_msg(sb, KERN_WARNING,
1756 "warning: feature flags set on rev 0 fs, "
1757 "running e2fsck is recommended");
1da177e4
LT
1758 /*
1759 * Check feature flags regardless of the revision level, since we
1760 * previously didn't change the revision level when setting the flags,
1761 * so there is a chance incompat flags are set on a rev 0 filesystem.
1762 */
1763 features = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP);
1764 if (features) {
4cf46b67
AF
1765 ext3_msg(sb, KERN_ERR,
1766 "error: couldn't mount because of unsupported "
1767 "optional features (%x)", le32_to_cpu(features));
1da177e4
LT
1768 goto failed_mount;
1769 }
1770 features = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP);
1771 if (!(sb->s_flags & MS_RDONLY) && features) {
4cf46b67
AF
1772 ext3_msg(sb, KERN_ERR,
1773 "error: couldn't mount RDWR because of unsupported "
1774 "optional features (%x)", le32_to_cpu(features));
1da177e4
LT
1775 goto failed_mount;
1776 }
1777 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1778
1779 if (blocksize < EXT3_MIN_BLOCK_SIZE ||
1780 blocksize > EXT3_MAX_BLOCK_SIZE) {
4cf46b67
AF
1781 ext3_msg(sb, KERN_ERR,
1782 "error: couldn't mount because of unsupported "
1783 "filesystem blocksize %d", blocksize);
1da177e4
LT
1784 goto failed_mount;
1785 }
1786
e1defc4f 1787 hblock = bdev_logical_block_size(sb->s_bdev);
1da177e4
LT
1788 if (sb->s_blocksize != blocksize) {
1789 /*
1790 * Make sure the blocksize for the filesystem is larger
1791 * than the hardware sectorsize for the machine.
1792 */
1793 if (blocksize < hblock) {
4cf46b67
AF
1794 ext3_msg(sb, KERN_ERR,
1795 "error: fsblocksize %d too small for "
1796 "hardware sectorsize %d", blocksize, hblock);
1da177e4
LT
1797 goto failed_mount;
1798 }
1799
1800 brelse (bh);
0f0a89eb 1801 if (!sb_set_blocksize(sb, blocksize)) {
4cf46b67
AF
1802 ext3_msg(sb, KERN_ERR,
1803 "error: bad blocksize %d", blocksize);
0f0a89eb
TS
1804 goto out_fail;
1805 }
1da177e4
LT
1806 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1807 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1808 bh = sb_bread(sb, logic_sb_block);
1809 if (!bh) {
4cf46b67
AF
1810 ext3_msg(sb, KERN_ERR,
1811 "error: can't read superblock on 2nd try");
1da177e4
LT
1812 goto failed_mount;
1813 }
57e94d86 1814 es = (struct ext3_super_block *)(bh->b_data + offset);
1da177e4
LT
1815 sbi->s_es = es;
1816 if (es->s_magic != cpu_to_le16(EXT3_SUPER_MAGIC)) {
4cf46b67
AF
1817 ext3_msg(sb, KERN_ERR,
1818 "error: magic mismatch");
1da177e4
LT
1819 goto failed_mount;
1820 }
1821 }
1822
1823 sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
1824
1825 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
1826 sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
1827 sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
1828 } else {
1829 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1830 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1831 if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
3fc74269 1832 (!is_power_of_2(sbi->s_inode_size)) ||
1da177e4 1833 (sbi->s_inode_size > blocksize)) {
4cf46b67
AF
1834 ext3_msg(sb, KERN_ERR,
1835 "error: unsupported inode size: %d",
1da177e4
LT
1836 sbi->s_inode_size);
1837 goto failed_mount;
1838 }
1839 }
1840 sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
1841 le32_to_cpu(es->s_log_frag_size);
1842 if (blocksize != sbi->s_frag_size) {
4cf46b67
AF
1843 ext3_msg(sb, KERN_ERR,
1844 "error: fragsize %lu != blocksize %u (unsupported)",
1da177e4
LT
1845 sbi->s_frag_size, blocksize);
1846 goto failed_mount;
1847 }
1848 sbi->s_frags_per_block = 1;
1849 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1850 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1851 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
b47b6f38 1852 if (EXT3_INODE_SIZE(sb) == 0 || EXT3_INODES_PER_GROUP(sb) == 0)
1da177e4
LT
1853 goto cantfind_ext3;
1854 sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
1855 if (sbi->s_inodes_per_block == 0)
1856 goto cantfind_ext3;
1857 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1858 sbi->s_inodes_per_block;
1859 sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
1860 sbi->s_sbh = bh;
1861 sbi->s_mount_state = le16_to_cpu(es->s_state);
f0d1b0b3
DH
1862 sbi->s_addr_per_block_bits = ilog2(EXT3_ADDR_PER_BLOCK(sb));
1863 sbi->s_desc_per_block_bits = ilog2(EXT3_DESC_PER_BLOCK(sb));
1da177e4
LT
1864 for (i=0; i < 4; i++)
1865 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1866 sbi->s_def_hash_version = es->s_def_hash_version;
5e1f8c9e
TT
1867 i = le32_to_cpu(es->s_flags);
1868 if (i & EXT2_FLAGS_UNSIGNED_HASH)
1869 sbi->s_hash_unsigned = 3;
1870 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
1871#ifdef __CHAR_UNSIGNED__
1872 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
1873 sbi->s_hash_unsigned = 3;
1874#else
1875 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
1876#endif
5e1f8c9e 1877 }
1da177e4
LT
1878
1879 if (sbi->s_blocks_per_group > blocksize * 8) {
4cf46b67
AF
1880 ext3_msg(sb, KERN_ERR,
1881 "#blocks per group too big: %lu",
1da177e4
LT
1882 sbi->s_blocks_per_group);
1883 goto failed_mount;
1884 }
1885 if (sbi->s_frags_per_group > blocksize * 8) {
4cf46b67
AF
1886 ext3_msg(sb, KERN_ERR,
1887 "error: #fragments per group too big: %lu",
1da177e4
LT
1888 sbi->s_frags_per_group);
1889 goto failed_mount;
1890 }
1891 if (sbi->s_inodes_per_group > blocksize * 8) {
4cf46b67
AF
1892 ext3_msg(sb, KERN_ERR,
1893 "error: #inodes per group too big: %lu",
1da177e4
LT
1894 sbi->s_inodes_per_group);
1895 goto failed_mount;
1896 }
1897
ad692bf3
DW
1898 err = generic_check_addressable(sb->s_blocksize_bits,
1899 le32_to_cpu(es->s_blocks_count));
1900 if (err) {
4cf46b67
AF
1901 ext3_msg(sb, KERN_ERR,
1902 "error: filesystem is too large to mount safely");
fcd5df35 1903 if (sizeof(sector_t) < 8)
4cf46b67
AF
1904 ext3_msg(sb, KERN_ERR,
1905 "error: CONFIG_LBDAF not enabled");
ad692bf3 1906 ret = err;
fcd5df35
MC
1907 goto failed_mount;
1908 }
1909
1da177e4
LT
1910 if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
1911 goto cantfind_ext3;
855565e8
ES
1912 sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
1913 le32_to_cpu(es->s_first_data_block) - 1)
1914 / EXT3_BLOCKS_PER_GROUP(sb)) + 1;
81a4e320 1915 db_count = DIV_ROUND_UP(sbi->s_groups_count, EXT3_DESC_PER_BLOCK(sb));
1da177e4
LT
1916 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1917 GFP_KERNEL);
1918 if (sbi->s_group_desc == NULL) {
4cf46b67
AF
1919 ext3_msg(sb, KERN_ERR,
1920 "error: not enough memory");
4569cd1b 1921 ret = -ENOMEM;
1da177e4
LT
1922 goto failed_mount;
1923 }
1924
5df096d6 1925 bgl_lock_init(sbi->s_blockgroup_lock);
1da177e4
LT
1926
1927 for (i = 0; i < db_count; i++) {
1928 block = descriptor_loc(sb, logic_sb_block, i);
1929 sbi->s_group_desc[i] = sb_bread(sb, block);
1930 if (!sbi->s_group_desc[i]) {
4cf46b67
AF
1931 ext3_msg(sb, KERN_ERR,
1932 "error: can't read group descriptor %d", i);
1da177e4
LT
1933 db_count = i;
1934 goto failed_mount2;
1935 }
1936 }
1937 if (!ext3_check_descriptors (sb)) {
4cf46b67
AF
1938 ext3_msg(sb, KERN_ERR,
1939 "error: group descriptors corrupted");
1da177e4
LT
1940 goto failed_mount2;
1941 }
1942 sbi->s_gdb_count = db_count;
1943 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1944 spin_lock_init(&sbi->s_next_gen_lock);
0216bfcf 1945
1da177e4
LT
1946 /* per fileystem reservation list head & lock */
1947 spin_lock_init(&sbi->s_rsv_window_lock);
1948 sbi->s_rsv_window_root = RB_ROOT;
1949 /* Add a single, static dummy reservation to the start of the
1950 * reservation window list --- it gives us a placeholder for
1951 * append-at-start-of-list which makes the allocation logic
1952 * _much_ simpler. */
1953 sbi->s_rsv_window_head.rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
1954 sbi->s_rsv_window_head.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
1955 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1956 sbi->s_rsv_window_head.rsv_goal_size = 0;
1957 ext3_rsv_window_add(sb, &sbi->s_rsv_window_head);
1958
1959 /*
1960 * set up enough so that it can read an inode
1961 */
1962 sb->s_op = &ext3_sops;
1963 sb->s_export_op = &ext3_export_ops;
1964 sb->s_xattr = ext3_xattr_handlers;
1965#ifdef CONFIG_QUOTA
1966 sb->s_qcop = &ext3_qctl_operations;
1967 sb->dq_op = &ext3_quota_operations;
1968#endif
03cb5f03 1969 memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
1da177e4 1970 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
b8a052d0 1971 mutex_init(&sbi->s_orphan_lock);
96d2a495 1972 mutex_init(&sbi->s_resize_lock);
1da177e4
LT
1973
1974 sb->s_root = NULL;
1975
1976 needs_recovery = (es->s_last_orphan != 0 ||
1977 EXT3_HAS_INCOMPAT_FEATURE(sb,
1978 EXT3_FEATURE_INCOMPAT_RECOVER));
1979
1980 /*
1981 * The first inode we look at is the journal inode. Don't try
1982 * root first: it may be modified in the journal!
1983 */
1984 if (!test_opt(sb, NOLOAD) &&
1985 EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
71b96257 1986 if (ext3_load_journal(sb, es, journal_devnum))
41d1a636 1987 goto failed_mount2;
1da177e4
LT
1988 } else if (journal_inum) {
1989 if (ext3_create_journal(sb, es, journal_inum))
41d1a636 1990 goto failed_mount2;
1da177e4
LT
1991 } else {
1992 if (!silent)
4cf46b67
AF
1993 ext3_msg(sb, KERN_ERR,
1994 "error: no journal found. "
1995 "mounting ext3 over ext2?");
41d1a636
DM
1996 goto failed_mount2;
1997 }
1998 err = percpu_counter_init(&sbi->s_freeblocks_counter,
1999 ext3_count_free_blocks(sb));
2000 if (!err) {
2001 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2002 ext3_count_free_inodes(sb));
2003 }
2004 if (!err) {
2005 err = percpu_counter_init(&sbi->s_dirs_counter,
2006 ext3_count_dirs(sb));
2007 }
2008 if (err) {
2009 ext3_msg(sb, KERN_ERR, "error: insufficient memory");
4569cd1b 2010 ret = err;
0216bfcf 2011 goto failed_mount3;
1da177e4
LT
2012 }
2013
2014 /* We have now updated the journal if required, so we can
2015 * validate the data journaling mode. */
2016 switch (test_opt(sb, DATA_FLAGS)) {
2017 case 0:
2018 /* No mode set, assume a default based on the journal
2019 capabilities: ORDERED_DATA if the journal can
2020 cope, else JOURNAL_DATA */
2021 if (journal_check_available_features
2022 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
bbae8bcc 2023 set_opt(sbi->s_mount_opt, DEFAULT_DATA_MODE);
1da177e4
LT
2024 else
2025 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2026 break;
2027
2028 case EXT3_MOUNT_ORDERED_DATA:
2029 case EXT3_MOUNT_WRITEBACK_DATA:
2030 if (!journal_check_available_features
2031 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
4cf46b67
AF
2032 ext3_msg(sb, KERN_ERR,
2033 "error: journal does not support "
2034 "requested data journaling mode");
41d1a636 2035 goto failed_mount3;
1da177e4
LT
2036 }
2037 default:
2038 break;
2039 }
2040
1da177e4
LT
2041 /*
2042 * The journal_load will have done any necessary log recovery,
2043 * so we can safely mount the rest of the filesystem now.
2044 */
2045
473043dc
DH
2046 root = ext3_iget(sb, EXT3_ROOT_INO);
2047 if (IS_ERR(root)) {
4cf46b67 2048 ext3_msg(sb, KERN_ERR, "error: get root inode failed");
473043dc 2049 ret = PTR_ERR(root);
41d1a636 2050 goto failed_mount3;
1da177e4
LT
2051 }
2052 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
473043dc 2053 iput(root);
4cf46b67 2054 ext3_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
41d1a636 2055 goto failed_mount3;
1da177e4 2056 }
48fde701 2057 sb->s_root = d_make_root(root);
473043dc 2058 if (!sb->s_root) {
4cf46b67 2059 ext3_msg(sb, KERN_ERR, "error: get root dentry failed");
473043dc 2060 ret = -ENOMEM;
41d1a636 2061 goto failed_mount3;
473043dc 2062 }
1da177e4 2063
f007dbf8
ES
2064 if (ext3_setup_super(sb, es, sb->s_flags & MS_RDONLY))
2065 sb->s_flags |= MS_RDONLY;
ed505ee4 2066
1da177e4
LT
2067 EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
2068 ext3_orphan_cleanup(sb, es);
2069 EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
63894ab9
EG
2070 if (needs_recovery) {
2071 ext3_mark_recovery_complete(sb, es);
4cf46b67 2072 ext3_msg(sb, KERN_INFO, "recovery complete");
63894ab9 2073 }
4cf46b67 2074 ext3_msg(sb, KERN_INFO, "mounted filesystem with %s data mode",
1da177e4
LT
2075 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
2076 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2077 "writeback");
2078
1da177e4
LT
2079 return 0;
2080
2081cantfind_ext3:
2082 if (!silent)
4cf46b67
AF
2083 ext3_msg(sb, KERN_INFO,
2084 "error: can't find ext3 filesystem on dev %s.",
1da177e4
LT
2085 sb->s_id);
2086 goto failed_mount;
2087
0216bfcf
MC
2088failed_mount3:
2089 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2090 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2091 percpu_counter_destroy(&sbi->s_dirs_counter);
41d1a636 2092 journal_destroy(sbi->s_journal);
1da177e4
LT
2093failed_mount2:
2094 for (i = 0; i < db_count; i++)
2095 brelse(sbi->s_group_desc[i]);
2096 kfree(sbi->s_group_desc);
2097failed_mount:
2098#ifdef CONFIG_QUOTA
2099 for (i = 0; i < MAXQUOTAS; i++)
2100 kfree(sbi->s_qf_names[i]);
2101#endif
2102 ext3_blkdev_remove(sbi);
2103 brelse(bh);
2104out_fail:
2105 sb->s_fs_info = NULL;
de5ce037 2106 kfree(sbi->s_blockgroup_lock);
1da177e4 2107 kfree(sbi);
473043dc 2108 return ret;
1da177e4
LT
2109}
2110
2111/*
2112 * Setup any per-fs journal parameters now. We'll do this both on
2113 * initial mount, once the journal has been initialised but before we've
ae6ddcc5 2114 * done any recovery; and again on any subsequent remount.
1da177e4
LT
2115 */
2116static void ext3_init_journal_params(struct super_block *sb, journal_t *journal)
2117{
2118 struct ext3_sb_info *sbi = EXT3_SB(sb);
2119
2120 if (sbi->s_commit_interval)
2121 journal->j_commit_interval = sbi->s_commit_interval;
2122 /* We could also set up an ext3-specific default for the commit
2123 * interval here, but for now we'll just fall back to the jbd
2124 * default. */
2125
2126 spin_lock(&journal->j_state_lock);
2127 if (test_opt(sb, BARRIER))
2128 journal->j_flags |= JFS_BARRIER;
2129 else
2130 journal->j_flags &= ~JFS_BARRIER;
0e4fb5e2
HK
2131 if (test_opt(sb, DATA_ERR_ABORT))
2132 journal->j_flags |= JFS_ABORT_ON_SYNCDATA_ERR;
2133 else
2134 journal->j_flags &= ~JFS_ABORT_ON_SYNCDATA_ERR;
1da177e4
LT
2135 spin_unlock(&journal->j_state_lock);
2136}
2137
eee194e7
ES
2138static journal_t *ext3_get_journal(struct super_block *sb,
2139 unsigned int journal_inum)
1da177e4
LT
2140{
2141 struct inode *journal_inode;
2142 journal_t *journal;
2143
2144 /* First, test for the existence of a valid inode on disk. Bad
2145 * things happen if we iget() an unused inode, as the subsequent
2146 * iput() will try to delete it. */
2147
473043dc
DH
2148 journal_inode = ext3_iget(sb, journal_inum);
2149 if (IS_ERR(journal_inode)) {
4cf46b67 2150 ext3_msg(sb, KERN_ERR, "error: no journal found");
1da177e4
LT
2151 return NULL;
2152 }
2153 if (!journal_inode->i_nlink) {
2154 make_bad_inode(journal_inode);
2155 iput(journal_inode);
4cf46b67 2156 ext3_msg(sb, KERN_ERR, "error: journal inode is deleted");
1da177e4
LT
2157 return NULL;
2158 }
2159
2160 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
2161 journal_inode, journal_inode->i_size);
473043dc 2162 if (!S_ISREG(journal_inode->i_mode)) {
4cf46b67 2163 ext3_msg(sb, KERN_ERR, "error: invalid journal inode");
1da177e4
LT
2164 iput(journal_inode);
2165 return NULL;
2166 }
2167
2168 journal = journal_init_inode(journal_inode);
2169 if (!journal) {
4cf46b67 2170 ext3_msg(sb, KERN_ERR, "error: could not load journal inode");
1da177e4
LT
2171 iput(journal_inode);
2172 return NULL;
2173 }
2174 journal->j_private = sb;
2175 ext3_init_journal_params(sb, journal);
2176 return journal;
2177}
2178
2179static journal_t *ext3_get_dev_journal(struct super_block *sb,
2180 dev_t j_dev)
2181{
2182 struct buffer_head * bh;
2183 journal_t *journal;
43d23f90 2184 ext3_fsblk_t start;
1c2bf374 2185 ext3_fsblk_t len;
1da177e4 2186 int hblock, blocksize;
43d23f90 2187 ext3_fsblk_t sb_block;
1da177e4
LT
2188 unsigned long offset;
2189 struct ext3_super_block * es;
2190 struct block_device *bdev;
2191
4cf46b67 2192 bdev = ext3_blkdev_get(j_dev, sb);
1da177e4
LT
2193 if (bdev == NULL)
2194 return NULL;
2195
1da177e4 2196 blocksize = sb->s_blocksize;
e1defc4f 2197 hblock = bdev_logical_block_size(bdev);
1da177e4 2198 if (blocksize < hblock) {
4cf46b67
AF
2199 ext3_msg(sb, KERN_ERR,
2200 "error: blocksize too small for journal device");
1da177e4
LT
2201 goto out_bdev;
2202 }
2203
2204 sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
2205 offset = EXT3_MIN_BLOCK_SIZE % blocksize;
2206 set_blocksize(bdev, blocksize);
2207 if (!(bh = __bread(bdev, sb_block, blocksize))) {
4cf46b67
AF
2208 ext3_msg(sb, KERN_ERR, "error: couldn't read superblock of "
2209 "external journal");
1da177e4
LT
2210 goto out_bdev;
2211 }
2212
57e94d86 2213 es = (struct ext3_super_block *) (bh->b_data + offset);
1da177e4
LT
2214 if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
2215 !(le32_to_cpu(es->s_feature_incompat) &
2216 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4cf46b67
AF
2217 ext3_msg(sb, KERN_ERR, "error: external journal has "
2218 "bad superblock");
1da177e4
LT
2219 brelse(bh);
2220 goto out_bdev;
2221 }
2222
2223 if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4cf46b67 2224 ext3_msg(sb, KERN_ERR, "error: journal UUID does not match");
1da177e4
LT
2225 brelse(bh);
2226 goto out_bdev;
2227 }
2228
2229 len = le32_to_cpu(es->s_blocks_count);
2230 start = sb_block + 1;
2231 brelse(bh); /* we're done with the superblock */
2232
2233 journal = journal_init_dev(bdev, sb->s_bdev,
2234 start, len, blocksize);
2235 if (!journal) {
4cf46b67
AF
2236 ext3_msg(sb, KERN_ERR,
2237 "error: failed to create device journal");
1da177e4
LT
2238 goto out_bdev;
2239 }
2240 journal->j_private = sb;
d03e1292
ZL
2241 if (!bh_uptodate_or_lock(journal->j_sb_buffer)) {
2242 if (bh_submit_read(journal->j_sb_buffer)) {
2243 ext3_msg(sb, KERN_ERR, "I/O error on journal device");
2244 goto out_journal;
2245 }
1da177e4
LT
2246 }
2247 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4cf46b67
AF
2248 ext3_msg(sb, KERN_ERR,
2249 "error: external journal has more than one "
2250 "user (unsupported) - %d",
1da177e4
LT
2251 be32_to_cpu(journal->j_superblock->s_nr_users));
2252 goto out_journal;
2253 }
2254 EXT3_SB(sb)->journal_bdev = bdev;
2255 ext3_init_journal_params(sb, journal);
2256 return journal;
2257out_journal:
2258 journal_destroy(journal);
2259out_bdev:
2260 ext3_blkdev_put(bdev);
2261 return NULL;
2262}
2263
71b96257
JL
2264static int ext3_load_journal(struct super_block *sb,
2265 struct ext3_super_block *es,
2266 unsigned long journal_devnum)
1da177e4
LT
2267{
2268 journal_t *journal;
eee194e7 2269 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
71b96257 2270 dev_t journal_dev;
1da177e4
LT
2271 int err = 0;
2272 int really_read_only;
2273
71b96257
JL
2274 if (journal_devnum &&
2275 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4cf46b67
AF
2276 ext3_msg(sb, KERN_INFO, "external journal device major/minor "
2277 "numbers have changed");
71b96257
JL
2278 journal_dev = new_decode_dev(journal_devnum);
2279 } else
2280 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2281
1da177e4
LT
2282 really_read_only = bdev_read_only(sb->s_bdev);
2283
2284 /*
2285 * Are we loading a blank journal or performing recovery after a
2286 * crash? For recovery, we need to check in advance whether we
2287 * can get read-write access to the device.
2288 */
2289
2290 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
2291 if (sb->s_flags & MS_RDONLY) {
4cf46b67
AF
2292 ext3_msg(sb, KERN_INFO,
2293 "recovery required on readonly filesystem");
1da177e4 2294 if (really_read_only) {
4cf46b67
AF
2295 ext3_msg(sb, KERN_ERR, "error: write access "
2296 "unavailable, cannot proceed");
1da177e4
LT
2297 return -EROFS;
2298 }
4cf46b67
AF
2299 ext3_msg(sb, KERN_INFO,
2300 "write access will be enabled during recovery");
1da177e4
LT
2301 }
2302 }
2303
2304 if (journal_inum && journal_dev) {
4cf46b67
AF
2305 ext3_msg(sb, KERN_ERR, "error: filesystem has both journal "
2306 "and inode journals");
1da177e4
LT
2307 return -EINVAL;
2308 }
2309
2310 if (journal_inum) {
2311 if (!(journal = ext3_get_journal(sb, journal_inum)))
2312 return -EINVAL;
2313 } else {
2314 if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
2315 return -EINVAL;
2316 }
2317
0636c73e
ES
2318 if (!(journal->j_flags & JFS_BARRIER))
2319 printk(KERN_INFO "EXT3-fs: barriers not enabled\n");
2320
1da177e4
LT
2321 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2322 err = journal_update_format(journal);
2323 if (err) {
4cf46b67 2324 ext3_msg(sb, KERN_ERR, "error updating journal");
1da177e4
LT
2325 journal_destroy(journal);
2326 return err;
2327 }
2328 }
2329
2330 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
2331 err = journal_wipe(journal, !really_read_only);
2332 if (!err)
2333 err = journal_load(journal);
2334
2335 if (err) {
4cf46b67 2336 ext3_msg(sb, KERN_ERR, "error loading journal");
1da177e4
LT
2337 journal_destroy(journal);
2338 return err;
2339 }
2340
2341 EXT3_SB(sb)->s_journal = journal;
2342 ext3_clear_journal_err(sb, es);
71b96257 2343
31d710a7 2344 if (!really_read_only && journal_devnum &&
71b96257
JL
2345 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2346 es->s_journal_dev = cpu_to_le32(journal_devnum);
71b96257
JL
2347
2348 /* Make sure we flush the recovery flag to disk. */
2349 ext3_commit_super(sb, es, 1);
2350 }
2351
1da177e4
LT
2352 return 0;
2353}
2354
4cf46b67
AF
2355static int ext3_create_journal(struct super_block *sb,
2356 struct ext3_super_block *es,
eee194e7 2357 unsigned int journal_inum)
1da177e4
LT
2358{
2359 journal_t *journal;
952d9de1 2360 int err;
1da177e4
LT
2361
2362 if (sb->s_flags & MS_RDONLY) {
4cf46b67
AF
2363 ext3_msg(sb, KERN_ERR,
2364 "error: readonly filesystem when trying to "
2365 "create journal");
1da177e4
LT
2366 return -EROFS;
2367 }
2368
952d9de1
BP
2369 journal = ext3_get_journal(sb, journal_inum);
2370 if (!journal)
1da177e4
LT
2371 return -EINVAL;
2372
4cf46b67 2373 ext3_msg(sb, KERN_INFO, "creating new journal on inode %u",
1da177e4
LT
2374 journal_inum);
2375
952d9de1
BP
2376 err = journal_create(journal);
2377 if (err) {
4cf46b67 2378 ext3_msg(sb, KERN_ERR, "error creating journal");
1da177e4
LT
2379 journal_destroy(journal);
2380 return -EIO;
2381 }
2382
2383 EXT3_SB(sb)->s_journal = journal;
2384
2385 ext3_update_dynamic_rev(sb);
2386 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2387 EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
2388
2389 es->s_journal_inum = cpu_to_le32(journal_inum);
1da177e4
LT
2390
2391 /* Make sure we flush the recovery flag to disk. */
2392 ext3_commit_super(sb, es, 1);
2393
2394 return 0;
2395}
2396
c4be0c1d
TS
2397static int ext3_commit_super(struct super_block *sb,
2398 struct ext3_super_block *es,
1da177e4
LT
2399 int sync)
2400{
2401 struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;
c4be0c1d 2402 int error = 0;
1da177e4
LT
2403
2404 if (!sbh)
c4be0c1d 2405 return error;
dff6825e
DW
2406
2407 if (buffer_write_io_error(sbh)) {
2408 /*
2409 * Oh, dear. A previous attempt to write the
2410 * superblock failed. This could happen because the
2411 * USB device was yanked out. Or it could happen to
2412 * be a transient write error and maybe the block will
2413 * be remapped. Nothing we can do but to retry the
2414 * write and hope for the best.
2415 */
2416 ext3_msg(sb, KERN_ERR, "previous I/O error to "
2417 "superblock detected");
2418 clear_buffer_write_io_error(sbh);
2419 set_buffer_uptodate(sbh);
2420 }
96ec2e0a
TT
2421 /*
2422 * If the file system is mounted read-only, don't update the
2423 * superblock write time. This avoids updating the superblock
2424 * write time when we are mounting the root file system
2425 * read/only but we need to replay the journal; at that point,
2426 * for people who are east of GMT and who make their clock
2427 * tick in localtime for Windows bug-for-bug compatibility,
2428 * the clock is set in the future, and this will cause e2fsck
2429 * to complain and force a full file system check.
2430 */
2431 if (!(sb->s_flags & MS_RDONLY))
2432 es->s_wtime = cpu_to_le32(get_seconds());
1da177e4
LT
2433 es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
2434 es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
2435 BUFFER_TRACE(sbh, "marking dirty");
2436 mark_buffer_dirty(sbh);
dff6825e 2437 if (sync) {
c4be0c1d 2438 error = sync_dirty_buffer(sbh);
dff6825e
DW
2439 if (buffer_write_io_error(sbh)) {
2440 ext3_msg(sb, KERN_ERR, "I/O error while writing "
2441 "superblock");
2442 clear_buffer_write_io_error(sbh);
2443 set_buffer_uptodate(sbh);
2444 }
2445 }
c4be0c1d 2446 return error;
1da177e4
LT
2447}
2448
2449
2450/*
2451 * Have we just finished recovery? If so, and if we are mounting (or
2452 * remounting) the filesystem readonly, then we will end up with a
2453 * consistent fs on disk. Record that fact.
2454 */
2455static void ext3_mark_recovery_complete(struct super_block * sb,
2456 struct ext3_super_block * es)
2457{
2458 journal_t *journal = EXT3_SB(sb)->s_journal;
2459
2460 journal_lock_updates(journal);
2d7c820e
HK
2461 if (journal_flush(journal) < 0)
2462 goto out;
2463
1da177e4
LT
2464 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
2465 sb->s_flags & MS_RDONLY) {
2466 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1da177e4
LT
2467 ext3_commit_super(sb, es, 1);
2468 }
2d7c820e
HK
2469
2470out:
1da177e4
LT
2471 journal_unlock_updates(journal);
2472}
2473
2474/*
2475 * If we are mounting (or read-write remounting) a filesystem whose journal
2476 * has recorded an error from a previous lifetime, move that error to the
2477 * main filesystem now.
2478 */
4cf46b67
AF
2479static void ext3_clear_journal_err(struct super_block *sb,
2480 struct ext3_super_block *es)
1da177e4
LT
2481{
2482 journal_t *journal;
2483 int j_errno;
2484 const char *errstr;
2485
2486 journal = EXT3_SB(sb)->s_journal;
2487
2488 /*
2489 * Now check for any error status which may have been recorded in the
2490 * journal by a prior ext3_error() or ext3_abort()
2491 */
2492
2493 j_errno = journal_errno(journal);
2494 if (j_errno) {
2495 char nbuf[16];
2496
2497 errstr = ext3_decode_error(sb, j_errno, nbuf);
e05b6b52 2498 ext3_warning(sb, __func__, "Filesystem error recorded "
1da177e4 2499 "from previous mount: %s", errstr);
e05b6b52 2500 ext3_warning(sb, __func__, "Marking fs in need of "
1da177e4
LT
2501 "filesystem check.");
2502
2503 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
2504 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
2505 ext3_commit_super (sb, es, 1);
2506
2507 journal_clear_err(journal);
2508 }
2509}
2510
2511/*
2512 * Force the running and committing transactions to commit,
2513 * and wait on the commit.
2514 */
2515int ext3_force_commit(struct super_block *sb)
2516{
2517 journal_t *journal;
2518 int ret;
2519
2520 if (sb->s_flags & MS_RDONLY)
2521 return 0;
2522
2523 journal = EXT3_SB(sb)->s_journal;
1da177e4
LT
2524 ret = ext3_journal_force_commit(journal);
2525 return ret;
2526}
2527
1da177e4
LT
2528static int ext3_sync_fs(struct super_block *sb, int wait)
2529{
02ac597c 2530 tid_t target;
c87591b7 2531
785c4bcc 2532 trace_ext3_sync_fs(sb, wait);
a1177825
JK
2533 /*
2534 * Writeback quota in non-journalled quota case - journalled quota has
2535 * no dirty dquots
2536 */
2537 dquot_writeback_dquots(sb, -1);
02ac597c
JK
2538 if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
2539 if (wait)
2540 log_wait_commit(EXT3_SB(sb)->s_journal, target);
2541 }
1da177e4
LT
2542 return 0;
2543}
2544
2545/*
2546 * LVM calls this function before a (read-only) snapshot is created. This
2547 * gives us a chance to flush the journal completely and mark the fs clean.
2548 */
c4be0c1d 2549static int ext3_freeze(struct super_block *sb)
1da177e4 2550{
c4be0c1d
TS
2551 int error = 0;
2552 journal_t *journal;
1da177e4
LT
2553
2554 if (!(sb->s_flags & MS_RDONLY)) {
c4be0c1d 2555 journal = EXT3_SB(sb)->s_journal;
1da177e4
LT
2556
2557 /* Now we set up the journal barrier. */
2558 journal_lock_updates(journal);
2d7c820e
HK
2559
2560 /*
2561 * We don't want to clear needs_recovery flag when we failed
2562 * to flush the journal.
2563 */
c4be0c1d
TS
2564 error = journal_flush(journal);
2565 if (error < 0)
2566 goto out;
1da177e4
LT
2567
2568 /* Journal blocked and flushed, clear needs_recovery flag. */
2569 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
c4be0c1d
TS
2570 error = ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
2571 if (error)
2572 goto out;
1da177e4 2573 }
c4be0c1d
TS
2574 return 0;
2575
2576out:
2577 journal_unlock_updates(journal);
2578 return error;
1da177e4
LT
2579}
2580
2581/*
2582 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2583 * flag here, even though the filesystem is not technically dirty yet.
2584 */
c4be0c1d 2585static int ext3_unfreeze(struct super_block *sb)
1da177e4
LT
2586{
2587 if (!(sb->s_flags & MS_RDONLY)) {
1da177e4
LT
2588 /* Reser the needs_recovery flag before the fs is unlocked. */
2589 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2590 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
1da177e4
LT
2591 journal_unlock_updates(EXT3_SB(sb)->s_journal);
2592 }
c4be0c1d 2593 return 0;
1da177e4
LT
2594}
2595
2596static int ext3_remount (struct super_block * sb, int * flags, char * data)
2597{
2598 struct ext3_super_block * es;
2599 struct ext3_sb_info *sbi = EXT3_SB(sb);
43d23f90 2600 ext3_fsblk_t n_blocks_count = 0;
08c6a96f
JK
2601 unsigned long old_sb_flags;
2602 struct ext3_mount_options old_opts;
c79d967d 2603 int enable_quota = 0;
08c6a96f
JK
2604 int err;
2605#ifdef CONFIG_QUOTA
2606 int i;
2607#endif
2608
2609 /* Store the original options */
2610 old_sb_flags = sb->s_flags;
2611 old_opts.s_mount_opt = sbi->s_mount_opt;
2612 old_opts.s_resuid = sbi->s_resuid;
2613 old_opts.s_resgid = sbi->s_resgid;
2614 old_opts.s_commit_interval = sbi->s_commit_interval;
2615#ifdef CONFIG_QUOTA
2616 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2617 for (i = 0; i < MAXQUOTAS; i++)
f56426ae
JK
2618 if (sbi->s_qf_names[i]) {
2619 old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
2620 GFP_KERNEL);
2621 if (!old_opts.s_qf_names[i]) {
2622 int j;
2623
2624 for (j = 0; j < i; j++)
2625 kfree(old_opts.s_qf_names[j]);
2626 return -ENOMEM;
2627 }
2628 } else
2629 old_opts.s_qf_names[i] = NULL;
08c6a96f 2630#endif
1da177e4
LT
2631
2632 /*
2633 * Allow the "check" option to be passed as a remount option.
2634 */
71b96257 2635 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
08c6a96f
JK
2636 err = -EINVAL;
2637 goto restore_opts;
2638 }
1da177e4 2639
e3c96435 2640 if (test_opt(sb, ABORT))
e05b6b52 2641 ext3_abort(sb, __func__, "Abort forced by user");
1da177e4
LT
2642
2643 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
e3c96435 2644 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
1da177e4
LT
2645
2646 es = sbi->s_es;
2647
2648 ext3_init_journal_params(sb, sbi->s_journal);
2649
2650 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2651 n_blocks_count > le32_to_cpu(es->s_blocks_count)) {
e3c96435 2652 if (test_opt(sb, ABORT)) {
08c6a96f
JK
2653 err = -EROFS;
2654 goto restore_opts;
2655 }
1da177e4
LT
2656
2657 if (*flags & MS_RDONLY) {
0f0dd62f
CH
2658 err = dquot_suspend(sb, -1);
2659 if (err < 0)
c79d967d 2660 goto restore_opts;
c79d967d 2661
1da177e4
LT
2662 /*
2663 * First of all, the unconditional stuff we have to do
2664 * to disable replay of the journal when we next remount
2665 */
2666 sb->s_flags |= MS_RDONLY;
2667
2668 /*
2669 * OK, test if we are remounting a valid rw partition
2670 * readonly, and if so set the rdonly flag and then
2671 * mark the partition as valid again.
2672 */
2673 if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
2674 (sbi->s_mount_state & EXT3_VALID_FS))
2675 es->s_state = cpu_to_le16(sbi->s_mount_state);
2676
2677 ext3_mark_recovery_complete(sb, es);
2678 } else {
2679 __le32 ret;
2680 if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
2681 ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
4cf46b67
AF
2682 ext3_msg(sb, KERN_WARNING,
2683 "warning: couldn't remount RDWR "
2684 "because of unsupported optional "
2685 "features (%x)", le32_to_cpu(ret));
08c6a96f
JK
2686 err = -EROFS;
2687 goto restore_opts;
1da177e4 2688 }
ea9a05a1
ES
2689
2690 /*
2691 * If we have an unprocessed orphan list hanging
2692 * around from a previously readonly bdev mount,
1cde201d 2693 * require a full umount & mount for now.
ea9a05a1
ES
2694 */
2695 if (es->s_last_orphan) {
4cf46b67 2696 ext3_msg(sb, KERN_WARNING, "warning: couldn't "
ea9a05a1
ES
2697 "remount RDWR because of unprocessed "
2698 "orphan inode list. Please "
1cde201d 2699 "umount & mount instead.");
ea9a05a1
ES
2700 err = -EINVAL;
2701 goto restore_opts;
2702 }
2703
1da177e4
LT
2704 /*
2705 * Mounting a RDONLY partition read-write, so reread
2706 * and store the current valid flag. (It may have
2707 * been changed by e2fsck since we originally mounted
2708 * the partition.)
2709 */
2710 ext3_clear_journal_err(sb, es);
2711 sbi->s_mount_state = le16_to_cpu(es->s_state);
a4e4de36 2712 if ((err = ext3_group_extend(sb, es, n_blocks_count)))
08c6a96f 2713 goto restore_opts;
1da177e4
LT
2714 if (!ext3_setup_super (sb, es, 0))
2715 sb->s_flags &= ~MS_RDONLY;
c79d967d 2716 enable_quota = 1;
1da177e4
LT
2717 }
2718 }
08c6a96f
JK
2719#ifdef CONFIG_QUOTA
2720 /* Release old quota file names */
2721 for (i = 0; i < MAXQUOTAS; i++)
f56426ae 2722 kfree(old_opts.s_qf_names[i]);
08c6a96f 2723#endif
c79d967d 2724 if (enable_quota)
0f0dd62f 2725 dquot_resume(sb, -1);
1da177e4 2726 return 0;
08c6a96f
JK
2727restore_opts:
2728 sb->s_flags = old_sb_flags;
2729 sbi->s_mount_opt = old_opts.s_mount_opt;
2730 sbi->s_resuid = old_opts.s_resuid;
2731 sbi->s_resgid = old_opts.s_resgid;
2732 sbi->s_commit_interval = old_opts.s_commit_interval;
2733#ifdef CONFIG_QUOTA
2734 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2735 for (i = 0; i < MAXQUOTAS; i++) {
f56426ae 2736 kfree(sbi->s_qf_names[i]);
08c6a96f
JK
2737 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2738 }
2739#endif
2740 return err;
1da177e4
LT
2741}
2742
726c3342 2743static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf)
1da177e4 2744{
726c3342 2745 struct super_block *sb = dentry->d_sb;
09fe316a
AT
2746 struct ext3_sb_info *sbi = EXT3_SB(sb);
2747 struct ext3_super_block *es = sbi->s_es;
50ee0a32 2748 u64 fsid;
1da177e4 2749
a71ce8c6
BP
2750 if (test_opt(sb, MINIX_DF)) {
2751 sbi->s_overhead_last = 0;
2752 } else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
2753 unsigned long ngroups = sbi->s_groups_count, i;
2754 ext3_fsblk_t overhead = 0;
1da177e4
LT
2755 smp_rmb();
2756
2757 /*
a71ce8c6
BP
2758 * Compute the overhead (FS structures). This is constant
2759 * for a given filesystem unless the number of block groups
2760 * changes so we cache the previous value until it does.
1da177e4
LT
2761 */
2762
2763 /*
2764 * All of the blocks before first_data_block are
2765 * overhead
2766 */
2767 overhead = le32_to_cpu(es->s_first_data_block);
2768
2769 /*
2770 * Add the overhead attributed to the superblock and
2771 * block group descriptors. If the sparse superblocks
2772 * feature is turned on, then not all groups have this.
2773 */
2774 for (i = 0; i < ngroups; i++) {
2775 overhead += ext3_bg_has_super(sb, i) +
2776 ext3_bg_num_gdb(sb, i);
2777 cond_resched();
2778 }
2779
2780 /*
2781 * Every block group has an inode bitmap, a block
2782 * bitmap, and an inode table.
2783 */
a71ce8c6
BP
2784 overhead += ngroups * (2 + sbi->s_itb_per_group);
2785 sbi->s_overhead_last = overhead;
2786 smp_wmb();
2787 sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
1da177e4
LT
2788 }
2789
2790 buf->f_type = EXT3_SUPER_MAGIC;
2791 buf->f_bsize = sb->s_blocksize;
a71ce8c6 2792 buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
52d9f3b4 2793 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
1da177e4
LT
2794 buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
2795 if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
2796 buf->f_bavail = 0;
2797 buf->f_files = le32_to_cpu(es->s_inodes_count);
52d9f3b4 2798 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
1da177e4 2799 buf->f_namelen = EXT3_NAME_LEN;
50ee0a32
PE
2800 fsid = le64_to_cpup((void *)es->s_uuid) ^
2801 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2802 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2803 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1da177e4
LT
2804 return 0;
2805}
2806
2807/* Helper function for writing quotas on sync - we need to start transaction before quota file
2808 * is locked for write. Otherwise the are possible deadlocks:
2809 * Process 1 Process 2
2810 * ext3_create() quota_sync()
2811 * journal_start() write_dquot()
871a2931 2812 * dquot_initialize() down(dqio_mutex)
d3be915f 2813 * down(dqio_mutex) journal_start()
1da177e4
LT
2814 *
2815 */
2816
2817#ifdef CONFIG_QUOTA
2818
2819static inline struct inode *dquot_to_inode(struct dquot *dquot)
2820{
4c376dca 2821 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
1da177e4
LT
2822}
2823
1da177e4
LT
2824static int ext3_write_dquot(struct dquot *dquot)
2825{
2826 int ret, err;
2827 handle_t *handle;
2828 struct inode *inode;
2829
2830 inode = dquot_to_inode(dquot);
2831 handle = ext3_journal_start(inode,
1f54587b 2832 EXT3_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1da177e4
LT
2833 if (IS_ERR(handle))
2834 return PTR_ERR(handle);
2835 ret = dquot_commit(dquot);
2836 err = ext3_journal_stop(handle);
2837 if (!ret)
2838 ret = err;
2839 return ret;
2840}
2841
2842static int ext3_acquire_dquot(struct dquot *dquot)
2843{
2844 int ret, err;
2845 handle_t *handle;
2846
2847 handle = ext3_journal_start(dquot_to_inode(dquot),
1f54587b 2848 EXT3_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1da177e4
LT
2849 if (IS_ERR(handle))
2850 return PTR_ERR(handle);
2851 ret = dquot_acquire(dquot);
2852 err = ext3_journal_stop(handle);
2853 if (!ret)
2854 ret = err;
2855 return ret;
2856}
2857
2858static int ext3_release_dquot(struct dquot *dquot)
2859{
2860 int ret, err;
2861 handle_t *handle;
2862
2863 handle = ext3_journal_start(dquot_to_inode(dquot),
1f54587b 2864 EXT3_QUOTA_DEL_BLOCKS(dquot->dq_sb));
9c3013e9
JK
2865 if (IS_ERR(handle)) {
2866 /* Release dquot anyway to avoid endless cycle in dqput() */
2867 dquot_release(dquot);
1da177e4 2868 return PTR_ERR(handle);
9c3013e9 2869 }
1da177e4
LT
2870 ret = dquot_release(dquot);
2871 err = ext3_journal_stop(handle);
2872 if (!ret)
2873 ret = err;
2874 return ret;
2875}
2876
2877static int ext3_mark_dquot_dirty(struct dquot *dquot)
2878{
99aeaf63 2879 /* Are we journaling quotas? */
1da177e4
LT
2880 if (EXT3_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2881 EXT3_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2882 dquot_mark_dquot_dirty(dquot);
2883 return ext3_write_dquot(dquot);
2884 } else {
2885 return dquot_mark_dquot_dirty(dquot);
2886 }
2887}
2888
2889static int ext3_write_info(struct super_block *sb, int type)
2890{
2891 int ret, err;
2892 handle_t *handle;
2893
2894 /* Data block + inode block */
2895 handle = ext3_journal_start(sb->s_root->d_inode, 2);
2896 if (IS_ERR(handle))
2897 return PTR_ERR(handle);
2898 ret = dquot_commit_info(sb, type);
2899 err = ext3_journal_stop(handle);
2900 if (!ret)
2901 ret = err;
2902 return ret;
2903}
2904
2905/*
2906 * Turn on quotas during mount time - we need to find
2907 * the quota file and such...
2908 */
2909static int ext3_quota_on_mount(struct super_block *sb, int type)
2910{
287a8095
CH
2911 return dquot_quota_on_mount(sb, EXT3_SB(sb)->s_qf_names[type],
2912 EXT3_SB(sb)->s_jquota_fmt, type);
1da177e4
LT
2913}
2914
2915/*
2916 * Standard function to be called on quota_on
2917 */
2918static int ext3_quota_on(struct super_block *sb, int type, int format_id,
f00c9e44 2919 struct path *path)
1da177e4
LT
2920{
2921 int err;
1da177e4 2922
1f54587b
JK
2923 if (!test_opt(sb, QUOTA))
2924 return -EINVAL;
9cfe7b90 2925
1da177e4 2926 /* Quotafile not on the same filesystem? */
d8c9584e 2927 if (path->dentry->d_sb != sb)
1da177e4 2928 return -EXDEV;
9cfe7b90
JK
2929 /* Journaling quota? */
2930 if (EXT3_SB(sb)->s_qf_names[type]) {
2931 /* Quotafile not of fs root? */
f00c9e44 2932 if (path->dentry->d_parent != sb->s_root)
4cf46b67
AF
2933 ext3_msg(sb, KERN_WARNING,
2934 "warning: Quota file not on filesystem root. "
2935 "Journaled quota will not work.");
9cfe7b90
JK
2936 }
2937
2938 /*
2939 * When we journal data on quota file, we have to flush journal to see
2940 * all updates to the file when we bypass pagecache...
2941 */
f00c9e44 2942 if (ext3_should_journal_data(path->dentry->d_inode)) {
9cfe7b90
JK
2943 /*
2944 * We don't need to lock updates but journal_flush() could
2945 * otherwise be livelocked...
2946 */
2947 journal_lock_updates(EXT3_SB(sb)->s_journal);
2d7c820e 2948 err = journal_flush(EXT3_SB(sb)->s_journal);
9cfe7b90 2949 journal_unlock_updates(EXT3_SB(sb)->s_journal);
f00c9e44 2950 if (err)
2d7c820e 2951 return err;
9cfe7b90
JK
2952 }
2953
f00c9e44 2954 return dquot_quota_on(sb, type, format_id, path);
1da177e4
LT
2955}
2956
2957/* Read data from quotafile - avoid pagecache and such because we cannot afford
2958 * acquiring the locks... As quota files are never truncated and quota code
25985edc 2959 * itself serializes the operations (and no one else should touch the files)
1da177e4
LT
2960 * we don't have to be afraid of races */
2961static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
2962 size_t len, loff_t off)
2963{
2964 struct inode *inode = sb_dqopt(sb)->files[type];
2965 sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
2966 int err = 0;
2967 int offset = off & (sb->s_blocksize - 1);
2968 int tocopy;
2969 size_t toread;
2970 struct buffer_head *bh;
2971 loff_t i_size = i_size_read(inode);
2972
2973 if (off > i_size)
2974 return 0;
2975 if (off+len > i_size)
2976 len = i_size-off;
2977 toread = len;
2978 while (toread > 0) {
2979 tocopy = sb->s_blocksize - offset < toread ?
2980 sb->s_blocksize - offset : toread;
2981 bh = ext3_bread(NULL, inode, blk, 0, &err);
2982 if (err)
2983 return err;
2984 if (!bh) /* A hole? */
2985 memset(data, 0, tocopy);
2986 else
2987 memcpy(data, bh->b_data+offset, tocopy);
2988 brelse(bh);
2989 offset = 0;
2990 toread -= tocopy;
2991 data += tocopy;
2992 blk++;
2993 }
2994 return len;
2995}
2996
2997/* Write to quotafile (we know the transaction is already started and has
2998 * enough credits) */
2999static ssize_t ext3_quota_write(struct super_block *sb, int type,
3000 const char *data, size_t len, loff_t off)
3001{
3002 struct inode *inode = sb_dqopt(sb)->files[type];
3003 sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
3004 int err = 0;
3005 int offset = off & (sb->s_blocksize - 1);
1da177e4 3006 int journal_quota = EXT3_SB(sb)->s_qf_names[type] != NULL;
1da177e4
LT
3007 struct buffer_head *bh;
3008 handle_t *handle = journal_current_handle();
3009
9c3013e9 3010 if (!handle) {
4cf46b67
AF
3011 ext3_msg(sb, KERN_WARNING,
3012 "warning: quota write (off=%llu, len=%llu)"
3013 " cancelled because transaction is not started.",
9c3013e9
JK
3014 (unsigned long long)off, (unsigned long long)len);
3015 return -EIO;
3016 }
e5472147
DM
3017
3018 /*
3019 * Since we account only one data block in transaction credits,
3020 * then it is impossible to cross a block boundary.
3021 */
3022 if (sb->s_blocksize - offset < len) {
3023 ext3_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
3024 " cancelled because not block aligned",
3025 (unsigned long long)off, (unsigned long long)len);
3026 return -EIO;
3027 }
e5472147
DM
3028 bh = ext3_bread(handle, inode, blk, 1, &err);
3029 if (!bh)
3030 goto out;
3031 if (journal_quota) {
3032 err = ext3_journal_get_write_access(handle, bh);
3033 if (err) {
3034 brelse(bh);
1da177e4 3035 goto out;
1da177e4 3036 }
1da177e4 3037 }
e5472147
DM
3038 lock_buffer(bh);
3039 memcpy(bh->b_data+offset, data, len);
3040 flush_dcache_page(bh->b_page);
3041 unlock_buffer(bh);
3042 if (journal_quota)
3043 err = ext3_journal_dirty_metadata(handle, bh);
3044 else {
3045 /* Always do at least ordered writes for quotas */
3046 err = ext3_journal_dirty_data(handle, bh);
3047 mark_buffer_dirty(bh);
3048 }
3049 brelse(bh);
1da177e4 3050out:
905c3937 3051 if (err)
1da177e4 3052 return err;
e5472147
DM
3053 if (inode->i_size < off + len) {
3054 i_size_write(inode, off + len);
1da177e4
LT
3055 EXT3_I(inode)->i_disksize = inode->i_size;
3056 }
3057 inode->i_version++;
3058 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3059 ext3_mark_inode_dirty(handle, inode);
e5472147 3060 return len;
1da177e4
LT
3061}
3062
3063#endif
3064
152a0836
AV
3065static struct dentry *ext3_mount(struct file_system_type *fs_type,
3066 int flags, const char *dev_name, void *data)
1da177e4 3067{
152a0836 3068 return mount_bdev(fs_type, flags, dev_name, data, ext3_fill_super);
1da177e4
LT
3069}
3070
3071static struct file_system_type ext3_fs_type = {
3072 .owner = THIS_MODULE,
3073 .name = "ext3",
152a0836 3074 .mount = ext3_mount,
1da177e4
LT
3075 .kill_sb = kill_block_super,
3076 .fs_flags = FS_REQUIRES_DEV,
3077};
7f78e035 3078MODULE_ALIAS_FS("ext3");
1da177e4
LT
3079
3080static int __init init_ext3_fs(void)
3081{
3082 int err = init_ext3_xattr();
3083 if (err)
3084 return err;
3085 err = init_inodecache();
3086 if (err)
3087 goto out1;
3088 err = register_filesystem(&ext3_fs_type);
3089 if (err)
3090 goto out;
3091 return 0;
3092out:
3093 destroy_inodecache();
3094out1:
e9ad5620 3095 exit_ext3_xattr();
1da177e4
LT
3096 return err;
3097}
3098
3099static void __exit exit_ext3_fs(void)
3100{
3101 unregister_filesystem(&ext3_fs_type);
3102 destroy_inodecache();
3103 exit_ext3_xattr();
3104}
3105
3106MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3107MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
3108MODULE_LICENSE("GPL");
3109module_init(init_ext3_fs)
3110module_exit(exit_ext3_fs)
This page took 0.898567 seconds and 5 git commands to generate.