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