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