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