blob: 61c4718e4a5391239191e2043c3a6a3e56e0f0c9 [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>
32#include <linux/vfs.h>
33#include <linux/random.h>
34#include <linux/mount.h>
35#include <linux/namei.h>
36#include <linux/quotaops.h>
37#include <linux/seq_file.h>
38
39#include <asm/uaccess.h>
40
41#include "xattr.h"
42#include "acl.h"
43#include "namei.h"
44
Mingming Cao617ba132006-10-11 01:20:53 -070045static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070046 unsigned long journal_devnum);
Mingming Cao617ba132006-10-11 01:20:53 -070047static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070048 unsigned int);
Mingming Cao617ba132006-10-11 01:20:53 -070049static void ext4_commit_super (struct super_block * sb,
50 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070051 int sync);
Mingming Cao617ba132006-10-11 01:20:53 -070052static void ext4_mark_recovery_complete(struct super_block * sb,
53 struct ext4_super_block * es);
54static void ext4_clear_journal_err(struct super_block * sb,
55 struct ext4_super_block * es);
56static int ext4_sync_fs(struct super_block *sb, int wait);
57static const char *ext4_decode_error(struct super_block * sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070058 char nbuf[16]);
Mingming Cao617ba132006-10-11 01:20:53 -070059static int ext4_remount (struct super_block * sb, int * flags, char * data);
60static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
61static void ext4_unlockfs(struct super_block *sb);
62static void ext4_write_super (struct super_block * sb);
63static void ext4_write_super_lockfs(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070065
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070066ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
67 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070068{
69 return le32_to_cpu(bg->bg_block_bitmap) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070070 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
71 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070072}
73
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070074ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
75 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070076{
77 return le32_to_cpu(bg->bg_inode_bitmap) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070078 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
79 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070080}
81
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070082ext4_fsblk_t ext4_inode_table(struct super_block *sb,
83 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070084{
85 return le32_to_cpu(bg->bg_inode_table) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070086 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
87 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070088}
89
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070090void ext4_block_bitmap_set(struct super_block *sb,
91 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070092{
93 bg->bg_block_bitmap = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070094 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
95 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070096}
97
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070098void ext4_inode_bitmap_set(struct super_block *sb,
99 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700100{
101 bg->bg_inode_bitmap = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700102 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
103 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700104}
105
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700106void ext4_inode_table_set(struct super_block *sb,
107 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700108{
109 bg->bg_inode_table = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700110 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
111 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700112}
113
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700114/*
Mingming Caodab291a2006-10-11 01:21:01 -0700115 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700116 *
117 * The only special thing we need to do here is to make sure that all
118 * journal_end calls result in the superblock being marked dirty, so
119 * that sync() will call the filesystem's write_super callback if
120 * appropriate.
121 */
Mingming Cao617ba132006-10-11 01:20:53 -0700122handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700123{
124 journal_t *journal;
125
126 if (sb->s_flags & MS_RDONLY)
127 return ERR_PTR(-EROFS);
128
129 /* Special case here: if the journal has aborted behind our
130 * backs (eg. EIO in the commit thread), then we still need to
131 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700132 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700133 if (is_journal_aborted(journal)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700134 ext4_abort(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700135 "Detected aborted journal");
136 return ERR_PTR(-EROFS);
137 }
138
Mingming Caodab291a2006-10-11 01:21:01 -0700139 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700140}
141
142/*
143 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700144 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145 * that sync() will call the filesystem's write_super callback if
146 * appropriate.
147 */
Mingming Cao617ba132006-10-11 01:20:53 -0700148int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700149{
150 struct super_block *sb;
151 int err;
152 int rc;
153
154 sb = handle->h_transaction->t_journal->j_private;
155 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700156 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700157
158 if (!err)
159 err = rc;
160 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700161 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700162 return err;
163}
164
Mingming Cao617ba132006-10-11 01:20:53 -0700165void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700166 struct buffer_head *bh, handle_t *handle, int err)
167{
168 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700169 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170
171 if (bh)
172 BUFFER_TRACE(bh, "abort");
173
174 if (!handle->h_err)
175 handle->h_err = err;
176
177 if (is_handle_aborted(handle))
178 return;
179
180 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
181 caller, errstr, err_fn);
182
Mingming Caodab291a2006-10-11 01:21:01 -0700183 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184}
185
186/* Deal with the reporting of failure conditions on a filesystem such as
187 * inconsistencies detected or read IO failures.
188 *
189 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700190 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191 * write ordering constraints on the superblock which prevent us from
192 * writing it out straight away; and given that the journal is about to
193 * be aborted, we can't rely on the current, or future, transactions to
194 * write out the superblock safely.
195 *
Mingming Caodab291a2006-10-11 01:21:01 -0700196 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197 * the journal instead. On recovery, the journal will compain about
198 * that error until we've noted it down and cleared it.
199 */
200
Mingming Cao617ba132006-10-11 01:20:53 -0700201static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202{
Mingming Cao617ba132006-10-11 01:20:53 -0700203 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204
Mingming Cao617ba132006-10-11 01:20:53 -0700205 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
206 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207
208 if (sb->s_flags & MS_RDONLY)
209 return;
210
211 if (!test_opt (sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700212 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213
Mingming Cao617ba132006-10-11 01:20:53 -0700214 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700216 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700217 }
218 if (test_opt (sb, ERRORS_RO)) {
219 printk (KERN_CRIT "Remounting filesystem read-only\n");
220 sb->s_flags |= MS_RDONLY;
221 }
Mingming Cao617ba132006-10-11 01:20:53 -0700222 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700224 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 sb->s_id);
226}
227
Mingming Cao617ba132006-10-11 01:20:53 -0700228void ext4_error (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229 const char * fmt, ...)
230{
231 va_list args;
232
233 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700234 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235 vprintk(fmt, args);
236 printk("\n");
237 va_end(args);
238
Mingming Cao617ba132006-10-11 01:20:53 -0700239 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240}
241
Mingming Cao617ba132006-10-11 01:20:53 -0700242static const char *ext4_decode_error(struct super_block * sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243 char nbuf[16])
244{
245 char *errstr = NULL;
246
247 switch (errno) {
248 case -EIO:
249 errstr = "IO failure";
250 break;
251 case -ENOMEM:
252 errstr = "Out of memory";
253 break;
254 case -EROFS:
Mingming Caodab291a2006-10-11 01:21:01 -0700255 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 errstr = "Journal has aborted";
257 else
258 errstr = "Readonly filesystem";
259 break;
260 default:
261 /* If the caller passed in an extra buffer for unknown
262 * errors, textualise them now. Else we just return
263 * NULL. */
264 if (nbuf) {
265 /* Check for truncated error codes... */
266 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
267 errstr = nbuf;
268 }
269 break;
270 }
271
272 return errstr;
273}
274
Mingming Cao617ba132006-10-11 01:20:53 -0700275/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276 * automatically and invokes the appropriate error response. */
277
Mingming Cao617ba132006-10-11 01:20:53 -0700278void __ext4_std_error (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 int errno)
280{
281 char nbuf[16];
282 const char *errstr;
283
284 /* Special case: if the error is EROFS, and we're not already
285 * inside a transaction, then there's really no point in logging
286 * an error. */
287 if (errno == -EROFS && journal_current_handle() == NULL &&
288 (sb->s_flags & MS_RDONLY))
289 return;
290
Mingming Cao617ba132006-10-11 01:20:53 -0700291 errstr = ext4_decode_error(sb, errno, nbuf);
292 printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293 sb->s_id, function, errstr);
294
Mingming Cao617ba132006-10-11 01:20:53 -0700295 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700296}
297
298/*
Mingming Cao617ba132006-10-11 01:20:53 -0700299 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300 * abort function may be used to deal with unrecoverable failures such
301 * as journal IO errors or ENOMEM at a critical moment in log management.
302 *
303 * We unconditionally force the filesystem into an ABORT|READONLY state,
304 * unless the error response on the fs has been set to panic in which
305 * case we take the easy way out and panic immediately.
306 */
307
Mingming Cao617ba132006-10-11 01:20:53 -0700308void ext4_abort (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 const char * fmt, ...)
310{
311 va_list args;
312
Mingming Cao617ba132006-10-11 01:20:53 -0700313 printk (KERN_CRIT "ext4_abort called.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314
315 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700316 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317 vprintk(fmt, args);
318 printk("\n");
319 va_end(args);
320
321 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700322 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323
324 if (sb->s_flags & MS_RDONLY)
325 return;
326
327 printk(KERN_CRIT "Remounting filesystem read-only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700328 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700329 sb->s_flags |= MS_RDONLY;
Mingming Cao617ba132006-10-11 01:20:53 -0700330 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Mingming Caodab291a2006-10-11 01:21:01 -0700331 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700332}
333
Mingming Cao617ba132006-10-11 01:20:53 -0700334void ext4_warning (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700335 const char * fmt, ...)
336{
337 va_list args;
338
339 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700340 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700341 sb->s_id, function);
342 vprintk(fmt, args);
343 printk("\n");
344 va_end(args);
345}
346
Mingming Cao617ba132006-10-11 01:20:53 -0700347void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700348{
Mingming Cao617ba132006-10-11 01:20:53 -0700349 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700350
Mingming Cao617ba132006-10-11 01:20:53 -0700351 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700352 return;
353
Mingming Cao617ba132006-10-11 01:20:53 -0700354 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700355 "updating to rev %d because of new feature flag, "
356 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700357 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358
Mingming Cao617ba132006-10-11 01:20:53 -0700359 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
360 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
361 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700362 /* leave es->s_feature_*compat flags alone */
363 /* es->s_uuid will be set by e2fsck if empty */
364
365 /*
366 * The rest of the superblock fields should be zero, and if not it
367 * means they are likely already in use, so leave them alone. We
368 * can leave it up to e2fsck to clean up any inconsistencies there.
369 */
370}
371
372/*
373 * Open the external journal device
374 */
Mingming Cao617ba132006-10-11 01:20:53 -0700375static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700376{
377 struct block_device *bdev;
378 char b[BDEVNAME_SIZE];
379
380 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
381 if (IS_ERR(bdev))
382 goto fail;
383 return bdev;
384
385fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700386 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700387 __bdevname(dev, b), PTR_ERR(bdev));
388 return NULL;
389}
390
391/*
392 * Release the journal device
393 */
Mingming Cao617ba132006-10-11 01:20:53 -0700394static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700395{
396 bd_release(bdev);
397 return blkdev_put(bdev);
398}
399
Mingming Cao617ba132006-10-11 01:20:53 -0700400static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700401{
402 struct block_device *bdev;
403 int ret = -ENODEV;
404
405 bdev = sbi->journal_bdev;
406 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700407 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408 sbi->journal_bdev = NULL;
409 }
410 return ret;
411}
412
413static inline struct inode *orphan_list_entry(struct list_head *l)
414{
Mingming Cao617ba132006-10-11 01:20:53 -0700415 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700416}
417
Mingming Cao617ba132006-10-11 01:20:53 -0700418static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700419{
420 struct list_head *l;
421
422 printk(KERN_ERR "sb orphan head is %d\n",
423 le32_to_cpu(sbi->s_es->s_last_orphan));
424
425 printk(KERN_ERR "sb_info orphan list:\n");
426 list_for_each(l, &sbi->s_orphan) {
427 struct inode *inode = orphan_list_entry(l);
428 printk(KERN_ERR " "
429 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
430 inode->i_sb->s_id, inode->i_ino, inode,
431 inode->i_mode, inode->i_nlink,
432 NEXT_ORPHAN(inode));
433 }
434}
435
Mingming Cao617ba132006-10-11 01:20:53 -0700436static void ext4_put_super (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700437{
Mingming Cao617ba132006-10-11 01:20:53 -0700438 struct ext4_sb_info *sbi = EXT4_SB(sb);
439 struct ext4_super_block *es = sbi->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440 int i;
441
Alex Tomasa86c6182006-10-11 01:21:03 -0700442 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700443 ext4_xattr_put_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -0700444 jbd2_journal_destroy(sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700445 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700446 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 es->s_state = cpu_to_le16(sbi->s_mount_state);
448 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
449 mark_buffer_dirty(sbi->s_sbh);
Mingming Cao617ba132006-10-11 01:20:53 -0700450 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451 }
452
453 for (i = 0; i < sbi->s_gdb_count; i++)
454 brelse(sbi->s_group_desc[i]);
455 kfree(sbi->s_group_desc);
456 percpu_counter_destroy(&sbi->s_freeblocks_counter);
457 percpu_counter_destroy(&sbi->s_freeinodes_counter);
458 percpu_counter_destroy(&sbi->s_dirs_counter);
459 brelse(sbi->s_sbh);
460#ifdef CONFIG_QUOTA
461 for (i = 0; i < MAXQUOTAS; i++)
462 kfree(sbi->s_qf_names[i]);
463#endif
464
465 /* Debugging code just in case the in-memory inode orphan list
466 * isn't empty. The on-disk one can be non-empty if we've
467 * detected an error and taken the fs readonly, but the
468 * in-memory list had better be clean by this point. */
469 if (!list_empty(&sbi->s_orphan))
470 dump_orphan_list(sb, sbi);
471 J_ASSERT(list_empty(&sbi->s_orphan));
472
473 invalidate_bdev(sb->s_bdev, 0);
474 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
475 /*
476 * Invalidate the journal device's buffers. We don't want them
477 * floating about in memory - the physical journal device may
478 * hotswapped, and it breaks the `ro-after' testing code.
479 */
480 sync_blockdev(sbi->journal_bdev);
481 invalidate_bdev(sbi->journal_bdev, 0);
Mingming Cao617ba132006-10-11 01:20:53 -0700482 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 }
484 sb->s_fs_info = NULL;
485 kfree(sbi);
486 return;
487}
488
Christoph Lametere18b8902006-12-06 20:33:20 -0800489static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490
491/*
492 * Called inside transaction, so use GFP_NOFS
493 */
Mingming Cao617ba132006-10-11 01:20:53 -0700494static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495{
Mingming Cao617ba132006-10-11 01:20:53 -0700496 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800498 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 if (!ei)
500 return NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700501#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
502 ei->i_acl = EXT4_ACL_NOT_CACHED;
503 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504#endif
505 ei->i_block_alloc_info = NULL;
506 ei->vfs_inode.i_version = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700507 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700508 return &ei->vfs_inode;
509}
510
Mingming Cao617ba132006-10-11 01:20:53 -0700511static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512{
Mingming Cao617ba132006-10-11 01:20:53 -0700513 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514}
515
Christoph Lametere18b8902006-12-06 20:33:20 -0800516static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517{
Mingming Cao617ba132006-10-11 01:20:53 -0700518 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519
520 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
521 SLAB_CTOR_CONSTRUCTOR) {
522 INIT_LIST_HEAD(&ei->i_orphan);
Mingming Cao617ba132006-10-11 01:20:53 -0700523#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524 init_rwsem(&ei->xattr_sem);
525#endif
526 mutex_init(&ei->truncate_mutex);
527 inode_init_once(&ei->vfs_inode);
528 }
529}
530
531static int init_inodecache(void)
532{
Mingming Cao617ba132006-10-11 01:20:53 -0700533 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
534 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535 0, (SLAB_RECLAIM_ACCOUNT|
536 SLAB_MEM_SPREAD),
537 init_once, NULL);
Mingming Cao617ba132006-10-11 01:20:53 -0700538 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539 return -ENOMEM;
540 return 0;
541}
542
543static void destroy_inodecache(void)
544{
Mingming Cao617ba132006-10-11 01:20:53 -0700545 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546}
547
Mingming Cao617ba132006-10-11 01:20:53 -0700548static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549{
Mingming Cao617ba132006-10-11 01:20:53 -0700550 struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
551#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
552 if (EXT4_I(inode)->i_acl &&
553 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
554 posix_acl_release(EXT4_I(inode)->i_acl);
555 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 }
Mingming Cao617ba132006-10-11 01:20:53 -0700557 if (EXT4_I(inode)->i_default_acl &&
558 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
559 posix_acl_release(EXT4_I(inode)->i_default_acl);
560 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 }
562#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700563 ext4_discard_reservation(inode);
564 EXT4_I(inode)->i_block_alloc_info = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 if (unlikely(rsv))
566 kfree(rsv);
567}
568
Mingming Cao617ba132006-10-11 01:20:53 -0700569static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570{
571#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700572 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573
574 if (sbi->s_jquota_fmt)
575 seq_printf(seq, ",jqfmt=%s",
576 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
577
578 if (sbi->s_qf_names[USRQUOTA])
579 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
580
581 if (sbi->s_qf_names[GRPQUOTA])
582 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
583
Mingming Cao617ba132006-10-11 01:20:53 -0700584 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585 seq_puts(seq, ",usrquota");
586
Mingming Cao617ba132006-10-11 01:20:53 -0700587 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 seq_puts(seq, ",grpquota");
589#endif
590}
591
Mingming Cao617ba132006-10-11 01:20:53 -0700592static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593{
594 struct super_block *sb = vfs->mnt_sb;
595
Mingming Cao617ba132006-10-11 01:20:53 -0700596 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700598 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700600 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601 seq_puts(seq, ",data=writeback");
602
Mingming Cao617ba132006-10-11 01:20:53 -0700603 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700604
605 return 0;
606}
607
608
Mingming Cao617ba132006-10-11 01:20:53 -0700609static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610{
611 __u32 *objp = vobjp;
612 unsigned long ino = objp[0];
613 __u32 generation = objp[1];
614 struct inode *inode;
615 struct dentry *result;
616
Mingming Cao617ba132006-10-11 01:20:53 -0700617 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700618 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700619 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620 return ERR_PTR(-ESTALE);
621
622 /* iget isn't really right if the inode is currently unallocated!!
623 *
Mingming Cao617ba132006-10-11 01:20:53 -0700624 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 * deleted, so we should be safe.
626 *
627 * Currently we don't know the generation for parent directory, so
628 * a generation of 0 means "accept any"
629 */
630 inode = iget(sb, ino);
631 if (inode == NULL)
632 return ERR_PTR(-ENOMEM);
633 if (is_bad_inode(inode) ||
634 (generation && inode->i_generation != generation)) {
635 iput(inode);
636 return ERR_PTR(-ESTALE);
637 }
638 /* now to find a dentry.
639 * If possible, get a well-connected one
640 */
641 result = d_alloc_anon(inode);
642 if (!result) {
643 iput(inode);
644 return ERR_PTR(-ENOMEM);
645 }
646 return result;
647}
648
649#ifdef CONFIG_QUOTA
650#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
651#define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
652
Mingming Cao617ba132006-10-11 01:20:53 -0700653static int ext4_dquot_initialize(struct inode *inode, int type);
654static int ext4_dquot_drop(struct inode *inode);
655static int ext4_write_dquot(struct dquot *dquot);
656static int ext4_acquire_dquot(struct dquot *dquot);
657static int ext4_release_dquot(struct dquot *dquot);
658static int ext4_mark_dquot_dirty(struct dquot *dquot);
659static int ext4_write_info(struct super_block *sb, int type);
660static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
661static int ext4_quota_on_mount(struct super_block *sb, int type);
662static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700664static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665 const char *data, size_t len, loff_t off);
666
Mingming Cao617ba132006-10-11 01:20:53 -0700667static struct dquot_operations ext4_quota_operations = {
668 .initialize = ext4_dquot_initialize,
669 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670 .alloc_space = dquot_alloc_space,
671 .alloc_inode = dquot_alloc_inode,
672 .free_space = dquot_free_space,
673 .free_inode = dquot_free_inode,
674 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700675 .write_dquot = ext4_write_dquot,
676 .acquire_dquot = ext4_acquire_dquot,
677 .release_dquot = ext4_release_dquot,
678 .mark_dirty = ext4_mark_dquot_dirty,
679 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700680};
681
Mingming Cao617ba132006-10-11 01:20:53 -0700682static struct quotactl_ops ext4_qctl_operations = {
683 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700684 .quota_off = vfs_quota_off,
685 .quota_sync = vfs_quota_sync,
686 .get_info = vfs_get_dqinfo,
687 .set_info = vfs_set_dqinfo,
688 .get_dqblk = vfs_get_dqblk,
689 .set_dqblk = vfs_set_dqblk
690};
691#endif
692
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800693static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700694 .alloc_inode = ext4_alloc_inode,
695 .destroy_inode = ext4_destroy_inode,
696 .read_inode = ext4_read_inode,
697 .write_inode = ext4_write_inode,
698 .dirty_inode = ext4_dirty_inode,
699 .delete_inode = ext4_delete_inode,
700 .put_super = ext4_put_super,
701 .write_super = ext4_write_super,
702 .sync_fs = ext4_sync_fs,
703 .write_super_lockfs = ext4_write_super_lockfs,
704 .unlockfs = ext4_unlockfs,
705 .statfs = ext4_statfs,
706 .remount_fs = ext4_remount,
707 .clear_inode = ext4_clear_inode,
708 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700710 .quota_read = ext4_quota_read,
711 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700712#endif
713};
714
Mingming Cao617ba132006-10-11 01:20:53 -0700715static struct export_operations ext4_export_ops = {
716 .get_parent = ext4_get_parent,
717 .get_dentry = ext4_get_dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718};
719
720enum {
721 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
722 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
723 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
724 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
725 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
726 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
727 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
728 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
729 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
730 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Alex Tomasa86c6182006-10-11 01:21:03 -0700731 Opt_grpquota, Opt_extents,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732};
733
734static match_table_t tokens = {
735 {Opt_bsd_df, "bsddf"},
736 {Opt_minix_df, "minixdf"},
737 {Opt_grpid, "grpid"},
738 {Opt_grpid, "bsdgroups"},
739 {Opt_nogrpid, "nogrpid"},
740 {Opt_nogrpid, "sysvgroups"},
741 {Opt_resgid, "resgid=%u"},
742 {Opt_resuid, "resuid=%u"},
743 {Opt_sb, "sb=%u"},
744 {Opt_err_cont, "errors=continue"},
745 {Opt_err_panic, "errors=panic"},
746 {Opt_err_ro, "errors=remount-ro"},
747 {Opt_nouid32, "nouid32"},
748 {Opt_nocheck, "nocheck"},
749 {Opt_nocheck, "check=none"},
750 {Opt_debug, "debug"},
751 {Opt_oldalloc, "oldalloc"},
752 {Opt_orlov, "orlov"},
753 {Opt_user_xattr, "user_xattr"},
754 {Opt_nouser_xattr, "nouser_xattr"},
755 {Opt_acl, "acl"},
756 {Opt_noacl, "noacl"},
757 {Opt_reservation, "reservation"},
758 {Opt_noreservation, "noreservation"},
759 {Opt_noload, "noload"},
760 {Opt_nobh, "nobh"},
761 {Opt_bh, "bh"},
762 {Opt_commit, "commit=%u"},
763 {Opt_journal_update, "journal=update"},
764 {Opt_journal_inum, "journal=%u"},
765 {Opt_journal_dev, "journal_dev=%u"},
766 {Opt_abort, "abort"},
767 {Opt_data_journal, "data=journal"},
768 {Opt_data_ordered, "data=ordered"},
769 {Opt_data_writeback, "data=writeback"},
770 {Opt_offusrjquota, "usrjquota="},
771 {Opt_usrjquota, "usrjquota=%s"},
772 {Opt_offgrpjquota, "grpjquota="},
773 {Opt_grpjquota, "grpjquota=%s"},
774 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
775 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
776 {Opt_grpquota, "grpquota"},
777 {Opt_noquota, "noquota"},
778 {Opt_quota, "quota"},
779 {Opt_usrquota, "usrquota"},
780 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -0700781 {Opt_extents, "extents"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782 {Opt_err, NULL},
783 {Opt_resize, "resize"},
784};
785
Mingming Cao617ba132006-10-11 01:20:53 -0700786static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787{
Mingming Cao617ba132006-10-11 01:20:53 -0700788 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789 char *options = (char *) *data;
790
791 if (!options || strncmp(options, "sb=", 3) != 0)
792 return 1; /* Default location */
793 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -0700794 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 sb_block = simple_strtoul(options, &options, 0);
796 if (*options && *options != ',') {
Mingming Cao617ba132006-10-11 01:20:53 -0700797 printk("EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798 (char *) *data);
799 return 1;
800 }
801 if (*options == ',')
802 options++;
803 *data = (void *) options;
804 return sb_block;
805}
806
807static int parse_options (char *options, struct super_block *sb,
808 unsigned int *inum, unsigned long *journal_devnum,
Mingming Cao617ba132006-10-11 01:20:53 -0700809 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810{
Mingming Cao617ba132006-10-11 01:20:53 -0700811 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700812 char * p;
813 substring_t args[MAX_OPT_ARGS];
814 int data_opt = 0;
815 int option;
816#ifdef CONFIG_QUOTA
817 int qtype;
818 char *qname;
819#endif
820
821 if (!options)
822 return 1;
823
824 while ((p = strsep (&options, ",")) != NULL) {
825 int token;
826 if (!*p)
827 continue;
828
829 token = match_token(p, tokens, args);
830 switch (token) {
831 case Opt_bsd_df:
832 clear_opt (sbi->s_mount_opt, MINIX_DF);
833 break;
834 case Opt_minix_df:
835 set_opt (sbi->s_mount_opt, MINIX_DF);
836 break;
837 case Opt_grpid:
838 set_opt (sbi->s_mount_opt, GRPID);
839 break;
840 case Opt_nogrpid:
841 clear_opt (sbi->s_mount_opt, GRPID);
842 break;
843 case Opt_resuid:
844 if (match_int(&args[0], &option))
845 return 0;
846 sbi->s_resuid = option;
847 break;
848 case Opt_resgid:
849 if (match_int(&args[0], &option))
850 return 0;
851 sbi->s_resgid = option;
852 break;
853 case Opt_sb:
854 /* handled by get_sb_block() instead of here */
855 /* *sb_block = match_int(&args[0]); */
856 break;
857 case Opt_err_panic:
858 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
859 clear_opt (sbi->s_mount_opt, ERRORS_RO);
860 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
861 break;
862 case Opt_err_ro:
863 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
864 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
865 set_opt (sbi->s_mount_opt, ERRORS_RO);
866 break;
867 case Opt_err_cont:
868 clear_opt (sbi->s_mount_opt, ERRORS_RO);
869 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
870 set_opt (sbi->s_mount_opt, ERRORS_CONT);
871 break;
872 case Opt_nouid32:
873 set_opt (sbi->s_mount_opt, NO_UID32);
874 break;
875 case Opt_nocheck:
876 clear_opt (sbi->s_mount_opt, CHECK);
877 break;
878 case Opt_debug:
879 set_opt (sbi->s_mount_opt, DEBUG);
880 break;
881 case Opt_oldalloc:
882 set_opt (sbi->s_mount_opt, OLDALLOC);
883 break;
884 case Opt_orlov:
885 clear_opt (sbi->s_mount_opt, OLDALLOC);
886 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700887#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888 case Opt_user_xattr:
889 set_opt (sbi->s_mount_opt, XATTR_USER);
890 break;
891 case Opt_nouser_xattr:
892 clear_opt (sbi->s_mount_opt, XATTR_USER);
893 break;
894#else
895 case Opt_user_xattr:
896 case Opt_nouser_xattr:
Mingming Cao617ba132006-10-11 01:20:53 -0700897 printk("EXT4 (no)user_xattr options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700898 break;
899#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700900#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 case Opt_acl:
902 set_opt(sbi->s_mount_opt, POSIX_ACL);
903 break;
904 case Opt_noacl:
905 clear_opt(sbi->s_mount_opt, POSIX_ACL);
906 break;
907#else
908 case Opt_acl:
909 case Opt_noacl:
Mingming Cao617ba132006-10-11 01:20:53 -0700910 printk("EXT4 (no)acl options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700911 break;
912#endif
913 case Opt_reservation:
914 set_opt(sbi->s_mount_opt, RESERVATION);
915 break;
916 case Opt_noreservation:
917 clear_opt(sbi->s_mount_opt, RESERVATION);
918 break;
919 case Opt_journal_update:
920 /* @@@ FIXME */
921 /* Eventually we will want to be able to create
922 a journal file here. For now, only allow the
923 user to specify an existing inode to be the
924 journal file. */
925 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700926 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927 "journal on remount\n");
928 return 0;
929 }
930 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
931 break;
932 case Opt_journal_inum:
933 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700934 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935 "journal on remount\n");
936 return 0;
937 }
938 if (match_int(&args[0], &option))
939 return 0;
940 *inum = option;
941 break;
942 case Opt_journal_dev:
943 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700944 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700945 "journal on remount\n");
946 return 0;
947 }
948 if (match_int(&args[0], &option))
949 return 0;
950 *journal_devnum = option;
951 break;
952 case Opt_noload:
953 set_opt (sbi->s_mount_opt, NOLOAD);
954 break;
955 case Opt_commit:
956 if (match_int(&args[0], &option))
957 return 0;
958 if (option < 0)
959 return 0;
960 if (option == 0)
961 option = JBD_DEFAULT_MAX_COMMIT_AGE;
962 sbi->s_commit_interval = HZ * option;
963 break;
964 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -0700965 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 goto datacheck;
967 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -0700968 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700969 goto datacheck;
970 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -0700971 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972 datacheck:
973 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700974 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700975 != data_opt) {
976 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700977 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 "mode on remount\n");
979 return 0;
980 }
981 } else {
Mingming Cao617ba132006-10-11 01:20:53 -0700982 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983 sbi->s_mount_opt |= data_opt;
984 }
985 break;
986#ifdef CONFIG_QUOTA
987 case Opt_usrjquota:
988 qtype = USRQUOTA;
989 goto set_qf_name;
990 case Opt_grpjquota:
991 qtype = GRPQUOTA;
992set_qf_name:
993 if (sb_any_quota_enabled(sb)) {
994 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700995 "EXT4-fs: Cannot change journalled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 "quota options when quota turned on.\n");
997 return 0;
998 }
999 qname = match_strdup(&args[0]);
1000 if (!qname) {
1001 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001002 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 "storing quotafile name.\n");
1004 return 0;
1005 }
1006 if (sbi->s_qf_names[qtype] &&
1007 strcmp(sbi->s_qf_names[qtype], qname)) {
1008 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001009 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 "specified.\n", QTYPE2NAME(qtype));
1011 kfree(qname);
1012 return 0;
1013 }
1014 sbi->s_qf_names[qtype] = qname;
1015 if (strchr(sbi->s_qf_names[qtype], '/')) {
1016 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001017 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018 "filesystem root.\n");
1019 kfree(sbi->s_qf_names[qtype]);
1020 sbi->s_qf_names[qtype] = NULL;
1021 return 0;
1022 }
1023 set_opt(sbi->s_mount_opt, QUOTA);
1024 break;
1025 case Opt_offusrjquota:
1026 qtype = USRQUOTA;
1027 goto clear_qf_name;
1028 case Opt_offgrpjquota:
1029 qtype = GRPQUOTA;
1030clear_qf_name:
1031 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001032 printk(KERN_ERR "EXT4-fs: Cannot change "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001033 "journalled quota options when "
1034 "quota turned on.\n");
1035 return 0;
1036 }
1037 /*
1038 * The space will be released later when all options
1039 * are confirmed to be correct
1040 */
1041 sbi->s_qf_names[qtype] = NULL;
1042 break;
1043 case Opt_jqfmt_vfsold:
1044 sbi->s_jquota_fmt = QFMT_VFS_OLD;
1045 break;
1046 case Opt_jqfmt_vfsv0:
1047 sbi->s_jquota_fmt = QFMT_VFS_V0;
1048 break;
1049 case Opt_quota:
1050 case Opt_usrquota:
1051 set_opt(sbi->s_mount_opt, QUOTA);
1052 set_opt(sbi->s_mount_opt, USRQUOTA);
1053 break;
1054 case Opt_grpquota:
1055 set_opt(sbi->s_mount_opt, QUOTA);
1056 set_opt(sbi->s_mount_opt, GRPQUOTA);
1057 break;
1058 case Opt_noquota:
1059 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001060 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 "options when quota turned on.\n");
1062 return 0;
1063 }
1064 clear_opt(sbi->s_mount_opt, QUOTA);
1065 clear_opt(sbi->s_mount_opt, USRQUOTA);
1066 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1067 break;
1068#else
1069 case Opt_quota:
1070 case Opt_usrquota:
1071 case Opt_grpquota:
1072 case Opt_usrjquota:
1073 case Opt_grpjquota:
1074 case Opt_offusrjquota:
1075 case Opt_offgrpjquota:
1076 case Opt_jqfmt_vfsold:
1077 case Opt_jqfmt_vfsv0:
1078 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001079 "EXT4-fs: journalled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001080 "supported.\n");
1081 break;
1082 case Opt_noquota:
1083 break;
1084#endif
1085 case Opt_abort:
1086 set_opt(sbi->s_mount_opt, ABORT);
1087 break;
1088 case Opt_barrier:
1089 if (match_int(&args[0], &option))
1090 return 0;
1091 if (option)
1092 set_opt(sbi->s_mount_opt, BARRIER);
1093 else
1094 clear_opt(sbi->s_mount_opt, BARRIER);
1095 break;
1096 case Opt_ignore:
1097 break;
1098 case Opt_resize:
1099 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001100 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001101 "for remount\n");
1102 return 0;
1103 }
1104 if (match_int(&args[0], &option) != 0)
1105 return 0;
1106 *n_blocks_count = option;
1107 break;
1108 case Opt_nobh:
1109 set_opt(sbi->s_mount_opt, NOBH);
1110 break;
1111 case Opt_bh:
1112 clear_opt(sbi->s_mount_opt, NOBH);
1113 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001114 case Opt_extents:
1115 set_opt (sbi->s_mount_opt, EXTENTS);
1116 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 default:
1118 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001119 "EXT4-fs: Unrecognized mount option \"%s\" "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001120 "or missing value\n", p);
1121 return 0;
1122 }
1123 }
1124#ifdef CONFIG_QUOTA
1125 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001126 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127 sbi->s_qf_names[USRQUOTA])
1128 clear_opt(sbi->s_mount_opt, USRQUOTA);
1129
Mingming Cao617ba132006-10-11 01:20:53 -07001130 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001131 sbi->s_qf_names[GRPQUOTA])
1132 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1133
1134 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001135 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001137 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1138 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001139 "format mixing.\n");
1140 return 0;
1141 }
1142
1143 if (!sbi->s_jquota_fmt) {
Mingming Cao617ba132006-10-11 01:20:53 -07001144 printk(KERN_ERR "EXT4-fs: journalled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145 "not specified.\n");
1146 return 0;
1147 }
1148 } else {
1149 if (sbi->s_jquota_fmt) {
Mingming Cao617ba132006-10-11 01:20:53 -07001150 printk(KERN_ERR "EXT4-fs: journalled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001151 "specified with no journalling "
1152 "enabled.\n");
1153 return 0;
1154 }
1155 }
1156#endif
1157 return 1;
1158}
1159
Mingming Cao617ba132006-10-11 01:20:53 -07001160static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161 int read_only)
1162{
Mingming Cao617ba132006-10-11 01:20:53 -07001163 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 int res = 0;
1165
Mingming Cao617ba132006-10-11 01:20:53 -07001166 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1167 printk (KERN_ERR "EXT4-fs warning: revision level too high, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001168 "forcing read-only mode\n");
1169 res = MS_RDONLY;
1170 }
1171 if (read_only)
1172 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001173 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1174 printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001176 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001178 "EXT4-fs warning: mounting fs with errors, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179 "running e2fsck is recommended\n");
1180 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1181 le16_to_cpu(es->s_mnt_count) >=
1182 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1183 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001184 "EXT4-fs warning: maximal mount count reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 "running e2fsck is recommended\n");
1186 else if (le32_to_cpu(es->s_checkinterval) &&
1187 (le32_to_cpu(es->s_lastcheck) +
1188 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1189 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001190 "EXT4-fs warning: checktime reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001191 "running e2fsck is recommended\n");
1192#if 0
1193 /* @@@ We _will_ want to clear the valid bit if we find
Andrew Morton63f57932006-10-11 01:21:24 -07001194 * inconsistencies, to force a fsck at reboot. But for
1195 * a plain journaled filesystem we can keep it set as
1196 * valid forever! :)
1197 */
Mingming Cao617ba132006-10-11 01:20:53 -07001198 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199#endif
1200 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001201 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001202 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1203 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001204 ext4_update_dynamic_rev(sb);
1205 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206
Mingming Cao617ba132006-10-11 01:20:53 -07001207 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -07001209 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001210 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1211 sb->s_blocksize,
1212 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001213 EXT4_BLOCKS_PER_GROUP(sb),
1214 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001215 sbi->s_mount_opt);
1216
Mingming Cao617ba132006-10-11 01:20:53 -07001217 printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1218 if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219 char b[BDEVNAME_SIZE];
1220
1221 printk("external journal on %s\n",
Mingming Cao617ba132006-10-11 01:20:53 -07001222 bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001223 } else {
1224 printk("internal journal\n");
1225 }
1226 return res;
1227}
1228
1229/* Called at mount-time, super-block is locked */
Mingming Cao617ba132006-10-11 01:20:53 -07001230static int ext4_check_descriptors (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001231{
Mingming Cao617ba132006-10-11 01:20:53 -07001232 struct ext4_sb_info *sbi = EXT4_SB(sb);
1233 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1234 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001235 ext4_fsblk_t block_bitmap;
1236 ext4_fsblk_t inode_bitmap;
1237 ext4_fsblk_t inode_table;
Mingming Cao617ba132006-10-11 01:20:53 -07001238 struct ext4_group_desc * gdp = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239 int desc_block = 0;
1240 int i;
1241
Mingming Cao617ba132006-10-11 01:20:53 -07001242 ext4_debug ("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001243
1244 for (i = 0; i < sbi->s_groups_count; i++)
1245 {
1246 if (i == sbi->s_groups_count - 1)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001247 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001248 else
1249 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001250 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001251
Mingming Cao617ba132006-10-11 01:20:53 -07001252 if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1253 gdp = (struct ext4_group_desc *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254 sbi->s_group_desc[desc_block++]->b_data;
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001255 block_bitmap = ext4_block_bitmap(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001256 if (block_bitmap < first_block || block_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001257 {
Mingming Cao617ba132006-10-11 01:20:53 -07001258 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001259 "Block bitmap for group %d"
Mingming Cao2ae02102006-10-11 01:21:11 -07001260 " not in group (block %llu)!",
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001261 i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001262 return 0;
1263 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001264 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001265 if (inode_bitmap < first_block || inode_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001266 {
Mingming Cao617ba132006-10-11 01:20:53 -07001267 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001268 "Inode bitmap for group %d"
Mingming Cao2ae02102006-10-11 01:21:11 -07001269 " not in group (block %llu)!",
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001270 i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271 return 0;
1272 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001273 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001274 if (inode_table < first_block ||
1275 inode_table + sbi->s_itb_per_group > 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 table 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_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281 return 0;
1282 }
Mingming Cao617ba132006-10-11 01:20:53 -07001283 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001284 gdp = (struct ext4_group_desc *)
1285 ((__u8 *)gdp + EXT4_DESC_SIZE(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 }
1287
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001288 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07001289 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001290 return 1;
1291}
1292
1293
Mingming Cao617ba132006-10-11 01:20:53 -07001294/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001295 * the superblock) which were deleted from all directories, but held open by
1296 * a process at the time of a crash. We walk the list and try to delete these
1297 * inodes at recovery time (only with a read-write filesystem).
1298 *
1299 * In order to keep the orphan inode chain consistent during traversal (in
1300 * case of crash during recovery), we link each inode into the superblock
1301 * orphan list_head and handle it the same way as an inode deletion during
1302 * normal operation (which journals the operations for us).
1303 *
1304 * We only do an iget() and an iput() on each inode, which is very safe if we
1305 * accidentally point at an in-use or already deleted inode. The worst that
1306 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001307 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001308 * e2fsck was run on this filesystem, and it must have already done the orphan
1309 * inode cleanup for us, so we can safely abort without any further action.
1310 */
Mingming Cao617ba132006-10-11 01:20:53 -07001311static void ext4_orphan_cleanup (struct super_block * sb,
1312 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001313{
1314 unsigned int s_flags = sb->s_flags;
1315 int nr_orphans = 0, nr_truncates = 0;
1316#ifdef CONFIG_QUOTA
1317 int i;
1318#endif
1319 if (!es->s_last_orphan) {
1320 jbd_debug(4, "no orphan inodes to clean up\n");
1321 return;
1322 }
1323
Eric Sandeena8f48a92006-12-06 20:40:13 -08001324 if (bdev_read_only(sb->s_bdev)) {
1325 printk(KERN_ERR "EXT4-fs: write access "
1326 "unavailable, skipping orphan cleanup.\n");
1327 return;
1328 }
1329
Mingming Cao617ba132006-10-11 01:20:53 -07001330 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001331 if (es->s_last_orphan)
1332 jbd_debug(1, "Errors on filesystem, "
1333 "clearing orphan list.\n");
1334 es->s_last_orphan = 0;
1335 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1336 return;
1337 }
1338
1339 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001340 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001341 sb->s_id);
1342 sb->s_flags &= ~MS_RDONLY;
1343 }
1344#ifdef CONFIG_QUOTA
1345 /* Needed for iput() to work correctly and not trash data */
1346 sb->s_flags |= MS_ACTIVE;
1347 /* Turn on quotas so that they are updated correctly */
1348 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001349 if (EXT4_SB(sb)->s_qf_names[i]) {
1350 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 if (ret < 0)
1352 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001353 "EXT4-fs: Cannot turn on journalled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001354 "quota: error %d\n", ret);
1355 }
1356 }
1357#endif
1358
1359 while (es->s_last_orphan) {
1360 struct inode *inode;
1361
1362 if (!(inode =
Mingming Cao617ba132006-10-11 01:20:53 -07001363 ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001364 es->s_last_orphan = 0;
1365 break;
1366 }
1367
Mingming Cao617ba132006-10-11 01:20:53 -07001368 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001369 DQUOT_INIT(inode);
1370 if (inode->i_nlink) {
1371 printk(KERN_DEBUG
1372 "%s: truncating inode %lu to %Ld bytes\n",
1373 __FUNCTION__, inode->i_ino, inode->i_size);
1374 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1375 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001376 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001377 nr_truncates++;
1378 } else {
1379 printk(KERN_DEBUG
1380 "%s: deleting unreferenced inode %lu\n",
1381 __FUNCTION__, inode->i_ino);
1382 jbd_debug(2, "deleting unreferenced inode %lu\n",
1383 inode->i_ino);
1384 nr_orphans++;
1385 }
1386 iput(inode); /* The delete magic happens here! */
1387 }
1388
1389#define PLURAL(x) (x), ((x)==1) ? "" : "s"
1390
1391 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001392 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001393 sb->s_id, PLURAL(nr_orphans));
1394 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001395 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001396 sb->s_id, PLURAL(nr_truncates));
1397#ifdef CONFIG_QUOTA
1398 /* Turn quotas off */
1399 for (i = 0; i < MAXQUOTAS; i++) {
1400 if (sb_dqopt(sb)->files[i])
1401 vfs_quota_off(sb, i);
1402 }
1403#endif
1404 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1405}
1406
1407#define log2(n) ffz(~(n))
1408
1409/*
1410 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1411 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1412 * We need to be 1 filesystem block less than the 2^32 sector limit.
1413 */
Mingming Cao617ba132006-10-11 01:20:53 -07001414static loff_t ext4_max_size(int bits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415{
Mingming Cao617ba132006-10-11 01:20:53 -07001416 loff_t res = EXT4_NDIR_BLOCKS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001417 /* This constant is calculated to be the largest file size for a
1418 * dense, 4k-blocksize file such that the total number of
1419 * sectors in the file, including data and all indirect blocks,
1420 * does not exceed 2^32. */
1421 const loff_t upper_limit = 0x1ff7fffd000LL;
1422
1423 res += 1LL << (bits-2);
1424 res += 1LL << (2*(bits-2));
1425 res += 1LL << (3*(bits-2));
1426 res <<= bits;
1427 if (res > upper_limit)
1428 res = upper_limit;
1429 return res;
1430}
1431
Mingming Cao617ba132006-10-11 01:20:53 -07001432static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001433 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001434{
Mingming Cao617ba132006-10-11 01:20:53 -07001435 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436 unsigned long bg, first_meta_bg;
1437 int has_super = 0;
1438
1439 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1440
Mingming Cao617ba132006-10-11 01:20:53 -07001441 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001442 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001443 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001444 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001445 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001446 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001447 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001448}
1449
1450
Mingming Cao617ba132006-10-11 01:20:53 -07001451static int ext4_fill_super (struct super_block *sb, void *data, int silent)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452{
1453 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001454 struct ext4_super_block *es = NULL;
1455 struct ext4_sb_info *sbi;
1456 ext4_fsblk_t block;
1457 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001458 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 unsigned long offset = 0;
1460 unsigned int journal_inum = 0;
1461 unsigned long journal_devnum = 0;
1462 unsigned long def_mount_opts;
1463 struct inode *root;
1464 int blocksize;
1465 int hblock;
1466 int db_count;
1467 int i;
1468 int needs_recovery;
1469 __le32 features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001470 __u64 blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001471
1472 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1473 if (!sbi)
1474 return -ENOMEM;
1475 sb->s_fs_info = sbi;
1476 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001477 sbi->s_resuid = EXT4_DEF_RESUID;
1478 sbi->s_resgid = EXT4_DEF_RESGID;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001479
1480 unlock_kernel();
1481
Mingming Cao617ba132006-10-11 01:20:53 -07001482 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001483 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07001484 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485 goto out_fail;
1486 }
1487
1488 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001489 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490 * block sizes. We need to calculate the offset from buffer start.
1491 */
Mingming Cao617ba132006-10-11 01:20:53 -07001492 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001493 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1494 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001495 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001496 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001497 }
1498
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001499 if (!(bh = sb_bread(sb, logical_sb_block))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001500 printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 goto out_fail;
1502 }
1503 /*
1504 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07001505 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001506 */
Mingming Cao617ba132006-10-11 01:20:53 -07001507 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001508 sbi->s_es = es;
1509 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07001510 if (sb->s_magic != EXT4_SUPER_MAGIC)
1511 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001512
1513 /* Set defaults before we parse the mount options */
1514 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07001515 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07001517 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001518 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07001519 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001520 set_opt(sbi->s_mount_opt, NO_UID32);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001521#ifdef CONFIG_EXT4DEV_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07001522 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001523 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001524#endif
1525#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07001526 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001527 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001528#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001529 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1530 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1531 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1532 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1533 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1534 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001535
Mingming Cao617ba132006-10-11 01:20:53 -07001536 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Mingming Cao617ba132006-10-11 01:20:53 -07001538 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001539 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dmitry Mishinceea16b2006-10-11 01:21:21 -07001540 else
1541 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001542
1543 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1544 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1545
1546 set_opt(sbi->s_mount_opt, RESERVATION);
1547
1548 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1549 NULL, 0))
1550 goto failed_mount;
1551
1552 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07001553 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001554
Mingming Cao617ba132006-10-11 01:20:53 -07001555 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1556 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1557 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1558 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001559 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001560 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001561 "running e2fsck is recommended\n");
1562 /*
1563 * Check feature flags regardless of the revision level, since we
1564 * previously didn't change the revision level when setting the flags,
1565 * so there is a chance incompat flags are set on a rev 0 filesystem.
1566 */
Mingming Cao617ba132006-10-11 01:20:53 -07001567 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001568 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001569 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001570 "unsupported optional features (%x).\n",
1571 sb->s_id, le32_to_cpu(features));
1572 goto failed_mount;
1573 }
Mingming Cao617ba132006-10-11 01:20:53 -07001574 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001575 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001576 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001577 "unsupported optional features (%x).\n",
1578 sb->s_id, le32_to_cpu(features));
1579 goto failed_mount;
1580 }
1581 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1582
Mingming Cao617ba132006-10-11 01:20:53 -07001583 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1584 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001585 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001586 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001587 blocksize, sb->s_id);
1588 goto failed_mount;
1589 }
1590
1591 hblock = bdev_hardsect_size(sb->s_bdev);
1592 if (sb->s_blocksize != blocksize) {
1593 /*
1594 * Make sure the blocksize for the filesystem is larger
1595 * than the hardware sectorsize for the machine.
1596 */
1597 if (blocksize < hblock) {
Mingming Cao617ba132006-10-11 01:20:53 -07001598 printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599 "device blocksize %d.\n", blocksize, hblock);
1600 goto failed_mount;
1601 }
1602
1603 brelse (bh);
1604 sb_set_blocksize(sb, blocksize);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001605 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1606 offset = do_div(logical_sb_block, blocksize);
1607 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001608 if (!bh) {
1609 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001610 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 goto failed_mount;
1612 }
Mingming Cao617ba132006-10-11 01:20:53 -07001613 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001614 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07001615 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001616 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001617 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001618 goto failed_mount;
1619 }
1620 }
1621
Mingming Cao617ba132006-10-11 01:20:53 -07001622 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001623
Mingming Cao617ba132006-10-11 01:20:53 -07001624 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
1625 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
1626 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001627 } else {
1628 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1629 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07001630 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001631 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1632 (sbi->s_inode_size > blocksize)) {
1633 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001634 "EXT4-fs: unsupported inode size: %d\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001635 sbi->s_inode_size);
1636 goto failed_mount;
1637 }
1638 }
Mingming Cao617ba132006-10-11 01:20:53 -07001639 sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001640 le32_to_cpu(es->s_log_frag_size);
1641 if (blocksize != sbi->s_frag_size) {
1642 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001643 "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001644 sbi->s_frag_size, blocksize);
1645 goto failed_mount;
1646 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001647 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
1648 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001649 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001650 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
1651 sbi->s_desc_size & (sbi->s_desc_size - 1)) {
1652 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001653 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001654 sbi->s_desc_size);
1655 goto failed_mount;
1656 }
1657 } else
1658 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001659 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1660 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1661 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Mingming Cao617ba132006-10-11 01:20:53 -07001662 if (EXT4_INODE_SIZE(sb) == 0)
1663 goto cantfind_ext4;
1664 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001665 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07001666 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001667 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1668 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001669 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001670 sbi->s_sbh = bh;
1671 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07001672 sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
1673 sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001674 for (i=0; i < 4; i++)
1675 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1676 sbi->s_def_hash_version = es->s_def_hash_version;
1677
1678 if (sbi->s_blocks_per_group > blocksize * 8) {
1679 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001680 "EXT4-fs: #blocks per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001681 sbi->s_blocks_per_group);
1682 goto failed_mount;
1683 }
1684 if (sbi->s_frags_per_group > blocksize * 8) {
1685 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001686 "EXT4-fs: #fragments per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001687 sbi->s_frags_per_group);
1688 goto failed_mount;
1689 }
1690 if (sbi->s_inodes_per_group > blocksize * 8) {
1691 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001692 "EXT4-fs: #inodes per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001693 sbi->s_inodes_per_group);
1694 goto failed_mount;
1695 }
1696
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001697 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001699 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700 " too large to mount safely\n", sb->s_id);
1701 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07001702 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001703 "enabled\n");
1704 goto failed_mount;
1705 }
1706
Mingming Cao617ba132006-10-11 01:20:53 -07001707 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
1708 goto cantfind_ext4;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001709 blocks_count = (ext4_blocks_count(es) -
1710 le32_to_cpu(es->s_first_data_block) +
1711 EXT4_BLOCKS_PER_GROUP(sb) - 1);
1712 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
1713 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07001714 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
1715 EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001716 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1717 GFP_KERNEL);
1718 if (sbi->s_group_desc == NULL) {
Mingming Cao617ba132006-10-11 01:20:53 -07001719 printk (KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720 goto failed_mount;
1721 }
1722
1723 bgl_lock_init(&sbi->s_blockgroup_lock);
1724
1725 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001726 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727 sbi->s_group_desc[i] = sb_bread(sb, block);
1728 if (!sbi->s_group_desc[i]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001729 printk (KERN_ERR "EXT4-fs: "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730 "can't read group descriptor %d\n", i);
1731 db_count = i;
1732 goto failed_mount2;
1733 }
1734 }
Mingming Cao617ba132006-10-11 01:20:53 -07001735 if (!ext4_check_descriptors (sb)) {
1736 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737 goto failed_mount2;
1738 }
1739 sbi->s_gdb_count = db_count;
1740 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1741 spin_lock_init(&sbi->s_next_gen_lock);
1742
1743 percpu_counter_init(&sbi->s_freeblocks_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001744 ext4_count_free_blocks(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745 percpu_counter_init(&sbi->s_freeinodes_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001746 ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001747 percpu_counter_init(&sbi->s_dirs_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001748 ext4_count_dirs(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001749
1750 /* per fileystem reservation list head & lock */
1751 spin_lock_init(&sbi->s_rsv_window_lock);
1752 sbi->s_rsv_window_root = RB_ROOT;
1753 /* Add a single, static dummy reservation to the start of the
1754 * reservation window list --- it gives us a placeholder for
1755 * append-at-start-of-list which makes the allocation logic
1756 * _much_ simpler. */
Mingming Cao617ba132006-10-11 01:20:53 -07001757 sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1758 sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001759 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1760 sbi->s_rsv_window_head.rsv_goal_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001761 ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001762
1763 /*
1764 * set up enough so that it can read an inode
1765 */
Mingming Cao617ba132006-10-11 01:20:53 -07001766 sb->s_op = &ext4_sops;
1767 sb->s_export_op = &ext4_export_ops;
1768 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001770 sb->s_qcop = &ext4_qctl_operations;
1771 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001772#endif
1773 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1774
1775 sb->s_root = NULL;
1776
1777 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07001778 EXT4_HAS_INCOMPAT_FEATURE(sb,
1779 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001780
1781 /*
1782 * The first inode we look at is the journal inode. Don't try
1783 * root first: it may be modified in the journal!
1784 */
1785 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07001786 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
1787 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001788 goto failed_mount3;
1789 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07001790 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001791 goto failed_mount3;
1792 } else {
1793 if (!silent)
1794 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001795 "ext4: No journal on filesystem on %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001796 sb->s_id);
1797 goto failed_mount3;
1798 }
1799
1800 /* We have now updated the journal if required, so we can
1801 * validate the data journaling mode. */
1802 switch (test_opt(sb, DATA_FLAGS)) {
1803 case 0:
1804 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07001805 * capabilities: ORDERED_DATA if the journal can
1806 * cope, else JOURNAL_DATA
1807 */
Mingming Caodab291a2006-10-11 01:21:01 -07001808 if (jbd2_journal_check_available_features
1809 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001810 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1811 else
1812 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1813 break;
1814
Mingming Cao617ba132006-10-11 01:20:53 -07001815 case EXT4_MOUNT_ORDERED_DATA:
1816 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07001817 if (!jbd2_journal_check_available_features
1818 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001819 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001820 "requested data journaling mode\n");
1821 goto failed_mount4;
1822 }
1823 default:
1824 break;
1825 }
1826
1827 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001828 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
1829 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001830 "its supported only with writeback mode\n");
1831 clear_opt(sbi->s_mount_opt, NOBH);
1832 }
1833 }
1834 /*
Mingming Caodab291a2006-10-11 01:21:01 -07001835 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836 * so we can safely mount the rest of the filesystem now.
1837 */
1838
Mingming Cao617ba132006-10-11 01:20:53 -07001839 root = iget(sb, EXT4_ROOT_INO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840 sb->s_root = d_alloc_root(root);
1841 if (!sb->s_root) {
Mingming Cao617ba132006-10-11 01:20:53 -07001842 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001843 iput(root);
1844 goto failed_mount4;
1845 }
1846 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1847 dput(sb->s_root);
1848 sb->s_root = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001849 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001850 goto failed_mount4;
1851 }
1852
Mingming Cao617ba132006-10-11 01:20:53 -07001853 ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854 /*
1855 * akpm: core read_super() calls in here with the superblock locked.
1856 * That deadlocks, because orphan cleanup needs to lock the superblock
1857 * in numerous places. Here we just pop the lock - it's relatively
1858 * harmless, because we are now ready to accept write_super() requests,
1859 * and aviro says that's the only reason for hanging onto the
1860 * superblock lock.
1861 */
Mingming Cao617ba132006-10-11 01:20:53 -07001862 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
1863 ext4_orphan_cleanup(sb, es);
1864 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001865 if (needs_recovery)
Mingming Cao617ba132006-10-11 01:20:53 -07001866 printk (KERN_INFO "EXT4-fs: recovery complete.\n");
1867 ext4_mark_recovery_complete(sb, es);
1868 printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
1869 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
1870 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001871 "writeback");
1872
Alex Tomasa86c6182006-10-11 01:21:03 -07001873 ext4_ext_init(sb);
1874
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001875 lock_kernel();
1876 return 0;
1877
Mingming Cao617ba132006-10-11 01:20:53 -07001878cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001879 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07001880 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001881 sb->s_id);
1882 goto failed_mount;
1883
1884failed_mount4:
Mingming Caodab291a2006-10-11 01:21:01 -07001885 jbd2_journal_destroy(sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001886failed_mount3:
1887 percpu_counter_destroy(&sbi->s_freeblocks_counter);
1888 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1889 percpu_counter_destroy(&sbi->s_dirs_counter);
1890failed_mount2:
1891 for (i = 0; i < db_count; i++)
1892 brelse(sbi->s_group_desc[i]);
1893 kfree(sbi->s_group_desc);
1894failed_mount:
1895#ifdef CONFIG_QUOTA
1896 for (i = 0; i < MAXQUOTAS; i++)
1897 kfree(sbi->s_qf_names[i]);
1898#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001899 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001900 brelse(bh);
1901out_fail:
1902 sb->s_fs_info = NULL;
1903 kfree(sbi);
1904 lock_kernel();
1905 return -EINVAL;
1906}
1907
1908/*
1909 * Setup any per-fs journal parameters now. We'll do this both on
1910 * initial mount, once the journal has been initialised but before we've
1911 * done any recovery; and again on any subsequent remount.
1912 */
Mingming Cao617ba132006-10-11 01:20:53 -07001913static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001914{
Mingming Cao617ba132006-10-11 01:20:53 -07001915 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001916
1917 if (sbi->s_commit_interval)
1918 journal->j_commit_interval = sbi->s_commit_interval;
Mingming Cao617ba132006-10-11 01:20:53 -07001919 /* We could also set up an ext4-specific default for the commit
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001920 * interval here, but for now we'll just fall back to the jbd
1921 * default. */
1922
1923 spin_lock(&journal->j_state_lock);
1924 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07001925 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001926 else
Mingming Caodab291a2006-10-11 01:21:01 -07001927 journal->j_flags &= ~JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001928 spin_unlock(&journal->j_state_lock);
1929}
1930
Mingming Cao617ba132006-10-11 01:20:53 -07001931static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001932 unsigned int journal_inum)
1933{
1934 struct inode *journal_inode;
1935 journal_t *journal;
1936
1937 /* First, test for the existence of a valid inode on disk. Bad
1938 * things happen if we iget() an unused inode, as the subsequent
1939 * iput() will try to delete it. */
1940
1941 journal_inode = iget(sb, journal_inum);
1942 if (!journal_inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001943 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001944 return NULL;
1945 }
1946 if (!journal_inode->i_nlink) {
1947 make_bad_inode(journal_inode);
1948 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07001949 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001950 return NULL;
1951 }
1952
1953 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1954 journal_inode, journal_inode->i_size);
1955 if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001956 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001957 iput(journal_inode);
1958 return NULL;
1959 }
1960
Mingming Caodab291a2006-10-11 01:21:01 -07001961 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001962 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07001963 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001964 iput(journal_inode);
1965 return NULL;
1966 }
1967 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001968 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969 return journal;
1970}
1971
Mingming Cao617ba132006-10-11 01:20:53 -07001972static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001973 dev_t j_dev)
1974{
1975 struct buffer_head * bh;
1976 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07001977 ext4_fsblk_t start;
1978 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001979 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07001980 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001981 unsigned long offset;
Mingming Cao617ba132006-10-11 01:20:53 -07001982 struct ext4_super_block * es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001983 struct block_device *bdev;
1984
Mingming Cao617ba132006-10-11 01:20:53 -07001985 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001986 if (bdev == NULL)
1987 return NULL;
1988
1989 if (bd_claim(bdev, sb)) {
1990 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001991 "EXT4: failed to claim external journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001992 blkdev_put(bdev);
1993 return NULL;
1994 }
1995
1996 blocksize = sb->s_blocksize;
1997 hblock = bdev_hardsect_size(bdev);
1998 if (blocksize < hblock) {
1999 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002000 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002001 goto out_bdev;
2002 }
2003
Mingming Cao617ba132006-10-11 01:20:53 -07002004 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2005 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002006 set_blocksize(bdev, blocksize);
2007 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002008 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002009 "external journal\n");
2010 goto out_bdev;
2011 }
2012
Mingming Cao617ba132006-10-11 01:20:53 -07002013 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2014 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002015 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002016 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2017 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002018 "bad superblock\n");
2019 brelse(bh);
2020 goto out_bdev;
2021 }
2022
Mingming Cao617ba132006-10-11 01:20:53 -07002023 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2024 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025 brelse(bh);
2026 goto out_bdev;
2027 }
2028
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002029 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002030 start = sb_block + 1;
2031 brelse(bh); /* we're done with the superblock */
2032
Mingming Caodab291a2006-10-11 01:21:01 -07002033 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 start, len, blocksize);
2035 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002036 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002037 goto out_bdev;
2038 }
2039 journal->j_private = sb;
2040 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2041 wait_on_buffer(journal->j_sb_buffer);
2042 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002043 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002044 goto out_journal;
2045 }
2046 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002047 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002048 "user (unsupported) - %d\n",
2049 be32_to_cpu(journal->j_superblock->s_nr_users));
2050 goto out_journal;
2051 }
Mingming Cao617ba132006-10-11 01:20:53 -07002052 EXT4_SB(sb)->journal_bdev = bdev;
2053 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002054 return journal;
2055out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002056 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002058 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002059 return NULL;
2060}
2061
Mingming Cao617ba132006-10-11 01:20:53 -07002062static int ext4_load_journal(struct super_block *sb,
2063 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002064 unsigned long journal_devnum)
2065{
2066 journal_t *journal;
2067 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2068 dev_t journal_dev;
2069 int err = 0;
2070 int really_read_only;
2071
2072 if (journal_devnum &&
2073 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002074 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002075 "numbers have changed\n");
2076 journal_dev = new_decode_dev(journal_devnum);
2077 } else
2078 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2079
2080 really_read_only = bdev_read_only(sb->s_bdev);
2081
2082 /*
2083 * Are we loading a blank journal or performing recovery after a
2084 * crash? For recovery, we need to check in advance whether we
2085 * can get read-write access to the device.
2086 */
2087
Mingming Cao617ba132006-10-11 01:20:53 -07002088 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002089 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002090 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002091 "required on readonly filesystem.\n");
2092 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002093 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002094 "unavailable, cannot proceed.\n");
2095 return -EROFS;
2096 }
Mingming Cao617ba132006-10-11 01:20:53 -07002097 printk (KERN_INFO "EXT4-fs: write access will "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002098 "be enabled during recovery.\n");
2099 }
2100 }
2101
2102 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002103 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002104 "and inode journals!\n");
2105 return -EINVAL;
2106 }
2107
2108 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002109 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 return -EINVAL;
2111 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002112 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002113 return -EINVAL;
2114 }
2115
2116 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002117 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002118 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002119 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002120 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002121 return err;
2122 }
2123 }
2124
Mingming Cao617ba132006-10-11 01:20:53 -07002125 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002126 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002127 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002128 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129
2130 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002131 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002132 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002133 return err;
2134 }
2135
Mingming Cao617ba132006-10-11 01:20:53 -07002136 EXT4_SB(sb)->s_journal = journal;
2137 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002138
2139 if (journal_devnum &&
2140 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2141 es->s_journal_dev = cpu_to_le32(journal_devnum);
2142 sb->s_dirt = 1;
2143
2144 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002145 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002146 }
2147
2148 return 0;
2149}
2150
Mingming Cao617ba132006-10-11 01:20:53 -07002151static int ext4_create_journal(struct super_block * sb,
2152 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002153 unsigned int journal_inum)
2154{
2155 journal_t *journal;
2156
2157 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002158 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002159 "create journal.\n");
2160 return -EROFS;
2161 }
2162
Mingming Cao617ba132006-10-11 01:20:53 -07002163 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002164 return -EINVAL;
2165
Mingming Cao617ba132006-10-11 01:20:53 -07002166 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002167 journal_inum);
2168
Mingming Caodab291a2006-10-11 01:21:01 -07002169 if (jbd2_journal_create(journal)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002170 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002171 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002172 return -EIO;
2173 }
2174
Mingming Cao617ba132006-10-11 01:20:53 -07002175 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002176
Mingming Cao617ba132006-10-11 01:20:53 -07002177 ext4_update_dynamic_rev(sb);
2178 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2179 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002180
2181 es->s_journal_inum = cpu_to_le32(journal_inum);
2182 sb->s_dirt = 1;
2183
2184 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002185 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002186
2187 return 0;
2188}
2189
Mingming Cao617ba132006-10-11 01:20:53 -07002190static void ext4_commit_super (struct super_block * sb,
2191 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002192 int sync)
2193{
Mingming Cao617ba132006-10-11 01:20:53 -07002194 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002195
2196 if (!sbh)
2197 return;
2198 es->s_wtime = cpu_to_le32(get_seconds());
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002199 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07002200 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002201 BUFFER_TRACE(sbh, "marking dirty");
2202 mark_buffer_dirty(sbh);
2203 if (sync)
2204 sync_dirty_buffer(sbh);
2205}
2206
2207
2208/*
2209 * Have we just finished recovery? If so, and if we are mounting (or
2210 * remounting) the filesystem readonly, then we will end up with a
2211 * consistent fs on disk. Record that fact.
2212 */
Mingming Cao617ba132006-10-11 01:20:53 -07002213static void ext4_mark_recovery_complete(struct super_block * sb,
2214 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002215{
Mingming Cao617ba132006-10-11 01:20:53 -07002216 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002217
Mingming Caodab291a2006-10-11 01:21:01 -07002218 jbd2_journal_lock_updates(journal);
2219 jbd2_journal_flush(journal);
Mingming Cao617ba132006-10-11 01:20:53 -07002220 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002221 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002222 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002223 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002224 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002225 }
Mingming Caodab291a2006-10-11 01:21:01 -07002226 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002227}
2228
2229/*
2230 * If we are mounting (or read-write remounting) a filesystem whose journal
2231 * has recorded an error from a previous lifetime, move that error to the
2232 * main filesystem now.
2233 */
Mingming Cao617ba132006-10-11 01:20:53 -07002234static void ext4_clear_journal_err(struct super_block * sb,
2235 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002236{
2237 journal_t *journal;
2238 int j_errno;
2239 const char *errstr;
2240
Mingming Cao617ba132006-10-11 01:20:53 -07002241 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242
2243 /*
2244 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07002245 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002246 */
2247
Mingming Caodab291a2006-10-11 01:21:01 -07002248 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002249 if (j_errno) {
2250 char nbuf[16];
2251
Mingming Cao617ba132006-10-11 01:20:53 -07002252 errstr = ext4_decode_error(sb, j_errno, nbuf);
2253 ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002254 "from previous mount: %s", errstr);
Mingming Cao617ba132006-10-11 01:20:53 -07002255 ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002256 "filesystem check.");
2257
Mingming Cao617ba132006-10-11 01:20:53 -07002258 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2259 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2260 ext4_commit_super (sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002261
Mingming Caodab291a2006-10-11 01:21:01 -07002262 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002263 }
2264}
2265
2266/*
2267 * Force the running and committing transactions to commit,
2268 * and wait on the commit.
2269 */
Mingming Cao617ba132006-10-11 01:20:53 -07002270int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271{
2272 journal_t *journal;
2273 int ret;
2274
2275 if (sb->s_flags & MS_RDONLY)
2276 return 0;
2277
Mingming Cao617ba132006-10-11 01:20:53 -07002278 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002279 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002280 ret = ext4_journal_force_commit(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002281 return ret;
2282}
2283
2284/*
Mingming Cao617ba132006-10-11 01:20:53 -07002285 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286 * have to propagate any other updates to the superblock on disk at this
2287 * point. Just start an async writeback to get the buffers on their way
2288 * to the disk.
2289 *
2290 * This implicitly triggers the writebehind on sync().
2291 */
2292
Mingming Cao617ba132006-10-11 01:20:53 -07002293static void ext4_write_super (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002294{
2295 if (mutex_trylock(&sb->s_lock) != 0)
2296 BUG();
2297 sb->s_dirt = 0;
2298}
2299
Mingming Cao617ba132006-10-11 01:20:53 -07002300static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002301{
2302 tid_t target;
2303
2304 sb->s_dirt = 0;
Mingming Caodab291a2006-10-11 01:21:01 -07002305 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002306 if (wait)
Mingming Caodab291a2006-10-11 01:21:01 -07002307 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002308 }
2309 return 0;
2310}
2311
2312/*
2313 * LVM calls this function before a (read-only) snapshot is created. This
2314 * gives us a chance to flush the journal completely and mark the fs clean.
2315 */
Mingming Cao617ba132006-10-11 01:20:53 -07002316static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002317{
2318 sb->s_dirt = 0;
2319
2320 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002321 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002322
2323 /* Now we set up the journal barrier. */
Mingming Caodab291a2006-10-11 01:21:01 -07002324 jbd2_journal_lock_updates(journal);
2325 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002326
2327 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07002328 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2329 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002330 }
2331}
2332
2333/*
2334 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2335 * flag here, even though the filesystem is not technically dirty yet.
2336 */
Mingming Cao617ba132006-10-11 01:20:53 -07002337static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002338{
2339 if (!(sb->s_flags & MS_RDONLY)) {
2340 lock_super(sb);
2341 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07002342 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2343 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002344 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002345 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002346 }
2347}
2348
Mingming Cao617ba132006-10-11 01:20:53 -07002349static int ext4_remount (struct super_block * sb, int * flags, char * data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002350{
Mingming Cao617ba132006-10-11 01:20:53 -07002351 struct ext4_super_block * es;
2352 struct ext4_sb_info *sbi = EXT4_SB(sb);
2353 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002354 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07002355 struct ext4_mount_options old_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002356 int err;
2357#ifdef CONFIG_QUOTA
2358 int i;
2359#endif
2360
2361 /* Store the original options */
2362 old_sb_flags = sb->s_flags;
2363 old_opts.s_mount_opt = sbi->s_mount_opt;
2364 old_opts.s_resuid = sbi->s_resuid;
2365 old_opts.s_resgid = sbi->s_resgid;
2366 old_opts.s_commit_interval = sbi->s_commit_interval;
2367#ifdef CONFIG_QUOTA
2368 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2369 for (i = 0; i < MAXQUOTAS; i++)
2370 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2371#endif
2372
2373 /*
2374 * Allow the "check" option to be passed as a remount option.
2375 */
2376 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2377 err = -EINVAL;
2378 goto restore_opts;
2379 }
2380
Mingming Cao617ba132006-10-11 01:20:53 -07002381 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2382 ext4_abort(sb, __FUNCTION__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383
2384 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002385 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002386
2387 es = sbi->s_es;
2388
Mingming Cao617ba132006-10-11 01:20:53 -07002389 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002390
2391 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002392 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002393 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002394 err = -EROFS;
2395 goto restore_opts;
2396 }
2397
2398 if (*flags & MS_RDONLY) {
2399 /*
2400 * First of all, the unconditional stuff we have to do
2401 * to disable replay of the journal when we next remount
2402 */
2403 sb->s_flags |= MS_RDONLY;
2404
2405 /*
2406 * OK, test if we are remounting a valid rw partition
2407 * readonly, and if so set the rdonly flag and then
2408 * mark the partition as valid again.
2409 */
Mingming Cao617ba132006-10-11 01:20:53 -07002410 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2411 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002412 es->s_state = cpu_to_le16(sbi->s_mount_state);
2413
Mingming Cao617ba132006-10-11 01:20:53 -07002414 ext4_mark_recovery_complete(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002415 } else {
2416 __le32 ret;
Mingming Cao617ba132006-10-11 01:20:53 -07002417 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2418 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2419 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002420 "remount RDWR because of unsupported "
2421 "optional features (%x).\n",
2422 sb->s_id, le32_to_cpu(ret));
2423 err = -EROFS;
2424 goto restore_opts;
2425 }
Eric Sandeenead65962007-02-10 01:46:08 -08002426
2427 /*
2428 * If we have an unprocessed orphan list hanging
2429 * around from a previously readonly bdev mount,
2430 * require a full umount/remount for now.
2431 */
2432 if (es->s_last_orphan) {
2433 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2434 "remount RDWR because of unprocessed "
2435 "orphan inode list. Please "
2436 "umount/remount instead.\n",
2437 sb->s_id);
2438 err = -EINVAL;
2439 goto restore_opts;
2440 }
2441
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002442 /*
2443 * Mounting a RDONLY partition read-write, so reread
2444 * and store the current valid flag. (It may have
2445 * been changed by e2fsck since we originally mounted
2446 * the partition.)
2447 */
Mingming Cao617ba132006-10-11 01:20:53 -07002448 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002449 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07002450 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002451 goto restore_opts;
Mingming Cao617ba132006-10-11 01:20:53 -07002452 if (!ext4_setup_super (sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002453 sb->s_flags &= ~MS_RDONLY;
2454 }
2455 }
2456#ifdef CONFIG_QUOTA
2457 /* Release old quota file names */
2458 for (i = 0; i < MAXQUOTAS; i++)
2459 if (old_opts.s_qf_names[i] &&
2460 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2461 kfree(old_opts.s_qf_names[i]);
2462#endif
2463 return 0;
2464restore_opts:
2465 sb->s_flags = old_sb_flags;
2466 sbi->s_mount_opt = old_opts.s_mount_opt;
2467 sbi->s_resuid = old_opts.s_resuid;
2468 sbi->s_resgid = old_opts.s_resgid;
2469 sbi->s_commit_interval = old_opts.s_commit_interval;
2470#ifdef CONFIG_QUOTA
2471 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2472 for (i = 0; i < MAXQUOTAS; i++) {
2473 if (sbi->s_qf_names[i] &&
2474 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2475 kfree(sbi->s_qf_names[i]);
2476 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2477 }
2478#endif
2479 return err;
2480}
2481
Mingming Cao617ba132006-10-11 01:20:53 -07002482static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002483{
2484 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002485 struct ext4_sb_info *sbi = EXT4_SB(sb);
2486 struct ext4_super_block *es = sbi->s_es;
2487 ext4_fsblk_t overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002488 int i;
Pekka Enberg960cc392006-12-06 20:35:29 -08002489 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002490
2491 if (test_opt (sb, MINIX_DF))
2492 overhead = 0;
2493 else {
2494 unsigned long ngroups;
Mingming Cao617ba132006-10-11 01:20:53 -07002495 ngroups = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002496 smp_rmb();
2497
2498 /*
2499 * Compute the overhead (FS structures)
2500 */
2501
2502 /*
2503 * All of the blocks before first_data_block are
2504 * overhead
2505 */
2506 overhead = le32_to_cpu(es->s_first_data_block);
2507
2508 /*
2509 * Add the overhead attributed to the superblock and
2510 * block group descriptors. If the sparse superblocks
2511 * feature is turned on, then not all groups have this.
2512 */
2513 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07002514 overhead += ext4_bg_has_super(sb, i) +
2515 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002516 cond_resched();
2517 }
2518
2519 /*
2520 * Every block group has an inode bitmap, a block
2521 * bitmap, and an inode table.
2522 */
Mingming Cao617ba132006-10-11 01:20:53 -07002523 overhead += (ngroups * (2 + EXT4_SB(sb)->s_itb_per_group));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002524 }
2525
Mingming Cao617ba132006-10-11 01:20:53 -07002526 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002527 buf->f_bsize = sb->s_blocksize;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002528 buf->f_blocks = ext4_blocks_count(es) - overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002529 buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002530 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2531 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002532 buf->f_bavail = 0;
2533 buf->f_files = le32_to_cpu(es->s_inodes_count);
2534 buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
Mingming Cao617ba132006-10-11 01:20:53 -07002535 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08002536 fsid = le64_to_cpup((void *)es->s_uuid) ^
2537 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2538 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2539 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002540 return 0;
2541}
2542
2543/* Helper function for writing quotas on sync - we need to start transaction before quota file
2544 * is locked for write. Otherwise the are possible deadlocks:
2545 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07002546 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07002547 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002548 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07002549 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002550 *
2551 */
2552
2553#ifdef CONFIG_QUOTA
2554
2555static inline struct inode *dquot_to_inode(struct dquot *dquot)
2556{
2557 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2558}
2559
Mingming Cao617ba132006-10-11 01:20:53 -07002560static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002561{
2562 handle_t *handle;
2563 int ret, err;
2564
2565 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07002566 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002567 if (IS_ERR(handle))
2568 return PTR_ERR(handle);
2569 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07002570 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002571 if (!ret)
2572 ret = err;
2573 return ret;
2574}
2575
Mingming Cao617ba132006-10-11 01:20:53 -07002576static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002577{
2578 handle_t *handle;
2579 int ret, err;
2580
2581 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07002582 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002583 if (IS_ERR(handle))
2584 return PTR_ERR(handle);
2585 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002586 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002587 if (!ret)
2588 ret = err;
2589 return ret;
2590}
2591
Mingming Cao617ba132006-10-11 01:20:53 -07002592static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002593{
2594 int ret, err;
2595 handle_t *handle;
2596 struct inode *inode;
2597
2598 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002599 handle = ext4_journal_start(inode,
2600 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002601 if (IS_ERR(handle))
2602 return PTR_ERR(handle);
2603 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002604 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002605 if (!ret)
2606 ret = err;
2607 return ret;
2608}
2609
Mingming Cao617ba132006-10-11 01:20:53 -07002610static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002611{
2612 int ret, err;
2613 handle_t *handle;
2614
Mingming Cao617ba132006-10-11 01:20:53 -07002615 handle = ext4_journal_start(dquot_to_inode(dquot),
2616 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002617 if (IS_ERR(handle))
2618 return PTR_ERR(handle);
2619 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002620 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002621 if (!ret)
2622 ret = err;
2623 return ret;
2624}
2625
Mingming Cao617ba132006-10-11 01:20:53 -07002626static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002627{
2628 int ret, err;
2629 handle_t *handle;
2630
Mingming Cao617ba132006-10-11 01:20:53 -07002631 handle = ext4_journal_start(dquot_to_inode(dquot),
2632 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002633 if (IS_ERR(handle))
2634 return PTR_ERR(handle);
2635 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002636 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002637 if (!ret)
2638 ret = err;
2639 return ret;
2640}
2641
Mingming Cao617ba132006-10-11 01:20:53 -07002642static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002643{
2644 /* Are we journalling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07002645 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2646 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002647 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002648 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002649 } else {
2650 return dquot_mark_dquot_dirty(dquot);
2651 }
2652}
2653
Mingming Cao617ba132006-10-11 01:20:53 -07002654static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002655{
2656 int ret, err;
2657 handle_t *handle;
2658
2659 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07002660 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002661 if (IS_ERR(handle))
2662 return PTR_ERR(handle);
2663 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07002664 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002665 if (!ret)
2666 ret = err;
2667 return ret;
2668}
2669
2670/*
2671 * Turn on quotas during mount time - we need to find
2672 * the quota file and such...
2673 */
Mingming Cao617ba132006-10-11 01:20:53 -07002674static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002675{
Mingming Cao617ba132006-10-11 01:20:53 -07002676 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
2677 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002678}
2679
2680/*
2681 * Standard function to be called on quota_on
2682 */
Mingming Cao617ba132006-10-11 01:20:53 -07002683static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002684 char *path)
2685{
2686 int err;
2687 struct nameidata nd;
2688
2689 if (!test_opt(sb, QUOTA))
2690 return -EINVAL;
2691 /* Not journalling quota? */
Mingming Cao617ba132006-10-11 01:20:53 -07002692 if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
2693 !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002694 return vfs_quota_on(sb, type, format_id, path);
2695 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2696 if (err)
2697 return err;
2698 /* Quotafile not on the same filesystem? */
2699 if (nd.mnt->mnt_sb != sb) {
2700 path_release(&nd);
2701 return -EXDEV;
2702 }
2703 /* Quotafile not of fs root? */
2704 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2705 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002706 "EXT4-fs: Quota file not on filesystem root. "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002707 "Journalled quota will not work.\n");
2708 path_release(&nd);
2709 return vfs_quota_on(sb, type, format_id, path);
2710}
2711
2712/* Read data from quotafile - avoid pagecache and such because we cannot afford
2713 * acquiring the locks... As quota files are never truncated and quota code
2714 * itself serializes the operations (and noone else should touch the files)
2715 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07002716static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002717 size_t len, loff_t off)
2718{
2719 struct inode *inode = sb_dqopt(sb)->files[type];
Mingming Cao617ba132006-10-11 01:20:53 -07002720 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002721 int err = 0;
2722 int offset = off & (sb->s_blocksize - 1);
2723 int tocopy;
2724 size_t toread;
2725 struct buffer_head *bh;
2726 loff_t i_size = i_size_read(inode);
2727
2728 if (off > i_size)
2729 return 0;
2730 if (off+len > i_size)
2731 len = i_size-off;
2732 toread = len;
2733 while (toread > 0) {
2734 tocopy = sb->s_blocksize - offset < toread ?
2735 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07002736 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002737 if (err)
2738 return err;
2739 if (!bh) /* A hole? */
2740 memset(data, 0, tocopy);
2741 else
2742 memcpy(data, bh->b_data+offset, tocopy);
2743 brelse(bh);
2744 offset = 0;
2745 toread -= tocopy;
2746 data += tocopy;
2747 blk++;
2748 }
2749 return len;
2750}
2751
2752/* Write to quotafile (we know the transaction is already started and has
2753 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07002754static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002755 const char *data, size_t len, loff_t off)
2756{
2757 struct inode *inode = sb_dqopt(sb)->files[type];
Mingming Cao617ba132006-10-11 01:20:53 -07002758 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002759 int err = 0;
2760 int offset = off & (sb->s_blocksize - 1);
2761 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07002762 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002763 size_t towrite = len;
2764 struct buffer_head *bh;
2765 handle_t *handle = journal_current_handle();
2766
2767 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2768 while (towrite > 0) {
2769 tocopy = sb->s_blocksize - offset < towrite ?
2770 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07002771 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002772 if (!bh)
2773 goto out;
2774 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07002775 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002776 if (err) {
2777 brelse(bh);
2778 goto out;
2779 }
2780 }
2781 lock_buffer(bh);
2782 memcpy(bh->b_data+offset, data, tocopy);
2783 flush_dcache_page(bh->b_page);
2784 unlock_buffer(bh);
2785 if (journal_quota)
Mingming Cao617ba132006-10-11 01:20:53 -07002786 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002787 else {
2788 /* Always do at least ordered writes for quotas */
Mingming Cao617ba132006-10-11 01:20:53 -07002789 err = ext4_journal_dirty_data(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002790 mark_buffer_dirty(bh);
2791 }
2792 brelse(bh);
2793 if (err)
2794 goto out;
2795 offset = 0;
2796 towrite -= tocopy;
2797 data += tocopy;
2798 blk++;
2799 }
2800out:
2801 if (len == towrite)
2802 return err;
2803 if (inode->i_size < off+len-towrite) {
2804 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07002805 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002806 }
2807 inode->i_version++;
2808 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07002809 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002810 mutex_unlock(&inode->i_mutex);
2811 return len - towrite;
2812}
2813
2814#endif
2815
Mingming Cao617ba132006-10-11 01:20:53 -07002816static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002817 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2818{
Mingming Cao617ba132006-10-11 01:20:53 -07002819 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002820}
2821
Mingming Cao617ba132006-10-11 01:20:53 -07002822static struct file_system_type ext4dev_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002823 .owner = THIS_MODULE,
Mingming Cao617ba132006-10-11 01:20:53 -07002824 .name = "ext4dev",
2825 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002826 .kill_sb = kill_block_super,
2827 .fs_flags = FS_REQUIRES_DEV,
2828};
2829
Mingming Cao617ba132006-10-11 01:20:53 -07002830static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002831{
Mingming Cao617ba132006-10-11 01:20:53 -07002832 int err = init_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002833 if (err)
2834 return err;
2835 err = init_inodecache();
2836 if (err)
2837 goto out1;
Andrew Morton63f57932006-10-11 01:21:24 -07002838 err = register_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002839 if (err)
2840 goto out;
2841 return 0;
2842out:
2843 destroy_inodecache();
2844out1:
Mingming Cao617ba132006-10-11 01:20:53 -07002845 exit_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002846 return err;
2847}
2848
Mingming Cao617ba132006-10-11 01:20:53 -07002849static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002850{
Mingming Cao617ba132006-10-11 01:20:53 -07002851 unregister_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002852 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07002853 exit_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002854}
2855
2856MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07002857MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002858MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07002859module_init(init_ext4_fs)
2860module_exit(exit_ext4_fs)