ext4: Make extents code sanely handle on-disk corruption
[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>
617ba13b 24#include <linux/ext4_fs.h>
dab291af 25#include <linux/ext4_jbd2.h>
ac27a0ec
DK
26#include <linux/slab.h>
27#include <linux/init.h>
28#include <linux/blkdev.h>
29#include <linux/parser.h>
30#include <linux/smp_lock.h>
31#include <linux/buffer_head.h>
a5694255 32#include <linux/exportfs.h>
ac27a0ec
DK
33#include <linux/vfs.h>
34#include <linux/random.h>
35#include <linux/mount.h>
36#include <linux/namei.h>
37#include <linux/quotaops.h>
38#include <linux/seq_file.h>
39
40#include <asm/uaccess.h>
41
42#include "xattr.h"
43#include "acl.h"
44#include "namei.h"
45
617ba13b 46static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
ac27a0ec 47 unsigned long journal_devnum);
617ba13b 48static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
ac27a0ec 49 unsigned int);
617ba13b
MC
50static void ext4_commit_super (struct super_block * sb,
51 struct ext4_super_block * es,
ac27a0ec 52 int sync);
617ba13b
MC
53static void ext4_mark_recovery_complete(struct super_block * sb,
54 struct ext4_super_block * es);
55static void ext4_clear_journal_err(struct super_block * sb,
56 struct ext4_super_block * es);
57static int ext4_sync_fs(struct super_block *sb, int wait);
58static const char *ext4_decode_error(struct super_block * sb, int errno,
ac27a0ec 59 char nbuf[16]);
617ba13b
MC
60static int ext4_remount (struct super_block * sb, int * flags, char * data);
61static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
62static void ext4_unlockfs(struct super_block *sb);
63static void ext4_write_super (struct super_block * sb);
64static void ext4_write_super_lockfs(struct super_block *sb);
ac27a0ec 65
bd81d8ee 66
8fadc143
AR
67ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
68 struct ext4_group_desc *bg)
bd81d8ee
LV
69{
70 return le32_to_cpu(bg->bg_block_bitmap) |
8fadc143
AR
71 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
72 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
bd81d8ee
LV
73}
74
8fadc143
AR
75ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
76 struct ext4_group_desc *bg)
bd81d8ee
LV
77{
78 return le32_to_cpu(bg->bg_inode_bitmap) |
8fadc143
AR
79 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
80 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
bd81d8ee
LV
81}
82
8fadc143
AR
83ext4_fsblk_t ext4_inode_table(struct super_block *sb,
84 struct ext4_group_desc *bg)
bd81d8ee
LV
85{
86 return le32_to_cpu(bg->bg_inode_table) |
8fadc143
AR
87 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
88 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
bd81d8ee
LV
89}
90
8fadc143
AR
91void ext4_block_bitmap_set(struct super_block *sb,
92 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee
LV
93{
94 bg->bg_block_bitmap = cpu_to_le32((u32)blk);
8fadc143
AR
95 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
96 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
97}
98
8fadc143
AR
99void ext4_inode_bitmap_set(struct super_block *sb,
100 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee
LV
101{
102 bg->bg_inode_bitmap = cpu_to_le32((u32)blk);
8fadc143
AR
103 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
104 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
105}
106
8fadc143
AR
107void ext4_inode_table_set(struct super_block *sb,
108 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee
LV
109{
110 bg->bg_inode_table = cpu_to_le32((u32)blk);
8fadc143
AR
111 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
112 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
113}
114
ac27a0ec 115/*
dab291af 116 * Wrappers for jbd2_journal_start/end.
ac27a0ec
DK
117 *
118 * The only special thing we need to do here is to make sure that all
119 * journal_end calls result in the superblock being marked dirty, so
120 * that sync() will call the filesystem's write_super callback if
121 * appropriate.
122 */
617ba13b 123handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
ac27a0ec
DK
124{
125 journal_t *journal;
126
127 if (sb->s_flags & MS_RDONLY)
128 return ERR_PTR(-EROFS);
129
130 /* Special case here: if the journal has aborted behind our
131 * backs (eg. EIO in the commit thread), then we still need to
132 * take the FS itself readonly cleanly. */
617ba13b 133 journal = EXT4_SB(sb)->s_journal;
ac27a0ec 134 if (is_journal_aborted(journal)) {
617ba13b 135 ext4_abort(sb, __FUNCTION__,
ac27a0ec
DK
136 "Detected aborted journal");
137 return ERR_PTR(-EROFS);
138 }
139
dab291af 140 return jbd2_journal_start(journal, nblocks);
ac27a0ec
DK
141}
142
143/*
144 * The only special thing we need to do here is to make sure that all
dab291af 145 * jbd2_journal_stop calls result in the superblock being marked dirty, so
ac27a0ec
DK
146 * that sync() will call the filesystem's write_super callback if
147 * appropriate.
148 */
617ba13b 149int __ext4_journal_stop(const char *where, handle_t *handle)
ac27a0ec
DK
150{
151 struct super_block *sb;
152 int err;
153 int rc;
154
155 sb = handle->h_transaction->t_journal->j_private;
156 err = handle->h_err;
dab291af 157 rc = jbd2_journal_stop(handle);
ac27a0ec
DK
158
159 if (!err)
160 err = rc;
161 if (err)
617ba13b 162 __ext4_std_error(sb, where, err);
ac27a0ec
DK
163 return err;
164}
165
617ba13b 166void ext4_journal_abort_handle(const char *caller, const char *err_fn,
ac27a0ec
DK
167 struct buffer_head *bh, handle_t *handle, int err)
168{
169 char nbuf[16];
617ba13b 170 const char *errstr = ext4_decode_error(NULL, err, nbuf);
ac27a0ec
DK
171
172 if (bh)
173 BUFFER_TRACE(bh, "abort");
174
175 if (!handle->h_err)
176 handle->h_err = err;
177
178 if (is_handle_aborted(handle))
179 return;
180
181 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
182 caller, errstr, err_fn);
183
dab291af 184 jbd2_journal_abort_handle(handle);
ac27a0ec
DK
185}
186
187/* Deal with the reporting of failure conditions on a filesystem such as
188 * inconsistencies detected or read IO failures.
189 *
190 * On ext2, we can store the error state of the filesystem in the
617ba13b 191 * superblock. That is not possible on ext4, because we may have other
ac27a0ec
DK
192 * write ordering constraints on the superblock which prevent us from
193 * writing it out straight away; and given that the journal is about to
194 * be aborted, we can't rely on the current, or future, transactions to
195 * write out the superblock safely.
196 *
dab291af 197 * We'll just use the jbd2_journal_abort() error code to record an error in
ac27a0ec
DK
198 * the journal instead. On recovery, the journal will compain about
199 * that error until we've noted it down and cleared it.
200 */
201
617ba13b 202static void ext4_handle_error(struct super_block *sb)
ac27a0ec 203{
617ba13b 204 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
ac27a0ec 205
617ba13b
MC
206 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
207 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
ac27a0ec
DK
208
209 if (sb->s_flags & MS_RDONLY)
210 return;
211
212 if (!test_opt (sb, ERRORS_CONT)) {
617ba13b 213 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 214
617ba13b 215 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
ac27a0ec 216 if (journal)
dab291af 217 jbd2_journal_abort(journal, -EIO);
ac27a0ec
DK
218 }
219 if (test_opt (sb, ERRORS_RO)) {
220 printk (KERN_CRIT "Remounting filesystem read-only\n");
221 sb->s_flags |= MS_RDONLY;
222 }
617ba13b 223 ext4_commit_super(sb, es, 1);
ac27a0ec 224 if (test_opt(sb, ERRORS_PANIC))
617ba13b 225 panic("EXT4-fs (device %s): panic forced after error\n",
ac27a0ec
DK
226 sb->s_id);
227}
228
617ba13b 229void ext4_error (struct super_block * sb, const char * function,
ac27a0ec
DK
230 const char * fmt, ...)
231{
232 va_list args;
233
234 va_start(args, fmt);
617ba13b 235 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
ac27a0ec
DK
236 vprintk(fmt, args);
237 printk("\n");
238 va_end(args);
239
617ba13b 240 ext4_handle_error(sb);
ac27a0ec
DK
241}
242
617ba13b 243static const char *ext4_decode_error(struct super_block * sb, int errno,
ac27a0ec
DK
244 char nbuf[16])
245{
246 char *errstr = NULL;
247
248 switch (errno) {
249 case -EIO:
250 errstr = "IO failure";
251 break;
252 case -ENOMEM:
253 errstr = "Out of memory";
254 break;
255 case -EROFS:
dab291af 256 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
ac27a0ec
DK
257 errstr = "Journal has aborted";
258 else
259 errstr = "Readonly filesystem";
260 break;
261 default:
262 /* If the caller passed in an extra buffer for unknown
263 * errors, textualise them now. Else we just return
264 * NULL. */
265 if (nbuf) {
266 /* Check for truncated error codes... */
267 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
268 errstr = nbuf;
269 }
270 break;
271 }
272
273 return errstr;
274}
275
617ba13b 276/* __ext4_std_error decodes expected errors from journaling functions
ac27a0ec
DK
277 * automatically and invokes the appropriate error response. */
278
617ba13b 279void __ext4_std_error (struct super_block * sb, const char * function,
ac27a0ec
DK
280 int errno)
281{
282 char nbuf[16];
283 const char *errstr;
284
285 /* Special case: if the error is EROFS, and we're not already
286 * inside a transaction, then there's really no point in logging
287 * an error. */
288 if (errno == -EROFS && journal_current_handle() == NULL &&
289 (sb->s_flags & MS_RDONLY))
290 return;
291
617ba13b
MC
292 errstr = ext4_decode_error(sb, errno, nbuf);
293 printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
ac27a0ec
DK
294 sb->s_id, function, errstr);
295
617ba13b 296 ext4_handle_error(sb);
ac27a0ec
DK
297}
298
299/*
617ba13b 300 * ext4_abort is a much stronger failure handler than ext4_error. The
ac27a0ec
DK
301 * abort function may be used to deal with unrecoverable failures such
302 * as journal IO errors or ENOMEM at a critical moment in log management.
303 *
304 * We unconditionally force the filesystem into an ABORT|READONLY state,
305 * unless the error response on the fs has been set to panic in which
306 * case we take the easy way out and panic immediately.
307 */
308
617ba13b 309void ext4_abort (struct super_block * sb, const char * function,
ac27a0ec
DK
310 const char * fmt, ...)
311{
312 va_list args;
313
617ba13b 314 printk (KERN_CRIT "ext4_abort called.\n");
ac27a0ec
DK
315
316 va_start(args, fmt);
617ba13b 317 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
ac27a0ec
DK
318 vprintk(fmt, args);
319 printk("\n");
320 va_end(args);
321
322 if (test_opt(sb, ERRORS_PANIC))
617ba13b 323 panic("EXT4-fs panic from previous error\n");
ac27a0ec
DK
324
325 if (sb->s_flags & MS_RDONLY)
326 return;
327
328 printk(KERN_CRIT "Remounting filesystem read-only\n");
617ba13b 329 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
ac27a0ec 330 sb->s_flags |= MS_RDONLY;
617ba13b 331 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
dab291af 332 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
ac27a0ec
DK
333}
334
617ba13b 335void ext4_warning (struct super_block * sb, const char * function,
ac27a0ec
DK
336 const char * fmt, ...)
337{
338 va_list args;
339
340 va_start(args, fmt);
617ba13b 341 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
ac27a0ec
DK
342 sb->s_id, function);
343 vprintk(fmt, args);
344 printk("\n");
345 va_end(args);
346}
347
617ba13b 348void ext4_update_dynamic_rev(struct super_block *sb)
ac27a0ec 349{
617ba13b 350 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
ac27a0ec 351
617ba13b 352 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
ac27a0ec
DK
353 return;
354
617ba13b 355 ext4_warning(sb, __FUNCTION__,
ac27a0ec
DK
356 "updating to rev %d because of new feature flag, "
357 "running e2fsck is recommended",
617ba13b 358 EXT4_DYNAMIC_REV);
ac27a0ec 359
617ba13b
MC
360 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
361 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
362 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
ac27a0ec
DK
363 /* leave es->s_feature_*compat flags alone */
364 /* es->s_uuid will be set by e2fsck if empty */
365
366 /*
367 * The rest of the superblock fields should be zero, and if not it
368 * means they are likely already in use, so leave them alone. We
369 * can leave it up to e2fsck to clean up any inconsistencies there.
370 */
371}
372
373/*
374 * Open the external journal device
375 */
617ba13b 376static struct block_device *ext4_blkdev_get(dev_t dev)
ac27a0ec
DK
377{
378 struct block_device *bdev;
379 char b[BDEVNAME_SIZE];
380
381 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
382 if (IS_ERR(bdev))
383 goto fail;
384 return bdev;
385
386fail:
617ba13b 387 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
ac27a0ec
DK
388 __bdevname(dev, b), PTR_ERR(bdev));
389 return NULL;
390}
391
392/*
393 * Release the journal device
394 */
617ba13b 395static int ext4_blkdev_put(struct block_device *bdev)
ac27a0ec
DK
396{
397 bd_release(bdev);
398 return blkdev_put(bdev);
399}
400
617ba13b 401static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
ac27a0ec
DK
402{
403 struct block_device *bdev;
404 int ret = -ENODEV;
405
406 bdev = sbi->journal_bdev;
407 if (bdev) {
617ba13b 408 ret = ext4_blkdev_put(bdev);
ac27a0ec
DK
409 sbi->journal_bdev = NULL;
410 }
411 return ret;
412}
413
414static inline struct inode *orphan_list_entry(struct list_head *l)
415{
617ba13b 416 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
ac27a0ec
DK
417}
418
617ba13b 419static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
ac27a0ec
DK
420{
421 struct list_head *l;
422
423 printk(KERN_ERR "sb orphan head is %d\n",
424 le32_to_cpu(sbi->s_es->s_last_orphan));
425
426 printk(KERN_ERR "sb_info orphan list:\n");
427 list_for_each(l, &sbi->s_orphan) {
428 struct inode *inode = orphan_list_entry(l);
429 printk(KERN_ERR " "
430 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
431 inode->i_sb->s_id, inode->i_ino, inode,
432 inode->i_mode, inode->i_nlink,
433 NEXT_ORPHAN(inode));
434 }
435}
436
617ba13b 437static void ext4_put_super (struct super_block * sb)
ac27a0ec 438{
617ba13b
MC
439 struct ext4_sb_info *sbi = EXT4_SB(sb);
440 struct ext4_super_block *es = sbi->s_es;
ac27a0ec
DK
441 int i;
442
a86c6181 443 ext4_ext_release(sb);
617ba13b 444 ext4_xattr_put_super(sb);
dab291af 445 jbd2_journal_destroy(sbi->s_journal);
ac27a0ec 446 if (!(sb->s_flags & MS_RDONLY)) {
617ba13b 447 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec
DK
448 es->s_state = cpu_to_le16(sbi->s_mount_state);
449 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
450 mark_buffer_dirty(sbi->s_sbh);
617ba13b 451 ext4_commit_super(sb, es, 1);
ac27a0ec
DK
452 }
453
454 for (i = 0; i < sbi->s_gdb_count; i++)
455 brelse(sbi->s_group_desc[i]);
456 kfree(sbi->s_group_desc);
457 percpu_counter_destroy(&sbi->s_freeblocks_counter);
458 percpu_counter_destroy(&sbi->s_freeinodes_counter);
459 percpu_counter_destroy(&sbi->s_dirs_counter);
460 brelse(sbi->s_sbh);
461#ifdef CONFIG_QUOTA
462 for (i = 0; i < MAXQUOTAS; i++)
463 kfree(sbi->s_qf_names[i]);
464#endif
465
466 /* Debugging code just in case the in-memory inode orphan list
467 * isn't empty. The on-disk one can be non-empty if we've
468 * detected an error and taken the fs readonly, but the
469 * in-memory list had better be clean by this point. */
470 if (!list_empty(&sbi->s_orphan))
471 dump_orphan_list(sb, sbi);
472 J_ASSERT(list_empty(&sbi->s_orphan));
473
f98393a6 474 invalidate_bdev(sb->s_bdev);
ac27a0ec
DK
475 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
476 /*
477 * Invalidate the journal device's buffers. We don't want them
478 * floating about in memory - the physical journal device may
479 * hotswapped, and it breaks the `ro-after' testing code.
480 */
481 sync_blockdev(sbi->journal_bdev);
f98393a6 482 invalidate_bdev(sbi->journal_bdev);
617ba13b 483 ext4_blkdev_remove(sbi);
ac27a0ec
DK
484 }
485 sb->s_fs_info = NULL;
486 kfree(sbi);
487 return;
488}
489
e18b890b 490static struct kmem_cache *ext4_inode_cachep;
ac27a0ec
DK
491
492/*
493 * Called inside transaction, so use GFP_NOFS
494 */
617ba13b 495static struct inode *ext4_alloc_inode(struct super_block *sb)
ac27a0ec 496{
617ba13b 497 struct ext4_inode_info *ei;
ac27a0ec 498
e6b4f8da 499 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
ac27a0ec
DK
500 if (!ei)
501 return NULL;
617ba13b
MC
502#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
503 ei->i_acl = EXT4_ACL_NOT_CACHED;
504 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
ac27a0ec
DK
505#endif
506 ei->i_block_alloc_info = NULL;
507 ei->vfs_inode.i_version = 1;
a86c6181 508 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
ac27a0ec
DK
509 return &ei->vfs_inode;
510}
511
617ba13b 512static void ext4_destroy_inode(struct inode *inode)
ac27a0ec 513{
9f7dd93d
VA
514 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
515 printk("EXT4 Inode %p: orphan list check failed!\n",
516 EXT4_I(inode));
517 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
518 EXT4_I(inode), sizeof(struct ext4_inode_info),
519 true);
520 dump_stack();
521 }
617ba13b 522 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
ac27a0ec
DK
523}
524
e18b890b 525static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
ac27a0ec 526{
617ba13b 527 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
ac27a0ec 528
a35afb83 529 INIT_LIST_HEAD(&ei->i_orphan);
617ba13b 530#ifdef CONFIG_EXT4DEV_FS_XATTR
a35afb83 531 init_rwsem(&ei->xattr_sem);
ac27a0ec 532#endif
a35afb83
CL
533 mutex_init(&ei->truncate_mutex);
534 inode_init_once(&ei->vfs_inode);
ac27a0ec
DK
535}
536
537static int init_inodecache(void)
538{
617ba13b
MC
539 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
540 sizeof(struct ext4_inode_info),
ac27a0ec
DK
541 0, (SLAB_RECLAIM_ACCOUNT|
542 SLAB_MEM_SPREAD),
543 init_once, NULL);
617ba13b 544 if (ext4_inode_cachep == NULL)
ac27a0ec
DK
545 return -ENOMEM;
546 return 0;
547}
548
549static void destroy_inodecache(void)
550{
617ba13b 551 kmem_cache_destroy(ext4_inode_cachep);
ac27a0ec
DK
552}
553
617ba13b 554static void ext4_clear_inode(struct inode *inode)
ac27a0ec 555{
617ba13b
MC
556 struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
557#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
558 if (EXT4_I(inode)->i_acl &&
559 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
560 posix_acl_release(EXT4_I(inode)->i_acl);
561 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
562 }
563 if (EXT4_I(inode)->i_default_acl &&
564 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
565 posix_acl_release(EXT4_I(inode)->i_default_acl);
566 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
ac27a0ec
DK
567 }
568#endif
617ba13b
MC
569 ext4_discard_reservation(inode);
570 EXT4_I(inode)->i_block_alloc_info = NULL;
ac27a0ec
DK
571 if (unlikely(rsv))
572 kfree(rsv);
573}
574
617ba13b 575static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
ac27a0ec
DK
576{
577#if defined(CONFIG_QUOTA)
617ba13b 578 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
579
580 if (sbi->s_jquota_fmt)
581 seq_printf(seq, ",jqfmt=%s",
582 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
583
584 if (sbi->s_qf_names[USRQUOTA])
585 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
586
587 if (sbi->s_qf_names[GRPQUOTA])
588 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
589
617ba13b 590 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
ac27a0ec
DK
591 seq_puts(seq, ",usrquota");
592
617ba13b 593 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
ac27a0ec
DK
594 seq_puts(seq, ",grpquota");
595#endif
596}
597
617ba13b 598static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
ac27a0ec
DK
599{
600 struct super_block *sb = vfs->mnt_sb;
601
617ba13b 602 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
ac27a0ec 603 seq_puts(seq, ",data=journal");
617ba13b 604 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
ac27a0ec 605 seq_puts(seq, ",data=ordered");
617ba13b 606 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
ac27a0ec
DK
607 seq_puts(seq, ",data=writeback");
608
617ba13b 609 ext4_show_quota_options(seq, sb);
ac27a0ec
DK
610
611 return 0;
612}
613
614
617ba13b 615static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
ac27a0ec
DK
616{
617 __u32 *objp = vobjp;
618 unsigned long ino = objp[0];
619 __u32 generation = objp[1];
620 struct inode *inode;
621 struct dentry *result;
622
617ba13b 623 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
ac27a0ec 624 return ERR_PTR(-ESTALE);
617ba13b 625 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
ac27a0ec
DK
626 return ERR_PTR(-ESTALE);
627
628 /* iget isn't really right if the inode is currently unallocated!!
629 *
617ba13b 630 * ext4_read_inode will return a bad_inode if the inode had been
ac27a0ec
DK
631 * deleted, so we should be safe.
632 *
633 * Currently we don't know the generation for parent directory, so
634 * a generation of 0 means "accept any"
635 */
636 inode = iget(sb, ino);
637 if (inode == NULL)
638 return ERR_PTR(-ENOMEM);
639 if (is_bad_inode(inode) ||
640 (generation && inode->i_generation != generation)) {
641 iput(inode);
642 return ERR_PTR(-ESTALE);
643 }
644 /* now to find a dentry.
645 * If possible, get a well-connected one
646 */
647 result = d_alloc_anon(inode);
648 if (!result) {
649 iput(inode);
650 return ERR_PTR(-ENOMEM);
651 }
652 return result;
653}
654
655#ifdef CONFIG_QUOTA
656#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
657#define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
658
617ba13b
MC
659static int ext4_dquot_initialize(struct inode *inode, int type);
660static int ext4_dquot_drop(struct inode *inode);
661static int ext4_write_dquot(struct dquot *dquot);
662static int ext4_acquire_dquot(struct dquot *dquot);
663static int ext4_release_dquot(struct dquot *dquot);
664static int ext4_mark_dquot_dirty(struct dquot *dquot);
665static int ext4_write_info(struct super_block *sb, int type);
666static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
667static int ext4_quota_on_mount(struct super_block *sb, int type);
668static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec 669 size_t len, loff_t off);
617ba13b 670static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec
DK
671 const char *data, size_t len, loff_t off);
672
617ba13b
MC
673static struct dquot_operations ext4_quota_operations = {
674 .initialize = ext4_dquot_initialize,
675 .drop = ext4_dquot_drop,
ac27a0ec
DK
676 .alloc_space = dquot_alloc_space,
677 .alloc_inode = dquot_alloc_inode,
678 .free_space = dquot_free_space,
679 .free_inode = dquot_free_inode,
680 .transfer = dquot_transfer,
617ba13b
MC
681 .write_dquot = ext4_write_dquot,
682 .acquire_dquot = ext4_acquire_dquot,
683 .release_dquot = ext4_release_dquot,
684 .mark_dirty = ext4_mark_dquot_dirty,
685 .write_info = ext4_write_info
ac27a0ec
DK
686};
687
617ba13b
MC
688static struct quotactl_ops ext4_qctl_operations = {
689 .quota_on = ext4_quota_on,
ac27a0ec
DK
690 .quota_off = vfs_quota_off,
691 .quota_sync = vfs_quota_sync,
692 .get_info = vfs_get_dqinfo,
693 .set_info = vfs_set_dqinfo,
694 .get_dqblk = vfs_get_dqblk,
695 .set_dqblk = vfs_set_dqblk
696};
697#endif
698
ee9b6d61 699static const struct super_operations ext4_sops = {
617ba13b
MC
700 .alloc_inode = ext4_alloc_inode,
701 .destroy_inode = ext4_destroy_inode,
702 .read_inode = ext4_read_inode,
703 .write_inode = ext4_write_inode,
704 .dirty_inode = ext4_dirty_inode,
705 .delete_inode = ext4_delete_inode,
706 .put_super = ext4_put_super,
707 .write_super = ext4_write_super,
708 .sync_fs = ext4_sync_fs,
709 .write_super_lockfs = ext4_write_super_lockfs,
710 .unlockfs = ext4_unlockfs,
711 .statfs = ext4_statfs,
712 .remount_fs = ext4_remount,
713 .clear_inode = ext4_clear_inode,
714 .show_options = ext4_show_options,
ac27a0ec 715#ifdef CONFIG_QUOTA
617ba13b
MC
716 .quota_read = ext4_quota_read,
717 .quota_write = ext4_quota_write,
ac27a0ec
DK
718#endif
719};
720
617ba13b
MC
721static struct export_operations ext4_export_ops = {
722 .get_parent = ext4_get_parent,
723 .get_dentry = ext4_get_dentry,
ac27a0ec
DK
724};
725
726enum {
727 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
728 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
729 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
730 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
731 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
732 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
733 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
734 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
735 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
736 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
1e2462f9 737 Opt_grpquota, Opt_extents, Opt_noextents,
ac27a0ec
DK
738};
739
740static match_table_t tokens = {
741 {Opt_bsd_df, "bsddf"},
742 {Opt_minix_df, "minixdf"},
743 {Opt_grpid, "grpid"},
744 {Opt_grpid, "bsdgroups"},
745 {Opt_nogrpid, "nogrpid"},
746 {Opt_nogrpid, "sysvgroups"},
747 {Opt_resgid, "resgid=%u"},
748 {Opt_resuid, "resuid=%u"},
749 {Opt_sb, "sb=%u"},
750 {Opt_err_cont, "errors=continue"},
751 {Opt_err_panic, "errors=panic"},
752 {Opt_err_ro, "errors=remount-ro"},
753 {Opt_nouid32, "nouid32"},
754 {Opt_nocheck, "nocheck"},
755 {Opt_nocheck, "check=none"},
756 {Opt_debug, "debug"},
757 {Opt_oldalloc, "oldalloc"},
758 {Opt_orlov, "orlov"},
759 {Opt_user_xattr, "user_xattr"},
760 {Opt_nouser_xattr, "nouser_xattr"},
761 {Opt_acl, "acl"},
762 {Opt_noacl, "noacl"},
763 {Opt_reservation, "reservation"},
764 {Opt_noreservation, "noreservation"},
765 {Opt_noload, "noload"},
766 {Opt_nobh, "nobh"},
767 {Opt_bh, "bh"},
768 {Opt_commit, "commit=%u"},
769 {Opt_journal_update, "journal=update"},
770 {Opt_journal_inum, "journal=%u"},
771 {Opt_journal_dev, "journal_dev=%u"},
772 {Opt_abort, "abort"},
773 {Opt_data_journal, "data=journal"},
774 {Opt_data_ordered, "data=ordered"},
775 {Opt_data_writeback, "data=writeback"},
776 {Opt_offusrjquota, "usrjquota="},
777 {Opt_usrjquota, "usrjquota=%s"},
778 {Opt_offgrpjquota, "grpjquota="},
779 {Opt_grpjquota, "grpjquota=%s"},
780 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
781 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
782 {Opt_grpquota, "grpquota"},
783 {Opt_noquota, "noquota"},
784 {Opt_quota, "quota"},
785 {Opt_usrquota, "usrquota"},
786 {Opt_barrier, "barrier=%u"},
a86c6181 787 {Opt_extents, "extents"},
1e2462f9 788 {Opt_noextents, "noextents"},
ac27a0ec
DK
789 {Opt_err, NULL},
790 {Opt_resize, "resize"},
791};
792
617ba13b 793static ext4_fsblk_t get_sb_block(void **data)
ac27a0ec 794{
617ba13b 795 ext4_fsblk_t sb_block;
ac27a0ec
DK
796 char *options = (char *) *data;
797
798 if (!options || strncmp(options, "sb=", 3) != 0)
799 return 1; /* Default location */
800 options += 3;
617ba13b 801 /*todo: use simple_strtoll with >32bit ext4 */
ac27a0ec
DK
802 sb_block = simple_strtoul(options, &options, 0);
803 if (*options && *options != ',') {
617ba13b 804 printk("EXT4-fs: Invalid sb specification: %s\n",
ac27a0ec
DK
805 (char *) *data);
806 return 1;
807 }
808 if (*options == ',')
809 options++;
810 *data = (void *) options;
811 return sb_block;
812}
813
814static int parse_options (char *options, struct super_block *sb,
815 unsigned int *inum, unsigned long *journal_devnum,
617ba13b 816 ext4_fsblk_t *n_blocks_count, int is_remount)
ac27a0ec 817{
617ba13b 818 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
819 char * p;
820 substring_t args[MAX_OPT_ARGS];
821 int data_opt = 0;
822 int option;
823#ifdef CONFIG_QUOTA
824 int qtype;
825 char *qname;
826#endif
827
828 if (!options)
829 return 1;
830
831 while ((p = strsep (&options, ",")) != NULL) {
832 int token;
833 if (!*p)
834 continue;
835
836 token = match_token(p, tokens, args);
837 switch (token) {
838 case Opt_bsd_df:
839 clear_opt (sbi->s_mount_opt, MINIX_DF);
840 break;
841 case Opt_minix_df:
842 set_opt (sbi->s_mount_opt, MINIX_DF);
843 break;
844 case Opt_grpid:
845 set_opt (sbi->s_mount_opt, GRPID);
846 break;
847 case Opt_nogrpid:
848 clear_opt (sbi->s_mount_opt, GRPID);
849 break;
850 case Opt_resuid:
851 if (match_int(&args[0], &option))
852 return 0;
853 sbi->s_resuid = option;
854 break;
855 case Opt_resgid:
856 if (match_int(&args[0], &option))
857 return 0;
858 sbi->s_resgid = option;
859 break;
860 case Opt_sb:
861 /* handled by get_sb_block() instead of here */
862 /* *sb_block = match_int(&args[0]); */
863 break;
864 case Opt_err_panic:
865 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
866 clear_opt (sbi->s_mount_opt, ERRORS_RO);
867 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
868 break;
869 case Opt_err_ro:
870 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
871 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
872 set_opt (sbi->s_mount_opt, ERRORS_RO);
873 break;
874 case Opt_err_cont:
875 clear_opt (sbi->s_mount_opt, ERRORS_RO);
876 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
877 set_opt (sbi->s_mount_opt, ERRORS_CONT);
878 break;
879 case Opt_nouid32:
880 set_opt (sbi->s_mount_opt, NO_UID32);
881 break;
882 case Opt_nocheck:
883 clear_opt (sbi->s_mount_opt, CHECK);
884 break;
885 case Opt_debug:
886 set_opt (sbi->s_mount_opt, DEBUG);
887 break;
888 case Opt_oldalloc:
889 set_opt (sbi->s_mount_opt, OLDALLOC);
890 break;
891 case Opt_orlov:
892 clear_opt (sbi->s_mount_opt, OLDALLOC);
893 break;
617ba13b 894#ifdef CONFIG_EXT4DEV_FS_XATTR
ac27a0ec
DK
895 case Opt_user_xattr:
896 set_opt (sbi->s_mount_opt, XATTR_USER);
897 break;
898 case Opt_nouser_xattr:
899 clear_opt (sbi->s_mount_opt, XATTR_USER);
900 break;
901#else
902 case Opt_user_xattr:
903 case Opt_nouser_xattr:
617ba13b 904 printk("EXT4 (no)user_xattr options not supported\n");
ac27a0ec
DK
905 break;
906#endif
617ba13b 907#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
ac27a0ec
DK
908 case Opt_acl:
909 set_opt(sbi->s_mount_opt, POSIX_ACL);
910 break;
911 case Opt_noacl:
912 clear_opt(sbi->s_mount_opt, POSIX_ACL);
913 break;
914#else
915 case Opt_acl:
916 case Opt_noacl:
617ba13b 917 printk("EXT4 (no)acl options not supported\n");
ac27a0ec
DK
918 break;
919#endif
920 case Opt_reservation:
921 set_opt(sbi->s_mount_opt, RESERVATION);
922 break;
923 case Opt_noreservation:
924 clear_opt(sbi->s_mount_opt, RESERVATION);
925 break;
926 case Opt_journal_update:
927 /* @@@ FIXME */
928 /* Eventually we will want to be able to create
929 a journal file here. For now, only allow the
930 user to specify an existing inode to be the
931 journal file. */
932 if (is_remount) {
617ba13b 933 printk(KERN_ERR "EXT4-fs: cannot specify "
ac27a0ec
DK
934 "journal on remount\n");
935 return 0;
936 }
937 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
938 break;
939 case Opt_journal_inum:
940 if (is_remount) {
617ba13b 941 printk(KERN_ERR "EXT4-fs: cannot specify "
ac27a0ec
DK
942 "journal on remount\n");
943 return 0;
944 }
945 if (match_int(&args[0], &option))
946 return 0;
947 *inum = option;
948 break;
949 case Opt_journal_dev:
950 if (is_remount) {
617ba13b 951 printk(KERN_ERR "EXT4-fs: cannot specify "
ac27a0ec
DK
952 "journal on remount\n");
953 return 0;
954 }
955 if (match_int(&args[0], &option))
956 return 0;
957 *journal_devnum = option;
958 break;
959 case Opt_noload:
960 set_opt (sbi->s_mount_opt, NOLOAD);
961 break;
962 case Opt_commit:
963 if (match_int(&args[0], &option))
964 return 0;
965 if (option < 0)
966 return 0;
967 if (option == 0)
968 option = JBD_DEFAULT_MAX_COMMIT_AGE;
969 sbi->s_commit_interval = HZ * option;
970 break;
971 case Opt_data_journal:
617ba13b 972 data_opt = EXT4_MOUNT_JOURNAL_DATA;
ac27a0ec
DK
973 goto datacheck;
974 case Opt_data_ordered:
617ba13b 975 data_opt = EXT4_MOUNT_ORDERED_DATA;
ac27a0ec
DK
976 goto datacheck;
977 case Opt_data_writeback:
617ba13b 978 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
ac27a0ec
DK
979 datacheck:
980 if (is_remount) {
617ba13b 981 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
ac27a0ec
DK
982 != data_opt) {
983 printk(KERN_ERR
617ba13b 984 "EXT4-fs: cannot change data "
ac27a0ec
DK
985 "mode on remount\n");
986 return 0;
987 }
988 } else {
617ba13b 989 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
ac27a0ec
DK
990 sbi->s_mount_opt |= data_opt;
991 }
992 break;
993#ifdef CONFIG_QUOTA
994 case Opt_usrjquota:
995 qtype = USRQUOTA;
996 goto set_qf_name;
997 case Opt_grpjquota:
998 qtype = GRPQUOTA;
999set_qf_name:
1000 if (sb_any_quota_enabled(sb)) {
1001 printk(KERN_ERR
617ba13b 1002 "EXT4-fs: Cannot change journalled "
ac27a0ec
DK
1003 "quota options when quota turned on.\n");
1004 return 0;
1005 }
1006 qname = match_strdup(&args[0]);
1007 if (!qname) {
1008 printk(KERN_ERR
617ba13b 1009 "EXT4-fs: not enough memory for "
ac27a0ec
DK
1010 "storing quotafile name.\n");
1011 return 0;
1012 }
1013 if (sbi->s_qf_names[qtype] &&
1014 strcmp(sbi->s_qf_names[qtype], qname)) {
1015 printk(KERN_ERR
617ba13b 1016 "EXT4-fs: %s quota file already "
ac27a0ec
DK
1017 "specified.\n", QTYPE2NAME(qtype));
1018 kfree(qname);
1019 return 0;
1020 }
1021 sbi->s_qf_names[qtype] = qname;
1022 if (strchr(sbi->s_qf_names[qtype], '/')) {
1023 printk(KERN_ERR
617ba13b 1024 "EXT4-fs: quotafile must be on "
ac27a0ec
DK
1025 "filesystem root.\n");
1026 kfree(sbi->s_qf_names[qtype]);
1027 sbi->s_qf_names[qtype] = NULL;
1028 return 0;
1029 }
1030 set_opt(sbi->s_mount_opt, QUOTA);
1031 break;
1032 case Opt_offusrjquota:
1033 qtype = USRQUOTA;
1034 goto clear_qf_name;
1035 case Opt_offgrpjquota:
1036 qtype = GRPQUOTA;
1037clear_qf_name:
1038 if (sb_any_quota_enabled(sb)) {
617ba13b 1039 printk(KERN_ERR "EXT4-fs: Cannot change "
ac27a0ec
DK
1040 "journalled quota options when "
1041 "quota turned on.\n");
1042 return 0;
1043 }
1044 /*
1045 * The space will be released later when all options
1046 * are confirmed to be correct
1047 */
1048 sbi->s_qf_names[qtype] = NULL;
1049 break;
1050 case Opt_jqfmt_vfsold:
1051 sbi->s_jquota_fmt = QFMT_VFS_OLD;
1052 break;
1053 case Opt_jqfmt_vfsv0:
1054 sbi->s_jquota_fmt = QFMT_VFS_V0;
1055 break;
1056 case Opt_quota:
1057 case Opt_usrquota:
1058 set_opt(sbi->s_mount_opt, QUOTA);
1059 set_opt(sbi->s_mount_opt, USRQUOTA);
1060 break;
1061 case Opt_grpquota:
1062 set_opt(sbi->s_mount_opt, QUOTA);
1063 set_opt(sbi->s_mount_opt, GRPQUOTA);
1064 break;
1065 case Opt_noquota:
1066 if (sb_any_quota_enabled(sb)) {
617ba13b 1067 printk(KERN_ERR "EXT4-fs: Cannot change quota "
ac27a0ec
DK
1068 "options when quota turned on.\n");
1069 return 0;
1070 }
1071 clear_opt(sbi->s_mount_opt, QUOTA);
1072 clear_opt(sbi->s_mount_opt, USRQUOTA);
1073 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1074 break;
1075#else
1076 case Opt_quota:
1077 case Opt_usrquota:
1078 case Opt_grpquota:
1079 case Opt_usrjquota:
1080 case Opt_grpjquota:
1081 case Opt_offusrjquota:
1082 case Opt_offgrpjquota:
1083 case Opt_jqfmt_vfsold:
1084 case Opt_jqfmt_vfsv0:
1085 printk(KERN_ERR
617ba13b 1086 "EXT4-fs: journalled quota options not "
ac27a0ec
DK
1087 "supported.\n");
1088 break;
1089 case Opt_noquota:
1090 break;
1091#endif
1092 case Opt_abort:
1093 set_opt(sbi->s_mount_opt, ABORT);
1094 break;
1095 case Opt_barrier:
1096 if (match_int(&args[0], &option))
1097 return 0;
1098 if (option)
1099 set_opt(sbi->s_mount_opt, BARRIER);
1100 else
1101 clear_opt(sbi->s_mount_opt, BARRIER);
1102 break;
1103 case Opt_ignore:
1104 break;
1105 case Opt_resize:
1106 if (!is_remount) {
617ba13b 1107 printk("EXT4-fs: resize option only available "
ac27a0ec
DK
1108 "for remount\n");
1109 return 0;
1110 }
1111 if (match_int(&args[0], &option) != 0)
1112 return 0;
1113 *n_blocks_count = option;
1114 break;
1115 case Opt_nobh:
1116 set_opt(sbi->s_mount_opt, NOBH);
1117 break;
1118 case Opt_bh:
1119 clear_opt(sbi->s_mount_opt, NOBH);
1120 break;
a86c6181
AT
1121 case Opt_extents:
1122 set_opt (sbi->s_mount_opt, EXTENTS);
1123 break;
1e2462f9
MC
1124 case Opt_noextents:
1125 clear_opt (sbi->s_mount_opt, EXTENTS);
1126 break;
ac27a0ec
DK
1127 default:
1128 printk (KERN_ERR
617ba13b 1129 "EXT4-fs: Unrecognized mount option \"%s\" "
ac27a0ec
DK
1130 "or missing value\n", p);
1131 return 0;
1132 }
1133 }
1134#ifdef CONFIG_QUOTA
1135 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
617ba13b 1136 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
ac27a0ec
DK
1137 sbi->s_qf_names[USRQUOTA])
1138 clear_opt(sbi->s_mount_opt, USRQUOTA);
1139
617ba13b 1140 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
ac27a0ec
DK
1141 sbi->s_qf_names[GRPQUOTA])
1142 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1143
1144 if ((sbi->s_qf_names[USRQUOTA] &&
617ba13b 1145 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
ac27a0ec 1146 (sbi->s_qf_names[GRPQUOTA] &&
617ba13b
MC
1147 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1148 printk(KERN_ERR "EXT4-fs: old and new quota "
ac27a0ec
DK
1149 "format mixing.\n");
1150 return 0;
1151 }
1152
1153 if (!sbi->s_jquota_fmt) {
617ba13b 1154 printk(KERN_ERR "EXT4-fs: journalled quota format "
ac27a0ec
DK
1155 "not specified.\n");
1156 return 0;
1157 }
1158 } else {
1159 if (sbi->s_jquota_fmt) {
617ba13b 1160 printk(KERN_ERR "EXT4-fs: journalled quota format "
ac27a0ec
DK
1161 "specified with no journalling "
1162 "enabled.\n");
1163 return 0;
1164 }
1165 }
1166#endif
1167 return 1;
1168}
1169
617ba13b 1170static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
ac27a0ec
DK
1171 int read_only)
1172{
617ba13b 1173 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
1174 int res = 0;
1175
617ba13b
MC
1176 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1177 printk (KERN_ERR "EXT4-fs warning: revision level too high, "
ac27a0ec
DK
1178 "forcing read-only mode\n");
1179 res = MS_RDONLY;
1180 }
1181 if (read_only)
1182 return res;
617ba13b
MC
1183 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1184 printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
ac27a0ec 1185 "running e2fsck is recommended\n");
617ba13b 1186 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
ac27a0ec 1187 printk (KERN_WARNING
617ba13b 1188 "EXT4-fs warning: mounting fs with errors, "
ac27a0ec
DK
1189 "running e2fsck is recommended\n");
1190 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1191 le16_to_cpu(es->s_mnt_count) >=
1192 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1193 printk (KERN_WARNING
617ba13b 1194 "EXT4-fs warning: maximal mount count reached, "
ac27a0ec
DK
1195 "running e2fsck is recommended\n");
1196 else if (le32_to_cpu(es->s_checkinterval) &&
1197 (le32_to_cpu(es->s_lastcheck) +
1198 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1199 printk (KERN_WARNING
617ba13b 1200 "EXT4-fs warning: checktime reached, "
ac27a0ec
DK
1201 "running e2fsck is recommended\n");
1202#if 0
1203 /* @@@ We _will_ want to clear the valid bit if we find
63f57933
AM
1204 * inconsistencies, to force a fsck at reboot. But for
1205 * a plain journaled filesystem we can keep it set as
1206 * valid forever! :)
1207 */
617ba13b 1208 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
ac27a0ec
DK
1209#endif
1210 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
617ba13b 1211 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
ac27a0ec
DK
1212 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1213 es->s_mtime = cpu_to_le32(get_seconds());
617ba13b
MC
1214 ext4_update_dynamic_rev(sb);
1215 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 1216
617ba13b 1217 ext4_commit_super(sb, es, 1);
ac27a0ec 1218 if (test_opt(sb, DEBUG))
617ba13b 1219 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
ac27a0ec
DK
1220 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1221 sb->s_blocksize,
1222 sbi->s_groups_count,
617ba13b
MC
1223 EXT4_BLOCKS_PER_GROUP(sb),
1224 EXT4_INODES_PER_GROUP(sb),
ac27a0ec
DK
1225 sbi->s_mount_opt);
1226
617ba13b
MC
1227 printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1228 if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
ac27a0ec
DK
1229 char b[BDEVNAME_SIZE];
1230
1231 printk("external journal on %s\n",
617ba13b 1232 bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
ac27a0ec
DK
1233 } else {
1234 printk("internal journal\n");
1235 }
1236 return res;
1237}
1238
1239/* Called at mount-time, super-block is locked */
617ba13b 1240static int ext4_check_descriptors (struct super_block * sb)
ac27a0ec 1241{
617ba13b
MC
1242 struct ext4_sb_info *sbi = EXT4_SB(sb);
1243 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1244 ext4_fsblk_t last_block;
bd81d8ee
LV
1245 ext4_fsblk_t block_bitmap;
1246 ext4_fsblk_t inode_bitmap;
1247 ext4_fsblk_t inode_table;
617ba13b 1248 struct ext4_group_desc * gdp = NULL;
ac27a0ec
DK
1249 int desc_block = 0;
1250 int i;
1251
617ba13b 1252 ext4_debug ("Checking group descriptors");
ac27a0ec
DK
1253
1254 for (i = 0; i < sbi->s_groups_count; i++)
1255 {
1256 if (i == sbi->s_groups_count - 1)
bd81d8ee 1257 last_block = ext4_blocks_count(sbi->s_es) - 1;
ac27a0ec
DK
1258 else
1259 last_block = first_block +
617ba13b 1260 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
ac27a0ec 1261
617ba13b
MC
1262 if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1263 gdp = (struct ext4_group_desc *)
ac27a0ec 1264 sbi->s_group_desc[desc_block++]->b_data;
8fadc143 1265 block_bitmap = ext4_block_bitmap(sb, gdp);
bd81d8ee 1266 if (block_bitmap < first_block || block_bitmap > last_block)
ac27a0ec 1267 {
617ba13b 1268 ext4_error (sb, "ext4_check_descriptors",
ac27a0ec 1269 "Block bitmap for group %d"
2ae02107 1270 " not in group (block %llu)!",
bd81d8ee 1271 i, block_bitmap);
ac27a0ec
DK
1272 return 0;
1273 }
8fadc143 1274 inode_bitmap = ext4_inode_bitmap(sb, gdp);
bd81d8ee 1275 if (inode_bitmap < first_block || inode_bitmap > last_block)
ac27a0ec 1276 {
617ba13b 1277 ext4_error (sb, "ext4_check_descriptors",
ac27a0ec 1278 "Inode bitmap for group %d"
2ae02107 1279 " not in group (block %llu)!",
bd81d8ee 1280 i, inode_bitmap);
ac27a0ec
DK
1281 return 0;
1282 }
8fadc143 1283 inode_table = ext4_inode_table(sb, gdp);
bd81d8ee
LV
1284 if (inode_table < first_block ||
1285 inode_table + sbi->s_itb_per_group > last_block)
ac27a0ec 1286 {
617ba13b 1287 ext4_error (sb, "ext4_check_descriptors",
ac27a0ec 1288 "Inode table for group %d"
2ae02107 1289 " not in group (block %llu)!",
bd81d8ee 1290 i, inode_table);
ac27a0ec
DK
1291 return 0;
1292 }
617ba13b 1293 first_block += EXT4_BLOCKS_PER_GROUP(sb);
0d1ee42f
AR
1294 gdp = (struct ext4_group_desc *)
1295 ((__u8 *)gdp + EXT4_DESC_SIZE(sb));
ac27a0ec
DK
1296 }
1297
bd81d8ee 1298 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
617ba13b 1299 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
ac27a0ec
DK
1300 return 1;
1301}
1302
1303
617ba13b 1304/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
ac27a0ec
DK
1305 * the superblock) which were deleted from all directories, but held open by
1306 * a process at the time of a crash. We walk the list and try to delete these
1307 * inodes at recovery time (only with a read-write filesystem).
1308 *
1309 * In order to keep the orphan inode chain consistent during traversal (in
1310 * case of crash during recovery), we link each inode into the superblock
1311 * orphan list_head and handle it the same way as an inode deletion during
1312 * normal operation (which journals the operations for us).
1313 *
1314 * We only do an iget() and an iput() on each inode, which is very safe if we
1315 * accidentally point at an in-use or already deleted inode. The worst that
1316 * can happen in this case is that we get a "bit already cleared" message from
617ba13b 1317 * ext4_free_inode(). The only reason we would point at a wrong inode is if
ac27a0ec
DK
1318 * e2fsck was run on this filesystem, and it must have already done the orphan
1319 * inode cleanup for us, so we can safely abort without any further action.
1320 */
617ba13b
MC
1321static void ext4_orphan_cleanup (struct super_block * sb,
1322 struct ext4_super_block * es)
ac27a0ec
DK
1323{
1324 unsigned int s_flags = sb->s_flags;
1325 int nr_orphans = 0, nr_truncates = 0;
1326#ifdef CONFIG_QUOTA
1327 int i;
1328#endif
1329 if (!es->s_last_orphan) {
1330 jbd_debug(4, "no orphan inodes to clean up\n");
1331 return;
1332 }
1333
a8f48a95
ES
1334 if (bdev_read_only(sb->s_bdev)) {
1335 printk(KERN_ERR "EXT4-fs: write access "
1336 "unavailable, skipping orphan cleanup.\n");
1337 return;
1338 }
1339
617ba13b 1340 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
ac27a0ec
DK
1341 if (es->s_last_orphan)
1342 jbd_debug(1, "Errors on filesystem, "
1343 "clearing orphan list.\n");
1344 es->s_last_orphan = 0;
1345 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1346 return;
1347 }
1348
1349 if (s_flags & MS_RDONLY) {
617ba13b 1350 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
ac27a0ec
DK
1351 sb->s_id);
1352 sb->s_flags &= ~MS_RDONLY;
1353 }
1354#ifdef CONFIG_QUOTA
1355 /* Needed for iput() to work correctly and not trash data */
1356 sb->s_flags |= MS_ACTIVE;
1357 /* Turn on quotas so that they are updated correctly */
1358 for (i = 0; i < MAXQUOTAS; i++) {
617ba13b
MC
1359 if (EXT4_SB(sb)->s_qf_names[i]) {
1360 int ret = ext4_quota_on_mount(sb, i);
ac27a0ec
DK
1361 if (ret < 0)
1362 printk(KERN_ERR
617ba13b 1363 "EXT4-fs: Cannot turn on journalled "
ac27a0ec
DK
1364 "quota: error %d\n", ret);
1365 }
1366 }
1367#endif
1368
1369 while (es->s_last_orphan) {
1370 struct inode *inode;
1371
1372 if (!(inode =
617ba13b 1373 ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
ac27a0ec
DK
1374 es->s_last_orphan = 0;
1375 break;
1376 }
1377
617ba13b 1378 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
ac27a0ec
DK
1379 DQUOT_INIT(inode);
1380 if (inode->i_nlink) {
1381 printk(KERN_DEBUG
1382 "%s: truncating inode %lu to %Ld bytes\n",
1383 __FUNCTION__, inode->i_ino, inode->i_size);
1384 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1385 inode->i_ino, inode->i_size);
617ba13b 1386 ext4_truncate(inode);
ac27a0ec
DK
1387 nr_truncates++;
1388 } else {
1389 printk(KERN_DEBUG
1390 "%s: deleting unreferenced inode %lu\n",
1391 __FUNCTION__, inode->i_ino);
1392 jbd_debug(2, "deleting unreferenced inode %lu\n",
1393 inode->i_ino);
1394 nr_orphans++;
1395 }
1396 iput(inode); /* The delete magic happens here! */
1397 }
1398
1399#define PLURAL(x) (x), ((x)==1) ? "" : "s"
1400
1401 if (nr_orphans)
617ba13b 1402 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
ac27a0ec
DK
1403 sb->s_id, PLURAL(nr_orphans));
1404 if (nr_truncates)
617ba13b 1405 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
ac27a0ec
DK
1406 sb->s_id, PLURAL(nr_truncates));
1407#ifdef CONFIG_QUOTA
1408 /* Turn quotas off */
1409 for (i = 0; i < MAXQUOTAS; i++) {
1410 if (sb_dqopt(sb)->files[i])
1411 vfs_quota_off(sb, i);
1412 }
1413#endif
1414 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1415}
1416
1417#define log2(n) ffz(~(n))
1418
1419/*
1420 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1421 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1422 * We need to be 1 filesystem block less than the 2^32 sector limit.
1423 */
617ba13b 1424static loff_t ext4_max_size(int bits)
ac27a0ec 1425{
617ba13b 1426 loff_t res = EXT4_NDIR_BLOCKS;
ac27a0ec
DK
1427 /* This constant is calculated to be the largest file size for a
1428 * dense, 4k-blocksize file such that the total number of
1429 * sectors in the file, including data and all indirect blocks,
1430 * does not exceed 2^32. */
1431 const loff_t upper_limit = 0x1ff7fffd000LL;
1432
1433 res += 1LL << (bits-2);
1434 res += 1LL << (2*(bits-2));
1435 res += 1LL << (3*(bits-2));
1436 res <<= bits;
1437 if (res > upper_limit)
1438 res = upper_limit;
1439 return res;
1440}
1441
617ba13b 1442static ext4_fsblk_t descriptor_loc(struct super_block *sb,
70bbb3e0 1443 ext4_fsblk_t logical_sb_block, int nr)
ac27a0ec 1444{
617ba13b 1445 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
1446 unsigned long bg, first_meta_bg;
1447 int has_super = 0;
1448
1449 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1450
617ba13b 1451 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
ac27a0ec 1452 nr < first_meta_bg)
70bbb3e0 1453 return logical_sb_block + nr + 1;
ac27a0ec 1454 bg = sbi->s_desc_per_block * nr;
617ba13b 1455 if (ext4_bg_has_super(sb, bg))
ac27a0ec 1456 has_super = 1;
617ba13b 1457 return (has_super + ext4_group_first_block_no(sb, bg));
ac27a0ec
DK
1458}
1459
1460
617ba13b 1461static int ext4_fill_super (struct super_block *sb, void *data, int silent)
ac27a0ec
DK
1462{
1463 struct buffer_head * bh;
617ba13b
MC
1464 struct ext4_super_block *es = NULL;
1465 struct ext4_sb_info *sbi;
1466 ext4_fsblk_t block;
1467 ext4_fsblk_t sb_block = get_sb_block(&data);
70bbb3e0 1468 ext4_fsblk_t logical_sb_block;
ac27a0ec
DK
1469 unsigned long offset = 0;
1470 unsigned int journal_inum = 0;
1471 unsigned long journal_devnum = 0;
1472 unsigned long def_mount_opts;
1473 struct inode *root;
1474 int blocksize;
1475 int hblock;
1476 int db_count;
1477 int i;
1478 int needs_recovery;
1479 __le32 features;
bd81d8ee 1480 __u64 blocks_count;
ac27a0ec
DK
1481
1482 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1483 if (!sbi)
1484 return -ENOMEM;
1485 sb->s_fs_info = sbi;
1486 sbi->s_mount_opt = 0;
617ba13b
MC
1487 sbi->s_resuid = EXT4_DEF_RESUID;
1488 sbi->s_resgid = EXT4_DEF_RESGID;
ac27a0ec
DK
1489
1490 unlock_kernel();
1491
617ba13b 1492 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
ac27a0ec 1493 if (!blocksize) {
617ba13b 1494 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
ac27a0ec
DK
1495 goto out_fail;
1496 }
1497
1498 /*
617ba13b 1499 * The ext4 superblock will not be buffer aligned for other than 1kB
ac27a0ec
DK
1500 * block sizes. We need to calculate the offset from buffer start.
1501 */
617ba13b 1502 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
70bbb3e0
AM
1503 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1504 offset = do_div(logical_sb_block, blocksize);
ac27a0ec 1505 } else {
70bbb3e0 1506 logical_sb_block = sb_block;
ac27a0ec
DK
1507 }
1508
70bbb3e0 1509 if (!(bh = sb_bread(sb, logical_sb_block))) {
617ba13b 1510 printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
ac27a0ec
DK
1511 goto out_fail;
1512 }
1513 /*
1514 * Note: s_es must be initialized as soon as possible because
617ba13b 1515 * some ext4 macro-instructions depend on its value
ac27a0ec 1516 */
617ba13b 1517 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
ac27a0ec
DK
1518 sbi->s_es = es;
1519 sb->s_magic = le16_to_cpu(es->s_magic);
617ba13b
MC
1520 if (sb->s_magic != EXT4_SUPER_MAGIC)
1521 goto cantfind_ext4;
ac27a0ec
DK
1522
1523 /* Set defaults before we parse the mount options */
1524 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
617ba13b 1525 if (def_mount_opts & EXT4_DEFM_DEBUG)
ac27a0ec 1526 set_opt(sbi->s_mount_opt, DEBUG);
617ba13b 1527 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
ac27a0ec 1528 set_opt(sbi->s_mount_opt, GRPID);
617ba13b 1529 if (def_mount_opts & EXT4_DEFM_UID16)
ac27a0ec 1530 set_opt(sbi->s_mount_opt, NO_UID32);
2e7842b8 1531#ifdef CONFIG_EXT4DEV_FS_XATTR
617ba13b 1532 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
ac27a0ec 1533 set_opt(sbi->s_mount_opt, XATTR_USER);
2e7842b8
HD
1534#endif
1535#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
617ba13b 1536 if (def_mount_opts & EXT4_DEFM_ACL)
ac27a0ec 1537 set_opt(sbi->s_mount_opt, POSIX_ACL);
2e7842b8 1538#endif
617ba13b
MC
1539 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1540 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1541 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1542 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1543 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1544 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
1545
1546 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
ac27a0ec 1547 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
617ba13b 1548 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
ac27a0ec 1549 set_opt(sbi->s_mount_opt, ERRORS_RO);
ceea16bf
DM
1550 else
1551 set_opt(sbi->s_mount_opt, ERRORS_CONT);
ac27a0ec
DK
1552
1553 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1554 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1555
1556 set_opt(sbi->s_mount_opt, RESERVATION);
1557
1e2462f9
MC
1558 /*
1559 * turn on extents feature by default in ext4 filesystem
1560 * User -o noextents to turn it off
1561 */
1562 set_opt(sbi->s_mount_opt, EXTENTS);
1563
ac27a0ec
DK
1564 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1565 NULL, 0))
1566 goto failed_mount;
1567
1568 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
617ba13b 1569 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec 1570
617ba13b
MC
1571 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1572 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1573 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1574 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
ac27a0ec 1575 printk(KERN_WARNING
617ba13b 1576 "EXT4-fs warning: feature flags set on rev 0 fs, "
ac27a0ec
DK
1577 "running e2fsck is recommended\n");
1578 /*
1579 * Check feature flags regardless of the revision level, since we
1580 * previously didn't change the revision level when setting the flags,
1581 * so there is a chance incompat flags are set on a rev 0 filesystem.
1582 */
617ba13b 1583 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
ac27a0ec 1584 if (features) {
617ba13b 1585 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
ac27a0ec
DK
1586 "unsupported optional features (%x).\n",
1587 sb->s_id, le32_to_cpu(features));
1588 goto failed_mount;
1589 }
617ba13b 1590 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
ac27a0ec 1591 if (!(sb->s_flags & MS_RDONLY) && features) {
617ba13b 1592 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
ac27a0ec
DK
1593 "unsupported optional features (%x).\n",
1594 sb->s_id, le32_to_cpu(features));
1595 goto failed_mount;
1596 }
1597 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1598
617ba13b
MC
1599 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1600 blocksize > EXT4_MAX_BLOCK_SIZE) {
ac27a0ec 1601 printk(KERN_ERR
617ba13b 1602 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
ac27a0ec
DK
1603 blocksize, sb->s_id);
1604 goto failed_mount;
1605 }
1606
1607 hblock = bdev_hardsect_size(sb->s_bdev);
1608 if (sb->s_blocksize != blocksize) {
1609 /*
1610 * Make sure the blocksize for the filesystem is larger
1611 * than the hardware sectorsize for the machine.
1612 */
1613 if (blocksize < hblock) {
617ba13b 1614 printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
ac27a0ec
DK
1615 "device blocksize %d.\n", blocksize, hblock);
1616 goto failed_mount;
1617 }
1618
1619 brelse (bh);
1620 sb_set_blocksize(sb, blocksize);
70bbb3e0
AM
1621 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1622 offset = do_div(logical_sb_block, blocksize);
1623 bh = sb_bread(sb, logical_sb_block);
ac27a0ec
DK
1624 if (!bh) {
1625 printk(KERN_ERR
617ba13b 1626 "EXT4-fs: Can't read superblock on 2nd try.\n");
ac27a0ec
DK
1627 goto failed_mount;
1628 }
617ba13b 1629 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
ac27a0ec 1630 sbi->s_es = es;
617ba13b 1631 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
ac27a0ec 1632 printk (KERN_ERR
617ba13b 1633 "EXT4-fs: Magic mismatch, very weird !\n");
ac27a0ec
DK
1634 goto failed_mount;
1635 }
1636 }
1637
617ba13b 1638 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
ac27a0ec 1639
617ba13b
MC
1640 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
1641 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
1642 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
ac27a0ec
DK
1643 } else {
1644 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1645 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
617ba13b 1646 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
ac27a0ec
DK
1647 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1648 (sbi->s_inode_size > blocksize)) {
1649 printk (KERN_ERR
617ba13b 1650 "EXT4-fs: unsupported inode size: %d\n",
ac27a0ec
DK
1651 sbi->s_inode_size);
1652 goto failed_mount;
1653 }
1654 }
617ba13b 1655 sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
ac27a0ec
DK
1656 le32_to_cpu(es->s_log_frag_size);
1657 if (blocksize != sbi->s_frag_size) {
1658 printk(KERN_ERR
617ba13b 1659 "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
ac27a0ec
DK
1660 sbi->s_frag_size, blocksize);
1661 goto failed_mount;
1662 }
0d1ee42f
AR
1663 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
1664 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
8fadc143 1665 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
0d1ee42f
AR
1666 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
1667 sbi->s_desc_size & (sbi->s_desc_size - 1)) {
1668 printk(KERN_ERR
8fadc143 1669 "EXT4-fs: unsupported descriptor size %lu\n",
0d1ee42f
AR
1670 sbi->s_desc_size);
1671 goto failed_mount;
1672 }
1673 } else
1674 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
ac27a0ec
DK
1675 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1676 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1677 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
617ba13b
MC
1678 if (EXT4_INODE_SIZE(sb) == 0)
1679 goto cantfind_ext4;
1680 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
ac27a0ec 1681 if (sbi->s_inodes_per_block == 0)
617ba13b 1682 goto cantfind_ext4;
ac27a0ec
DK
1683 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1684 sbi->s_inodes_per_block;
0d1ee42f 1685 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
ac27a0ec
DK
1686 sbi->s_sbh = bh;
1687 sbi->s_mount_state = le16_to_cpu(es->s_state);
617ba13b
MC
1688 sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
1689 sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
ac27a0ec
DK
1690 for (i=0; i < 4; i++)
1691 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1692 sbi->s_def_hash_version = es->s_def_hash_version;
1693
1694 if (sbi->s_blocks_per_group > blocksize * 8) {
1695 printk (KERN_ERR
617ba13b 1696 "EXT4-fs: #blocks per group too big: %lu\n",
ac27a0ec
DK
1697 sbi->s_blocks_per_group);
1698 goto failed_mount;
1699 }
1700 if (sbi->s_frags_per_group > blocksize * 8) {
1701 printk (KERN_ERR
617ba13b 1702 "EXT4-fs: #fragments per group too big: %lu\n",
ac27a0ec
DK
1703 sbi->s_frags_per_group);
1704 goto failed_mount;
1705 }
1706 if (sbi->s_inodes_per_group > blocksize * 8) {
1707 printk (KERN_ERR
617ba13b 1708 "EXT4-fs: #inodes per group too big: %lu\n",
ac27a0ec
DK
1709 sbi->s_inodes_per_group);
1710 goto failed_mount;
1711 }
1712
bd81d8ee 1713 if (ext4_blocks_count(es) >
ac27a0ec 1714 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
617ba13b 1715 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
ac27a0ec
DK
1716 " too large to mount safely\n", sb->s_id);
1717 if (sizeof(sector_t) < 8)
617ba13b 1718 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
ac27a0ec
DK
1719 "enabled\n");
1720 goto failed_mount;
1721 }
1722
617ba13b
MC
1723 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
1724 goto cantfind_ext4;
bd81d8ee
LV
1725 blocks_count = (ext4_blocks_count(es) -
1726 le32_to_cpu(es->s_first_data_block) +
1727 EXT4_BLOCKS_PER_GROUP(sb) - 1);
1728 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
1729 sbi->s_groups_count = blocks_count;
617ba13b
MC
1730 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
1731 EXT4_DESC_PER_BLOCK(sb);
ac27a0ec
DK
1732 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1733 GFP_KERNEL);
1734 if (sbi->s_group_desc == NULL) {
617ba13b 1735 printk (KERN_ERR "EXT4-fs: not enough memory\n");
ac27a0ec
DK
1736 goto failed_mount;
1737 }
1738
1739 bgl_lock_init(&sbi->s_blockgroup_lock);
1740
1741 for (i = 0; i < db_count; i++) {
70bbb3e0 1742 block = descriptor_loc(sb, logical_sb_block, i);
ac27a0ec
DK
1743 sbi->s_group_desc[i] = sb_bread(sb, block);
1744 if (!sbi->s_group_desc[i]) {
617ba13b 1745 printk (KERN_ERR "EXT4-fs: "
ac27a0ec
DK
1746 "can't read group descriptor %d\n", i);
1747 db_count = i;
1748 goto failed_mount2;
1749 }
1750 }
617ba13b
MC
1751 if (!ext4_check_descriptors (sb)) {
1752 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
ac27a0ec
DK
1753 goto failed_mount2;
1754 }
1755 sbi->s_gdb_count = db_count;
1756 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1757 spin_lock_init(&sbi->s_next_gen_lock);
1758
1759 percpu_counter_init(&sbi->s_freeblocks_counter,
617ba13b 1760 ext4_count_free_blocks(sb));
ac27a0ec 1761 percpu_counter_init(&sbi->s_freeinodes_counter,
617ba13b 1762 ext4_count_free_inodes(sb));
ac27a0ec 1763 percpu_counter_init(&sbi->s_dirs_counter,
617ba13b 1764 ext4_count_dirs(sb));
ac27a0ec
DK
1765
1766 /* per fileystem reservation list head & lock */
1767 spin_lock_init(&sbi->s_rsv_window_lock);
1768 sbi->s_rsv_window_root = RB_ROOT;
1769 /* Add a single, static dummy reservation to the start of the
1770 * reservation window list --- it gives us a placeholder for
1771 * append-at-start-of-list which makes the allocation logic
1772 * _much_ simpler. */
617ba13b
MC
1773 sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1774 sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
ac27a0ec
DK
1775 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1776 sbi->s_rsv_window_head.rsv_goal_size = 0;
617ba13b 1777 ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
ac27a0ec
DK
1778
1779 /*
1780 * set up enough so that it can read an inode
1781 */
617ba13b
MC
1782 sb->s_op = &ext4_sops;
1783 sb->s_export_op = &ext4_export_ops;
1784 sb->s_xattr = ext4_xattr_handlers;
ac27a0ec 1785#ifdef CONFIG_QUOTA
617ba13b
MC
1786 sb->s_qcop = &ext4_qctl_operations;
1787 sb->dq_op = &ext4_quota_operations;
ac27a0ec
DK
1788#endif
1789 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1790
1791 sb->s_root = NULL;
1792
1793 needs_recovery = (es->s_last_orphan != 0 ||
617ba13b
MC
1794 EXT4_HAS_INCOMPAT_FEATURE(sb,
1795 EXT4_FEATURE_INCOMPAT_RECOVER));
ac27a0ec
DK
1796
1797 /*
1798 * The first inode we look at is the journal inode. Don't try
1799 * root first: it may be modified in the journal!
1800 */
1801 if (!test_opt(sb, NOLOAD) &&
617ba13b
MC
1802 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
1803 if (ext4_load_journal(sb, es, journal_devnum))
ac27a0ec
DK
1804 goto failed_mount3;
1805 } else if (journal_inum) {
617ba13b 1806 if (ext4_create_journal(sb, es, journal_inum))
ac27a0ec
DK
1807 goto failed_mount3;
1808 } else {
1809 if (!silent)
1810 printk (KERN_ERR
617ba13b 1811 "ext4: No journal on filesystem on %s\n",
ac27a0ec
DK
1812 sb->s_id);
1813 goto failed_mount3;
1814 }
1815
1816 /* We have now updated the journal if required, so we can
1817 * validate the data journaling mode. */
1818 switch (test_opt(sb, DATA_FLAGS)) {
1819 case 0:
1820 /* No mode set, assume a default based on the journal
63f57933
AM
1821 * capabilities: ORDERED_DATA if the journal can
1822 * cope, else JOURNAL_DATA
1823 */
dab291af
MC
1824 if (jbd2_journal_check_available_features
1825 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
ac27a0ec
DK
1826 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1827 else
1828 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1829 break;
1830
617ba13b
MC
1831 case EXT4_MOUNT_ORDERED_DATA:
1832 case EXT4_MOUNT_WRITEBACK_DATA:
dab291af
MC
1833 if (!jbd2_journal_check_available_features
1834 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
617ba13b 1835 printk(KERN_ERR "EXT4-fs: Journal does not support "
ac27a0ec
DK
1836 "requested data journaling mode\n");
1837 goto failed_mount4;
1838 }
1839 default:
1840 break;
1841 }
1842
1843 if (test_opt(sb, NOBH)) {
617ba13b
MC
1844 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
1845 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
ac27a0ec
DK
1846 "its supported only with writeback mode\n");
1847 clear_opt(sbi->s_mount_opt, NOBH);
1848 }
1849 }
1850 /*
dab291af 1851 * The jbd2_journal_load will have done any necessary log recovery,
ac27a0ec
DK
1852 * so we can safely mount the rest of the filesystem now.
1853 */
1854
617ba13b 1855 root = iget(sb, EXT4_ROOT_INO);
ac27a0ec
DK
1856 sb->s_root = d_alloc_root(root);
1857 if (!sb->s_root) {
617ba13b 1858 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
ac27a0ec
DK
1859 iput(root);
1860 goto failed_mount4;
1861 }
1862 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1863 dput(sb->s_root);
1864 sb->s_root = NULL;
617ba13b 1865 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
ac27a0ec
DK
1866 goto failed_mount4;
1867 }
1868
617ba13b 1869 ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
ac27a0ec
DK
1870 /*
1871 * akpm: core read_super() calls in here with the superblock locked.
1872 * That deadlocks, because orphan cleanup needs to lock the superblock
1873 * in numerous places. Here we just pop the lock - it's relatively
1874 * harmless, because we are now ready to accept write_super() requests,
1875 * and aviro says that's the only reason for hanging onto the
1876 * superblock lock.
1877 */
617ba13b
MC
1878 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
1879 ext4_orphan_cleanup(sb, es);
1880 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
ac27a0ec 1881 if (needs_recovery)
617ba13b
MC
1882 printk (KERN_INFO "EXT4-fs: recovery complete.\n");
1883 ext4_mark_recovery_complete(sb, es);
1884 printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
1885 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
1886 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
ac27a0ec
DK
1887 "writeback");
1888
a86c6181
AT
1889 ext4_ext_init(sb);
1890
ac27a0ec
DK
1891 lock_kernel();
1892 return 0;
1893
617ba13b 1894cantfind_ext4:
ac27a0ec 1895 if (!silent)
617ba13b 1896 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
ac27a0ec
DK
1897 sb->s_id);
1898 goto failed_mount;
1899
1900failed_mount4:
dab291af 1901 jbd2_journal_destroy(sbi->s_journal);
ac27a0ec
DK
1902failed_mount3:
1903 percpu_counter_destroy(&sbi->s_freeblocks_counter);
1904 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1905 percpu_counter_destroy(&sbi->s_dirs_counter);
1906failed_mount2:
1907 for (i = 0; i < db_count; i++)
1908 brelse(sbi->s_group_desc[i]);
1909 kfree(sbi->s_group_desc);
1910failed_mount:
1911#ifdef CONFIG_QUOTA
1912 for (i = 0; i < MAXQUOTAS; i++)
1913 kfree(sbi->s_qf_names[i]);
1914#endif
617ba13b 1915 ext4_blkdev_remove(sbi);
ac27a0ec
DK
1916 brelse(bh);
1917out_fail:
1918 sb->s_fs_info = NULL;
1919 kfree(sbi);
1920 lock_kernel();
1921 return -EINVAL;
1922}
1923
1924/*
1925 * Setup any per-fs journal parameters now. We'll do this both on
1926 * initial mount, once the journal has been initialised but before we've
1927 * done any recovery; and again on any subsequent remount.
1928 */
617ba13b 1929static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
ac27a0ec 1930{
617ba13b 1931 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
1932
1933 if (sbi->s_commit_interval)
1934 journal->j_commit_interval = sbi->s_commit_interval;
617ba13b 1935 /* We could also set up an ext4-specific default for the commit
ac27a0ec
DK
1936 * interval here, but for now we'll just fall back to the jbd
1937 * default. */
1938
1939 spin_lock(&journal->j_state_lock);
1940 if (test_opt(sb, BARRIER))
dab291af 1941 journal->j_flags |= JBD2_BARRIER;
ac27a0ec 1942 else
dab291af 1943 journal->j_flags &= ~JBD2_BARRIER;
ac27a0ec
DK
1944 spin_unlock(&journal->j_state_lock);
1945}
1946
617ba13b 1947static journal_t *ext4_get_journal(struct super_block *sb,
ac27a0ec
DK
1948 unsigned int journal_inum)
1949{
1950 struct inode *journal_inode;
1951 journal_t *journal;
1952
1953 /* First, test for the existence of a valid inode on disk. Bad
1954 * things happen if we iget() an unused inode, as the subsequent
1955 * iput() will try to delete it. */
1956
1957 journal_inode = iget(sb, journal_inum);
1958 if (!journal_inode) {
617ba13b 1959 printk(KERN_ERR "EXT4-fs: no journal found.\n");
ac27a0ec
DK
1960 return NULL;
1961 }
1962 if (!journal_inode->i_nlink) {
1963 make_bad_inode(journal_inode);
1964 iput(journal_inode);
617ba13b 1965 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
ac27a0ec
DK
1966 return NULL;
1967 }
1968
1969 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1970 journal_inode, journal_inode->i_size);
1971 if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
617ba13b 1972 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
ac27a0ec
DK
1973 iput(journal_inode);
1974 return NULL;
1975 }
1976
dab291af 1977 journal = jbd2_journal_init_inode(journal_inode);
ac27a0ec 1978 if (!journal) {
617ba13b 1979 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
ac27a0ec
DK
1980 iput(journal_inode);
1981 return NULL;
1982 }
1983 journal->j_private = sb;
617ba13b 1984 ext4_init_journal_params(sb, journal);
ac27a0ec
DK
1985 return journal;
1986}
1987
617ba13b 1988static journal_t *ext4_get_dev_journal(struct super_block *sb,
ac27a0ec
DK
1989 dev_t j_dev)
1990{
1991 struct buffer_head * bh;
1992 journal_t *journal;
617ba13b
MC
1993 ext4_fsblk_t start;
1994 ext4_fsblk_t len;
ac27a0ec 1995 int hblock, blocksize;
617ba13b 1996 ext4_fsblk_t sb_block;
ac27a0ec 1997 unsigned long offset;
617ba13b 1998 struct ext4_super_block * es;
ac27a0ec
DK
1999 struct block_device *bdev;
2000
617ba13b 2001 bdev = ext4_blkdev_get(j_dev);
ac27a0ec
DK
2002 if (bdev == NULL)
2003 return NULL;
2004
2005 if (bd_claim(bdev, sb)) {
2006 printk(KERN_ERR
8c55e204 2007 "EXT4: failed to claim external journal device.\n");
ac27a0ec
DK
2008 blkdev_put(bdev);
2009 return NULL;
2010 }
2011
2012 blocksize = sb->s_blocksize;
2013 hblock = bdev_hardsect_size(bdev);
2014 if (blocksize < hblock) {
2015 printk(KERN_ERR
617ba13b 2016 "EXT4-fs: blocksize too small for journal device.\n");
ac27a0ec
DK
2017 goto out_bdev;
2018 }
2019
617ba13b
MC
2020 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2021 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
ac27a0ec
DK
2022 set_blocksize(bdev, blocksize);
2023 if (!(bh = __bread(bdev, sb_block, blocksize))) {
617ba13b 2024 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
ac27a0ec
DK
2025 "external journal\n");
2026 goto out_bdev;
2027 }
2028
617ba13b
MC
2029 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2030 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
ac27a0ec 2031 !(le32_to_cpu(es->s_feature_incompat) &
617ba13b
MC
2032 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2033 printk(KERN_ERR "EXT4-fs: external journal has "
ac27a0ec
DK
2034 "bad superblock\n");
2035 brelse(bh);
2036 goto out_bdev;
2037 }
2038
617ba13b
MC
2039 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2040 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
ac27a0ec
DK
2041 brelse(bh);
2042 goto out_bdev;
2043 }
2044
bd81d8ee 2045 len = ext4_blocks_count(es);
ac27a0ec
DK
2046 start = sb_block + 1;
2047 brelse(bh); /* we're done with the superblock */
2048
dab291af 2049 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
ac27a0ec
DK
2050 start, len, blocksize);
2051 if (!journal) {
617ba13b 2052 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
ac27a0ec
DK
2053 goto out_bdev;
2054 }
2055 journal->j_private = sb;
2056 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2057 wait_on_buffer(journal->j_sb_buffer);
2058 if (!buffer_uptodate(journal->j_sb_buffer)) {
617ba13b 2059 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
ac27a0ec
DK
2060 goto out_journal;
2061 }
2062 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
617ba13b 2063 printk(KERN_ERR "EXT4-fs: External journal has more than one "
ac27a0ec
DK
2064 "user (unsupported) - %d\n",
2065 be32_to_cpu(journal->j_superblock->s_nr_users));
2066 goto out_journal;
2067 }
617ba13b
MC
2068 EXT4_SB(sb)->journal_bdev = bdev;
2069 ext4_init_journal_params(sb, journal);
ac27a0ec
DK
2070 return journal;
2071out_journal:
dab291af 2072 jbd2_journal_destroy(journal);
ac27a0ec 2073out_bdev:
617ba13b 2074 ext4_blkdev_put(bdev);
ac27a0ec
DK
2075 return NULL;
2076}
2077
617ba13b
MC
2078static int ext4_load_journal(struct super_block *sb,
2079 struct ext4_super_block *es,
ac27a0ec
DK
2080 unsigned long journal_devnum)
2081{
2082 journal_t *journal;
2083 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2084 dev_t journal_dev;
2085 int err = 0;
2086 int really_read_only;
2087
2088 if (journal_devnum &&
2089 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
617ba13b 2090 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
ac27a0ec
DK
2091 "numbers have changed\n");
2092 journal_dev = new_decode_dev(journal_devnum);
2093 } else
2094 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2095
2096 really_read_only = bdev_read_only(sb->s_bdev);
2097
2098 /*
2099 * Are we loading a blank journal or performing recovery after a
2100 * crash? For recovery, we need to check in advance whether we
2101 * can get read-write access to the device.
2102 */
2103
617ba13b 2104 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
ac27a0ec 2105 if (sb->s_flags & MS_RDONLY) {
617ba13b 2106 printk(KERN_INFO "EXT4-fs: INFO: recovery "
ac27a0ec
DK
2107 "required on readonly filesystem.\n");
2108 if (really_read_only) {
617ba13b 2109 printk(KERN_ERR "EXT4-fs: write access "
ac27a0ec
DK
2110 "unavailable, cannot proceed.\n");
2111 return -EROFS;
2112 }
617ba13b 2113 printk (KERN_INFO "EXT4-fs: write access will "
ac27a0ec
DK
2114 "be enabled during recovery.\n");
2115 }
2116 }
2117
2118 if (journal_inum && journal_dev) {
617ba13b 2119 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
ac27a0ec
DK
2120 "and inode journals!\n");
2121 return -EINVAL;
2122 }
2123
2124 if (journal_inum) {
617ba13b 2125 if (!(journal = ext4_get_journal(sb, journal_inum)))
ac27a0ec
DK
2126 return -EINVAL;
2127 } else {
617ba13b 2128 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
ac27a0ec
DK
2129 return -EINVAL;
2130 }
2131
2132 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
dab291af 2133 err = jbd2_journal_update_format(journal);
ac27a0ec 2134 if (err) {
617ba13b 2135 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
dab291af 2136 jbd2_journal_destroy(journal);
ac27a0ec
DK
2137 return err;
2138 }
2139 }
2140
617ba13b 2141 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
dab291af 2142 err = jbd2_journal_wipe(journal, !really_read_only);
ac27a0ec 2143 if (!err)
dab291af 2144 err = jbd2_journal_load(journal);
ac27a0ec
DK
2145
2146 if (err) {
617ba13b 2147 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
dab291af 2148 jbd2_journal_destroy(journal);
ac27a0ec
DK
2149 return err;
2150 }
2151
617ba13b
MC
2152 EXT4_SB(sb)->s_journal = journal;
2153 ext4_clear_journal_err(sb, es);
ac27a0ec
DK
2154
2155 if (journal_devnum &&
2156 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2157 es->s_journal_dev = cpu_to_le32(journal_devnum);
2158 sb->s_dirt = 1;
2159
2160 /* Make sure we flush the recovery flag to disk. */
617ba13b 2161 ext4_commit_super(sb, es, 1);
ac27a0ec
DK
2162 }
2163
2164 return 0;
2165}
2166
617ba13b
MC
2167static int ext4_create_journal(struct super_block * sb,
2168 struct ext4_super_block * es,
ac27a0ec
DK
2169 unsigned int journal_inum)
2170{
2171 journal_t *journal;
6c675bd4 2172 int err;
ac27a0ec
DK
2173
2174 if (sb->s_flags & MS_RDONLY) {
617ba13b 2175 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
ac27a0ec
DK
2176 "create journal.\n");
2177 return -EROFS;
2178 }
2179
6c675bd4
BP
2180 journal = ext4_get_journal(sb, journal_inum);
2181 if (!journal)
ac27a0ec
DK
2182 return -EINVAL;
2183
617ba13b 2184 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
ac27a0ec
DK
2185 journal_inum);
2186
6c675bd4
BP
2187 err = jbd2_journal_create(journal);
2188 if (err) {
617ba13b 2189 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
dab291af 2190 jbd2_journal_destroy(journal);
ac27a0ec
DK
2191 return -EIO;
2192 }
2193
617ba13b 2194 EXT4_SB(sb)->s_journal = journal;
ac27a0ec 2195
617ba13b
MC
2196 ext4_update_dynamic_rev(sb);
2197 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2198 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
ac27a0ec
DK
2199
2200 es->s_journal_inum = cpu_to_le32(journal_inum);
2201 sb->s_dirt = 1;
2202
2203 /* Make sure we flush the recovery flag to disk. */
617ba13b 2204 ext4_commit_super(sb, es, 1);
ac27a0ec
DK
2205
2206 return 0;
2207}
2208
617ba13b
MC
2209static void ext4_commit_super (struct super_block * sb,
2210 struct ext4_super_block * es,
ac27a0ec
DK
2211 int sync)
2212{
617ba13b 2213 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
ac27a0ec
DK
2214
2215 if (!sbh)
2216 return;
2217 es->s_wtime = cpu_to_le32(get_seconds());
bd81d8ee 2218 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
617ba13b 2219 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
ac27a0ec
DK
2220 BUFFER_TRACE(sbh, "marking dirty");
2221 mark_buffer_dirty(sbh);
2222 if (sync)
2223 sync_dirty_buffer(sbh);
2224}
2225
2226
2227/*
2228 * Have we just finished recovery? If so, and if we are mounting (or
2229 * remounting) the filesystem readonly, then we will end up with a
2230 * consistent fs on disk. Record that fact.
2231 */
617ba13b
MC
2232static void ext4_mark_recovery_complete(struct super_block * sb,
2233 struct ext4_super_block * es)
ac27a0ec 2234{
617ba13b 2235 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 2236
dab291af
MC
2237 jbd2_journal_lock_updates(journal);
2238 jbd2_journal_flush(journal);
32c37730 2239 lock_super(sb);
617ba13b 2240 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
ac27a0ec 2241 sb->s_flags & MS_RDONLY) {
617ba13b 2242 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 2243 sb->s_dirt = 0;
617ba13b 2244 ext4_commit_super(sb, es, 1);
ac27a0ec 2245 }
32c37730 2246 unlock_super(sb);
dab291af 2247 jbd2_journal_unlock_updates(journal);
ac27a0ec
DK
2248}
2249
2250/*
2251 * If we are mounting (or read-write remounting) a filesystem whose journal
2252 * has recorded an error from a previous lifetime, move that error to the
2253 * main filesystem now.
2254 */
617ba13b
MC
2255static void ext4_clear_journal_err(struct super_block * sb,
2256 struct ext4_super_block * es)
ac27a0ec
DK
2257{
2258 journal_t *journal;
2259 int j_errno;
2260 const char *errstr;
2261
617ba13b 2262 journal = EXT4_SB(sb)->s_journal;
ac27a0ec
DK
2263
2264 /*
2265 * Now check for any error status which may have been recorded in the
617ba13b 2266 * journal by a prior ext4_error() or ext4_abort()
ac27a0ec
DK
2267 */
2268
dab291af 2269 j_errno = jbd2_journal_errno(journal);
ac27a0ec
DK
2270 if (j_errno) {
2271 char nbuf[16];
2272
617ba13b
MC
2273 errstr = ext4_decode_error(sb, j_errno, nbuf);
2274 ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
ac27a0ec 2275 "from previous mount: %s", errstr);
617ba13b 2276 ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
ac27a0ec
DK
2277 "filesystem check.");
2278
617ba13b
MC
2279 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2280 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2281 ext4_commit_super (sb, es, 1);
ac27a0ec 2282
dab291af 2283 jbd2_journal_clear_err(journal);
ac27a0ec
DK
2284 }
2285}
2286
2287/*
2288 * Force the running and committing transactions to commit,
2289 * and wait on the commit.
2290 */
617ba13b 2291int ext4_force_commit(struct super_block *sb)
ac27a0ec
DK
2292{
2293 journal_t *journal;
2294 int ret;
2295
2296 if (sb->s_flags & MS_RDONLY)
2297 return 0;
2298
617ba13b 2299 journal = EXT4_SB(sb)->s_journal;
ac27a0ec 2300 sb->s_dirt = 0;
617ba13b 2301 ret = ext4_journal_force_commit(journal);
ac27a0ec
DK
2302 return ret;
2303}
2304
2305/*
617ba13b 2306 * Ext4 always journals updates to the superblock itself, so we don't
ac27a0ec
DK
2307 * have to propagate any other updates to the superblock on disk at this
2308 * point. Just start an async writeback to get the buffers on their way
2309 * to the disk.
2310 *
2311 * This implicitly triggers the writebehind on sync().
2312 */
2313
617ba13b 2314static void ext4_write_super (struct super_block * sb)
ac27a0ec
DK
2315{
2316 if (mutex_trylock(&sb->s_lock) != 0)
2317 BUG();
2318 sb->s_dirt = 0;
2319}
2320
617ba13b 2321static int ext4_sync_fs(struct super_block *sb, int wait)
ac27a0ec
DK
2322{
2323 tid_t target;
2324
2325 sb->s_dirt = 0;
dab291af 2326 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
ac27a0ec 2327 if (wait)
dab291af 2328 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
ac27a0ec
DK
2329 }
2330 return 0;
2331}
2332
2333/*
2334 * LVM calls this function before a (read-only) snapshot is created. This
2335 * gives us a chance to flush the journal completely and mark the fs clean.
2336 */
617ba13b 2337static void ext4_write_super_lockfs(struct super_block *sb)
ac27a0ec
DK
2338{
2339 sb->s_dirt = 0;
2340
2341 if (!(sb->s_flags & MS_RDONLY)) {
617ba13b 2342 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec
DK
2343
2344 /* Now we set up the journal barrier. */
dab291af
MC
2345 jbd2_journal_lock_updates(journal);
2346 jbd2_journal_flush(journal);
ac27a0ec
DK
2347
2348 /* Journal blocked and flushed, clear needs_recovery flag. */
617ba13b
MC
2349 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2350 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
ac27a0ec
DK
2351 }
2352}
2353
2354/*
2355 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2356 * flag here, even though the filesystem is not technically dirty yet.
2357 */
617ba13b 2358static void ext4_unlockfs(struct super_block *sb)
ac27a0ec
DK
2359{
2360 if (!(sb->s_flags & MS_RDONLY)) {
2361 lock_super(sb);
2362 /* Reser the needs_recovery flag before the fs is unlocked. */
617ba13b
MC
2363 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2364 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
ac27a0ec 2365 unlock_super(sb);
dab291af 2366 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
ac27a0ec
DK
2367 }
2368}
2369
617ba13b 2370static int ext4_remount (struct super_block * sb, int * flags, char * data)
ac27a0ec 2371{
617ba13b
MC
2372 struct ext4_super_block * es;
2373 struct ext4_sb_info *sbi = EXT4_SB(sb);
2374 ext4_fsblk_t n_blocks_count = 0;
ac27a0ec 2375 unsigned long old_sb_flags;
617ba13b 2376 struct ext4_mount_options old_opts;
ac27a0ec
DK
2377 int err;
2378#ifdef CONFIG_QUOTA
2379 int i;
2380#endif
2381
2382 /* Store the original options */
2383 old_sb_flags = sb->s_flags;
2384 old_opts.s_mount_opt = sbi->s_mount_opt;
2385 old_opts.s_resuid = sbi->s_resuid;
2386 old_opts.s_resgid = sbi->s_resgid;
2387 old_opts.s_commit_interval = sbi->s_commit_interval;
2388#ifdef CONFIG_QUOTA
2389 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2390 for (i = 0; i < MAXQUOTAS; i++)
2391 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2392#endif
2393
2394 /*
2395 * Allow the "check" option to be passed as a remount option.
2396 */
2397 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2398 err = -EINVAL;
2399 goto restore_opts;
2400 }
2401
617ba13b
MC
2402 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2403 ext4_abort(sb, __FUNCTION__, "Abort forced by user");
ac27a0ec
DK
2404
2405 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
617ba13b 2406 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec
DK
2407
2408 es = sbi->s_es;
2409
617ba13b 2410 ext4_init_journal_params(sb, sbi->s_journal);
ac27a0ec
DK
2411
2412 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
bd81d8ee 2413 n_blocks_count > ext4_blocks_count(es)) {
617ba13b 2414 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
ac27a0ec
DK
2415 err = -EROFS;
2416 goto restore_opts;
2417 }
2418
2419 if (*flags & MS_RDONLY) {
2420 /*
2421 * First of all, the unconditional stuff we have to do
2422 * to disable replay of the journal when we next remount
2423 */
2424 sb->s_flags |= MS_RDONLY;
2425
2426 /*
2427 * OK, test if we are remounting a valid rw partition
2428 * readonly, and if so set the rdonly flag and then
2429 * mark the partition as valid again.
2430 */
617ba13b
MC
2431 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2432 (sbi->s_mount_state & EXT4_VALID_FS))
ac27a0ec
DK
2433 es->s_state = cpu_to_le16(sbi->s_mount_state);
2434
32c37730
JK
2435 /*
2436 * We have to unlock super so that we can wait for
2437 * transactions.
2438 */
2439 unlock_super(sb);
617ba13b 2440 ext4_mark_recovery_complete(sb, es);
32c37730 2441 lock_super(sb);
ac27a0ec
DK
2442 } else {
2443 __le32 ret;
617ba13b
MC
2444 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2445 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2446 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
ac27a0ec
DK
2447 "remount RDWR because of unsupported "
2448 "optional features (%x).\n",
2449 sb->s_id, le32_to_cpu(ret));
2450 err = -EROFS;
2451 goto restore_opts;
2452 }
ead6596b
ES
2453
2454 /*
2455 * If we have an unprocessed orphan list hanging
2456 * around from a previously readonly bdev mount,
2457 * require a full umount/remount for now.
2458 */
2459 if (es->s_last_orphan) {
2460 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2461 "remount RDWR because of unprocessed "
2462 "orphan inode list. Please "
2463 "umount/remount instead.\n",
2464 sb->s_id);
2465 err = -EINVAL;
2466 goto restore_opts;
2467 }
2468
ac27a0ec
DK
2469 /*
2470 * Mounting a RDONLY partition read-write, so reread
2471 * and store the current valid flag. (It may have
2472 * been changed by e2fsck since we originally mounted
2473 * the partition.)
2474 */
617ba13b 2475 ext4_clear_journal_err(sb, es);
ac27a0ec 2476 sbi->s_mount_state = le16_to_cpu(es->s_state);
617ba13b 2477 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
ac27a0ec 2478 goto restore_opts;
617ba13b 2479 if (!ext4_setup_super (sb, es, 0))
ac27a0ec
DK
2480 sb->s_flags &= ~MS_RDONLY;
2481 }
2482 }
2483#ifdef CONFIG_QUOTA
2484 /* Release old quota file names */
2485 for (i = 0; i < MAXQUOTAS; i++)
2486 if (old_opts.s_qf_names[i] &&
2487 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2488 kfree(old_opts.s_qf_names[i]);
2489#endif
2490 return 0;
2491restore_opts:
2492 sb->s_flags = old_sb_flags;
2493 sbi->s_mount_opt = old_opts.s_mount_opt;
2494 sbi->s_resuid = old_opts.s_resuid;
2495 sbi->s_resgid = old_opts.s_resgid;
2496 sbi->s_commit_interval = old_opts.s_commit_interval;
2497#ifdef CONFIG_QUOTA
2498 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2499 for (i = 0; i < MAXQUOTAS; i++) {
2500 if (sbi->s_qf_names[i] &&
2501 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2502 kfree(sbi->s_qf_names[i]);
2503 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2504 }
2505#endif
2506 return err;
2507}
2508
617ba13b 2509static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
ac27a0ec
DK
2510{
2511 struct super_block *sb = dentry->d_sb;
617ba13b
MC
2512 struct ext4_sb_info *sbi = EXT4_SB(sb);
2513 struct ext4_super_block *es = sbi->s_es;
960cc398 2514 u64 fsid;
ac27a0ec 2515
5e70030d
BP
2516 if (test_opt(sb, MINIX_DF)) {
2517 sbi->s_overhead_last = 0;
2518 } else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
2519 unsigned long ngroups = sbi->s_groups_count, i;
2520 ext4_fsblk_t overhead = 0;
ac27a0ec
DK
2521 smp_rmb();
2522
2523 /*
5e70030d
BP
2524 * Compute the overhead (FS structures). This is constant
2525 * for a given filesystem unless the number of block groups
2526 * changes so we cache the previous value until it does.
ac27a0ec
DK
2527 */
2528
2529 /*
2530 * All of the blocks before first_data_block are
2531 * overhead
2532 */
2533 overhead = le32_to_cpu(es->s_first_data_block);
2534
2535 /*
2536 * Add the overhead attributed to the superblock and
2537 * block group descriptors. If the sparse superblocks
2538 * feature is turned on, then not all groups have this.
2539 */
2540 for (i = 0; i < ngroups; i++) {
617ba13b
MC
2541 overhead += ext4_bg_has_super(sb, i) +
2542 ext4_bg_num_gdb(sb, i);
ac27a0ec
DK
2543 cond_resched();
2544 }
2545
2546 /*
2547 * Every block group has an inode bitmap, a block
2548 * bitmap, and an inode table.
2549 */
5e70030d
BP
2550 overhead += ngroups * (2 + sbi->s_itb_per_group);
2551 sbi->s_overhead_last = overhead;
2552 smp_wmb();
2553 sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
ac27a0ec
DK
2554 }
2555
617ba13b 2556 buf->f_type = EXT4_SUPER_MAGIC;
ac27a0ec 2557 buf->f_bsize = sb->s_blocksize;
5e70030d 2558 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
ac27a0ec 2559 buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
5e70030d 2560 es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
bd81d8ee
LV
2561 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2562 if (buf->f_bfree < ext4_r_blocks_count(es))
ac27a0ec
DK
2563 buf->f_bavail = 0;
2564 buf->f_files = le32_to_cpu(es->s_inodes_count);
2565 buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
5e70030d 2566 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
617ba13b 2567 buf->f_namelen = EXT4_NAME_LEN;
960cc398
PE
2568 fsid = le64_to_cpup((void *)es->s_uuid) ^
2569 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2570 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2571 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
ac27a0ec
DK
2572 return 0;
2573}
2574
2575/* Helper function for writing quotas on sync - we need to start transaction before quota file
2576 * is locked for write. Otherwise the are possible deadlocks:
2577 * Process 1 Process 2
617ba13b 2578 * ext4_create() quota_sync()
dab291af 2579 * jbd2_journal_start() write_dquot()
ac27a0ec 2580 * DQUOT_INIT() down(dqio_mutex)
dab291af 2581 * down(dqio_mutex) jbd2_journal_start()
ac27a0ec
DK
2582 *
2583 */
2584
2585#ifdef CONFIG_QUOTA
2586
2587static inline struct inode *dquot_to_inode(struct dquot *dquot)
2588{
2589 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2590}
2591
617ba13b 2592static int ext4_dquot_initialize(struct inode *inode, int type)
ac27a0ec
DK
2593{
2594 handle_t *handle;
2595 int ret, err;
2596
2597 /* We may create quota structure so we need to reserve enough blocks */
617ba13b 2598 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
ac27a0ec
DK
2599 if (IS_ERR(handle))
2600 return PTR_ERR(handle);
2601 ret = dquot_initialize(inode, type);
617ba13b 2602 err = ext4_journal_stop(handle);
ac27a0ec
DK
2603 if (!ret)
2604 ret = err;
2605 return ret;
2606}
2607
617ba13b 2608static int ext4_dquot_drop(struct inode *inode)
ac27a0ec
DK
2609{
2610 handle_t *handle;
2611 int ret, err;
2612
2613 /* We may delete quota structure so we need to reserve enough blocks */
617ba13b 2614 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
ac27a0ec
DK
2615 if (IS_ERR(handle))
2616 return PTR_ERR(handle);
2617 ret = dquot_drop(inode);
617ba13b 2618 err = ext4_journal_stop(handle);
ac27a0ec
DK
2619 if (!ret)
2620 ret = err;
2621 return ret;
2622}
2623
617ba13b 2624static int ext4_write_dquot(struct dquot *dquot)
ac27a0ec
DK
2625{
2626 int ret, err;
2627 handle_t *handle;
2628 struct inode *inode;
2629
2630 inode = dquot_to_inode(dquot);
617ba13b
MC
2631 handle = ext4_journal_start(inode,
2632 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
2633 if (IS_ERR(handle))
2634 return PTR_ERR(handle);
2635 ret = dquot_commit(dquot);
617ba13b 2636 err = ext4_journal_stop(handle);
ac27a0ec
DK
2637 if (!ret)
2638 ret = err;
2639 return ret;
2640}
2641
617ba13b 2642static int ext4_acquire_dquot(struct dquot *dquot)
ac27a0ec
DK
2643{
2644 int ret, err;
2645 handle_t *handle;
2646
617ba13b
MC
2647 handle = ext4_journal_start(dquot_to_inode(dquot),
2648 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
2649 if (IS_ERR(handle))
2650 return PTR_ERR(handle);
2651 ret = dquot_acquire(dquot);
617ba13b 2652 err = ext4_journal_stop(handle);
ac27a0ec
DK
2653 if (!ret)
2654 ret = err;
2655 return ret;
2656}
2657
617ba13b 2658static int ext4_release_dquot(struct dquot *dquot)
ac27a0ec
DK
2659{
2660 int ret, err;
2661 handle_t *handle;
2662
617ba13b
MC
2663 handle = ext4_journal_start(dquot_to_inode(dquot),
2664 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
2665 if (IS_ERR(handle))
2666 return PTR_ERR(handle);
2667 ret = dquot_release(dquot);
617ba13b 2668 err = ext4_journal_stop(handle);
ac27a0ec
DK
2669 if (!ret)
2670 ret = err;
2671 return ret;
2672}
2673
617ba13b 2674static int ext4_mark_dquot_dirty(struct dquot *dquot)
ac27a0ec
DK
2675{
2676 /* Are we journalling quotas? */
617ba13b
MC
2677 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2678 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
ac27a0ec 2679 dquot_mark_dquot_dirty(dquot);
617ba13b 2680 return ext4_write_dquot(dquot);
ac27a0ec
DK
2681 } else {
2682 return dquot_mark_dquot_dirty(dquot);
2683 }
2684}
2685
617ba13b 2686static int ext4_write_info(struct super_block *sb, int type)
ac27a0ec
DK
2687{
2688 int ret, err;
2689 handle_t *handle;
2690
2691 /* Data block + inode block */
617ba13b 2692 handle = ext4_journal_start(sb->s_root->d_inode, 2);
ac27a0ec
DK
2693 if (IS_ERR(handle))
2694 return PTR_ERR(handle);
2695 ret = dquot_commit_info(sb, type);
617ba13b 2696 err = ext4_journal_stop(handle);
ac27a0ec
DK
2697 if (!ret)
2698 ret = err;
2699 return ret;
2700}
2701
2702/*
2703 * Turn on quotas during mount time - we need to find
2704 * the quota file and such...
2705 */
617ba13b 2706static int ext4_quota_on_mount(struct super_block *sb, int type)
ac27a0ec 2707{
617ba13b
MC
2708 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
2709 EXT4_SB(sb)->s_jquota_fmt, type);
ac27a0ec
DK
2710}
2711
2712/*
2713 * Standard function to be called on quota_on
2714 */
617ba13b 2715static int ext4_quota_on(struct super_block *sb, int type, int format_id,
ac27a0ec
DK
2716 char *path)
2717{
2718 int err;
2719 struct nameidata nd;
2720
2721 if (!test_opt(sb, QUOTA))
2722 return -EINVAL;
2723 /* Not journalling quota? */
617ba13b
MC
2724 if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
2725 !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
ac27a0ec
DK
2726 return vfs_quota_on(sb, type, format_id, path);
2727 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2728 if (err)
2729 return err;
2730 /* Quotafile not on the same filesystem? */
2731 if (nd.mnt->mnt_sb != sb) {
2732 path_release(&nd);
2733 return -EXDEV;
2734 }
2735 /* Quotafile not of fs root? */
2736 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2737 printk(KERN_WARNING
617ba13b 2738 "EXT4-fs: Quota file not on filesystem root. "
ac27a0ec
DK
2739 "Journalled quota will not work.\n");
2740 path_release(&nd);
2741 return vfs_quota_on(sb, type, format_id, path);
2742}
2743
2744/* Read data from quotafile - avoid pagecache and such because we cannot afford
2745 * acquiring the locks... As quota files are never truncated and quota code
2746 * itself serializes the operations (and noone else should touch the files)
2747 * we don't have to be afraid of races */
617ba13b 2748static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec
DK
2749 size_t len, loff_t off)
2750{
2751 struct inode *inode = sb_dqopt(sb)->files[type];
617ba13b 2752 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
2753 int err = 0;
2754 int offset = off & (sb->s_blocksize - 1);
2755 int tocopy;
2756 size_t toread;
2757 struct buffer_head *bh;
2758 loff_t i_size = i_size_read(inode);
2759
2760 if (off > i_size)
2761 return 0;
2762 if (off+len > i_size)
2763 len = i_size-off;
2764 toread = len;
2765 while (toread > 0) {
2766 tocopy = sb->s_blocksize - offset < toread ?
2767 sb->s_blocksize - offset : toread;
617ba13b 2768 bh = ext4_bread(NULL, inode, blk, 0, &err);
ac27a0ec
DK
2769 if (err)
2770 return err;
2771 if (!bh) /* A hole? */
2772 memset(data, 0, tocopy);
2773 else
2774 memcpy(data, bh->b_data+offset, tocopy);
2775 brelse(bh);
2776 offset = 0;
2777 toread -= tocopy;
2778 data += tocopy;
2779 blk++;
2780 }
2781 return len;
2782}
2783
2784/* Write to quotafile (we know the transaction is already started and has
2785 * enough credits) */
617ba13b 2786static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec
DK
2787 const char *data, size_t len, loff_t off)
2788{
2789 struct inode *inode = sb_dqopt(sb)->files[type];
617ba13b 2790 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
2791 int err = 0;
2792 int offset = off & (sb->s_blocksize - 1);
2793 int tocopy;
617ba13b 2794 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
ac27a0ec
DK
2795 size_t towrite = len;
2796 struct buffer_head *bh;
2797 handle_t *handle = journal_current_handle();
2798
2799 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2800 while (towrite > 0) {
2801 tocopy = sb->s_blocksize - offset < towrite ?
2802 sb->s_blocksize - offset : towrite;
617ba13b 2803 bh = ext4_bread(handle, inode, blk, 1, &err);
ac27a0ec
DK
2804 if (!bh)
2805 goto out;
2806 if (journal_quota) {
617ba13b 2807 err = ext4_journal_get_write_access(handle, bh);
ac27a0ec
DK
2808 if (err) {
2809 brelse(bh);
2810 goto out;
2811 }
2812 }
2813 lock_buffer(bh);
2814 memcpy(bh->b_data+offset, data, tocopy);
2815 flush_dcache_page(bh->b_page);
2816 unlock_buffer(bh);
2817 if (journal_quota)
617ba13b 2818 err = ext4_journal_dirty_metadata(handle, bh);
ac27a0ec
DK
2819 else {
2820 /* Always do at least ordered writes for quotas */
617ba13b 2821 err = ext4_journal_dirty_data(handle, bh);
ac27a0ec
DK
2822 mark_buffer_dirty(bh);
2823 }
2824 brelse(bh);
2825 if (err)
2826 goto out;
2827 offset = 0;
2828 towrite -= tocopy;
2829 data += tocopy;
2830 blk++;
2831 }
2832out:
2833 if (len == towrite)
2834 return err;
2835 if (inode->i_size < off+len-towrite) {
2836 i_size_write(inode, off+len-towrite);
617ba13b 2837 EXT4_I(inode)->i_disksize = inode->i_size;
ac27a0ec
DK
2838 }
2839 inode->i_version++;
2840 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
617ba13b 2841 ext4_mark_inode_dirty(handle, inode);
ac27a0ec
DK
2842 mutex_unlock(&inode->i_mutex);
2843 return len - towrite;
2844}
2845
2846#endif
2847
617ba13b 2848static int ext4_get_sb(struct file_system_type *fs_type,
ac27a0ec
DK
2849 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2850{
617ba13b 2851 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
ac27a0ec
DK
2852}
2853
617ba13b 2854static struct file_system_type ext4dev_fs_type = {
ac27a0ec 2855 .owner = THIS_MODULE,
617ba13b
MC
2856 .name = "ext4dev",
2857 .get_sb = ext4_get_sb,
ac27a0ec
DK
2858 .kill_sb = kill_block_super,
2859 .fs_flags = FS_REQUIRES_DEV,
2860};
2861
617ba13b 2862static int __init init_ext4_fs(void)
ac27a0ec 2863{
617ba13b 2864 int err = init_ext4_xattr();
ac27a0ec
DK
2865 if (err)
2866 return err;
2867 err = init_inodecache();
2868 if (err)
2869 goto out1;
63f57933 2870 err = register_filesystem(&ext4dev_fs_type);
ac27a0ec
DK
2871 if (err)
2872 goto out;
2873 return 0;
2874out:
2875 destroy_inodecache();
2876out1:
617ba13b 2877 exit_ext4_xattr();
ac27a0ec
DK
2878 return err;
2879}
2880
617ba13b 2881static void __exit exit_ext4_fs(void)
ac27a0ec 2882{
617ba13b 2883 unregister_filesystem(&ext4dev_fs_type);
ac27a0ec 2884 destroy_inodecache();
617ba13b 2885 exit_ext4_xattr();
ac27a0ec
DK
2886}
2887
2888MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
617ba13b 2889MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
ac27a0ec 2890MODULE_LICENSE("GPL");
617ba13b
MC
2891module_init(init_ext4_fs)
2892module_exit(exit_ext4_fs)