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