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