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