ext4: Remove i_ext_generation from ext4_inode_info structure
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / fs / ext4 / super.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/super.c
ac27a0ec
DK
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>
dab291af 23#include <linux/jbd2.h>
ac27a0ec
DK
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/blkdev.h>
27#include <linux/parser.h>
28#include <linux/smp_lock.h>
29#include <linux/buffer_head.h>
a5694255 30#include <linux/exportfs.h>
ac27a0ec
DK
31#include <linux/vfs.h>
32#include <linux/random.h>
33#include <linux/mount.h>
34#include <linux/namei.h>
35#include <linux/quotaops.h>
36#include <linux/seq_file.h>
9f6200bb 37#include <linux/proc_fs.h>
ede86cc4 38#include <linux/marker.h>
1330593e 39#include <linux/log2.h>
717d50e4 40#include <linux/crc16.h>
ac27a0ec
DK
41#include <asm/uaccess.h>
42
3dcf5451
CH
43#include "ext4.h"
44#include "ext4_jbd2.h"
ac27a0ec
DK
45#include "xattr.h"
46#include "acl.h"
47#include "namei.h"
717d50e4 48#include "group.h"
ac27a0ec 49
9f6200bb
TT
50struct proc_dir_entry *ext4_proc_root;
51
617ba13b 52static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
ac27a0ec 53 unsigned long journal_devnum);
617ba13b 54static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
ac27a0ec 55 unsigned int);
2b2d6d01
TT
56static void ext4_commit_super(struct super_block *sb,
57 struct ext4_super_block *es, int sync);
58static void ext4_mark_recovery_complete(struct super_block *sb,
59 struct ext4_super_block *es);
60static void ext4_clear_journal_err(struct super_block *sb,
61 struct ext4_super_block *es);
617ba13b 62static int ext4_sync_fs(struct super_block *sb, int wait);
2b2d6d01 63static const char *ext4_decode_error(struct super_block *sb, int errno,
ac27a0ec 64 char nbuf[16]);
2b2d6d01
TT
65static int ext4_remount(struct super_block *sb, int *flags, char *data);
66static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
617ba13b 67static void ext4_unlockfs(struct super_block *sb);
2b2d6d01 68static void ext4_write_super(struct super_block *sb);
617ba13b 69static void ext4_write_super_lockfs(struct super_block *sb);
ac27a0ec 70
bd81d8ee 71
8fadc143
AR
72ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
73 struct ext4_group_desc *bg)
bd81d8ee 74{
3a14589c 75 return le32_to_cpu(bg->bg_block_bitmap_lo) |
8fadc143 76 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3a14589c 77 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
bd81d8ee
LV
78}
79
8fadc143
AR
80ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
81 struct ext4_group_desc *bg)
bd81d8ee 82{
5272f837 83 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
8fadc143 84 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
5272f837 85 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
bd81d8ee
LV
86}
87
8fadc143
AR
88ext4_fsblk_t ext4_inode_table(struct super_block *sb,
89 struct ext4_group_desc *bg)
bd81d8ee 90{
5272f837 91 return le32_to_cpu(bg->bg_inode_table_lo) |
8fadc143 92 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
5272f837 93 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
bd81d8ee
LV
94}
95
8fadc143
AR
96void ext4_block_bitmap_set(struct super_block *sb,
97 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 98{
3a14589c 99 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
8fadc143
AR
100 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
101 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
102}
103
8fadc143
AR
104void ext4_inode_bitmap_set(struct super_block *sb,
105 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 106{
5272f837 107 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
8fadc143
AR
108 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
109 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
110}
111
8fadc143
AR
112void ext4_inode_table_set(struct super_block *sb,
113 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 114{
5272f837 115 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
8fadc143
AR
116 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
117 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
118}
119
ac27a0ec 120/*
dab291af 121 * Wrappers for jbd2_journal_start/end.
ac27a0ec
DK
122 *
123 * The only special thing we need to do here is to make sure that all
124 * journal_end calls result in the superblock being marked dirty, so
125 * that sync() will call the filesystem's write_super callback if
126 * appropriate.
127 */
617ba13b 128handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
ac27a0ec
DK
129{
130 journal_t *journal;
131
132 if (sb->s_flags & MS_RDONLY)
133 return ERR_PTR(-EROFS);
134
135 /* Special case here: if the journal has aborted behind our
136 * backs (eg. EIO in the commit thread), then we still need to
137 * take the FS itself readonly cleanly. */
617ba13b 138 journal = EXT4_SB(sb)->s_journal;
0390131b
FM
139 if (journal) {
140 if (is_journal_aborted(journal)) {
141 ext4_abort(sb, __func__,
142 "Detected aborted journal");
143 return ERR_PTR(-EROFS);
144 }
145 return jbd2_journal_start(journal, nblocks);
ac27a0ec 146 }
0390131b
FM
147 /*
148 * We're not journaling, return the appropriate indication.
149 */
150 current->journal_info = EXT4_NOJOURNAL_HANDLE;
151 return current->journal_info;
ac27a0ec
DK
152}
153
154/*
155 * The only special thing we need to do here is to make sure that all
dab291af 156 * jbd2_journal_stop calls result in the superblock being marked dirty, so
ac27a0ec
DK
157 * that sync() will call the filesystem's write_super callback if
158 * appropriate.
159 */
617ba13b 160int __ext4_journal_stop(const char *where, handle_t *handle)
ac27a0ec
DK
161{
162 struct super_block *sb;
163 int err;
164 int rc;
165
0390131b
FM
166 if (!ext4_handle_valid(handle)) {
167 /*
168 * Do this here since we don't call jbd2_journal_stop() in
169 * no-journal mode.
170 */
171 current->journal_info = NULL;
172 return 0;
173 }
ac27a0ec
DK
174 sb = handle->h_transaction->t_journal->j_private;
175 err = handle->h_err;
dab291af 176 rc = jbd2_journal_stop(handle);
ac27a0ec
DK
177
178 if (!err)
179 err = rc;
180 if (err)
617ba13b 181 __ext4_std_error(sb, where, err);
ac27a0ec
DK
182 return err;
183}
184
617ba13b 185void ext4_journal_abort_handle(const char *caller, const char *err_fn,
ac27a0ec
DK
186 struct buffer_head *bh, handle_t *handle, int err)
187{
188 char nbuf[16];
617ba13b 189 const char *errstr = ext4_decode_error(NULL, err, nbuf);
ac27a0ec 190
0390131b
FM
191 BUG_ON(!ext4_handle_valid(handle));
192
ac27a0ec
DK
193 if (bh)
194 BUFFER_TRACE(bh, "abort");
195
196 if (!handle->h_err)
197 handle->h_err = err;
198
199 if (is_handle_aborted(handle))
200 return;
201
202 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
203 caller, errstr, err_fn);
204
dab291af 205 jbd2_journal_abort_handle(handle);
ac27a0ec
DK
206}
207
208/* Deal with the reporting of failure conditions on a filesystem such as
209 * inconsistencies detected or read IO failures.
210 *
211 * On ext2, we can store the error state of the filesystem in the
617ba13b 212 * superblock. That is not possible on ext4, because we may have other
ac27a0ec
DK
213 * write ordering constraints on the superblock which prevent us from
214 * writing it out straight away; and given that the journal is about to
215 * be aborted, we can't rely on the current, or future, transactions to
216 * write out the superblock safely.
217 *
dab291af 218 * We'll just use the jbd2_journal_abort() error code to record an error in
ac27a0ec
DK
219 * the journal instead. On recovery, the journal will compain about
220 * that error until we've noted it down and cleared it.
221 */
222
617ba13b 223static void ext4_handle_error(struct super_block *sb)
ac27a0ec 224{
617ba13b 225 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
ac27a0ec 226
617ba13b
MC
227 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
228 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
ac27a0ec
DK
229
230 if (sb->s_flags & MS_RDONLY)
231 return;
232
2b2d6d01 233 if (!test_opt(sb, ERRORS_CONT)) {
617ba13b 234 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 235
617ba13b 236 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
ac27a0ec 237 if (journal)
dab291af 238 jbd2_journal_abort(journal, -EIO);
ac27a0ec 239 }
2b2d6d01
TT
240 if (test_opt(sb, ERRORS_RO)) {
241 printk(KERN_CRIT "Remounting filesystem read-only\n");
ac27a0ec
DK
242 sb->s_flags |= MS_RDONLY;
243 }
617ba13b 244 ext4_commit_super(sb, es, 1);
ac27a0ec 245 if (test_opt(sb, ERRORS_PANIC))
617ba13b 246 panic("EXT4-fs (device %s): panic forced after error\n",
ac27a0ec
DK
247 sb->s_id);
248}
249
2b2d6d01
TT
250void ext4_error(struct super_block *sb, const char *function,
251 const char *fmt, ...)
ac27a0ec
DK
252{
253 va_list args;
254
255 va_start(args, fmt);
2b2d6d01 256 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
ac27a0ec
DK
257 vprintk(fmt, args);
258 printk("\n");
259 va_end(args);
260
617ba13b 261 ext4_handle_error(sb);
ac27a0ec
DK
262}
263
2b2d6d01 264static const char *ext4_decode_error(struct super_block *sb, int errno,
ac27a0ec
DK
265 char nbuf[16])
266{
267 char *errstr = NULL;
268
269 switch (errno) {
270 case -EIO:
271 errstr = "IO failure";
272 break;
273 case -ENOMEM:
274 errstr = "Out of memory";
275 break;
276 case -EROFS:
dab291af 277 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
ac27a0ec
DK
278 errstr = "Journal has aborted";
279 else
280 errstr = "Readonly filesystem";
281 break;
282 default:
283 /* If the caller passed in an extra buffer for unknown
284 * errors, textualise them now. Else we just return
285 * NULL. */
286 if (nbuf) {
287 /* Check for truncated error codes... */
288 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
289 errstr = nbuf;
290 }
291 break;
292 }
293
294 return errstr;
295}
296
617ba13b 297/* __ext4_std_error decodes expected errors from journaling functions
ac27a0ec
DK
298 * automatically and invokes the appropriate error response. */
299
2b2d6d01 300void __ext4_std_error(struct super_block *sb, const char *function, int errno)
ac27a0ec
DK
301{
302 char nbuf[16];
303 const char *errstr;
304
305 /* Special case: if the error is EROFS, and we're not already
306 * inside a transaction, then there's really no point in logging
307 * an error. */
308 if (errno == -EROFS && journal_current_handle() == NULL &&
309 (sb->s_flags & MS_RDONLY))
310 return;
311
617ba13b 312 errstr = ext4_decode_error(sb, errno, nbuf);
2b2d6d01
TT
313 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
314 sb->s_id, function, errstr);
ac27a0ec 315
617ba13b 316 ext4_handle_error(sb);
ac27a0ec
DK
317}
318
319/*
617ba13b 320 * ext4_abort is a much stronger failure handler than ext4_error. The
ac27a0ec
DK
321 * abort function may be used to deal with unrecoverable failures such
322 * as journal IO errors or ENOMEM at a critical moment in log management.
323 *
324 * We unconditionally force the filesystem into an ABORT|READONLY state,
325 * unless the error response on the fs has been set to panic in which
326 * case we take the easy way out and panic immediately.
327 */
328
2b2d6d01
TT
329void ext4_abort(struct super_block *sb, const char *function,
330 const char *fmt, ...)
ac27a0ec
DK
331{
332 va_list args;
333
2b2d6d01 334 printk(KERN_CRIT "ext4_abort called.\n");
ac27a0ec
DK
335
336 va_start(args, fmt);
2b2d6d01 337 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
ac27a0ec
DK
338 vprintk(fmt, args);
339 printk("\n");
340 va_end(args);
341
342 if (test_opt(sb, ERRORS_PANIC))
617ba13b 343 panic("EXT4-fs panic from previous error\n");
ac27a0ec
DK
344
345 if (sb->s_flags & MS_RDONLY)
346 return;
347
348 printk(KERN_CRIT "Remounting filesystem read-only\n");
617ba13b 349 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
ac27a0ec 350 sb->s_flags |= MS_RDONLY;
617ba13b 351 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
ef2cabf7
HK
352 if (EXT4_SB(sb)->s_journal)
353 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
ac27a0ec
DK
354}
355
2b2d6d01
TT
356void ext4_warning(struct super_block *sb, const char *function,
357 const char *fmt, ...)
ac27a0ec
DK
358{
359 va_list args;
360
361 va_start(args, fmt);
617ba13b 362 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
ac27a0ec
DK
363 sb->s_id, function);
364 vprintk(fmt, args);
365 printk("\n");
366 va_end(args);
367}
368
617ba13b 369void ext4_update_dynamic_rev(struct super_block *sb)
ac27a0ec 370{
617ba13b 371 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
ac27a0ec 372
617ba13b 373 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
ac27a0ec
DK
374 return;
375
46e665e9 376 ext4_warning(sb, __func__,
ac27a0ec
DK
377 "updating to rev %d because of new feature flag, "
378 "running e2fsck is recommended",
617ba13b 379 EXT4_DYNAMIC_REV);
ac27a0ec 380
617ba13b
MC
381 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
382 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
383 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
ac27a0ec
DK
384 /* leave es->s_feature_*compat flags alone */
385 /* es->s_uuid will be set by e2fsck if empty */
386
387 /*
388 * The rest of the superblock fields should be zero, and if not it
389 * means they are likely already in use, so leave them alone. We
390 * can leave it up to e2fsck to clean up any inconsistencies there.
391 */
392}
393
394/*
395 * Open the external journal device
396 */
617ba13b 397static struct block_device *ext4_blkdev_get(dev_t dev)
ac27a0ec
DK
398{
399 struct block_device *bdev;
400 char b[BDEVNAME_SIZE];
401
402 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
403 if (IS_ERR(bdev))
404 goto fail;
405 return bdev;
406
407fail:
617ba13b 408 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
ac27a0ec
DK
409 __bdevname(dev, b), PTR_ERR(bdev));
410 return NULL;
411}
412
413/*
414 * Release the journal device
415 */
617ba13b 416static int ext4_blkdev_put(struct block_device *bdev)
ac27a0ec
DK
417{
418 bd_release(bdev);
9a1c3542 419 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
ac27a0ec
DK
420}
421
617ba13b 422static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
ac27a0ec
DK
423{
424 struct block_device *bdev;
425 int ret = -ENODEV;
426
427 bdev = sbi->journal_bdev;
428 if (bdev) {
617ba13b 429 ret = ext4_blkdev_put(bdev);
ac27a0ec
DK
430 sbi->journal_bdev = NULL;
431 }
432 return ret;
433}
434
435static inline struct inode *orphan_list_entry(struct list_head *l)
436{
617ba13b 437 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
ac27a0ec
DK
438}
439
617ba13b 440static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
ac27a0ec
DK
441{
442 struct list_head *l;
443
444 printk(KERN_ERR "sb orphan head is %d\n",
445 le32_to_cpu(sbi->s_es->s_last_orphan));
446
447 printk(KERN_ERR "sb_info orphan list:\n");
448 list_for_each(l, &sbi->s_orphan) {
449 struct inode *inode = orphan_list_entry(l);
450 printk(KERN_ERR " "
451 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
452 inode->i_sb->s_id, inode->i_ino, inode,
453 inode->i_mode, inode->i_nlink,
454 NEXT_ORPHAN(inode));
455 }
456}
457
2b2d6d01 458static void ext4_put_super(struct super_block *sb)
ac27a0ec 459{
617ba13b
MC
460 struct ext4_sb_info *sbi = EXT4_SB(sb);
461 struct ext4_super_block *es = sbi->s_es;
ef2cabf7 462 int i, err;
ac27a0ec 463
c9de560d 464 ext4_mb_release(sb);
a86c6181 465 ext4_ext_release(sb);
617ba13b 466 ext4_xattr_put_super(sb);
0390131b
FM
467 if (sbi->s_journal) {
468 err = jbd2_journal_destroy(sbi->s_journal);
469 sbi->s_journal = NULL;
470 if (err < 0)
471 ext4_abort(sb, __func__,
472 "Couldn't clean up the journal");
473 }
ac27a0ec 474 if (!(sb->s_flags & MS_RDONLY)) {
617ba13b 475 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 476 es->s_state = cpu_to_le16(sbi->s_mount_state);
617ba13b 477 ext4_commit_super(sb, es, 1);
ac27a0ec 478 }
240799cd
TT
479 if (sbi->s_proc) {
480 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
9f6200bb 481 remove_proc_entry(sb->s_id, ext4_proc_root);
240799cd 482 }
ac27a0ec
DK
483
484 for (i = 0; i < sbi->s_gdb_count; i++)
485 brelse(sbi->s_group_desc[i]);
486 kfree(sbi->s_group_desc);
772cb7c8 487 kfree(sbi->s_flex_groups);
ac27a0ec
DK
488 percpu_counter_destroy(&sbi->s_freeblocks_counter);
489 percpu_counter_destroy(&sbi->s_freeinodes_counter);
490 percpu_counter_destroy(&sbi->s_dirs_counter);
6bc6e63f 491 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
ac27a0ec
DK
492 brelse(sbi->s_sbh);
493#ifdef CONFIG_QUOTA
494 for (i = 0; i < MAXQUOTAS; i++)
495 kfree(sbi->s_qf_names[i]);
496#endif
497
498 /* Debugging code just in case the in-memory inode orphan list
499 * isn't empty. The on-disk one can be non-empty if we've
500 * detected an error and taken the fs readonly, but the
501 * in-memory list had better be clean by this point. */
502 if (!list_empty(&sbi->s_orphan))
503 dump_orphan_list(sb, sbi);
504 J_ASSERT(list_empty(&sbi->s_orphan));
505
f98393a6 506 invalidate_bdev(sb->s_bdev);
ac27a0ec
DK
507 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
508 /*
509 * Invalidate the journal device's buffers. We don't want them
510 * floating about in memory - the physical journal device may
511 * hotswapped, and it breaks the `ro-after' testing code.
512 */
513 sync_blockdev(sbi->journal_bdev);
f98393a6 514 invalidate_bdev(sbi->journal_bdev);
617ba13b 515 ext4_blkdev_remove(sbi);
ac27a0ec
DK
516 }
517 sb->s_fs_info = NULL;
518 kfree(sbi);
519 return;
520}
521
e18b890b 522static struct kmem_cache *ext4_inode_cachep;
ac27a0ec
DK
523
524/*
525 * Called inside transaction, so use GFP_NOFS
526 */
617ba13b 527static struct inode *ext4_alloc_inode(struct super_block *sb)
ac27a0ec 528{
617ba13b 529 struct ext4_inode_info *ei;
ac27a0ec 530
e6b4f8da 531 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
ac27a0ec
DK
532 if (!ei)
533 return NULL;
03010a33 534#ifdef CONFIG_EXT4_FS_POSIX_ACL
617ba13b
MC
535 ei->i_acl = EXT4_ACL_NOT_CACHED;
536 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
ac27a0ec 537#endif
ac27a0ec 538 ei->vfs_inode.i_version = 1;
91246c00 539 ei->vfs_inode.i_data.writeback_index = 0;
a86c6181 540 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
c9de560d
AT
541 INIT_LIST_HEAD(&ei->i_prealloc_list);
542 spin_lock_init(&ei->i_prealloc_lock);
0390131b
FM
543 /*
544 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
545 * therefore it can be null here. Don't check it, just initialize
546 * jinode.
547 */
678aaf48 548 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
d2a17637
MC
549 ei->i_reserved_data_blocks = 0;
550 ei->i_reserved_meta_blocks = 0;
551 ei->i_allocated_meta_blocks = 0;
552 ei->i_delalloc_reserved_flag = 0;
553 spin_lock_init(&(ei->i_block_reservation_lock));
ac27a0ec
DK
554 return &ei->vfs_inode;
555}
556
617ba13b 557static void ext4_destroy_inode(struct inode *inode)
ac27a0ec 558{
9f7dd93d
VA
559 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
560 printk("EXT4 Inode %p: orphan list check failed!\n",
561 EXT4_I(inode));
562 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
563 EXT4_I(inode), sizeof(struct ext4_inode_info),
564 true);
565 dump_stack();
566 }
617ba13b 567 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
ac27a0ec
DK
568}
569
51cc5068 570static void init_once(void *foo)
ac27a0ec 571{
617ba13b 572 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
ac27a0ec 573
a35afb83 574 INIT_LIST_HEAD(&ei->i_orphan);
03010a33 575#ifdef CONFIG_EXT4_FS_XATTR
a35afb83 576 init_rwsem(&ei->xattr_sem);
ac27a0ec 577#endif
0e855ac8 578 init_rwsem(&ei->i_data_sem);
a35afb83 579 inode_init_once(&ei->vfs_inode);
ac27a0ec
DK
580}
581
582static int init_inodecache(void)
583{
617ba13b
MC
584 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
585 sizeof(struct ext4_inode_info),
ac27a0ec
DK
586 0, (SLAB_RECLAIM_ACCOUNT|
587 SLAB_MEM_SPREAD),
20c2df83 588 init_once);
617ba13b 589 if (ext4_inode_cachep == NULL)
ac27a0ec
DK
590 return -ENOMEM;
591 return 0;
592}
593
594static void destroy_inodecache(void)
595{
617ba13b 596 kmem_cache_destroy(ext4_inode_cachep);
ac27a0ec
DK
597}
598
617ba13b 599static void ext4_clear_inode(struct inode *inode)
ac27a0ec 600{
03010a33 601#ifdef CONFIG_EXT4_FS_POSIX_ACL
617ba13b
MC
602 if (EXT4_I(inode)->i_acl &&
603 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
604 posix_acl_release(EXT4_I(inode)->i_acl);
605 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
606 }
607 if (EXT4_I(inode)->i_default_acl &&
608 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
609 posix_acl_release(EXT4_I(inode)->i_default_acl);
610 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
ac27a0ec
DK
611 }
612#endif
c2ea3fde 613 ext4_discard_preallocations(inode);
0390131b
FM
614 if (EXT4_JOURNAL(inode))
615 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
678aaf48 616 &EXT4_I(inode)->jinode);
ac27a0ec
DK
617}
618
2b2d6d01
TT
619static inline void ext4_show_quota_options(struct seq_file *seq,
620 struct super_block *sb)
ac27a0ec
DK
621{
622#if defined(CONFIG_QUOTA)
617ba13b 623 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
624
625 if (sbi->s_jquota_fmt)
626 seq_printf(seq, ",jqfmt=%s",
af5bc92d 627 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
ac27a0ec
DK
628
629 if (sbi->s_qf_names[USRQUOTA])
630 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
631
632 if (sbi->s_qf_names[GRPQUOTA])
633 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
634
617ba13b 635 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
ac27a0ec
DK
636 seq_puts(seq, ",usrquota");
637
617ba13b 638 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
ac27a0ec
DK
639 seq_puts(seq, ",grpquota");
640#endif
641}
642
d9c9bef1
MS
643/*
644 * Show an option if
645 * - it's set to a non-default value OR
646 * - if the per-sb default is different from the global default
647 */
617ba13b 648static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
ac27a0ec 649{
aa22df2c
AK
650 int def_errors;
651 unsigned long def_mount_opts;
ac27a0ec 652 struct super_block *sb = vfs->mnt_sb;
d9c9bef1
MS
653 struct ext4_sb_info *sbi = EXT4_SB(sb);
654 struct ext4_super_block *es = sbi->s_es;
d9c9bef1
MS
655
656 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
aa22df2c 657 def_errors = le16_to_cpu(es->s_errors);
d9c9bef1
MS
658
659 if (sbi->s_sb_block != 1)
660 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
661 if (test_opt(sb, MINIX_DF))
662 seq_puts(seq, ",minixdf");
aa22df2c 663 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
d9c9bef1
MS
664 seq_puts(seq, ",grpid");
665 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
666 seq_puts(seq, ",nogrpid");
667 if (sbi->s_resuid != EXT4_DEF_RESUID ||
668 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
669 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
670 }
671 if (sbi->s_resgid != EXT4_DEF_RESGID ||
672 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
673 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
674 }
bb4f397a 675 if (test_opt(sb, ERRORS_RO)) {
d9c9bef1 676 if (def_errors == EXT4_ERRORS_PANIC ||
bb4f397a
AK
677 def_errors == EXT4_ERRORS_CONTINUE) {
678 seq_puts(seq, ",errors=remount-ro");
d9c9bef1
MS
679 }
680 }
aa22df2c 681 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
bb4f397a 682 seq_puts(seq, ",errors=continue");
aa22df2c 683 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
d9c9bef1 684 seq_puts(seq, ",errors=panic");
aa22df2c 685 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
d9c9bef1 686 seq_puts(seq, ",nouid32");
aa22df2c 687 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
d9c9bef1
MS
688 seq_puts(seq, ",debug");
689 if (test_opt(sb, OLDALLOC))
690 seq_puts(seq, ",oldalloc");
03010a33 691#ifdef CONFIG_EXT4_FS_XATTR
aa22df2c
AK
692 if (test_opt(sb, XATTR_USER) &&
693 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
d9c9bef1
MS
694 seq_puts(seq, ",user_xattr");
695 if (!test_opt(sb, XATTR_USER) &&
696 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
697 seq_puts(seq, ",nouser_xattr");
698 }
699#endif
03010a33 700#ifdef CONFIG_EXT4_FS_POSIX_ACL
aa22df2c 701 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
d9c9bef1
MS
702 seq_puts(seq, ",acl");
703 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
704 seq_puts(seq, ",noacl");
705#endif
706 if (!test_opt(sb, RESERVATION))
707 seq_puts(seq, ",noreservation");
30773840 708 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
d9c9bef1
MS
709 seq_printf(seq, ",commit=%u",
710 (unsigned) (sbi->s_commit_interval / HZ));
711 }
30773840
TT
712 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
713 seq_printf(seq, ",min_batch_time=%u",
714 (unsigned) sbi->s_min_batch_time);
715 }
716 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
717 seq_printf(seq, ",max_batch_time=%u",
718 (unsigned) sbi->s_min_batch_time);
719 }
720
571640ca
ES
721 /*
722 * We're changing the default of barrier mount option, so
723 * let's always display its mount state so it's clear what its
724 * status is.
725 */
726 seq_puts(seq, ",barrier=");
727 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
cd0b6a39
TT
728 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
729 seq_puts(seq, ",journal_async_commit");
d9c9bef1
MS
730 if (test_opt(sb, NOBH))
731 seq_puts(seq, ",nobh");
732 if (!test_opt(sb, EXTENTS))
733 seq_puts(seq, ",noextents");
25ec56b5
JNC
734 if (test_opt(sb, I_VERSION))
735 seq_puts(seq, ",i_version");
dd919b98
AK
736 if (!test_opt(sb, DELALLOC))
737 seq_puts(seq, ",nodelalloc");
738
ac27a0ec 739
cb45bbe4
MS
740 if (sbi->s_stripe)
741 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
aa22df2c
AK
742 /*
743 * journal mode get enabled in different ways
744 * So just print the value even if we didn't specify it
745 */
617ba13b 746 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
ac27a0ec 747 seq_puts(seq, ",data=journal");
617ba13b 748 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
ac27a0ec 749 seq_puts(seq, ",data=ordered");
617ba13b 750 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
ac27a0ec
DK
751 seq_puts(seq, ",data=writeback");
752
240799cd
TT
753 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
754 seq_printf(seq, ",inode_readahead_blks=%u",
755 sbi->s_inode_readahead_blks);
756
5bf5683a
HK
757 if (test_opt(sb, DATA_ERR_ABORT))
758 seq_puts(seq, ",data_err=abort");
759
617ba13b 760 ext4_show_quota_options(seq, sb);
ac27a0ec
DK
761 return 0;
762}
763
764
1b961ac0
CH
765static struct inode *ext4_nfs_get_inode(struct super_block *sb,
766 u64 ino, u32 generation)
ac27a0ec 767{
ac27a0ec 768 struct inode *inode;
ac27a0ec 769
617ba13b 770 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
ac27a0ec 771 return ERR_PTR(-ESTALE);
617ba13b 772 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
ac27a0ec
DK
773 return ERR_PTR(-ESTALE);
774
775 /* iget isn't really right if the inode is currently unallocated!!
776 *
617ba13b 777 * ext4_read_inode will return a bad_inode if the inode had been
ac27a0ec
DK
778 * deleted, so we should be safe.
779 *
780 * Currently we don't know the generation for parent directory, so
781 * a generation of 0 means "accept any"
782 */
1d1fe1ee
DH
783 inode = ext4_iget(sb, ino);
784 if (IS_ERR(inode))
785 return ERR_CAST(inode);
786 if (generation && inode->i_generation != generation) {
ac27a0ec
DK
787 iput(inode);
788 return ERR_PTR(-ESTALE);
789 }
1b961ac0
CH
790
791 return inode;
792}
793
794static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
795 int fh_len, int fh_type)
796{
797 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
798 ext4_nfs_get_inode);
799}
800
801static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
802 int fh_len, int fh_type)
803{
804 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
805 ext4_nfs_get_inode);
ac27a0ec
DK
806}
807
808#ifdef CONFIG_QUOTA
af5bc92d 809#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
2b2d6d01 810#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
ac27a0ec 811
617ba13b
MC
812static int ext4_dquot_initialize(struct inode *inode, int type);
813static int ext4_dquot_drop(struct inode *inode);
814static int ext4_write_dquot(struct dquot *dquot);
815static int ext4_acquire_dquot(struct dquot *dquot);
816static int ext4_release_dquot(struct dquot *dquot);
817static int ext4_mark_dquot_dirty(struct dquot *dquot);
818static int ext4_write_info(struct super_block *sb, int type);
6f28e087
JK
819static int ext4_quota_on(struct super_block *sb, int type, int format_id,
820 char *path, int remount);
617ba13b
MC
821static int ext4_quota_on_mount(struct super_block *sb, int type);
822static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec 823 size_t len, loff_t off);
617ba13b 824static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec
DK
825 const char *data, size_t len, loff_t off);
826
617ba13b
MC
827static struct dquot_operations ext4_quota_operations = {
828 .initialize = ext4_dquot_initialize,
829 .drop = ext4_dquot_drop,
ac27a0ec
DK
830 .alloc_space = dquot_alloc_space,
831 .alloc_inode = dquot_alloc_inode,
832 .free_space = dquot_free_space,
833 .free_inode = dquot_free_inode,
834 .transfer = dquot_transfer,
617ba13b
MC
835 .write_dquot = ext4_write_dquot,
836 .acquire_dquot = ext4_acquire_dquot,
837 .release_dquot = ext4_release_dquot,
838 .mark_dirty = ext4_mark_dquot_dirty,
839 .write_info = ext4_write_info
ac27a0ec
DK
840};
841
617ba13b
MC
842static struct quotactl_ops ext4_qctl_operations = {
843 .quota_on = ext4_quota_on,
ac27a0ec
DK
844 .quota_off = vfs_quota_off,
845 .quota_sync = vfs_quota_sync,
846 .get_info = vfs_get_dqinfo,
847 .set_info = vfs_set_dqinfo,
848 .get_dqblk = vfs_get_dqblk,
849 .set_dqblk = vfs_set_dqblk
850};
851#endif
852
ee9b6d61 853static const struct super_operations ext4_sops = {
617ba13b
MC
854 .alloc_inode = ext4_alloc_inode,
855 .destroy_inode = ext4_destroy_inode,
617ba13b
MC
856 .write_inode = ext4_write_inode,
857 .dirty_inode = ext4_dirty_inode,
858 .delete_inode = ext4_delete_inode,
859 .put_super = ext4_put_super,
860 .write_super = ext4_write_super,
861 .sync_fs = ext4_sync_fs,
862 .write_super_lockfs = ext4_write_super_lockfs,
863 .unlockfs = ext4_unlockfs,
864 .statfs = ext4_statfs,
865 .remount_fs = ext4_remount,
866 .clear_inode = ext4_clear_inode,
867 .show_options = ext4_show_options,
ac27a0ec 868#ifdef CONFIG_QUOTA
617ba13b
MC
869 .quota_read = ext4_quota_read,
870 .quota_write = ext4_quota_write,
ac27a0ec
DK
871#endif
872};
873
39655164 874static const struct export_operations ext4_export_ops = {
1b961ac0
CH
875 .fh_to_dentry = ext4_fh_to_dentry,
876 .fh_to_parent = ext4_fh_to_parent,
617ba13b 877 .get_parent = ext4_get_parent,
ac27a0ec
DK
878};
879
880enum {
881 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
882 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
01436ef2 883 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
ac27a0ec
DK
884 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
885 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
30773840
TT
886 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
887 Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
818d276c 888 Opt_journal_checksum, Opt_journal_async_commit,
ac27a0ec 889 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
5bf5683a 890 Opt_data_err_abort, Opt_data_err_ignore,
ac27a0ec
DK
891 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
892 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
893 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
25ec56b5 894 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
01436ef2 895 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
240799cd 896 Opt_inode_readahead_blks
ac27a0ec
DK
897};
898
a447c093 899static const match_table_t tokens = {
ac27a0ec
DK
900 {Opt_bsd_df, "bsddf"},
901 {Opt_minix_df, "minixdf"},
902 {Opt_grpid, "grpid"},
903 {Opt_grpid, "bsdgroups"},
904 {Opt_nogrpid, "nogrpid"},
905 {Opt_nogrpid, "sysvgroups"},
906 {Opt_resgid, "resgid=%u"},
907 {Opt_resuid, "resuid=%u"},
908 {Opt_sb, "sb=%u"},
909 {Opt_err_cont, "errors=continue"},
910 {Opt_err_panic, "errors=panic"},
911 {Opt_err_ro, "errors=remount-ro"},
912 {Opt_nouid32, "nouid32"},
ac27a0ec
DK
913 {Opt_debug, "debug"},
914 {Opt_oldalloc, "oldalloc"},
915 {Opt_orlov, "orlov"},
916 {Opt_user_xattr, "user_xattr"},
917 {Opt_nouser_xattr, "nouser_xattr"},
918 {Opt_acl, "acl"},
919 {Opt_noacl, "noacl"},
920 {Opt_reservation, "reservation"},
921 {Opt_noreservation, "noreservation"},
922 {Opt_noload, "noload"},
923 {Opt_nobh, "nobh"},
924 {Opt_bh, "bh"},
925 {Opt_commit, "commit=%u"},
30773840
TT
926 {Opt_min_batch_time, "min_batch_time=%u"},
927 {Opt_max_batch_time, "max_batch_time=%u"},
ac27a0ec
DK
928 {Opt_journal_update, "journal=update"},
929 {Opt_journal_inum, "journal=%u"},
930 {Opt_journal_dev, "journal_dev=%u"},
818d276c
GS
931 {Opt_journal_checksum, "journal_checksum"},
932 {Opt_journal_async_commit, "journal_async_commit"},
ac27a0ec
DK
933 {Opt_abort, "abort"},
934 {Opt_data_journal, "data=journal"},
935 {Opt_data_ordered, "data=ordered"},
936 {Opt_data_writeback, "data=writeback"},
5bf5683a
HK
937 {Opt_data_err_abort, "data_err=abort"},
938 {Opt_data_err_ignore, "data_err=ignore"},
ac27a0ec
DK
939 {Opt_offusrjquota, "usrjquota="},
940 {Opt_usrjquota, "usrjquota=%s"},
941 {Opt_offgrpjquota, "grpjquota="},
942 {Opt_grpjquota, "grpjquota=%s"},
943 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
944 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
945 {Opt_grpquota, "grpquota"},
946 {Opt_noquota, "noquota"},
947 {Opt_quota, "quota"},
948 {Opt_usrquota, "usrquota"},
949 {Opt_barrier, "barrier=%u"},
a86c6181 950 {Opt_extents, "extents"},
1e2462f9 951 {Opt_noextents, "noextents"},
25ec56b5 952 {Opt_i_version, "i_version"},
c9de560d 953 {Opt_stripe, "stripe=%u"},
ac27a0ec 954 {Opt_resize, "resize"},
64769240 955 {Opt_delalloc, "delalloc"},
dd919b98 956 {Opt_nodelalloc, "nodelalloc"},
240799cd 957 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
f3f12faa 958 {Opt_err, NULL},
ac27a0ec
DK
959};
960
617ba13b 961static ext4_fsblk_t get_sb_block(void **data)
ac27a0ec 962{
617ba13b 963 ext4_fsblk_t sb_block;
ac27a0ec
DK
964 char *options = (char *) *data;
965
966 if (!options || strncmp(options, "sb=", 3) != 0)
967 return 1; /* Default location */
968 options += 3;
617ba13b 969 /*todo: use simple_strtoll with >32bit ext4 */
ac27a0ec
DK
970 sb_block = simple_strtoul(options, &options, 0);
971 if (*options && *options != ',') {
4776004f 972 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
ac27a0ec
DK
973 (char *) *data);
974 return 1;
975 }
976 if (*options == ',')
977 options++;
978 *data = (void *) options;
979 return sb_block;
980}
981
2b2d6d01
TT
982static int parse_options(char *options, struct super_block *sb,
983 unsigned int *inum, unsigned long *journal_devnum,
984 ext4_fsblk_t *n_blocks_count, int is_remount)
ac27a0ec 985{
617ba13b 986 struct ext4_sb_info *sbi = EXT4_SB(sb);
2b2d6d01 987 char *p;
ac27a0ec
DK
988 substring_t args[MAX_OPT_ARGS];
989 int data_opt = 0;
990 int option;
991#ifdef CONFIG_QUOTA
dfc5d03f 992 int qtype, qfmt;
ac27a0ec
DK
993 char *qname;
994#endif
c07651b5 995 ext4_fsblk_t last_block;
ac27a0ec
DK
996
997 if (!options)
998 return 1;
999
2b2d6d01 1000 while ((p = strsep(&options, ",")) != NULL) {
ac27a0ec
DK
1001 int token;
1002 if (!*p)
1003 continue;
1004
1005 token = match_token(p, tokens, args);
1006 switch (token) {
1007 case Opt_bsd_df:
2b2d6d01 1008 clear_opt(sbi->s_mount_opt, MINIX_DF);
ac27a0ec
DK
1009 break;
1010 case Opt_minix_df:
2b2d6d01 1011 set_opt(sbi->s_mount_opt, MINIX_DF);
ac27a0ec
DK
1012 break;
1013 case Opt_grpid:
2b2d6d01 1014 set_opt(sbi->s_mount_opt, GRPID);
ac27a0ec
DK
1015 break;
1016 case Opt_nogrpid:
2b2d6d01 1017 clear_opt(sbi->s_mount_opt, GRPID);
ac27a0ec
DK
1018 break;
1019 case Opt_resuid:
1020 if (match_int(&args[0], &option))
1021 return 0;
1022 sbi->s_resuid = option;
1023 break;
1024 case Opt_resgid:
1025 if (match_int(&args[0], &option))
1026 return 0;
1027 sbi->s_resgid = option;
1028 break;
1029 case Opt_sb:
1030 /* handled by get_sb_block() instead of here */
1031 /* *sb_block = match_int(&args[0]); */
1032 break;
1033 case Opt_err_panic:
2b2d6d01
TT
1034 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1035 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1036 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
ac27a0ec
DK
1037 break;
1038 case Opt_err_ro:
2b2d6d01
TT
1039 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1040 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1041 set_opt(sbi->s_mount_opt, ERRORS_RO);
ac27a0ec
DK
1042 break;
1043 case Opt_err_cont:
2b2d6d01
TT
1044 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1045 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1046 set_opt(sbi->s_mount_opt, ERRORS_CONT);
ac27a0ec
DK
1047 break;
1048 case Opt_nouid32:
2b2d6d01 1049 set_opt(sbi->s_mount_opt, NO_UID32);
ac27a0ec 1050 break;
ac27a0ec 1051 case Opt_debug:
2b2d6d01 1052 set_opt(sbi->s_mount_opt, DEBUG);
ac27a0ec
DK
1053 break;
1054 case Opt_oldalloc:
2b2d6d01 1055 set_opt(sbi->s_mount_opt, OLDALLOC);
ac27a0ec
DK
1056 break;
1057 case Opt_orlov:
2b2d6d01 1058 clear_opt(sbi->s_mount_opt, OLDALLOC);
ac27a0ec 1059 break;
03010a33 1060#ifdef CONFIG_EXT4_FS_XATTR
ac27a0ec 1061 case Opt_user_xattr:
2b2d6d01 1062 set_opt(sbi->s_mount_opt, XATTR_USER);
ac27a0ec
DK
1063 break;
1064 case Opt_nouser_xattr:
2b2d6d01 1065 clear_opt(sbi->s_mount_opt, XATTR_USER);
ac27a0ec
DK
1066 break;
1067#else
1068 case Opt_user_xattr:
1069 case Opt_nouser_xattr:
4776004f
TT
1070 printk(KERN_ERR "EXT4 (no)user_xattr options "
1071 "not supported\n");
ac27a0ec
DK
1072 break;
1073#endif
03010a33 1074#ifdef CONFIG_EXT4_FS_POSIX_ACL
ac27a0ec
DK
1075 case Opt_acl:
1076 set_opt(sbi->s_mount_opt, POSIX_ACL);
1077 break;
1078 case Opt_noacl:
1079 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1080 break;
1081#else
1082 case Opt_acl:
1083 case Opt_noacl:
4776004f
TT
1084 printk(KERN_ERR "EXT4 (no)acl options "
1085 "not supported\n");
ac27a0ec
DK
1086 break;
1087#endif
1088 case Opt_reservation:
1089 set_opt(sbi->s_mount_opt, RESERVATION);
1090 break;
1091 case Opt_noreservation:
1092 clear_opt(sbi->s_mount_opt, RESERVATION);
1093 break;
1094 case Opt_journal_update:
1095 /* @@@ FIXME */
1096 /* Eventually we will want to be able to create
1097 a journal file here. For now, only allow the
1098 user to specify an existing inode to be the
1099 journal file. */
1100 if (is_remount) {
617ba13b 1101 printk(KERN_ERR "EXT4-fs: cannot specify "
ac27a0ec
DK
1102 "journal on remount\n");
1103 return 0;
1104 }
2b2d6d01 1105 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
ac27a0ec
DK
1106 break;
1107 case Opt_journal_inum:
1108 if (is_remount) {
617ba13b 1109 printk(KERN_ERR "EXT4-fs: cannot specify "
ac27a0ec
DK
1110 "journal on remount\n");
1111 return 0;
1112 }
1113 if (match_int(&args[0], &option))
1114 return 0;
1115 *inum = option;
1116 break;
1117 case Opt_journal_dev:
1118 if (is_remount) {
617ba13b 1119 printk(KERN_ERR "EXT4-fs: cannot specify "
ac27a0ec
DK
1120 "journal on remount\n");
1121 return 0;
1122 }
1123 if (match_int(&args[0], &option))
1124 return 0;
1125 *journal_devnum = option;
1126 break;
818d276c
GS
1127 case Opt_journal_checksum:
1128 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1129 break;
1130 case Opt_journal_async_commit:
1131 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1132 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1133 break;
ac27a0ec 1134 case Opt_noload:
2b2d6d01 1135 set_opt(sbi->s_mount_opt, NOLOAD);
ac27a0ec
DK
1136 break;
1137 case Opt_commit:
1138 if (match_int(&args[0], &option))
1139 return 0;
1140 if (option < 0)
1141 return 0;
1142 if (option == 0)
cd02ff0b 1143 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
ac27a0ec
DK
1144 sbi->s_commit_interval = HZ * option;
1145 break;
30773840
TT
1146 case Opt_max_batch_time:
1147 if (match_int(&args[0], &option))
1148 return 0;
1149 if (option < 0)
1150 return 0;
1151 if (option == 0)
1152 option = EXT4_DEF_MAX_BATCH_TIME;
1153 sbi->s_max_batch_time = option;
1154 break;
1155 case Opt_min_batch_time:
1156 if (match_int(&args[0], &option))
1157 return 0;
1158 if (option < 0)
1159 return 0;
1160 sbi->s_min_batch_time = option;
1161 break;
ac27a0ec 1162 case Opt_data_journal:
617ba13b 1163 data_opt = EXT4_MOUNT_JOURNAL_DATA;
ac27a0ec
DK
1164 goto datacheck;
1165 case Opt_data_ordered:
617ba13b 1166 data_opt = EXT4_MOUNT_ORDERED_DATA;
ac27a0ec
DK
1167 goto datacheck;
1168 case Opt_data_writeback:
617ba13b 1169 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
ac27a0ec
DK
1170 datacheck:
1171 if (is_remount) {
617ba13b 1172 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
ac27a0ec
DK
1173 != data_opt) {
1174 printk(KERN_ERR
617ba13b 1175 "EXT4-fs: cannot change data "
ac27a0ec
DK
1176 "mode on remount\n");
1177 return 0;
1178 }
1179 } else {
617ba13b 1180 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
ac27a0ec
DK
1181 sbi->s_mount_opt |= data_opt;
1182 }
1183 break;
5bf5683a
HK
1184 case Opt_data_err_abort:
1185 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1186 break;
1187 case Opt_data_err_ignore:
1188 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1189 break;
ac27a0ec
DK
1190#ifdef CONFIG_QUOTA
1191 case Opt_usrjquota:
1192 qtype = USRQUOTA;
1193 goto set_qf_name;
1194 case Opt_grpjquota:
1195 qtype = GRPQUOTA;
1196set_qf_name:
dfc5d03f
JK
1197 if ((sb_any_quota_enabled(sb) ||
1198 sb_any_quota_suspended(sb)) &&
1199 !sbi->s_qf_names[qtype]) {
ac27a0ec 1200 printk(KERN_ERR
4776004f
TT
1201 "EXT4-fs: Cannot change journaled "
1202 "quota options when quota turned on.\n");
ac27a0ec
DK
1203 return 0;
1204 }
1205 qname = match_strdup(&args[0]);
1206 if (!qname) {
1207 printk(KERN_ERR
617ba13b 1208 "EXT4-fs: not enough memory for "
ac27a0ec
DK
1209 "storing quotafile name.\n");
1210 return 0;
1211 }
1212 if (sbi->s_qf_names[qtype] &&
1213 strcmp(sbi->s_qf_names[qtype], qname)) {
1214 printk(KERN_ERR
617ba13b 1215 "EXT4-fs: %s quota file already "
ac27a0ec
DK
1216 "specified.\n", QTYPE2NAME(qtype));
1217 kfree(qname);
1218 return 0;
1219 }
1220 sbi->s_qf_names[qtype] = qname;
1221 if (strchr(sbi->s_qf_names[qtype], '/')) {
1222 printk(KERN_ERR
617ba13b 1223 "EXT4-fs: quotafile must be on "
ac27a0ec
DK
1224 "filesystem root.\n");
1225 kfree(sbi->s_qf_names[qtype]);
1226 sbi->s_qf_names[qtype] = NULL;
1227 return 0;
1228 }
1229 set_opt(sbi->s_mount_opt, QUOTA);
1230 break;
1231 case Opt_offusrjquota:
1232 qtype = USRQUOTA;
1233 goto clear_qf_name;
1234 case Opt_offgrpjquota:
1235 qtype = GRPQUOTA;
1236clear_qf_name:
dfc5d03f
JK
1237 if ((sb_any_quota_enabled(sb) ||
1238 sb_any_quota_suspended(sb)) &&
1239 sbi->s_qf_names[qtype]) {
617ba13b 1240 printk(KERN_ERR "EXT4-fs: Cannot change "
2c8be6b2 1241 "journaled quota options when "
ac27a0ec
DK
1242 "quota turned on.\n");
1243 return 0;
1244 }
1245 /*
1246 * The space will be released later when all options
1247 * are confirmed to be correct
1248 */
1249 sbi->s_qf_names[qtype] = NULL;
1250 break;
1251 case Opt_jqfmt_vfsold:
dfc5d03f
JK
1252 qfmt = QFMT_VFS_OLD;
1253 goto set_qf_format;
ac27a0ec 1254 case Opt_jqfmt_vfsv0:
dfc5d03f
JK
1255 qfmt = QFMT_VFS_V0;
1256set_qf_format:
1257 if ((sb_any_quota_enabled(sb) ||
1258 sb_any_quota_suspended(sb)) &&
1259 sbi->s_jquota_fmt != qfmt) {
1260 printk(KERN_ERR "EXT4-fs: Cannot change "
1261 "journaled quota options when "
1262 "quota turned on.\n");
1263 return 0;
1264 }
1265 sbi->s_jquota_fmt = qfmt;
ac27a0ec
DK
1266 break;
1267 case Opt_quota:
1268 case Opt_usrquota:
1269 set_opt(sbi->s_mount_opt, QUOTA);
1270 set_opt(sbi->s_mount_opt, USRQUOTA);
1271 break;
1272 case Opt_grpquota:
1273 set_opt(sbi->s_mount_opt, QUOTA);
1274 set_opt(sbi->s_mount_opt, GRPQUOTA);
1275 break;
1276 case Opt_noquota:
1277 if (sb_any_quota_enabled(sb)) {
617ba13b 1278 printk(KERN_ERR "EXT4-fs: Cannot change quota "
ac27a0ec
DK
1279 "options when quota turned on.\n");
1280 return 0;
1281 }
1282 clear_opt(sbi->s_mount_opt, QUOTA);
1283 clear_opt(sbi->s_mount_opt, USRQUOTA);
1284 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1285 break;
1286#else
1287 case Opt_quota:
1288 case Opt_usrquota:
1289 case Opt_grpquota:
cd59e7b9
JK
1290 printk(KERN_ERR
1291 "EXT4-fs: quota options not supported.\n");
1292 break;
ac27a0ec
DK
1293 case Opt_usrjquota:
1294 case Opt_grpjquota:
1295 case Opt_offusrjquota:
1296 case Opt_offgrpjquota:
1297 case Opt_jqfmt_vfsold:
1298 case Opt_jqfmt_vfsv0:
1299 printk(KERN_ERR
cd59e7b9 1300 "EXT4-fs: journaled quota options not "
ac27a0ec
DK
1301 "supported.\n");
1302 break;
1303 case Opt_noquota:
1304 break;
1305#endif
1306 case Opt_abort:
1307 set_opt(sbi->s_mount_opt, ABORT);
1308 break;
1309 case Opt_barrier:
1310 if (match_int(&args[0], &option))
1311 return 0;
1312 if (option)
1313 set_opt(sbi->s_mount_opt, BARRIER);
1314 else
1315 clear_opt(sbi->s_mount_opt, BARRIER);
1316 break;
1317 case Opt_ignore:
1318 break;
1319 case Opt_resize:
1320 if (!is_remount) {
617ba13b 1321 printk("EXT4-fs: resize option only available "
ac27a0ec
DK
1322 "for remount\n");
1323 return 0;
1324 }
1325 if (match_int(&args[0], &option) != 0)
1326 return 0;
1327 *n_blocks_count = option;
1328 break;
1329 case Opt_nobh:
1330 set_opt(sbi->s_mount_opt, NOBH);
1331 break;
1332 case Opt_bh:
1333 clear_opt(sbi->s_mount_opt, NOBH);
1334 break;
a86c6181 1335 case Opt_extents:
e4079a11
ES
1336 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1337 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1338 ext4_warning(sb, __func__,
1339 "extents feature not enabled "
fde4d95a 1340 "on this filesystem, use tune2fs");
e4079a11
ES
1341 return 0;
1342 }
2b2d6d01 1343 set_opt(sbi->s_mount_opt, EXTENTS);
a86c6181 1344 break;
1e2462f9 1345 case Opt_noextents:
c07651b5
AK
1346 /*
1347 * When e2fsprogs support resizing an already existing
1348 * ext3 file system to greater than 2**32 we need to
1349 * add support to block allocator to handle growing
1350 * already existing block mapped inode so that blocks
1351 * allocated for them fall within 2**32
1352 */
1353 last_block = ext4_blocks_count(sbi->s_es) - 1;
1354 if (last_block > 0xffffffffULL) {
1355 printk(KERN_ERR "EXT4-fs: Filesystem too "
1356 "large to mount with "
1357 "-o noextents options\n");
1358 return 0;
1359 }
2b2d6d01 1360 clear_opt(sbi->s_mount_opt, EXTENTS);
1e2462f9 1361 break;
25ec56b5
JNC
1362 case Opt_i_version:
1363 set_opt(sbi->s_mount_opt, I_VERSION);
1364 sb->s_flags |= MS_I_VERSION;
1365 break;
dd919b98
AK
1366 case Opt_nodelalloc:
1367 clear_opt(sbi->s_mount_opt, DELALLOC);
1368 break;
c9de560d
AT
1369 case Opt_stripe:
1370 if (match_int(&args[0], &option))
1371 return 0;
1372 if (option < 0)
1373 return 0;
1374 sbi->s_stripe = option;
1375 break;
64769240
AT
1376 case Opt_delalloc:
1377 set_opt(sbi->s_mount_opt, DELALLOC);
1378 break;
240799cd
TT
1379 case Opt_inode_readahead_blks:
1380 if (match_int(&args[0], &option))
1381 return 0;
1382 if (option < 0 || option > (1 << 30))
1383 return 0;
1384 sbi->s_inode_readahead_blks = option;
1385 break;
ac27a0ec 1386 default:
2b2d6d01
TT
1387 printk(KERN_ERR
1388 "EXT4-fs: Unrecognized mount option \"%s\" "
1389 "or missing value\n", p);
ac27a0ec
DK
1390 return 0;
1391 }
1392 }
1393#ifdef CONFIG_QUOTA
1394 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
617ba13b 1395 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
ac27a0ec
DK
1396 sbi->s_qf_names[USRQUOTA])
1397 clear_opt(sbi->s_mount_opt, USRQUOTA);
1398
617ba13b 1399 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
ac27a0ec
DK
1400 sbi->s_qf_names[GRPQUOTA])
1401 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1402
1403 if ((sbi->s_qf_names[USRQUOTA] &&
617ba13b 1404 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
ac27a0ec 1405 (sbi->s_qf_names[GRPQUOTA] &&
617ba13b
MC
1406 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1407 printk(KERN_ERR "EXT4-fs: old and new quota "
ac27a0ec
DK
1408 "format mixing.\n");
1409 return 0;
1410 }
1411
1412 if (!sbi->s_jquota_fmt) {
2c8be6b2 1413 printk(KERN_ERR "EXT4-fs: journaled quota format "
ac27a0ec
DK
1414 "not specified.\n");
1415 return 0;
1416 }
1417 } else {
1418 if (sbi->s_jquota_fmt) {
2c8be6b2
JK
1419 printk(KERN_ERR "EXT4-fs: journaled quota format "
1420 "specified with no journaling "
ac27a0ec
DK
1421 "enabled.\n");
1422 return 0;
1423 }
1424 }
1425#endif
1426 return 1;
1427}
1428
617ba13b 1429static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
ac27a0ec
DK
1430 int read_only)
1431{
617ba13b 1432 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
1433 int res = 0;
1434
617ba13b 1435 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2b2d6d01
TT
1436 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1437 "forcing read-only mode\n");
ac27a0ec
DK
1438 res = MS_RDONLY;
1439 }
1440 if (read_only)
1441 return res;
617ba13b 1442 if (!(sbi->s_mount_state & EXT4_VALID_FS))
2b2d6d01
TT
1443 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1444 "running e2fsck is recommended\n");
617ba13b 1445 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
2b2d6d01
TT
1446 printk(KERN_WARNING
1447 "EXT4-fs warning: mounting fs with errors, "
1448 "running e2fsck is recommended\n");
ac27a0ec
DK
1449 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1450 le16_to_cpu(es->s_mnt_count) >=
1451 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2b2d6d01
TT
1452 printk(KERN_WARNING
1453 "EXT4-fs warning: maximal mount count reached, "
1454 "running e2fsck is recommended\n");
ac27a0ec
DK
1455 else if (le32_to_cpu(es->s_checkinterval) &&
1456 (le32_to_cpu(es->s_lastcheck) +
1457 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
2b2d6d01
TT
1458 printk(KERN_WARNING
1459 "EXT4-fs warning: checktime reached, "
1460 "running e2fsck is recommended\n");
0390131b
FM
1461 if (!sbi->s_journal)
1462 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
ac27a0ec 1463 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
617ba13b 1464 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
e8546d06 1465 le16_add_cpu(&es->s_mnt_count, 1);
ac27a0ec 1466 es->s_mtime = cpu_to_le32(get_seconds());
617ba13b 1467 ext4_update_dynamic_rev(sb);
0390131b
FM
1468 if (sbi->s_journal)
1469 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 1470
617ba13b 1471 ext4_commit_super(sb, es, 1);
ac27a0ec 1472 if (test_opt(sb, DEBUG))
617ba13b 1473 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
ac27a0ec
DK
1474 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1475 sb->s_blocksize,
1476 sbi->s_groups_count,
617ba13b
MC
1477 EXT4_BLOCKS_PER_GROUP(sb),
1478 EXT4_INODES_PER_GROUP(sb),
ac27a0ec
DK
1479 sbi->s_mount_opt);
1480
0390131b
FM
1481 if (EXT4_SB(sb)->s_journal) {
1482 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1483 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1484 "external", EXT4_SB(sb)->s_journal->j_devname);
1485 } else {
1486 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1487 }
ac27a0ec
DK
1488 return res;
1489}
1490
772cb7c8
JS
1491static int ext4_fill_flex_info(struct super_block *sb)
1492{
1493 struct ext4_sb_info *sbi = EXT4_SB(sb);
1494 struct ext4_group_desc *gdp = NULL;
1495 struct buffer_head *bh;
1496 ext4_group_t flex_group_count;
1497 ext4_group_t flex_group;
1498 int groups_per_flex = 0;
772cb7c8
JS
1499 int i;
1500
1501 if (!sbi->s_es->s_log_groups_per_flex) {
1502 sbi->s_log_groups_per_flex = 0;
1503 return 1;
1504 }
1505
1506 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1507 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1508
c62a11fd
FB
1509 /* We allocate both existing and potentially added groups */
1510 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
d94e99a6
AK
1511 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1512 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
ec05e868 1513 sbi->s_flex_groups = kzalloc(flex_group_count *
772cb7c8
JS
1514 sizeof(struct flex_groups), GFP_KERNEL);
1515 if (sbi->s_flex_groups == NULL) {
ec05e868
LZ
1516 printk(KERN_ERR "EXT4-fs: not enough memory for "
1517 "%lu flex groups\n", flex_group_count);
772cb7c8
JS
1518 goto failed;
1519 }
772cb7c8 1520
772cb7c8
JS
1521 for (i = 0; i < sbi->s_groups_count; i++) {
1522 gdp = ext4_get_group_desc(sb, i, &bh);
1523
1524 flex_group = ext4_flex_group(sbi, i);
1525 sbi->s_flex_groups[flex_group].free_inodes +=
1526 le16_to_cpu(gdp->bg_free_inodes_count);
1527 sbi->s_flex_groups[flex_group].free_blocks +=
1528 le16_to_cpu(gdp->bg_free_blocks_count);
1529 }
1530
1531 return 1;
1532failed:
1533 return 0;
1534}
1535
717d50e4
AD
1536__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1537 struct ext4_group_desc *gdp)
1538{
1539 __u16 crc = 0;
1540
1541 if (sbi->s_es->s_feature_ro_compat &
1542 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1543 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1544 __le32 le_group = cpu_to_le32(block_group);
1545
1546 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1547 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1548 crc = crc16(crc, (__u8 *)gdp, offset);
1549 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1550 /* for checksum of struct ext4_group_desc do the rest...*/
1551 if ((sbi->s_es->s_feature_incompat &
1552 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1553 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1554 crc = crc16(crc, (__u8 *)gdp + offset,
1555 le16_to_cpu(sbi->s_es->s_desc_size) -
1556 offset);
1557 }
1558
1559 return cpu_to_le16(crc);
1560}
1561
1562int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1563 struct ext4_group_desc *gdp)
1564{
1565 if ((sbi->s_es->s_feature_ro_compat &
1566 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1567 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1568 return 0;
1569
1570 return 1;
1571}
1572
ac27a0ec 1573/* Called at mount-time, super-block is locked */
197cd65a 1574static int ext4_check_descriptors(struct super_block *sb)
ac27a0ec 1575{
617ba13b
MC
1576 struct ext4_sb_info *sbi = EXT4_SB(sb);
1577 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1578 ext4_fsblk_t last_block;
bd81d8ee
LV
1579 ext4_fsblk_t block_bitmap;
1580 ext4_fsblk_t inode_bitmap;
1581 ext4_fsblk_t inode_table;
ce421581 1582 int flexbg_flag = 0;
fd2d4291 1583 ext4_group_t i;
ac27a0ec 1584
ce421581
JS
1585 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1586 flexbg_flag = 1;
1587
af5bc92d 1588 ext4_debug("Checking group descriptors");
ac27a0ec 1589
197cd65a
AM
1590 for (i = 0; i < sbi->s_groups_count; i++) {
1591 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1592
ce421581 1593 if (i == sbi->s_groups_count - 1 || flexbg_flag)
bd81d8ee 1594 last_block = ext4_blocks_count(sbi->s_es) - 1;
ac27a0ec
DK
1595 else
1596 last_block = first_block +
617ba13b 1597 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
ac27a0ec 1598
8fadc143 1599 block_bitmap = ext4_block_bitmap(sb, gdp);
2b2d6d01 1600 if (block_bitmap < first_block || block_bitmap > last_block) {
c19204b0
JB
1601 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1602 "Block bitmap for group %lu not in group "
5128273a 1603 "(block %llu)!\n", i, block_bitmap);
ac27a0ec
DK
1604 return 0;
1605 }
8fadc143 1606 inode_bitmap = ext4_inode_bitmap(sb, gdp);
2b2d6d01 1607 if (inode_bitmap < first_block || inode_bitmap > last_block) {
c19204b0
JB
1608 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1609 "Inode bitmap for group %lu not in group "
5128273a 1610 "(block %llu)!\n", i, inode_bitmap);
ac27a0ec
DK
1611 return 0;
1612 }
8fadc143 1613 inode_table = ext4_inode_table(sb, gdp);
bd81d8ee 1614 if (inode_table < first_block ||
2b2d6d01 1615 inode_table + sbi->s_itb_per_group - 1 > last_block) {
c19204b0
JB
1616 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1617 "Inode table for group %lu not in group "
5128273a 1618 "(block %llu)!\n", i, inode_table);
ac27a0ec
DK
1619 return 0;
1620 }
b5f10eed 1621 spin_lock(sb_bgl_lock(sbi, i));
717d50e4 1622 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
c19204b0
JB
1623 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1624 "Checksum for group %lu failed (%u!=%u)\n",
1625 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1626 gdp)), le16_to_cpu(gdp->bg_checksum));
7ee1ec4c
LZ
1627 if (!(sb->s_flags & MS_RDONLY)) {
1628 spin_unlock(sb_bgl_lock(sbi, i));
8a266467 1629 return 0;
7ee1ec4c 1630 }
717d50e4 1631 }
b5f10eed 1632 spin_unlock(sb_bgl_lock(sbi, i));
ce421581
JS
1633 if (!flexbg_flag)
1634 first_block += EXT4_BLOCKS_PER_GROUP(sb);
ac27a0ec
DK
1635 }
1636
bd81d8ee 1637 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
2b2d6d01 1638 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
ac27a0ec
DK
1639 return 1;
1640}
1641
617ba13b 1642/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
ac27a0ec
DK
1643 * the superblock) which were deleted from all directories, but held open by
1644 * a process at the time of a crash. We walk the list and try to delete these
1645 * inodes at recovery time (only with a read-write filesystem).
1646 *
1647 * In order to keep the orphan inode chain consistent during traversal (in
1648 * case of crash during recovery), we link each inode into the superblock
1649 * orphan list_head and handle it the same way as an inode deletion during
1650 * normal operation (which journals the operations for us).
1651 *
1652 * We only do an iget() and an iput() on each inode, which is very safe if we
1653 * accidentally point at an in-use or already deleted inode. The worst that
1654 * can happen in this case is that we get a "bit already cleared" message from
617ba13b 1655 * ext4_free_inode(). The only reason we would point at a wrong inode is if
ac27a0ec
DK
1656 * e2fsck was run on this filesystem, and it must have already done the orphan
1657 * inode cleanup for us, so we can safely abort without any further action.
1658 */
2b2d6d01
TT
1659static void ext4_orphan_cleanup(struct super_block *sb,
1660 struct ext4_super_block *es)
ac27a0ec
DK
1661{
1662 unsigned int s_flags = sb->s_flags;
1663 int nr_orphans = 0, nr_truncates = 0;
1664#ifdef CONFIG_QUOTA
1665 int i;
1666#endif
1667 if (!es->s_last_orphan) {
1668 jbd_debug(4, "no orphan inodes to clean up\n");
1669 return;
1670 }
1671
a8f48a95
ES
1672 if (bdev_read_only(sb->s_bdev)) {
1673 printk(KERN_ERR "EXT4-fs: write access "
1674 "unavailable, skipping orphan cleanup.\n");
1675 return;
1676 }
1677
617ba13b 1678 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
ac27a0ec
DK
1679 if (es->s_last_orphan)
1680 jbd_debug(1, "Errors on filesystem, "
1681 "clearing orphan list.\n");
1682 es->s_last_orphan = 0;
1683 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1684 return;
1685 }
1686
1687 if (s_flags & MS_RDONLY) {
617ba13b 1688 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
ac27a0ec
DK
1689 sb->s_id);
1690 sb->s_flags &= ~MS_RDONLY;
1691 }
1692#ifdef CONFIG_QUOTA
1693 /* Needed for iput() to work correctly and not trash data */
1694 sb->s_flags |= MS_ACTIVE;
1695 /* Turn on quotas so that they are updated correctly */
1696 for (i = 0; i < MAXQUOTAS; i++) {
617ba13b
MC
1697 if (EXT4_SB(sb)->s_qf_names[i]) {
1698 int ret = ext4_quota_on_mount(sb, i);
ac27a0ec
DK
1699 if (ret < 0)
1700 printk(KERN_ERR
2c8be6b2 1701 "EXT4-fs: Cannot turn on journaled "
ac27a0ec
DK
1702 "quota: error %d\n", ret);
1703 }
1704 }
1705#endif
1706
1707 while (es->s_last_orphan) {
1708 struct inode *inode;
1709
97bd42b9
JB
1710 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1711 if (IS_ERR(inode)) {
ac27a0ec
DK
1712 es->s_last_orphan = 0;
1713 break;
1714 }
1715
617ba13b 1716 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
ac27a0ec
DK
1717 DQUOT_INIT(inode);
1718 if (inode->i_nlink) {
1719 printk(KERN_DEBUG
e5f8eab8 1720 "%s: truncating inode %lu to %lld bytes\n",
46e665e9 1721 __func__, inode->i_ino, inode->i_size);
e5f8eab8 1722 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
ac27a0ec 1723 inode->i_ino, inode->i_size);
617ba13b 1724 ext4_truncate(inode);
ac27a0ec
DK
1725 nr_truncates++;
1726 } else {
1727 printk(KERN_DEBUG
1728 "%s: deleting unreferenced inode %lu\n",
46e665e9 1729 __func__, inode->i_ino);
ac27a0ec
DK
1730 jbd_debug(2, "deleting unreferenced inode %lu\n",
1731 inode->i_ino);
1732 nr_orphans++;
1733 }
1734 iput(inode); /* The delete magic happens here! */
1735 }
1736
2b2d6d01 1737#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
ac27a0ec
DK
1738
1739 if (nr_orphans)
617ba13b 1740 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
ac27a0ec
DK
1741 sb->s_id, PLURAL(nr_orphans));
1742 if (nr_truncates)
617ba13b 1743 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
ac27a0ec
DK
1744 sb->s_id, PLURAL(nr_truncates));
1745#ifdef CONFIG_QUOTA
1746 /* Turn quotas off */
1747 for (i = 0; i < MAXQUOTAS; i++) {
1748 if (sb_dqopt(sb)->files[i])
6f28e087 1749 vfs_quota_off(sb, i, 0);
ac27a0ec
DK
1750 }
1751#endif
1752 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1753}
cd2291a4
ES
1754/*
1755 * Maximal extent format file size.
1756 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1757 * extent format containers, within a sector_t, and within i_blocks
1758 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1759 * so that won't be a limiting factor.
1760 *
1761 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1762 */
f287a1a5 1763static loff_t ext4_max_size(int blkbits, int has_huge_files)
cd2291a4
ES
1764{
1765 loff_t res;
1766 loff_t upper_limit = MAX_LFS_FILESIZE;
1767
1768 /* small i_blocks in vfs inode? */
f287a1a5 1769 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
cd2291a4 1770 /*
b3a6ffe1 1771 * CONFIG_LBD is not enabled implies the inode
cd2291a4
ES
1772 * i_block represent total blocks in 512 bytes
1773 * 32 == size of vfs inode i_blocks * 8
1774 */
1775 upper_limit = (1LL << 32) - 1;
1776
1777 /* total blocks in file system block size */
1778 upper_limit >>= (blkbits - 9);
1779 upper_limit <<= blkbits;
1780 }
1781
1782 /* 32-bit extent-start container, ee_block */
1783 res = 1LL << 32;
1784 res <<= blkbits;
1785 res -= 1;
1786
1787 /* Sanity check against vm- & vfs- imposed limits */
1788 if (res > upper_limit)
1789 res = upper_limit;
1790
1791 return res;
1792}
ac27a0ec 1793
ac27a0ec 1794/*
cd2291a4 1795 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
0fc1b451
AK
1796 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1797 * We need to be 1 filesystem block less than the 2^48 sector limit.
ac27a0ec 1798 */
f287a1a5 1799static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
ac27a0ec 1800{
617ba13b 1801 loff_t res = EXT4_NDIR_BLOCKS;
0fc1b451
AK
1802 int meta_blocks;
1803 loff_t upper_limit;
1804 /* This is calculated to be the largest file size for a
cd2291a4 1805 * dense, bitmapped file such that the total number of
ac27a0ec 1806 * sectors in the file, including data and all indirect blocks,
0fc1b451
AK
1807 * does not exceed 2^48 -1
1808 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1809 * total number of 512 bytes blocks of the file
1810 */
1811
f287a1a5 1812 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
0fc1b451 1813 /*
b3a6ffe1 1814 * !has_huge_files or CONFIG_LBD is not enabled
f287a1a5
TT
1815 * implies the inode i_block represent total blocks in
1816 * 512 bytes 32 == size of vfs inode i_blocks * 8
0fc1b451
AK
1817 */
1818 upper_limit = (1LL << 32) - 1;
1819
1820 /* total blocks in file system block size */
1821 upper_limit >>= (bits - 9);
1822
1823 } else {
8180a562
AK
1824 /*
1825 * We use 48 bit ext4_inode i_blocks
1826 * With EXT4_HUGE_FILE_FL set the i_blocks
1827 * represent total number of blocks in
1828 * file system block size
1829 */
0fc1b451
AK
1830 upper_limit = (1LL << 48) - 1;
1831
0fc1b451
AK
1832 }
1833
1834 /* indirect blocks */
1835 meta_blocks = 1;
1836 /* double indirect blocks */
1837 meta_blocks += 1 + (1LL << (bits-2));
1838 /* tripple indirect blocks */
1839 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1840
1841 upper_limit -= meta_blocks;
1842 upper_limit <<= bits;
ac27a0ec
DK
1843
1844 res += 1LL << (bits-2);
1845 res += 1LL << (2*(bits-2));
1846 res += 1LL << (3*(bits-2));
1847 res <<= bits;
1848 if (res > upper_limit)
1849 res = upper_limit;
0fc1b451
AK
1850
1851 if (res > MAX_LFS_FILESIZE)
1852 res = MAX_LFS_FILESIZE;
1853
ac27a0ec
DK
1854 return res;
1855}
1856
617ba13b 1857static ext4_fsblk_t descriptor_loc(struct super_block *sb,
70bbb3e0 1858 ext4_fsblk_t logical_sb_block, int nr)
ac27a0ec 1859{
617ba13b 1860 struct ext4_sb_info *sbi = EXT4_SB(sb);
fd2d4291 1861 ext4_group_t bg, first_meta_bg;
ac27a0ec
DK
1862 int has_super = 0;
1863
1864 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1865
617ba13b 1866 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
ac27a0ec 1867 nr < first_meta_bg)
70bbb3e0 1868 return logical_sb_block + nr + 1;
ac27a0ec 1869 bg = sbi->s_desc_per_block * nr;
617ba13b 1870 if (ext4_bg_has_super(sb, bg))
ac27a0ec 1871 has_super = 1;
617ba13b 1872 return (has_super + ext4_group_first_block_no(sb, bg));
ac27a0ec
DK
1873}
1874
c9de560d
AT
1875/**
1876 * ext4_get_stripe_size: Get the stripe size.
1877 * @sbi: In memory super block info
1878 *
1879 * If we have specified it via mount option, then
1880 * use the mount option value. If the value specified at mount time is
1881 * greater than the blocks per group use the super block value.
1882 * If the super block value is greater than blocks per group return 0.
1883 * Allocator needs it be less than blocks per group.
1884 *
1885 */
1886static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1887{
1888 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1889 unsigned long stripe_width =
1890 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1891
1892 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1893 return sbi->s_stripe;
1894
1895 if (stripe_width <= sbi->s_blocks_per_group)
1896 return stripe_width;
1897
1898 if (stride <= sbi->s_blocks_per_group)
1899 return stride;
1900
1901 return 0;
1902}
ac27a0ec 1903
2b2d6d01 1904static int ext4_fill_super(struct super_block *sb, void *data, int silent)
7477827f
AK
1905 __releases(kernel_lock)
1906 __acquires(kernel_lock)
1d03ec98 1907
ac27a0ec 1908{
2b2d6d01 1909 struct buffer_head *bh;
617ba13b
MC
1910 struct ext4_super_block *es = NULL;
1911 struct ext4_sb_info *sbi;
1912 ext4_fsblk_t block;
1913 ext4_fsblk_t sb_block = get_sb_block(&data);
70bbb3e0 1914 ext4_fsblk_t logical_sb_block;
ac27a0ec
DK
1915 unsigned long offset = 0;
1916 unsigned int journal_inum = 0;
1917 unsigned long journal_devnum = 0;
1918 unsigned long def_mount_opts;
1919 struct inode *root;
9f6200bb 1920 char *cp;
0390131b 1921 const char *descr;
1d1fe1ee 1922 int ret = -EINVAL;
ac27a0ec 1923 int blocksize;
ac27a0ec
DK
1924 int db_count;
1925 int i;
f287a1a5 1926 int needs_recovery, has_huge_files;
ac27a0ec 1927 __le32 features;
bd81d8ee 1928 __u64 blocks_count;
833f4077 1929 int err;
ac27a0ec
DK
1930
1931 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1932 if (!sbi)
1933 return -ENOMEM;
1934 sb->s_fs_info = sbi;
1935 sbi->s_mount_opt = 0;
617ba13b
MC
1936 sbi->s_resuid = EXT4_DEF_RESUID;
1937 sbi->s_resgid = EXT4_DEF_RESGID;
240799cd 1938 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
d9c9bef1 1939 sbi->s_sb_block = sb_block;
ac27a0ec
DK
1940
1941 unlock_kernel();
1942
9f6200bb
TT
1943 /* Cleanup superblock name */
1944 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
1945 *cp = '!';
1946
617ba13b 1947 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
ac27a0ec 1948 if (!blocksize) {
617ba13b 1949 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
ac27a0ec
DK
1950 goto out_fail;
1951 }
1952
1953 /*
617ba13b 1954 * The ext4 superblock will not be buffer aligned for other than 1kB
ac27a0ec
DK
1955 * block sizes. We need to calculate the offset from buffer start.
1956 */
617ba13b 1957 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
70bbb3e0
AM
1958 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1959 offset = do_div(logical_sb_block, blocksize);
ac27a0ec 1960 } else {
70bbb3e0 1961 logical_sb_block = sb_block;
ac27a0ec
DK
1962 }
1963
70bbb3e0 1964 if (!(bh = sb_bread(sb, logical_sb_block))) {
2b2d6d01 1965 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
ac27a0ec
DK
1966 goto out_fail;
1967 }
1968 /*
1969 * Note: s_es must be initialized as soon as possible because
617ba13b 1970 * some ext4 macro-instructions depend on its value
ac27a0ec 1971 */
617ba13b 1972 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
ac27a0ec
DK
1973 sbi->s_es = es;
1974 sb->s_magic = le16_to_cpu(es->s_magic);
617ba13b
MC
1975 if (sb->s_magic != EXT4_SUPER_MAGIC)
1976 goto cantfind_ext4;
ac27a0ec
DK
1977
1978 /* Set defaults before we parse the mount options */
1979 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
617ba13b 1980 if (def_mount_opts & EXT4_DEFM_DEBUG)
ac27a0ec 1981 set_opt(sbi->s_mount_opt, DEBUG);
617ba13b 1982 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
ac27a0ec 1983 set_opt(sbi->s_mount_opt, GRPID);
617ba13b 1984 if (def_mount_opts & EXT4_DEFM_UID16)
ac27a0ec 1985 set_opt(sbi->s_mount_opt, NO_UID32);
03010a33 1986#ifdef CONFIG_EXT4_FS_XATTR
617ba13b 1987 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
ac27a0ec 1988 set_opt(sbi->s_mount_opt, XATTR_USER);
2e7842b8 1989#endif
03010a33 1990#ifdef CONFIG_EXT4_FS_POSIX_ACL
617ba13b 1991 if (def_mount_opts & EXT4_DEFM_ACL)
ac27a0ec 1992 set_opt(sbi->s_mount_opt, POSIX_ACL);
2e7842b8 1993#endif
617ba13b
MC
1994 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1995 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1996 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1997 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1998 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1999 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2000
2001 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
ac27a0ec 2002 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
bb4f397a 2003 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
ceea16bf 2004 set_opt(sbi->s_mount_opt, ERRORS_CONT);
bb4f397a
AK
2005 else
2006 set_opt(sbi->s_mount_opt, ERRORS_RO);
ac27a0ec
DK
2007
2008 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2009 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
30773840
TT
2010 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2011 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2012 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
ac27a0ec
DK
2013
2014 set_opt(sbi->s_mount_opt, RESERVATION);
571640ca 2015 set_opt(sbi->s_mount_opt, BARRIER);
ac27a0ec 2016
1e2462f9
MC
2017 /*
2018 * turn on extents feature by default in ext4 filesystem
e4079a11
ES
2019 * only if feature flag already set by mkfs or tune2fs.
2020 * Use -o noextents to turn it off
1e2462f9 2021 */
e4079a11
ES
2022 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2023 set_opt(sbi->s_mount_opt, EXTENTS);
2024 else
2025 ext4_warning(sb, __func__,
2026 "extents feature not enabled on this filesystem, "
fde4d95a 2027 "use tune2fs.");
1e2462f9 2028
dd919b98
AK
2029 /*
2030 * enable delayed allocation by default
2031 * Use -o nodelalloc to turn it off
2032 */
2033 set_opt(sbi->s_mount_opt, DELALLOC);
2034
2035
2b2d6d01
TT
2036 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2037 NULL, 0))
ac27a0ec
DK
2038 goto failed_mount;
2039
2040 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
617ba13b 2041 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec 2042
617ba13b
MC
2043 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2044 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2045 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2046 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
ac27a0ec 2047 printk(KERN_WARNING
617ba13b 2048 "EXT4-fs warning: feature flags set on rev 0 fs, "
ac27a0ec 2049 "running e2fsck is recommended\n");
469108ff 2050
ac27a0ec
DK
2051 /*
2052 * Check feature flags regardless of the revision level, since we
2053 * previously didn't change the revision level when setting the flags,
2054 * so there is a chance incompat flags are set on a rev 0 filesystem.
2055 */
617ba13b 2056 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
ac27a0ec 2057 if (features) {
617ba13b 2058 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
ac27a0ec
DK
2059 "unsupported optional features (%x).\n",
2060 sb->s_id, le32_to_cpu(features));
2061 goto failed_mount;
2062 }
617ba13b 2063 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
ac27a0ec 2064 if (!(sb->s_flags & MS_RDONLY) && features) {
617ba13b 2065 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
ac27a0ec
DK
2066 "unsupported optional features (%x).\n",
2067 sb->s_id, le32_to_cpu(features));
2068 goto failed_mount;
2069 }
f287a1a5
TT
2070 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2071 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2072 if (has_huge_files) {
0fc1b451
AK
2073 /*
2074 * Large file size enabled file system can only be
b3a6ffe1 2075 * mount if kernel is build with CONFIG_LBD
0fc1b451
AK
2076 */
2077 if (sizeof(root->i_blocks) < sizeof(u64) &&
2078 !(sb->s_flags & MS_RDONLY)) {
2079 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2080 "files cannot be mounted read-write "
b3a6ffe1 2081 "without CONFIG_LBD.\n", sb->s_id);
0fc1b451
AK
2082 goto failed_mount;
2083 }
2084 }
ac27a0ec
DK
2085 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2086
617ba13b
MC
2087 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2088 blocksize > EXT4_MAX_BLOCK_SIZE) {
ac27a0ec 2089 printk(KERN_ERR
617ba13b 2090 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
ac27a0ec
DK
2091 blocksize, sb->s_id);
2092 goto failed_mount;
2093 }
2094
ac27a0ec 2095 if (sb->s_blocksize != blocksize) {
ce40733c
AK
2096
2097 /* Validate the filesystem blocksize */
2098 if (!sb_set_blocksize(sb, blocksize)) {
2099 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2100 blocksize);
ac27a0ec
DK
2101 goto failed_mount;
2102 }
2103
2b2d6d01 2104 brelse(bh);
70bbb3e0
AM
2105 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2106 offset = do_div(logical_sb_block, blocksize);
2107 bh = sb_bread(sb, logical_sb_block);
ac27a0ec
DK
2108 if (!bh) {
2109 printk(KERN_ERR
617ba13b 2110 "EXT4-fs: Can't read superblock on 2nd try.\n");
ac27a0ec
DK
2111 goto failed_mount;
2112 }
617ba13b 2113 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
ac27a0ec 2114 sbi->s_es = es;
617ba13b 2115 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2b2d6d01
TT
2116 printk(KERN_ERR
2117 "EXT4-fs: Magic mismatch, very weird !\n");
ac27a0ec
DK
2118 goto failed_mount;
2119 }
2120 }
2121
f287a1a5
TT
2122 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2123 has_huge_files);
2124 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
ac27a0ec 2125
617ba13b
MC
2126 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2127 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2128 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
ac27a0ec
DK
2129 } else {
2130 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2131 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
617ba13b 2132 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
1330593e 2133 (!is_power_of_2(sbi->s_inode_size)) ||
ac27a0ec 2134 (sbi->s_inode_size > blocksize)) {
2b2d6d01
TT
2135 printk(KERN_ERR
2136 "EXT4-fs: unsupported inode size: %d\n",
2137 sbi->s_inode_size);
ac27a0ec
DK
2138 goto failed_mount;
2139 }
ef7f3835
KS
2140 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2141 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
ac27a0ec 2142 }
0d1ee42f
AR
2143 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2144 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
8fadc143 2145 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
0d1ee42f 2146 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
d8ea6cf8 2147 !is_power_of_2(sbi->s_desc_size)) {
0d1ee42f 2148 printk(KERN_ERR
8fadc143 2149 "EXT4-fs: unsupported descriptor size %lu\n",
0d1ee42f
AR
2150 sbi->s_desc_size);
2151 goto failed_mount;
2152 }
2153 } else
2154 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
ac27a0ec 2155 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
ac27a0ec 2156 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
b47b6f38 2157 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
617ba13b
MC
2158 goto cantfind_ext4;
2159 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
ac27a0ec 2160 if (sbi->s_inodes_per_block == 0)
617ba13b 2161 goto cantfind_ext4;
ac27a0ec
DK
2162 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2163 sbi->s_inodes_per_block;
0d1ee42f 2164 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
ac27a0ec
DK
2165 sbi->s_sbh = bh;
2166 sbi->s_mount_state = le16_to_cpu(es->s_state);
e57aa839
FW
2167 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2168 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2b2d6d01 2169 for (i = 0; i < 4; i++)
ac27a0ec
DK
2170 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2171 sbi->s_def_hash_version = es->s_def_hash_version;
f99b2589
TT
2172 i = le32_to_cpu(es->s_flags);
2173 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2174 sbi->s_hash_unsigned = 3;
2175 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2176#ifdef __CHAR_UNSIGNED__
2177 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2178 sbi->s_hash_unsigned = 3;
2179#else
2180 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2181#endif
2182 sb->s_dirt = 1;
2183 }
ac27a0ec
DK
2184
2185 if (sbi->s_blocks_per_group > blocksize * 8) {
2b2d6d01
TT
2186 printk(KERN_ERR
2187 "EXT4-fs: #blocks per group too big: %lu\n",
2188 sbi->s_blocks_per_group);
ac27a0ec
DK
2189 goto failed_mount;
2190 }
ac27a0ec 2191 if (sbi->s_inodes_per_group > blocksize * 8) {
2b2d6d01
TT
2192 printk(KERN_ERR
2193 "EXT4-fs: #inodes per group too big: %lu\n",
2194 sbi->s_inodes_per_group);
ac27a0ec
DK
2195 goto failed_mount;
2196 }
2197
bd81d8ee 2198 if (ext4_blocks_count(es) >
ac27a0ec 2199 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
617ba13b 2200 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
ac27a0ec
DK
2201 " too large to mount safely\n", sb->s_id);
2202 if (sizeof(sector_t) < 8)
617ba13b 2203 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
ac27a0ec
DK
2204 "enabled\n");
2205 goto failed_mount;
2206 }
2207
617ba13b
MC
2208 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2209 goto cantfind_ext4;
e7c95593
ES
2210
2211 /* ensure blocks_count calculation below doesn't sign-extend */
2212 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2213 le32_to_cpu(es->s_first_data_block) + 1) {
2214 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2215 "first data block %u, blocks per group %lu\n",
2216 ext4_blocks_count(es),
2217 le32_to_cpu(es->s_first_data_block),
2218 EXT4_BLOCKS_PER_GROUP(sb));
2219 goto failed_mount;
2220 }
bd81d8ee
LV
2221 blocks_count = (ext4_blocks_count(es) -
2222 le32_to_cpu(es->s_first_data_block) +
2223 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2224 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2225 sbi->s_groups_count = blocks_count;
617ba13b
MC
2226 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2227 EXT4_DESC_PER_BLOCK(sb);
2b2d6d01 2228 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
ac27a0ec
DK
2229 GFP_KERNEL);
2230 if (sbi->s_group_desc == NULL) {
2b2d6d01 2231 printk(KERN_ERR "EXT4-fs: not enough memory\n");
ac27a0ec
DK
2232 goto failed_mount;
2233 }
2234
3244fcb1 2235#ifdef CONFIG_PROC_FS
9f6200bb
TT
2236 if (ext4_proc_root)
2237 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2238
240799cd
TT
2239 if (sbi->s_proc)
2240 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2241 &ext4_ui_proc_fops,
2242 &sbi->s_inode_readahead_blks);
3244fcb1 2243#endif
240799cd 2244
ac27a0ec
DK
2245 bgl_lock_init(&sbi->s_blockgroup_lock);
2246
2247 for (i = 0; i < db_count; i++) {
70bbb3e0 2248 block = descriptor_loc(sb, logical_sb_block, i);
ac27a0ec
DK
2249 sbi->s_group_desc[i] = sb_bread(sb, block);
2250 if (!sbi->s_group_desc[i]) {
2b2d6d01
TT
2251 printk(KERN_ERR "EXT4-fs: "
2252 "can't read group descriptor %d\n", i);
ac27a0ec
DK
2253 db_count = i;
2254 goto failed_mount2;
2255 }
2256 }
2b2d6d01 2257 if (!ext4_check_descriptors(sb)) {
617ba13b 2258 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
ac27a0ec
DK
2259 goto failed_mount2;
2260 }
772cb7c8
JS
2261 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2262 if (!ext4_fill_flex_info(sb)) {
2263 printk(KERN_ERR
2264 "EXT4-fs: unable to initialize "
2265 "flex_bg meta info!\n");
2266 goto failed_mount2;
2267 }
2268
ac27a0ec
DK
2269 sbi->s_gdb_count = db_count;
2270 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2271 spin_lock_init(&sbi->s_next_gen_lock);
2272
833f4077
PZ
2273 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2274 ext4_count_free_blocks(sb));
2275 if (!err) {
2276 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2277 ext4_count_free_inodes(sb));
2278 }
2279 if (!err) {
2280 err = percpu_counter_init(&sbi->s_dirs_counter,
2281 ext4_count_dirs(sb));
2282 }
6bc6e63f
AK
2283 if (!err) {
2284 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2285 }
833f4077
PZ
2286 if (err) {
2287 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2288 goto failed_mount3;
2289 }
ac27a0ec 2290
c9de560d
AT
2291 sbi->s_stripe = ext4_get_stripe_size(sbi);
2292
ac27a0ec
DK
2293 /*
2294 * set up enough so that it can read an inode
2295 */
617ba13b
MC
2296 sb->s_op = &ext4_sops;
2297 sb->s_export_op = &ext4_export_ops;
2298 sb->s_xattr = ext4_xattr_handlers;
ac27a0ec 2299#ifdef CONFIG_QUOTA
617ba13b
MC
2300 sb->s_qcop = &ext4_qctl_operations;
2301 sb->dq_op = &ext4_quota_operations;
ac27a0ec
DK
2302#endif
2303 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2304
2305 sb->s_root = NULL;
2306
2307 needs_recovery = (es->s_last_orphan != 0 ||
617ba13b
MC
2308 EXT4_HAS_INCOMPAT_FEATURE(sb,
2309 EXT4_FEATURE_INCOMPAT_RECOVER));
ac27a0ec
DK
2310
2311 /*
2312 * The first inode we look at is the journal inode. Don't try
2313 * root first: it may be modified in the journal!
2314 */
2315 if (!test_opt(sb, NOLOAD) &&
617ba13b
MC
2316 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2317 if (ext4_load_journal(sb, es, journal_devnum))
ac27a0ec 2318 goto failed_mount3;
624080ed
TT
2319 if (!(sb->s_flags & MS_RDONLY) &&
2320 EXT4_SB(sb)->s_journal->j_failed_commit) {
2321 printk(KERN_CRIT "EXT4-fs error (device %s): "
2322 "ext4_fill_super: Journal transaction "
2b2d6d01 2323 "%u is corrupt\n", sb->s_id,
624080ed 2324 EXT4_SB(sb)->s_journal->j_failed_commit);
2b2d6d01
TT
2325 if (test_opt(sb, ERRORS_RO)) {
2326 printk(KERN_CRIT
2327 "Mounting filesystem read-only\n");
624080ed
TT
2328 sb->s_flags |= MS_RDONLY;
2329 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2330 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2331 }
2332 if (test_opt(sb, ERRORS_PANIC)) {
2333 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2334 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2335 ext4_commit_super(sb, es, 1);
624080ed
TT
2336 goto failed_mount4;
2337 }
2338 }
ac27a0ec 2339 } else if (journal_inum) {
617ba13b 2340 if (ext4_create_journal(sb, es, journal_inum))
ac27a0ec 2341 goto failed_mount3;
0390131b
FM
2342 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2343 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2344 printk(KERN_ERR "EXT4-fs: required journal recovery "
2345 "suppressed and not mounted read-only\n");
2346 goto failed_mount4;
ac27a0ec 2347 } else {
0390131b
FM
2348 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2349 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2350 sbi->s_journal = NULL;
2351 needs_recovery = 0;
2352 goto no_journal;
ac27a0ec
DK
2353 }
2354
eb40a09c
JS
2355 if (ext4_blocks_count(es) > 0xffffffffULL &&
2356 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2357 JBD2_FEATURE_INCOMPAT_64BIT)) {
2358 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2359 goto failed_mount4;
2360 }
2361
818d276c
GS
2362 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2363 jbd2_journal_set_features(sbi->s_journal,
2364 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2365 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2366 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2367 jbd2_journal_set_features(sbi->s_journal,
2368 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2369 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2370 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2371 } else {
2372 jbd2_journal_clear_features(sbi->s_journal,
2373 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2374 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2375 }
2376
ac27a0ec
DK
2377 /* We have now updated the journal if required, so we can
2378 * validate the data journaling mode. */
2379 switch (test_opt(sb, DATA_FLAGS)) {
2380 case 0:
2381 /* No mode set, assume a default based on the journal
63f57933
AM
2382 * capabilities: ORDERED_DATA if the journal can
2383 * cope, else JOURNAL_DATA
2384 */
dab291af
MC
2385 if (jbd2_journal_check_available_features
2386 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
ac27a0ec
DK
2387 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2388 else
2389 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2390 break;
2391
617ba13b
MC
2392 case EXT4_MOUNT_ORDERED_DATA:
2393 case EXT4_MOUNT_WRITEBACK_DATA:
dab291af
MC
2394 if (!jbd2_journal_check_available_features
2395 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
617ba13b 2396 printk(KERN_ERR "EXT4-fs: Journal does not support "
ac27a0ec
DK
2397 "requested data journaling mode\n");
2398 goto failed_mount4;
2399 }
2400 default:
2401 break;
2402 }
2403
0390131b
FM
2404no_journal:
2405
ac27a0ec 2406 if (test_opt(sb, NOBH)) {
617ba13b
MC
2407 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2408 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
ac27a0ec
DK
2409 "its supported only with writeback mode\n");
2410 clear_opt(sbi->s_mount_opt, NOBH);
2411 }
2412 }
2413 /*
dab291af 2414 * The jbd2_journal_load will have done any necessary log recovery,
ac27a0ec
DK
2415 * so we can safely mount the rest of the filesystem now.
2416 */
2417
1d1fe1ee
DH
2418 root = ext4_iget(sb, EXT4_ROOT_INO);
2419 if (IS_ERR(root)) {
617ba13b 2420 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
1d1fe1ee 2421 ret = PTR_ERR(root);
ac27a0ec
DK
2422 goto failed_mount4;
2423 }
2424 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1d1fe1ee 2425 iput(root);
617ba13b 2426 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
ac27a0ec
DK
2427 goto failed_mount4;
2428 }
1d1fe1ee
DH
2429 sb->s_root = d_alloc_root(root);
2430 if (!sb->s_root) {
2431 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2432 iput(root);
2433 ret = -ENOMEM;
2434 goto failed_mount4;
2435 }
ac27a0ec 2436
2b2d6d01 2437 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
ef7f3835
KS
2438
2439 /* determine the minimum size of new large inodes, if present */
2440 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2441 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2442 EXT4_GOOD_OLD_INODE_SIZE;
2443 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2444 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2445 if (sbi->s_want_extra_isize <
2446 le16_to_cpu(es->s_want_extra_isize))
2447 sbi->s_want_extra_isize =
2448 le16_to_cpu(es->s_want_extra_isize);
2449 if (sbi->s_want_extra_isize <
2450 le16_to_cpu(es->s_min_extra_isize))
2451 sbi->s_want_extra_isize =
2452 le16_to_cpu(es->s_min_extra_isize);
2453 }
2454 }
2455 /* Check if enough inode space is available */
2456 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2457 sbi->s_inode_size) {
2458 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2459 EXT4_GOOD_OLD_INODE_SIZE;
2460 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2461 "available.\n");
2462 }
2463
c2774d84
AK
2464 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2465 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2466 "requested data journaling mode\n");
2467 clear_opt(sbi->s_mount_opt, DELALLOC);
2468 } else if (test_opt(sb, DELALLOC))
2469 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2470
2471 ext4_ext_init(sb);
2472 err = ext4_mb_init(sb, needs_recovery);
2473 if (err) {
2474 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2475 err);
2476 goto failed_mount4;
2477 }
2478
ac27a0ec
DK
2479 /*
2480 * akpm: core read_super() calls in here with the superblock locked.
2481 * That deadlocks, because orphan cleanup needs to lock the superblock
2482 * in numerous places. Here we just pop the lock - it's relatively
2483 * harmless, because we are now ready to accept write_super() requests,
2484 * and aviro says that's the only reason for hanging onto the
2485 * superblock lock.
2486 */
617ba13b
MC
2487 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2488 ext4_orphan_cleanup(sb, es);
2489 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
0390131b 2490 if (needs_recovery) {
2b2d6d01 2491 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
0390131b
FM
2492 ext4_mark_recovery_complete(sb, es);
2493 }
2494 if (EXT4_SB(sb)->s_journal) {
2495 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2496 descr = " journalled data mode";
2497 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2498 descr = " ordered data mode";
2499 else
2500 descr = " writeback data mode";
2501 } else
2502 descr = "out journal";
2503
2504 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2505 sb->s_id, descr);
ac27a0ec
DK
2506
2507 lock_kernel();
2508 return 0;
2509
617ba13b 2510cantfind_ext4:
ac27a0ec 2511 if (!silent)
617ba13b 2512 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
ac27a0ec
DK
2513 sb->s_id);
2514 goto failed_mount;
2515
2516failed_mount4:
0390131b
FM
2517 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2518 if (sbi->s_journal) {
2519 jbd2_journal_destroy(sbi->s_journal);
2520 sbi->s_journal = NULL;
2521 }
ac27a0ec
DK
2522failed_mount3:
2523 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2524 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2525 percpu_counter_destroy(&sbi->s_dirs_counter);
6bc6e63f 2526 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
ac27a0ec
DK
2527failed_mount2:
2528 for (i = 0; i < db_count; i++)
2529 brelse(sbi->s_group_desc[i]);
2530 kfree(sbi->s_group_desc);
2531failed_mount:
240799cd
TT
2532 if (sbi->s_proc) {
2533 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
9f6200bb 2534 remove_proc_entry(sb->s_id, ext4_proc_root);
240799cd 2535 }
ac27a0ec
DK
2536#ifdef CONFIG_QUOTA
2537 for (i = 0; i < MAXQUOTAS; i++)
2538 kfree(sbi->s_qf_names[i]);
2539#endif
617ba13b 2540 ext4_blkdev_remove(sbi);
ac27a0ec
DK
2541 brelse(bh);
2542out_fail:
2543 sb->s_fs_info = NULL;
2544 kfree(sbi);
2545 lock_kernel();
1d1fe1ee 2546 return ret;
ac27a0ec
DK
2547}
2548
2549/*
2550 * Setup any per-fs journal parameters now. We'll do this both on
2551 * initial mount, once the journal has been initialised but before we've
2552 * done any recovery; and again on any subsequent remount.
2553 */
617ba13b 2554static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
ac27a0ec 2555{
617ba13b 2556 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 2557
30773840
TT
2558 journal->j_commit_interval = sbi->s_commit_interval;
2559 journal->j_min_batch_time = sbi->s_min_batch_time;
2560 journal->j_max_batch_time = sbi->s_max_batch_time;
ac27a0ec
DK
2561
2562 spin_lock(&journal->j_state_lock);
2563 if (test_opt(sb, BARRIER))
dab291af 2564 journal->j_flags |= JBD2_BARRIER;
ac27a0ec 2565 else
dab291af 2566 journal->j_flags &= ~JBD2_BARRIER;
5bf5683a
HK
2567 if (test_opt(sb, DATA_ERR_ABORT))
2568 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2569 else
2570 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
ac27a0ec
DK
2571 spin_unlock(&journal->j_state_lock);
2572}
2573
617ba13b 2574static journal_t *ext4_get_journal(struct super_block *sb,
ac27a0ec
DK
2575 unsigned int journal_inum)
2576{
2577 struct inode *journal_inode;
2578 journal_t *journal;
2579
0390131b
FM
2580 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2581
ac27a0ec
DK
2582 /* First, test for the existence of a valid inode on disk. Bad
2583 * things happen if we iget() an unused inode, as the subsequent
2584 * iput() will try to delete it. */
2585
1d1fe1ee
DH
2586 journal_inode = ext4_iget(sb, journal_inum);
2587 if (IS_ERR(journal_inode)) {
617ba13b 2588 printk(KERN_ERR "EXT4-fs: no journal found.\n");
ac27a0ec
DK
2589 return NULL;
2590 }
2591 if (!journal_inode->i_nlink) {
2592 make_bad_inode(journal_inode);
2593 iput(journal_inode);
617ba13b 2594 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
ac27a0ec
DK
2595 return NULL;
2596 }
2597
e5f8eab8 2598 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
ac27a0ec 2599 journal_inode, journal_inode->i_size);
1d1fe1ee 2600 if (!S_ISREG(journal_inode->i_mode)) {
617ba13b 2601 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
ac27a0ec
DK
2602 iput(journal_inode);
2603 return NULL;
2604 }
2605
dab291af 2606 journal = jbd2_journal_init_inode(journal_inode);
ac27a0ec 2607 if (!journal) {
617ba13b 2608 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
ac27a0ec
DK
2609 iput(journal_inode);
2610 return NULL;
2611 }
2612 journal->j_private = sb;
617ba13b 2613 ext4_init_journal_params(sb, journal);
ac27a0ec
DK
2614 return journal;
2615}
2616
617ba13b 2617static journal_t *ext4_get_dev_journal(struct super_block *sb,
ac27a0ec
DK
2618 dev_t j_dev)
2619{
2b2d6d01 2620 struct buffer_head *bh;
ac27a0ec 2621 journal_t *journal;
617ba13b
MC
2622 ext4_fsblk_t start;
2623 ext4_fsblk_t len;
ac27a0ec 2624 int hblock, blocksize;
617ba13b 2625 ext4_fsblk_t sb_block;
ac27a0ec 2626 unsigned long offset;
2b2d6d01 2627 struct ext4_super_block *es;
ac27a0ec
DK
2628 struct block_device *bdev;
2629
0390131b
FM
2630 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2631
617ba13b 2632 bdev = ext4_blkdev_get(j_dev);
ac27a0ec
DK
2633 if (bdev == NULL)
2634 return NULL;
2635
2636 if (bd_claim(bdev, sb)) {
2637 printk(KERN_ERR
8c55e204 2638 "EXT4: failed to claim external journal device.\n");
9a1c3542 2639 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
ac27a0ec
DK
2640 return NULL;
2641 }
2642
2643 blocksize = sb->s_blocksize;
2644 hblock = bdev_hardsect_size(bdev);
2645 if (blocksize < hblock) {
2646 printk(KERN_ERR
617ba13b 2647 "EXT4-fs: blocksize too small for journal device.\n");
ac27a0ec
DK
2648 goto out_bdev;
2649 }
2650
617ba13b
MC
2651 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2652 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
ac27a0ec
DK
2653 set_blocksize(bdev, blocksize);
2654 if (!(bh = __bread(bdev, sb_block, blocksize))) {
617ba13b 2655 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
ac27a0ec
DK
2656 "external journal\n");
2657 goto out_bdev;
2658 }
2659
617ba13b
MC
2660 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2661 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
ac27a0ec 2662 !(le32_to_cpu(es->s_feature_incompat) &
617ba13b
MC
2663 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2664 printk(KERN_ERR "EXT4-fs: external journal has "
ac27a0ec
DK
2665 "bad superblock\n");
2666 brelse(bh);
2667 goto out_bdev;
2668 }
2669
617ba13b
MC
2670 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2671 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
ac27a0ec
DK
2672 brelse(bh);
2673 goto out_bdev;
2674 }
2675
bd81d8ee 2676 len = ext4_blocks_count(es);
ac27a0ec
DK
2677 start = sb_block + 1;
2678 brelse(bh); /* we're done with the superblock */
2679
dab291af 2680 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
ac27a0ec
DK
2681 start, len, blocksize);
2682 if (!journal) {
617ba13b 2683 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
ac27a0ec
DK
2684 goto out_bdev;
2685 }
2686 journal->j_private = sb;
2687 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2688 wait_on_buffer(journal->j_sb_buffer);
2689 if (!buffer_uptodate(journal->j_sb_buffer)) {
617ba13b 2690 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
ac27a0ec
DK
2691 goto out_journal;
2692 }
2693 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
617ba13b 2694 printk(KERN_ERR "EXT4-fs: External journal has more than one "
ac27a0ec
DK
2695 "user (unsupported) - %d\n",
2696 be32_to_cpu(journal->j_superblock->s_nr_users));
2697 goto out_journal;
2698 }
617ba13b
MC
2699 EXT4_SB(sb)->journal_bdev = bdev;
2700 ext4_init_journal_params(sb, journal);
ac27a0ec
DK
2701 return journal;
2702out_journal:
dab291af 2703 jbd2_journal_destroy(journal);
ac27a0ec 2704out_bdev:
617ba13b 2705 ext4_blkdev_put(bdev);
ac27a0ec
DK
2706 return NULL;
2707}
2708
617ba13b
MC
2709static int ext4_load_journal(struct super_block *sb,
2710 struct ext4_super_block *es,
ac27a0ec
DK
2711 unsigned long journal_devnum)
2712{
2713 journal_t *journal;
2714 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2715 dev_t journal_dev;
2716 int err = 0;
2717 int really_read_only;
2718
0390131b
FM
2719 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2720
ac27a0ec
DK
2721 if (journal_devnum &&
2722 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
617ba13b 2723 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
ac27a0ec
DK
2724 "numbers have changed\n");
2725 journal_dev = new_decode_dev(journal_devnum);
2726 } else
2727 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2728
2729 really_read_only = bdev_read_only(sb->s_bdev);
2730
2731 /*
2732 * Are we loading a blank journal or performing recovery after a
2733 * crash? For recovery, we need to check in advance whether we
2734 * can get read-write access to the device.
2735 */
2736
617ba13b 2737 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
ac27a0ec 2738 if (sb->s_flags & MS_RDONLY) {
617ba13b 2739 printk(KERN_INFO "EXT4-fs: INFO: recovery "
ac27a0ec
DK
2740 "required on readonly filesystem.\n");
2741 if (really_read_only) {
617ba13b 2742 printk(KERN_ERR "EXT4-fs: write access "
ac27a0ec
DK
2743 "unavailable, cannot proceed.\n");
2744 return -EROFS;
2745 }
2b2d6d01
TT
2746 printk(KERN_INFO "EXT4-fs: write access will "
2747 "be enabled during recovery.\n");
ac27a0ec
DK
2748 }
2749 }
2750
2751 if (journal_inum && journal_dev) {
617ba13b 2752 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
ac27a0ec
DK
2753 "and inode journals!\n");
2754 return -EINVAL;
2755 }
2756
2757 if (journal_inum) {
617ba13b 2758 if (!(journal = ext4_get_journal(sb, journal_inum)))
ac27a0ec
DK
2759 return -EINVAL;
2760 } else {
617ba13b 2761 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
ac27a0ec
DK
2762 return -EINVAL;
2763 }
2764
4776004f
TT
2765 if (journal->j_flags & JBD2_BARRIER)
2766 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2767 else
2768 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2769
ac27a0ec 2770 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
dab291af 2771 err = jbd2_journal_update_format(journal);
ac27a0ec 2772 if (err) {
617ba13b 2773 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
dab291af 2774 jbd2_journal_destroy(journal);
ac27a0ec
DK
2775 return err;
2776 }
2777 }
2778
617ba13b 2779 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
dab291af 2780 err = jbd2_journal_wipe(journal, !really_read_only);
ac27a0ec 2781 if (!err)
dab291af 2782 err = jbd2_journal_load(journal);
ac27a0ec
DK
2783
2784 if (err) {
617ba13b 2785 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
dab291af 2786 jbd2_journal_destroy(journal);
ac27a0ec
DK
2787 return err;
2788 }
2789
617ba13b
MC
2790 EXT4_SB(sb)->s_journal = journal;
2791 ext4_clear_journal_err(sb, es);
ac27a0ec
DK
2792
2793 if (journal_devnum &&
2794 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2795 es->s_journal_dev = cpu_to_le32(journal_devnum);
2796 sb->s_dirt = 1;
2797
2798 /* Make sure we flush the recovery flag to disk. */
617ba13b 2799 ext4_commit_super(sb, es, 1);
ac27a0ec
DK
2800 }
2801
2802 return 0;
2803}
2804
2b2d6d01
TT
2805static int ext4_create_journal(struct super_block *sb,
2806 struct ext4_super_block *es,
ac27a0ec
DK
2807 unsigned int journal_inum)
2808{
2809 journal_t *journal;
6c675bd4 2810 int err;
ac27a0ec
DK
2811
2812 if (sb->s_flags & MS_RDONLY) {
617ba13b 2813 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
ac27a0ec
DK
2814 "create journal.\n");
2815 return -EROFS;
2816 }
2817
6c675bd4
BP
2818 journal = ext4_get_journal(sb, journal_inum);
2819 if (!journal)
ac27a0ec
DK
2820 return -EINVAL;
2821
617ba13b 2822 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
ac27a0ec
DK
2823 journal_inum);
2824
6c675bd4
BP
2825 err = jbd2_journal_create(journal);
2826 if (err) {
617ba13b 2827 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
dab291af 2828 jbd2_journal_destroy(journal);
ac27a0ec
DK
2829 return -EIO;
2830 }
2831
617ba13b 2832 EXT4_SB(sb)->s_journal = journal;
ac27a0ec 2833
617ba13b
MC
2834 ext4_update_dynamic_rev(sb);
2835 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2836 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
ac27a0ec
DK
2837
2838 es->s_journal_inum = cpu_to_le32(journal_inum);
2839 sb->s_dirt = 1;
2840
2841 /* Make sure we flush the recovery flag to disk. */
617ba13b 2842 ext4_commit_super(sb, es, 1);
ac27a0ec
DK
2843
2844 return 0;
2845}
2846
2b2d6d01
TT
2847static void ext4_commit_super(struct super_block *sb,
2848 struct ext4_super_block *es, int sync)
ac27a0ec 2849{
617ba13b 2850 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
ac27a0ec
DK
2851
2852 if (!sbh)
2853 return;
914258bf
TT
2854 if (buffer_write_io_error(sbh)) {
2855 /*
2856 * Oh, dear. A previous attempt to write the
2857 * superblock failed. This could happen because the
2858 * USB device was yanked out. Or it could happen to
2859 * be a transient write error and maybe the block will
2860 * be remapped. Nothing we can do but to retry the
2861 * write and hope for the best.
2862 */
2863 printk(KERN_ERR "ext4: previous I/O error to "
2864 "superblock detected for %s.\n", sb->s_id);
2865 clear_buffer_write_io_error(sbh);
2866 set_buffer_uptodate(sbh);
2867 }
ac27a0ec 2868 es->s_wtime = cpu_to_le32(get_seconds());
bd81d8ee 2869 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
617ba13b 2870 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
ac27a0ec
DK
2871 BUFFER_TRACE(sbh, "marking dirty");
2872 mark_buffer_dirty(sbh);
914258bf 2873 if (sync) {
ac27a0ec 2874 sync_dirty_buffer(sbh);
914258bf
TT
2875 if (buffer_write_io_error(sbh)) {
2876 printk(KERN_ERR "ext4: I/O error while writing "
2877 "superblock for %s.\n", sb->s_id);
2878 clear_buffer_write_io_error(sbh);
2879 set_buffer_uptodate(sbh);
2880 }
2881 }
ac27a0ec
DK
2882}
2883
2884
2885/*
2886 * Have we just finished recovery? If so, and if we are mounting (or
2887 * remounting) the filesystem readonly, then we will end up with a
2888 * consistent fs on disk. Record that fact.
2889 */
2b2d6d01
TT
2890static void ext4_mark_recovery_complete(struct super_block *sb,
2891 struct ext4_super_block *es)
ac27a0ec 2892{
617ba13b 2893 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 2894
0390131b
FM
2895 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2896 BUG_ON(journal != NULL);
2897 return;
2898 }
dab291af 2899 jbd2_journal_lock_updates(journal);
7ffe1ea8
HK
2900 if (jbd2_journal_flush(journal) < 0)
2901 goto out;
2902
32c37730 2903 lock_super(sb);
617ba13b 2904 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
ac27a0ec 2905 sb->s_flags & MS_RDONLY) {
617ba13b 2906 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 2907 sb->s_dirt = 0;
617ba13b 2908 ext4_commit_super(sb, es, 1);
ac27a0ec 2909 }
32c37730 2910 unlock_super(sb);
7ffe1ea8
HK
2911
2912out:
dab291af 2913 jbd2_journal_unlock_updates(journal);
ac27a0ec
DK
2914}
2915
2916/*
2917 * If we are mounting (or read-write remounting) a filesystem whose journal
2918 * has recorded an error from a previous lifetime, move that error to the
2919 * main filesystem now.
2920 */
2b2d6d01
TT
2921static void ext4_clear_journal_err(struct super_block *sb,
2922 struct ext4_super_block *es)
ac27a0ec
DK
2923{
2924 journal_t *journal;
2925 int j_errno;
2926 const char *errstr;
2927
0390131b
FM
2928 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2929
617ba13b 2930 journal = EXT4_SB(sb)->s_journal;
ac27a0ec
DK
2931
2932 /*
2933 * Now check for any error status which may have been recorded in the
617ba13b 2934 * journal by a prior ext4_error() or ext4_abort()
ac27a0ec
DK
2935 */
2936
dab291af 2937 j_errno = jbd2_journal_errno(journal);
ac27a0ec
DK
2938 if (j_errno) {
2939 char nbuf[16];
2940
617ba13b 2941 errstr = ext4_decode_error(sb, j_errno, nbuf);
46e665e9 2942 ext4_warning(sb, __func__, "Filesystem error recorded "
ac27a0ec 2943 "from previous mount: %s", errstr);
46e665e9 2944 ext4_warning(sb, __func__, "Marking fs in need of "
ac27a0ec
DK
2945 "filesystem check.");
2946
617ba13b
MC
2947 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2948 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2b2d6d01 2949 ext4_commit_super(sb, es, 1);
ac27a0ec 2950
dab291af 2951 jbd2_journal_clear_err(journal);
ac27a0ec
DK
2952 }
2953}
2954
2955/*
2956 * Force the running and committing transactions to commit,
2957 * and wait on the commit.
2958 */
617ba13b 2959int ext4_force_commit(struct super_block *sb)
ac27a0ec
DK
2960{
2961 journal_t *journal;
0390131b 2962 int ret = 0;
ac27a0ec
DK
2963
2964 if (sb->s_flags & MS_RDONLY)
2965 return 0;
2966
617ba13b 2967 journal = EXT4_SB(sb)->s_journal;
0390131b
FM
2968 if (journal) {
2969 sb->s_dirt = 0;
2970 ret = ext4_journal_force_commit(journal);
2971 }
2972
ac27a0ec
DK
2973 return ret;
2974}
2975
2976/*
617ba13b 2977 * Ext4 always journals updates to the superblock itself, so we don't
ac27a0ec 2978 * have to propagate any other updates to the superblock on disk at this
14ce0cb4
TT
2979 * point. (We can probably nuke this function altogether, and remove
2980 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
ac27a0ec 2981 */
2b2d6d01 2982static void ext4_write_super(struct super_block *sb)
ac27a0ec 2983{
0390131b
FM
2984 if (EXT4_SB(sb)->s_journal) {
2985 if (mutex_trylock(&sb->s_lock) != 0)
2986 BUG();
2987 sb->s_dirt = 0;
2988 } else {
2989 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2990 }
ac27a0ec
DK
2991}
2992
617ba13b 2993static int ext4_sync_fs(struct super_block *sb, int wait)
ac27a0ec 2994{
14ce0cb4 2995 int ret = 0;
ac27a0ec 2996
ede86cc4 2997 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
ac27a0ec 2998 sb->s_dirt = 0;
0390131b
FM
2999 if (EXT4_SB(sb)->s_journal) {
3000 if (wait)
3001 ret = ext4_force_commit(sb);
3002 else
3003 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
3004 } else {
3005 ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
3006 }
14ce0cb4 3007 return ret;
ac27a0ec
DK
3008}
3009
3010/*
3011 * LVM calls this function before a (read-only) snapshot is created. This
3012 * gives us a chance to flush the journal completely and mark the fs clean.
3013 */
617ba13b 3014static void ext4_write_super_lockfs(struct super_block *sb)
ac27a0ec
DK
3015{
3016 sb->s_dirt = 0;
3017
3018 if (!(sb->s_flags & MS_RDONLY)) {
617ba13b 3019 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 3020
0390131b
FM
3021 if (journal) {
3022 /* Now we set up the journal barrier. */
3023 jbd2_journal_lock_updates(journal);
7ffe1ea8 3024
0390131b
FM
3025 /*
3026 * We don't want to clear needs_recovery flag when we
3027 * failed to flush the journal.
3028 */
3029 if (jbd2_journal_flush(journal) < 0)
3030 return;
3031 }
ac27a0ec
DK
3032
3033 /* Journal blocked and flushed, clear needs_recovery flag. */
617ba13b
MC
3034 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3035 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
ac27a0ec
DK
3036 }
3037}
3038
3039/*
3040 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3041 * flag here, even though the filesystem is not technically dirty yet.
3042 */
617ba13b 3043static void ext4_unlockfs(struct super_block *sb)
ac27a0ec 3044{
0390131b 3045 if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
ac27a0ec
DK
3046 lock_super(sb);
3047 /* Reser the needs_recovery flag before the fs is unlocked. */
617ba13b
MC
3048 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3049 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
ac27a0ec 3050 unlock_super(sb);
dab291af 3051 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
ac27a0ec
DK
3052 }
3053}
3054
2b2d6d01 3055static int ext4_remount(struct super_block *sb, int *flags, char *data)
ac27a0ec 3056{
2b2d6d01 3057 struct ext4_super_block *es;
617ba13b
MC
3058 struct ext4_sb_info *sbi = EXT4_SB(sb);
3059 ext4_fsblk_t n_blocks_count = 0;
ac27a0ec 3060 unsigned long old_sb_flags;
617ba13b 3061 struct ext4_mount_options old_opts;
8a266467 3062 ext4_group_t g;
ac27a0ec
DK
3063 int err;
3064#ifdef CONFIG_QUOTA
3065 int i;
3066#endif
3067
3068 /* Store the original options */
3069 old_sb_flags = sb->s_flags;
3070 old_opts.s_mount_opt = sbi->s_mount_opt;
3071 old_opts.s_resuid = sbi->s_resuid;
3072 old_opts.s_resgid = sbi->s_resgid;
3073 old_opts.s_commit_interval = sbi->s_commit_interval;
30773840
TT
3074 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3075 old_opts.s_max_batch_time = sbi->s_max_batch_time;
ac27a0ec
DK
3076#ifdef CONFIG_QUOTA
3077 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3078 for (i = 0; i < MAXQUOTAS; i++)
3079 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3080#endif
3081
3082 /*
3083 * Allow the "check" option to be passed as a remount option.
3084 */
3085 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3086 err = -EINVAL;
3087 goto restore_opts;
3088 }
3089
617ba13b 3090 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
46e665e9 3091 ext4_abort(sb, __func__, "Abort forced by user");
ac27a0ec
DK
3092
3093 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
617ba13b 3094 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec
DK
3095
3096 es = sbi->s_es;
3097
0390131b
FM
3098 if (sbi->s_journal)
3099 ext4_init_journal_params(sb, sbi->s_journal);
ac27a0ec
DK
3100
3101 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
bd81d8ee 3102 n_blocks_count > ext4_blocks_count(es)) {
617ba13b 3103 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
ac27a0ec
DK
3104 err = -EROFS;
3105 goto restore_opts;
3106 }
3107
3108 if (*flags & MS_RDONLY) {
3109 /*
3110 * First of all, the unconditional stuff we have to do
3111 * to disable replay of the journal when we next remount
3112 */
3113 sb->s_flags |= MS_RDONLY;
3114
3115 /*
3116 * OK, test if we are remounting a valid rw partition
3117 * readonly, and if so set the rdonly flag and then
3118 * mark the partition as valid again.
3119 */
617ba13b
MC
3120 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3121 (sbi->s_mount_state & EXT4_VALID_FS))
ac27a0ec
DK
3122 es->s_state = cpu_to_le16(sbi->s_mount_state);
3123
32c37730
JK
3124 /*
3125 * We have to unlock super so that we can wait for
3126 * transactions.
3127 */
0390131b
FM
3128 if (sbi->s_journal) {
3129 unlock_super(sb);
3130 ext4_mark_recovery_complete(sb, es);
3131 lock_super(sb);
3132 }
ac27a0ec
DK
3133 } else {
3134 __le32 ret;
617ba13b
MC
3135 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3136 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3137 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
ac27a0ec
DK
3138 "remount RDWR because of unsupported "
3139 "optional features (%x).\n",
3140 sb->s_id, le32_to_cpu(ret));
3141 err = -EROFS;
3142 goto restore_opts;
3143 }
ead6596b 3144
8a266467
TT
3145 /*
3146 * Make sure the group descriptor checksums
3147 * are sane. If they aren't, refuse to
3148 * remount r/w.
3149 */
3150 for (g = 0; g < sbi->s_groups_count; g++) {
3151 struct ext4_group_desc *gdp =
3152 ext4_get_group_desc(sb, g, NULL);
3153
3154 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3155 printk(KERN_ERR
3156 "EXT4-fs: ext4_remount: "
3157 "Checksum for group %lu failed (%u!=%u)\n",
3158 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3159 le16_to_cpu(gdp->bg_checksum));
3160 err = -EINVAL;
3161 goto restore_opts;
3162 }
3163 }
3164
ead6596b
ES
3165 /*
3166 * If we have an unprocessed orphan list hanging
3167 * around from a previously readonly bdev mount,
3168 * require a full umount/remount for now.
3169 */
3170 if (es->s_last_orphan) {
3171 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3172 "remount RDWR because of unprocessed "
3173 "orphan inode list. Please "
3174 "umount/remount instead.\n",
3175 sb->s_id);
3176 err = -EINVAL;
3177 goto restore_opts;
3178 }
3179
ac27a0ec
DK
3180 /*
3181 * Mounting a RDONLY partition read-write, so reread
3182 * and store the current valid flag. (It may have
3183 * been changed by e2fsck since we originally mounted
3184 * the partition.)
3185 */
0390131b
FM
3186 if (sbi->s_journal)
3187 ext4_clear_journal_err(sb, es);
ac27a0ec 3188 sbi->s_mount_state = le16_to_cpu(es->s_state);
617ba13b 3189 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
ac27a0ec 3190 goto restore_opts;
2b2d6d01 3191 if (!ext4_setup_super(sb, es, 0))
ac27a0ec
DK
3192 sb->s_flags &= ~MS_RDONLY;
3193 }
3194 }
0390131b
FM
3195 if (sbi->s_journal == NULL)
3196 ext4_commit_super(sb, es, 1);
3197
ac27a0ec
DK
3198#ifdef CONFIG_QUOTA
3199 /* Release old quota file names */
3200 for (i = 0; i < MAXQUOTAS; i++)
3201 if (old_opts.s_qf_names[i] &&
3202 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3203 kfree(old_opts.s_qf_names[i]);
3204#endif
3205 return 0;
3206restore_opts:
3207 sb->s_flags = old_sb_flags;
3208 sbi->s_mount_opt = old_opts.s_mount_opt;
3209 sbi->s_resuid = old_opts.s_resuid;
3210 sbi->s_resgid = old_opts.s_resgid;
3211 sbi->s_commit_interval = old_opts.s_commit_interval;
30773840
TT
3212 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3213 sbi->s_max_batch_time = old_opts.s_max_batch_time;
ac27a0ec
DK
3214#ifdef CONFIG_QUOTA
3215 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3216 for (i = 0; i < MAXQUOTAS; i++) {
3217 if (sbi->s_qf_names[i] &&
3218 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3219 kfree(sbi->s_qf_names[i]);
3220 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3221 }
3222#endif
3223 return err;
3224}
3225
2b2d6d01 3226static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
ac27a0ec
DK
3227{
3228 struct super_block *sb = dentry->d_sb;
617ba13b
MC
3229 struct ext4_sb_info *sbi = EXT4_SB(sb);
3230 struct ext4_super_block *es = sbi->s_es;
960cc398 3231 u64 fsid;
ac27a0ec 3232
5e70030d
BP
3233 if (test_opt(sb, MINIX_DF)) {
3234 sbi->s_overhead_last = 0;
6bc9feff 3235 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
fd2d4291 3236 ext4_group_t ngroups = sbi->s_groups_count, i;
5e70030d 3237 ext4_fsblk_t overhead = 0;
ac27a0ec
DK
3238 smp_rmb();
3239
3240 /*
5e70030d
BP
3241 * Compute the overhead (FS structures). This is constant
3242 * for a given filesystem unless the number of block groups
3243 * changes so we cache the previous value until it does.
ac27a0ec
DK
3244 */
3245
3246 /*
3247 * All of the blocks before first_data_block are
3248 * overhead
3249 */
3250 overhead = le32_to_cpu(es->s_first_data_block);
3251
3252 /*
3253 * Add the overhead attributed to the superblock and
3254 * block group descriptors. If the sparse superblocks
3255 * feature is turned on, then not all groups have this.
3256 */
3257 for (i = 0; i < ngroups; i++) {
617ba13b
MC
3258 overhead += ext4_bg_has_super(sb, i) +
3259 ext4_bg_num_gdb(sb, i);
ac27a0ec
DK
3260 cond_resched();
3261 }
3262
3263 /*
3264 * Every block group has an inode bitmap, a block
3265 * bitmap, and an inode table.
3266 */
5e70030d
BP
3267 overhead += ngroups * (2 + sbi->s_itb_per_group);
3268 sbi->s_overhead_last = overhead;
3269 smp_wmb();
6bc9feff 3270 sbi->s_blocks_last = ext4_blocks_count(es);
ac27a0ec
DK
3271 }
3272
617ba13b 3273 buf->f_type = EXT4_SUPER_MAGIC;
ac27a0ec 3274 buf->f_bsize = sb->s_blocksize;
5e70030d 3275 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
6bc6e63f
AK
3276 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3277 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
308ba3ec 3278 ext4_free_blocks_count_set(es, buf->f_bfree);
bd81d8ee
LV
3279 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3280 if (buf->f_bfree < ext4_r_blocks_count(es))
ac27a0ec
DK
3281 buf->f_bavail = 0;
3282 buf->f_files = le32_to_cpu(es->s_inodes_count);
52d9f3b4 3283 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5e70030d 3284 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
617ba13b 3285 buf->f_namelen = EXT4_NAME_LEN;
960cc398
PE
3286 fsid = le64_to_cpup((void *)es->s_uuid) ^
3287 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3288 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3289 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
ac27a0ec
DK
3290 return 0;
3291}
3292
3293/* Helper function for writing quotas on sync - we need to start transaction before quota file
3294 * is locked for write. Otherwise the are possible deadlocks:
3295 * Process 1 Process 2
617ba13b 3296 * ext4_create() quota_sync()
dab291af 3297 * jbd2_journal_start() write_dquot()
ac27a0ec 3298 * DQUOT_INIT() down(dqio_mutex)
dab291af 3299 * down(dqio_mutex) jbd2_journal_start()
ac27a0ec
DK
3300 *
3301 */
3302
3303#ifdef CONFIG_QUOTA
3304
3305static inline struct inode *dquot_to_inode(struct dquot *dquot)
3306{
3307 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3308}
3309
617ba13b 3310static int ext4_dquot_initialize(struct inode *inode, int type)
ac27a0ec
DK
3311{
3312 handle_t *handle;
3313 int ret, err;
3314
3315 /* We may create quota structure so we need to reserve enough blocks */
617ba13b 3316 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
ac27a0ec
DK
3317 if (IS_ERR(handle))
3318 return PTR_ERR(handle);
3319 ret = dquot_initialize(inode, type);
617ba13b 3320 err = ext4_journal_stop(handle);
ac27a0ec
DK
3321 if (!ret)
3322 ret = err;
3323 return ret;
3324}
3325
617ba13b 3326static int ext4_dquot_drop(struct inode *inode)
ac27a0ec
DK
3327{
3328 handle_t *handle;
3329 int ret, err;
3330
3331 /* We may delete quota structure so we need to reserve enough blocks */
617ba13b 3332 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
2887df13
JK
3333 if (IS_ERR(handle)) {
3334 /*
3335 * We call dquot_drop() anyway to at least release references
3336 * to quota structures so that umount does not hang.
3337 */
3338 dquot_drop(inode);
ac27a0ec 3339 return PTR_ERR(handle);
2887df13 3340 }
ac27a0ec 3341 ret = dquot_drop(inode);
617ba13b 3342 err = ext4_journal_stop(handle);
ac27a0ec
DK
3343 if (!ret)
3344 ret = err;
3345 return ret;
3346}
3347
617ba13b 3348static int ext4_write_dquot(struct dquot *dquot)
ac27a0ec
DK
3349{
3350 int ret, err;
3351 handle_t *handle;
3352 struct inode *inode;
3353
3354 inode = dquot_to_inode(dquot);
617ba13b
MC
3355 handle = ext4_journal_start(inode,
3356 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
3357 if (IS_ERR(handle))
3358 return PTR_ERR(handle);
3359 ret = dquot_commit(dquot);
617ba13b 3360 err = ext4_journal_stop(handle);
ac27a0ec
DK
3361 if (!ret)
3362 ret = err;
3363 return ret;
3364}
3365
617ba13b 3366static int ext4_acquire_dquot(struct dquot *dquot)
ac27a0ec
DK
3367{
3368 int ret, err;
3369 handle_t *handle;
3370
617ba13b
MC
3371 handle = ext4_journal_start(dquot_to_inode(dquot),
3372 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
3373 if (IS_ERR(handle))
3374 return PTR_ERR(handle);
3375 ret = dquot_acquire(dquot);
617ba13b 3376 err = ext4_journal_stop(handle);
ac27a0ec
DK
3377 if (!ret)
3378 ret = err;
3379 return ret;
3380}
3381
617ba13b 3382static int ext4_release_dquot(struct dquot *dquot)
ac27a0ec
DK
3383{
3384 int ret, err;
3385 handle_t *handle;
3386
617ba13b
MC
3387 handle = ext4_journal_start(dquot_to_inode(dquot),
3388 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
9c3013e9
JK
3389 if (IS_ERR(handle)) {
3390 /* Release dquot anyway to avoid endless cycle in dqput() */
3391 dquot_release(dquot);
ac27a0ec 3392 return PTR_ERR(handle);
9c3013e9 3393 }
ac27a0ec 3394 ret = dquot_release(dquot);
617ba13b 3395 err = ext4_journal_stop(handle);
ac27a0ec
DK
3396 if (!ret)
3397 ret = err;
3398 return ret;
3399}
3400
617ba13b 3401static int ext4_mark_dquot_dirty(struct dquot *dquot)
ac27a0ec 3402{
2c8be6b2 3403 /* Are we journaling quotas? */
617ba13b
MC
3404 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3405 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
ac27a0ec 3406 dquot_mark_dquot_dirty(dquot);
617ba13b 3407 return ext4_write_dquot(dquot);
ac27a0ec
DK
3408 } else {
3409 return dquot_mark_dquot_dirty(dquot);
3410 }
3411}
3412
617ba13b 3413static int ext4_write_info(struct super_block *sb, int type)
ac27a0ec
DK
3414{
3415 int ret, err;
3416 handle_t *handle;
3417
3418 /* Data block + inode block */
617ba13b 3419 handle = ext4_journal_start(sb->s_root->d_inode, 2);
ac27a0ec
DK
3420 if (IS_ERR(handle))
3421 return PTR_ERR(handle);
3422 ret = dquot_commit_info(sb, type);
617ba13b 3423 err = ext4_journal_stop(handle);
ac27a0ec
DK
3424 if (!ret)
3425 ret = err;
3426 return ret;
3427}
3428
3429/*
3430 * Turn on quotas during mount time - we need to find
3431 * the quota file and such...
3432 */
617ba13b 3433static int ext4_quota_on_mount(struct super_block *sb, int type)
ac27a0ec 3434{
617ba13b
MC
3435 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3436 EXT4_SB(sb)->s_jquota_fmt, type);
ac27a0ec
DK
3437}
3438
3439/*
3440 * Standard function to be called on quota_on
3441 */
617ba13b 3442static int ext4_quota_on(struct super_block *sb, int type, int format_id,
8264613d 3443 char *name, int remount)
ac27a0ec
DK
3444{
3445 int err;
8264613d 3446 struct path path;
ac27a0ec
DK
3447
3448 if (!test_opt(sb, QUOTA))
3449 return -EINVAL;
8264613d 3450 /* When remounting, no checks are needed and in fact, name is NULL */
0623543b 3451 if (remount)
8264613d 3452 return vfs_quota_on(sb, type, format_id, name, remount);
0623543b 3453
8264613d 3454 err = kern_path(name, LOOKUP_FOLLOW, &path);
ac27a0ec
DK
3455 if (err)
3456 return err;
0623543b 3457
ac27a0ec 3458 /* Quotafile not on the same filesystem? */
8264613d
AV
3459 if (path.mnt->mnt_sb != sb) {
3460 path_put(&path);
ac27a0ec
DK
3461 return -EXDEV;
3462 }
0623543b
JK
3463 /* Journaling quota? */
3464 if (EXT4_SB(sb)->s_qf_names[type]) {
2b2d6d01 3465 /* Quotafile not in fs root? */
8264613d 3466 if (path.dentry->d_parent != sb->s_root)
0623543b
JK
3467 printk(KERN_WARNING
3468 "EXT4-fs: Quota file not on filesystem root. "
3469 "Journaled quota will not work.\n");
2b2d6d01 3470 }
0623543b
JK
3471
3472 /*
3473 * When we journal data on quota file, we have to flush journal to see
3474 * all updates to the file when we bypass pagecache...
3475 */
0390131b
FM
3476 if (EXT4_SB(sb)->s_journal &&
3477 ext4_should_journal_data(path.dentry->d_inode)) {
0623543b
JK
3478 /*
3479 * We don't need to lock updates but journal_flush() could
3480 * otherwise be livelocked...
3481 */
3482 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
7ffe1ea8 3483 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
0623543b 3484 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
7ffe1ea8 3485 if (err) {
8264613d 3486 path_put(&path);
7ffe1ea8
HK
3487 return err;
3488 }
0623543b
JK
3489 }
3490
8264613d
AV
3491 err = vfs_quota_on_path(sb, type, format_id, &path);
3492 path_put(&path);
77e69dac 3493 return err;
ac27a0ec
DK
3494}
3495
3496/* Read data from quotafile - avoid pagecache and such because we cannot afford
3497 * acquiring the locks... As quota files are never truncated and quota code
3498 * itself serializes the operations (and noone else should touch the files)
3499 * we don't have to be afraid of races */
617ba13b 3500static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec
DK
3501 size_t len, loff_t off)
3502{
3503 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 3504 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
3505 int err = 0;
3506 int offset = off & (sb->s_blocksize - 1);
3507 int tocopy;
3508 size_t toread;
3509 struct buffer_head *bh;
3510 loff_t i_size = i_size_read(inode);
3511
3512 if (off > i_size)
3513 return 0;
3514 if (off+len > i_size)
3515 len = i_size-off;
3516 toread = len;
3517 while (toread > 0) {
3518 tocopy = sb->s_blocksize - offset < toread ?
3519 sb->s_blocksize - offset : toread;
617ba13b 3520 bh = ext4_bread(NULL, inode, blk, 0, &err);
ac27a0ec
DK
3521 if (err)
3522 return err;
3523 if (!bh) /* A hole? */
3524 memset(data, 0, tocopy);
3525 else
3526 memcpy(data, bh->b_data+offset, tocopy);
3527 brelse(bh);
3528 offset = 0;
3529 toread -= tocopy;
3530 data += tocopy;
3531 blk++;
3532 }
3533 return len;
3534}
3535
3536/* Write to quotafile (we know the transaction is already started and has
3537 * enough credits) */
617ba13b 3538static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec
DK
3539 const char *data, size_t len, loff_t off)
3540{
3541 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 3542 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
3543 int err = 0;
3544 int offset = off & (sb->s_blocksize - 1);
3545 int tocopy;
617ba13b 3546 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
ac27a0ec
DK
3547 size_t towrite = len;
3548 struct buffer_head *bh;
3549 handle_t *handle = journal_current_handle();
3550
0390131b 3551 if (EXT4_SB(sb)->s_journal && !handle) {
e5f8eab8 3552 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
9c3013e9
JK
3553 " cancelled because transaction is not started.\n",
3554 (unsigned long long)off, (unsigned long long)len);
3555 return -EIO;
3556 }
ac27a0ec
DK
3557 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3558 while (towrite > 0) {
3559 tocopy = sb->s_blocksize - offset < towrite ?
3560 sb->s_blocksize - offset : towrite;
617ba13b 3561 bh = ext4_bread(handle, inode, blk, 1, &err);
ac27a0ec
DK
3562 if (!bh)
3563 goto out;
3564 if (journal_quota) {
617ba13b 3565 err = ext4_journal_get_write_access(handle, bh);
ac27a0ec
DK
3566 if (err) {
3567 brelse(bh);
3568 goto out;
3569 }
3570 }
3571 lock_buffer(bh);
3572 memcpy(bh->b_data+offset, data, tocopy);
3573 flush_dcache_page(bh->b_page);
3574 unlock_buffer(bh);
3575 if (journal_quota)
0390131b 3576 err = ext4_handle_dirty_metadata(handle, NULL, bh);
ac27a0ec
DK
3577 else {
3578 /* Always do at least ordered writes for quotas */
678aaf48 3579 err = ext4_jbd2_file_inode(handle, inode);
ac27a0ec
DK
3580 mark_buffer_dirty(bh);
3581 }
3582 brelse(bh);
3583 if (err)
3584 goto out;
3585 offset = 0;
3586 towrite -= tocopy;
3587 data += tocopy;
3588 blk++;
3589 }
3590out:
4d04e4fb
JK
3591 if (len == towrite) {
3592 mutex_unlock(&inode->i_mutex);
ac27a0ec 3593 return err;
4d04e4fb 3594 }
ac27a0ec
DK
3595 if (inode->i_size < off+len-towrite) {
3596 i_size_write(inode, off+len-towrite);
617ba13b 3597 EXT4_I(inode)->i_disksize = inode->i_size;
ac27a0ec 3598 }
ac27a0ec 3599 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
617ba13b 3600 ext4_mark_inode_dirty(handle, inode);
ac27a0ec
DK
3601 mutex_unlock(&inode->i_mutex);
3602 return len - towrite;
3603}
3604
3605#endif
3606
617ba13b 3607static int ext4_get_sb(struct file_system_type *fs_type,
ac27a0ec
DK
3608 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3609{
617ba13b 3610 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
ac27a0ec
DK
3611}
3612
5e8814f2
TT
3613#ifdef CONFIG_PROC_FS
3614static int ext4_ui_proc_show(struct seq_file *m, void *v)
3615{
3616 unsigned int *p = m->private;
3617
3618 seq_printf(m, "%u\n", *p);
3619 return 0;
3620}
3621
3622static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3623{
3624 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3625}
3626
3627static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3628 size_t cnt, loff_t *ppos)
3629{
23475e26 3630 unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
5e8814f2 3631 char str[32];
5e8814f2
TT
3632
3633 if (cnt >= sizeof(str))
3634 return -EINVAL;
3635 if (copy_from_user(str, buf, cnt))
3636 return -EFAULT;
23475e26
RK
3637
3638 *p = simple_strtoul(str, NULL, 0);
5e8814f2
TT
3639 return cnt;
3640}
3641
3642const struct file_operations ext4_ui_proc_fops = {
3643 .owner = THIS_MODULE,
3644 .open = ext4_ui_proc_open,
3645 .read = seq_read,
3646 .llseek = seq_lseek,
3647 .release = single_release,
3648 .write = ext4_ui_proc_write,
3649};
3650#endif
3651
03010a33
TT
3652static struct file_system_type ext4_fs_type = {
3653 .owner = THIS_MODULE,
3654 .name = "ext4",
3655 .get_sb = ext4_get_sb,
3656 .kill_sb = kill_block_super,
3657 .fs_flags = FS_REQUIRES_DEV,
3658};
3659
3660#ifdef CONFIG_EXT4DEV_COMPAT
3661static int ext4dev_get_sb(struct file_system_type *fs_type,
3662 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3663{
3664 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3665 "to mount using ext4\n");
3666 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3667 "will go away by 2.6.31\n");
3668 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3669}
3670
617ba13b 3671static struct file_system_type ext4dev_fs_type = {
ac27a0ec 3672 .owner = THIS_MODULE,
617ba13b 3673 .name = "ext4dev",
03010a33 3674 .get_sb = ext4dev_get_sb,
ac27a0ec
DK
3675 .kill_sb = kill_block_super,
3676 .fs_flags = FS_REQUIRES_DEV,
3677};
03010a33
TT
3678MODULE_ALIAS("ext4dev");
3679#endif
ac27a0ec 3680
617ba13b 3681static int __init init_ext4_fs(void)
ac27a0ec 3682{
c9de560d
AT
3683 int err;
3684
9f6200bb 3685 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
c9de560d 3686 err = init_ext4_mballoc();
ac27a0ec
DK
3687 if (err)
3688 return err;
c9de560d
AT
3689
3690 err = init_ext4_xattr();
3691 if (err)
3692 goto out2;
ac27a0ec
DK
3693 err = init_inodecache();
3694 if (err)
3695 goto out1;
03010a33 3696 err = register_filesystem(&ext4_fs_type);
ac27a0ec
DK
3697 if (err)
3698 goto out;
03010a33
TT
3699#ifdef CONFIG_EXT4DEV_COMPAT
3700 err = register_filesystem(&ext4dev_fs_type);
3701 if (err) {
3702 unregister_filesystem(&ext4_fs_type);
3703 goto out;
3704 }
3705#endif
ac27a0ec
DK
3706 return 0;
3707out:
3708 destroy_inodecache();
3709out1:
617ba13b 3710 exit_ext4_xattr();
c9de560d
AT
3711out2:
3712 exit_ext4_mballoc();
ac27a0ec
DK
3713 return err;
3714}
3715
617ba13b 3716static void __exit exit_ext4_fs(void)
ac27a0ec 3717{
03010a33
TT
3718 unregister_filesystem(&ext4_fs_type);
3719#ifdef CONFIG_EXT4DEV_COMPAT
617ba13b 3720 unregister_filesystem(&ext4dev_fs_type);
03010a33 3721#endif
ac27a0ec 3722 destroy_inodecache();
617ba13b 3723 exit_ext4_xattr();
c9de560d 3724 exit_ext4_mballoc();
9f6200bb 3725 remove_proc_entry("fs/ext4", NULL);
ac27a0ec
DK
3726}
3727
3728MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
617ba13b 3729MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
ac27a0ec 3730MODULE_LICENSE("GPL");
617ba13b
MC
3731module_init(init_ext4_fs)
3732module_exit(exit_ext4_fs)