ext2: Call dquot_writeback_dquots() with s_umount held
[sfrench/cifs-2.6.git] / fs / ext2 / super.c
1 /*
2  *  linux/fs/ext2/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/slab.h>
23 #include <linux/init.h>
24 #include <linux/blkdev.h>
25 #include <linux/parser.h>
26 #include <linux/random.h>
27 #include <linux/buffer_head.h>
28 #include <linux/exportfs.h>
29 #include <linux/vfs.h>
30 #include <linux/seq_file.h>
31 #include <linux/mount.h>
32 #include <linux/log2.h>
33 #include <linux/quotaops.h>
34 #include <linux/uaccess.h>
35 #include "ext2.h"
36 #include "xattr.h"
37 #include "acl.h"
38
39 static void ext2_write_super(struct super_block *sb);
40 static int ext2_remount (struct super_block * sb, int * flags, char * data);
41 static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
42 static int ext2_sync_fs(struct super_block *sb, int wait);
43 static int ext2_freeze(struct super_block *sb);
44 static int ext2_unfreeze(struct super_block *sb);
45
46 void ext2_error(struct super_block *sb, const char *function,
47                 const char *fmt, ...)
48 {
49         struct va_format vaf;
50         va_list args;
51         struct ext2_sb_info *sbi = EXT2_SB(sb);
52         struct ext2_super_block *es = sbi->s_es;
53
54         if (!(sb->s_flags & MS_RDONLY)) {
55                 spin_lock(&sbi->s_lock);
56                 sbi->s_mount_state |= EXT2_ERROR_FS;
57                 es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
58                 spin_unlock(&sbi->s_lock);
59                 ext2_sync_super(sb, es, 1);
60         }
61
62         va_start(args, fmt);
63
64         vaf.fmt = fmt;
65         vaf.va = &args;
66
67         printk(KERN_CRIT "EXT2-fs (%s): error: %s: %pV\n",
68                sb->s_id, function, &vaf);
69
70         va_end(args);
71
72         if (test_opt(sb, ERRORS_PANIC))
73                 panic("EXT2-fs: panic from previous error\n");
74         if (test_opt(sb, ERRORS_RO)) {
75                 ext2_msg(sb, KERN_CRIT,
76                              "error: remounting filesystem read-only");
77                 sb->s_flags |= MS_RDONLY;
78         }
79 }
80
81 void ext2_msg(struct super_block *sb, const char *prefix,
82                 const char *fmt, ...)
83 {
84         struct va_format vaf;
85         va_list args;
86
87         va_start(args, fmt);
88
89         vaf.fmt = fmt;
90         vaf.va = &args;
91
92         printk("%sEXT2-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
93
94         va_end(args);
95 }
96
97 /*
98  * This must be called with sbi->s_lock held.
99  */
100 void ext2_update_dynamic_rev(struct super_block *sb)
101 {
102         struct ext2_super_block *es = EXT2_SB(sb)->s_es;
103
104         if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
105                 return;
106
107         ext2_msg(sb, KERN_WARNING,
108                      "warning: updating to rev %d because of "
109                      "new feature flag, running e2fsck is recommended",
110                      EXT2_DYNAMIC_REV);
111
112         es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
113         es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
114         es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
115         /* leave es->s_feature_*compat flags alone */
116         /* es->s_uuid will be set by e2fsck if empty */
117
118         /*
119          * The rest of the superblock fields should be zero, and if not it
120          * means they are likely already in use, so leave them alone.  We
121          * can leave it up to e2fsck to clean up any inconsistencies there.
122          */
123 }
124
125 static void ext2_put_super (struct super_block * sb)
126 {
127         int db_count;
128         int i;
129         struct ext2_sb_info *sbi = EXT2_SB(sb);
130
131         dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
132
133         if (sbi->s_mb_cache) {
134                 ext2_xattr_destroy_cache(sbi->s_mb_cache);
135                 sbi->s_mb_cache = NULL;
136         }
137         if (!(sb->s_flags & MS_RDONLY)) {
138                 struct ext2_super_block *es = sbi->s_es;
139
140                 spin_lock(&sbi->s_lock);
141                 es->s_state = cpu_to_le16(sbi->s_mount_state);
142                 spin_unlock(&sbi->s_lock);
143                 ext2_sync_super(sb, es, 1);
144         }
145         db_count = sbi->s_gdb_count;
146         for (i = 0; i < db_count; i++)
147                 if (sbi->s_group_desc[i])
148                         brelse (sbi->s_group_desc[i]);
149         kfree(sbi->s_group_desc);
150         kfree(sbi->s_debts);
151         percpu_counter_destroy(&sbi->s_freeblocks_counter);
152         percpu_counter_destroy(&sbi->s_freeinodes_counter);
153         percpu_counter_destroy(&sbi->s_dirs_counter);
154         brelse (sbi->s_sbh);
155         sb->s_fs_info = NULL;
156         kfree(sbi->s_blockgroup_lock);
157         kfree(sbi);
158 }
159
160 static struct kmem_cache * ext2_inode_cachep;
161
162 static struct inode *ext2_alloc_inode(struct super_block *sb)
163 {
164         struct ext2_inode_info *ei;
165         ei = kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
166         if (!ei)
167                 return NULL;
168         ei->i_block_alloc_info = NULL;
169         ei->vfs_inode.i_version = 1;
170 #ifdef CONFIG_QUOTA
171         memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
172 #endif
173
174         return &ei->vfs_inode;
175 }
176
177 static void ext2_i_callback(struct rcu_head *head)
178 {
179         struct inode *inode = container_of(head, struct inode, i_rcu);
180         kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
181 }
182
183 static void ext2_destroy_inode(struct inode *inode)
184 {
185         call_rcu(&inode->i_rcu, ext2_i_callback);
186 }
187
188 static void init_once(void *foo)
189 {
190         struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
191
192         rwlock_init(&ei->i_meta_lock);
193 #ifdef CONFIG_EXT2_FS_XATTR
194         init_rwsem(&ei->xattr_sem);
195 #endif
196         mutex_init(&ei->truncate_mutex);
197 #ifdef CONFIG_FS_DAX
198         init_rwsem(&ei->dax_sem);
199 #endif
200         inode_init_once(&ei->vfs_inode);
201 }
202
203 static int __init init_inodecache(void)
204 {
205         ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
206                                              sizeof(struct ext2_inode_info),
207                                              0, (SLAB_RECLAIM_ACCOUNT|
208                                                 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
209                                              init_once);
210         if (ext2_inode_cachep == NULL)
211                 return -ENOMEM;
212         return 0;
213 }
214
215 static void destroy_inodecache(void)
216 {
217         /*
218          * Make sure all delayed rcu free inodes are flushed before we
219          * destroy cache.
220          */
221         rcu_barrier();
222         kmem_cache_destroy(ext2_inode_cachep);
223 }
224
225 static int ext2_show_options(struct seq_file *seq, struct dentry *root)
226 {
227         struct super_block *sb = root->d_sb;
228         struct ext2_sb_info *sbi = EXT2_SB(sb);
229         struct ext2_super_block *es = sbi->s_es;
230         unsigned long def_mount_opts;
231
232         spin_lock(&sbi->s_lock);
233         def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
234
235         if (sbi->s_sb_block != 1)
236                 seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
237         if (test_opt(sb, MINIX_DF))
238                 seq_puts(seq, ",minixdf");
239         if (test_opt(sb, GRPID))
240                 seq_puts(seq, ",grpid");
241         if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
242                 seq_puts(seq, ",nogrpid");
243         if (!uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT2_DEF_RESUID)) ||
244             le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
245                 seq_printf(seq, ",resuid=%u",
246                                 from_kuid_munged(&init_user_ns, sbi->s_resuid));
247         }
248         if (!gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT2_DEF_RESGID)) ||
249             le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
250                 seq_printf(seq, ",resgid=%u",
251                                 from_kgid_munged(&init_user_ns, sbi->s_resgid));
252         }
253         if (test_opt(sb, ERRORS_RO)) {
254                 int def_errors = le16_to_cpu(es->s_errors);
255
256                 if (def_errors == EXT2_ERRORS_PANIC ||
257                     def_errors == EXT2_ERRORS_CONTINUE) {
258                         seq_puts(seq, ",errors=remount-ro");
259                 }
260         }
261         if (test_opt(sb, ERRORS_CONT))
262                 seq_puts(seq, ",errors=continue");
263         if (test_opt(sb, ERRORS_PANIC))
264                 seq_puts(seq, ",errors=panic");
265         if (test_opt(sb, NO_UID32))
266                 seq_puts(seq, ",nouid32");
267         if (test_opt(sb, DEBUG))
268                 seq_puts(seq, ",debug");
269         if (test_opt(sb, OLDALLOC))
270                 seq_puts(seq, ",oldalloc");
271
272 #ifdef CONFIG_EXT2_FS_XATTR
273         if (test_opt(sb, XATTR_USER))
274                 seq_puts(seq, ",user_xattr");
275         if (!test_opt(sb, XATTR_USER) &&
276             (def_mount_opts & EXT2_DEFM_XATTR_USER)) {
277                 seq_puts(seq, ",nouser_xattr");
278         }
279 #endif
280
281 #ifdef CONFIG_EXT2_FS_POSIX_ACL
282         if (test_opt(sb, POSIX_ACL))
283                 seq_puts(seq, ",acl");
284         if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT2_DEFM_ACL))
285                 seq_puts(seq, ",noacl");
286 #endif
287
288         if (test_opt(sb, NOBH))
289                 seq_puts(seq, ",nobh");
290
291 #if defined(CONFIG_QUOTA)
292         if (sbi->s_mount_opt & EXT2_MOUNT_USRQUOTA)
293                 seq_puts(seq, ",usrquota");
294
295         if (sbi->s_mount_opt & EXT2_MOUNT_GRPQUOTA)
296                 seq_puts(seq, ",grpquota");
297 #endif
298
299 #ifdef CONFIG_FS_DAX
300         if (sbi->s_mount_opt & EXT2_MOUNT_XIP)
301                 seq_puts(seq, ",xip");
302         if (sbi->s_mount_opt & EXT2_MOUNT_DAX)
303                 seq_puts(seq, ",dax");
304 #endif
305
306         if (!test_opt(sb, RESERVATION))
307                 seq_puts(seq, ",noreservation");
308
309         spin_unlock(&sbi->s_lock);
310         return 0;
311 }
312
313 #ifdef CONFIG_QUOTA
314 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
315 static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
316 static struct dquot **ext2_get_dquots(struct inode *inode)
317 {
318         return EXT2_I(inode)->i_dquot;
319 }
320 #endif
321
322 static const struct super_operations ext2_sops = {
323         .alloc_inode    = ext2_alloc_inode,
324         .destroy_inode  = ext2_destroy_inode,
325         .write_inode    = ext2_write_inode,
326         .evict_inode    = ext2_evict_inode,
327         .put_super      = ext2_put_super,
328         .sync_fs        = ext2_sync_fs,
329         .freeze_fs      = ext2_freeze,
330         .unfreeze_fs    = ext2_unfreeze,
331         .statfs         = ext2_statfs,
332         .remount_fs     = ext2_remount,
333         .show_options   = ext2_show_options,
334 #ifdef CONFIG_QUOTA
335         .quota_read     = ext2_quota_read,
336         .quota_write    = ext2_quota_write,
337         .get_dquots     = ext2_get_dquots,
338 #endif
339 };
340
341 static struct inode *ext2_nfs_get_inode(struct super_block *sb,
342                 u64 ino, u32 generation)
343 {
344         struct inode *inode;
345
346         if (ino < EXT2_FIRST_INO(sb) && ino != EXT2_ROOT_INO)
347                 return ERR_PTR(-ESTALE);
348         if (ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
349                 return ERR_PTR(-ESTALE);
350
351         /*
352          * ext2_iget isn't quite right if the inode is currently unallocated!
353          * However ext2_iget currently does appropriate checks to handle stale
354          * inodes so everything is OK.
355          */
356         inode = ext2_iget(sb, ino);
357         if (IS_ERR(inode))
358                 return ERR_CAST(inode);
359         if (generation && inode->i_generation != generation) {
360                 /* we didn't find the right inode.. */
361                 iput(inode);
362                 return ERR_PTR(-ESTALE);
363         }
364         return inode;
365 }
366
367 static struct dentry *ext2_fh_to_dentry(struct super_block *sb, struct fid *fid,
368                 int fh_len, int fh_type)
369 {
370         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
371                                     ext2_nfs_get_inode);
372 }
373
374 static struct dentry *ext2_fh_to_parent(struct super_block *sb, struct fid *fid,
375                 int fh_len, int fh_type)
376 {
377         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
378                                     ext2_nfs_get_inode);
379 }
380
381 static const struct export_operations ext2_export_ops = {
382         .fh_to_dentry = ext2_fh_to_dentry,
383         .fh_to_parent = ext2_fh_to_parent,
384         .get_parent = ext2_get_parent,
385 };
386
387 static unsigned long get_sb_block(void **data)
388 {
389         unsigned long   sb_block;
390         char            *options = (char *) *data;
391
392         if (!options || strncmp(options, "sb=", 3) != 0)
393                 return 1;       /* Default location */
394         options += 3;
395         sb_block = simple_strtoul(options, &options, 0);
396         if (*options && *options != ',') {
397                 printk("EXT2-fs: Invalid sb specification: %s\n",
398                        (char *) *data);
399                 return 1;
400         }
401         if (*options == ',')
402                 options++;
403         *data = (void *) options;
404         return sb_block;
405 }
406
407 enum {
408         Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
409         Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
410         Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
411         Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
412         Opt_acl, Opt_noacl, Opt_xip, Opt_dax, Opt_ignore, Opt_err, Opt_quota,
413         Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
414 };
415
416 static const match_table_t tokens = {
417         {Opt_bsd_df, "bsddf"},
418         {Opt_minix_df, "minixdf"},
419         {Opt_grpid, "grpid"},
420         {Opt_grpid, "bsdgroups"},
421         {Opt_nogrpid, "nogrpid"},
422         {Opt_nogrpid, "sysvgroups"},
423         {Opt_resgid, "resgid=%u"},
424         {Opt_resuid, "resuid=%u"},
425         {Opt_sb, "sb=%u"},
426         {Opt_err_cont, "errors=continue"},
427         {Opt_err_panic, "errors=panic"},
428         {Opt_err_ro, "errors=remount-ro"},
429         {Opt_nouid32, "nouid32"},
430         {Opt_nocheck, "check=none"},
431         {Opt_nocheck, "nocheck"},
432         {Opt_debug, "debug"},
433         {Opt_oldalloc, "oldalloc"},
434         {Opt_orlov, "orlov"},
435         {Opt_nobh, "nobh"},
436         {Opt_user_xattr, "user_xattr"},
437         {Opt_nouser_xattr, "nouser_xattr"},
438         {Opt_acl, "acl"},
439         {Opt_noacl, "noacl"},
440         {Opt_xip, "xip"},
441         {Opt_dax, "dax"},
442         {Opt_grpquota, "grpquota"},
443         {Opt_ignore, "noquota"},
444         {Opt_quota, "quota"},
445         {Opt_usrquota, "usrquota"},
446         {Opt_reservation, "reservation"},
447         {Opt_noreservation, "noreservation"},
448         {Opt_err, NULL}
449 };
450
451 static int parse_options(char *options, struct super_block *sb)
452 {
453         char *p;
454         struct ext2_sb_info *sbi = EXT2_SB(sb);
455         substring_t args[MAX_OPT_ARGS];
456         int option;
457         kuid_t uid;
458         kgid_t gid;
459
460         if (!options)
461                 return 1;
462
463         while ((p = strsep (&options, ",")) != NULL) {
464                 int token;
465                 if (!*p)
466                         continue;
467
468                 token = match_token(p, tokens, args);
469                 switch (token) {
470                 case Opt_bsd_df:
471                         clear_opt (sbi->s_mount_opt, MINIX_DF);
472                         break;
473                 case Opt_minix_df:
474                         set_opt (sbi->s_mount_opt, MINIX_DF);
475                         break;
476                 case Opt_grpid:
477                         set_opt (sbi->s_mount_opt, GRPID);
478                         break;
479                 case Opt_nogrpid:
480                         clear_opt (sbi->s_mount_opt, GRPID);
481                         break;
482                 case Opt_resuid:
483                         if (match_int(&args[0], &option))
484                                 return 0;
485                         uid = make_kuid(current_user_ns(), option);
486                         if (!uid_valid(uid)) {
487                                 ext2_msg(sb, KERN_ERR, "Invalid uid value %d", option);
488                                 return 0;
489
490                         }
491                         sbi->s_resuid = uid;
492                         break;
493                 case Opt_resgid:
494                         if (match_int(&args[0], &option))
495                                 return 0;
496                         gid = make_kgid(current_user_ns(), option);
497                         if (!gid_valid(gid)) {
498                                 ext2_msg(sb, KERN_ERR, "Invalid gid value %d", option);
499                                 return 0;
500                         }
501                         sbi->s_resgid = gid;
502                         break;
503                 case Opt_sb:
504                         /* handled by get_sb_block() instead of here */
505                         /* *sb_block = match_int(&args[0]); */
506                         break;
507                 case Opt_err_panic:
508                         clear_opt (sbi->s_mount_opt, ERRORS_CONT);
509                         clear_opt (sbi->s_mount_opt, ERRORS_RO);
510                         set_opt (sbi->s_mount_opt, ERRORS_PANIC);
511                         break;
512                 case Opt_err_ro:
513                         clear_opt (sbi->s_mount_opt, ERRORS_CONT);
514                         clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
515                         set_opt (sbi->s_mount_opt, ERRORS_RO);
516                         break;
517                 case Opt_err_cont:
518                         clear_opt (sbi->s_mount_opt, ERRORS_RO);
519                         clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
520                         set_opt (sbi->s_mount_opt, ERRORS_CONT);
521                         break;
522                 case Opt_nouid32:
523                         set_opt (sbi->s_mount_opt, NO_UID32);
524                         break;
525                 case Opt_nocheck:
526                         clear_opt (sbi->s_mount_opt, CHECK);
527                         break;
528                 case Opt_debug:
529                         set_opt (sbi->s_mount_opt, DEBUG);
530                         break;
531                 case Opt_oldalloc:
532                         set_opt (sbi->s_mount_opt, OLDALLOC);
533                         break;
534                 case Opt_orlov:
535                         clear_opt (sbi->s_mount_opt, OLDALLOC);
536                         break;
537                 case Opt_nobh:
538                         set_opt (sbi->s_mount_opt, NOBH);
539                         break;
540 #ifdef CONFIG_EXT2_FS_XATTR
541                 case Opt_user_xattr:
542                         set_opt (sbi->s_mount_opt, XATTR_USER);
543                         break;
544                 case Opt_nouser_xattr:
545                         clear_opt (sbi->s_mount_opt, XATTR_USER);
546                         break;
547 #else
548                 case Opt_user_xattr:
549                 case Opt_nouser_xattr:
550                         ext2_msg(sb, KERN_INFO, "(no)user_xattr options"
551                                 "not supported");
552                         break;
553 #endif
554 #ifdef CONFIG_EXT2_FS_POSIX_ACL
555                 case Opt_acl:
556                         set_opt(sbi->s_mount_opt, POSIX_ACL);
557                         break;
558                 case Opt_noacl:
559                         clear_opt(sbi->s_mount_opt, POSIX_ACL);
560                         break;
561 #else
562                 case Opt_acl:
563                 case Opt_noacl:
564                         ext2_msg(sb, KERN_INFO,
565                                 "(no)acl options not supported");
566                         break;
567 #endif
568                 case Opt_xip:
569                         ext2_msg(sb, KERN_INFO, "use dax instead of xip");
570                         set_opt(sbi->s_mount_opt, XIP);
571                         /* Fall through */
572                 case Opt_dax:
573 #ifdef CONFIG_FS_DAX
574                         ext2_msg(sb, KERN_WARNING,
575                 "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
576                         set_opt(sbi->s_mount_opt, DAX);
577 #else
578                         ext2_msg(sb, KERN_INFO, "dax option not supported");
579 #endif
580                         break;
581
582 #if defined(CONFIG_QUOTA)
583                 case Opt_quota:
584                 case Opt_usrquota:
585                         set_opt(sbi->s_mount_opt, USRQUOTA);
586                         break;
587
588                 case Opt_grpquota:
589                         set_opt(sbi->s_mount_opt, GRPQUOTA);
590                         break;
591 #else
592                 case Opt_quota:
593                 case Opt_usrquota:
594                 case Opt_grpquota:
595                         ext2_msg(sb, KERN_INFO,
596                                 "quota operations not supported");
597                         break;
598 #endif
599
600                 case Opt_reservation:
601                         set_opt(sbi->s_mount_opt, RESERVATION);
602                         ext2_msg(sb, KERN_INFO, "reservations ON");
603                         break;
604                 case Opt_noreservation:
605                         clear_opt(sbi->s_mount_opt, RESERVATION);
606                         ext2_msg(sb, KERN_INFO, "reservations OFF");
607                         break;
608                 case Opt_ignore:
609                         break;
610                 default:
611                         return 0;
612                 }
613         }
614         return 1;
615 }
616
617 static int ext2_setup_super (struct super_block * sb,
618                               struct ext2_super_block * es,
619                               int read_only)
620 {
621         int res = 0;
622         struct ext2_sb_info *sbi = EXT2_SB(sb);
623
624         if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
625                 ext2_msg(sb, KERN_ERR,
626                         "error: revision level too high, "
627                         "forcing read-only mode");
628                 res = MS_RDONLY;
629         }
630         if (read_only)
631                 return res;
632         if (!(sbi->s_mount_state & EXT2_VALID_FS))
633                 ext2_msg(sb, KERN_WARNING,
634                         "warning: mounting unchecked fs, "
635                         "running e2fsck is recommended");
636         else if ((sbi->s_mount_state & EXT2_ERROR_FS))
637                 ext2_msg(sb, KERN_WARNING,
638                         "warning: mounting fs with errors, "
639                         "running e2fsck is recommended");
640         else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
641                  le16_to_cpu(es->s_mnt_count) >=
642                  (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
643                 ext2_msg(sb, KERN_WARNING,
644                         "warning: maximal mount count reached, "
645                         "running e2fsck is recommended");
646         else if (le32_to_cpu(es->s_checkinterval) &&
647                 (le32_to_cpu(es->s_lastcheck) +
648                         le32_to_cpu(es->s_checkinterval) <= get_seconds()))
649                 ext2_msg(sb, KERN_WARNING,
650                         "warning: checktime reached, "
651                         "running e2fsck is recommended");
652         if (!le16_to_cpu(es->s_max_mnt_count))
653                 es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
654         le16_add_cpu(&es->s_mnt_count, 1);
655         if (test_opt (sb, DEBUG))
656                 ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, fs=%lu, gc=%lu, "
657                         "bpg=%lu, ipg=%lu, mo=%04lx]",
658                         EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
659                         sbi->s_frag_size,
660                         sbi->s_groups_count,
661                         EXT2_BLOCKS_PER_GROUP(sb),
662                         EXT2_INODES_PER_GROUP(sb),
663                         sbi->s_mount_opt);
664         return res;
665 }
666
667 static int ext2_check_descriptors(struct super_block *sb)
668 {
669         int i;
670         struct ext2_sb_info *sbi = EXT2_SB(sb);
671
672         ext2_debug ("Checking group descriptors");
673
674         for (i = 0; i < sbi->s_groups_count; i++) {
675                 struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
676                 ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
677                 ext2_fsblk_t last_block;
678
679                 if (i == sbi->s_groups_count - 1)
680                         last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
681                 else
682                         last_block = first_block +
683                                 (EXT2_BLOCKS_PER_GROUP(sb) - 1);
684
685                 if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
686                     le32_to_cpu(gdp->bg_block_bitmap) > last_block)
687                 {
688                         ext2_error (sb, "ext2_check_descriptors",
689                                     "Block bitmap for group %d"
690                                     " not in group (block %lu)!",
691                                     i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
692                         return 0;
693                 }
694                 if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
695                     le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
696                 {
697                         ext2_error (sb, "ext2_check_descriptors",
698                                     "Inode bitmap for group %d"
699                                     " not in group (block %lu)!",
700                                     i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
701                         return 0;
702                 }
703                 if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
704                     le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
705                     last_block)
706                 {
707                         ext2_error (sb, "ext2_check_descriptors",
708                                     "Inode table for group %d"
709                                     " not in group (block %lu)!",
710                                     i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
711                         return 0;
712                 }
713         }
714         return 1;
715 }
716
717 /*
718  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
719  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
720  * We need to be 1 filesystem block less than the 2^32 sector limit.
721  */
722 static loff_t ext2_max_size(int bits)
723 {
724         loff_t res = EXT2_NDIR_BLOCKS;
725         int meta_blocks;
726         loff_t upper_limit;
727
728         /* This is calculated to be the largest file size for a
729          * dense, file such that the total number of
730          * sectors in the file, including data and all indirect blocks,
731          * does not exceed 2^32 -1
732          * __u32 i_blocks representing the total number of
733          * 512 bytes blocks of the file
734          */
735         upper_limit = (1LL << 32) - 1;
736
737         /* total blocks in file system block size */
738         upper_limit >>= (bits - 9);
739
740
741         /* indirect blocks */
742         meta_blocks = 1;
743         /* double indirect blocks */
744         meta_blocks += 1 + (1LL << (bits-2));
745         /* tripple indirect blocks */
746         meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
747
748         upper_limit -= meta_blocks;
749         upper_limit <<= bits;
750
751         res += 1LL << (bits-2);
752         res += 1LL << (2*(bits-2));
753         res += 1LL << (3*(bits-2));
754         res <<= bits;
755         if (res > upper_limit)
756                 res = upper_limit;
757
758         if (res > MAX_LFS_FILESIZE)
759                 res = MAX_LFS_FILESIZE;
760
761         return res;
762 }
763
764 static unsigned long descriptor_loc(struct super_block *sb,
765                                     unsigned long logic_sb_block,
766                                     int nr)
767 {
768         struct ext2_sb_info *sbi = EXT2_SB(sb);
769         unsigned long bg, first_meta_bg;
770         int has_super = 0;
771         
772         first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
773
774         if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
775             nr < first_meta_bg)
776                 return (logic_sb_block + nr + 1);
777         bg = sbi->s_desc_per_block * nr;
778         if (ext2_bg_has_super(sb, bg))
779                 has_super = 1;
780
781         return ext2_group_first_block_no(sb, bg) + has_super;
782 }
783
784 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
785 {
786         struct buffer_head * bh;
787         struct ext2_sb_info * sbi;
788         struct ext2_super_block * es;
789         struct inode *root;
790         unsigned long block;
791         unsigned long sb_block = get_sb_block(&data);
792         unsigned long logic_sb_block;
793         unsigned long offset = 0;
794         unsigned long def_mount_opts;
795         long ret = -EINVAL;
796         int blocksize = BLOCK_SIZE;
797         int db_count;
798         int i, j;
799         __le32 features;
800         int err;
801
802         err = -ENOMEM;
803         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
804         if (!sbi)
805                 goto failed;
806
807         sbi->s_blockgroup_lock =
808                 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
809         if (!sbi->s_blockgroup_lock) {
810                 kfree(sbi);
811                 goto failed;
812         }
813         sb->s_fs_info = sbi;
814         sbi->s_sb_block = sb_block;
815
816         spin_lock_init(&sbi->s_lock);
817
818         /*
819          * See what the current blocksize for the device is, and
820          * use that as the blocksize.  Otherwise (or if the blocksize
821          * is smaller than the default) use the default.
822          * This is important for devices that have a hardware
823          * sectorsize that is larger than the default.
824          */
825         blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
826         if (!blocksize) {
827                 ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
828                 goto failed_sbi;
829         }
830
831         /*
832          * If the superblock doesn't start on a hardware sector boundary,
833          * calculate the offset.  
834          */
835         if (blocksize != BLOCK_SIZE) {
836                 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
837                 offset = (sb_block*BLOCK_SIZE) % blocksize;
838         } else {
839                 logic_sb_block = sb_block;
840         }
841
842         if (!(bh = sb_bread(sb, logic_sb_block))) {
843                 ext2_msg(sb, KERN_ERR, "error: unable to read superblock");
844                 goto failed_sbi;
845         }
846         /*
847          * Note: s_es must be initialized as soon as possible because
848          *       some ext2 macro-instructions depend on its value
849          */
850         es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
851         sbi->s_es = es;
852         sb->s_magic = le16_to_cpu(es->s_magic);
853
854         if (sb->s_magic != EXT2_SUPER_MAGIC)
855                 goto cantfind_ext2;
856
857         /* Set defaults before we parse the mount options */
858         def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
859         if (def_mount_opts & EXT2_DEFM_DEBUG)
860                 set_opt(sbi->s_mount_opt, DEBUG);
861         if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
862                 set_opt(sbi->s_mount_opt, GRPID);
863         if (def_mount_opts & EXT2_DEFM_UID16)
864                 set_opt(sbi->s_mount_opt, NO_UID32);
865 #ifdef CONFIG_EXT2_FS_XATTR
866         if (def_mount_opts & EXT2_DEFM_XATTR_USER)
867                 set_opt(sbi->s_mount_opt, XATTR_USER);
868 #endif
869 #ifdef CONFIG_EXT2_FS_POSIX_ACL
870         if (def_mount_opts & EXT2_DEFM_ACL)
871                 set_opt(sbi->s_mount_opt, POSIX_ACL);
872 #endif
873         
874         if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
875                 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
876         else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
877                 set_opt(sbi->s_mount_opt, ERRORS_CONT);
878         else
879                 set_opt(sbi->s_mount_opt, ERRORS_RO);
880
881         sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
882         sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
883         
884         set_opt(sbi->s_mount_opt, RESERVATION);
885
886         if (!parse_options((char *) data, sb))
887                 goto failed_mount;
888
889         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
890                 ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
891                  MS_POSIXACL : 0);
892         sb->s_iflags |= SB_I_CGROUPWB;
893
894         if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
895             (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
896              EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
897              EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
898                 ext2_msg(sb, KERN_WARNING,
899                         "warning: feature flags set on rev 0 fs, "
900                         "running e2fsck is recommended");
901         /*
902          * Check feature flags regardless of the revision level, since we
903          * previously didn't change the revision level when setting the flags,
904          * so there is a chance incompat flags are set on a rev 0 filesystem.
905          */
906         features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
907         if (features) {
908                 ext2_msg(sb, KERN_ERR,  "error: couldn't mount because of "
909                        "unsupported optional features (%x)",
910                         le32_to_cpu(features));
911                 goto failed_mount;
912         }
913         if (!(sb->s_flags & MS_RDONLY) &&
914             (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
915                 ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of "
916                        "unsupported optional features (%x)",
917                        le32_to_cpu(features));
918                 goto failed_mount;
919         }
920
921         blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
922
923         if (sbi->s_mount_opt & EXT2_MOUNT_DAX) {
924                 err = bdev_dax_supported(sb, blocksize);
925                 if (err)
926                         goto failed_mount;
927         }
928
929         /* If the blocksize doesn't match, re-read the thing.. */
930         if (sb->s_blocksize != blocksize) {
931                 brelse(bh);
932
933                 if (!sb_set_blocksize(sb, blocksize)) {
934                         ext2_msg(sb, KERN_ERR,
935                                 "error: bad blocksize %d", blocksize);
936                         goto failed_sbi;
937                 }
938
939                 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
940                 offset = (sb_block*BLOCK_SIZE) % blocksize;
941                 bh = sb_bread(sb, logic_sb_block);
942                 if(!bh) {
943                         ext2_msg(sb, KERN_ERR, "error: couldn't read"
944                                 "superblock on 2nd try");
945                         goto failed_sbi;
946                 }
947                 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
948                 sbi->s_es = es;
949                 if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
950                         ext2_msg(sb, KERN_ERR, "error: magic mismatch");
951                         goto failed_mount;
952                 }
953         }
954
955         sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
956         sb->s_max_links = EXT2_LINK_MAX;
957
958         if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
959                 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
960                 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
961         } else {
962                 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
963                 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
964                 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
965                     !is_power_of_2(sbi->s_inode_size) ||
966                     (sbi->s_inode_size > blocksize)) {
967                         ext2_msg(sb, KERN_ERR,
968                                 "error: unsupported inode size: %d",
969                                 sbi->s_inode_size);
970                         goto failed_mount;
971                 }
972         }
973
974         sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
975                                    le32_to_cpu(es->s_log_frag_size);
976         if (sbi->s_frag_size == 0)
977                 goto cantfind_ext2;
978         sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
979
980         sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
981         sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
982         sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
983
984         if (EXT2_INODE_SIZE(sb) == 0)
985                 goto cantfind_ext2;
986         sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
987         if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
988                 goto cantfind_ext2;
989         sbi->s_itb_per_group = sbi->s_inodes_per_group /
990                                         sbi->s_inodes_per_block;
991         sbi->s_desc_per_block = sb->s_blocksize /
992                                         sizeof (struct ext2_group_desc);
993         sbi->s_sbh = bh;
994         sbi->s_mount_state = le16_to_cpu(es->s_state);
995         sbi->s_addr_per_block_bits =
996                 ilog2 (EXT2_ADDR_PER_BLOCK(sb));
997         sbi->s_desc_per_block_bits =
998                 ilog2 (EXT2_DESC_PER_BLOCK(sb));
999
1000         if (sb->s_magic != EXT2_SUPER_MAGIC)
1001                 goto cantfind_ext2;
1002
1003         if (sb->s_blocksize != bh->b_size) {
1004                 if (!silent)
1005                         ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
1006                 goto failed_mount;
1007         }
1008
1009         if (sb->s_blocksize != sbi->s_frag_size) {
1010                 ext2_msg(sb, KERN_ERR,
1011                         "error: fragsize %lu != blocksize %lu"
1012                         "(not supported yet)",
1013                         sbi->s_frag_size, sb->s_blocksize);
1014                 goto failed_mount;
1015         }
1016
1017         if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
1018                 ext2_msg(sb, KERN_ERR,
1019                         "error: #blocks per group too big: %lu",
1020                         sbi->s_blocks_per_group);
1021                 goto failed_mount;
1022         }
1023         if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
1024                 ext2_msg(sb, KERN_ERR,
1025                         "error: #fragments per group too big: %lu",
1026                         sbi->s_frags_per_group);
1027                 goto failed_mount;
1028         }
1029         if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
1030                 ext2_msg(sb, KERN_ERR,
1031                         "error: #inodes per group too big: %lu",
1032                         sbi->s_inodes_per_group);
1033                 goto failed_mount;
1034         }
1035
1036         if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
1037                 goto cantfind_ext2;
1038         sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
1039                                 le32_to_cpu(es->s_first_data_block) - 1)
1040                                         / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
1041         db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
1042                    EXT2_DESC_PER_BLOCK(sb);
1043         sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
1044         if (sbi->s_group_desc == NULL) {
1045                 ext2_msg(sb, KERN_ERR, "error: not enough memory");
1046                 goto failed_mount;
1047         }
1048         bgl_lock_init(sbi->s_blockgroup_lock);
1049         sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
1050         if (!sbi->s_debts) {
1051                 ext2_msg(sb, KERN_ERR, "error: not enough memory");
1052                 goto failed_mount_group_desc;
1053         }
1054         for (i = 0; i < db_count; i++) {
1055                 block = descriptor_loc(sb, logic_sb_block, i);
1056                 sbi->s_group_desc[i] = sb_bread(sb, block);
1057                 if (!sbi->s_group_desc[i]) {
1058                         for (j = 0; j < i; j++)
1059                                 brelse (sbi->s_group_desc[j]);
1060                         ext2_msg(sb, KERN_ERR,
1061                                 "error: unable to read group descriptors");
1062                         goto failed_mount_group_desc;
1063                 }
1064         }
1065         if (!ext2_check_descriptors (sb)) {
1066                 ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
1067                 goto failed_mount2;
1068         }
1069         sbi->s_gdb_count = db_count;
1070         get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1071         spin_lock_init(&sbi->s_next_gen_lock);
1072
1073         /* per fileystem reservation list head & lock */
1074         spin_lock_init(&sbi->s_rsv_window_lock);
1075         sbi->s_rsv_window_root = RB_ROOT;
1076         /*
1077          * Add a single, static dummy reservation to the start of the
1078          * reservation window list --- it gives us a placeholder for
1079          * append-at-start-of-list which makes the allocation logic
1080          * _much_ simpler.
1081          */
1082         sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
1083         sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
1084         sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1085         sbi->s_rsv_window_head.rsv_goal_size = 0;
1086         ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
1087
1088         err = percpu_counter_init(&sbi->s_freeblocks_counter,
1089                                 ext2_count_free_blocks(sb), GFP_KERNEL);
1090         if (!err) {
1091                 err = percpu_counter_init(&sbi->s_freeinodes_counter,
1092                                 ext2_count_free_inodes(sb), GFP_KERNEL);
1093         }
1094         if (!err) {
1095                 err = percpu_counter_init(&sbi->s_dirs_counter,
1096                                 ext2_count_dirs(sb), GFP_KERNEL);
1097         }
1098         if (err) {
1099                 ext2_msg(sb, KERN_ERR, "error: insufficient memory");
1100                 goto failed_mount3;
1101         }
1102
1103 #ifdef CONFIG_EXT2_FS_XATTR
1104         sbi->s_mb_cache = ext2_xattr_create_cache();
1105         if (!sbi->s_mb_cache) {
1106                 ext2_msg(sb, KERN_ERR, "Failed to create an mb_cache");
1107                 goto failed_mount3;
1108         }
1109 #endif
1110         /*
1111          * set up enough so that it can read an inode
1112          */
1113         sb->s_op = &ext2_sops;
1114         sb->s_export_op = &ext2_export_ops;
1115         sb->s_xattr = ext2_xattr_handlers;
1116
1117 #ifdef CONFIG_QUOTA
1118         sb->dq_op = &dquot_operations;
1119         sb->s_qcop = &dquot_quotactl_ops;
1120         sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
1121 #endif
1122
1123         root = ext2_iget(sb, EXT2_ROOT_INO);
1124         if (IS_ERR(root)) {
1125                 ret = PTR_ERR(root);
1126                 goto failed_mount3;
1127         }
1128         if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1129                 iput(root);
1130                 ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
1131                 goto failed_mount3;
1132         }
1133
1134         sb->s_root = d_make_root(root);
1135         if (!sb->s_root) {
1136                 ext2_msg(sb, KERN_ERR, "error: get root inode failed");
1137                 ret = -ENOMEM;
1138                 goto failed_mount3;
1139         }
1140         if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1141                 ext2_msg(sb, KERN_WARNING,
1142                         "warning: mounting ext3 filesystem as ext2");
1143         if (ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY))
1144                 sb->s_flags |= MS_RDONLY;
1145         ext2_write_super(sb);
1146         return 0;
1147
1148 cantfind_ext2:
1149         if (!silent)
1150                 ext2_msg(sb, KERN_ERR,
1151                         "error: can't find an ext2 filesystem on dev %s.",
1152                         sb->s_id);
1153         goto failed_mount;
1154 failed_mount3:
1155         if (sbi->s_mb_cache)
1156                 ext2_xattr_destroy_cache(sbi->s_mb_cache);
1157         percpu_counter_destroy(&sbi->s_freeblocks_counter);
1158         percpu_counter_destroy(&sbi->s_freeinodes_counter);
1159         percpu_counter_destroy(&sbi->s_dirs_counter);
1160 failed_mount2:
1161         for (i = 0; i < db_count; i++)
1162                 brelse(sbi->s_group_desc[i]);
1163 failed_mount_group_desc:
1164         kfree(sbi->s_group_desc);
1165         kfree(sbi->s_debts);
1166 failed_mount:
1167         brelse(bh);
1168 failed_sbi:
1169         sb->s_fs_info = NULL;
1170         kfree(sbi->s_blockgroup_lock);
1171         kfree(sbi);
1172 failed:
1173         return ret;
1174 }
1175
1176 static void ext2_clear_super_error(struct super_block *sb)
1177 {
1178         struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;
1179
1180         if (buffer_write_io_error(sbh)) {
1181                 /*
1182                  * Oh, dear.  A previous attempt to write the
1183                  * superblock failed.  This could happen because the
1184                  * USB device was yanked out.  Or it could happen to
1185                  * be a transient write error and maybe the block will
1186                  * be remapped.  Nothing we can do but to retry the
1187                  * write and hope for the best.
1188                  */
1189                 ext2_msg(sb, KERN_ERR,
1190                        "previous I/O error to superblock detected\n");
1191                 clear_buffer_write_io_error(sbh);
1192                 set_buffer_uptodate(sbh);
1193         }
1194 }
1195
1196 void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es,
1197                      int wait)
1198 {
1199         ext2_clear_super_error(sb);
1200         spin_lock(&EXT2_SB(sb)->s_lock);
1201         es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
1202         es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
1203         es->s_wtime = cpu_to_le32(get_seconds());
1204         /* unlock before we do IO */
1205         spin_unlock(&EXT2_SB(sb)->s_lock);
1206         mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
1207         if (wait)
1208                 sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
1209 }
1210
1211 /*
1212  * In the second extended file system, it is not necessary to
1213  * write the super block since we use a mapping of the
1214  * disk super block in a buffer.
1215  *
1216  * However, this function is still used to set the fs valid
1217  * flags to 0.  We need to set this flag to 0 since the fs
1218  * may have been checked while mounted and e2fsck may have
1219  * set s_state to EXT2_VALID_FS after some corrections.
1220  */
1221 static int ext2_sync_fs(struct super_block *sb, int wait)
1222 {
1223         struct ext2_sb_info *sbi = EXT2_SB(sb);
1224         struct ext2_super_block *es = EXT2_SB(sb)->s_es;
1225
1226         /*
1227          * Write quota structures to quota file, sync_blockdev() will write
1228          * them to disk later
1229          */
1230         dquot_writeback_dquots(sb, -1);
1231
1232         spin_lock(&sbi->s_lock);
1233         if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) {
1234                 ext2_debug("setting valid to 0\n");
1235                 es->s_state &= cpu_to_le16(~EXT2_VALID_FS);
1236         }
1237         spin_unlock(&sbi->s_lock);
1238         ext2_sync_super(sb, es, wait);
1239         return 0;
1240 }
1241
1242 static int ext2_freeze(struct super_block *sb)
1243 {
1244         struct ext2_sb_info *sbi = EXT2_SB(sb);
1245
1246         /*
1247          * Open but unlinked files present? Keep EXT2_VALID_FS flag cleared
1248          * because we have unattached inodes and thus filesystem is not fully
1249          * consistent.
1250          */
1251         if (atomic_long_read(&sb->s_remove_count)) {
1252                 ext2_sync_fs(sb, 1);
1253                 return 0;
1254         }
1255         /* Set EXT2_FS_VALID flag */
1256         spin_lock(&sbi->s_lock);
1257         sbi->s_es->s_state = cpu_to_le16(sbi->s_mount_state);
1258         spin_unlock(&sbi->s_lock);
1259         ext2_sync_super(sb, sbi->s_es, 1);
1260
1261         return 0;
1262 }
1263
1264 static int ext2_unfreeze(struct super_block *sb)
1265 {
1266         /* Just write sb to clear EXT2_VALID_FS flag */
1267         ext2_write_super(sb);
1268
1269         return 0;
1270 }
1271
1272 static void ext2_write_super(struct super_block *sb)
1273 {
1274         if (!(sb->s_flags & MS_RDONLY))
1275                 ext2_sync_fs(sb, 1);
1276 }
1277
1278 static int ext2_remount (struct super_block * sb, int * flags, char * data)
1279 {
1280         struct ext2_sb_info * sbi = EXT2_SB(sb);
1281         struct ext2_super_block * es;
1282         struct ext2_mount_options old_opts;
1283         unsigned long old_sb_flags;
1284         int err;
1285
1286         sync_filesystem(sb);
1287         spin_lock(&sbi->s_lock);
1288
1289         /* Store the old options */
1290         old_sb_flags = sb->s_flags;
1291         old_opts.s_mount_opt = sbi->s_mount_opt;
1292         old_opts.s_resuid = sbi->s_resuid;
1293         old_opts.s_resgid = sbi->s_resgid;
1294
1295         /*
1296          * Allow the "check" option to be passed as a remount option.
1297          */
1298         if (!parse_options(data, sb)) {
1299                 err = -EINVAL;
1300                 goto restore_opts;
1301         }
1302
1303         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1304                 ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1305
1306         es = sbi->s_es;
1307         if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT2_MOUNT_DAX) {
1308                 ext2_msg(sb, KERN_WARNING, "warning: refusing change of "
1309                          "dax flag with busy inodes while remounting");
1310                 sbi->s_mount_opt ^= EXT2_MOUNT_DAX;
1311         }
1312         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1313                 spin_unlock(&sbi->s_lock);
1314                 return 0;
1315         }
1316         if (*flags & MS_RDONLY) {
1317                 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
1318                     !(sbi->s_mount_state & EXT2_VALID_FS)) {
1319                         spin_unlock(&sbi->s_lock);
1320                         return 0;
1321                 }
1322
1323                 /*
1324                  * OK, we are remounting a valid rw partition rdonly, so set
1325                  * the rdonly flag and then mark the partition as valid again.
1326                  */
1327                 es->s_state = cpu_to_le16(sbi->s_mount_state);
1328                 es->s_mtime = cpu_to_le32(get_seconds());
1329                 spin_unlock(&sbi->s_lock);
1330
1331                 err = dquot_suspend(sb, -1);
1332                 if (err < 0) {
1333                         spin_lock(&sbi->s_lock);
1334                         goto restore_opts;
1335                 }
1336
1337                 ext2_sync_super(sb, es, 1);
1338         } else {
1339                 __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
1340                                                ~EXT2_FEATURE_RO_COMPAT_SUPP);
1341                 if (ret) {
1342                         ext2_msg(sb, KERN_WARNING,
1343                                 "warning: couldn't remount RDWR because of "
1344                                 "unsupported optional features (%x).",
1345                                 le32_to_cpu(ret));
1346                         err = -EROFS;
1347                         goto restore_opts;
1348                 }
1349                 /*
1350                  * Mounting a RDONLY partition read-write, so reread and
1351                  * store the current valid flag.  (It may have been changed
1352                  * by e2fsck since we originally mounted the partition.)
1353                  */
1354                 sbi->s_mount_state = le16_to_cpu(es->s_state);
1355                 if (!ext2_setup_super (sb, es, 0))
1356                         sb->s_flags &= ~MS_RDONLY;
1357                 spin_unlock(&sbi->s_lock);
1358
1359                 ext2_write_super(sb);
1360
1361                 dquot_resume(sb, -1);
1362         }
1363
1364         return 0;
1365 restore_opts:
1366         sbi->s_mount_opt = old_opts.s_mount_opt;
1367         sbi->s_resuid = old_opts.s_resuid;
1368         sbi->s_resgid = old_opts.s_resgid;
1369         sb->s_flags = old_sb_flags;
1370         spin_unlock(&sbi->s_lock);
1371         return err;
1372 }
1373
1374 static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
1375 {
1376         struct super_block *sb = dentry->d_sb;
1377         struct ext2_sb_info *sbi = EXT2_SB(sb);
1378         struct ext2_super_block *es = sbi->s_es;
1379         u64 fsid;
1380
1381         spin_lock(&sbi->s_lock);
1382
1383         if (test_opt (sb, MINIX_DF))
1384                 sbi->s_overhead_last = 0;
1385         else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
1386                 unsigned long i, overhead = 0;
1387                 smp_rmb();
1388
1389                 /*
1390                  * Compute the overhead (FS structures). This is constant
1391                  * for a given filesystem unless the number of block groups
1392                  * changes so we cache the previous value until it does.
1393                  */
1394
1395                 /*
1396                  * All of the blocks before first_data_block are
1397                  * overhead
1398                  */
1399                 overhead = le32_to_cpu(es->s_first_data_block);
1400
1401                 /*
1402                  * Add the overhead attributed to the superblock and
1403                  * block group descriptors.  If the sparse superblocks
1404                  * feature is turned on, then not all groups have this.
1405                  */
1406                 for (i = 0; i < sbi->s_groups_count; i++)
1407                         overhead += ext2_bg_has_super(sb, i) +
1408                                 ext2_bg_num_gdb(sb, i);
1409
1410                 /*
1411                  * Every block group has an inode bitmap, a block
1412                  * bitmap, and an inode table.
1413                  */
1414                 overhead += (sbi->s_groups_count *
1415                              (2 + sbi->s_itb_per_group));
1416                 sbi->s_overhead_last = overhead;
1417                 smp_wmb();
1418                 sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
1419         }
1420
1421         buf->f_type = EXT2_SUPER_MAGIC;
1422         buf->f_bsize = sb->s_blocksize;
1423         buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
1424         buf->f_bfree = ext2_count_free_blocks(sb);
1425         es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
1426         buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
1427         if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
1428                 buf->f_bavail = 0;
1429         buf->f_files = le32_to_cpu(es->s_inodes_count);
1430         buf->f_ffree = ext2_count_free_inodes(sb);
1431         es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
1432         buf->f_namelen = EXT2_NAME_LEN;
1433         fsid = le64_to_cpup((void *)es->s_uuid) ^
1434                le64_to_cpup((void *)es->s_uuid + sizeof(u64));
1435         buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1436         buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1437         spin_unlock(&sbi->s_lock);
1438         return 0;
1439 }
1440
1441 static struct dentry *ext2_mount(struct file_system_type *fs_type,
1442         int flags, const char *dev_name, void *data)
1443 {
1444         return mount_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
1445 }
1446
1447 #ifdef CONFIG_QUOTA
1448
1449 /* Read data from quotafile - avoid pagecache and such because we cannot afford
1450  * acquiring the locks... As quota files are never truncated and quota code
1451  * itself serializes the operations (and no one else should touch the files)
1452  * we don't have to be afraid of races */
1453 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
1454                                size_t len, loff_t off)
1455 {
1456         struct inode *inode = sb_dqopt(sb)->files[type];
1457         sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
1458         int err = 0;
1459         int offset = off & (sb->s_blocksize - 1);
1460         int tocopy;
1461         size_t toread;
1462         struct buffer_head tmp_bh;
1463         struct buffer_head *bh;
1464         loff_t i_size = i_size_read(inode);
1465
1466         if (off > i_size)
1467                 return 0;
1468         if (off+len > i_size)
1469                 len = i_size-off;
1470         toread = len;
1471         while (toread > 0) {
1472                 tocopy = sb->s_blocksize - offset < toread ?
1473                                 sb->s_blocksize - offset : toread;
1474
1475                 tmp_bh.b_state = 0;
1476                 tmp_bh.b_size = sb->s_blocksize;
1477                 err = ext2_get_block(inode, blk, &tmp_bh, 0);
1478                 if (err < 0)
1479                         return err;
1480                 if (!buffer_mapped(&tmp_bh))    /* A hole? */
1481                         memset(data, 0, tocopy);
1482                 else {
1483                         bh = sb_bread(sb, tmp_bh.b_blocknr);
1484                         if (!bh)
1485                                 return -EIO;
1486                         memcpy(data, bh->b_data+offset, tocopy);
1487                         brelse(bh);
1488                 }
1489                 offset = 0;
1490                 toread -= tocopy;
1491                 data += tocopy;
1492                 blk++;
1493         }
1494         return len;
1495 }
1496
1497 /* Write to quotafile */
1498 static ssize_t ext2_quota_write(struct super_block *sb, int type,
1499                                 const char *data, size_t len, loff_t off)
1500 {
1501         struct inode *inode = sb_dqopt(sb)->files[type];
1502         sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
1503         int err = 0;
1504         int offset = off & (sb->s_blocksize - 1);
1505         int tocopy;
1506         size_t towrite = len;
1507         struct buffer_head tmp_bh;
1508         struct buffer_head *bh;
1509
1510         while (towrite > 0) {
1511                 tocopy = sb->s_blocksize - offset < towrite ?
1512                                 sb->s_blocksize - offset : towrite;
1513
1514                 tmp_bh.b_state = 0;
1515                 tmp_bh.b_size = sb->s_blocksize;
1516                 err = ext2_get_block(inode, blk, &tmp_bh, 1);
1517                 if (err < 0)
1518                         goto out;
1519                 if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
1520                         bh = sb_bread(sb, tmp_bh.b_blocknr);
1521                 else
1522                         bh = sb_getblk(sb, tmp_bh.b_blocknr);
1523                 if (unlikely(!bh)) {
1524                         err = -EIO;
1525                         goto out;
1526                 }
1527                 lock_buffer(bh);
1528                 memcpy(bh->b_data+offset, data, tocopy);
1529                 flush_dcache_page(bh->b_page);
1530                 set_buffer_uptodate(bh);
1531                 mark_buffer_dirty(bh);
1532                 unlock_buffer(bh);
1533                 brelse(bh);
1534                 offset = 0;
1535                 towrite -= tocopy;
1536                 data += tocopy;
1537                 blk++;
1538         }
1539 out:
1540         if (len == towrite)
1541                 return err;
1542         if (inode->i_size < off+len-towrite)
1543                 i_size_write(inode, off+len-towrite);
1544         inode->i_version++;
1545         inode->i_mtime = inode->i_ctime = current_time(inode);
1546         mark_inode_dirty(inode);
1547         return len - towrite;
1548 }
1549
1550 #endif
1551
1552 static struct file_system_type ext2_fs_type = {
1553         .owner          = THIS_MODULE,
1554         .name           = "ext2",
1555         .mount          = ext2_mount,
1556         .kill_sb        = kill_block_super,
1557         .fs_flags       = FS_REQUIRES_DEV,
1558 };
1559 MODULE_ALIAS_FS("ext2");
1560
1561 static int __init init_ext2_fs(void)
1562 {
1563         int err;
1564
1565         err = init_inodecache();
1566         if (err)
1567                 return err;
1568         err = register_filesystem(&ext2_fs_type);
1569         if (err)
1570                 goto out;
1571         return 0;
1572 out:
1573         destroy_inodecache();
1574         return err;
1575 }
1576
1577 static void __exit exit_ext2_fs(void)
1578 {
1579         unregister_filesystem(&ext2_fs_type);
1580         destroy_inodecache();
1581 }
1582
1583 MODULE_AUTHOR("Remy Card and others");
1584 MODULE_DESCRIPTION("Second Extended Filesystem");
1585 MODULE_LICENSE("GPL");
1586 module_init(init_ext2_fs)
1587 module_exit(exit_ext2_fs)