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