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