]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ext4/super.c
simplify some logic in ext4_mb_normalize_request
[net-next-2.6.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
GS
1282 case Opt_journal_checksum:
1283 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1284 break;
1285 case Opt_journal_async_commit:
1286 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1287 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1288 break;
ac27a0ec 1289 case Opt_noload:
2b2d6d01 1290 set_opt(sbi->s_mount_opt, NOLOAD);
ac27a0ec
DK
1291 break;
1292 case Opt_commit:
1293 if (match_int(&args[0], &option))
1294 return 0;
1295 if (option < 0)
1296 return 0;
1297 if (option == 0)
cd02ff0b 1298 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
ac27a0ec
DK
1299 sbi->s_commit_interval = HZ * option;
1300 break;
30773840
TT
1301 case Opt_max_batch_time:
1302 if (match_int(&args[0], &option))
1303 return 0;
1304 if (option < 0)
1305 return 0;
1306 if (option == 0)
1307 option = EXT4_DEF_MAX_BATCH_TIME;
1308 sbi->s_max_batch_time = option;
1309 break;
1310 case Opt_min_batch_time:
1311 if (match_int(&args[0], &option))
1312 return 0;
1313 if (option < 0)
1314 return 0;
1315 sbi->s_min_batch_time = option;
1316 break;
ac27a0ec 1317 case Opt_data_journal:
617ba13b 1318 data_opt = EXT4_MOUNT_JOURNAL_DATA;
ac27a0ec
DK
1319 goto datacheck;
1320 case Opt_data_ordered:
617ba13b 1321 data_opt = EXT4_MOUNT_ORDERED_DATA;
ac27a0ec
DK
1322 goto datacheck;
1323 case Opt_data_writeback:
617ba13b 1324 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
ac27a0ec
DK
1325 datacheck:
1326 if (is_remount) {
617ba13b 1327 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
ac27a0ec 1328 != data_opt) {
b31e1552
ES
1329 ext4_msg(sb, KERN_ERR,
1330 "Cannot change data mode on remount");
ac27a0ec
DK
1331 return 0;
1332 }
1333 } else {
617ba13b 1334 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
ac27a0ec
DK
1335 sbi->s_mount_opt |= data_opt;
1336 }
1337 break;
5bf5683a
HK
1338 case Opt_data_err_abort:
1339 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1340 break;
1341 case Opt_data_err_ignore:
1342 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1343 break;
f4033903
CW
1344 case Opt_mb_history_length:
1345 if (match_int(&args[0], &option))
1346 return 0;
1347 if (option < 0)
1348 return 0;
1349 sbi->s_mb_history_max = option;
1350 break;
ac27a0ec
DK
1351#ifdef CONFIG_QUOTA
1352 case Opt_usrjquota:
1353 qtype = USRQUOTA;
1354 goto set_qf_name;
1355 case Opt_grpjquota:
1356 qtype = GRPQUOTA;
1357set_qf_name:
17bd13b3 1358 if (sb_any_quota_loaded(sb) &&
dfc5d03f 1359 !sbi->s_qf_names[qtype]) {
b31e1552
ES
1360 ext4_msg(sb, KERN_ERR,
1361 "Cannot change journaled "
1362 "quota options when quota turned on");
ac27a0ec
DK
1363 return 0;
1364 }
1365 qname = match_strdup(&args[0]);
1366 if (!qname) {
b31e1552
ES
1367 ext4_msg(sb, KERN_ERR,
1368 "Not enough memory for "
1369 "storing quotafile name");
ac27a0ec
DK
1370 return 0;
1371 }
1372 if (sbi->s_qf_names[qtype] &&
1373 strcmp(sbi->s_qf_names[qtype], qname)) {
b31e1552
ES
1374 ext4_msg(sb, KERN_ERR,
1375 "%s quota file already "
1376 "specified", QTYPE2NAME(qtype));
ac27a0ec
DK
1377 kfree(qname);
1378 return 0;
1379 }
1380 sbi->s_qf_names[qtype] = qname;
1381 if (strchr(sbi->s_qf_names[qtype], '/')) {
b31e1552
ES
1382 ext4_msg(sb, KERN_ERR,
1383 "quotafile must be on "
1384 "filesystem root");
ac27a0ec
DK
1385 kfree(sbi->s_qf_names[qtype]);
1386 sbi->s_qf_names[qtype] = NULL;
1387 return 0;
1388 }
1389 set_opt(sbi->s_mount_opt, QUOTA);
1390 break;
1391 case Opt_offusrjquota:
1392 qtype = USRQUOTA;
1393 goto clear_qf_name;
1394 case Opt_offgrpjquota:
1395 qtype = GRPQUOTA;
1396clear_qf_name:
17bd13b3 1397 if (sb_any_quota_loaded(sb) &&
dfc5d03f 1398 sbi->s_qf_names[qtype]) {
b31e1552 1399 ext4_msg(sb, KERN_ERR, "Cannot change "
2c8be6b2 1400 "journaled quota options when "
b31e1552 1401 "quota turned on");
ac27a0ec
DK
1402 return 0;
1403 }
1404 /*
1405 * The space will be released later when all options
1406 * are confirmed to be correct
1407 */
1408 sbi->s_qf_names[qtype] = NULL;
1409 break;
1410 case Opt_jqfmt_vfsold:
dfc5d03f
JK
1411 qfmt = QFMT_VFS_OLD;
1412 goto set_qf_format;
ac27a0ec 1413 case Opt_jqfmt_vfsv0:
dfc5d03f
JK
1414 qfmt = QFMT_VFS_V0;
1415set_qf_format:
17bd13b3 1416 if (sb_any_quota_loaded(sb) &&
dfc5d03f 1417 sbi->s_jquota_fmt != qfmt) {
b31e1552 1418 ext4_msg(sb, KERN_ERR, "Cannot change "
dfc5d03f 1419 "journaled quota options when "
b31e1552 1420 "quota turned on");
dfc5d03f
JK
1421 return 0;
1422 }
1423 sbi->s_jquota_fmt = qfmt;
ac27a0ec
DK
1424 break;
1425 case Opt_quota:
1426 case Opt_usrquota:
1427 set_opt(sbi->s_mount_opt, QUOTA);
1428 set_opt(sbi->s_mount_opt, USRQUOTA);
1429 break;
1430 case Opt_grpquota:
1431 set_opt(sbi->s_mount_opt, QUOTA);
1432 set_opt(sbi->s_mount_opt, GRPQUOTA);
1433 break;
1434 case Opt_noquota:
17bd13b3 1435 if (sb_any_quota_loaded(sb)) {
b31e1552
ES
1436 ext4_msg(sb, KERN_ERR, "Cannot change quota "
1437 "options when quota turned on");
ac27a0ec
DK
1438 return 0;
1439 }
1440 clear_opt(sbi->s_mount_opt, QUOTA);
1441 clear_opt(sbi->s_mount_opt, USRQUOTA);
1442 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1443 break;
1444#else
1445 case Opt_quota:
1446 case Opt_usrquota:
1447 case Opt_grpquota:
b31e1552
ES
1448 ext4_msg(sb, KERN_ERR,
1449 "quota options not supported");
cd59e7b9 1450 break;
ac27a0ec
DK
1451 case Opt_usrjquota:
1452 case Opt_grpjquota:
1453 case Opt_offusrjquota:
1454 case Opt_offgrpjquota:
1455 case Opt_jqfmt_vfsold:
1456 case Opt_jqfmt_vfsv0:
b31e1552
ES
1457 ext4_msg(sb, KERN_ERR,
1458 "journaled quota options not supported");
ac27a0ec
DK
1459 break;
1460 case Opt_noquota:
1461 break;
1462#endif
1463 case Opt_abort:
4ab2f15b 1464 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
ac27a0ec 1465 break;
06705bff
TT
1466 case Opt_nobarrier:
1467 clear_opt(sbi->s_mount_opt, BARRIER);
1468 break;
ac27a0ec 1469 case Opt_barrier:
06705bff
TT
1470 if (match_int(&args[0], &option)) {
1471 set_opt(sbi->s_mount_opt, BARRIER);
1472 break;
1473 }
ac27a0ec
DK
1474 if (option)
1475 set_opt(sbi->s_mount_opt, BARRIER);
1476 else
1477 clear_opt(sbi->s_mount_opt, BARRIER);
1478 break;
1479 case Opt_ignore:
1480 break;
1481 case Opt_resize:
1482 if (!is_remount) {
b31e1552
ES
1483 ext4_msg(sb, KERN_ERR,
1484 "resize option only available "
1485 "for remount");
ac27a0ec
DK
1486 return 0;
1487 }
1488 if (match_int(&args[0], &option) != 0)
1489 return 0;
1490 *n_blocks_count = option;
1491 break;
1492 case Opt_nobh:
1493 set_opt(sbi->s_mount_opt, NOBH);
1494 break;
1495 case Opt_bh:
1496 clear_opt(sbi->s_mount_opt, NOBH);
1497 break;
25ec56b5
JNC
1498 case Opt_i_version:
1499 set_opt(sbi->s_mount_opt, I_VERSION);
1500 sb->s_flags |= MS_I_VERSION;
1501 break;
dd919b98
AK
1502 case Opt_nodelalloc:
1503 clear_opt(sbi->s_mount_opt, DELALLOC);
1504 break;
c9de560d
AT
1505 case Opt_stripe:
1506 if (match_int(&args[0], &option))
1507 return 0;
1508 if (option < 0)
1509 return 0;
1510 sbi->s_stripe = option;
1511 break;
64769240
AT
1512 case Opt_delalloc:
1513 set_opt(sbi->s_mount_opt, DELALLOC);
1514 break;
6fd058f7
TT
1515 case Opt_block_validity:
1516 set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1517 break;
1518 case Opt_noblock_validity:
1519 clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1520 break;
240799cd
TT
1521 case Opt_inode_readahead_blks:
1522 if (match_int(&args[0], &option))
1523 return 0;
1524 if (option < 0 || option > (1 << 30))
1525 return 0;
f7c43950 1526 if (!is_power_of_2(option)) {
b31e1552
ES
1527 ext4_msg(sb, KERN_ERR,
1528 "EXT4-fs: inode_readahead_blks"
1529 " must be a power of 2");
3197ebdb
TT
1530 return 0;
1531 }
240799cd
TT
1532 sbi->s_inode_readahead_blks = option;
1533 break;
b3881f74
TT
1534 case Opt_journal_ioprio:
1535 if (match_int(&args[0], &option))
1536 return 0;
1537 if (option < 0 || option > 7)
1538 break;
1539 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1540 option);
1541 break;
06705bff
TT
1542 case Opt_noauto_da_alloc:
1543 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1544 break;
afd4672d 1545 case Opt_auto_da_alloc:
06705bff
TT
1546 if (match_int(&args[0], &option)) {
1547 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1548 break;
1549 }
afd4672d
TT
1550 if (option)
1551 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1552 else
1553 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1554 break;
ac27a0ec 1555 default:
b31e1552
ES
1556 ext4_msg(sb, KERN_ERR,
1557 "Unrecognized mount option \"%s\" "
1558 "or missing value", p);
ac27a0ec
DK
1559 return 0;
1560 }
1561 }
1562#ifdef CONFIG_QUOTA
1563 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
617ba13b 1564 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
ac27a0ec
DK
1565 sbi->s_qf_names[USRQUOTA])
1566 clear_opt(sbi->s_mount_opt, USRQUOTA);
1567
617ba13b 1568 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
ac27a0ec
DK
1569 sbi->s_qf_names[GRPQUOTA])
1570 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1571
1572 if ((sbi->s_qf_names[USRQUOTA] &&
617ba13b 1573 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
ac27a0ec 1574 (sbi->s_qf_names[GRPQUOTA] &&
617ba13b 1575 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
b31e1552
ES
1576 ext4_msg(sb, KERN_ERR, "old and new quota "
1577 "format mixing");
ac27a0ec
DK
1578 return 0;
1579 }
1580
1581 if (!sbi->s_jquota_fmt) {
b31e1552
ES
1582 ext4_msg(sb, KERN_ERR, "journaled quota format "
1583 "not specified");
ac27a0ec
DK
1584 return 0;
1585 }
1586 } else {
1587 if (sbi->s_jquota_fmt) {
b31e1552 1588 ext4_msg(sb, KERN_ERR, "journaled quota format "
2c8be6b2 1589 "specified with no journaling "
b31e1552 1590 "enabled");
ac27a0ec
DK
1591 return 0;
1592 }
1593 }
1594#endif
1595 return 1;
1596}
1597
617ba13b 1598static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
ac27a0ec
DK
1599 int read_only)
1600{
617ba13b 1601 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
1602 int res = 0;
1603
617ba13b 1604 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
b31e1552
ES
1605 ext4_msg(sb, KERN_ERR, "revision level too high, "
1606 "forcing read-only mode");
ac27a0ec
DK
1607 res = MS_RDONLY;
1608 }
1609 if (read_only)
1610 return res;
617ba13b 1611 if (!(sbi->s_mount_state & EXT4_VALID_FS))
b31e1552
ES
1612 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1613 "running e2fsck is recommended");
617ba13b 1614 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
b31e1552
ES
1615 ext4_msg(sb, KERN_WARNING,
1616 "warning: mounting fs with errors, "
1617 "running e2fsck is recommended");
ac27a0ec
DK
1618 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1619 le16_to_cpu(es->s_mnt_count) >=
1620 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
b31e1552
ES
1621 ext4_msg(sb, KERN_WARNING,
1622 "warning: maximal mount count reached, "
1623 "running e2fsck is recommended");
ac27a0ec
DK
1624 else if (le32_to_cpu(es->s_checkinterval) &&
1625 (le32_to_cpu(es->s_lastcheck) +
1626 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
b31e1552
ES
1627 ext4_msg(sb, KERN_WARNING,
1628 "warning: checktime reached, "
1629 "running e2fsck is recommended");
0b8e58a1 1630 if (!sbi->s_journal)
0390131b 1631 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
ac27a0ec 1632 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
617ba13b 1633 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
e8546d06 1634 le16_add_cpu(&es->s_mnt_count, 1);
ac27a0ec 1635 es->s_mtime = cpu_to_le32(get_seconds());
617ba13b 1636 ext4_update_dynamic_rev(sb);
0390131b
FM
1637 if (sbi->s_journal)
1638 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 1639
e2d67052 1640 ext4_commit_super(sb, 1);
ac27a0ec 1641 if (test_opt(sb, DEBUG))
a9df9a49 1642 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
7f4520cc 1643 "bpg=%lu, ipg=%lu, mo=%04x]\n",
ac27a0ec
DK
1644 sb->s_blocksize,
1645 sbi->s_groups_count,
617ba13b
MC
1646 EXT4_BLOCKS_PER_GROUP(sb),
1647 EXT4_INODES_PER_GROUP(sb),
ac27a0ec
DK
1648 sbi->s_mount_opt);
1649
0390131b 1650 if (EXT4_SB(sb)->s_journal) {
b31e1552
ES
1651 ext4_msg(sb, KERN_INFO, "%s journal on %s",
1652 EXT4_SB(sb)->s_journal->j_inode ? "internal" :
0390131b
FM
1653 "external", EXT4_SB(sb)->s_journal->j_devname);
1654 } else {
b31e1552 1655 ext4_msg(sb, KERN_INFO, "no journal");
0390131b 1656 }
ac27a0ec
DK
1657 return res;
1658}
1659
772cb7c8
JS
1660static int ext4_fill_flex_info(struct super_block *sb)
1661{
1662 struct ext4_sb_info *sbi = EXT4_SB(sb);
1663 struct ext4_group_desc *gdp = NULL;
772cb7c8
JS
1664 ext4_group_t flex_group_count;
1665 ext4_group_t flex_group;
1666 int groups_per_flex = 0;
c5ca7c76 1667 size_t size;
772cb7c8
JS
1668 int i;
1669
1670 if (!sbi->s_es->s_log_groups_per_flex) {
1671 sbi->s_log_groups_per_flex = 0;
1672 return 1;
1673 }
1674
1675 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1676 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1677
c62a11fd
FB
1678 /* We allocate both existing and potentially added groups */
1679 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
d94e99a6
AK
1680 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1681 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
c5ca7c76
TT
1682 size = flex_group_count * sizeof(struct flex_groups);
1683 sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1684 if (sbi->s_flex_groups == NULL) {
1685 sbi->s_flex_groups = vmalloc(size);
1686 if (sbi->s_flex_groups)
1687 memset(sbi->s_flex_groups, 0, size);
1688 }
772cb7c8 1689 if (sbi->s_flex_groups == NULL) {
b31e1552
ES
1690 ext4_msg(sb, KERN_ERR, "not enough memory for "
1691 "%u flex groups", flex_group_count);
772cb7c8
JS
1692 goto failed;
1693 }
772cb7c8 1694
772cb7c8 1695 for (i = 0; i < sbi->s_groups_count; i++) {
88b6edd1 1696 gdp = ext4_get_group_desc(sb, i, NULL);
772cb7c8
JS
1697
1698 flex_group = ext4_flex_group(sbi, i);
9f24e420
TT
1699 atomic_set(&sbi->s_flex_groups[flex_group].free_inodes,
1700 ext4_free_inodes_count(sb, gdp));
1701 atomic_set(&sbi->s_flex_groups[flex_group].free_blocks,
1702 ext4_free_blks_count(sb, gdp));
7d39db14
TT
1703 atomic_set(&sbi->s_flex_groups[flex_group].used_dirs,
1704 ext4_used_dirs_count(sb, gdp));
772cb7c8
JS
1705 }
1706
1707 return 1;
1708failed:
1709 return 0;
1710}
1711
717d50e4
AD
1712__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1713 struct ext4_group_desc *gdp)
1714{
1715 __u16 crc = 0;
1716
1717 if (sbi->s_es->s_feature_ro_compat &
1718 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1719 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1720 __le32 le_group = cpu_to_le32(block_group);
1721
1722 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1723 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1724 crc = crc16(crc, (__u8 *)gdp, offset);
1725 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1726 /* for checksum of struct ext4_group_desc do the rest...*/
1727 if ((sbi->s_es->s_feature_incompat &
1728 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1729 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1730 crc = crc16(crc, (__u8 *)gdp + offset,
1731 le16_to_cpu(sbi->s_es->s_desc_size) -
1732 offset);
1733 }
1734
1735 return cpu_to_le16(crc);
1736}
1737
1738int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1739 struct ext4_group_desc *gdp)
1740{
1741 if ((sbi->s_es->s_feature_ro_compat &
1742 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1743 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1744 return 0;
1745
1746 return 1;
1747}
1748
ac27a0ec 1749/* Called at mount-time, super-block is locked */
197cd65a 1750static int ext4_check_descriptors(struct super_block *sb)
ac27a0ec 1751{
617ba13b
MC
1752 struct ext4_sb_info *sbi = EXT4_SB(sb);
1753 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1754 ext4_fsblk_t last_block;
bd81d8ee
LV
1755 ext4_fsblk_t block_bitmap;
1756 ext4_fsblk_t inode_bitmap;
1757 ext4_fsblk_t inode_table;
ce421581 1758 int flexbg_flag = 0;
fd2d4291 1759 ext4_group_t i;
ac27a0ec 1760
ce421581
JS
1761 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1762 flexbg_flag = 1;
1763
af5bc92d 1764 ext4_debug("Checking group descriptors");
ac27a0ec 1765
197cd65a
AM
1766 for (i = 0; i < sbi->s_groups_count; i++) {
1767 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1768
ce421581 1769 if (i == sbi->s_groups_count - 1 || flexbg_flag)
bd81d8ee 1770 last_block = ext4_blocks_count(sbi->s_es) - 1;
ac27a0ec
DK
1771 else
1772 last_block = first_block +
617ba13b 1773 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
ac27a0ec 1774
8fadc143 1775 block_bitmap = ext4_block_bitmap(sb, gdp);
2b2d6d01 1776 if (block_bitmap < first_block || block_bitmap > last_block) {
b31e1552 1777 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 1778 "Block bitmap for group %u not in group "
b31e1552 1779 "(block %llu)!", i, block_bitmap);
ac27a0ec
DK
1780 return 0;
1781 }
8fadc143 1782 inode_bitmap = ext4_inode_bitmap(sb, gdp);
2b2d6d01 1783 if (inode_bitmap < first_block || inode_bitmap > last_block) {
b31e1552 1784 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 1785 "Inode bitmap for group %u not in group "
b31e1552 1786 "(block %llu)!", i, inode_bitmap);
ac27a0ec
DK
1787 return 0;
1788 }
8fadc143 1789 inode_table = ext4_inode_table(sb, gdp);
bd81d8ee 1790 if (inode_table < first_block ||
2b2d6d01 1791 inode_table + sbi->s_itb_per_group - 1 > last_block) {
b31e1552 1792 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 1793 "Inode table for group %u not in group "
b31e1552 1794 "(block %llu)!", i, inode_table);
ac27a0ec
DK
1795 return 0;
1796 }
955ce5f5 1797 ext4_lock_group(sb, i);
717d50e4 1798 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
b31e1552
ES
1799 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1800 "Checksum for group %u failed (%u!=%u)",
1801 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1802 gdp)), le16_to_cpu(gdp->bg_checksum));
7ee1ec4c 1803 if (!(sb->s_flags & MS_RDONLY)) {
955ce5f5 1804 ext4_unlock_group(sb, i);
8a266467 1805 return 0;
7ee1ec4c 1806 }
717d50e4 1807 }
955ce5f5 1808 ext4_unlock_group(sb, i);
ce421581
JS
1809 if (!flexbg_flag)
1810 first_block += EXT4_BLOCKS_PER_GROUP(sb);
ac27a0ec
DK
1811 }
1812
bd81d8ee 1813 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
0b8e58a1 1814 sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
ac27a0ec
DK
1815 return 1;
1816}
1817
617ba13b 1818/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
ac27a0ec
DK
1819 * the superblock) which were deleted from all directories, but held open by
1820 * a process at the time of a crash. We walk the list and try to delete these
1821 * inodes at recovery time (only with a read-write filesystem).
1822 *
1823 * In order to keep the orphan inode chain consistent during traversal (in
1824 * case of crash during recovery), we link each inode into the superblock
1825 * orphan list_head and handle it the same way as an inode deletion during
1826 * normal operation (which journals the operations for us).
1827 *
1828 * We only do an iget() and an iput() on each inode, which is very safe if we
1829 * accidentally point at an in-use or already deleted inode. The worst that
1830 * can happen in this case is that we get a "bit already cleared" message from
617ba13b 1831 * ext4_free_inode(). The only reason we would point at a wrong inode is if
ac27a0ec
DK
1832 * e2fsck was run on this filesystem, and it must have already done the orphan
1833 * inode cleanup for us, so we can safely abort without any further action.
1834 */
2b2d6d01
TT
1835static void ext4_orphan_cleanup(struct super_block *sb,
1836 struct ext4_super_block *es)
ac27a0ec
DK
1837{
1838 unsigned int s_flags = sb->s_flags;
1839 int nr_orphans = 0, nr_truncates = 0;
1840#ifdef CONFIG_QUOTA
1841 int i;
1842#endif
1843 if (!es->s_last_orphan) {
1844 jbd_debug(4, "no orphan inodes to clean up\n");
1845 return;
1846 }
1847
a8f48a95 1848 if (bdev_read_only(sb->s_bdev)) {
b31e1552
ES
1849 ext4_msg(sb, KERN_ERR, "write access "
1850 "unavailable, skipping orphan cleanup");
a8f48a95
ES
1851 return;
1852 }
1853
617ba13b 1854 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
ac27a0ec
DK
1855 if (es->s_last_orphan)
1856 jbd_debug(1, "Errors on filesystem, "
1857 "clearing orphan list.\n");
1858 es->s_last_orphan = 0;
1859 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1860 return;
1861 }
1862
1863 if (s_flags & MS_RDONLY) {
b31e1552 1864 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
ac27a0ec
DK
1865 sb->s_flags &= ~MS_RDONLY;
1866 }
1867#ifdef CONFIG_QUOTA
1868 /* Needed for iput() to work correctly and not trash data */
1869 sb->s_flags |= MS_ACTIVE;
1870 /* Turn on quotas so that they are updated correctly */
1871 for (i = 0; i < MAXQUOTAS; i++) {
617ba13b
MC
1872 if (EXT4_SB(sb)->s_qf_names[i]) {
1873 int ret = ext4_quota_on_mount(sb, i);
ac27a0ec 1874 if (ret < 0)
b31e1552
ES
1875 ext4_msg(sb, KERN_ERR,
1876 "Cannot turn on journaled "
1877 "quota: error %d", ret);
ac27a0ec
DK
1878 }
1879 }
1880#endif
1881
1882 while (es->s_last_orphan) {
1883 struct inode *inode;
1884
97bd42b9
JB
1885 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1886 if (IS_ERR(inode)) {
ac27a0ec
DK
1887 es->s_last_orphan = 0;
1888 break;
1889 }
1890
617ba13b 1891 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
a269eb18 1892 vfs_dq_init(inode);
ac27a0ec 1893 if (inode->i_nlink) {
b31e1552
ES
1894 ext4_msg(sb, KERN_DEBUG,
1895 "%s: truncating inode %lu to %lld bytes",
46e665e9 1896 __func__, inode->i_ino, inode->i_size);
e5f8eab8 1897 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
ac27a0ec 1898 inode->i_ino, inode->i_size);
617ba13b 1899 ext4_truncate(inode);
ac27a0ec
DK
1900 nr_truncates++;
1901 } else {
b31e1552
ES
1902 ext4_msg(sb, KERN_DEBUG,
1903 "%s: deleting unreferenced inode %lu",
46e665e9 1904 __func__, inode->i_ino);
ac27a0ec
DK
1905 jbd_debug(2, "deleting unreferenced inode %lu\n",
1906 inode->i_ino);
1907 nr_orphans++;
1908 }
1909 iput(inode); /* The delete magic happens here! */
1910 }
1911
2b2d6d01 1912#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
ac27a0ec
DK
1913
1914 if (nr_orphans)
b31e1552
ES
1915 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
1916 PLURAL(nr_orphans));
ac27a0ec 1917 if (nr_truncates)
b31e1552
ES
1918 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
1919 PLURAL(nr_truncates));
ac27a0ec
DK
1920#ifdef CONFIG_QUOTA
1921 /* Turn quotas off */
1922 for (i = 0; i < MAXQUOTAS; i++) {
1923 if (sb_dqopt(sb)->files[i])
6f28e087 1924 vfs_quota_off(sb, i, 0);
ac27a0ec
DK
1925 }
1926#endif
1927 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1928}
0b8e58a1 1929
cd2291a4
ES
1930/*
1931 * Maximal extent format file size.
1932 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1933 * extent format containers, within a sector_t, and within i_blocks
1934 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1935 * so that won't be a limiting factor.
1936 *
1937 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1938 */
f287a1a5 1939static loff_t ext4_max_size(int blkbits, int has_huge_files)
cd2291a4
ES
1940{
1941 loff_t res;
1942 loff_t upper_limit = MAX_LFS_FILESIZE;
1943
1944 /* small i_blocks in vfs inode? */
f287a1a5 1945 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
cd2291a4 1946 /*
90c699a9 1947 * CONFIG_LBDAF is not enabled implies the inode
cd2291a4
ES
1948 * i_block represent total blocks in 512 bytes
1949 * 32 == size of vfs inode i_blocks * 8
1950 */
1951 upper_limit = (1LL << 32) - 1;
1952
1953 /* total blocks in file system block size */
1954 upper_limit >>= (blkbits - 9);
1955 upper_limit <<= blkbits;
1956 }
1957
1958 /* 32-bit extent-start container, ee_block */
1959 res = 1LL << 32;
1960 res <<= blkbits;
1961 res -= 1;
1962
1963 /* Sanity check against vm- & vfs- imposed limits */
1964 if (res > upper_limit)
1965 res = upper_limit;
1966
1967 return res;
1968}
ac27a0ec 1969
ac27a0ec 1970/*
cd2291a4 1971 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
0fc1b451
AK
1972 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1973 * We need to be 1 filesystem block less than the 2^48 sector limit.
ac27a0ec 1974 */
f287a1a5 1975static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
ac27a0ec 1976{
617ba13b 1977 loff_t res = EXT4_NDIR_BLOCKS;
0fc1b451
AK
1978 int meta_blocks;
1979 loff_t upper_limit;
0b8e58a1
AD
1980 /* This is calculated to be the largest file size for a dense, block
1981 * mapped file such that the file's total number of 512-byte sectors,
1982 * including data and all indirect blocks, does not exceed (2^48 - 1).
1983 *
1984 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
1985 * number of 512-byte sectors of the file.
0fc1b451
AK
1986 */
1987
f287a1a5 1988 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
0fc1b451 1989 /*
90c699a9 1990 * !has_huge_files or CONFIG_LBDAF not enabled implies that
0b8e58a1
AD
1991 * the inode i_block field represents total file blocks in
1992 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
0fc1b451
AK
1993 */
1994 upper_limit = (1LL << 32) - 1;
1995
1996 /* total blocks in file system block size */
1997 upper_limit >>= (bits - 9);
1998
1999 } else {
8180a562
AK
2000 /*
2001 * We use 48 bit ext4_inode i_blocks
2002 * With EXT4_HUGE_FILE_FL set the i_blocks
2003 * represent total number of blocks in
2004 * file system block size
2005 */
0fc1b451
AK
2006 upper_limit = (1LL << 48) - 1;
2007
0fc1b451
AK
2008 }
2009
2010 /* indirect blocks */
2011 meta_blocks = 1;
2012 /* double indirect blocks */
2013 meta_blocks += 1 + (1LL << (bits-2));
2014 /* tripple indirect blocks */
2015 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2016
2017 upper_limit -= meta_blocks;
2018 upper_limit <<= bits;
ac27a0ec
DK
2019
2020 res += 1LL << (bits-2);
2021 res += 1LL << (2*(bits-2));
2022 res += 1LL << (3*(bits-2));
2023 res <<= bits;
2024 if (res > upper_limit)
2025 res = upper_limit;
0fc1b451
AK
2026
2027 if (res > MAX_LFS_FILESIZE)
2028 res = MAX_LFS_FILESIZE;
2029
ac27a0ec
DK
2030 return res;
2031}
2032
617ba13b 2033static ext4_fsblk_t descriptor_loc(struct super_block *sb,
0b8e58a1 2034 ext4_fsblk_t logical_sb_block, int nr)
ac27a0ec 2035{
617ba13b 2036 struct ext4_sb_info *sbi = EXT4_SB(sb);
fd2d4291 2037 ext4_group_t bg, first_meta_bg;
ac27a0ec
DK
2038 int has_super = 0;
2039
2040 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2041
617ba13b 2042 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
ac27a0ec 2043 nr < first_meta_bg)
70bbb3e0 2044 return logical_sb_block + nr + 1;
ac27a0ec 2045 bg = sbi->s_desc_per_block * nr;
617ba13b 2046 if (ext4_bg_has_super(sb, bg))
ac27a0ec 2047 has_super = 1;
0b8e58a1 2048
617ba13b 2049 return (has_super + ext4_group_first_block_no(sb, bg));
ac27a0ec
DK
2050}
2051
c9de560d
AT
2052/**
2053 * ext4_get_stripe_size: Get the stripe size.
2054 * @sbi: In memory super block info
2055 *
2056 * If we have specified it via mount option, then
2057 * use the mount option value. If the value specified at mount time is
2058 * greater than the blocks per group use the super block value.
2059 * If the super block value is greater than blocks per group return 0.
2060 * Allocator needs it be less than blocks per group.
2061 *
2062 */
2063static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2064{
2065 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2066 unsigned long stripe_width =
2067 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2068
2069 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2070 return sbi->s_stripe;
2071
2072 if (stripe_width <= sbi->s_blocks_per_group)
2073 return stripe_width;
2074
2075 if (stride <= sbi->s_blocks_per_group)
2076 return stride;
2077
2078 return 0;
2079}
ac27a0ec 2080
3197ebdb
TT
2081/* sysfs supprt */
2082
2083struct ext4_attr {
2084 struct attribute attr;
2085 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
2086 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
2087 const char *, size_t);
2088 int offset;
2089};
2090
2091static int parse_strtoul(const char *buf,
2092 unsigned long max, unsigned long *value)
2093{
2094 char *endp;
2095
2096 while (*buf && isspace(*buf))
2097 buf++;
2098 *value = simple_strtoul(buf, &endp, 0);
2099 while (*endp && isspace(*endp))
2100 endp++;
2101 if (*endp || *value > max)
2102 return -EINVAL;
2103
2104 return 0;
2105}
2106
2107static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2108 struct ext4_sb_info *sbi,
2109 char *buf)
2110{
2111 return snprintf(buf, PAGE_SIZE, "%llu\n",
2112 (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2113}
2114
2115static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2116 struct ext4_sb_info *sbi, char *buf)
2117{
2118 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2119
2120 return snprintf(buf, PAGE_SIZE, "%lu\n",
2121 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2122 sbi->s_sectors_written_start) >> 1);
2123}
2124
2125static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2126 struct ext4_sb_info *sbi, char *buf)
2127{
2128 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2129
2130 return snprintf(buf, PAGE_SIZE, "%llu\n",
2131 sbi->s_kbytes_written +
2132 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2133 EXT4_SB(sb)->s_sectors_written_start) >> 1));
2134}
2135
2136static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2137 struct ext4_sb_info *sbi,
2138 const char *buf, size_t count)
2139{
2140 unsigned long t;
2141
2142 if (parse_strtoul(buf, 0x40000000, &t))
2143 return -EINVAL;
2144
f7c43950 2145 if (!is_power_of_2(t))
3197ebdb
TT
2146 return -EINVAL;
2147
2148 sbi->s_inode_readahead_blks = t;
2149 return count;
2150}
2151
2152static ssize_t sbi_ui_show(struct ext4_attr *a,
0b8e58a1 2153 struct ext4_sb_info *sbi, char *buf)
3197ebdb
TT
2154{
2155 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2156
2157 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2158}
2159
2160static ssize_t sbi_ui_store(struct ext4_attr *a,
2161 struct ext4_sb_info *sbi,
2162 const char *buf, size_t count)
2163{
2164 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2165 unsigned long t;
2166
2167 if (parse_strtoul(buf, 0xffffffff, &t))
2168 return -EINVAL;
2169 *ui = t;
2170 return count;
2171}
2172
2173#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2174static struct ext4_attr ext4_attr_##_name = { \
2175 .attr = {.name = __stringify(_name), .mode = _mode }, \
2176 .show = _show, \
2177 .store = _store, \
2178 .offset = offsetof(struct ext4_sb_info, _elname), \
2179}
2180#define EXT4_ATTR(name, mode, show, store) \
2181static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2182
2183#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2184#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2185#define EXT4_RW_ATTR_SBI_UI(name, elname) \
2186 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2187#define ATTR_LIST(name) &ext4_attr_##name.attr
2188
2189EXT4_RO_ATTR(delayed_allocation_blocks);
2190EXT4_RO_ATTR(session_write_kbytes);
2191EXT4_RO_ATTR(lifetime_write_kbytes);
2192EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2193 inode_readahead_blks_store, s_inode_readahead_blks);
11013911 2194EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
3197ebdb
TT
2195EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2196EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2197EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2198EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2199EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2200EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2201
2202static struct attribute *ext4_attrs[] = {
2203 ATTR_LIST(delayed_allocation_blocks),
2204 ATTR_LIST(session_write_kbytes),
2205 ATTR_LIST(lifetime_write_kbytes),
2206 ATTR_LIST(inode_readahead_blks),
11013911 2207 ATTR_LIST(inode_goal),
3197ebdb
TT
2208 ATTR_LIST(mb_stats),
2209 ATTR_LIST(mb_max_to_scan),
2210 ATTR_LIST(mb_min_to_scan),
2211 ATTR_LIST(mb_order2_req),
2212 ATTR_LIST(mb_stream_req),
2213 ATTR_LIST(mb_group_prealloc),
2214 NULL,
2215};
2216
2217static ssize_t ext4_attr_show(struct kobject *kobj,
2218 struct attribute *attr, char *buf)
2219{
2220 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2221 s_kobj);
2222 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2223
2224 return a->show ? a->show(a, sbi, buf) : 0;
2225}
2226
2227static ssize_t ext4_attr_store(struct kobject *kobj,
2228 struct attribute *attr,
2229 const char *buf, size_t len)
2230{
2231 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2232 s_kobj);
2233 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2234
2235 return a->store ? a->store(a, sbi, buf, len) : 0;
2236}
2237
2238static void ext4_sb_release(struct kobject *kobj)
2239{
2240 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2241 s_kobj);
2242 complete(&sbi->s_kobj_unregister);
2243}
2244
2245
2246static struct sysfs_ops ext4_attr_ops = {
2247 .show = ext4_attr_show,
2248 .store = ext4_attr_store,
2249};
2250
2251static struct kobj_type ext4_ktype = {
2252 .default_attrs = ext4_attrs,
2253 .sysfs_ops = &ext4_attr_ops,
2254 .release = ext4_sb_release,
2255};
2256
2b2d6d01 2257static int ext4_fill_super(struct super_block *sb, void *data, int silent)
7477827f
AK
2258 __releases(kernel_lock)
2259 __acquires(kernel_lock)
ac27a0ec 2260{
2b2d6d01 2261 struct buffer_head *bh;
617ba13b
MC
2262 struct ext4_super_block *es = NULL;
2263 struct ext4_sb_info *sbi;
2264 ext4_fsblk_t block;
2265 ext4_fsblk_t sb_block = get_sb_block(&data);
70bbb3e0 2266 ext4_fsblk_t logical_sb_block;
ac27a0ec 2267 unsigned long offset = 0;
ac27a0ec
DK
2268 unsigned long journal_devnum = 0;
2269 unsigned long def_mount_opts;
2270 struct inode *root;
9f6200bb 2271 char *cp;
0390131b 2272 const char *descr;
1d1fe1ee 2273 int ret = -EINVAL;
ac27a0ec 2274 int blocksize;
4ec11028
TT
2275 unsigned int db_count;
2276 unsigned int i;
f287a1a5 2277 int needs_recovery, has_huge_files;
3a06d778 2278 int features;
bd81d8ee 2279 __u64 blocks_count;
833f4077 2280 int err;
b3881f74 2281 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
ac27a0ec
DK
2282
2283 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2284 if (!sbi)
2285 return -ENOMEM;
705895b6
PE
2286
2287 sbi->s_blockgroup_lock =
2288 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2289 if (!sbi->s_blockgroup_lock) {
2290 kfree(sbi);
2291 return -ENOMEM;
2292 }
ac27a0ec
DK
2293 sb->s_fs_info = sbi;
2294 sbi->s_mount_opt = 0;
617ba13b
MC
2295 sbi->s_resuid = EXT4_DEF_RESUID;
2296 sbi->s_resgid = EXT4_DEF_RESGID;
240799cd 2297 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
d9c9bef1 2298 sbi->s_sb_block = sb_block;
afc32f7e
TT
2299 sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2300 sectors[1]);
ac27a0ec
DK
2301
2302 unlock_kernel();
2303
9f6200bb
TT
2304 /* Cleanup superblock name */
2305 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2306 *cp = '!';
2307
617ba13b 2308 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
ac27a0ec 2309 if (!blocksize) {
b31e1552 2310 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
ac27a0ec
DK
2311 goto out_fail;
2312 }
2313
2314 /*
617ba13b 2315 * The ext4 superblock will not be buffer aligned for other than 1kB
ac27a0ec
DK
2316 * block sizes. We need to calculate the offset from buffer start.
2317 */
617ba13b 2318 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
70bbb3e0
AM
2319 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2320 offset = do_div(logical_sb_block, blocksize);
ac27a0ec 2321 } else {
70bbb3e0 2322 logical_sb_block = sb_block;
ac27a0ec
DK
2323 }
2324
70bbb3e0 2325 if (!(bh = sb_bread(sb, logical_sb_block))) {
b31e1552 2326 ext4_msg(sb, KERN_ERR, "unable to read superblock");
ac27a0ec
DK
2327 goto out_fail;
2328 }
2329 /*
2330 * Note: s_es must be initialized as soon as possible because
617ba13b 2331 * some ext4 macro-instructions depend on its value
ac27a0ec 2332 */
617ba13b 2333 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
ac27a0ec
DK
2334 sbi->s_es = es;
2335 sb->s_magic = le16_to_cpu(es->s_magic);
617ba13b
MC
2336 if (sb->s_magic != EXT4_SUPER_MAGIC)
2337 goto cantfind_ext4;
afc32f7e 2338 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
ac27a0ec
DK
2339
2340 /* Set defaults before we parse the mount options */
2341 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
617ba13b 2342 if (def_mount_opts & EXT4_DEFM_DEBUG)
ac27a0ec 2343 set_opt(sbi->s_mount_opt, DEBUG);
617ba13b 2344 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
ac27a0ec 2345 set_opt(sbi->s_mount_opt, GRPID);
617ba13b 2346 if (def_mount_opts & EXT4_DEFM_UID16)
ac27a0ec 2347 set_opt(sbi->s_mount_opt, NO_UID32);
03010a33 2348#ifdef CONFIG_EXT4_FS_XATTR
617ba13b 2349 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
ac27a0ec 2350 set_opt(sbi->s_mount_opt, XATTR_USER);
2e7842b8 2351#endif
03010a33 2352#ifdef CONFIG_EXT4_FS_POSIX_ACL
617ba13b 2353 if (def_mount_opts & EXT4_DEFM_ACL)
ac27a0ec 2354 set_opt(sbi->s_mount_opt, POSIX_ACL);
2e7842b8 2355#endif
617ba13b
MC
2356 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2357 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2358 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2359 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2360 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2361 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2362
2363 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
ac27a0ec 2364 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
bb4f397a 2365 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
ceea16bf 2366 set_opt(sbi->s_mount_opt, ERRORS_CONT);
bb4f397a
AK
2367 else
2368 set_opt(sbi->s_mount_opt, ERRORS_RO);
ac27a0ec
DK
2369
2370 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2371 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
30773840
TT
2372 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2373 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2374 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
f4033903 2375 sbi->s_mb_history_max = default_mb_history_length;
ac27a0ec 2376
571640ca 2377 set_opt(sbi->s_mount_opt, BARRIER);
ac27a0ec 2378
dd919b98
AK
2379 /*
2380 * enable delayed allocation by default
2381 * Use -o nodelalloc to turn it off
2382 */
2383 set_opt(sbi->s_mount_opt, DELALLOC);
2384
b3881f74
TT
2385 if (!parse_options((char *) data, sb, &journal_devnum,
2386 &journal_ioprio, NULL, 0))
ac27a0ec
DK
2387 goto failed_mount;
2388
2389 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
617ba13b 2390 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec 2391
617ba13b
MC
2392 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2393 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2394 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2395 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
b31e1552
ES
2396 ext4_msg(sb, KERN_WARNING,
2397 "feature flags set on rev 0 fs, "
2398 "running e2fsck is recommended");
469108ff 2399
ac27a0ec
DK
2400 /*
2401 * Check feature flags regardless of the revision level, since we
2402 * previously didn't change the revision level when setting the flags,
2403 * so there is a chance incompat flags are set on a rev 0 filesystem.
2404 */
617ba13b 2405 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
ac27a0ec 2406 if (features) {
b31e1552
ES
2407 ext4_msg(sb, KERN_ERR,
2408 "Couldn't mount because of "
2409 "unsupported optional features (%x)",
3a06d778
AK
2410 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2411 ~EXT4_FEATURE_INCOMPAT_SUPP));
ac27a0ec
DK
2412 goto failed_mount;
2413 }
617ba13b 2414 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
ac27a0ec 2415 if (!(sb->s_flags & MS_RDONLY) && features) {
b31e1552
ES
2416 ext4_msg(sb, KERN_ERR,
2417 "Couldn't mount RDWR because of "
2418 "unsupported optional features (%x)",
3a06d778
AK
2419 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2420 ~EXT4_FEATURE_RO_COMPAT_SUPP));
ac27a0ec
DK
2421 goto failed_mount;
2422 }
f287a1a5
TT
2423 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2424 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2425 if (has_huge_files) {
0fc1b451
AK
2426 /*
2427 * Large file size enabled file system can only be
90c699a9 2428 * mount if kernel is build with CONFIG_LBDAF
0fc1b451
AK
2429 */
2430 if (sizeof(root->i_blocks) < sizeof(u64) &&
2431 !(sb->s_flags & MS_RDONLY)) {
b31e1552 2432 ext4_msg(sb, KERN_ERR, "Filesystem with huge "
0fc1b451 2433 "files cannot be mounted read-write "
90c699a9 2434 "without CONFIG_LBDAF");
0fc1b451
AK
2435 goto failed_mount;
2436 }
2437 }
ac27a0ec
DK
2438 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2439
617ba13b
MC
2440 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2441 blocksize > EXT4_MAX_BLOCK_SIZE) {
b31e1552
ES
2442 ext4_msg(sb, KERN_ERR,
2443 "Unsupported filesystem blocksize %d", blocksize);
ac27a0ec
DK
2444 goto failed_mount;
2445 }
2446
ac27a0ec 2447 if (sb->s_blocksize != blocksize) {
ce40733c
AK
2448 /* Validate the filesystem blocksize */
2449 if (!sb_set_blocksize(sb, blocksize)) {
b31e1552 2450 ext4_msg(sb, KERN_ERR, "bad block size %d",
ce40733c 2451 blocksize);
ac27a0ec
DK
2452 goto failed_mount;
2453 }
2454
2b2d6d01 2455 brelse(bh);
70bbb3e0
AM
2456 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2457 offset = do_div(logical_sb_block, blocksize);
2458 bh = sb_bread(sb, logical_sb_block);
ac27a0ec 2459 if (!bh) {
b31e1552
ES
2460 ext4_msg(sb, KERN_ERR,
2461 "Can't read superblock on 2nd try");
ac27a0ec
DK
2462 goto failed_mount;
2463 }
617ba13b 2464 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
ac27a0ec 2465 sbi->s_es = es;
617ba13b 2466 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
b31e1552
ES
2467 ext4_msg(sb, KERN_ERR,
2468 "Magic mismatch, very weird!");
ac27a0ec
DK
2469 goto failed_mount;
2470 }
2471 }
2472
f287a1a5
TT
2473 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2474 has_huge_files);
2475 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
ac27a0ec 2476
617ba13b
MC
2477 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2478 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2479 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
ac27a0ec
DK
2480 } else {
2481 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2482 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
617ba13b 2483 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
1330593e 2484 (!is_power_of_2(sbi->s_inode_size)) ||
ac27a0ec 2485 (sbi->s_inode_size > blocksize)) {
b31e1552
ES
2486 ext4_msg(sb, KERN_ERR,
2487 "unsupported inode size: %d",
2b2d6d01 2488 sbi->s_inode_size);
ac27a0ec
DK
2489 goto failed_mount;
2490 }
ef7f3835
KS
2491 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2492 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
ac27a0ec 2493 }
0b8e58a1 2494
0d1ee42f
AR
2495 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2496 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
8fadc143 2497 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
0d1ee42f 2498 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
d8ea6cf8 2499 !is_power_of_2(sbi->s_desc_size)) {
b31e1552
ES
2500 ext4_msg(sb, KERN_ERR,
2501 "unsupported descriptor size %lu",
0d1ee42f
AR
2502 sbi->s_desc_size);
2503 goto failed_mount;
2504 }
2505 } else
2506 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
0b8e58a1 2507
ac27a0ec 2508 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
ac27a0ec 2509 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
b47b6f38 2510 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
617ba13b 2511 goto cantfind_ext4;
0b8e58a1 2512
617ba13b 2513 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
ac27a0ec 2514 if (sbi->s_inodes_per_block == 0)
617ba13b 2515 goto cantfind_ext4;
ac27a0ec
DK
2516 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2517 sbi->s_inodes_per_block;
0d1ee42f 2518 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
ac27a0ec
DK
2519 sbi->s_sbh = bh;
2520 sbi->s_mount_state = le16_to_cpu(es->s_state);
e57aa839
FW
2521 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2522 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
0b8e58a1 2523
2b2d6d01 2524 for (i = 0; i < 4; i++)
ac27a0ec
DK
2525 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2526 sbi->s_def_hash_version = es->s_def_hash_version;
f99b2589
TT
2527 i = le32_to_cpu(es->s_flags);
2528 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2529 sbi->s_hash_unsigned = 3;
2530 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2531#ifdef __CHAR_UNSIGNED__
2532 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2533 sbi->s_hash_unsigned = 3;
2534#else
2535 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2536#endif
2537 sb->s_dirt = 1;
2538 }
ac27a0ec
DK
2539
2540 if (sbi->s_blocks_per_group > blocksize * 8) {
b31e1552
ES
2541 ext4_msg(sb, KERN_ERR,
2542 "#blocks per group too big: %lu",
2b2d6d01 2543 sbi->s_blocks_per_group);
ac27a0ec
DK
2544 goto failed_mount;
2545 }
ac27a0ec 2546 if (sbi->s_inodes_per_group > blocksize * 8) {
b31e1552
ES
2547 ext4_msg(sb, KERN_ERR,
2548 "#inodes per group too big: %lu",
2b2d6d01 2549 sbi->s_inodes_per_group);
ac27a0ec
DK
2550 goto failed_mount;
2551 }
2552
bf43d84b
ES
2553 /*
2554 * Test whether we have more sectors than will fit in sector_t,
2555 * and whether the max offset is addressable by the page cache.
2556 */
2557 if ((ext4_blocks_count(es) >
2558 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
2559 (ext4_blocks_count(es) >
2560 (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
b31e1552 2561 ext4_msg(sb, KERN_ERR, "filesystem"
bf43d84b 2562 " too large to mount safely on this system");
ac27a0ec 2563 if (sizeof(sector_t) < 8)
90c699a9 2564 ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
bf43d84b 2565 ret = -EFBIG;
ac27a0ec
DK
2566 goto failed_mount;
2567 }
2568
617ba13b
MC
2569 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2570 goto cantfind_ext4;
e7c95593 2571
0f2ddca6
FTN
2572 /* check blocks count against device size */
2573 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2574 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
b31e1552
ES
2575 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2576 "exceeds size of device (%llu blocks)",
0f2ddca6
FTN
2577 ext4_blocks_count(es), blocks_count);
2578 goto failed_mount;
2579 }
2580
0b8e58a1
AD
2581 /*
2582 * It makes no sense for the first data block to be beyond the end
2583 * of the filesystem.
2584 */
2585 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
b31e1552
ES
2586 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2587 "block %u is beyond end of filesystem (%llu)",
2588 le32_to_cpu(es->s_first_data_block),
2589 ext4_blocks_count(es));
e7c95593
ES
2590 goto failed_mount;
2591 }
bd81d8ee
LV
2592 blocks_count = (ext4_blocks_count(es) -
2593 le32_to_cpu(es->s_first_data_block) +
2594 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2595 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
4ec11028 2596 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
b31e1552 2597 ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
4ec11028 2598 "(block count %llu, first data block %u, "
b31e1552 2599 "blocks per group %lu)", sbi->s_groups_count,
4ec11028
TT
2600 ext4_blocks_count(es),
2601 le32_to_cpu(es->s_first_data_block),
2602 EXT4_BLOCKS_PER_GROUP(sb));
2603 goto failed_mount;
2604 }
bd81d8ee 2605 sbi->s_groups_count = blocks_count;
617ba13b
MC
2606 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2607 EXT4_DESC_PER_BLOCK(sb);
2b2d6d01 2608 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
ac27a0ec
DK
2609 GFP_KERNEL);
2610 if (sbi->s_group_desc == NULL) {
b31e1552 2611 ext4_msg(sb, KERN_ERR, "not enough memory");
ac27a0ec
DK
2612 goto failed_mount;
2613 }
2614
3244fcb1 2615#ifdef CONFIG_PROC_FS
9f6200bb
TT
2616 if (ext4_proc_root)
2617 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
3244fcb1 2618#endif
240799cd 2619
705895b6 2620 bgl_lock_init(sbi->s_blockgroup_lock);
ac27a0ec
DK
2621
2622 for (i = 0; i < db_count; i++) {
70bbb3e0 2623 block = descriptor_loc(sb, logical_sb_block, i);
ac27a0ec
DK
2624 sbi->s_group_desc[i] = sb_bread(sb, block);
2625 if (!sbi->s_group_desc[i]) {
b31e1552
ES
2626 ext4_msg(sb, KERN_ERR,
2627 "can't read group descriptor %d", i);
ac27a0ec
DK
2628 db_count = i;
2629 goto failed_mount2;
2630 }
2631 }
2b2d6d01 2632 if (!ext4_check_descriptors(sb)) {
b31e1552 2633 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
ac27a0ec
DK
2634 goto failed_mount2;
2635 }
772cb7c8
JS
2636 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2637 if (!ext4_fill_flex_info(sb)) {
b31e1552
ES
2638 ext4_msg(sb, KERN_ERR,
2639 "unable to initialize "
2640 "flex_bg meta info!");
772cb7c8
JS
2641 goto failed_mount2;
2642 }
2643
ac27a0ec
DK
2644 sbi->s_gdb_count = db_count;
2645 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2646 spin_lock_init(&sbi->s_next_gen_lock);
2647
833f4077
PZ
2648 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2649 ext4_count_free_blocks(sb));
2650 if (!err) {
2651 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2652 ext4_count_free_inodes(sb));
2653 }
2654 if (!err) {
2655 err = percpu_counter_init(&sbi->s_dirs_counter,
2656 ext4_count_dirs(sb));
2657 }
6bc6e63f
AK
2658 if (!err) {
2659 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2660 }
833f4077 2661 if (err) {
b31e1552 2662 ext4_msg(sb, KERN_ERR, "insufficient memory");
833f4077
PZ
2663 goto failed_mount3;
2664 }
ac27a0ec 2665
c9de560d
AT
2666 sbi->s_stripe = ext4_get_stripe_size(sbi);
2667
ac27a0ec
DK
2668 /*
2669 * set up enough so that it can read an inode
2670 */
9ca92389
TT
2671 if (!test_opt(sb, NOLOAD) &&
2672 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2673 sb->s_op = &ext4_sops;
2674 else
2675 sb->s_op = &ext4_nojournal_sops;
617ba13b
MC
2676 sb->s_export_op = &ext4_export_ops;
2677 sb->s_xattr = ext4_xattr_handlers;
ac27a0ec 2678#ifdef CONFIG_QUOTA
617ba13b
MC
2679 sb->s_qcop = &ext4_qctl_operations;
2680 sb->dq_op = &ext4_quota_operations;
ac27a0ec
DK
2681#endif
2682 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
3b9d4ed2 2683 mutex_init(&sbi->s_orphan_lock);
32ed5058 2684 mutex_init(&sbi->s_resize_lock);
ac27a0ec
DK
2685
2686 sb->s_root = NULL;
2687
2688 needs_recovery = (es->s_last_orphan != 0 ||
617ba13b
MC
2689 EXT4_HAS_INCOMPAT_FEATURE(sb,
2690 EXT4_FEATURE_INCOMPAT_RECOVER));
ac27a0ec
DK
2691
2692 /*
2693 * The first inode we look at is the journal inode. Don't try
2694 * root first: it may be modified in the journal!
2695 */
2696 if (!test_opt(sb, NOLOAD) &&
617ba13b
MC
2697 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2698 if (ext4_load_journal(sb, es, journal_devnum))
ac27a0ec 2699 goto failed_mount3;
624080ed
TT
2700 if (!(sb->s_flags & MS_RDONLY) &&
2701 EXT4_SB(sb)->s_journal->j_failed_commit) {
b31e1552 2702 ext4_msg(sb, KERN_CRIT, "error: "
624080ed 2703 "ext4_fill_super: Journal transaction "
b31e1552 2704 "%u is corrupt",
624080ed 2705 EXT4_SB(sb)->s_journal->j_failed_commit);
2b2d6d01 2706 if (test_opt(sb, ERRORS_RO)) {
b31e1552
ES
2707 ext4_msg(sb, KERN_CRIT,
2708 "Mounting filesystem read-only");
624080ed
TT
2709 sb->s_flags |= MS_RDONLY;
2710 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2711 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2712 }
2713 if (test_opt(sb, ERRORS_PANIC)) {
2714 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2715 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
e2d67052 2716 ext4_commit_super(sb, 1);
624080ed
TT
2717 goto failed_mount4;
2718 }
2719 }
0390131b
FM
2720 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2721 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
b31e1552
ES
2722 ext4_msg(sb, KERN_ERR, "required journal recovery "
2723 "suppressed and not mounted read-only");
0390131b 2724 goto failed_mount4;
ac27a0ec 2725 } else {
0390131b
FM
2726 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2727 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2728 sbi->s_journal = NULL;
2729 needs_recovery = 0;
2730 goto no_journal;
ac27a0ec
DK
2731 }
2732
eb40a09c
JS
2733 if (ext4_blocks_count(es) > 0xffffffffULL &&
2734 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2735 JBD2_FEATURE_INCOMPAT_64BIT)) {
b31e1552 2736 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
eb40a09c
JS
2737 goto failed_mount4;
2738 }
2739
818d276c
GS
2740 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2741 jbd2_journal_set_features(sbi->s_journal,
2742 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2743 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2744 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2745 jbd2_journal_set_features(sbi->s_journal,
2746 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2747 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2748 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2749 } else {
2750 jbd2_journal_clear_features(sbi->s_journal,
2751 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2752 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2753 }
2754
ac27a0ec
DK
2755 /* We have now updated the journal if required, so we can
2756 * validate the data journaling mode. */
2757 switch (test_opt(sb, DATA_FLAGS)) {
2758 case 0:
2759 /* No mode set, assume a default based on the journal
63f57933
AM
2760 * capabilities: ORDERED_DATA if the journal can
2761 * cope, else JOURNAL_DATA
2762 */
dab291af
MC
2763 if (jbd2_journal_check_available_features
2764 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
ac27a0ec
DK
2765 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2766 else
2767 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2768 break;
2769
617ba13b
MC
2770 case EXT4_MOUNT_ORDERED_DATA:
2771 case EXT4_MOUNT_WRITEBACK_DATA:
dab291af
MC
2772 if (!jbd2_journal_check_available_features
2773 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
b31e1552
ES
2774 ext4_msg(sb, KERN_ERR, "Journal does not support "
2775 "requested data journaling mode");
ac27a0ec
DK
2776 goto failed_mount4;
2777 }
2778 default:
2779 break;
2780 }
b3881f74 2781 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
ac27a0ec 2782
0390131b 2783no_journal:
ac27a0ec
DK
2784
2785 if (test_opt(sb, NOBH)) {
617ba13b 2786 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
b31e1552
ES
2787 ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
2788 "its supported only with writeback mode");
ac27a0ec
DK
2789 clear_opt(sbi->s_mount_opt, NOBH);
2790 }
2791 }
2792 /*
dab291af 2793 * The jbd2_journal_load will have done any necessary log recovery,
ac27a0ec
DK
2794 * so we can safely mount the rest of the filesystem now.
2795 */
2796
1d1fe1ee
DH
2797 root = ext4_iget(sb, EXT4_ROOT_INO);
2798 if (IS_ERR(root)) {
b31e1552 2799 ext4_msg(sb, KERN_ERR, "get root inode failed");
1d1fe1ee 2800 ret = PTR_ERR(root);
ac27a0ec
DK
2801 goto failed_mount4;
2802 }
2803 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1d1fe1ee 2804 iput(root);
b31e1552 2805 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
ac27a0ec
DK
2806 goto failed_mount4;
2807 }
1d1fe1ee
DH
2808 sb->s_root = d_alloc_root(root);
2809 if (!sb->s_root) {
b31e1552 2810 ext4_msg(sb, KERN_ERR, "get root dentry failed");
1d1fe1ee
DH
2811 iput(root);
2812 ret = -ENOMEM;
2813 goto failed_mount4;
2814 }
ac27a0ec 2815
2b2d6d01 2816 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
ef7f3835
KS
2817
2818 /* determine the minimum size of new large inodes, if present */
2819 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2820 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2821 EXT4_GOOD_OLD_INODE_SIZE;
2822 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2823 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2824 if (sbi->s_want_extra_isize <
2825 le16_to_cpu(es->s_want_extra_isize))
2826 sbi->s_want_extra_isize =
2827 le16_to_cpu(es->s_want_extra_isize);
2828 if (sbi->s_want_extra_isize <
2829 le16_to_cpu(es->s_min_extra_isize))
2830 sbi->s_want_extra_isize =
2831 le16_to_cpu(es->s_min_extra_isize);
2832 }
2833 }
2834 /* Check if enough inode space is available */
2835 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2836 sbi->s_inode_size) {
2837 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2838 EXT4_GOOD_OLD_INODE_SIZE;
b31e1552
ES
2839 ext4_msg(sb, KERN_INFO, "required extra inode space not"
2840 "available");
ef7f3835
KS
2841 }
2842
c2774d84 2843 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
b31e1552
ES
2844 ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2845 "requested data journaling mode");
c2774d84
AK
2846 clear_opt(sbi->s_mount_opt, DELALLOC);
2847 } else if (test_opt(sb, DELALLOC))
b31e1552 2848 ext4_msg(sb, KERN_INFO, "delayed allocation enabled");
c2774d84 2849
6fd058f7
TT
2850 err = ext4_setup_system_zone(sb);
2851 if (err) {
b31e1552
ES
2852 ext4_msg(sb, KERN_ERR, "failed to initialize system "
2853 "zone (%d)\n", err);
6fd058f7
TT
2854 goto failed_mount4;
2855 }
2856
c2774d84
AK
2857 ext4_ext_init(sb);
2858 err = ext4_mb_init(sb, needs_recovery);
2859 if (err) {
b31e1552
ES
2860 ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
2861 err);
c2774d84
AK
2862 goto failed_mount4;
2863 }
2864
3197ebdb
TT
2865 sbi->s_kobj.kset = ext4_kset;
2866 init_completion(&sbi->s_kobj_unregister);
2867 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
2868 "%s", sb->s_id);
2869 if (err) {
2870 ext4_mb_release(sb);
2871 ext4_ext_release(sb);
2872 goto failed_mount4;
2873 };
2874
617ba13b
MC
2875 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2876 ext4_orphan_cleanup(sb, es);
2877 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
0390131b 2878 if (needs_recovery) {
b31e1552 2879 ext4_msg(sb, KERN_INFO, "recovery complete");
0390131b
FM
2880 ext4_mark_recovery_complete(sb, es);
2881 }
2882 if (EXT4_SB(sb)->s_journal) {
2883 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2884 descr = " journalled data mode";
2885 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2886 descr = " ordered data mode";
2887 else
2888 descr = " writeback data mode";
2889 } else
2890 descr = "out journal";
2891
b31e1552 2892 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr);
ac27a0ec
DK
2893
2894 lock_kernel();
2895 return 0;
2896
617ba13b 2897cantfind_ext4:
ac27a0ec 2898 if (!silent)
b31e1552 2899 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
ac27a0ec
DK
2900 goto failed_mount;
2901
2902failed_mount4:
b31e1552 2903 ext4_msg(sb, KERN_ERR, "mount failed");
6fd058f7 2904 ext4_release_system_zone(sb);
0390131b
FM
2905 if (sbi->s_journal) {
2906 jbd2_journal_destroy(sbi->s_journal);
2907 sbi->s_journal = NULL;
2908 }
ac27a0ec 2909failed_mount3:
c5ca7c76
TT
2910 if (sbi->s_flex_groups) {
2911 if (is_vmalloc_addr(sbi->s_flex_groups))
2912 vfree(sbi->s_flex_groups);
2913 else
2914 kfree(sbi->s_flex_groups);
2915 }
ac27a0ec
DK
2916 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2917 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2918 percpu_counter_destroy(&sbi->s_dirs_counter);
6bc6e63f 2919 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
ac27a0ec
DK
2920failed_mount2:
2921 for (i = 0; i < db_count; i++)
2922 brelse(sbi->s_group_desc[i]);
2923 kfree(sbi->s_group_desc);
2924failed_mount:
240799cd 2925 if (sbi->s_proc) {
9f6200bb 2926 remove_proc_entry(sb->s_id, ext4_proc_root);
240799cd 2927 }
ac27a0ec
DK
2928#ifdef CONFIG_QUOTA
2929 for (i = 0; i < MAXQUOTAS; i++)
2930 kfree(sbi->s_qf_names[i]);
2931#endif
617ba13b 2932 ext4_blkdev_remove(sbi);
ac27a0ec
DK
2933 brelse(bh);
2934out_fail:
2935 sb->s_fs_info = NULL;
f6830165 2936 kfree(sbi->s_blockgroup_lock);
ac27a0ec
DK
2937 kfree(sbi);
2938 lock_kernel();
1d1fe1ee 2939 return ret;
ac27a0ec
DK
2940}
2941
2942/*
2943 * Setup any per-fs journal parameters now. We'll do this both on
2944 * initial mount, once the journal has been initialised but before we've
2945 * done any recovery; and again on any subsequent remount.
2946 */
617ba13b 2947static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
ac27a0ec 2948{
617ba13b 2949 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 2950
30773840
TT
2951 journal->j_commit_interval = sbi->s_commit_interval;
2952 journal->j_min_batch_time = sbi->s_min_batch_time;
2953 journal->j_max_batch_time = sbi->s_max_batch_time;
ac27a0ec
DK
2954
2955 spin_lock(&journal->j_state_lock);
2956 if (test_opt(sb, BARRIER))
dab291af 2957 journal->j_flags |= JBD2_BARRIER;
ac27a0ec 2958 else
dab291af 2959 journal->j_flags &= ~JBD2_BARRIER;
5bf5683a
HK
2960 if (test_opt(sb, DATA_ERR_ABORT))
2961 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2962 else
2963 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
ac27a0ec
DK
2964 spin_unlock(&journal->j_state_lock);
2965}
2966
617ba13b 2967static journal_t *ext4_get_journal(struct super_block *sb,
ac27a0ec
DK
2968 unsigned int journal_inum)
2969{
2970 struct inode *journal_inode;
2971 journal_t *journal;
2972
0390131b
FM
2973 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2974
ac27a0ec
DK
2975 /* First, test for the existence of a valid inode on disk. Bad
2976 * things happen if we iget() an unused inode, as the subsequent
2977 * iput() will try to delete it. */
2978
1d1fe1ee
DH
2979 journal_inode = ext4_iget(sb, journal_inum);
2980 if (IS_ERR(journal_inode)) {
b31e1552 2981 ext4_msg(sb, KERN_ERR, "no journal found");
ac27a0ec
DK
2982 return NULL;
2983 }
2984 if (!journal_inode->i_nlink) {
2985 make_bad_inode(journal_inode);
2986 iput(journal_inode);
b31e1552 2987 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
ac27a0ec
DK
2988 return NULL;
2989 }
2990
e5f8eab8 2991 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
ac27a0ec 2992 journal_inode, journal_inode->i_size);
1d1fe1ee 2993 if (!S_ISREG(journal_inode->i_mode)) {
b31e1552 2994 ext4_msg(sb, KERN_ERR, "invalid journal inode");
ac27a0ec
DK
2995 iput(journal_inode);
2996 return NULL;
2997 }
2998
dab291af 2999 journal = jbd2_journal_init_inode(journal_inode);
ac27a0ec 3000 if (!journal) {
b31e1552 3001 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
ac27a0ec
DK
3002 iput(journal_inode);
3003 return NULL;
3004 }
3005 journal->j_private = sb;
617ba13b 3006 ext4_init_journal_params(sb, journal);
ac27a0ec
DK
3007 return journal;
3008}
3009
617ba13b 3010static journal_t *ext4_get_dev_journal(struct super_block *sb,
ac27a0ec
DK
3011 dev_t j_dev)
3012{
2b2d6d01 3013 struct buffer_head *bh;
ac27a0ec 3014 journal_t *journal;
617ba13b
MC
3015 ext4_fsblk_t start;
3016 ext4_fsblk_t len;
ac27a0ec 3017 int hblock, blocksize;
617ba13b 3018 ext4_fsblk_t sb_block;
ac27a0ec 3019 unsigned long offset;
2b2d6d01 3020 struct ext4_super_block *es;
ac27a0ec
DK
3021 struct block_device *bdev;
3022
0390131b
FM
3023 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3024
b31e1552 3025 bdev = ext4_blkdev_get(j_dev, sb);
ac27a0ec
DK
3026 if (bdev == NULL)
3027 return NULL;
3028
3029 if (bd_claim(bdev, sb)) {
b31e1552
ES
3030 ext4_msg(sb, KERN_ERR,
3031 "failed to claim external journal device");
9a1c3542 3032 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
ac27a0ec
DK
3033 return NULL;
3034 }
3035
3036 blocksize = sb->s_blocksize;
e1defc4f 3037 hblock = bdev_logical_block_size(bdev);
ac27a0ec 3038 if (blocksize < hblock) {
b31e1552
ES
3039 ext4_msg(sb, KERN_ERR,
3040 "blocksize too small for journal device");
ac27a0ec
DK
3041 goto out_bdev;
3042 }
3043
617ba13b
MC
3044 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3045 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
ac27a0ec
DK
3046 set_blocksize(bdev, blocksize);
3047 if (!(bh = __bread(bdev, sb_block, blocksize))) {
b31e1552
ES
3048 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3049 "external journal");
ac27a0ec
DK
3050 goto out_bdev;
3051 }
3052
617ba13b
MC
3053 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3054 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
ac27a0ec 3055 !(le32_to_cpu(es->s_feature_incompat) &
617ba13b 3056 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
b31e1552
ES
3057 ext4_msg(sb, KERN_ERR, "external journal has "
3058 "bad superblock");
ac27a0ec
DK
3059 brelse(bh);
3060 goto out_bdev;
3061 }
3062
617ba13b 3063 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
b31e1552 3064 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
ac27a0ec
DK
3065 brelse(bh);
3066 goto out_bdev;
3067 }
3068
bd81d8ee 3069 len = ext4_blocks_count(es);
ac27a0ec
DK
3070 start = sb_block + 1;
3071 brelse(bh); /* we're done with the superblock */
3072
dab291af 3073 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
ac27a0ec
DK
3074 start, len, blocksize);
3075 if (!journal) {
b31e1552 3076 ext4_msg(sb, KERN_ERR, "failed to create device journal");
ac27a0ec
DK
3077 goto out_bdev;
3078 }
3079 journal->j_private = sb;
3080 ll_rw_block(READ, 1, &journal->j_sb_buffer);
3081 wait_on_buffer(journal->j_sb_buffer);
3082 if (!buffer_uptodate(journal->j_sb_buffer)) {
b31e1552 3083 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
ac27a0ec
DK
3084 goto out_journal;
3085 }
3086 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
b31e1552
ES
3087 ext4_msg(sb, KERN_ERR, "External journal has more than one "
3088 "user (unsupported) - %d",
ac27a0ec
DK
3089 be32_to_cpu(journal->j_superblock->s_nr_users));
3090 goto out_journal;
3091 }
617ba13b
MC
3092 EXT4_SB(sb)->journal_bdev = bdev;
3093 ext4_init_journal_params(sb, journal);
ac27a0ec 3094 return journal;
0b8e58a1 3095
ac27a0ec 3096out_journal:
dab291af 3097 jbd2_journal_destroy(journal);
ac27a0ec 3098out_bdev:
617ba13b 3099 ext4_blkdev_put(bdev);
ac27a0ec
DK
3100 return NULL;
3101}
3102
617ba13b
MC
3103static int ext4_load_journal(struct super_block *sb,
3104 struct ext4_super_block *es,
ac27a0ec
DK
3105 unsigned long journal_devnum)
3106{
3107 journal_t *journal;
3108 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3109 dev_t journal_dev;
3110 int err = 0;
3111 int really_read_only;
3112
0390131b
FM
3113 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3114
ac27a0ec
DK
3115 if (journal_devnum &&
3116 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
b31e1552
ES
3117 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3118 "numbers have changed");
ac27a0ec
DK
3119 journal_dev = new_decode_dev(journal_devnum);
3120 } else
3121 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3122
3123 really_read_only = bdev_read_only(sb->s_bdev);
3124
3125 /*
3126 * Are we loading a blank journal or performing recovery after a
3127 * crash? For recovery, we need to check in advance whether we
3128 * can get read-write access to the device.
3129 */
617ba13b 3130 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
ac27a0ec 3131 if (sb->s_flags & MS_RDONLY) {
b31e1552
ES
3132 ext4_msg(sb, KERN_INFO, "INFO: recovery "
3133 "required on readonly filesystem");
ac27a0ec 3134 if (really_read_only) {
b31e1552
ES
3135 ext4_msg(sb, KERN_ERR, "write access "
3136 "unavailable, cannot proceed");
ac27a0ec
DK
3137 return -EROFS;
3138 }
b31e1552
ES
3139 ext4_msg(sb, KERN_INFO, "write access will "
3140 "be enabled during recovery");
ac27a0ec
DK
3141 }
3142 }
3143
3144 if (journal_inum && journal_dev) {
b31e1552
ES
3145 ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3146 "and inode journals!");
ac27a0ec
DK
3147 return -EINVAL;
3148 }
3149
3150 if (journal_inum) {
617ba13b 3151 if (!(journal = ext4_get_journal(sb, journal_inum)))
ac27a0ec
DK
3152 return -EINVAL;
3153 } else {
617ba13b 3154 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
ac27a0ec
DK
3155 return -EINVAL;
3156 }
3157
4776004f 3158 if (journal->j_flags & JBD2_BARRIER)
b31e1552 3159 ext4_msg(sb, KERN_INFO, "barriers enabled");
4776004f 3160 else
b31e1552 3161 ext4_msg(sb, KERN_INFO, "barriers disabled");
4776004f 3162
ac27a0ec 3163 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
dab291af 3164 err = jbd2_journal_update_format(journal);
ac27a0ec 3165 if (err) {
b31e1552 3166 ext4_msg(sb, KERN_ERR, "error updating journal");
dab291af 3167 jbd2_journal_destroy(journal);
ac27a0ec
DK
3168 return err;
3169 }
3170 }
3171
617ba13b 3172 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
dab291af 3173 err = jbd2_journal_wipe(journal, !really_read_only);
ac27a0ec 3174 if (!err)
dab291af 3175 err = jbd2_journal_load(journal);
ac27a0ec
DK
3176
3177 if (err) {
b31e1552 3178 ext4_msg(sb, KERN_ERR, "error loading journal");
dab291af 3179 jbd2_journal_destroy(journal);
ac27a0ec
DK
3180 return err;
3181 }
3182
617ba13b
MC
3183 EXT4_SB(sb)->s_journal = journal;
3184 ext4_clear_journal_err(sb, es);
ac27a0ec
DK
3185
3186 if (journal_devnum &&
3187 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3188 es->s_journal_dev = cpu_to_le32(journal_devnum);
ac27a0ec
DK
3189
3190 /* Make sure we flush the recovery flag to disk. */
e2d67052 3191 ext4_commit_super(sb, 1);
ac27a0ec
DK
3192 }
3193
3194 return 0;
3195}
3196
e2d67052 3197static int ext4_commit_super(struct super_block *sb, int sync)
ac27a0ec 3198{
e2d67052 3199 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
617ba13b 3200 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
c4be0c1d 3201 int error = 0;
ac27a0ec
DK
3202
3203 if (!sbh)
c4be0c1d 3204 return error;
914258bf
TT
3205 if (buffer_write_io_error(sbh)) {
3206 /*
3207 * Oh, dear. A previous attempt to write the
3208 * superblock failed. This could happen because the
3209 * USB device was yanked out. Or it could happen to
3210 * be a transient write error and maybe the block will
3211 * be remapped. Nothing we can do but to retry the
3212 * write and hope for the best.
3213 */
b31e1552
ES
3214 ext4_msg(sb, KERN_ERR, "previous I/O error to "
3215 "superblock detected");
914258bf
TT
3216 clear_buffer_write_io_error(sbh);
3217 set_buffer_uptodate(sbh);
3218 }
ac27a0ec 3219 es->s_wtime = cpu_to_le32(get_seconds());
afc32f7e
TT
3220 es->s_kbytes_written =
3221 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3222 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3223 EXT4_SB(sb)->s_sectors_written_start) >> 1));
5d1b1b3f
AK
3224 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
3225 &EXT4_SB(sb)->s_freeblocks_counter));
3226 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
3227 &EXT4_SB(sb)->s_freeinodes_counter));
7234ab2a 3228 sb->s_dirt = 0;
ac27a0ec
DK
3229 BUFFER_TRACE(sbh, "marking dirty");
3230 mark_buffer_dirty(sbh);
914258bf 3231 if (sync) {
c4be0c1d
TS
3232 error = sync_dirty_buffer(sbh);
3233 if (error)
3234 return error;
3235
3236 error = buffer_write_io_error(sbh);
3237 if (error) {
b31e1552
ES
3238 ext4_msg(sb, KERN_ERR, "I/O error while writing "
3239 "superblock");
914258bf
TT
3240 clear_buffer_write_io_error(sbh);
3241 set_buffer_uptodate(sbh);
3242 }
3243 }
c4be0c1d 3244 return error;
ac27a0ec
DK
3245}
3246
ac27a0ec
DK
3247/*
3248 * Have we just finished recovery? If so, and if we are mounting (or
3249 * remounting) the filesystem readonly, then we will end up with a
3250 * consistent fs on disk. Record that fact.
3251 */
2b2d6d01
TT
3252static void ext4_mark_recovery_complete(struct super_block *sb,
3253 struct ext4_super_block *es)
ac27a0ec 3254{
617ba13b 3255 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 3256
0390131b
FM
3257 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3258 BUG_ON(journal != NULL);
3259 return;
3260 }
dab291af 3261 jbd2_journal_lock_updates(journal);
7ffe1ea8
HK
3262 if (jbd2_journal_flush(journal) < 0)
3263 goto out;
3264
617ba13b 3265 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
ac27a0ec 3266 sb->s_flags & MS_RDONLY) {
617ba13b 3267 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
e2d67052 3268 ext4_commit_super(sb, 1);
ac27a0ec 3269 }
7ffe1ea8
HK
3270
3271out:
dab291af 3272 jbd2_journal_unlock_updates(journal);
ac27a0ec
DK
3273}
3274
3275/*
3276 * If we are mounting (or read-write remounting) a filesystem whose journal
3277 * has recorded an error from a previous lifetime, move that error to the
3278 * main filesystem now.
3279 */
2b2d6d01
TT
3280static void ext4_clear_journal_err(struct super_block *sb,
3281 struct ext4_super_block *es)
ac27a0ec
DK
3282{
3283 journal_t *journal;
3284 int j_errno;
3285 const char *errstr;
3286
0390131b
FM
3287 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3288
617ba13b 3289 journal = EXT4_SB(sb)->s_journal;
ac27a0ec
DK
3290
3291 /*
3292 * Now check for any error status which may have been recorded in the
617ba13b 3293 * journal by a prior ext4_error() or ext4_abort()
ac27a0ec
DK
3294 */
3295
dab291af 3296 j_errno = jbd2_journal_errno(journal);
ac27a0ec
DK
3297 if (j_errno) {
3298 char nbuf[16];
3299
617ba13b 3300 errstr = ext4_decode_error(sb, j_errno, nbuf);
46e665e9 3301 ext4_warning(sb, __func__, "Filesystem error recorded "
ac27a0ec 3302 "from previous mount: %s", errstr);
46e665e9 3303 ext4_warning(sb, __func__, "Marking fs in need of "
ac27a0ec
DK
3304 "filesystem check.");
3305
617ba13b
MC
3306 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3307 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
e2d67052 3308 ext4_commit_super(sb, 1);
ac27a0ec 3309
dab291af 3310 jbd2_journal_clear_err(journal);
ac27a0ec
DK
3311 }
3312}
3313
3314/*
3315 * Force the running and committing transactions to commit,
3316 * and wait on the commit.
3317 */
617ba13b 3318int ext4_force_commit(struct super_block *sb)
ac27a0ec
DK
3319{
3320 journal_t *journal;
0390131b 3321 int ret = 0;
ac27a0ec
DK
3322
3323 if (sb->s_flags & MS_RDONLY)
3324 return 0;
3325
617ba13b 3326 journal = EXT4_SB(sb)->s_journal;
7234ab2a 3327 if (journal)
0390131b 3328 ret = ext4_journal_force_commit(journal);
0390131b 3329
ac27a0ec
DK
3330 return ret;
3331}
3332
2b2d6d01 3333static void ext4_write_super(struct super_block *sb)
ac27a0ec 3334{
ebc1ac16 3335 lock_super(sb);
9ca92389 3336 ext4_commit_super(sb, 1);
ebc1ac16 3337 unlock_super(sb);
ac27a0ec
DK
3338}
3339
617ba13b 3340static int ext4_sync_fs(struct super_block *sb, int wait)
ac27a0ec 3341{
14ce0cb4 3342 int ret = 0;
9eddacf9 3343 tid_t target;
ac27a0ec 3344
9bffad1e 3345 trace_ext4_sync_fs(sb, wait);
9ca92389
TT
3346 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
3347 if (wait)
3348 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
0390131b 3349 }
14ce0cb4 3350 return ret;
ac27a0ec
DK
3351}
3352
3353/*
3354 * LVM calls this function before a (read-only) snapshot is created. This
3355 * gives us a chance to flush the journal completely and mark the fs clean.
3356 */
c4be0c1d 3357static int ext4_freeze(struct super_block *sb)
ac27a0ec 3358{
c4be0c1d
TS
3359 int error = 0;
3360 journal_t *journal;
ac27a0ec 3361
9ca92389
TT
3362 if (sb->s_flags & MS_RDONLY)
3363 return 0;
ac27a0ec 3364
9ca92389 3365 journal = EXT4_SB(sb)->s_journal;
7ffe1ea8 3366
9ca92389
TT
3367 /* Now we set up the journal barrier. */
3368 jbd2_journal_lock_updates(journal);
ac27a0ec 3369
9ca92389
TT
3370 /*
3371 * Don't clear the needs_recovery flag if we failed to flush
3372 * the journal.
3373 */
3374 error = jbd2_journal_flush(journal);
3375 if (error < 0) {
3376 out:
3377 jbd2_journal_unlock_updates(journal);
3378 return error;
ac27a0ec 3379 }
9ca92389
TT
3380
3381 /* Journal blocked and flushed, clear needs_recovery flag. */
3382 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3383 error = ext4_commit_super(sb, 1);
3384 if (error)
3385 goto out;
c4be0c1d 3386 return 0;
ac27a0ec
DK
3387}
3388
3389/*
3390 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3391 * flag here, even though the filesystem is not technically dirty yet.
3392 */
c4be0c1d 3393static int ext4_unfreeze(struct super_block *sb)
ac27a0ec 3394{
9ca92389
TT
3395 if (sb->s_flags & MS_RDONLY)
3396 return 0;
3397
3398 lock_super(sb);
3399 /* Reset the needs_recovery flag before the fs is unlocked. */
3400 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3401 ext4_commit_super(sb, 1);
3402 unlock_super(sb);
3403 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
c4be0c1d 3404 return 0;
ac27a0ec
DK
3405}
3406
2b2d6d01 3407static int ext4_remount(struct super_block *sb, int *flags, char *data)
ac27a0ec 3408{
2b2d6d01 3409 struct ext4_super_block *es;
617ba13b
MC
3410 struct ext4_sb_info *sbi = EXT4_SB(sb);
3411 ext4_fsblk_t n_blocks_count = 0;
ac27a0ec 3412 unsigned long old_sb_flags;
617ba13b 3413 struct ext4_mount_options old_opts;
8a266467 3414 ext4_group_t g;
b3881f74 3415 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
ac27a0ec
DK
3416 int err;
3417#ifdef CONFIG_QUOTA
3418 int i;
3419#endif
3420
337eb00a
AIB
3421 lock_kernel();
3422
ac27a0ec 3423 /* Store the original options */
bbd6851a 3424 lock_super(sb);
ac27a0ec
DK
3425 old_sb_flags = sb->s_flags;
3426 old_opts.s_mount_opt = sbi->s_mount_opt;
3427 old_opts.s_resuid = sbi->s_resuid;
3428 old_opts.s_resgid = sbi->s_resgid;
3429 old_opts.s_commit_interval = sbi->s_commit_interval;
30773840
TT
3430 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3431 old_opts.s_max_batch_time = sbi->s_max_batch_time;
ac27a0ec
DK
3432#ifdef CONFIG_QUOTA
3433 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3434 for (i = 0; i < MAXQUOTAS; i++)
3435 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3436#endif
b3881f74
TT
3437 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3438 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
ac27a0ec
DK
3439
3440 /*
3441 * Allow the "check" option to be passed as a remount option.
3442 */
b3881f74
TT
3443 if (!parse_options(data, sb, NULL, &journal_ioprio,
3444 &n_blocks_count, 1)) {
ac27a0ec
DK
3445 err = -EINVAL;
3446 goto restore_opts;
3447 }
3448
4ab2f15b 3449 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
46e665e9 3450 ext4_abort(sb, __func__, "Abort forced by user");
ac27a0ec
DK
3451
3452 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
617ba13b 3453 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec
DK
3454
3455 es = sbi->s_es;
3456
b3881f74 3457 if (sbi->s_journal) {
0390131b 3458 ext4_init_journal_params(sb, sbi->s_journal);
b3881f74
TT
3459 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3460 }
ac27a0ec
DK
3461
3462 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
bd81d8ee 3463 n_blocks_count > ext4_blocks_count(es)) {
4ab2f15b 3464 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
ac27a0ec
DK
3465 err = -EROFS;
3466 goto restore_opts;
3467 }
3468
3469 if (*flags & MS_RDONLY) {
3470 /*
3471 * First of all, the unconditional stuff we have to do
3472 * to disable replay of the journal when we next remount
3473 */
3474 sb->s_flags |= MS_RDONLY;
3475
3476 /*
3477 * OK, test if we are remounting a valid rw partition
3478 * readonly, and if so set the rdonly flag and then
3479 * mark the partition as valid again.
3480 */
617ba13b
MC
3481 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3482 (sbi->s_mount_state & EXT4_VALID_FS))
ac27a0ec
DK
3483 es->s_state = cpu_to_le16(sbi->s_mount_state);
3484
a63c9eb2 3485 if (sbi->s_journal)
0390131b 3486 ext4_mark_recovery_complete(sb, es);
ac27a0ec 3487 } else {
3a06d778 3488 int ret;
617ba13b
MC
3489 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3490 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
b31e1552 3491 ext4_msg(sb, KERN_WARNING, "couldn't "
ac27a0ec 3492 "remount RDWR because of unsupported "
b31e1552 3493 "optional features (%x)",
3a06d778
AK
3494 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3495 ~EXT4_FEATURE_RO_COMPAT_SUPP));
ac27a0ec
DK
3496 err = -EROFS;
3497 goto restore_opts;
3498 }
ead6596b 3499
8a266467
TT
3500 /*
3501 * Make sure the group descriptor checksums
0b8e58a1 3502 * are sane. If they aren't, refuse to remount r/w.
8a266467
TT
3503 */
3504 for (g = 0; g < sbi->s_groups_count; g++) {
3505 struct ext4_group_desc *gdp =
3506 ext4_get_group_desc(sb, g, NULL);
3507
3508 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
b31e1552
ES
3509 ext4_msg(sb, KERN_ERR,
3510 "ext4_remount: Checksum for group %u failed (%u!=%u)",
8a266467
TT
3511 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3512 le16_to_cpu(gdp->bg_checksum));
3513 err = -EINVAL;
3514 goto restore_opts;
3515 }
3516 }
3517
ead6596b
ES
3518 /*
3519 * If we have an unprocessed orphan list hanging
3520 * around from a previously readonly bdev mount,
3521 * require a full umount/remount for now.
3522 */
3523 if (es->s_last_orphan) {
b31e1552 3524 ext4_msg(sb, KERN_WARNING, "Couldn't "
ead6596b
ES
3525 "remount RDWR because of unprocessed "
3526 "orphan inode list. Please "
b31e1552 3527 "umount/remount instead");
ead6596b
ES
3528 err = -EINVAL;
3529 goto restore_opts;
3530 }
3531
ac27a0ec
DK
3532 /*
3533 * Mounting a RDONLY partition read-write, so reread
3534 * and store the current valid flag. (It may have
3535 * been changed by e2fsck since we originally mounted
3536 * the partition.)
3537 */
0390131b
FM
3538 if (sbi->s_journal)
3539 ext4_clear_journal_err(sb, es);
ac27a0ec 3540 sbi->s_mount_state = le16_to_cpu(es->s_state);
617ba13b 3541 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
ac27a0ec 3542 goto restore_opts;
2b2d6d01 3543 if (!ext4_setup_super(sb, es, 0))
ac27a0ec
DK
3544 sb->s_flags &= ~MS_RDONLY;
3545 }
3546 }
6fd058f7 3547 ext4_setup_system_zone(sb);
0390131b 3548 if (sbi->s_journal == NULL)
e2d67052 3549 ext4_commit_super(sb, 1);
0390131b 3550
ac27a0ec
DK
3551#ifdef CONFIG_QUOTA
3552 /* Release old quota file names */
3553 for (i = 0; i < MAXQUOTAS; i++)
3554 if (old_opts.s_qf_names[i] &&
3555 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3556 kfree(old_opts.s_qf_names[i]);
3557#endif
bbd6851a 3558 unlock_super(sb);
337eb00a 3559 unlock_kernel();
ac27a0ec 3560 return 0;
0b8e58a1 3561
ac27a0ec
DK
3562restore_opts:
3563 sb->s_flags = old_sb_flags;
3564 sbi->s_mount_opt = old_opts.s_mount_opt;
3565 sbi->s_resuid = old_opts.s_resuid;
3566 sbi->s_resgid = old_opts.s_resgid;
3567 sbi->s_commit_interval = old_opts.s_commit_interval;
30773840
TT
3568 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3569 sbi->s_max_batch_time = old_opts.s_max_batch_time;
ac27a0ec
DK
3570#ifdef CONFIG_QUOTA
3571 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3572 for (i = 0; i < MAXQUOTAS; i++) {
3573 if (sbi->s_qf_names[i] &&
3574 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3575 kfree(sbi->s_qf_names[i]);
3576 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3577 }
3578#endif
bbd6851a 3579 unlock_super(sb);
337eb00a 3580 unlock_kernel();
ac27a0ec
DK
3581 return err;
3582}
3583
2b2d6d01 3584static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
ac27a0ec
DK
3585{
3586 struct super_block *sb = dentry->d_sb;
617ba13b
MC
3587 struct ext4_sb_info *sbi = EXT4_SB(sb);
3588 struct ext4_super_block *es = sbi->s_es;
960cc398 3589 u64 fsid;
ac27a0ec 3590
5e70030d
BP
3591 if (test_opt(sb, MINIX_DF)) {
3592 sbi->s_overhead_last = 0;
6bc9feff 3593 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
8df9675f 3594 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
5e70030d 3595 ext4_fsblk_t overhead = 0;
ac27a0ec
DK
3596
3597 /*
5e70030d
BP
3598 * Compute the overhead (FS structures). This is constant
3599 * for a given filesystem unless the number of block groups
3600 * changes so we cache the previous value until it does.
ac27a0ec
DK
3601 */
3602
3603 /*
3604 * All of the blocks before first_data_block are
3605 * overhead
3606 */
3607 overhead = le32_to_cpu(es->s_first_data_block);
3608
3609 /*
3610 * Add the overhead attributed to the superblock and
3611 * block group descriptors. If the sparse superblocks
3612 * feature is turned on, then not all groups have this.
3613 */
3614 for (i = 0; i < ngroups; i++) {
617ba13b
MC
3615 overhead += ext4_bg_has_super(sb, i) +
3616 ext4_bg_num_gdb(sb, i);
ac27a0ec
DK
3617 cond_resched();
3618 }
3619
3620 /*
3621 * Every block group has an inode bitmap, a block
3622 * bitmap, and an inode table.
3623 */
5e70030d
BP
3624 overhead += ngroups * (2 + sbi->s_itb_per_group);
3625 sbi->s_overhead_last = overhead;
3626 smp_wmb();
6bc9feff 3627 sbi->s_blocks_last = ext4_blocks_count(es);
ac27a0ec
DK
3628 }
3629
617ba13b 3630 buf->f_type = EXT4_SUPER_MAGIC;
ac27a0ec 3631 buf->f_bsize = sb->s_blocksize;
5e70030d 3632 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
6bc6e63f
AK
3633 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3634 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
308ba3ec 3635 ext4_free_blocks_count_set(es, buf->f_bfree);
bd81d8ee
LV
3636 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3637 if (buf->f_bfree < ext4_r_blocks_count(es))
ac27a0ec
DK
3638 buf->f_bavail = 0;
3639 buf->f_files = le32_to_cpu(es->s_inodes_count);
52d9f3b4 3640 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5e70030d 3641 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
617ba13b 3642 buf->f_namelen = EXT4_NAME_LEN;
960cc398
PE
3643 fsid = le64_to_cpup((void *)es->s_uuid) ^
3644 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3645 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3646 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
0b8e58a1 3647
ac27a0ec
DK
3648 return 0;
3649}
3650
0b8e58a1
AD
3651/* Helper function for writing quotas on sync - we need to start transaction
3652 * before quota file is locked for write. Otherwise the are possible deadlocks:
ac27a0ec 3653 * Process 1 Process 2
617ba13b 3654 * ext4_create() quota_sync()
a269eb18
JK
3655 * jbd2_journal_start() write_dquot()
3656 * vfs_dq_init() down(dqio_mutex)
dab291af 3657 * down(dqio_mutex) jbd2_journal_start()
ac27a0ec
DK
3658 *
3659 */
3660
3661#ifdef CONFIG_QUOTA
3662
3663static inline struct inode *dquot_to_inode(struct dquot *dquot)
3664{
3665 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3666}
3667
617ba13b 3668static int ext4_write_dquot(struct dquot *dquot)
ac27a0ec
DK
3669{
3670 int ret, err;
3671 handle_t *handle;
3672 struct inode *inode;
3673
3674 inode = dquot_to_inode(dquot);
617ba13b 3675 handle = ext4_journal_start(inode,
0b8e58a1 3676 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
3677 if (IS_ERR(handle))
3678 return PTR_ERR(handle);
3679 ret = dquot_commit(dquot);
617ba13b 3680 err = ext4_journal_stop(handle);
ac27a0ec
DK
3681 if (!ret)
3682 ret = err;
3683 return ret;
3684}
3685
617ba13b 3686static int ext4_acquire_dquot(struct dquot *dquot)
ac27a0ec
DK
3687{
3688 int ret, err;
3689 handle_t *handle;
3690
617ba13b 3691 handle = ext4_journal_start(dquot_to_inode(dquot),
0b8e58a1 3692 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
3693 if (IS_ERR(handle))
3694 return PTR_ERR(handle);
3695 ret = dquot_acquire(dquot);
617ba13b 3696 err = ext4_journal_stop(handle);
ac27a0ec
DK
3697 if (!ret)
3698 ret = err;
3699 return ret;
3700}
3701
617ba13b 3702static int ext4_release_dquot(struct dquot *dquot)
ac27a0ec
DK
3703{
3704 int ret, err;
3705 handle_t *handle;
3706
617ba13b 3707 handle = ext4_journal_start(dquot_to_inode(dquot),
0b8e58a1 3708 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
9c3013e9
JK
3709 if (IS_ERR(handle)) {
3710 /* Release dquot anyway to avoid endless cycle in dqput() */
3711 dquot_release(dquot);
ac27a0ec 3712 return PTR_ERR(handle);
9c3013e9 3713 }
ac27a0ec 3714 ret = dquot_release(dquot);
617ba13b 3715 err = ext4_journal_stop(handle);
ac27a0ec
DK
3716 if (!ret)
3717 ret = err;
3718 return ret;
3719}
3720
617ba13b 3721static int ext4_mark_dquot_dirty(struct dquot *dquot)
ac27a0ec 3722{
2c8be6b2 3723 /* Are we journaling quotas? */
617ba13b
MC
3724 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3725 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
ac27a0ec 3726 dquot_mark_dquot_dirty(dquot);
617ba13b 3727 return ext4_write_dquot(dquot);
ac27a0ec
DK
3728 } else {
3729 return dquot_mark_dquot_dirty(dquot);
3730 }
3731}
3732
617ba13b 3733static int ext4_write_info(struct super_block *sb, int type)
ac27a0ec
DK
3734{
3735 int ret, err;
3736 handle_t *handle;
3737
3738 /* Data block + inode block */
617ba13b 3739 handle = ext4_journal_start(sb->s_root->d_inode, 2);
ac27a0ec
DK
3740 if (IS_ERR(handle))
3741 return PTR_ERR(handle);
3742 ret = dquot_commit_info(sb, type);
617ba13b 3743 err = ext4_journal_stop(handle);
ac27a0ec
DK
3744 if (!ret)
3745 ret = err;
3746 return ret;
3747}
3748
3749/*
3750 * Turn on quotas during mount time - we need to find
3751 * the quota file and such...
3752 */
617ba13b 3753static int ext4_quota_on_mount(struct super_block *sb, int type)
ac27a0ec 3754{
617ba13b 3755 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
0b8e58a1 3756 EXT4_SB(sb)->s_jquota_fmt, type);
ac27a0ec
DK
3757}
3758
3759/*
3760 * Standard function to be called on quota_on
3761 */
617ba13b 3762static int ext4_quota_on(struct super_block *sb, int type, int format_id,
8264613d 3763 char *name, int remount)
ac27a0ec
DK
3764{
3765 int err;
8264613d 3766 struct path path;
ac27a0ec
DK
3767
3768 if (!test_opt(sb, QUOTA))
3769 return -EINVAL;
8264613d 3770 /* When remounting, no checks are needed and in fact, name is NULL */
0623543b 3771 if (remount)
8264613d 3772 return vfs_quota_on(sb, type, format_id, name, remount);
0623543b 3773
8264613d 3774 err = kern_path(name, LOOKUP_FOLLOW, &path);
ac27a0ec
DK
3775 if (err)
3776 return err;
0623543b 3777
ac27a0ec 3778 /* Quotafile not on the same filesystem? */
8264613d
AV
3779 if (path.mnt->mnt_sb != sb) {
3780 path_put(&path);
ac27a0ec
DK
3781 return -EXDEV;
3782 }
0623543b
JK
3783 /* Journaling quota? */
3784 if (EXT4_SB(sb)->s_qf_names[type]) {
2b2d6d01 3785 /* Quotafile not in fs root? */
8264613d 3786 if (path.dentry->d_parent != sb->s_root)
b31e1552
ES
3787 ext4_msg(sb, KERN_WARNING,
3788 "Quota file not on filesystem root. "
3789 "Journaled quota will not work");
2b2d6d01 3790 }
0623543b
JK
3791
3792 /*
3793 * When we journal data on quota file, we have to flush journal to see
3794 * all updates to the file when we bypass pagecache...
3795 */
0390131b
FM
3796 if (EXT4_SB(sb)->s_journal &&
3797 ext4_should_journal_data(path.dentry->d_inode)) {
0623543b
JK
3798 /*
3799 * We don't need to lock updates but journal_flush() could
3800 * otherwise be livelocked...
3801 */
3802 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
7ffe1ea8 3803 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
0623543b 3804 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
7ffe1ea8 3805 if (err) {
8264613d 3806 path_put(&path);
7ffe1ea8
HK
3807 return err;
3808 }
0623543b
JK
3809 }
3810
8264613d
AV
3811 err = vfs_quota_on_path(sb, type, format_id, &path);
3812 path_put(&path);
77e69dac 3813 return err;
ac27a0ec
DK
3814}
3815
3816/* Read data from quotafile - avoid pagecache and such because we cannot afford
3817 * acquiring the locks... As quota files are never truncated and quota code
3818 * itself serializes the operations (and noone else should touch the files)
3819 * we don't have to be afraid of races */
617ba13b 3820static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec
DK
3821 size_t len, loff_t off)
3822{
3823 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 3824 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
3825 int err = 0;
3826 int offset = off & (sb->s_blocksize - 1);
3827 int tocopy;
3828 size_t toread;
3829 struct buffer_head *bh;
3830 loff_t i_size = i_size_read(inode);
3831
3832 if (off > i_size)
3833 return 0;
3834 if (off+len > i_size)
3835 len = i_size-off;
3836 toread = len;
3837 while (toread > 0) {
3838 tocopy = sb->s_blocksize - offset < toread ?
3839 sb->s_blocksize - offset : toread;
617ba13b 3840 bh = ext4_bread(NULL, inode, blk, 0, &err);
ac27a0ec
DK
3841 if (err)
3842 return err;
3843 if (!bh) /* A hole? */
3844 memset(data, 0, tocopy);
3845 else
3846 memcpy(data, bh->b_data+offset, tocopy);
3847 brelse(bh);
3848 offset = 0;
3849 toread -= tocopy;
3850 data += tocopy;
3851 blk++;
3852 }
3853 return len;
3854}
3855
3856/* Write to quotafile (we know the transaction is already started and has
3857 * enough credits) */
617ba13b 3858static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec
DK
3859 const char *data, size_t len, loff_t off)
3860{
3861 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 3862 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
3863 int err = 0;
3864 int offset = off & (sb->s_blocksize - 1);
3865 int tocopy;
617ba13b 3866 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
ac27a0ec
DK
3867 size_t towrite = len;
3868 struct buffer_head *bh;
3869 handle_t *handle = journal_current_handle();
3870
0390131b 3871 if (EXT4_SB(sb)->s_journal && !handle) {
b31e1552
ES
3872 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
3873 " cancelled because transaction is not started",
9c3013e9
JK
3874 (unsigned long long)off, (unsigned long long)len);
3875 return -EIO;
3876 }
ac27a0ec
DK
3877 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3878 while (towrite > 0) {
3879 tocopy = sb->s_blocksize - offset < towrite ?
3880 sb->s_blocksize - offset : towrite;
617ba13b 3881 bh = ext4_bread(handle, inode, blk, 1, &err);
ac27a0ec
DK
3882 if (!bh)
3883 goto out;
3884 if (journal_quota) {
617ba13b 3885 err = ext4_journal_get_write_access(handle, bh);
ac27a0ec
DK
3886 if (err) {
3887 brelse(bh);
3888 goto out;
3889 }
3890 }
3891 lock_buffer(bh);
3892 memcpy(bh->b_data+offset, data, tocopy);
3893 flush_dcache_page(bh->b_page);
3894 unlock_buffer(bh);
3895 if (journal_quota)
0390131b 3896 err = ext4_handle_dirty_metadata(handle, NULL, bh);
ac27a0ec
DK
3897 else {
3898 /* Always do at least ordered writes for quotas */
678aaf48 3899 err = ext4_jbd2_file_inode(handle, inode);
ac27a0ec
DK
3900 mark_buffer_dirty(bh);
3901 }
3902 brelse(bh);
3903 if (err)
3904 goto out;
3905 offset = 0;
3906 towrite -= tocopy;
3907 data += tocopy;
3908 blk++;
3909 }
3910out:
4d04e4fb
JK
3911 if (len == towrite) {
3912 mutex_unlock(&inode->i_mutex);
ac27a0ec 3913 return err;
4d04e4fb 3914 }
ac27a0ec
DK
3915 if (inode->i_size < off+len-towrite) {
3916 i_size_write(inode, off+len-towrite);
617ba13b 3917 EXT4_I(inode)->i_disksize = inode->i_size;
ac27a0ec 3918 }
ac27a0ec 3919 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
617ba13b 3920 ext4_mark_inode_dirty(handle, inode);
ac27a0ec
DK
3921 mutex_unlock(&inode->i_mutex);
3922 return len - towrite;
3923}
3924
3925#endif
3926
0b8e58a1
AD
3927static int ext4_get_sb(struct file_system_type *fs_type, int flags,
3928 const char *dev_name, void *data, struct vfsmount *mnt)
ac27a0ec 3929{
0b8e58a1 3930 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
ac27a0ec
DK
3931}
3932
03010a33
TT
3933static struct file_system_type ext4_fs_type = {
3934 .owner = THIS_MODULE,
3935 .name = "ext4",
3936 .get_sb = ext4_get_sb,
3937 .kill_sb = kill_block_super,
3938 .fs_flags = FS_REQUIRES_DEV,
3939};
3940
3941#ifdef CONFIG_EXT4DEV_COMPAT
0b8e58a1
AD
3942static int ext4dev_get_sb(struct file_system_type *fs_type, int flags,
3943 const char *dev_name, void *data,struct vfsmount *mnt)
03010a33 3944{
b31e1552
ES
3945 printk(KERN_WARNING "EXT4-fs (%s): Update your userspace programs "
3946 "to mount using ext4\n", dev_name);
3947 printk(KERN_WARNING "EXT4-fs (%s): ext4dev backwards compatibility "
3948 "will go away by 2.6.31\n", dev_name);
0b8e58a1 3949 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
03010a33
TT
3950}
3951
617ba13b 3952static struct file_system_type ext4dev_fs_type = {
ac27a0ec 3953 .owner = THIS_MODULE,
617ba13b 3954 .name = "ext4dev",
03010a33 3955 .get_sb = ext4dev_get_sb,
ac27a0ec
DK
3956 .kill_sb = kill_block_super,
3957 .fs_flags = FS_REQUIRES_DEV,
3958};
03010a33
TT
3959MODULE_ALIAS("ext4dev");
3960#endif
ac27a0ec 3961
617ba13b 3962static int __init init_ext4_fs(void)
ac27a0ec 3963{
c9de560d
AT
3964 int err;
3965
6fd058f7
TT
3966 err = init_ext4_system_zone();
3967 if (err)
3968 return err;
3197ebdb
TT
3969 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
3970 if (!ext4_kset)
6fd058f7 3971 goto out4;
9f6200bb 3972 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
c9de560d 3973 err = init_ext4_mballoc();
ac27a0ec 3974 if (err)
6fd058f7 3975 goto out3;
c9de560d
AT
3976
3977 err = init_ext4_xattr();
3978 if (err)
3979 goto out2;
ac27a0ec
DK
3980 err = init_inodecache();
3981 if (err)
3982 goto out1;
03010a33 3983 err = register_filesystem(&ext4_fs_type);
ac27a0ec
DK
3984 if (err)
3985 goto out;
03010a33
TT
3986#ifdef CONFIG_EXT4DEV_COMPAT
3987 err = register_filesystem(&ext4dev_fs_type);
3988 if (err) {
3989 unregister_filesystem(&ext4_fs_type);
3990 goto out;
3991 }
3992#endif
ac27a0ec
DK
3993 return 0;
3994out:
3995 destroy_inodecache();
3996out1:
617ba13b 3997 exit_ext4_xattr();
c9de560d
AT
3998out2:
3999 exit_ext4_mballoc();
6fd058f7
TT
4000out3:
4001 remove_proc_entry("fs/ext4", NULL);
4002 kset_unregister(ext4_kset);
4003out4:
4004 exit_ext4_system_zone();
ac27a0ec
DK
4005 return err;
4006}
4007
617ba13b 4008static void __exit exit_ext4_fs(void)
ac27a0ec 4009{
03010a33
TT
4010 unregister_filesystem(&ext4_fs_type);
4011#ifdef CONFIG_EXT4DEV_COMPAT
617ba13b 4012 unregister_filesystem(&ext4dev_fs_type);
03010a33 4013#endif
ac27a0ec 4014 destroy_inodecache();
617ba13b 4015 exit_ext4_xattr();
c9de560d 4016 exit_ext4_mballoc();
9f6200bb 4017 remove_proc_entry("fs/ext4", NULL);
3197ebdb 4018 kset_unregister(ext4_kset);
6fd058f7 4019 exit_ext4_system_zone();
ac27a0ec
DK
4020}
4021
4022MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
83982b6f 4023MODULE_DESCRIPTION("Fourth Extended Filesystem");
ac27a0ec 4024MODULE_LICENSE("GPL");
617ba13b
MC
4025module_init(init_ext4_fs)
4026module_exit(exit_ext4_fs)