blob: 175b68c609685fb06742caf6bed160ca7a98d68b [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
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700473 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474 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);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700481 invalidate_bdev(sbi->journal_bdev);
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
Christoph Lametera35afb82007-05-16 22:10:57 -0700520 INIT_LIST_HEAD(&ei->i_orphan);
Mingming Cao617ba132006-10-11 01:20:53 -0700521#ifdef CONFIG_EXT4DEV_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700522 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523#endif
Christoph Lametera35afb82007-05-16 22:10:57 -0700524 mutex_init(&ei->truncate_mutex);
525 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526}
527
528static int init_inodecache(void)
529{
Mingming Cao617ba132006-10-11 01:20:53 -0700530 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
531 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 0, (SLAB_RECLAIM_ACCOUNT|
533 SLAB_MEM_SPREAD),
534 init_once, NULL);
Mingming Cao617ba132006-10-11 01:20:53 -0700535 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700536 return -ENOMEM;
537 return 0;
538}
539
540static void destroy_inodecache(void)
541{
Mingming Cao617ba132006-10-11 01:20:53 -0700542 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543}
544
Mingming Cao617ba132006-10-11 01:20:53 -0700545static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546{
Mingming Cao617ba132006-10-11 01:20:53 -0700547 struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
548#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
549 if (EXT4_I(inode)->i_acl &&
550 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
551 posix_acl_release(EXT4_I(inode)->i_acl);
552 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553 }
Mingming Cao617ba132006-10-11 01:20:53 -0700554 if (EXT4_I(inode)->i_default_acl &&
555 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
556 posix_acl_release(EXT4_I(inode)->i_default_acl);
557 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558 }
559#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700560 ext4_discard_reservation(inode);
561 EXT4_I(inode)->i_block_alloc_info = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562 if (unlikely(rsv))
563 kfree(rsv);
564}
565
Mingming Cao617ba132006-10-11 01:20:53 -0700566static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567{
568#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700569 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570
571 if (sbi->s_jquota_fmt)
572 seq_printf(seq, ",jqfmt=%s",
573 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
574
575 if (sbi->s_qf_names[USRQUOTA])
576 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
577
578 if (sbi->s_qf_names[GRPQUOTA])
579 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
580
Mingming Cao617ba132006-10-11 01:20:53 -0700581 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 seq_puts(seq, ",usrquota");
583
Mingming Cao617ba132006-10-11 01:20:53 -0700584 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585 seq_puts(seq, ",grpquota");
586#endif
587}
588
Mingming Cao617ba132006-10-11 01:20:53 -0700589static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590{
591 struct super_block *sb = vfs->mnt_sb;
592
Mingming Cao617ba132006-10-11 01:20:53 -0700593 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700594 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700595 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700597 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700598 seq_puts(seq, ",data=writeback");
599
Mingming Cao617ba132006-10-11 01:20:53 -0700600 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601
602 return 0;
603}
604
605
Mingming Cao617ba132006-10-11 01:20:53 -0700606static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607{
608 __u32 *objp = vobjp;
609 unsigned long ino = objp[0];
610 __u32 generation = objp[1];
611 struct inode *inode;
612 struct dentry *result;
613
Mingming Cao617ba132006-10-11 01:20:53 -0700614 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700615 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700616 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617 return ERR_PTR(-ESTALE);
618
619 /* iget isn't really right if the inode is currently unallocated!!
620 *
Mingming Cao617ba132006-10-11 01:20:53 -0700621 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700622 * deleted, so we should be safe.
623 *
624 * Currently we don't know the generation for parent directory, so
625 * a generation of 0 means "accept any"
626 */
627 inode = iget(sb, ino);
628 if (inode == NULL)
629 return ERR_PTR(-ENOMEM);
630 if (is_bad_inode(inode) ||
631 (generation && inode->i_generation != generation)) {
632 iput(inode);
633 return ERR_PTR(-ESTALE);
634 }
635 /* now to find a dentry.
636 * If possible, get a well-connected one
637 */
638 result = d_alloc_anon(inode);
639 if (!result) {
640 iput(inode);
641 return ERR_PTR(-ENOMEM);
642 }
643 return result;
644}
645
646#ifdef CONFIG_QUOTA
647#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
648#define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
649
Mingming Cao617ba132006-10-11 01:20:53 -0700650static int ext4_dquot_initialize(struct inode *inode, int type);
651static int ext4_dquot_drop(struct inode *inode);
652static int ext4_write_dquot(struct dquot *dquot);
653static int ext4_acquire_dquot(struct dquot *dquot);
654static int ext4_release_dquot(struct dquot *dquot);
655static int ext4_mark_dquot_dirty(struct dquot *dquot);
656static int ext4_write_info(struct super_block *sb, int type);
657static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
658static int ext4_quota_on_mount(struct super_block *sb, int type);
659static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700661static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700662 const char *data, size_t len, loff_t off);
663
Mingming Cao617ba132006-10-11 01:20:53 -0700664static struct dquot_operations ext4_quota_operations = {
665 .initialize = ext4_dquot_initialize,
666 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667 .alloc_space = dquot_alloc_space,
668 .alloc_inode = dquot_alloc_inode,
669 .free_space = dquot_free_space,
670 .free_inode = dquot_free_inode,
671 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700672 .write_dquot = ext4_write_dquot,
673 .acquire_dquot = ext4_acquire_dquot,
674 .release_dquot = ext4_release_dquot,
675 .mark_dirty = ext4_mark_dquot_dirty,
676 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677};
678
Mingming Cao617ba132006-10-11 01:20:53 -0700679static struct quotactl_ops ext4_qctl_operations = {
680 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 .quota_off = vfs_quota_off,
682 .quota_sync = vfs_quota_sync,
683 .get_info = vfs_get_dqinfo,
684 .set_info = vfs_set_dqinfo,
685 .get_dqblk = vfs_get_dqblk,
686 .set_dqblk = vfs_set_dqblk
687};
688#endif
689
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800690static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700691 .alloc_inode = ext4_alloc_inode,
692 .destroy_inode = ext4_destroy_inode,
693 .read_inode = ext4_read_inode,
694 .write_inode = ext4_write_inode,
695 .dirty_inode = ext4_dirty_inode,
696 .delete_inode = ext4_delete_inode,
697 .put_super = ext4_put_super,
698 .write_super = ext4_write_super,
699 .sync_fs = ext4_sync_fs,
700 .write_super_lockfs = ext4_write_super_lockfs,
701 .unlockfs = ext4_unlockfs,
702 .statfs = ext4_statfs,
703 .remount_fs = ext4_remount,
704 .clear_inode = ext4_clear_inode,
705 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700707 .quota_read = ext4_quota_read,
708 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709#endif
710};
711
Mingming Cao617ba132006-10-11 01:20:53 -0700712static struct export_operations ext4_export_ops = {
713 .get_parent = ext4_get_parent,
714 .get_dentry = ext4_get_dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715};
716
717enum {
718 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
719 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
720 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
721 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
722 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
723 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
724 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
725 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
726 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
727 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Alex Tomasa86c6182006-10-11 01:21:03 -0700728 Opt_grpquota, Opt_extents,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729};
730
731static match_table_t tokens = {
732 {Opt_bsd_df, "bsddf"},
733 {Opt_minix_df, "minixdf"},
734 {Opt_grpid, "grpid"},
735 {Opt_grpid, "bsdgroups"},
736 {Opt_nogrpid, "nogrpid"},
737 {Opt_nogrpid, "sysvgroups"},
738 {Opt_resgid, "resgid=%u"},
739 {Opt_resuid, "resuid=%u"},
740 {Opt_sb, "sb=%u"},
741 {Opt_err_cont, "errors=continue"},
742 {Opt_err_panic, "errors=panic"},
743 {Opt_err_ro, "errors=remount-ro"},
744 {Opt_nouid32, "nouid32"},
745 {Opt_nocheck, "nocheck"},
746 {Opt_nocheck, "check=none"},
747 {Opt_debug, "debug"},
748 {Opt_oldalloc, "oldalloc"},
749 {Opt_orlov, "orlov"},
750 {Opt_user_xattr, "user_xattr"},
751 {Opt_nouser_xattr, "nouser_xattr"},
752 {Opt_acl, "acl"},
753 {Opt_noacl, "noacl"},
754 {Opt_reservation, "reservation"},
755 {Opt_noreservation, "noreservation"},
756 {Opt_noload, "noload"},
757 {Opt_nobh, "nobh"},
758 {Opt_bh, "bh"},
759 {Opt_commit, "commit=%u"},
760 {Opt_journal_update, "journal=update"},
761 {Opt_journal_inum, "journal=%u"},
762 {Opt_journal_dev, "journal_dev=%u"},
763 {Opt_abort, "abort"},
764 {Opt_data_journal, "data=journal"},
765 {Opt_data_ordered, "data=ordered"},
766 {Opt_data_writeback, "data=writeback"},
767 {Opt_offusrjquota, "usrjquota="},
768 {Opt_usrjquota, "usrjquota=%s"},
769 {Opt_offgrpjquota, "grpjquota="},
770 {Opt_grpjquota, "grpjquota=%s"},
771 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
772 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
773 {Opt_grpquota, "grpquota"},
774 {Opt_noquota, "noquota"},
775 {Opt_quota, "quota"},
776 {Opt_usrquota, "usrquota"},
777 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -0700778 {Opt_extents, "extents"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779 {Opt_err, NULL},
780 {Opt_resize, "resize"},
781};
782
Mingming Cao617ba132006-10-11 01:20:53 -0700783static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700784{
Mingming Cao617ba132006-10-11 01:20:53 -0700785 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 char *options = (char *) *data;
787
788 if (!options || strncmp(options, "sb=", 3) != 0)
789 return 1; /* Default location */
790 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -0700791 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 sb_block = simple_strtoul(options, &options, 0);
793 if (*options && *options != ',') {
Mingming Cao617ba132006-10-11 01:20:53 -0700794 printk("EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 (char *) *data);
796 return 1;
797 }
798 if (*options == ',')
799 options++;
800 *data = (void *) options;
801 return sb_block;
802}
803
804static int parse_options (char *options, struct super_block *sb,
805 unsigned int *inum, unsigned long *journal_devnum,
Mingming Cao617ba132006-10-11 01:20:53 -0700806 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807{
Mingming Cao617ba132006-10-11 01:20:53 -0700808 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 char * p;
810 substring_t args[MAX_OPT_ARGS];
811 int data_opt = 0;
812 int option;
813#ifdef CONFIG_QUOTA
814 int qtype;
815 char *qname;
816#endif
817
818 if (!options)
819 return 1;
820
821 while ((p = strsep (&options, ",")) != NULL) {
822 int token;
823 if (!*p)
824 continue;
825
826 token = match_token(p, tokens, args);
827 switch (token) {
828 case Opt_bsd_df:
829 clear_opt (sbi->s_mount_opt, MINIX_DF);
830 break;
831 case Opt_minix_df:
832 set_opt (sbi->s_mount_opt, MINIX_DF);
833 break;
834 case Opt_grpid:
835 set_opt (sbi->s_mount_opt, GRPID);
836 break;
837 case Opt_nogrpid:
838 clear_opt (sbi->s_mount_opt, GRPID);
839 break;
840 case Opt_resuid:
841 if (match_int(&args[0], &option))
842 return 0;
843 sbi->s_resuid = option;
844 break;
845 case Opt_resgid:
846 if (match_int(&args[0], &option))
847 return 0;
848 sbi->s_resgid = option;
849 break;
850 case Opt_sb:
851 /* handled by get_sb_block() instead of here */
852 /* *sb_block = match_int(&args[0]); */
853 break;
854 case Opt_err_panic:
855 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
856 clear_opt (sbi->s_mount_opt, ERRORS_RO);
857 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
858 break;
859 case Opt_err_ro:
860 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
861 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
862 set_opt (sbi->s_mount_opt, ERRORS_RO);
863 break;
864 case Opt_err_cont:
865 clear_opt (sbi->s_mount_opt, ERRORS_RO);
866 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
867 set_opt (sbi->s_mount_opt, ERRORS_CONT);
868 break;
869 case Opt_nouid32:
870 set_opt (sbi->s_mount_opt, NO_UID32);
871 break;
872 case Opt_nocheck:
873 clear_opt (sbi->s_mount_opt, CHECK);
874 break;
875 case Opt_debug:
876 set_opt (sbi->s_mount_opt, DEBUG);
877 break;
878 case Opt_oldalloc:
879 set_opt (sbi->s_mount_opt, OLDALLOC);
880 break;
881 case Opt_orlov:
882 clear_opt (sbi->s_mount_opt, OLDALLOC);
883 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700884#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700885 case Opt_user_xattr:
886 set_opt (sbi->s_mount_opt, XATTR_USER);
887 break;
888 case Opt_nouser_xattr:
889 clear_opt (sbi->s_mount_opt, XATTR_USER);
890 break;
891#else
892 case Opt_user_xattr:
893 case Opt_nouser_xattr:
Mingming Cao617ba132006-10-11 01:20:53 -0700894 printk("EXT4 (no)user_xattr options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 break;
896#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700897#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700898 case Opt_acl:
899 set_opt(sbi->s_mount_opt, POSIX_ACL);
900 break;
901 case Opt_noacl:
902 clear_opt(sbi->s_mount_opt, POSIX_ACL);
903 break;
904#else
905 case Opt_acl:
906 case Opt_noacl:
Mingming Cao617ba132006-10-11 01:20:53 -0700907 printk("EXT4 (no)acl options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 break;
909#endif
910 case Opt_reservation:
911 set_opt(sbi->s_mount_opt, RESERVATION);
912 break;
913 case Opt_noreservation:
914 clear_opt(sbi->s_mount_opt, RESERVATION);
915 break;
916 case Opt_journal_update:
917 /* @@@ FIXME */
918 /* Eventually we will want to be able to create
919 a journal file here. For now, only allow the
920 user to specify an existing inode to be the
921 journal file. */
922 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700923 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700924 "journal on remount\n");
925 return 0;
926 }
927 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
928 break;
929 case Opt_journal_inum:
930 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700931 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 "journal on remount\n");
933 return 0;
934 }
935 if (match_int(&args[0], &option))
936 return 0;
937 *inum = option;
938 break;
939 case Opt_journal_dev:
940 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700941 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942 "journal on remount\n");
943 return 0;
944 }
945 if (match_int(&args[0], &option))
946 return 0;
947 *journal_devnum = option;
948 break;
949 case Opt_noload:
950 set_opt (sbi->s_mount_opt, NOLOAD);
951 break;
952 case Opt_commit:
953 if (match_int(&args[0], &option))
954 return 0;
955 if (option < 0)
956 return 0;
957 if (option == 0)
958 option = JBD_DEFAULT_MAX_COMMIT_AGE;
959 sbi->s_commit_interval = HZ * option;
960 break;
961 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -0700962 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 goto datacheck;
964 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -0700965 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 goto datacheck;
967 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -0700968 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700969 datacheck:
970 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700971 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972 != data_opt) {
973 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700974 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700975 "mode on remount\n");
976 return 0;
977 }
978 } else {
Mingming Cao617ba132006-10-11 01:20:53 -0700979 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700980 sbi->s_mount_opt |= data_opt;
981 }
982 break;
983#ifdef CONFIG_QUOTA
984 case Opt_usrjquota:
985 qtype = USRQUOTA;
986 goto set_qf_name;
987 case Opt_grpjquota:
988 qtype = GRPQUOTA;
989set_qf_name:
990 if (sb_any_quota_enabled(sb)) {
991 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700992 "EXT4-fs: Cannot change journalled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 "quota options when quota turned on.\n");
994 return 0;
995 }
996 qname = match_strdup(&args[0]);
997 if (!qname) {
998 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700999 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000 "storing quotafile name.\n");
1001 return 0;
1002 }
1003 if (sbi->s_qf_names[qtype] &&
1004 strcmp(sbi->s_qf_names[qtype], qname)) {
1005 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001006 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001007 "specified.\n", QTYPE2NAME(qtype));
1008 kfree(qname);
1009 return 0;
1010 }
1011 sbi->s_qf_names[qtype] = qname;
1012 if (strchr(sbi->s_qf_names[qtype], '/')) {
1013 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001014 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015 "filesystem root.\n");
1016 kfree(sbi->s_qf_names[qtype]);
1017 sbi->s_qf_names[qtype] = NULL;
1018 return 0;
1019 }
1020 set_opt(sbi->s_mount_opt, QUOTA);
1021 break;
1022 case Opt_offusrjquota:
1023 qtype = USRQUOTA;
1024 goto clear_qf_name;
1025 case Opt_offgrpjquota:
1026 qtype = GRPQUOTA;
1027clear_qf_name:
1028 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001029 printk(KERN_ERR "EXT4-fs: Cannot change "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001030 "journalled quota options when "
1031 "quota turned on.\n");
1032 return 0;
1033 }
1034 /*
1035 * The space will be released later when all options
1036 * are confirmed to be correct
1037 */
1038 sbi->s_qf_names[qtype] = NULL;
1039 break;
1040 case Opt_jqfmt_vfsold:
1041 sbi->s_jquota_fmt = QFMT_VFS_OLD;
1042 break;
1043 case Opt_jqfmt_vfsv0:
1044 sbi->s_jquota_fmt = QFMT_VFS_V0;
1045 break;
1046 case Opt_quota:
1047 case Opt_usrquota:
1048 set_opt(sbi->s_mount_opt, QUOTA);
1049 set_opt(sbi->s_mount_opt, USRQUOTA);
1050 break;
1051 case Opt_grpquota:
1052 set_opt(sbi->s_mount_opt, QUOTA);
1053 set_opt(sbi->s_mount_opt, GRPQUOTA);
1054 break;
1055 case Opt_noquota:
1056 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001057 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 "options when quota turned on.\n");
1059 return 0;
1060 }
1061 clear_opt(sbi->s_mount_opt, QUOTA);
1062 clear_opt(sbi->s_mount_opt, USRQUOTA);
1063 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1064 break;
1065#else
1066 case Opt_quota:
1067 case Opt_usrquota:
1068 case Opt_grpquota:
1069 case Opt_usrjquota:
1070 case Opt_grpjquota:
1071 case Opt_offusrjquota:
1072 case Opt_offgrpjquota:
1073 case Opt_jqfmt_vfsold:
1074 case Opt_jqfmt_vfsv0:
1075 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001076 "EXT4-fs: journalled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001077 "supported.\n");
1078 break;
1079 case Opt_noquota:
1080 break;
1081#endif
1082 case Opt_abort:
1083 set_opt(sbi->s_mount_opt, ABORT);
1084 break;
1085 case Opt_barrier:
1086 if (match_int(&args[0], &option))
1087 return 0;
1088 if (option)
1089 set_opt(sbi->s_mount_opt, BARRIER);
1090 else
1091 clear_opt(sbi->s_mount_opt, BARRIER);
1092 break;
1093 case Opt_ignore:
1094 break;
1095 case Opt_resize:
1096 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001097 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098 "for remount\n");
1099 return 0;
1100 }
1101 if (match_int(&args[0], &option) != 0)
1102 return 0;
1103 *n_blocks_count = option;
1104 break;
1105 case Opt_nobh:
1106 set_opt(sbi->s_mount_opt, NOBH);
1107 break;
1108 case Opt_bh:
1109 clear_opt(sbi->s_mount_opt, NOBH);
1110 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001111 case Opt_extents:
1112 set_opt (sbi->s_mount_opt, EXTENTS);
1113 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114 default:
1115 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001116 "EXT4-fs: Unrecognized mount option \"%s\" "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 "or missing value\n", p);
1118 return 0;
1119 }
1120 }
1121#ifdef CONFIG_QUOTA
1122 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001123 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 sbi->s_qf_names[USRQUOTA])
1125 clear_opt(sbi->s_mount_opt, USRQUOTA);
1126
Mingming Cao617ba132006-10-11 01:20:53 -07001127 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001128 sbi->s_qf_names[GRPQUOTA])
1129 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1130
1131 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001132 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001134 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1135 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 "format mixing.\n");
1137 return 0;
1138 }
1139
1140 if (!sbi->s_jquota_fmt) {
Mingming Cao617ba132006-10-11 01:20:53 -07001141 printk(KERN_ERR "EXT4-fs: journalled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001142 "not specified.\n");
1143 return 0;
1144 }
1145 } else {
1146 if (sbi->s_jquota_fmt) {
Mingming Cao617ba132006-10-11 01:20:53 -07001147 printk(KERN_ERR "EXT4-fs: journalled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 "specified with no journalling "
1149 "enabled.\n");
1150 return 0;
1151 }
1152 }
1153#endif
1154 return 1;
1155}
1156
Mingming Cao617ba132006-10-11 01:20:53 -07001157static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001158 int read_only)
1159{
Mingming Cao617ba132006-10-11 01:20:53 -07001160 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161 int res = 0;
1162
Mingming Cao617ba132006-10-11 01:20:53 -07001163 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1164 printk (KERN_ERR "EXT4-fs warning: revision level too high, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 "forcing read-only mode\n");
1166 res = MS_RDONLY;
1167 }
1168 if (read_only)
1169 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001170 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1171 printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001173 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001175 "EXT4-fs warning: mounting fs with errors, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001176 "running e2fsck is recommended\n");
1177 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1178 le16_to_cpu(es->s_mnt_count) >=
1179 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1180 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001181 "EXT4-fs warning: maximal mount count reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001182 "running e2fsck is recommended\n");
1183 else if (le32_to_cpu(es->s_checkinterval) &&
1184 (le32_to_cpu(es->s_lastcheck) +
1185 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1186 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001187 "EXT4-fs warning: checktime reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188 "running e2fsck is recommended\n");
1189#if 0
1190 /* @@@ We _will_ want to clear the valid bit if we find
Andrew Morton63f57932006-10-11 01:21:24 -07001191 * inconsistencies, to force a fsck at reboot. But for
1192 * a plain journaled filesystem we can keep it set as
1193 * valid forever! :)
1194 */
Mingming Cao617ba132006-10-11 01:20:53 -07001195 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001196#endif
1197 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001198 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1200 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001201 ext4_update_dynamic_rev(sb);
1202 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203
Mingming Cao617ba132006-10-11 01:20:53 -07001204 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001205 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -07001206 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001207 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1208 sb->s_blocksize,
1209 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001210 EXT4_BLOCKS_PER_GROUP(sb),
1211 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001212 sbi->s_mount_opt);
1213
Mingming Cao617ba132006-10-11 01:20:53 -07001214 printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1215 if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216 char b[BDEVNAME_SIZE];
1217
1218 printk("external journal on %s\n",
Mingming Cao617ba132006-10-11 01:20:53 -07001219 bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001220 } else {
1221 printk("internal journal\n");
1222 }
1223 return res;
1224}
1225
1226/* Called at mount-time, super-block is locked */
Mingming Cao617ba132006-10-11 01:20:53 -07001227static int ext4_check_descriptors (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001228{
Mingming Cao617ba132006-10-11 01:20:53 -07001229 struct ext4_sb_info *sbi = EXT4_SB(sb);
1230 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1231 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001232 ext4_fsblk_t block_bitmap;
1233 ext4_fsblk_t inode_bitmap;
1234 ext4_fsblk_t inode_table;
Mingming Cao617ba132006-10-11 01:20:53 -07001235 struct ext4_group_desc * gdp = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001236 int desc_block = 0;
1237 int i;
1238
Mingming Cao617ba132006-10-11 01:20:53 -07001239 ext4_debug ("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001240
1241 for (i = 0; i < sbi->s_groups_count; i++)
1242 {
1243 if (i == sbi->s_groups_count - 1)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001244 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001245 else
1246 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001247 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001248
Mingming Cao617ba132006-10-11 01:20:53 -07001249 if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1250 gdp = (struct ext4_group_desc *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001251 sbi->s_group_desc[desc_block++]->b_data;
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001252 block_bitmap = ext4_block_bitmap(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001253 if (block_bitmap < first_block || block_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254 {
Mingming Cao617ba132006-10-11 01:20:53 -07001255 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256 "Block bitmap for group %d"
Mingming Cao2ae02102006-10-11 01:21:11 -07001257 " not in group (block %llu)!",
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001258 i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001259 return 0;
1260 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001261 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001262 if (inode_bitmap < first_block || inode_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263 {
Mingming Cao617ba132006-10-11 01:20:53 -07001264 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001265 "Inode bitmap for group %d"
Mingming Cao2ae02102006-10-11 01:21:11 -07001266 " not in group (block %llu)!",
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001267 i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001268 return 0;
1269 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001270 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001271 if (inode_table < first_block ||
1272 inode_table + sbi->s_itb_per_group > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001273 {
Mingming Cao617ba132006-10-11 01:20:53 -07001274 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001275 "Inode table for group %d"
Mingming Cao2ae02102006-10-11 01:21:11 -07001276 " not in group (block %llu)!",
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001277 i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001278 return 0;
1279 }
Mingming Cao617ba132006-10-11 01:20:53 -07001280 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001281 gdp = (struct ext4_group_desc *)
1282 ((__u8 *)gdp + EXT4_DESC_SIZE(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283 }
1284
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001285 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07001286 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287 return 1;
1288}
1289
1290
Mingming Cao617ba132006-10-11 01:20:53 -07001291/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292 * the superblock) which were deleted from all directories, but held open by
1293 * a process at the time of a crash. We walk the list and try to delete these
1294 * inodes at recovery time (only with a read-write filesystem).
1295 *
1296 * In order to keep the orphan inode chain consistent during traversal (in
1297 * case of crash during recovery), we link each inode into the superblock
1298 * orphan list_head and handle it the same way as an inode deletion during
1299 * normal operation (which journals the operations for us).
1300 *
1301 * We only do an iget() and an iput() on each inode, which is very safe if we
1302 * accidentally point at an in-use or already deleted inode. The worst that
1303 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001304 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001305 * e2fsck was run on this filesystem, and it must have already done the orphan
1306 * inode cleanup for us, so we can safely abort without any further action.
1307 */
Mingming Cao617ba132006-10-11 01:20:53 -07001308static void ext4_orphan_cleanup (struct super_block * sb,
1309 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310{
1311 unsigned int s_flags = sb->s_flags;
1312 int nr_orphans = 0, nr_truncates = 0;
1313#ifdef CONFIG_QUOTA
1314 int i;
1315#endif
1316 if (!es->s_last_orphan) {
1317 jbd_debug(4, "no orphan inodes to clean up\n");
1318 return;
1319 }
1320
Eric Sandeena8f48a92006-12-06 20:40:13 -08001321 if (bdev_read_only(sb->s_bdev)) {
1322 printk(KERN_ERR "EXT4-fs: write access "
1323 "unavailable, skipping orphan cleanup.\n");
1324 return;
1325 }
1326
Mingming Cao617ba132006-10-11 01:20:53 -07001327 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001328 if (es->s_last_orphan)
1329 jbd_debug(1, "Errors on filesystem, "
1330 "clearing orphan list.\n");
1331 es->s_last_orphan = 0;
1332 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1333 return;
1334 }
1335
1336 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001337 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001338 sb->s_id);
1339 sb->s_flags &= ~MS_RDONLY;
1340 }
1341#ifdef CONFIG_QUOTA
1342 /* Needed for iput() to work correctly and not trash data */
1343 sb->s_flags |= MS_ACTIVE;
1344 /* Turn on quotas so that they are updated correctly */
1345 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001346 if (EXT4_SB(sb)->s_qf_names[i]) {
1347 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001348 if (ret < 0)
1349 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001350 "EXT4-fs: Cannot turn on journalled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 "quota: error %d\n", ret);
1352 }
1353 }
1354#endif
1355
1356 while (es->s_last_orphan) {
1357 struct inode *inode;
1358
1359 if (!(inode =
Mingming Cao617ba132006-10-11 01:20:53 -07001360 ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361 es->s_last_orphan = 0;
1362 break;
1363 }
1364
Mingming Cao617ba132006-10-11 01:20:53 -07001365 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001366 DQUOT_INIT(inode);
1367 if (inode->i_nlink) {
1368 printk(KERN_DEBUG
1369 "%s: truncating inode %lu to %Ld bytes\n",
1370 __FUNCTION__, inode->i_ino, inode->i_size);
1371 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1372 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001373 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374 nr_truncates++;
1375 } else {
1376 printk(KERN_DEBUG
1377 "%s: deleting unreferenced inode %lu\n",
1378 __FUNCTION__, inode->i_ino);
1379 jbd_debug(2, "deleting unreferenced inode %lu\n",
1380 inode->i_ino);
1381 nr_orphans++;
1382 }
1383 iput(inode); /* The delete magic happens here! */
1384 }
1385
1386#define PLURAL(x) (x), ((x)==1) ? "" : "s"
1387
1388 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001389 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001390 sb->s_id, PLURAL(nr_orphans));
1391 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001392 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001393 sb->s_id, PLURAL(nr_truncates));
1394#ifdef CONFIG_QUOTA
1395 /* Turn quotas off */
1396 for (i = 0; i < MAXQUOTAS; i++) {
1397 if (sb_dqopt(sb)->files[i])
1398 vfs_quota_off(sb, i);
1399 }
1400#endif
1401 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1402}
1403
1404#define log2(n) ffz(~(n))
1405
1406/*
1407 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1408 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1409 * We need to be 1 filesystem block less than the 2^32 sector limit.
1410 */
Mingming Cao617ba132006-10-11 01:20:53 -07001411static loff_t ext4_max_size(int bits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001412{
Mingming Cao617ba132006-10-11 01:20:53 -07001413 loff_t res = EXT4_NDIR_BLOCKS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001414 /* This constant is calculated to be the largest file size for a
1415 * dense, 4k-blocksize file such that the total number of
1416 * sectors in the file, including data and all indirect blocks,
1417 * does not exceed 2^32. */
1418 const loff_t upper_limit = 0x1ff7fffd000LL;
1419
1420 res += 1LL << (bits-2);
1421 res += 1LL << (2*(bits-2));
1422 res += 1LL << (3*(bits-2));
1423 res <<= bits;
1424 if (res > upper_limit)
1425 res = upper_limit;
1426 return res;
1427}
1428
Mingming Cao617ba132006-10-11 01:20:53 -07001429static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001430 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431{
Mingming Cao617ba132006-10-11 01:20:53 -07001432 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001433 unsigned long bg, first_meta_bg;
1434 int has_super = 0;
1435
1436 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1437
Mingming Cao617ba132006-10-11 01:20:53 -07001438 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001439 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001440 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001441 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001442 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001443 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001444 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001445}
1446
1447
Mingming Cao617ba132006-10-11 01:20:53 -07001448static int ext4_fill_super (struct super_block *sb, void *data, int silent)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449{
1450 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001451 struct ext4_super_block *es = NULL;
1452 struct ext4_sb_info *sbi;
1453 ext4_fsblk_t block;
1454 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001455 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456 unsigned long offset = 0;
1457 unsigned int journal_inum = 0;
1458 unsigned long journal_devnum = 0;
1459 unsigned long def_mount_opts;
1460 struct inode *root;
1461 int blocksize;
1462 int hblock;
1463 int db_count;
1464 int i;
1465 int needs_recovery;
1466 __le32 features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001467 __u64 blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468
1469 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1470 if (!sbi)
1471 return -ENOMEM;
1472 sb->s_fs_info = sbi;
1473 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001474 sbi->s_resuid = EXT4_DEF_RESUID;
1475 sbi->s_resgid = EXT4_DEF_RESGID;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476
1477 unlock_kernel();
1478
Mingming Cao617ba132006-10-11 01:20:53 -07001479 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07001481 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001482 goto out_fail;
1483 }
1484
1485 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001486 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001487 * block sizes. We need to calculate the offset from buffer start.
1488 */
Mingming Cao617ba132006-10-11 01:20:53 -07001489 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001490 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1491 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001493 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001494 }
1495
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001496 if (!(bh = sb_bread(sb, logical_sb_block))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001497 printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498 goto out_fail;
1499 }
1500 /*
1501 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07001502 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001503 */
Mingming Cao617ba132006-10-11 01:20:53 -07001504 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 sbi->s_es = es;
1506 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07001507 if (sb->s_magic != EXT4_SUPER_MAGIC)
1508 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001509
1510 /* Set defaults before we parse the mount options */
1511 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07001512 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001513 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07001514 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001515 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07001516 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001517 set_opt(sbi->s_mount_opt, NO_UID32);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001518#ifdef CONFIG_EXT4DEV_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07001519 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001520 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001521#endif
1522#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07001523 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001524 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001525#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001526 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1527 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1528 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1529 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1530 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1531 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001532
Mingming Cao617ba132006-10-11 01:20:53 -07001533 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001534 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Mingming Cao617ba132006-10-11 01:20:53 -07001535 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dmitry Mishinceea16b2006-10-11 01:21:21 -07001537 else
1538 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001539
1540 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1541 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1542
1543 set_opt(sbi->s_mount_opt, RESERVATION);
1544
1545 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1546 NULL, 0))
1547 goto failed_mount;
1548
1549 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07001550 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551
Mingming Cao617ba132006-10-11 01:20:53 -07001552 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1553 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1554 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1555 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001556 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001557 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001558 "running e2fsck is recommended\n");
1559 /*
1560 * Check feature flags regardless of the revision level, since we
1561 * previously didn't change the revision level when setting the flags,
1562 * so there is a chance incompat flags are set on a rev 0 filesystem.
1563 */
Mingming Cao617ba132006-10-11 01:20:53 -07001564 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001565 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001566 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001567 "unsupported optional features (%x).\n",
1568 sb->s_id, le32_to_cpu(features));
1569 goto failed_mount;
1570 }
Mingming Cao617ba132006-10-11 01:20:53 -07001571 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001572 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001573 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001574 "unsupported optional features (%x).\n",
1575 sb->s_id, le32_to_cpu(features));
1576 goto failed_mount;
1577 }
1578 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1579
Mingming Cao617ba132006-10-11 01:20:53 -07001580 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1581 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001582 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001583 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584 blocksize, sb->s_id);
1585 goto failed_mount;
1586 }
1587
1588 hblock = bdev_hardsect_size(sb->s_bdev);
1589 if (sb->s_blocksize != blocksize) {
1590 /*
1591 * Make sure the blocksize for the filesystem is larger
1592 * than the hardware sectorsize for the machine.
1593 */
1594 if (blocksize < hblock) {
Mingming Cao617ba132006-10-11 01:20:53 -07001595 printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001596 "device blocksize %d.\n", blocksize, hblock);
1597 goto failed_mount;
1598 }
1599
1600 brelse (bh);
1601 sb_set_blocksize(sb, blocksize);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001602 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1603 offset = do_div(logical_sb_block, blocksize);
1604 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605 if (!bh) {
1606 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001607 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001608 goto failed_mount;
1609 }
Mingming Cao617ba132006-10-11 01:20:53 -07001610 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07001612 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001614 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615 goto failed_mount;
1616 }
1617 }
1618
Mingming Cao617ba132006-10-11 01:20:53 -07001619 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001620
Mingming Cao617ba132006-10-11 01:20:53 -07001621 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
1622 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
1623 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001624 } else {
1625 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1626 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07001627 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001628 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1629 (sbi->s_inode_size > blocksize)) {
1630 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001631 "EXT4-fs: unsupported inode size: %d\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 sbi->s_inode_size);
1633 goto failed_mount;
1634 }
1635 }
Mingming Cao617ba132006-10-11 01:20:53 -07001636 sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001637 le32_to_cpu(es->s_log_frag_size);
1638 if (blocksize != sbi->s_frag_size) {
1639 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001640 "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641 sbi->s_frag_size, blocksize);
1642 goto failed_mount;
1643 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001644 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
1645 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001646 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001647 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
1648 sbi->s_desc_size & (sbi->s_desc_size - 1)) {
1649 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001650 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001651 sbi->s_desc_size);
1652 goto failed_mount;
1653 }
1654 } else
1655 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1657 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1658 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Mingming Cao617ba132006-10-11 01:20:53 -07001659 if (EXT4_INODE_SIZE(sb) == 0)
1660 goto cantfind_ext4;
1661 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001662 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07001663 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001664 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1665 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07001666 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001667 sbi->s_sbh = bh;
1668 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07001669 sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
1670 sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001671 for (i=0; i < 4; i++)
1672 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1673 sbi->s_def_hash_version = es->s_def_hash_version;
1674
1675 if (sbi->s_blocks_per_group > blocksize * 8) {
1676 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001677 "EXT4-fs: #blocks per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001678 sbi->s_blocks_per_group);
1679 goto failed_mount;
1680 }
1681 if (sbi->s_frags_per_group > blocksize * 8) {
1682 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001683 "EXT4-fs: #fragments per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001684 sbi->s_frags_per_group);
1685 goto failed_mount;
1686 }
1687 if (sbi->s_inodes_per_group > blocksize * 8) {
1688 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001689 "EXT4-fs: #inodes per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001690 sbi->s_inodes_per_group);
1691 goto failed_mount;
1692 }
1693
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001694 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001695 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001696 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001697 " too large to mount safely\n", sb->s_id);
1698 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07001699 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700 "enabled\n");
1701 goto failed_mount;
1702 }
1703
Mingming Cao617ba132006-10-11 01:20:53 -07001704 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
1705 goto cantfind_ext4;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001706 blocks_count = (ext4_blocks_count(es) -
1707 le32_to_cpu(es->s_first_data_block) +
1708 EXT4_BLOCKS_PER_GROUP(sb) - 1);
1709 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
1710 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07001711 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
1712 EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1714 GFP_KERNEL);
1715 if (sbi->s_group_desc == NULL) {
Mingming Cao617ba132006-10-11 01:20:53 -07001716 printk (KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717 goto failed_mount;
1718 }
1719
1720 bgl_lock_init(&sbi->s_blockgroup_lock);
1721
1722 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001723 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001724 sbi->s_group_desc[i] = sb_bread(sb, block);
1725 if (!sbi->s_group_desc[i]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001726 printk (KERN_ERR "EXT4-fs: "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727 "can't read group descriptor %d\n", i);
1728 db_count = i;
1729 goto failed_mount2;
1730 }
1731 }
Mingming Cao617ba132006-10-11 01:20:53 -07001732 if (!ext4_check_descriptors (sb)) {
1733 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001734 goto failed_mount2;
1735 }
1736 sbi->s_gdb_count = db_count;
1737 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1738 spin_lock_init(&sbi->s_next_gen_lock);
1739
1740 percpu_counter_init(&sbi->s_freeblocks_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001741 ext4_count_free_blocks(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001742 percpu_counter_init(&sbi->s_freeinodes_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001743 ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744 percpu_counter_init(&sbi->s_dirs_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001745 ext4_count_dirs(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001746
1747 /* per fileystem reservation list head & lock */
1748 spin_lock_init(&sbi->s_rsv_window_lock);
1749 sbi->s_rsv_window_root = RB_ROOT;
1750 /* Add a single, static dummy reservation to the start of the
1751 * reservation window list --- it gives us a placeholder for
1752 * append-at-start-of-list which makes the allocation logic
1753 * _much_ simpler. */
Mingming Cao617ba132006-10-11 01:20:53 -07001754 sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1755 sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001756 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1757 sbi->s_rsv_window_head.rsv_goal_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001758 ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001759
1760 /*
1761 * set up enough so that it can read an inode
1762 */
Mingming Cao617ba132006-10-11 01:20:53 -07001763 sb->s_op = &ext4_sops;
1764 sb->s_export_op = &ext4_export_ops;
1765 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001766#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001767 sb->s_qcop = &ext4_qctl_operations;
1768 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769#endif
1770 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1771
1772 sb->s_root = NULL;
1773
1774 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07001775 EXT4_HAS_INCOMPAT_FEATURE(sb,
1776 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001777
1778 /*
1779 * The first inode we look at is the journal inode. Don't try
1780 * root first: it may be modified in the journal!
1781 */
1782 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07001783 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
1784 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001785 goto failed_mount3;
1786 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07001787 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001788 goto failed_mount3;
1789 } else {
1790 if (!silent)
1791 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001792 "ext4: No journal on filesystem on %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793 sb->s_id);
1794 goto failed_mount3;
1795 }
1796
1797 /* We have now updated the journal if required, so we can
1798 * validate the data journaling mode. */
1799 switch (test_opt(sb, DATA_FLAGS)) {
1800 case 0:
1801 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07001802 * capabilities: ORDERED_DATA if the journal can
1803 * cope, else JOURNAL_DATA
1804 */
Mingming Caodab291a2006-10-11 01:21:01 -07001805 if (jbd2_journal_check_available_features
1806 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001807 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1808 else
1809 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1810 break;
1811
Mingming Cao617ba132006-10-11 01:20:53 -07001812 case EXT4_MOUNT_ORDERED_DATA:
1813 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07001814 if (!jbd2_journal_check_available_features
1815 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001816 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001817 "requested data journaling mode\n");
1818 goto failed_mount4;
1819 }
1820 default:
1821 break;
1822 }
1823
1824 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001825 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
1826 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001827 "its supported only with writeback mode\n");
1828 clear_opt(sbi->s_mount_opt, NOBH);
1829 }
1830 }
1831 /*
Mingming Caodab291a2006-10-11 01:21:01 -07001832 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001833 * so we can safely mount the rest of the filesystem now.
1834 */
1835
Mingming Cao617ba132006-10-11 01:20:53 -07001836 root = iget(sb, EXT4_ROOT_INO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001837 sb->s_root = d_alloc_root(root);
1838 if (!sb->s_root) {
Mingming Cao617ba132006-10-11 01:20:53 -07001839 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840 iput(root);
1841 goto failed_mount4;
1842 }
1843 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1844 dput(sb->s_root);
1845 sb->s_root = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001846 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001847 goto failed_mount4;
1848 }
1849
Mingming Cao617ba132006-10-11 01:20:53 -07001850 ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001851 /*
1852 * akpm: core read_super() calls in here with the superblock locked.
1853 * That deadlocks, because orphan cleanup needs to lock the superblock
1854 * in numerous places. Here we just pop the lock - it's relatively
1855 * harmless, because we are now ready to accept write_super() requests,
1856 * and aviro says that's the only reason for hanging onto the
1857 * superblock lock.
1858 */
Mingming Cao617ba132006-10-11 01:20:53 -07001859 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
1860 ext4_orphan_cleanup(sb, es);
1861 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862 if (needs_recovery)
Mingming Cao617ba132006-10-11 01:20:53 -07001863 printk (KERN_INFO "EXT4-fs: recovery complete.\n");
1864 ext4_mark_recovery_complete(sb, es);
1865 printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
1866 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
1867 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001868 "writeback");
1869
Alex Tomasa86c6182006-10-11 01:21:03 -07001870 ext4_ext_init(sb);
1871
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001872 lock_kernel();
1873 return 0;
1874
Mingming Cao617ba132006-10-11 01:20:53 -07001875cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07001877 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001878 sb->s_id);
1879 goto failed_mount;
1880
1881failed_mount4:
Mingming Caodab291a2006-10-11 01:21:01 -07001882 jbd2_journal_destroy(sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001883failed_mount3:
1884 percpu_counter_destroy(&sbi->s_freeblocks_counter);
1885 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1886 percpu_counter_destroy(&sbi->s_dirs_counter);
1887failed_mount2:
1888 for (i = 0; i < db_count; i++)
1889 brelse(sbi->s_group_desc[i]);
1890 kfree(sbi->s_group_desc);
1891failed_mount:
1892#ifdef CONFIG_QUOTA
1893 for (i = 0; i < MAXQUOTAS; i++)
1894 kfree(sbi->s_qf_names[i]);
1895#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001896 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001897 brelse(bh);
1898out_fail:
1899 sb->s_fs_info = NULL;
1900 kfree(sbi);
1901 lock_kernel();
1902 return -EINVAL;
1903}
1904
1905/*
1906 * Setup any per-fs journal parameters now. We'll do this both on
1907 * initial mount, once the journal has been initialised but before we've
1908 * done any recovery; and again on any subsequent remount.
1909 */
Mingming Cao617ba132006-10-11 01:20:53 -07001910static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001911{
Mingming Cao617ba132006-10-11 01:20:53 -07001912 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001913
1914 if (sbi->s_commit_interval)
1915 journal->j_commit_interval = sbi->s_commit_interval;
Mingming Cao617ba132006-10-11 01:20:53 -07001916 /* We could also set up an ext4-specific default for the commit
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001917 * interval here, but for now we'll just fall back to the jbd
1918 * default. */
1919
1920 spin_lock(&journal->j_state_lock);
1921 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07001922 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001923 else
Mingming Caodab291a2006-10-11 01:21:01 -07001924 journal->j_flags &= ~JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001925 spin_unlock(&journal->j_state_lock);
1926}
1927
Mingming Cao617ba132006-10-11 01:20:53 -07001928static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001929 unsigned int journal_inum)
1930{
1931 struct inode *journal_inode;
1932 journal_t *journal;
1933
1934 /* First, test for the existence of a valid inode on disk. Bad
1935 * things happen if we iget() an unused inode, as the subsequent
1936 * iput() will try to delete it. */
1937
1938 journal_inode = iget(sb, journal_inum);
1939 if (!journal_inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001940 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001941 return NULL;
1942 }
1943 if (!journal_inode->i_nlink) {
1944 make_bad_inode(journal_inode);
1945 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07001946 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001947 return NULL;
1948 }
1949
1950 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1951 journal_inode, journal_inode->i_size);
1952 if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001953 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001954 iput(journal_inode);
1955 return NULL;
1956 }
1957
Mingming Caodab291a2006-10-11 01:21:01 -07001958 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001959 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07001960 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001961 iput(journal_inode);
1962 return NULL;
1963 }
1964 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001965 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001966 return journal;
1967}
1968
Mingming Cao617ba132006-10-11 01:20:53 -07001969static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001970 dev_t j_dev)
1971{
1972 struct buffer_head * bh;
1973 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07001974 ext4_fsblk_t start;
1975 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001976 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07001977 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001978 unsigned long offset;
Mingming Cao617ba132006-10-11 01:20:53 -07001979 struct ext4_super_block * es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001980 struct block_device *bdev;
1981
Mingming Cao617ba132006-10-11 01:20:53 -07001982 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001983 if (bdev == NULL)
1984 return NULL;
1985
1986 if (bd_claim(bdev, sb)) {
1987 printk(KERN_ERR
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001988 "EXT4: failed to claim external journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 blkdev_put(bdev);
1990 return NULL;
1991 }
1992
1993 blocksize = sb->s_blocksize;
1994 hblock = bdev_hardsect_size(bdev);
1995 if (blocksize < hblock) {
1996 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001997 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001998 goto out_bdev;
1999 }
2000
Mingming Cao617ba132006-10-11 01:20:53 -07002001 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2002 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002003 set_blocksize(bdev, blocksize);
2004 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002005 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002006 "external journal\n");
2007 goto out_bdev;
2008 }
2009
Mingming Cao617ba132006-10-11 01:20:53 -07002010 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2011 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002012 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002013 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2014 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002015 "bad superblock\n");
2016 brelse(bh);
2017 goto out_bdev;
2018 }
2019
Mingming Cao617ba132006-10-11 01:20:53 -07002020 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2021 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002022 brelse(bh);
2023 goto out_bdev;
2024 }
2025
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002026 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002027 start = sb_block + 1;
2028 brelse(bh); /* we're done with the superblock */
2029
Mingming Caodab291a2006-10-11 01:21:01 -07002030 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002031 start, len, blocksize);
2032 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002033 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 goto out_bdev;
2035 }
2036 journal->j_private = sb;
2037 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2038 wait_on_buffer(journal->j_sb_buffer);
2039 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002040 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002041 goto out_journal;
2042 }
2043 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002044 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002045 "user (unsupported) - %d\n",
2046 be32_to_cpu(journal->j_superblock->s_nr_users));
2047 goto out_journal;
2048 }
Mingming Cao617ba132006-10-11 01:20:53 -07002049 EXT4_SB(sb)->journal_bdev = bdev;
2050 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002051 return journal;
2052out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002053 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002054out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002055 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002056 return NULL;
2057}
2058
Mingming Cao617ba132006-10-11 01:20:53 -07002059static int ext4_load_journal(struct super_block *sb,
2060 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002061 unsigned long journal_devnum)
2062{
2063 journal_t *journal;
2064 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2065 dev_t journal_dev;
2066 int err = 0;
2067 int really_read_only;
2068
2069 if (journal_devnum &&
2070 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002071 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002072 "numbers have changed\n");
2073 journal_dev = new_decode_dev(journal_devnum);
2074 } else
2075 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2076
2077 really_read_only = bdev_read_only(sb->s_bdev);
2078
2079 /*
2080 * Are we loading a blank journal or performing recovery after a
2081 * crash? For recovery, we need to check in advance whether we
2082 * can get read-write access to the device.
2083 */
2084
Mingming Cao617ba132006-10-11 01:20:53 -07002085 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002086 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002087 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002088 "required on readonly filesystem.\n");
2089 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002090 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002091 "unavailable, cannot proceed.\n");
2092 return -EROFS;
2093 }
Mingming Cao617ba132006-10-11 01:20:53 -07002094 printk (KERN_INFO "EXT4-fs: write access will "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002095 "be enabled during recovery.\n");
2096 }
2097 }
2098
2099 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002100 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002101 "and inode journals!\n");
2102 return -EINVAL;
2103 }
2104
2105 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002106 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002107 return -EINVAL;
2108 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002109 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 return -EINVAL;
2111 }
2112
2113 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002114 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002115 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002116 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002117 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002118 return err;
2119 }
2120 }
2121
Mingming Cao617ba132006-10-11 01:20:53 -07002122 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002123 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002124 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002125 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002126
2127 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002128 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002129 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130 return err;
2131 }
2132
Mingming Cao617ba132006-10-11 01:20:53 -07002133 EXT4_SB(sb)->s_journal = journal;
2134 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135
2136 if (journal_devnum &&
2137 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2138 es->s_journal_dev = cpu_to_le32(journal_devnum);
2139 sb->s_dirt = 1;
2140
2141 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002142 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002143 }
2144
2145 return 0;
2146}
2147
Mingming Cao617ba132006-10-11 01:20:53 -07002148static int ext4_create_journal(struct super_block * sb,
2149 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002150 unsigned int journal_inum)
2151{
2152 journal_t *journal;
2153
2154 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002155 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002156 "create journal.\n");
2157 return -EROFS;
2158 }
2159
Mingming Cao617ba132006-10-11 01:20:53 -07002160 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002161 return -EINVAL;
2162
Mingming Cao617ba132006-10-11 01:20:53 -07002163 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002164 journal_inum);
2165
Mingming Caodab291a2006-10-11 01:21:01 -07002166 if (jbd2_journal_create(journal)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002167 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002168 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169 return -EIO;
2170 }
2171
Mingming Cao617ba132006-10-11 01:20:53 -07002172 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002173
Mingming Cao617ba132006-10-11 01:20:53 -07002174 ext4_update_dynamic_rev(sb);
2175 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2176 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002177
2178 es->s_journal_inum = cpu_to_le32(journal_inum);
2179 sb->s_dirt = 1;
2180
2181 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002182 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002183
2184 return 0;
2185}
2186
Mingming Cao617ba132006-10-11 01:20:53 -07002187static void ext4_commit_super (struct super_block * sb,
2188 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002189 int sync)
2190{
Mingming Cao617ba132006-10-11 01:20:53 -07002191 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002192
2193 if (!sbh)
2194 return;
2195 es->s_wtime = cpu_to_le32(get_seconds());
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002196 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07002197 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002198 BUFFER_TRACE(sbh, "marking dirty");
2199 mark_buffer_dirty(sbh);
2200 if (sync)
2201 sync_dirty_buffer(sbh);
2202}
2203
2204
2205/*
2206 * Have we just finished recovery? If so, and if we are mounting (or
2207 * remounting) the filesystem readonly, then we will end up with a
2208 * consistent fs on disk. Record that fact.
2209 */
Mingming Cao617ba132006-10-11 01:20:53 -07002210static void ext4_mark_recovery_complete(struct super_block * sb,
2211 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002212{
Mingming Cao617ba132006-10-11 01:20:53 -07002213 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002214
Mingming Caodab291a2006-10-11 01:21:01 -07002215 jbd2_journal_lock_updates(journal);
2216 jbd2_journal_flush(journal);
Mingming Cao617ba132006-10-11 01:20:53 -07002217 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002218 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002219 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002220 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002221 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002222 }
Mingming Caodab291a2006-10-11 01:21:01 -07002223 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224}
2225
2226/*
2227 * If we are mounting (or read-write remounting) a filesystem whose journal
2228 * has recorded an error from a previous lifetime, move that error to the
2229 * main filesystem now.
2230 */
Mingming Cao617ba132006-10-11 01:20:53 -07002231static void ext4_clear_journal_err(struct super_block * sb,
2232 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002233{
2234 journal_t *journal;
2235 int j_errno;
2236 const char *errstr;
2237
Mingming Cao617ba132006-10-11 01:20:53 -07002238 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002239
2240 /*
2241 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07002242 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002243 */
2244
Mingming Caodab291a2006-10-11 01:21:01 -07002245 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002246 if (j_errno) {
2247 char nbuf[16];
2248
Mingming Cao617ba132006-10-11 01:20:53 -07002249 errstr = ext4_decode_error(sb, j_errno, nbuf);
2250 ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002251 "from previous mount: %s", errstr);
Mingming Cao617ba132006-10-11 01:20:53 -07002252 ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002253 "filesystem check.");
2254
Mingming Cao617ba132006-10-11 01:20:53 -07002255 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2256 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2257 ext4_commit_super (sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002258
Mingming Caodab291a2006-10-11 01:21:01 -07002259 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260 }
2261}
2262
2263/*
2264 * Force the running and committing transactions to commit,
2265 * and wait on the commit.
2266 */
Mingming Cao617ba132006-10-11 01:20:53 -07002267int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002268{
2269 journal_t *journal;
2270 int ret;
2271
2272 if (sb->s_flags & MS_RDONLY)
2273 return 0;
2274
Mingming Cao617ba132006-10-11 01:20:53 -07002275 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002276 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002277 ret = ext4_journal_force_commit(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002278 return ret;
2279}
2280
2281/*
Mingming Cao617ba132006-10-11 01:20:53 -07002282 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002283 * have to propagate any other updates to the superblock on disk at this
2284 * point. Just start an async writeback to get the buffers on their way
2285 * to the disk.
2286 *
2287 * This implicitly triggers the writebehind on sync().
2288 */
2289
Mingming Cao617ba132006-10-11 01:20:53 -07002290static void ext4_write_super (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002291{
2292 if (mutex_trylock(&sb->s_lock) != 0)
2293 BUG();
2294 sb->s_dirt = 0;
2295}
2296
Mingming Cao617ba132006-10-11 01:20:53 -07002297static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002298{
2299 tid_t target;
2300
2301 sb->s_dirt = 0;
Mingming Caodab291a2006-10-11 01:21:01 -07002302 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002303 if (wait)
Mingming Caodab291a2006-10-11 01:21:01 -07002304 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002305 }
2306 return 0;
2307}
2308
2309/*
2310 * LVM calls this function before a (read-only) snapshot is created. This
2311 * gives us a chance to flush the journal completely and mark the fs clean.
2312 */
Mingming Cao617ba132006-10-11 01:20:53 -07002313static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002314{
2315 sb->s_dirt = 0;
2316
2317 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002318 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002319
2320 /* Now we set up the journal barrier. */
Mingming Caodab291a2006-10-11 01:21:01 -07002321 jbd2_journal_lock_updates(journal);
2322 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002323
2324 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07002325 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2326 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002327 }
2328}
2329
2330/*
2331 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2332 * flag here, even though the filesystem is not technically dirty yet.
2333 */
Mingming Cao617ba132006-10-11 01:20:53 -07002334static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002335{
2336 if (!(sb->s_flags & MS_RDONLY)) {
2337 lock_super(sb);
2338 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07002339 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2340 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002341 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002342 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002343 }
2344}
2345
Mingming Cao617ba132006-10-11 01:20:53 -07002346static int ext4_remount (struct super_block * sb, int * flags, char * data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002347{
Mingming Cao617ba132006-10-11 01:20:53 -07002348 struct ext4_super_block * es;
2349 struct ext4_sb_info *sbi = EXT4_SB(sb);
2350 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002351 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07002352 struct ext4_mount_options old_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002353 int err;
2354#ifdef CONFIG_QUOTA
2355 int i;
2356#endif
2357
2358 /* Store the original options */
2359 old_sb_flags = sb->s_flags;
2360 old_opts.s_mount_opt = sbi->s_mount_opt;
2361 old_opts.s_resuid = sbi->s_resuid;
2362 old_opts.s_resgid = sbi->s_resgid;
2363 old_opts.s_commit_interval = sbi->s_commit_interval;
2364#ifdef CONFIG_QUOTA
2365 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2366 for (i = 0; i < MAXQUOTAS; i++)
2367 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2368#endif
2369
2370 /*
2371 * Allow the "check" option to be passed as a remount option.
2372 */
2373 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2374 err = -EINVAL;
2375 goto restore_opts;
2376 }
2377
Mingming Cao617ba132006-10-11 01:20:53 -07002378 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2379 ext4_abort(sb, __FUNCTION__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002380
2381 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002382 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383
2384 es = sbi->s_es;
2385
Mingming Cao617ba132006-10-11 01:20:53 -07002386 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002387
2388 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002389 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002390 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002391 err = -EROFS;
2392 goto restore_opts;
2393 }
2394
2395 if (*flags & MS_RDONLY) {
2396 /*
2397 * First of all, the unconditional stuff we have to do
2398 * to disable replay of the journal when we next remount
2399 */
2400 sb->s_flags |= MS_RDONLY;
2401
2402 /*
2403 * OK, test if we are remounting a valid rw partition
2404 * readonly, and if so set the rdonly flag and then
2405 * mark the partition as valid again.
2406 */
Mingming Cao617ba132006-10-11 01:20:53 -07002407 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2408 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002409 es->s_state = cpu_to_le16(sbi->s_mount_state);
2410
Mingming Cao617ba132006-10-11 01:20:53 -07002411 ext4_mark_recovery_complete(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002412 } else {
2413 __le32 ret;
Mingming Cao617ba132006-10-11 01:20:53 -07002414 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2415 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2416 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002417 "remount RDWR because of unsupported "
2418 "optional features (%x).\n",
2419 sb->s_id, le32_to_cpu(ret));
2420 err = -EROFS;
2421 goto restore_opts;
2422 }
Eric Sandeenead65962007-02-10 01:46:08 -08002423
2424 /*
2425 * If we have an unprocessed orphan list hanging
2426 * around from a previously readonly bdev mount,
2427 * require a full umount/remount for now.
2428 */
2429 if (es->s_last_orphan) {
2430 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2431 "remount RDWR because of unprocessed "
2432 "orphan inode list. Please "
2433 "umount/remount instead.\n",
2434 sb->s_id);
2435 err = -EINVAL;
2436 goto restore_opts;
2437 }
2438
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439 /*
2440 * Mounting a RDONLY partition read-write, so reread
2441 * and store the current valid flag. (It may have
2442 * been changed by e2fsck since we originally mounted
2443 * the partition.)
2444 */
Mingming Cao617ba132006-10-11 01:20:53 -07002445 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002446 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07002447 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002448 goto restore_opts;
Mingming Cao617ba132006-10-11 01:20:53 -07002449 if (!ext4_setup_super (sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002450 sb->s_flags &= ~MS_RDONLY;
2451 }
2452 }
2453#ifdef CONFIG_QUOTA
2454 /* Release old quota file names */
2455 for (i = 0; i < MAXQUOTAS; i++)
2456 if (old_opts.s_qf_names[i] &&
2457 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2458 kfree(old_opts.s_qf_names[i]);
2459#endif
2460 return 0;
2461restore_opts:
2462 sb->s_flags = old_sb_flags;
2463 sbi->s_mount_opt = old_opts.s_mount_opt;
2464 sbi->s_resuid = old_opts.s_resuid;
2465 sbi->s_resgid = old_opts.s_resgid;
2466 sbi->s_commit_interval = old_opts.s_commit_interval;
2467#ifdef CONFIG_QUOTA
2468 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2469 for (i = 0; i < MAXQUOTAS; i++) {
2470 if (sbi->s_qf_names[i] &&
2471 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2472 kfree(sbi->s_qf_names[i]);
2473 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2474 }
2475#endif
2476 return err;
2477}
2478
Mingming Cao617ba132006-10-11 01:20:53 -07002479static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480{
2481 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002482 struct ext4_sb_info *sbi = EXT4_SB(sb);
2483 struct ext4_super_block *es = sbi->s_es;
2484 ext4_fsblk_t overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002485 int i;
Pekka Enberg960cc392006-12-06 20:35:29 -08002486 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002487
2488 if (test_opt (sb, MINIX_DF))
2489 overhead = 0;
2490 else {
2491 unsigned long ngroups;
Mingming Cao617ba132006-10-11 01:20:53 -07002492 ngroups = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002493 smp_rmb();
2494
2495 /*
2496 * Compute the overhead (FS structures)
2497 */
2498
2499 /*
2500 * All of the blocks before first_data_block are
2501 * overhead
2502 */
2503 overhead = le32_to_cpu(es->s_first_data_block);
2504
2505 /*
2506 * Add the overhead attributed to the superblock and
2507 * block group descriptors. If the sparse superblocks
2508 * feature is turned on, then not all groups have this.
2509 */
2510 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07002511 overhead += ext4_bg_has_super(sb, i) +
2512 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002513 cond_resched();
2514 }
2515
2516 /*
2517 * Every block group has an inode bitmap, a block
2518 * bitmap, and an inode table.
2519 */
Mingming Cao617ba132006-10-11 01:20:53 -07002520 overhead += (ngroups * (2 + EXT4_SB(sb)->s_itb_per_group));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002521 }
2522
Mingming Cao617ba132006-10-11 01:20:53 -07002523 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002524 buf->f_bsize = sb->s_blocksize;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002525 buf->f_blocks = ext4_blocks_count(es) - overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002526 buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002527 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2528 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002529 buf->f_bavail = 0;
2530 buf->f_files = le32_to_cpu(es->s_inodes_count);
2531 buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
Mingming Cao617ba132006-10-11 01:20:53 -07002532 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08002533 fsid = le64_to_cpup((void *)es->s_uuid) ^
2534 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2535 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2536 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002537 return 0;
2538}
2539
2540/* Helper function for writing quotas on sync - we need to start transaction before quota file
2541 * is locked for write. Otherwise the are possible deadlocks:
2542 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07002543 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07002544 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002545 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07002546 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002547 *
2548 */
2549
2550#ifdef CONFIG_QUOTA
2551
2552static inline struct inode *dquot_to_inode(struct dquot *dquot)
2553{
2554 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2555}
2556
Mingming Cao617ba132006-10-11 01:20:53 -07002557static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002558{
2559 handle_t *handle;
2560 int ret, err;
2561
2562 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07002563 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002564 if (IS_ERR(handle))
2565 return PTR_ERR(handle);
2566 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07002567 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002568 if (!ret)
2569 ret = err;
2570 return ret;
2571}
2572
Mingming Cao617ba132006-10-11 01:20:53 -07002573static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002574{
2575 handle_t *handle;
2576 int ret, err;
2577
2578 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07002579 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002580 if (IS_ERR(handle))
2581 return PTR_ERR(handle);
2582 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002583 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002584 if (!ret)
2585 ret = err;
2586 return ret;
2587}
2588
Mingming Cao617ba132006-10-11 01:20:53 -07002589static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002590{
2591 int ret, err;
2592 handle_t *handle;
2593 struct inode *inode;
2594
2595 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002596 handle = ext4_journal_start(inode,
2597 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002598 if (IS_ERR(handle))
2599 return PTR_ERR(handle);
2600 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002601 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002602 if (!ret)
2603 ret = err;
2604 return ret;
2605}
2606
Mingming Cao617ba132006-10-11 01:20:53 -07002607static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002608{
2609 int ret, err;
2610 handle_t *handle;
2611
Mingming Cao617ba132006-10-11 01:20:53 -07002612 handle = ext4_journal_start(dquot_to_inode(dquot),
2613 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002614 if (IS_ERR(handle))
2615 return PTR_ERR(handle);
2616 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002617 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002618 if (!ret)
2619 ret = err;
2620 return ret;
2621}
2622
Mingming Cao617ba132006-10-11 01:20:53 -07002623static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002624{
2625 int ret, err;
2626 handle_t *handle;
2627
Mingming Cao617ba132006-10-11 01:20:53 -07002628 handle = ext4_journal_start(dquot_to_inode(dquot),
2629 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002630 if (IS_ERR(handle))
2631 return PTR_ERR(handle);
2632 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002633 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002634 if (!ret)
2635 ret = err;
2636 return ret;
2637}
2638
Mingming Cao617ba132006-10-11 01:20:53 -07002639static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002640{
2641 /* Are we journalling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07002642 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2643 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002644 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002645 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002646 } else {
2647 return dquot_mark_dquot_dirty(dquot);
2648 }
2649}
2650
Mingming Cao617ba132006-10-11 01:20:53 -07002651static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002652{
2653 int ret, err;
2654 handle_t *handle;
2655
2656 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07002657 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002658 if (IS_ERR(handle))
2659 return PTR_ERR(handle);
2660 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07002661 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002662 if (!ret)
2663 ret = err;
2664 return ret;
2665}
2666
2667/*
2668 * Turn on quotas during mount time - we need to find
2669 * the quota file and such...
2670 */
Mingming Cao617ba132006-10-11 01:20:53 -07002671static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002672{
Mingming Cao617ba132006-10-11 01:20:53 -07002673 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
2674 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002675}
2676
2677/*
2678 * Standard function to be called on quota_on
2679 */
Mingming Cao617ba132006-10-11 01:20:53 -07002680static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002681 char *path)
2682{
2683 int err;
2684 struct nameidata nd;
2685
2686 if (!test_opt(sb, QUOTA))
2687 return -EINVAL;
2688 /* Not journalling quota? */
Mingming Cao617ba132006-10-11 01:20:53 -07002689 if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
2690 !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002691 return vfs_quota_on(sb, type, format_id, path);
2692 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2693 if (err)
2694 return err;
2695 /* Quotafile not on the same filesystem? */
2696 if (nd.mnt->mnt_sb != sb) {
2697 path_release(&nd);
2698 return -EXDEV;
2699 }
2700 /* Quotafile not of fs root? */
2701 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2702 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002703 "EXT4-fs: Quota file not on filesystem root. "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002704 "Journalled quota will not work.\n");
2705 path_release(&nd);
2706 return vfs_quota_on(sb, type, format_id, path);
2707}
2708
2709/* Read data from quotafile - avoid pagecache and such because we cannot afford
2710 * acquiring the locks... As quota files are never truncated and quota code
2711 * itself serializes the operations (and noone else should touch the files)
2712 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07002713static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002714 size_t len, loff_t off)
2715{
2716 struct inode *inode = sb_dqopt(sb)->files[type];
Mingming Cao617ba132006-10-11 01:20:53 -07002717 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002718 int err = 0;
2719 int offset = off & (sb->s_blocksize - 1);
2720 int tocopy;
2721 size_t toread;
2722 struct buffer_head *bh;
2723 loff_t i_size = i_size_read(inode);
2724
2725 if (off > i_size)
2726 return 0;
2727 if (off+len > i_size)
2728 len = i_size-off;
2729 toread = len;
2730 while (toread > 0) {
2731 tocopy = sb->s_blocksize - offset < toread ?
2732 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07002733 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002734 if (err)
2735 return err;
2736 if (!bh) /* A hole? */
2737 memset(data, 0, tocopy);
2738 else
2739 memcpy(data, bh->b_data+offset, tocopy);
2740 brelse(bh);
2741 offset = 0;
2742 toread -= tocopy;
2743 data += tocopy;
2744 blk++;
2745 }
2746 return len;
2747}
2748
2749/* Write to quotafile (we know the transaction is already started and has
2750 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07002751static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002752 const char *data, size_t len, loff_t off)
2753{
2754 struct inode *inode = sb_dqopt(sb)->files[type];
Mingming Cao617ba132006-10-11 01:20:53 -07002755 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002756 int err = 0;
2757 int offset = off & (sb->s_blocksize - 1);
2758 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07002759 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002760 size_t towrite = len;
2761 struct buffer_head *bh;
2762 handle_t *handle = journal_current_handle();
2763
2764 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2765 while (towrite > 0) {
2766 tocopy = sb->s_blocksize - offset < towrite ?
2767 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07002768 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002769 if (!bh)
2770 goto out;
2771 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07002772 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002773 if (err) {
2774 brelse(bh);
2775 goto out;
2776 }
2777 }
2778 lock_buffer(bh);
2779 memcpy(bh->b_data+offset, data, tocopy);
2780 flush_dcache_page(bh->b_page);
2781 unlock_buffer(bh);
2782 if (journal_quota)
Mingming Cao617ba132006-10-11 01:20:53 -07002783 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002784 else {
2785 /* Always do at least ordered writes for quotas */
Mingming Cao617ba132006-10-11 01:20:53 -07002786 err = ext4_journal_dirty_data(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002787 mark_buffer_dirty(bh);
2788 }
2789 brelse(bh);
2790 if (err)
2791 goto out;
2792 offset = 0;
2793 towrite -= tocopy;
2794 data += tocopy;
2795 blk++;
2796 }
2797out:
2798 if (len == towrite)
2799 return err;
2800 if (inode->i_size < off+len-towrite) {
2801 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07002802 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002803 }
2804 inode->i_version++;
2805 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07002806 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002807 mutex_unlock(&inode->i_mutex);
2808 return len - towrite;
2809}
2810
2811#endif
2812
Mingming Cao617ba132006-10-11 01:20:53 -07002813static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002814 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2815{
Mingming Cao617ba132006-10-11 01:20:53 -07002816 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002817}
2818
Mingming Cao617ba132006-10-11 01:20:53 -07002819static struct file_system_type ext4dev_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002820 .owner = THIS_MODULE,
Mingming Cao617ba132006-10-11 01:20:53 -07002821 .name = "ext4dev",
2822 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002823 .kill_sb = kill_block_super,
2824 .fs_flags = FS_REQUIRES_DEV,
2825};
2826
Mingming Cao617ba132006-10-11 01:20:53 -07002827static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002828{
Mingming Cao617ba132006-10-11 01:20:53 -07002829 int err = init_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002830 if (err)
2831 return err;
2832 err = init_inodecache();
2833 if (err)
2834 goto out1;
Andrew Morton63f57932006-10-11 01:21:24 -07002835 err = register_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002836 if (err)
2837 goto out;
2838 return 0;
2839out:
2840 destroy_inodecache();
2841out1:
Mingming Cao617ba132006-10-11 01:20:53 -07002842 exit_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002843 return err;
2844}
2845
Mingming Cao617ba132006-10-11 01:20:53 -07002846static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002847{
Mingming Cao617ba132006-10-11 01:20:53 -07002848 unregister_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002849 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07002850 exit_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002851}
2852
2853MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07002854MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002855MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07002856module_init(init_ext4_fs)
2857module_exit(exit_ext4_fs)