blob: b47259f6f39cd3e31db251b7c3eb904fdf4f083d [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/super.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
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>
Mingming Caodab291a2006-10-11 01:21:01 -070023#include <linux/jbd2.h>
Mingming Cao617ba132006-10-11 01:20:53 -070024#include <linux/ext4_fs.h>
Mingming Caodab291a2006-10-11 01:21:01 -070025#include <linux/ext4_jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070026#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>
Christoph Hellwiga5694252007-07-17 04:04:28 -070032#include <linux/exportfs.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070033#include <linux/vfs.h>
34#include <linux/random.h>
35#include <linux/mount.h>
36#include <linux/namei.h>
37#include <linux/quotaops.h>
38#include <linux/seq_file.h>
39
40#include <asm/uaccess.h>
41
42#include "xattr.h"
43#include "acl.h"
44#include "namei.h"
45
Mingming Cao617ba132006-10-11 01:20:53 -070046static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047 unsigned long journal_devnum);
Mingming Cao617ba132006-10-11 01:20:53 -070048static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049 unsigned int);
Mingming Cao617ba132006-10-11 01:20:53 -070050static void ext4_commit_super (struct super_block * sb,
51 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070052 int sync);
Mingming Cao617ba132006-10-11 01:20:53 -070053static void ext4_mark_recovery_complete(struct super_block * sb,
54 struct ext4_super_block * es);
55static void ext4_clear_journal_err(struct super_block * sb,
56 struct ext4_super_block * es);
57static int ext4_sync_fs(struct super_block *sb, int wait);
58static const char *ext4_decode_error(struct super_block * sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070059 char nbuf[16]);
Mingming Cao617ba132006-10-11 01:20:53 -070060static int ext4_remount (struct super_block * sb, int * flags, char * data);
61static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
62static void ext4_unlockfs(struct super_block *sb);
63static void ext4_write_super (struct super_block * sb);
64static void ext4_write_super_lockfs(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070066
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070067ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
68 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070069{
70 return le32_to_cpu(bg->bg_block_bitmap) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070071 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
72 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070073}
74
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070075ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
76 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070077{
78 return le32_to_cpu(bg->bg_inode_bitmap) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070079 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
80 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070081}
82
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070083ext4_fsblk_t ext4_inode_table(struct super_block *sb,
84 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070085{
86 return le32_to_cpu(bg->bg_inode_table) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070087 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
88 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070089}
90
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070091void ext4_block_bitmap_set(struct super_block *sb,
92 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070093{
94 bg->bg_block_bitmap = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070095 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
96 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070097}
98
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070099void ext4_inode_bitmap_set(struct super_block *sb,
100 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700101{
102 bg->bg_inode_bitmap = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700103 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
104 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700105}
106
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700107void ext4_inode_table_set(struct super_block *sb,
108 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700109{
110 bg->bg_inode_table = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700111 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
112 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700113}
114
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700115/*
Mingming Caodab291a2006-10-11 01:21:01 -0700116 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700117 *
118 * The only special thing we need to do here is to make sure that all
119 * journal_end calls result in the superblock being marked dirty, so
120 * that sync() will call the filesystem's write_super callback if
121 * appropriate.
122 */
Mingming Cao617ba132006-10-11 01:20:53 -0700123handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700124{
125 journal_t *journal;
126
127 if (sb->s_flags & MS_RDONLY)
128 return ERR_PTR(-EROFS);
129
130 /* Special case here: if the journal has aborted behind our
131 * backs (eg. EIO in the commit thread), then we still need to
132 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700133 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700134 if (is_journal_aborted(journal)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700135 ext4_abort(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700136 "Detected aborted journal");
137 return ERR_PTR(-EROFS);
138 }
139
Mingming Caodab291a2006-10-11 01:21:01 -0700140 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700141}
142
143/*
144 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700145 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146 * that sync() will call the filesystem's write_super callback if
147 * appropriate.
148 */
Mingming Cao617ba132006-10-11 01:20:53 -0700149int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150{
151 struct super_block *sb;
152 int err;
153 int rc;
154
155 sb = handle->h_transaction->t_journal->j_private;
156 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700157 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158
159 if (!err)
160 err = rc;
161 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700162 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163 return err;
164}
165
Mingming Cao617ba132006-10-11 01:20:53 -0700166void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700167 struct buffer_head *bh, handle_t *handle, int err)
168{
169 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700170 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171
172 if (bh)
173 BUFFER_TRACE(bh, "abort");
174
175 if (!handle->h_err)
176 handle->h_err = err;
177
178 if (is_handle_aborted(handle))
179 return;
180
181 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
182 caller, errstr, err_fn);
183
Mingming Caodab291a2006-10-11 01:21:01 -0700184 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185}
186
187/* Deal with the reporting of failure conditions on a filesystem such as
188 * inconsistencies detected or read IO failures.
189 *
190 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700191 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192 * write ordering constraints on the superblock which prevent us from
193 * writing it out straight away; and given that the journal is about to
194 * be aborted, we can't rely on the current, or future, transactions to
195 * write out the superblock safely.
196 *
Mingming Caodab291a2006-10-11 01:21:01 -0700197 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198 * the journal instead. On recovery, the journal will compain about
199 * that error until we've noted it down and cleared it.
200 */
201
Mingming Cao617ba132006-10-11 01:20:53 -0700202static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203{
Mingming Cao617ba132006-10-11 01:20:53 -0700204 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205
Mingming Cao617ba132006-10-11 01:20:53 -0700206 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
207 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208
209 if (sb->s_flags & MS_RDONLY)
210 return;
211
212 if (!test_opt (sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700213 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700214
Mingming Cao617ba132006-10-11 01:20:53 -0700215 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700216 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700217 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218 }
219 if (test_opt (sb, ERRORS_RO)) {
220 printk (KERN_CRIT "Remounting filesystem read-only\n");
221 sb->s_flags |= MS_RDONLY;
222 }
Mingming Cao617ba132006-10-11 01:20:53 -0700223 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700225 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 sb->s_id);
227}
228
Mingming Cao617ba132006-10-11 01:20:53 -0700229void ext4_error (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700230 const char * fmt, ...)
231{
232 va_list args;
233
234 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700235 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700236 vprintk(fmt, args);
237 printk("\n");
238 va_end(args);
239
Mingming Cao617ba132006-10-11 01:20:53 -0700240 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241}
242
Mingming Cao617ba132006-10-11 01:20:53 -0700243static const char *ext4_decode_error(struct super_block * sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 char nbuf[16])
245{
246 char *errstr = NULL;
247
248 switch (errno) {
249 case -EIO:
250 errstr = "IO failure";
251 break;
252 case -ENOMEM:
253 errstr = "Out of memory";
254 break;
255 case -EROFS:
Mingming Caodab291a2006-10-11 01:21:01 -0700256 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257 errstr = "Journal has aborted";
258 else
259 errstr = "Readonly filesystem";
260 break;
261 default:
262 /* If the caller passed in an extra buffer for unknown
263 * errors, textualise them now. Else we just return
264 * NULL. */
265 if (nbuf) {
266 /* Check for truncated error codes... */
267 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
268 errstr = nbuf;
269 }
270 break;
271 }
272
273 return errstr;
274}
275
Mingming Cao617ba132006-10-11 01:20:53 -0700276/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277 * automatically and invokes the appropriate error response. */
278
Mingming Cao617ba132006-10-11 01:20:53 -0700279void __ext4_std_error (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700280 int errno)
281{
282 char nbuf[16];
283 const char *errstr;
284
285 /* Special case: if the error is EROFS, and we're not already
286 * inside a transaction, then there's really no point in logging
287 * an error. */
288 if (errno == -EROFS && journal_current_handle() == NULL &&
289 (sb->s_flags & MS_RDONLY))
290 return;
291
Mingming Cao617ba132006-10-11 01:20:53 -0700292 errstr = ext4_decode_error(sb, errno, nbuf);
293 printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294 sb->s_id, function, errstr);
295
Mingming Cao617ba132006-10-11 01:20:53 -0700296 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297}
298
299/*
Mingming Cao617ba132006-10-11 01:20:53 -0700300 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 * abort function may be used to deal with unrecoverable failures such
302 * as journal IO errors or ENOMEM at a critical moment in log management.
303 *
304 * We unconditionally force the filesystem into an ABORT|READONLY state,
305 * unless the error response on the fs has been set to panic in which
306 * case we take the easy way out and panic immediately.
307 */
308
Mingming Cao617ba132006-10-11 01:20:53 -0700309void ext4_abort (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310 const char * fmt, ...)
311{
312 va_list args;
313
Mingming Cao617ba132006-10-11 01:20:53 -0700314 printk (KERN_CRIT "ext4_abort called.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315
316 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700317 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700318 vprintk(fmt, args);
319 printk("\n");
320 va_end(args);
321
322 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700323 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324
325 if (sb->s_flags & MS_RDONLY)
326 return;
327
328 printk(KERN_CRIT "Remounting filesystem read-only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700329 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700330 sb->s_flags |= MS_RDONLY;
Mingming Cao617ba132006-10-11 01:20:53 -0700331 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Mingming Caodab291a2006-10-11 01:21:01 -0700332 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700333}
334
Mingming Cao617ba132006-10-11 01:20:53 -0700335void ext4_warning (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336 const char * fmt, ...)
337{
338 va_list args;
339
340 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700341 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700342 sb->s_id, function);
343 vprintk(fmt, args);
344 printk("\n");
345 va_end(args);
346}
347
Mingming Cao617ba132006-10-11 01:20:53 -0700348void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700349{
Mingming Cao617ba132006-10-11 01:20:53 -0700350 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351
Mingming Cao617ba132006-10-11 01:20:53 -0700352 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700353 return;
354
Mingming Cao617ba132006-10-11 01:20:53 -0700355 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700356 "updating to rev %d because of new feature flag, "
357 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700358 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700359
Mingming Cao617ba132006-10-11 01:20:53 -0700360 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
361 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
362 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363 /* leave es->s_feature_*compat flags alone */
364 /* es->s_uuid will be set by e2fsck if empty */
365
366 /*
367 * The rest of the superblock fields should be zero, and if not it
368 * means they are likely already in use, so leave them alone. We
369 * can leave it up to e2fsck to clean up any inconsistencies there.
370 */
371}
372
373/*
374 * Open the external journal device
375 */
Mingming Cao617ba132006-10-11 01:20:53 -0700376static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700377{
378 struct block_device *bdev;
379 char b[BDEVNAME_SIZE];
380
381 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
382 if (IS_ERR(bdev))
383 goto fail;
384 return bdev;
385
386fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700387 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700388 __bdevname(dev, b), PTR_ERR(bdev));
389 return NULL;
390}
391
392/*
393 * Release the journal device
394 */
Mingming Cao617ba132006-10-11 01:20:53 -0700395static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396{
397 bd_release(bdev);
398 return blkdev_put(bdev);
399}
400
Mingming Cao617ba132006-10-11 01:20:53 -0700401static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402{
403 struct block_device *bdev;
404 int ret = -ENODEV;
405
406 bdev = sbi->journal_bdev;
407 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700408 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 sbi->journal_bdev = NULL;
410 }
411 return ret;
412}
413
414static inline struct inode *orphan_list_entry(struct list_head *l)
415{
Mingming Cao617ba132006-10-11 01:20:53 -0700416 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417}
418
Mingming Cao617ba132006-10-11 01:20:53 -0700419static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700420{
421 struct list_head *l;
422
423 printk(KERN_ERR "sb orphan head is %d\n",
424 le32_to_cpu(sbi->s_es->s_last_orphan));
425
426 printk(KERN_ERR "sb_info orphan list:\n");
427 list_for_each(l, &sbi->s_orphan) {
428 struct inode *inode = orphan_list_entry(l);
429 printk(KERN_ERR " "
430 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
431 inode->i_sb->s_id, inode->i_ino, inode,
432 inode->i_mode, inode->i_nlink,
433 NEXT_ORPHAN(inode));
434 }
435}
436
Mingming Cao617ba132006-10-11 01:20:53 -0700437static void ext4_put_super (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700438{
Mingming Cao617ba132006-10-11 01:20:53 -0700439 struct ext4_sb_info *sbi = EXT4_SB(sb);
440 struct ext4_super_block *es = sbi->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441 int i;
442
Alex Tomasa86c6182006-10-11 01:21:03 -0700443 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700444 ext4_xattr_put_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -0700445 jbd2_journal_destroy(sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700446 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700447 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700448 es->s_state = cpu_to_le16(sbi->s_mount_state);
449 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
450 mark_buffer_dirty(sbi->s_sbh);
Mingming Cao617ba132006-10-11 01:20:53 -0700451 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700452 }
453
454 for (i = 0; i < sbi->s_gdb_count; i++)
455 brelse(sbi->s_group_desc[i]);
456 kfree(sbi->s_group_desc);
457 percpu_counter_destroy(&sbi->s_freeblocks_counter);
458 percpu_counter_destroy(&sbi->s_freeinodes_counter);
459 percpu_counter_destroy(&sbi->s_dirs_counter);
460 brelse(sbi->s_sbh);
461#ifdef CONFIG_QUOTA
462 for (i = 0; i < MAXQUOTAS; i++)
463 kfree(sbi->s_qf_names[i]);
464#endif
465
466 /* Debugging code just in case the in-memory inode orphan list
467 * isn't empty. The on-disk one can be non-empty if we've
468 * detected an error and taken the fs readonly, but the
469 * in-memory list had better be clean by this point. */
470 if (!list_empty(&sbi->s_orphan))
471 dump_orphan_list(sb, sbi);
472 J_ASSERT(list_empty(&sbi->s_orphan));
473
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700474 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
476 /*
477 * Invalidate the journal device's buffers. We don't want them
478 * floating about in memory - the physical journal device may
479 * hotswapped, and it breaks the `ro-after' testing code.
480 */
481 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700482 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700483 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700484 }
485 sb->s_fs_info = NULL;
486 kfree(sbi);
487 return;
488}
489
Christoph Lametere18b8902006-12-06 20:33:20 -0800490static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491
492/*
493 * Called inside transaction, so use GFP_NOFS
494 */
Mingming Cao617ba132006-10-11 01:20:53 -0700495static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496{
Mingming Cao617ba132006-10-11 01:20:53 -0700497 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800499 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 if (!ei)
501 return NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700502#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
503 ei->i_acl = EXT4_ACL_NOT_CACHED;
504 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505#endif
506 ei->i_block_alloc_info = NULL;
507 ei->vfs_inode.i_version = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700508 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509 return &ei->vfs_inode;
510}
511
Mingming Cao617ba132006-10-11 01:20:53 -0700512static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700513{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700514 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
515 printk("EXT4 Inode %p: orphan list check failed!\n",
516 EXT4_I(inode));
517 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
518 EXT4_I(inode), sizeof(struct ext4_inode_info),
519 true);
520 dump_stack();
521 }
Mingming Cao617ba132006-10-11 01:20:53 -0700522 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523}
524
Christoph Lametere18b8902006-12-06 20:33:20 -0800525static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526{
Mingming Cao617ba132006-10-11 01:20:53 -0700527 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528
Christoph Lametera35afb82007-05-16 22:10:57 -0700529 INIT_LIST_HEAD(&ei->i_orphan);
Mingming Cao617ba132006-10-11 01:20:53 -0700530#ifdef CONFIG_EXT4DEV_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700531 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532#endif
Christoph Lametera35afb82007-05-16 22:10:57 -0700533 mutex_init(&ei->truncate_mutex);
534 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535}
536
537static int init_inodecache(void)
538{
Mingming Cao617ba132006-10-11 01:20:53 -0700539 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
540 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541 0, (SLAB_RECLAIM_ACCOUNT|
542 SLAB_MEM_SPREAD),
543 init_once, NULL);
Mingming Cao617ba132006-10-11 01:20:53 -0700544 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 return -ENOMEM;
546 return 0;
547}
548
549static void destroy_inodecache(void)
550{
Mingming Cao617ba132006-10-11 01:20:53 -0700551 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552}
553
Mingming Cao617ba132006-10-11 01:20:53 -0700554static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555{
Mingming Cao617ba132006-10-11 01:20:53 -0700556 struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
557#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
558 if (EXT4_I(inode)->i_acl &&
559 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
560 posix_acl_release(EXT4_I(inode)->i_acl);
561 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562 }
Mingming Cao617ba132006-10-11 01:20:53 -0700563 if (EXT4_I(inode)->i_default_acl &&
564 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
565 posix_acl_release(EXT4_I(inode)->i_default_acl);
566 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567 }
568#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700569 ext4_discard_reservation(inode);
570 EXT4_I(inode)->i_block_alloc_info = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700571 if (unlikely(rsv))
572 kfree(rsv);
573}
574
Mingming Cao617ba132006-10-11 01:20:53 -0700575static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700576{
577#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700578 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579
580 if (sbi->s_jquota_fmt)
581 seq_printf(seq, ",jqfmt=%s",
582 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
583
584 if (sbi->s_qf_names[USRQUOTA])
585 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
586
587 if (sbi->s_qf_names[GRPQUOTA])
588 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
589
Mingming Cao617ba132006-10-11 01:20:53 -0700590 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591 seq_puts(seq, ",usrquota");
592
Mingming Cao617ba132006-10-11 01:20:53 -0700593 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700594 seq_puts(seq, ",grpquota");
595#endif
596}
597
Mingming Cao617ba132006-10-11 01:20:53 -0700598static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599{
600 struct super_block *sb = vfs->mnt_sb;
601
Mingming Cao617ba132006-10-11 01:20:53 -0700602 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700604 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700605 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700606 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607 seq_puts(seq, ",data=writeback");
608
Mingming Cao617ba132006-10-11 01:20:53 -0700609 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610
611 return 0;
612}
613
614
Mingming Cao617ba132006-10-11 01:20:53 -0700615static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700616{
617 __u32 *objp = vobjp;
618 unsigned long ino = objp[0];
619 __u32 generation = objp[1];
620 struct inode *inode;
621 struct dentry *result;
622
Mingming Cao617ba132006-10-11 01:20:53 -0700623 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700625 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700626 return ERR_PTR(-ESTALE);
627
628 /* iget isn't really right if the inode is currently unallocated!!
629 *
Mingming Cao617ba132006-10-11 01:20:53 -0700630 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700631 * deleted, so we should be safe.
632 *
633 * Currently we don't know the generation for parent directory, so
634 * a generation of 0 means "accept any"
635 */
636 inode = iget(sb, ino);
637 if (inode == NULL)
638 return ERR_PTR(-ENOMEM);
639 if (is_bad_inode(inode) ||
640 (generation && inode->i_generation != generation)) {
641 iput(inode);
642 return ERR_PTR(-ESTALE);
643 }
644 /* now to find a dentry.
645 * If possible, get a well-connected one
646 */
647 result = d_alloc_anon(inode);
648 if (!result) {
649 iput(inode);
650 return ERR_PTR(-ENOMEM);
651 }
652 return result;
653}
654
655#ifdef CONFIG_QUOTA
656#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
657#define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
658
Mingming Cao617ba132006-10-11 01:20:53 -0700659static int ext4_dquot_initialize(struct inode *inode, int type);
660static int ext4_dquot_drop(struct inode *inode);
661static int ext4_write_dquot(struct dquot *dquot);
662static int ext4_acquire_dquot(struct dquot *dquot);
663static int ext4_release_dquot(struct dquot *dquot);
664static int ext4_mark_dquot_dirty(struct dquot *dquot);
665static int ext4_write_info(struct super_block *sb, int type);
666static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
667static int ext4_quota_on_mount(struct super_block *sb, int type);
668static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700670static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 const char *data, size_t len, loff_t off);
672
Mingming Cao617ba132006-10-11 01:20:53 -0700673static struct dquot_operations ext4_quota_operations = {
674 .initialize = ext4_dquot_initialize,
675 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 .alloc_space = dquot_alloc_space,
677 .alloc_inode = dquot_alloc_inode,
678 .free_space = dquot_free_space,
679 .free_inode = dquot_free_inode,
680 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700681 .write_dquot = ext4_write_dquot,
682 .acquire_dquot = ext4_acquire_dquot,
683 .release_dquot = ext4_release_dquot,
684 .mark_dirty = ext4_mark_dquot_dirty,
685 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686};
687
Mingming Cao617ba132006-10-11 01:20:53 -0700688static struct quotactl_ops ext4_qctl_operations = {
689 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700690 .quota_off = vfs_quota_off,
691 .quota_sync = vfs_quota_sync,
692 .get_info = vfs_get_dqinfo,
693 .set_info = vfs_set_dqinfo,
694 .get_dqblk = vfs_get_dqblk,
695 .set_dqblk = vfs_set_dqblk
696};
697#endif
698
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800699static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700700 .alloc_inode = ext4_alloc_inode,
701 .destroy_inode = ext4_destroy_inode,
702 .read_inode = ext4_read_inode,
703 .write_inode = ext4_write_inode,
704 .dirty_inode = ext4_dirty_inode,
705 .delete_inode = ext4_delete_inode,
706 .put_super = ext4_put_super,
707 .write_super = ext4_write_super,
708 .sync_fs = ext4_sync_fs,
709 .write_super_lockfs = ext4_write_super_lockfs,
710 .unlockfs = ext4_unlockfs,
711 .statfs = ext4_statfs,
712 .remount_fs = ext4_remount,
713 .clear_inode = ext4_clear_inode,
714 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700716 .quota_read = ext4_quota_read,
717 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718#endif
719};
720
Mingming Cao617ba132006-10-11 01:20:53 -0700721static struct export_operations ext4_export_ops = {
722 .get_parent = ext4_get_parent,
723 .get_dentry = ext4_get_dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724};
725
726enum {
727 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
728 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
729 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
730 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
731 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
732 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
733 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
734 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
735 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
736 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Mingming Cao1e2462f2007-07-18 09:00:55 -0400737 Opt_grpquota, Opt_extents, Opt_noextents,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738};
739
740static match_table_t tokens = {
741 {Opt_bsd_df, "bsddf"},
742 {Opt_minix_df, "minixdf"},
743 {Opt_grpid, "grpid"},
744 {Opt_grpid, "bsdgroups"},
745 {Opt_nogrpid, "nogrpid"},
746 {Opt_nogrpid, "sysvgroups"},
747 {Opt_resgid, "resgid=%u"},
748 {Opt_resuid, "resuid=%u"},
749 {Opt_sb, "sb=%u"},
750 {Opt_err_cont, "errors=continue"},
751 {Opt_err_panic, "errors=panic"},
752 {Opt_err_ro, "errors=remount-ro"},
753 {Opt_nouid32, "nouid32"},
754 {Opt_nocheck, "nocheck"},
755 {Opt_nocheck, "check=none"},
756 {Opt_debug, "debug"},
757 {Opt_oldalloc, "oldalloc"},
758 {Opt_orlov, "orlov"},
759 {Opt_user_xattr, "user_xattr"},
760 {Opt_nouser_xattr, "nouser_xattr"},
761 {Opt_acl, "acl"},
762 {Opt_noacl, "noacl"},
763 {Opt_reservation, "reservation"},
764 {Opt_noreservation, "noreservation"},
765 {Opt_noload, "noload"},
766 {Opt_nobh, "nobh"},
767 {Opt_bh, "bh"},
768 {Opt_commit, "commit=%u"},
769 {Opt_journal_update, "journal=update"},
770 {Opt_journal_inum, "journal=%u"},
771 {Opt_journal_dev, "journal_dev=%u"},
772 {Opt_abort, "abort"},
773 {Opt_data_journal, "data=journal"},
774 {Opt_data_ordered, "data=ordered"},
775 {Opt_data_writeback, "data=writeback"},
776 {Opt_offusrjquota, "usrjquota="},
777 {Opt_usrjquota, "usrjquota=%s"},
778 {Opt_offgrpjquota, "grpjquota="},
779 {Opt_grpjquota, "grpjquota=%s"},
780 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
781 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
782 {Opt_grpquota, "grpquota"},
783 {Opt_noquota, "noquota"},
784 {Opt_quota, "quota"},
785 {Opt_usrquota, "usrquota"},
786 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -0700787 {Opt_extents, "extents"},
Mingming Cao1e2462f2007-07-18 09:00:55 -0400788 {Opt_noextents, "noextents"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789 {Opt_err, NULL},
790 {Opt_resize, "resize"},
791};
792
Mingming Cao617ba132006-10-11 01:20:53 -0700793static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700794{
Mingming Cao617ba132006-10-11 01:20:53 -0700795 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796 char *options = (char *) *data;
797
798 if (!options || strncmp(options, "sb=", 3) != 0)
799 return 1; /* Default location */
800 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -0700801 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700802 sb_block = simple_strtoul(options, &options, 0);
803 if (*options && *options != ',') {
Mingming Cao617ba132006-10-11 01:20:53 -0700804 printk("EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700805 (char *) *data);
806 return 1;
807 }
808 if (*options == ',')
809 options++;
810 *data = (void *) options;
811 return sb_block;
812}
813
814static int parse_options (char *options, struct super_block *sb,
815 unsigned int *inum, unsigned long *journal_devnum,
Mingming Cao617ba132006-10-11 01:20:53 -0700816 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817{
Mingming Cao617ba132006-10-11 01:20:53 -0700818 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 char * p;
820 substring_t args[MAX_OPT_ARGS];
821 int data_opt = 0;
822 int option;
823#ifdef CONFIG_QUOTA
824 int qtype;
825 char *qname;
826#endif
827
828 if (!options)
829 return 1;
830
831 while ((p = strsep (&options, ",")) != NULL) {
832 int token;
833 if (!*p)
834 continue;
835
836 token = match_token(p, tokens, args);
837 switch (token) {
838 case Opt_bsd_df:
839 clear_opt (sbi->s_mount_opt, MINIX_DF);
840 break;
841 case Opt_minix_df:
842 set_opt (sbi->s_mount_opt, MINIX_DF);
843 break;
844 case Opt_grpid:
845 set_opt (sbi->s_mount_opt, GRPID);
846 break;
847 case Opt_nogrpid:
848 clear_opt (sbi->s_mount_opt, GRPID);
849 break;
850 case Opt_resuid:
851 if (match_int(&args[0], &option))
852 return 0;
853 sbi->s_resuid = option;
854 break;
855 case Opt_resgid:
856 if (match_int(&args[0], &option))
857 return 0;
858 sbi->s_resgid = option;
859 break;
860 case Opt_sb:
861 /* handled by get_sb_block() instead of here */
862 /* *sb_block = match_int(&args[0]); */
863 break;
864 case Opt_err_panic:
865 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
866 clear_opt (sbi->s_mount_opt, ERRORS_RO);
867 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
868 break;
869 case Opt_err_ro:
870 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
871 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
872 set_opt (sbi->s_mount_opt, ERRORS_RO);
873 break;
874 case Opt_err_cont:
875 clear_opt (sbi->s_mount_opt, ERRORS_RO);
876 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
877 set_opt (sbi->s_mount_opt, ERRORS_CONT);
878 break;
879 case Opt_nouid32:
880 set_opt (sbi->s_mount_opt, NO_UID32);
881 break;
882 case Opt_nocheck:
883 clear_opt (sbi->s_mount_opt, CHECK);
884 break;
885 case Opt_debug:
886 set_opt (sbi->s_mount_opt, DEBUG);
887 break;
888 case Opt_oldalloc:
889 set_opt (sbi->s_mount_opt, OLDALLOC);
890 break;
891 case Opt_orlov:
892 clear_opt (sbi->s_mount_opt, OLDALLOC);
893 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700894#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 case Opt_user_xattr:
896 set_opt (sbi->s_mount_opt, XATTR_USER);
897 break;
898 case Opt_nouser_xattr:
899 clear_opt (sbi->s_mount_opt, XATTR_USER);
900 break;
901#else
902 case Opt_user_xattr:
903 case Opt_nouser_xattr:
Mingming Cao617ba132006-10-11 01:20:53 -0700904 printk("EXT4 (no)user_xattr options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700905 break;
906#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700907#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 case Opt_acl:
909 set_opt(sbi->s_mount_opt, POSIX_ACL);
910 break;
911 case Opt_noacl:
912 clear_opt(sbi->s_mount_opt, POSIX_ACL);
913 break;
914#else
915 case Opt_acl:
916 case Opt_noacl:
Mingming Cao617ba132006-10-11 01:20:53 -0700917 printk("EXT4 (no)acl options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700918 break;
919#endif
920 case Opt_reservation:
921 set_opt(sbi->s_mount_opt, RESERVATION);
922 break;
923 case Opt_noreservation:
924 clear_opt(sbi->s_mount_opt, RESERVATION);
925 break;
926 case Opt_journal_update:
927 /* @@@ FIXME */
928 /* Eventually we will want to be able to create
929 a journal file here. For now, only allow the
930 user to specify an existing inode to be the
931 journal file. */
932 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700933 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 "journal on remount\n");
935 return 0;
936 }
937 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
938 break;
939 case Opt_journal_inum:
940 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700941 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942 "journal on remount\n");
943 return 0;
944 }
945 if (match_int(&args[0], &option))
946 return 0;
947 *inum = option;
948 break;
949 case Opt_journal_dev:
950 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700951 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 "journal on remount\n");
953 return 0;
954 }
955 if (match_int(&args[0], &option))
956 return 0;
957 *journal_devnum = option;
958 break;
959 case Opt_noload:
960 set_opt (sbi->s_mount_opt, NOLOAD);
961 break;
962 case Opt_commit:
963 if (match_int(&args[0], &option))
964 return 0;
965 if (option < 0)
966 return 0;
967 if (option == 0)
968 option = JBD_DEFAULT_MAX_COMMIT_AGE;
969 sbi->s_commit_interval = HZ * option;
970 break;
971 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -0700972 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 goto datacheck;
974 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -0700975 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 goto datacheck;
977 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -0700978 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700979 datacheck:
980 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700981 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 != data_opt) {
983 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700984 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985 "mode on remount\n");
986 return 0;
987 }
988 } else {
Mingming Cao617ba132006-10-11 01:20:53 -0700989 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700990 sbi->s_mount_opt |= data_opt;
991 }
992 break;
993#ifdef CONFIG_QUOTA
994 case Opt_usrjquota:
995 qtype = USRQUOTA;
996 goto set_qf_name;
997 case Opt_grpjquota:
998 qtype = GRPQUOTA;
999set_qf_name:
1000 if (sb_any_quota_enabled(sb)) {
1001 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001002 "EXT4-fs: Cannot change journalled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 "quota options when quota turned on.\n");
1004 return 0;
1005 }
1006 qname = match_strdup(&args[0]);
1007 if (!qname) {
1008 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001009 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 "storing quotafile name.\n");
1011 return 0;
1012 }
1013 if (sbi->s_qf_names[qtype] &&
1014 strcmp(sbi->s_qf_names[qtype], qname)) {
1015 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001016 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001017 "specified.\n", QTYPE2NAME(qtype));
1018 kfree(qname);
1019 return 0;
1020 }
1021 sbi->s_qf_names[qtype] = qname;
1022 if (strchr(sbi->s_qf_names[qtype], '/')) {
1023 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001024 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001025 "filesystem root.\n");
1026 kfree(sbi->s_qf_names[qtype]);
1027 sbi->s_qf_names[qtype] = NULL;
1028 return 0;
1029 }
1030 set_opt(sbi->s_mount_opt, QUOTA);
1031 break;
1032 case Opt_offusrjquota:
1033 qtype = USRQUOTA;
1034 goto clear_qf_name;
1035 case Opt_offgrpjquota:
1036 qtype = GRPQUOTA;
1037clear_qf_name:
1038 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001039 printk(KERN_ERR "EXT4-fs: Cannot change "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 "journalled quota options when "
1041 "quota turned on.\n");
1042 return 0;
1043 }
1044 /*
1045 * The space will be released later when all options
1046 * are confirmed to be correct
1047 */
1048 sbi->s_qf_names[qtype] = NULL;
1049 break;
1050 case Opt_jqfmt_vfsold:
1051 sbi->s_jquota_fmt = QFMT_VFS_OLD;
1052 break;
1053 case Opt_jqfmt_vfsv0:
1054 sbi->s_jquota_fmt = QFMT_VFS_V0;
1055 break;
1056 case Opt_quota:
1057 case Opt_usrquota:
1058 set_opt(sbi->s_mount_opt, QUOTA);
1059 set_opt(sbi->s_mount_opt, USRQUOTA);
1060 break;
1061 case Opt_grpquota:
1062 set_opt(sbi->s_mount_opt, QUOTA);
1063 set_opt(sbi->s_mount_opt, GRPQUOTA);
1064 break;
1065 case Opt_noquota:
1066 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001067 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001068 "options when quota turned on.\n");
1069 return 0;
1070 }
1071 clear_opt(sbi->s_mount_opt, QUOTA);
1072 clear_opt(sbi->s_mount_opt, USRQUOTA);
1073 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1074 break;
1075#else
1076 case Opt_quota:
1077 case Opt_usrquota:
1078 case Opt_grpquota:
1079 case Opt_usrjquota:
1080 case Opt_grpjquota:
1081 case Opt_offusrjquota:
1082 case Opt_offgrpjquota:
1083 case Opt_jqfmt_vfsold:
1084 case Opt_jqfmt_vfsv0:
1085 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001086 "EXT4-fs: journalled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001087 "supported.\n");
1088 break;
1089 case Opt_noquota:
1090 break;
1091#endif
1092 case Opt_abort:
1093 set_opt(sbi->s_mount_opt, ABORT);
1094 break;
1095 case Opt_barrier:
1096 if (match_int(&args[0], &option))
1097 return 0;
1098 if (option)
1099 set_opt(sbi->s_mount_opt, BARRIER);
1100 else
1101 clear_opt(sbi->s_mount_opt, BARRIER);
1102 break;
1103 case Opt_ignore:
1104 break;
1105 case Opt_resize:
1106 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001107 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 "for remount\n");
1109 return 0;
1110 }
1111 if (match_int(&args[0], &option) != 0)
1112 return 0;
1113 *n_blocks_count = option;
1114 break;
1115 case Opt_nobh:
1116 set_opt(sbi->s_mount_opt, NOBH);
1117 break;
1118 case Opt_bh:
1119 clear_opt(sbi->s_mount_opt, NOBH);
1120 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001121 case Opt_extents:
1122 set_opt (sbi->s_mount_opt, EXTENTS);
1123 break;
Mingming Cao1e2462f2007-07-18 09:00:55 -04001124 case Opt_noextents:
1125 clear_opt (sbi->s_mount_opt, EXTENTS);
1126 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127 default:
1128 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001129 "EXT4-fs: Unrecognized mount option \"%s\" "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001130 "or missing value\n", p);
1131 return 0;
1132 }
1133 }
1134#ifdef CONFIG_QUOTA
1135 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001136 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001137 sbi->s_qf_names[USRQUOTA])
1138 clear_opt(sbi->s_mount_opt, USRQUOTA);
1139
Mingming Cao617ba132006-10-11 01:20:53 -07001140 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001141 sbi->s_qf_names[GRPQUOTA])
1142 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1143
1144 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001145 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001147 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1148 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149 "format mixing.\n");
1150 return 0;
1151 }
1152
1153 if (!sbi->s_jquota_fmt) {
Mingming Cao617ba132006-10-11 01:20:53 -07001154 printk(KERN_ERR "EXT4-fs: journalled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001155 "not specified.\n");
1156 return 0;
1157 }
1158 } else {
1159 if (sbi->s_jquota_fmt) {
Mingming Cao617ba132006-10-11 01:20:53 -07001160 printk(KERN_ERR "EXT4-fs: journalled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161 "specified with no journalling "
1162 "enabled.\n");
1163 return 0;
1164 }
1165 }
1166#endif
1167 return 1;
1168}
1169
Mingming Cao617ba132006-10-11 01:20:53 -07001170static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171 int read_only)
1172{
Mingming Cao617ba132006-10-11 01:20:53 -07001173 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 int res = 0;
1175
Mingming Cao617ba132006-10-11 01:20:53 -07001176 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1177 printk (KERN_ERR "EXT4-fs warning: revision level too high, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 "forcing read-only mode\n");
1179 res = MS_RDONLY;
1180 }
1181 if (read_only)
1182 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001183 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1184 printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001186 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001187 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001188 "EXT4-fs warning: mounting fs with errors, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001189 "running e2fsck is recommended\n");
1190 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1191 le16_to_cpu(es->s_mnt_count) >=
1192 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1193 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001194 "EXT4-fs warning: maximal mount count reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001195 "running e2fsck is recommended\n");
1196 else if (le32_to_cpu(es->s_checkinterval) &&
1197 (le32_to_cpu(es->s_lastcheck) +
1198 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1199 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001200 "EXT4-fs warning: checktime reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 "running e2fsck is recommended\n");
1202#if 0
1203 /* @@@ We _will_ want to clear the valid bit if we find
Andrew Morton63f57932006-10-11 01:21:24 -07001204 * inconsistencies, to force a fsck at reboot. But for
1205 * a plain journaled filesystem we can keep it set as
1206 * valid forever! :)
1207 */
Mingming Cao617ba132006-10-11 01:20:53 -07001208 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209#endif
1210 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001211 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001212 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1213 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001214 ext4_update_dynamic_rev(sb);
1215 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216
Mingming Cao617ba132006-10-11 01:20:53 -07001217 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001218 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -07001219 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001220 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1221 sb->s_blocksize,
1222 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001223 EXT4_BLOCKS_PER_GROUP(sb),
1224 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001225 sbi->s_mount_opt);
1226
Mingming Cao617ba132006-10-11 01:20:53 -07001227 printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1228 if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229 char b[BDEVNAME_SIZE];
1230
1231 printk("external journal on %s\n",
Mingming Cao617ba132006-10-11 01:20:53 -07001232 bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001233 } else {
1234 printk("internal journal\n");
1235 }
1236 return res;
1237}
1238
1239/* Called at mount-time, super-block is locked */
Mingming Cao617ba132006-10-11 01:20:53 -07001240static int ext4_check_descriptors (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001241{
Mingming Cao617ba132006-10-11 01:20:53 -07001242 struct ext4_sb_info *sbi = EXT4_SB(sb);
1243 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1244 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001245 ext4_fsblk_t block_bitmap;
1246 ext4_fsblk_t inode_bitmap;
1247 ext4_fsblk_t inode_table;
Mingming Cao617ba132006-10-11 01:20:53 -07001248 struct ext4_group_desc * gdp = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001249 int desc_block = 0;
1250 int i;
1251
Mingming Cao617ba132006-10-11 01:20:53 -07001252 ext4_debug ("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001253
1254 for (i = 0; i < sbi->s_groups_count; i++)
1255 {
1256 if (i == sbi->s_groups_count - 1)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001257 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258 else
1259 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001260 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001261
Mingming Cao617ba132006-10-11 01:20:53 -07001262 if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1263 gdp = (struct ext4_group_desc *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001264 sbi->s_group_desc[desc_block++]->b_data;
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001265 block_bitmap = ext4_block_bitmap(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001266 if (block_bitmap < first_block || block_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001267 {
Mingming Cao617ba132006-10-11 01:20:53 -07001268 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 "Block bitmap for group %d"
Mingming Cao2ae02102006-10-11 01:21:11 -07001270 " not in group (block %llu)!",
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001271 i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001272 return 0;
1273 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001274 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001275 if (inode_bitmap < first_block || inode_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001276 {
Mingming Cao617ba132006-10-11 01:20:53 -07001277 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001278 "Inode bitmap for group %d"
Mingming Cao2ae02102006-10-11 01:21:11 -07001279 " not in group (block %llu)!",
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001280 i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281 return 0;
1282 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001283 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001284 if (inode_table < first_block ||
1285 inode_table + sbi->s_itb_per_group > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 {
Mingming Cao617ba132006-10-11 01:20:53 -07001287 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001288 "Inode table for group %d"
Mingming Cao2ae02102006-10-11 01:21:11 -07001289 " not in group (block %llu)!",
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001290 i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001291 return 0;
1292 }
Mingming Cao617ba132006-10-11 01:20:53 -07001293 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001294 gdp = (struct ext4_group_desc *)
1295 ((__u8 *)gdp + EXT4_DESC_SIZE(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296 }
1297
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001298 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07001299 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001300 return 1;
1301}
1302
1303
Mingming Cao617ba132006-10-11 01:20:53 -07001304/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001305 * the superblock) which were deleted from all directories, but held open by
1306 * a process at the time of a crash. We walk the list and try to delete these
1307 * inodes at recovery time (only with a read-write filesystem).
1308 *
1309 * In order to keep the orphan inode chain consistent during traversal (in
1310 * case of crash during recovery), we link each inode into the superblock
1311 * orphan list_head and handle it the same way as an inode deletion during
1312 * normal operation (which journals the operations for us).
1313 *
1314 * We only do an iget() and an iput() on each inode, which is very safe if we
1315 * accidentally point at an in-use or already deleted inode. The worst that
1316 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001317 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001318 * e2fsck was run on this filesystem, and it must have already done the orphan
1319 * inode cleanup for us, so we can safely abort without any further action.
1320 */
Mingming Cao617ba132006-10-11 01:20:53 -07001321static void ext4_orphan_cleanup (struct super_block * sb,
1322 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001323{
1324 unsigned int s_flags = sb->s_flags;
1325 int nr_orphans = 0, nr_truncates = 0;
1326#ifdef CONFIG_QUOTA
1327 int i;
1328#endif
1329 if (!es->s_last_orphan) {
1330 jbd_debug(4, "no orphan inodes to clean up\n");
1331 return;
1332 }
1333
Eric Sandeena8f48a92006-12-06 20:40:13 -08001334 if (bdev_read_only(sb->s_bdev)) {
1335 printk(KERN_ERR "EXT4-fs: write access "
1336 "unavailable, skipping orphan cleanup.\n");
1337 return;
1338 }
1339
Mingming Cao617ba132006-10-11 01:20:53 -07001340 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001341 if (es->s_last_orphan)
1342 jbd_debug(1, "Errors on filesystem, "
1343 "clearing orphan list.\n");
1344 es->s_last_orphan = 0;
1345 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1346 return;
1347 }
1348
1349 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001350 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 sb->s_id);
1352 sb->s_flags &= ~MS_RDONLY;
1353 }
1354#ifdef CONFIG_QUOTA
1355 /* Needed for iput() to work correctly and not trash data */
1356 sb->s_flags |= MS_ACTIVE;
1357 /* Turn on quotas so that they are updated correctly */
1358 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001359 if (EXT4_SB(sb)->s_qf_names[i]) {
1360 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361 if (ret < 0)
1362 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001363 "EXT4-fs: Cannot turn on journalled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001364 "quota: error %d\n", ret);
1365 }
1366 }
1367#endif
1368
1369 while (es->s_last_orphan) {
1370 struct inode *inode;
1371
1372 if (!(inode =
Mingming Cao617ba132006-10-11 01:20:53 -07001373 ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374 es->s_last_orphan = 0;
1375 break;
1376 }
1377
Mingming Cao617ba132006-10-11 01:20:53 -07001378 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001379 DQUOT_INIT(inode);
1380 if (inode->i_nlink) {
1381 printk(KERN_DEBUG
1382 "%s: truncating inode %lu to %Ld bytes\n",
1383 __FUNCTION__, inode->i_ino, inode->i_size);
1384 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1385 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001386 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001387 nr_truncates++;
1388 } else {
1389 printk(KERN_DEBUG
1390 "%s: deleting unreferenced inode %lu\n",
1391 __FUNCTION__, inode->i_ino);
1392 jbd_debug(2, "deleting unreferenced inode %lu\n",
1393 inode->i_ino);
1394 nr_orphans++;
1395 }
1396 iput(inode); /* The delete magic happens here! */
1397 }
1398
1399#define PLURAL(x) (x), ((x)==1) ? "" : "s"
1400
1401 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001402 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403 sb->s_id, PLURAL(nr_orphans));
1404 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001405 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406 sb->s_id, PLURAL(nr_truncates));
1407#ifdef CONFIG_QUOTA
1408 /* Turn quotas off */
1409 for (i = 0; i < MAXQUOTAS; i++) {
1410 if (sb_dqopt(sb)->files[i])
1411 vfs_quota_off(sb, i);
1412 }
1413#endif
1414 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1415}
1416
1417#define log2(n) ffz(~(n))
1418
1419/*
1420 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1421 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1422 * We need to be 1 filesystem block less than the 2^32 sector limit.
1423 */
Mingming Cao617ba132006-10-11 01:20:53 -07001424static loff_t ext4_max_size(int bits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001425{
Mingming Cao617ba132006-10-11 01:20:53 -07001426 loff_t res = EXT4_NDIR_BLOCKS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001427 /* This constant is calculated to be the largest file size for a
1428 * dense, 4k-blocksize file such that the total number of
1429 * sectors in the file, including data and all indirect blocks,
1430 * does not exceed 2^32. */
1431 const loff_t upper_limit = 0x1ff7fffd000LL;
1432
1433 res += 1LL << (bits-2);
1434 res += 1LL << (2*(bits-2));
1435 res += 1LL << (3*(bits-2));
1436 res <<= bits;
1437 if (res > upper_limit)
1438 res = upper_limit;
1439 return res;
1440}
1441
Mingming Cao617ba132006-10-11 01:20:53 -07001442static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001443 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001444{
Mingming Cao617ba132006-10-11 01:20:53 -07001445 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001446 unsigned long bg, first_meta_bg;
1447 int has_super = 0;
1448
1449 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1450
Mingming Cao617ba132006-10-11 01:20:53 -07001451 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001453 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001455 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001457 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458}
1459
1460
Mingming Cao617ba132006-10-11 01:20:53 -07001461static int ext4_fill_super (struct super_block *sb, void *data, int silent)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462{
1463 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001464 struct ext4_super_block *es = NULL;
1465 struct ext4_sb_info *sbi;
1466 ext4_fsblk_t block;
1467 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001468 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001469 unsigned long offset = 0;
1470 unsigned int journal_inum = 0;
1471 unsigned long journal_devnum = 0;
1472 unsigned long def_mount_opts;
1473 struct inode *root;
1474 int blocksize;
1475 int hblock;
1476 int db_count;
1477 int i;
1478 int needs_recovery;
1479 __le32 features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001480 __u64 blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001481
1482 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1483 if (!sbi)
1484 return -ENOMEM;
1485 sb->s_fs_info = sbi;
1486 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001487 sbi->s_resuid = EXT4_DEF_RESUID;
1488 sbi->s_resgid = EXT4_DEF_RESGID;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489
1490 unlock_kernel();
1491
Mingming Cao617ba132006-10-11 01:20:53 -07001492 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001493 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07001494 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001495 goto out_fail;
1496 }
1497
1498 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001499 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001500 * block sizes. We need to calculate the offset from buffer start.
1501 */
Mingming Cao617ba132006-10-11 01:20:53 -07001502 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001503 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1504 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001506 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 }
1508
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001509 if (!(bh = sb_bread(sb, logical_sb_block))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001510 printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 goto out_fail;
1512 }
1513 /*
1514 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07001515 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516 */
Mingming Cao617ba132006-10-11 01:20:53 -07001517 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001518 sbi->s_es = es;
1519 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07001520 if (sb->s_magic != EXT4_SUPER_MAGIC)
1521 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522
1523 /* Set defaults before we parse the mount options */
1524 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07001525 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001526 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07001527 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001528 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07001529 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530 set_opt(sbi->s_mount_opt, NO_UID32);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001531#ifdef CONFIG_EXT4DEV_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07001532 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001533 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001534#endif
1535#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07001536 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001538#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001539 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1540 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1541 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1542 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1543 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1544 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545
Mingming Cao617ba132006-10-11 01:20:53 -07001546 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001547 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Mingming Cao617ba132006-10-11 01:20:53 -07001548 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001549 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dmitry Mishinceea16b2006-10-11 01:21:21 -07001550 else
1551 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552
1553 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1554 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1555
1556 set_opt(sbi->s_mount_opt, RESERVATION);
1557
Mingming Cao1e2462f2007-07-18 09:00:55 -04001558 /*
1559 * turn on extents feature by default in ext4 filesystem
1560 * User -o noextents to turn it off
1561 */
1562 set_opt(sbi->s_mount_opt, EXTENTS);
1563
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001564 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1565 NULL, 0))
1566 goto failed_mount;
1567
1568 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07001569 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001570
Mingming Cao617ba132006-10-11 01:20:53 -07001571 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1572 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1573 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1574 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001575 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001576 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001577 "running e2fsck is recommended\n");
1578 /*
1579 * Check feature flags regardless of the revision level, since we
1580 * previously didn't change the revision level when setting the flags,
1581 * so there is a chance incompat flags are set on a rev 0 filesystem.
1582 */
Mingming Cao617ba132006-10-11 01:20:53 -07001583 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001585 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001586 "unsupported optional features (%x).\n",
1587 sb->s_id, le32_to_cpu(features));
1588 goto failed_mount;
1589 }
Mingming Cao617ba132006-10-11 01:20:53 -07001590 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001591 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001592 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593 "unsupported optional features (%x).\n",
1594 sb->s_id, le32_to_cpu(features));
1595 goto failed_mount;
1596 }
1597 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1598
Mingming Cao617ba132006-10-11 01:20:53 -07001599 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1600 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001601 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001602 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603 blocksize, sb->s_id);
1604 goto failed_mount;
1605 }
1606
1607 hblock = bdev_hardsect_size(sb->s_bdev);
1608 if (sb->s_blocksize != blocksize) {
1609 /*
1610 * Make sure the blocksize for the filesystem is larger
1611 * than the hardware sectorsize for the machine.
1612 */
1613 if (blocksize < hblock) {
Mingming Cao617ba132006-10-11 01:20:53 -07001614 printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615 "device blocksize %d.\n", blocksize, hblock);
1616 goto failed_mount;
1617 }
1618
1619 brelse (bh);
1620 sb_set_blocksize(sb, blocksize);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001621 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1622 offset = do_div(logical_sb_block, blocksize);
1623 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001624 if (!bh) {
1625 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001626 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001627 goto failed_mount;
1628 }
Mingming Cao617ba132006-10-11 01:20:53 -07001629 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001630 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07001631 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001633 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001634 goto failed_mount;
1635 }
1636 }
1637
Mingming Cao617ba132006-10-11 01:20:53 -07001638 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639
Mingming Cao617ba132006-10-11 01:20:53 -07001640 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
1641 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
1642 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643 } else {
1644 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1645 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07001646 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1648 (sbi->s_inode_size > blocksize)) {
1649 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001650 "EXT4-fs: unsupported inode size: %d\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001651 sbi->s_inode_size);
1652 goto failed_mount;
1653 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04001654 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
1655 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656 }
Mingming Cao617ba132006-10-11 01:20:53 -07001657 sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001658 le32_to_cpu(es->s_log_frag_size);
1659 if (blocksize != sbi->s_frag_size) {
1660 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001661 "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001662 sbi->s_frag_size, blocksize);
1663 goto failed_mount;
1664 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001665 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
1666 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001667 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001668 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
1669 sbi->s_desc_size & (sbi->s_desc_size - 1)) {
1670 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001671 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001672 sbi->s_desc_size);
1673 goto failed_mount;
1674 }
1675 } else
1676 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001677 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1678 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1679 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Mingming Cao617ba132006-10-11 01:20:53 -07001680 if (EXT4_INODE_SIZE(sb) == 0)
1681 goto cantfind_ext4;
1682 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001683 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07001684 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001685 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1686 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001687 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001688 sbi->s_sbh = bh;
1689 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07001690 sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
1691 sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001692 for (i=0; i < 4; i++)
1693 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1694 sbi->s_def_hash_version = es->s_def_hash_version;
1695
1696 if (sbi->s_blocks_per_group > blocksize * 8) {
1697 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001698 "EXT4-fs: #blocks per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001699 sbi->s_blocks_per_group);
1700 goto failed_mount;
1701 }
1702 if (sbi->s_frags_per_group > blocksize * 8) {
1703 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001704 "EXT4-fs: #fragments per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001705 sbi->s_frags_per_group);
1706 goto failed_mount;
1707 }
1708 if (sbi->s_inodes_per_group > blocksize * 8) {
1709 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001710 "EXT4-fs: #inodes per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001711 sbi->s_inodes_per_group);
1712 goto failed_mount;
1713 }
1714
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001715 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001716 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001717 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001718 " too large to mount safely\n", sb->s_id);
1719 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07001720 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001721 "enabled\n");
1722 goto failed_mount;
1723 }
1724
Mingming Cao617ba132006-10-11 01:20:53 -07001725 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
1726 goto cantfind_ext4;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001727 blocks_count = (ext4_blocks_count(es) -
1728 le32_to_cpu(es->s_first_data_block) +
1729 EXT4_BLOCKS_PER_GROUP(sb) - 1);
1730 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
1731 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07001732 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
1733 EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001734 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1735 GFP_KERNEL);
1736 if (sbi->s_group_desc == NULL) {
Mingming Cao617ba132006-10-11 01:20:53 -07001737 printk (KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001738 goto failed_mount;
1739 }
1740
1741 bgl_lock_init(&sbi->s_blockgroup_lock);
1742
1743 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001744 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745 sbi->s_group_desc[i] = sb_bread(sb, block);
1746 if (!sbi->s_group_desc[i]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001747 printk (KERN_ERR "EXT4-fs: "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001748 "can't read group descriptor %d\n", i);
1749 db_count = i;
1750 goto failed_mount2;
1751 }
1752 }
Mingming Cao617ba132006-10-11 01:20:53 -07001753 if (!ext4_check_descriptors (sb)) {
1754 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755 goto failed_mount2;
1756 }
1757 sbi->s_gdb_count = db_count;
1758 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1759 spin_lock_init(&sbi->s_next_gen_lock);
1760
1761 percpu_counter_init(&sbi->s_freeblocks_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001762 ext4_count_free_blocks(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001763 percpu_counter_init(&sbi->s_freeinodes_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001764 ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001765 percpu_counter_init(&sbi->s_dirs_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001766 ext4_count_dirs(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001767
1768 /* per fileystem reservation list head & lock */
1769 spin_lock_init(&sbi->s_rsv_window_lock);
1770 sbi->s_rsv_window_root = RB_ROOT;
1771 /* Add a single, static dummy reservation to the start of the
1772 * reservation window list --- it gives us a placeholder for
1773 * append-at-start-of-list which makes the allocation logic
1774 * _much_ simpler. */
Mingming Cao617ba132006-10-11 01:20:53 -07001775 sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1776 sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001777 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1778 sbi->s_rsv_window_head.rsv_goal_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001779 ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001780
1781 /*
1782 * set up enough so that it can read an inode
1783 */
Mingming Cao617ba132006-10-11 01:20:53 -07001784 sb->s_op = &ext4_sops;
1785 sb->s_export_op = &ext4_export_ops;
1786 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001788 sb->s_qcop = &ext4_qctl_operations;
1789 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001790#endif
1791 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1792
1793 sb->s_root = NULL;
1794
1795 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07001796 EXT4_HAS_INCOMPAT_FEATURE(sb,
1797 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798
1799 /*
1800 * The first inode we look at is the journal inode. Don't try
1801 * root first: it may be modified in the journal!
1802 */
1803 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07001804 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
1805 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001806 goto failed_mount3;
1807 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07001808 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001809 goto failed_mount3;
1810 } else {
1811 if (!silent)
1812 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001813 "ext4: No journal on filesystem on %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001814 sb->s_id);
1815 goto failed_mount3;
1816 }
1817
Jose R. Santoseb40a092007-07-18 08:37:25 -04001818 if (ext4_blocks_count(es) > 0xffffffffULL &&
1819 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
1820 JBD2_FEATURE_INCOMPAT_64BIT)) {
1821 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
1822 goto failed_mount4;
1823 }
1824
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001825 /* We have now updated the journal if required, so we can
1826 * validate the data journaling mode. */
1827 switch (test_opt(sb, DATA_FLAGS)) {
1828 case 0:
1829 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07001830 * capabilities: ORDERED_DATA if the journal can
1831 * cope, else JOURNAL_DATA
1832 */
Mingming Caodab291a2006-10-11 01:21:01 -07001833 if (jbd2_journal_check_available_features
1834 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001835 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1836 else
1837 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1838 break;
1839
Mingming Cao617ba132006-10-11 01:20:53 -07001840 case EXT4_MOUNT_ORDERED_DATA:
1841 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07001842 if (!jbd2_journal_check_available_features
1843 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001844 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001845 "requested data journaling mode\n");
1846 goto failed_mount4;
1847 }
1848 default:
1849 break;
1850 }
1851
1852 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001853 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
1854 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001855 "its supported only with writeback mode\n");
1856 clear_opt(sbi->s_mount_opt, NOBH);
1857 }
1858 }
1859 /*
Mingming Caodab291a2006-10-11 01:21:01 -07001860 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001861 * so we can safely mount the rest of the filesystem now.
1862 */
1863
Mingming Cao617ba132006-10-11 01:20:53 -07001864 root = iget(sb, EXT4_ROOT_INO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001865 sb->s_root = d_alloc_root(root);
1866 if (!sb->s_root) {
Mingming Cao617ba132006-10-11 01:20:53 -07001867 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001868 iput(root);
1869 goto failed_mount4;
1870 }
1871 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1872 dput(sb->s_root);
1873 sb->s_root = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001874 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001875 goto failed_mount4;
1876 }
1877
Mingming Cao617ba132006-10-11 01:20:53 -07001878 ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04001879
1880 /* determine the minimum size of new large inodes, if present */
1881 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
1882 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
1883 EXT4_GOOD_OLD_INODE_SIZE;
1884 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
1885 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
1886 if (sbi->s_want_extra_isize <
1887 le16_to_cpu(es->s_want_extra_isize))
1888 sbi->s_want_extra_isize =
1889 le16_to_cpu(es->s_want_extra_isize);
1890 if (sbi->s_want_extra_isize <
1891 le16_to_cpu(es->s_min_extra_isize))
1892 sbi->s_want_extra_isize =
1893 le16_to_cpu(es->s_min_extra_isize);
1894 }
1895 }
1896 /* Check if enough inode space is available */
1897 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
1898 sbi->s_inode_size) {
1899 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
1900 EXT4_GOOD_OLD_INODE_SIZE;
1901 printk(KERN_INFO "EXT4-fs: required extra inode space not"
1902 "available.\n");
1903 }
1904
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001905 /*
1906 * akpm: core read_super() calls in here with the superblock locked.
1907 * That deadlocks, because orphan cleanup needs to lock the superblock
1908 * in numerous places. Here we just pop the lock - it's relatively
1909 * harmless, because we are now ready to accept write_super() requests,
1910 * and aviro says that's the only reason for hanging onto the
1911 * superblock lock.
1912 */
Mingming Cao617ba132006-10-11 01:20:53 -07001913 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
1914 ext4_orphan_cleanup(sb, es);
1915 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001916 if (needs_recovery)
Mingming Cao617ba132006-10-11 01:20:53 -07001917 printk (KERN_INFO "EXT4-fs: recovery complete.\n");
1918 ext4_mark_recovery_complete(sb, es);
1919 printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
1920 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
1921 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001922 "writeback");
1923
Alex Tomasa86c6182006-10-11 01:21:03 -07001924 ext4_ext_init(sb);
1925
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001926 lock_kernel();
1927 return 0;
1928
Mingming Cao617ba132006-10-11 01:20:53 -07001929cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001930 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07001931 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001932 sb->s_id);
1933 goto failed_mount;
1934
1935failed_mount4:
Mingming Caodab291a2006-10-11 01:21:01 -07001936 jbd2_journal_destroy(sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001937failed_mount3:
1938 percpu_counter_destroy(&sbi->s_freeblocks_counter);
1939 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1940 percpu_counter_destroy(&sbi->s_dirs_counter);
1941failed_mount2:
1942 for (i = 0; i < db_count; i++)
1943 brelse(sbi->s_group_desc[i]);
1944 kfree(sbi->s_group_desc);
1945failed_mount:
1946#ifdef CONFIG_QUOTA
1947 for (i = 0; i < MAXQUOTAS; i++)
1948 kfree(sbi->s_qf_names[i]);
1949#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001950 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001951 brelse(bh);
1952out_fail:
1953 sb->s_fs_info = NULL;
1954 kfree(sbi);
1955 lock_kernel();
1956 return -EINVAL;
1957}
1958
1959/*
1960 * Setup any per-fs journal parameters now. We'll do this both on
1961 * initial mount, once the journal has been initialised but before we've
1962 * done any recovery; and again on any subsequent remount.
1963 */
Mingming Cao617ba132006-10-11 01:20:53 -07001964static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001965{
Mingming Cao617ba132006-10-11 01:20:53 -07001966 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001967
1968 if (sbi->s_commit_interval)
1969 journal->j_commit_interval = sbi->s_commit_interval;
Mingming Cao617ba132006-10-11 01:20:53 -07001970 /* We could also set up an ext4-specific default for the commit
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001971 * interval here, but for now we'll just fall back to the jbd
1972 * default. */
1973
1974 spin_lock(&journal->j_state_lock);
1975 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07001976 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001977 else
Mingming Caodab291a2006-10-11 01:21:01 -07001978 journal->j_flags &= ~JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001979 spin_unlock(&journal->j_state_lock);
1980}
1981
Mingming Cao617ba132006-10-11 01:20:53 -07001982static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001983 unsigned int journal_inum)
1984{
1985 struct inode *journal_inode;
1986 journal_t *journal;
1987
1988 /* First, test for the existence of a valid inode on disk. Bad
1989 * things happen if we iget() an unused inode, as the subsequent
1990 * iput() will try to delete it. */
1991
1992 journal_inode = iget(sb, journal_inum);
1993 if (!journal_inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001994 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001995 return NULL;
1996 }
1997 if (!journal_inode->i_nlink) {
1998 make_bad_inode(journal_inode);
1999 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002000 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002001 return NULL;
2002 }
2003
2004 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
2005 journal_inode, journal_inode->i_size);
2006 if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002007 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002008 iput(journal_inode);
2009 return NULL;
2010 }
2011
Mingming Caodab291a2006-10-11 01:21:01 -07002012 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002014 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002015 iput(journal_inode);
2016 return NULL;
2017 }
2018 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002019 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002020 return journal;
2021}
2022
Mingming Cao617ba132006-10-11 01:20:53 -07002023static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002024 dev_t j_dev)
2025{
2026 struct buffer_head * bh;
2027 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07002028 ext4_fsblk_t start;
2029 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002030 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07002031 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002032 unsigned long offset;
Mingming Cao617ba132006-10-11 01:20:53 -07002033 struct ext4_super_block * es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 struct block_device *bdev;
2035
Mingming Cao617ba132006-10-11 01:20:53 -07002036 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002037 if (bdev == NULL)
2038 return NULL;
2039
2040 if (bd_claim(bdev, sb)) {
2041 printk(KERN_ERR
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002042 "EXT4: failed to claim external journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002043 blkdev_put(bdev);
2044 return NULL;
2045 }
2046
2047 blocksize = sb->s_blocksize;
2048 hblock = bdev_hardsect_size(bdev);
2049 if (blocksize < hblock) {
2050 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002051 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052 goto out_bdev;
2053 }
2054
Mingming Cao617ba132006-10-11 01:20:53 -07002055 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2056 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057 set_blocksize(bdev, blocksize);
2058 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002059 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002060 "external journal\n");
2061 goto out_bdev;
2062 }
2063
Mingming Cao617ba132006-10-11 01:20:53 -07002064 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2065 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002066 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002067 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2068 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002069 "bad superblock\n");
2070 brelse(bh);
2071 goto out_bdev;
2072 }
2073
Mingming Cao617ba132006-10-11 01:20:53 -07002074 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2075 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002076 brelse(bh);
2077 goto out_bdev;
2078 }
2079
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002080 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081 start = sb_block + 1;
2082 brelse(bh); /* we're done with the superblock */
2083
Mingming Caodab291a2006-10-11 01:21:01 -07002084 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002085 start, len, blocksize);
2086 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002087 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002088 goto out_bdev;
2089 }
2090 journal->j_private = sb;
2091 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2092 wait_on_buffer(journal->j_sb_buffer);
2093 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002094 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002095 goto out_journal;
2096 }
2097 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002098 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002099 "user (unsupported) - %d\n",
2100 be32_to_cpu(journal->j_superblock->s_nr_users));
2101 goto out_journal;
2102 }
Mingming Cao617ba132006-10-11 01:20:53 -07002103 EXT4_SB(sb)->journal_bdev = bdev;
2104 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002105 return journal;
2106out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002107 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002109 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 return NULL;
2111}
2112
Mingming Cao617ba132006-10-11 01:20:53 -07002113static int ext4_load_journal(struct super_block *sb,
2114 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002115 unsigned long journal_devnum)
2116{
2117 journal_t *journal;
2118 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2119 dev_t journal_dev;
2120 int err = 0;
2121 int really_read_only;
2122
2123 if (journal_devnum &&
2124 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002125 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002126 "numbers have changed\n");
2127 journal_dev = new_decode_dev(journal_devnum);
2128 } else
2129 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2130
2131 really_read_only = bdev_read_only(sb->s_bdev);
2132
2133 /*
2134 * Are we loading a blank journal or performing recovery after a
2135 * crash? For recovery, we need to check in advance whether we
2136 * can get read-write access to the device.
2137 */
2138
Mingming Cao617ba132006-10-11 01:20:53 -07002139 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002140 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002141 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002142 "required on readonly filesystem.\n");
2143 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002144 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002145 "unavailable, cannot proceed.\n");
2146 return -EROFS;
2147 }
Mingming Cao617ba132006-10-11 01:20:53 -07002148 printk (KERN_INFO "EXT4-fs: write access will "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002149 "be enabled during recovery.\n");
2150 }
2151 }
2152
2153 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002154 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002155 "and inode journals!\n");
2156 return -EINVAL;
2157 }
2158
2159 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002160 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002161 return -EINVAL;
2162 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002163 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002164 return -EINVAL;
2165 }
2166
2167 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002168 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002170 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002171 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002172 return err;
2173 }
2174 }
2175
Mingming Cao617ba132006-10-11 01:20:53 -07002176 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002177 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002178 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002179 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002180
2181 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002182 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002183 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002184 return err;
2185 }
2186
Mingming Cao617ba132006-10-11 01:20:53 -07002187 EXT4_SB(sb)->s_journal = journal;
2188 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002189
2190 if (journal_devnum &&
2191 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2192 es->s_journal_dev = cpu_to_le32(journal_devnum);
2193 sb->s_dirt = 1;
2194
2195 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002196 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002197 }
2198
2199 return 0;
2200}
2201
Mingming Cao617ba132006-10-11 01:20:53 -07002202static int ext4_create_journal(struct super_block * sb,
2203 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002204 unsigned int journal_inum)
2205{
2206 journal_t *journal;
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002207 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002208
2209 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002210 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002211 "create journal.\n");
2212 return -EROFS;
2213 }
2214
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002215 journal = ext4_get_journal(sb, journal_inum);
2216 if (!journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002217 return -EINVAL;
2218
Mingming Cao617ba132006-10-11 01:20:53 -07002219 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002220 journal_inum);
2221
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002222 err = jbd2_journal_create(journal);
2223 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002224 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002225 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002226 return -EIO;
2227 }
2228
Mingming Cao617ba132006-10-11 01:20:53 -07002229 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002230
Mingming Cao617ba132006-10-11 01:20:53 -07002231 ext4_update_dynamic_rev(sb);
2232 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2233 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002234
2235 es->s_journal_inum = cpu_to_le32(journal_inum);
2236 sb->s_dirt = 1;
2237
2238 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002239 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002240
2241 return 0;
2242}
2243
Mingming Cao617ba132006-10-11 01:20:53 -07002244static void ext4_commit_super (struct super_block * sb,
2245 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002246 int sync)
2247{
Mingming Cao617ba132006-10-11 01:20:53 -07002248 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002249
2250 if (!sbh)
2251 return;
2252 es->s_wtime = cpu_to_le32(get_seconds());
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002253 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07002254 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002255 BUFFER_TRACE(sbh, "marking dirty");
2256 mark_buffer_dirty(sbh);
2257 if (sync)
2258 sync_dirty_buffer(sbh);
2259}
2260
2261
2262/*
2263 * Have we just finished recovery? If so, and if we are mounting (or
2264 * remounting) the filesystem readonly, then we will end up with a
2265 * consistent fs on disk. Record that fact.
2266 */
Mingming Cao617ba132006-10-11 01:20:53 -07002267static void ext4_mark_recovery_complete(struct super_block * sb,
2268 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002269{
Mingming Cao617ba132006-10-11 01:20:53 -07002270 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271
Mingming Caodab291a2006-10-11 01:21:01 -07002272 jbd2_journal_lock_updates(journal);
2273 jbd2_journal_flush(journal);
Jan Kara32c37732007-07-15 23:41:09 -07002274 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002275 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002276 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002277 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002278 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002279 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002280 }
Jan Kara32c37732007-07-15 23:41:09 -07002281 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002282 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002283}
2284
2285/*
2286 * If we are mounting (or read-write remounting) a filesystem whose journal
2287 * has recorded an error from a previous lifetime, move that error to the
2288 * main filesystem now.
2289 */
Mingming Cao617ba132006-10-11 01:20:53 -07002290static void ext4_clear_journal_err(struct super_block * sb,
2291 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002292{
2293 journal_t *journal;
2294 int j_errno;
2295 const char *errstr;
2296
Mingming Cao617ba132006-10-11 01:20:53 -07002297 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002298
2299 /*
2300 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07002301 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002302 */
2303
Mingming Caodab291a2006-10-11 01:21:01 -07002304 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002305 if (j_errno) {
2306 char nbuf[16];
2307
Mingming Cao617ba132006-10-11 01:20:53 -07002308 errstr = ext4_decode_error(sb, j_errno, nbuf);
2309 ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002310 "from previous mount: %s", errstr);
Mingming Cao617ba132006-10-11 01:20:53 -07002311 ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002312 "filesystem check.");
2313
Mingming Cao617ba132006-10-11 01:20:53 -07002314 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2315 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2316 ext4_commit_super (sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002317
Mingming Caodab291a2006-10-11 01:21:01 -07002318 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002319 }
2320}
2321
2322/*
2323 * Force the running and committing transactions to commit,
2324 * and wait on the commit.
2325 */
Mingming Cao617ba132006-10-11 01:20:53 -07002326int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002327{
2328 journal_t *journal;
2329 int ret;
2330
2331 if (sb->s_flags & MS_RDONLY)
2332 return 0;
2333
Mingming Cao617ba132006-10-11 01:20:53 -07002334 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002335 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002336 ret = ext4_journal_force_commit(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002337 return ret;
2338}
2339
2340/*
Mingming Cao617ba132006-10-11 01:20:53 -07002341 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002342 * have to propagate any other updates to the superblock on disk at this
2343 * point. Just start an async writeback to get the buffers on their way
2344 * to the disk.
2345 *
2346 * This implicitly triggers the writebehind on sync().
2347 */
2348
Mingming Cao617ba132006-10-11 01:20:53 -07002349static void ext4_write_super (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002350{
2351 if (mutex_trylock(&sb->s_lock) != 0)
2352 BUG();
2353 sb->s_dirt = 0;
2354}
2355
Mingming Cao617ba132006-10-11 01:20:53 -07002356static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002357{
2358 tid_t target;
2359
2360 sb->s_dirt = 0;
Mingming Caodab291a2006-10-11 01:21:01 -07002361 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002362 if (wait)
Mingming Caodab291a2006-10-11 01:21:01 -07002363 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002364 }
2365 return 0;
2366}
2367
2368/*
2369 * LVM calls this function before a (read-only) snapshot is created. This
2370 * gives us a chance to flush the journal completely and mark the fs clean.
2371 */
Mingming Cao617ba132006-10-11 01:20:53 -07002372static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002373{
2374 sb->s_dirt = 0;
2375
2376 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002377 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002378
2379 /* Now we set up the journal barrier. */
Mingming Caodab291a2006-10-11 01:21:01 -07002380 jbd2_journal_lock_updates(journal);
2381 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002382
2383 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07002384 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2385 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002386 }
2387}
2388
2389/*
2390 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2391 * flag here, even though the filesystem is not technically dirty yet.
2392 */
Mingming Cao617ba132006-10-11 01:20:53 -07002393static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002394{
2395 if (!(sb->s_flags & MS_RDONLY)) {
2396 lock_super(sb);
2397 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07002398 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2399 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002400 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002401 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002402 }
2403}
2404
Mingming Cao617ba132006-10-11 01:20:53 -07002405static int ext4_remount (struct super_block * sb, int * flags, char * data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002406{
Mingming Cao617ba132006-10-11 01:20:53 -07002407 struct ext4_super_block * es;
2408 struct ext4_sb_info *sbi = EXT4_SB(sb);
2409 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002410 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07002411 struct ext4_mount_options old_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002412 int err;
2413#ifdef CONFIG_QUOTA
2414 int i;
2415#endif
2416
2417 /* Store the original options */
2418 old_sb_flags = sb->s_flags;
2419 old_opts.s_mount_opt = sbi->s_mount_opt;
2420 old_opts.s_resuid = sbi->s_resuid;
2421 old_opts.s_resgid = sbi->s_resgid;
2422 old_opts.s_commit_interval = sbi->s_commit_interval;
2423#ifdef CONFIG_QUOTA
2424 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2425 for (i = 0; i < MAXQUOTAS; i++)
2426 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2427#endif
2428
2429 /*
2430 * Allow the "check" option to be passed as a remount option.
2431 */
2432 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2433 err = -EINVAL;
2434 goto restore_opts;
2435 }
2436
Mingming Cao617ba132006-10-11 01:20:53 -07002437 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2438 ext4_abort(sb, __FUNCTION__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439
2440 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002441 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002442
2443 es = sbi->s_es;
2444
Mingming Cao617ba132006-10-11 01:20:53 -07002445 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002446
2447 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002448 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002449 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002450 err = -EROFS;
2451 goto restore_opts;
2452 }
2453
2454 if (*flags & MS_RDONLY) {
2455 /*
2456 * First of all, the unconditional stuff we have to do
2457 * to disable replay of the journal when we next remount
2458 */
2459 sb->s_flags |= MS_RDONLY;
2460
2461 /*
2462 * OK, test if we are remounting a valid rw partition
2463 * readonly, and if so set the rdonly flag and then
2464 * mark the partition as valid again.
2465 */
Mingming Cao617ba132006-10-11 01:20:53 -07002466 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2467 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002468 es->s_state = cpu_to_le16(sbi->s_mount_state);
2469
Jan Kara32c37732007-07-15 23:41:09 -07002470 /*
2471 * We have to unlock super so that we can wait for
2472 * transactions.
2473 */
2474 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002475 ext4_mark_recovery_complete(sb, es);
Jan Kara32c37732007-07-15 23:41:09 -07002476 lock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002477 } else {
2478 __le32 ret;
Mingming Cao617ba132006-10-11 01:20:53 -07002479 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2480 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2481 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002482 "remount RDWR because of unsupported "
2483 "optional features (%x).\n",
2484 sb->s_id, le32_to_cpu(ret));
2485 err = -EROFS;
2486 goto restore_opts;
2487 }
Eric Sandeenead65962007-02-10 01:46:08 -08002488
2489 /*
2490 * If we have an unprocessed orphan list hanging
2491 * around from a previously readonly bdev mount,
2492 * require a full umount/remount for now.
2493 */
2494 if (es->s_last_orphan) {
2495 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2496 "remount RDWR because of unprocessed "
2497 "orphan inode list. Please "
2498 "umount/remount instead.\n",
2499 sb->s_id);
2500 err = -EINVAL;
2501 goto restore_opts;
2502 }
2503
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002504 /*
2505 * Mounting a RDONLY partition read-write, so reread
2506 * and store the current valid flag. (It may have
2507 * been changed by e2fsck since we originally mounted
2508 * the partition.)
2509 */
Mingming Cao617ba132006-10-11 01:20:53 -07002510 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002511 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07002512 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002513 goto restore_opts;
Mingming Cao617ba132006-10-11 01:20:53 -07002514 if (!ext4_setup_super (sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002515 sb->s_flags &= ~MS_RDONLY;
2516 }
2517 }
2518#ifdef CONFIG_QUOTA
2519 /* Release old quota file names */
2520 for (i = 0; i < MAXQUOTAS; i++)
2521 if (old_opts.s_qf_names[i] &&
2522 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2523 kfree(old_opts.s_qf_names[i]);
2524#endif
2525 return 0;
2526restore_opts:
2527 sb->s_flags = old_sb_flags;
2528 sbi->s_mount_opt = old_opts.s_mount_opt;
2529 sbi->s_resuid = old_opts.s_resuid;
2530 sbi->s_resgid = old_opts.s_resgid;
2531 sbi->s_commit_interval = old_opts.s_commit_interval;
2532#ifdef CONFIG_QUOTA
2533 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2534 for (i = 0; i < MAXQUOTAS; i++) {
2535 if (sbi->s_qf_names[i] &&
2536 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2537 kfree(sbi->s_qf_names[i]);
2538 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2539 }
2540#endif
2541 return err;
2542}
2543
Mingming Cao617ba132006-10-11 01:20:53 -07002544static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002545{
2546 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002547 struct ext4_sb_info *sbi = EXT4_SB(sb);
2548 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08002549 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002550
Badari Pulavarty5e700302007-07-15 23:42:00 -07002551 if (test_opt(sb, MINIX_DF)) {
2552 sbi->s_overhead_last = 0;
2553 } else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
2554 unsigned long ngroups = sbi->s_groups_count, i;
2555 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002556 smp_rmb();
2557
2558 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07002559 * Compute the overhead (FS structures). This is constant
2560 * for a given filesystem unless the number of block groups
2561 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002562 */
2563
2564 /*
2565 * All of the blocks before first_data_block are
2566 * overhead
2567 */
2568 overhead = le32_to_cpu(es->s_first_data_block);
2569
2570 /*
2571 * Add the overhead attributed to the superblock and
2572 * block group descriptors. If the sparse superblocks
2573 * feature is turned on, then not all groups have this.
2574 */
2575 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07002576 overhead += ext4_bg_has_super(sb, i) +
2577 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002578 cond_resched();
2579 }
2580
2581 /*
2582 * Every block group has an inode bitmap, a block
2583 * bitmap, and an inode table.
2584 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07002585 overhead += ngroups * (2 + sbi->s_itb_per_group);
2586 sbi->s_overhead_last = overhead;
2587 smp_wmb();
2588 sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002589 }
2590
Mingming Cao617ba132006-10-11 01:20:53 -07002591 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002592 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07002593 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002594 buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07002595 es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002596 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2597 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002598 buf->f_bavail = 0;
2599 buf->f_files = le32_to_cpu(es->s_inodes_count);
2600 buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07002601 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07002602 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08002603 fsid = le64_to_cpup((void *)es->s_uuid) ^
2604 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2605 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2606 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002607 return 0;
2608}
2609
2610/* Helper function for writing quotas on sync - we need to start transaction before quota file
2611 * is locked for write. Otherwise the are possible deadlocks:
2612 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07002613 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07002614 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002615 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07002616 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002617 *
2618 */
2619
2620#ifdef CONFIG_QUOTA
2621
2622static inline struct inode *dquot_to_inode(struct dquot *dquot)
2623{
2624 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2625}
2626
Mingming Cao617ba132006-10-11 01:20:53 -07002627static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002628{
2629 handle_t *handle;
2630 int ret, err;
2631
2632 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07002633 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002634 if (IS_ERR(handle))
2635 return PTR_ERR(handle);
2636 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07002637 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002638 if (!ret)
2639 ret = err;
2640 return ret;
2641}
2642
Mingming Cao617ba132006-10-11 01:20:53 -07002643static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002644{
2645 handle_t *handle;
2646 int ret, err;
2647
2648 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07002649 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002650 if (IS_ERR(handle))
2651 return PTR_ERR(handle);
2652 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002653 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002654 if (!ret)
2655 ret = err;
2656 return ret;
2657}
2658
Mingming Cao617ba132006-10-11 01:20:53 -07002659static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002660{
2661 int ret, err;
2662 handle_t *handle;
2663 struct inode *inode;
2664
2665 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002666 handle = ext4_journal_start(inode,
2667 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002668 if (IS_ERR(handle))
2669 return PTR_ERR(handle);
2670 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002671 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002672 if (!ret)
2673 ret = err;
2674 return ret;
2675}
2676
Mingming Cao617ba132006-10-11 01:20:53 -07002677static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002678{
2679 int ret, err;
2680 handle_t *handle;
2681
Mingming Cao617ba132006-10-11 01:20:53 -07002682 handle = ext4_journal_start(dquot_to_inode(dquot),
2683 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002684 if (IS_ERR(handle))
2685 return PTR_ERR(handle);
2686 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002687 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002688 if (!ret)
2689 ret = err;
2690 return ret;
2691}
2692
Mingming Cao617ba132006-10-11 01:20:53 -07002693static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002694{
2695 int ret, err;
2696 handle_t *handle;
2697
Mingming Cao617ba132006-10-11 01:20:53 -07002698 handle = ext4_journal_start(dquot_to_inode(dquot),
2699 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002700 if (IS_ERR(handle))
2701 return PTR_ERR(handle);
2702 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002703 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002704 if (!ret)
2705 ret = err;
2706 return ret;
2707}
2708
Mingming Cao617ba132006-10-11 01:20:53 -07002709static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002710{
2711 /* Are we journalling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07002712 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2713 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002714 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002715 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002716 } else {
2717 return dquot_mark_dquot_dirty(dquot);
2718 }
2719}
2720
Mingming Cao617ba132006-10-11 01:20:53 -07002721static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002722{
2723 int ret, err;
2724 handle_t *handle;
2725
2726 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07002727 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002728 if (IS_ERR(handle))
2729 return PTR_ERR(handle);
2730 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07002731 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002732 if (!ret)
2733 ret = err;
2734 return ret;
2735}
2736
2737/*
2738 * Turn on quotas during mount time - we need to find
2739 * the quota file and such...
2740 */
Mingming Cao617ba132006-10-11 01:20:53 -07002741static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002742{
Mingming Cao617ba132006-10-11 01:20:53 -07002743 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
2744 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002745}
2746
2747/*
2748 * Standard function to be called on quota_on
2749 */
Mingming Cao617ba132006-10-11 01:20:53 -07002750static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002751 char *path)
2752{
2753 int err;
2754 struct nameidata nd;
2755
2756 if (!test_opt(sb, QUOTA))
2757 return -EINVAL;
2758 /* Not journalling quota? */
Mingming Cao617ba132006-10-11 01:20:53 -07002759 if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
2760 !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002761 return vfs_quota_on(sb, type, format_id, path);
2762 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2763 if (err)
2764 return err;
2765 /* Quotafile not on the same filesystem? */
2766 if (nd.mnt->mnt_sb != sb) {
2767 path_release(&nd);
2768 return -EXDEV;
2769 }
2770 /* Quotafile not of fs root? */
2771 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2772 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002773 "EXT4-fs: Quota file not on filesystem root. "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002774 "Journalled quota will not work.\n");
2775 path_release(&nd);
2776 return vfs_quota_on(sb, type, format_id, path);
2777}
2778
2779/* Read data from quotafile - avoid pagecache and such because we cannot afford
2780 * acquiring the locks... As quota files are never truncated and quota code
2781 * itself serializes the operations (and noone else should touch the files)
2782 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07002783static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002784 size_t len, loff_t off)
2785{
2786 struct inode *inode = sb_dqopt(sb)->files[type];
Mingming Cao617ba132006-10-11 01:20:53 -07002787 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002788 int err = 0;
2789 int offset = off & (sb->s_blocksize - 1);
2790 int tocopy;
2791 size_t toread;
2792 struct buffer_head *bh;
2793 loff_t i_size = i_size_read(inode);
2794
2795 if (off > i_size)
2796 return 0;
2797 if (off+len > i_size)
2798 len = i_size-off;
2799 toread = len;
2800 while (toread > 0) {
2801 tocopy = sb->s_blocksize - offset < toread ?
2802 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07002803 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002804 if (err)
2805 return err;
2806 if (!bh) /* A hole? */
2807 memset(data, 0, tocopy);
2808 else
2809 memcpy(data, bh->b_data+offset, tocopy);
2810 brelse(bh);
2811 offset = 0;
2812 toread -= tocopy;
2813 data += tocopy;
2814 blk++;
2815 }
2816 return len;
2817}
2818
2819/* Write to quotafile (we know the transaction is already started and has
2820 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07002821static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002822 const char *data, size_t len, loff_t off)
2823{
2824 struct inode *inode = sb_dqopt(sb)->files[type];
Mingming Cao617ba132006-10-11 01:20:53 -07002825 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002826 int err = 0;
2827 int offset = off & (sb->s_blocksize - 1);
2828 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07002829 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002830 size_t towrite = len;
2831 struct buffer_head *bh;
2832 handle_t *handle = journal_current_handle();
2833
2834 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2835 while (towrite > 0) {
2836 tocopy = sb->s_blocksize - offset < towrite ?
2837 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07002838 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002839 if (!bh)
2840 goto out;
2841 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07002842 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002843 if (err) {
2844 brelse(bh);
2845 goto out;
2846 }
2847 }
2848 lock_buffer(bh);
2849 memcpy(bh->b_data+offset, data, tocopy);
2850 flush_dcache_page(bh->b_page);
2851 unlock_buffer(bh);
2852 if (journal_quota)
Mingming Cao617ba132006-10-11 01:20:53 -07002853 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002854 else {
2855 /* Always do at least ordered writes for quotas */
Mingming Cao617ba132006-10-11 01:20:53 -07002856 err = ext4_journal_dirty_data(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002857 mark_buffer_dirty(bh);
2858 }
2859 brelse(bh);
2860 if (err)
2861 goto out;
2862 offset = 0;
2863 towrite -= tocopy;
2864 data += tocopy;
2865 blk++;
2866 }
2867out:
2868 if (len == towrite)
2869 return err;
2870 if (inode->i_size < off+len-towrite) {
2871 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07002872 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002873 }
2874 inode->i_version++;
2875 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07002876 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002877 mutex_unlock(&inode->i_mutex);
2878 return len - towrite;
2879}
2880
2881#endif
2882
Mingming Cao617ba132006-10-11 01:20:53 -07002883static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002884 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2885{
Mingming Cao617ba132006-10-11 01:20:53 -07002886 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002887}
2888
Mingming Cao617ba132006-10-11 01:20:53 -07002889static struct file_system_type ext4dev_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002890 .owner = THIS_MODULE,
Mingming Cao617ba132006-10-11 01:20:53 -07002891 .name = "ext4dev",
2892 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002893 .kill_sb = kill_block_super,
2894 .fs_flags = FS_REQUIRES_DEV,
2895};
2896
Mingming Cao617ba132006-10-11 01:20:53 -07002897static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002898{
Mingming Cao617ba132006-10-11 01:20:53 -07002899 int err = init_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002900 if (err)
2901 return err;
2902 err = init_inodecache();
2903 if (err)
2904 goto out1;
Andrew Morton63f57932006-10-11 01:21:24 -07002905 err = register_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002906 if (err)
2907 goto out;
2908 return 0;
2909out:
2910 destroy_inodecache();
2911out1:
Mingming Cao617ba132006-10-11 01:20:53 -07002912 exit_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002913 return err;
2914}
2915
Mingming Cao617ba132006-10-11 01:20:53 -07002916static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002917{
Mingming Cao617ba132006-10-11 01:20:53 -07002918 unregister_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002919 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07002920 exit_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002921}
2922
2923MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07002924MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002925MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07002926module_init(init_ext4_fs)
2927module_exit(exit_ext4_fs)