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