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