blob: cb96f127c36684eab7ddff36e97a64b3d1be0d2c [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>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/blkdev.h>
27#include <linux/parser.h>
28#include <linux/smp_lock.h>
29#include <linux/buffer_head.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070030#include <linux/exportfs.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#include <linux/vfs.h>
32#include <linux/random.h>
33#include <linux/mount.h>
34#include <linux/namei.h>
35#include <linux/quotaops.h>
36#include <linux/seq_file.h>
Vignesh Babu13305932007-07-18 09:11:02 -040037#include <linux/log2.h>
Andreas Dilger717d50e2007-10-16 18:38:25 -040038#include <linux/crc16.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070039#include <asm/uaccess.h>
40
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040041#include "ext4.h"
42#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070043#include "xattr.h"
44#include "acl.h"
45#include "namei.h"
Andreas Dilger717d50e2007-10-16 18:38:25 -040046#include "group.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047
Mingming Cao617ba132006-10-11 01:20:53 -070048static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049 unsigned long journal_devnum);
Mingming Cao617ba132006-10-11 01:20:53 -070050static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070051 unsigned int);
Mingming Cao617ba132006-10-11 01:20:53 -070052static void ext4_commit_super (struct super_block * sb,
53 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070054 int sync);
Mingming Cao617ba132006-10-11 01:20:53 -070055static void ext4_mark_recovery_complete(struct super_block * sb,
56 struct ext4_super_block * es);
57static void ext4_clear_journal_err(struct super_block * sb,
58 struct ext4_super_block * es);
59static int ext4_sync_fs(struct super_block *sb, int wait);
60static const char *ext4_decode_error(struct super_block * sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070061 char nbuf[16]);
Mingming Cao617ba132006-10-11 01:20:53 -070062static int ext4_remount (struct super_block * sb, int * flags, char * data);
63static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
64static void ext4_unlockfs(struct super_block *sb);
65static void ext4_write_super (struct super_block * sb);
66static void ext4_write_super_lockfs(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070067
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070068
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070069ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
70 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070071{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040072 return le32_to_cpu(bg->bg_block_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070073 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040074 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070075}
76
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070077ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
78 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070079{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040080 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070081 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040082 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070083}
84
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070085ext4_fsblk_t ext4_inode_table(struct super_block *sb,
86 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070087{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040088 return le32_to_cpu(bg->bg_inode_table_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070089 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040090 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070091}
92
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070093void ext4_block_bitmap_set(struct super_block *sb,
94 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070095{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040096 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070097 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
98 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070099}
100
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700101void ext4_inode_bitmap_set(struct super_block *sb,
102 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700103{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400104 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700105 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
106 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700107}
108
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700109void ext4_inode_table_set(struct super_block *sb,
110 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700111{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400112 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700113 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
114 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700115}
116
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700117/*
Mingming Caodab291a2006-10-11 01:21:01 -0700118 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700119 *
120 * The only special thing we need to do here is to make sure that all
121 * journal_end calls result in the superblock being marked dirty, so
122 * that sync() will call the filesystem's write_super callback if
123 * appropriate.
124 */
Mingming Cao617ba132006-10-11 01:20:53 -0700125handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700126{
127 journal_t *journal;
128
129 if (sb->s_flags & MS_RDONLY)
130 return ERR_PTR(-EROFS);
131
132 /* Special case here: if the journal has aborted behind our
133 * backs (eg. EIO in the commit thread), then we still need to
134 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700135 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700136 if (is_journal_aborted(journal)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400137 ext4_abort(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138 "Detected aborted journal");
139 return ERR_PTR(-EROFS);
140 }
141
Mingming Caodab291a2006-10-11 01:21:01 -0700142 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700143}
144
145/*
146 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700147 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700148 * that sync() will call the filesystem's write_super callback if
149 * appropriate.
150 */
Mingming Cao617ba132006-10-11 01:20:53 -0700151int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700152{
153 struct super_block *sb;
154 int err;
155 int rc;
156
157 sb = handle->h_transaction->t_journal->j_private;
158 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700159 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160
161 if (!err)
162 err = rc;
163 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700164 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165 return err;
166}
167
Mingming Cao617ba132006-10-11 01:20:53 -0700168void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 struct buffer_head *bh, handle_t *handle, int err)
170{
171 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700172 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700173
174 if (bh)
175 BUFFER_TRACE(bh, "abort");
176
177 if (!handle->h_err)
178 handle->h_err = err;
179
180 if (is_handle_aborted(handle))
181 return;
182
183 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
184 caller, errstr, err_fn);
185
Mingming Caodab291a2006-10-11 01:21:01 -0700186 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187}
188
189/* Deal with the reporting of failure conditions on a filesystem such as
190 * inconsistencies detected or read IO failures.
191 *
192 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700193 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 * write ordering constraints on the superblock which prevent us from
195 * writing it out straight away; and given that the journal is about to
196 * be aborted, we can't rely on the current, or future, transactions to
197 * write out the superblock safely.
198 *
Mingming Caodab291a2006-10-11 01:21:01 -0700199 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700200 * the journal instead. On recovery, the journal will compain about
201 * that error until we've noted it down and cleared it.
202 */
203
Mingming Cao617ba132006-10-11 01:20:53 -0700204static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205{
Mingming Cao617ba132006-10-11 01:20:53 -0700206 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207
Mingming Cao617ba132006-10-11 01:20:53 -0700208 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
209 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210
211 if (sb->s_flags & MS_RDONLY)
212 return;
213
214 if (!test_opt (sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700215 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700216
Mingming Cao617ba132006-10-11 01:20:53 -0700217 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700219 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220 }
221 if (test_opt (sb, ERRORS_RO)) {
222 printk (KERN_CRIT "Remounting filesystem read-only\n");
223 sb->s_flags |= MS_RDONLY;
224 }
Mingming Cao617ba132006-10-11 01:20:53 -0700225 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700227 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228 sb->s_id);
229}
230
Mingming Cao617ba132006-10-11 01:20:53 -0700231void ext4_error (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700232 const char * fmt, ...)
233{
234 va_list args;
235
236 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700237 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700238 vprintk(fmt, args);
239 printk("\n");
240 va_end(args);
241
Mingming Cao617ba132006-10-11 01:20:53 -0700242 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243}
244
Mingming Cao617ba132006-10-11 01:20:53 -0700245static const char *ext4_decode_error(struct super_block * sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246 char nbuf[16])
247{
248 char *errstr = NULL;
249
250 switch (errno) {
251 case -EIO:
252 errstr = "IO failure";
253 break;
254 case -ENOMEM:
255 errstr = "Out of memory";
256 break;
257 case -EROFS:
Mingming Caodab291a2006-10-11 01:21:01 -0700258 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 errstr = "Journal has aborted";
260 else
261 errstr = "Readonly filesystem";
262 break;
263 default:
264 /* If the caller passed in an extra buffer for unknown
265 * errors, textualise them now. Else we just return
266 * NULL. */
267 if (nbuf) {
268 /* Check for truncated error codes... */
269 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
270 errstr = nbuf;
271 }
272 break;
273 }
274
275 return errstr;
276}
277
Mingming Cao617ba132006-10-11 01:20:53 -0700278/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 * automatically and invokes the appropriate error response. */
280
Mingming Cao617ba132006-10-11 01:20:53 -0700281void __ext4_std_error (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 int errno)
283{
284 char nbuf[16];
285 const char *errstr;
286
287 /* Special case: if the error is EROFS, and we're not already
288 * inside a transaction, then there's really no point in logging
289 * an error. */
290 if (errno == -EROFS && journal_current_handle() == NULL &&
291 (sb->s_flags & MS_RDONLY))
292 return;
293
Mingming Cao617ba132006-10-11 01:20:53 -0700294 errstr = ext4_decode_error(sb, errno, nbuf);
295 printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700296 sb->s_id, function, errstr);
297
Mingming Cao617ba132006-10-11 01:20:53 -0700298 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299}
300
301/*
Mingming Cao617ba132006-10-11 01:20:53 -0700302 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 * abort function may be used to deal with unrecoverable failures such
304 * as journal IO errors or ENOMEM at a critical moment in log management.
305 *
306 * We unconditionally force the filesystem into an ABORT|READONLY state,
307 * unless the error response on the fs has been set to panic in which
308 * case we take the easy way out and panic immediately.
309 */
310
Mingming Cao617ba132006-10-11 01:20:53 -0700311void ext4_abort (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 const char * fmt, ...)
313{
314 va_list args;
315
Mingming Cao617ba132006-10-11 01:20:53 -0700316 printk (KERN_CRIT "ext4_abort called.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317
318 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700319 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320 vprintk(fmt, args);
321 printk("\n");
322 va_end(args);
323
324 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700325 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700326
327 if (sb->s_flags & MS_RDONLY)
328 return;
329
330 printk(KERN_CRIT "Remounting filesystem read-only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700331 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700332 sb->s_flags |= MS_RDONLY;
Mingming Cao617ba132006-10-11 01:20:53 -0700333 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Mingming Caodab291a2006-10-11 01:21:01 -0700334 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700335}
336
Mingming Cao617ba132006-10-11 01:20:53 -0700337void ext4_warning (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338 const char * fmt, ...)
339{
340 va_list args;
341
342 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700343 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700344 sb->s_id, function);
345 vprintk(fmt, args);
346 printk("\n");
347 va_end(args);
348}
349
Mingming Cao617ba132006-10-11 01:20:53 -0700350void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351{
Mingming Cao617ba132006-10-11 01:20:53 -0700352 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700353
Mingming Cao617ba132006-10-11 01:20:53 -0700354 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700355 return;
356
Harvey Harrison46e665e2008-04-17 10:38:59 -0400357 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 "updating to rev %d because of new feature flag, "
359 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700360 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361
Mingming Cao617ba132006-10-11 01:20:53 -0700362 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
363 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
364 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365 /* leave es->s_feature_*compat flags alone */
366 /* es->s_uuid will be set by e2fsck if empty */
367
368 /*
369 * The rest of the superblock fields should be zero, and if not it
370 * means they are likely already in use, so leave them alone. We
371 * can leave it up to e2fsck to clean up any inconsistencies there.
372 */
373}
374
Aneesh Kumar K.V99e6f822008-01-28 23:58:27 -0500375int ext4_update_compat_feature(handle_t *handle,
376 struct super_block *sb, __u32 compat)
377{
378 int err = 0;
379 if (!EXT4_HAS_COMPAT_FEATURE(sb, compat)) {
380 err = ext4_journal_get_write_access(handle,
381 EXT4_SB(sb)->s_sbh);
382 if (err)
383 return err;
384 EXT4_SET_COMPAT_FEATURE(sb, compat);
385 sb->s_dirt = 1;
386 handle->h_sync = 1;
387 BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
388 "call ext4_journal_dirty_met adata");
389 err = ext4_journal_dirty_metadata(handle,
390 EXT4_SB(sb)->s_sbh);
391 }
392 return err;
393}
394
395int ext4_update_rocompat_feature(handle_t *handle,
396 struct super_block *sb, __u32 rocompat)
397{
398 int err = 0;
399 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, rocompat)) {
400 err = ext4_journal_get_write_access(handle,
401 EXT4_SB(sb)->s_sbh);
402 if (err)
403 return err;
404 EXT4_SET_RO_COMPAT_FEATURE(sb, rocompat);
405 sb->s_dirt = 1;
406 handle->h_sync = 1;
407 BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
408 "call ext4_journal_dirty_met adata");
409 err = ext4_journal_dirty_metadata(handle,
410 EXT4_SB(sb)->s_sbh);
411 }
412 return err;
413}
414
415int ext4_update_incompat_feature(handle_t *handle,
416 struct super_block *sb, __u32 incompat)
417{
418 int err = 0;
419 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, incompat)) {
420 err = ext4_journal_get_write_access(handle,
421 EXT4_SB(sb)->s_sbh);
422 if (err)
423 return err;
424 EXT4_SET_INCOMPAT_FEATURE(sb, incompat);
425 sb->s_dirt = 1;
426 handle->h_sync = 1;
427 BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
428 "call ext4_journal_dirty_met adata");
429 err = ext4_journal_dirty_metadata(handle,
430 EXT4_SB(sb)->s_sbh);
431 }
432 return err;
433}
434
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435/*
436 * Open the external journal device
437 */
Mingming Cao617ba132006-10-11 01:20:53 -0700438static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700439{
440 struct block_device *bdev;
441 char b[BDEVNAME_SIZE];
442
443 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
444 if (IS_ERR(bdev))
445 goto fail;
446 return bdev;
447
448fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700449 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450 __bdevname(dev, b), PTR_ERR(bdev));
451 return NULL;
452}
453
454/*
455 * Release the journal device
456 */
Mingming Cao617ba132006-10-11 01:20:53 -0700457static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458{
459 bd_release(bdev);
460 return blkdev_put(bdev);
461}
462
Mingming Cao617ba132006-10-11 01:20:53 -0700463static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700464{
465 struct block_device *bdev;
466 int ret = -ENODEV;
467
468 bdev = sbi->journal_bdev;
469 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700470 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 sbi->journal_bdev = NULL;
472 }
473 return ret;
474}
475
476static inline struct inode *orphan_list_entry(struct list_head *l)
477{
Mingming Cao617ba132006-10-11 01:20:53 -0700478 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479}
480
Mingming Cao617ba132006-10-11 01:20:53 -0700481static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482{
483 struct list_head *l;
484
485 printk(KERN_ERR "sb orphan head is %d\n",
486 le32_to_cpu(sbi->s_es->s_last_orphan));
487
488 printk(KERN_ERR "sb_info orphan list:\n");
489 list_for_each(l, &sbi->s_orphan) {
490 struct inode *inode = orphan_list_entry(l);
491 printk(KERN_ERR " "
492 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
493 inode->i_sb->s_id, inode->i_ino, inode,
494 inode->i_mode, inode->i_nlink,
495 NEXT_ORPHAN(inode));
496 }
497}
498
Mingming Cao617ba132006-10-11 01:20:53 -0700499static void ext4_put_super (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500{
Mingming Cao617ba132006-10-11 01:20:53 -0700501 struct ext4_sb_info *sbi = EXT4_SB(sb);
502 struct ext4_super_block *es = sbi->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503 int i;
504
Alex Tomasc9de5602008-01-29 00:19:52 -0500505 ext4_mb_release(sb);
Alex Tomasa86c6182006-10-11 01:21:03 -0700506 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700507 ext4_xattr_put_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -0700508 jbd2_journal_destroy(sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700510 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 es->s_state = cpu_to_le16(sbi->s_mount_state);
512 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
513 mark_buffer_dirty(sbi->s_sbh);
Mingming Cao617ba132006-10-11 01:20:53 -0700514 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515 }
516
517 for (i = 0; i < sbi->s_gdb_count; i++)
518 brelse(sbi->s_group_desc[i]);
519 kfree(sbi->s_group_desc);
520 percpu_counter_destroy(&sbi->s_freeblocks_counter);
521 percpu_counter_destroy(&sbi->s_freeinodes_counter);
522 percpu_counter_destroy(&sbi->s_dirs_counter);
523 brelse(sbi->s_sbh);
524#ifdef CONFIG_QUOTA
525 for (i = 0; i < MAXQUOTAS; i++)
526 kfree(sbi->s_qf_names[i]);
527#endif
528
529 /* Debugging code just in case the in-memory inode orphan list
530 * isn't empty. The on-disk one can be non-empty if we've
531 * detected an error and taken the fs readonly, but the
532 * in-memory list had better be clean by this point. */
533 if (!list_empty(&sbi->s_orphan))
534 dump_orphan_list(sb, sbi);
535 J_ASSERT(list_empty(&sbi->s_orphan));
536
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700537 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
539 /*
540 * Invalidate the journal device's buffers. We don't want them
541 * floating about in memory - the physical journal device may
542 * hotswapped, and it breaks the `ro-after' testing code.
543 */
544 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700545 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700546 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 }
548 sb->s_fs_info = NULL;
549 kfree(sbi);
550 return;
551}
552
Christoph Lametere18b8902006-12-06 20:33:20 -0800553static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554
555/*
556 * Called inside transaction, so use GFP_NOFS
557 */
Mingming Cao617ba132006-10-11 01:20:53 -0700558static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559{
Mingming Cao617ba132006-10-11 01:20:53 -0700560 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800562 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 if (!ei)
564 return NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700565#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
566 ei->i_acl = EXT4_ACL_NOT_CACHED;
567 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568#endif
569 ei->i_block_alloc_info = NULL;
570 ei->vfs_inode.i_version = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700571 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500572 INIT_LIST_HEAD(&ei->i_prealloc_list);
573 spin_lock_init(&ei->i_prealloc_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700574 return &ei->vfs_inode;
575}
576
Mingming Cao617ba132006-10-11 01:20:53 -0700577static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700579 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
580 printk("EXT4 Inode %p: orphan list check failed!\n",
581 EXT4_I(inode));
582 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
583 EXT4_I(inode), sizeof(struct ext4_inode_info),
584 true);
585 dump_stack();
586 }
Mingming Cao617ba132006-10-11 01:20:53 -0700587 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588}
589
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700590static void init_once(struct kmem_cache *cachep, void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591{
Mingming Cao617ba132006-10-11 01:20:53 -0700592 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593
Christoph Lametera35afb82007-05-16 22:10:57 -0700594 INIT_LIST_HEAD(&ei->i_orphan);
Mingming Cao617ba132006-10-11 01:20:53 -0700595#ifdef CONFIG_EXT4DEV_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700596 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500598 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700599 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600}
601
602static int init_inodecache(void)
603{
Mingming Cao617ba132006-10-11 01:20:53 -0700604 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
605 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 0, (SLAB_RECLAIM_ACCOUNT|
607 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900608 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700609 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610 return -ENOMEM;
611 return 0;
612}
613
614static void destroy_inodecache(void)
615{
Mingming Cao617ba132006-10-11 01:20:53 -0700616 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617}
618
Mingming Cao617ba132006-10-11 01:20:53 -0700619static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620{
Mingming Cao617ba132006-10-11 01:20:53 -0700621 struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
622#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
623 if (EXT4_I(inode)->i_acl &&
624 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
625 posix_acl_release(EXT4_I(inode)->i_acl);
626 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700627 }
Mingming Cao617ba132006-10-11 01:20:53 -0700628 if (EXT4_I(inode)->i_default_acl &&
629 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
630 posix_acl_release(EXT4_I(inode)->i_default_acl);
631 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632 }
633#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700634 ext4_discard_reservation(inode);
635 EXT4_I(inode)->i_block_alloc_info = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636 if (unlikely(rsv))
637 kfree(rsv);
638}
639
Mingming Cao617ba132006-10-11 01:20:53 -0700640static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700641{
642#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700643 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700644
645 if (sbi->s_jquota_fmt)
646 seq_printf(seq, ",jqfmt=%s",
647 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
648
649 if (sbi->s_qf_names[USRQUOTA])
650 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
651
652 if (sbi->s_qf_names[GRPQUOTA])
653 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
654
Mingming Cao617ba132006-10-11 01:20:53 -0700655 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700656 seq_puts(seq, ",usrquota");
657
Mingming Cao617ba132006-10-11 01:20:53 -0700658 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659 seq_puts(seq, ",grpquota");
660#endif
661}
662
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700663/*
664 * Show an option if
665 * - it's set to a non-default value OR
666 * - if the per-sb default is different from the global default
667 */
Mingming Cao617ba132006-10-11 01:20:53 -0700668static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500670 int def_errors;
671 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700673 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeen571640c2008-05-26 12:29:46 -0400674 journal_t *journal = sbi->s_journal;
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700675 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700676
677 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500678 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700679
680 if (sbi->s_sb_block != 1)
681 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
682 if (test_opt(sb, MINIX_DF))
683 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500684 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700685 seq_puts(seq, ",grpid");
686 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
687 seq_puts(seq, ",nogrpid");
688 if (sbi->s_resuid != EXT4_DEF_RESUID ||
689 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
690 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
691 }
692 if (sbi->s_resgid != EXT4_DEF_RESGID ||
693 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
694 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
695 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500696 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700697 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500698 def_errors == EXT4_ERRORS_CONTINUE) {
699 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700700 }
701 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500702 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500703 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500704 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700705 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500706 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700707 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500708 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700709 seq_puts(seq, ",debug");
710 if (test_opt(sb, OLDALLOC))
711 seq_puts(seq, ",oldalloc");
Adrian Bunk07620f62008-01-28 23:58:27 -0500712#ifdef CONFIG_EXT4DEV_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500713 if (test_opt(sb, XATTR_USER) &&
714 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700715 seq_puts(seq, ",user_xattr");
716 if (!test_opt(sb, XATTR_USER) &&
717 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
718 seq_puts(seq, ",nouser_xattr");
719 }
720#endif
Adrian Bunk07620f62008-01-28 23:58:27 -0500721#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500722 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700723 seq_puts(seq, ",acl");
724 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
725 seq_puts(seq, ",noacl");
726#endif
727 if (!test_opt(sb, RESERVATION))
728 seq_puts(seq, ",noreservation");
729 if (sbi->s_commit_interval) {
730 seq_printf(seq, ",commit=%u",
731 (unsigned) (sbi->s_commit_interval / HZ));
732 }
Eric Sandeen571640c2008-05-26 12:29:46 -0400733 /*
734 * We're changing the default of barrier mount option, so
735 * let's always display its mount state so it's clear what its
736 * status is.
737 */
738 seq_puts(seq, ",barrier=");
739 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400740 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
741 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700742 if (test_opt(sb, NOBH))
743 seq_puts(seq, ",nobh");
744 if (!test_opt(sb, EXTENTS))
745 seq_puts(seq, ",noextents");
Aneesh Kumar K.V3dbd0ed2008-01-28 23:58:26 -0500746 if (!test_opt(sb, MBALLOC))
747 seq_puts(seq, ",nomballoc");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500748 if (test_opt(sb, I_VERSION))
749 seq_puts(seq, ",i_version");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500751 if (sbi->s_stripe)
752 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500753 /*
754 * journal mode get enabled in different ways
755 * So just print the value even if we didn't specify it
756 */
Mingming Cao617ba132006-10-11 01:20:53 -0700757 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700759 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700760 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700761 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762 seq_puts(seq, ",data=writeback");
763
Mingming Cao617ba132006-10-11 01:20:53 -0700764 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765 return 0;
766}
767
768
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700769static struct inode *ext4_nfs_get_inode(struct super_block *sb,
770 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773
Mingming Cao617ba132006-10-11 01:20:53 -0700774 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700776 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777 return ERR_PTR(-ESTALE);
778
779 /* iget isn't really right if the inode is currently unallocated!!
780 *
Mingming Cao617ba132006-10-11 01:20:53 -0700781 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782 * deleted, so we should be safe.
783 *
784 * Currently we don't know the generation for parent directory, so
785 * a generation of 0 means "accept any"
786 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800787 inode = ext4_iget(sb, ino);
788 if (IS_ERR(inode))
789 return ERR_CAST(inode);
790 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791 iput(inode);
792 return ERR_PTR(-ESTALE);
793 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700794
795 return inode;
796}
797
798static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
799 int fh_len, int fh_type)
800{
801 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
802 ext4_nfs_get_inode);
803}
804
805static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
806 int fh_len, int fh_type)
807{
808 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
809 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810}
811
812#ifdef CONFIG_QUOTA
813#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
814#define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
815
Mingming Cao617ba132006-10-11 01:20:53 -0700816static int ext4_dquot_initialize(struct inode *inode, int type);
817static int ext4_dquot_drop(struct inode *inode);
818static int ext4_write_dquot(struct dquot *dquot);
819static int ext4_acquire_dquot(struct dquot *dquot);
820static int ext4_release_dquot(struct dquot *dquot);
821static int ext4_mark_dquot_dirty(struct dquot *dquot);
822static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -0700823static int ext4_quota_on(struct super_block *sb, int type, int format_id,
824 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -0700825static int ext4_quota_on_mount(struct super_block *sb, int type);
826static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700827 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700828static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700829 const char *data, size_t len, loff_t off);
830
Mingming Cao617ba132006-10-11 01:20:53 -0700831static struct dquot_operations ext4_quota_operations = {
832 .initialize = ext4_dquot_initialize,
833 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700834 .alloc_space = dquot_alloc_space,
835 .alloc_inode = dquot_alloc_inode,
836 .free_space = dquot_free_space,
837 .free_inode = dquot_free_inode,
838 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700839 .write_dquot = ext4_write_dquot,
840 .acquire_dquot = ext4_acquire_dquot,
841 .release_dquot = ext4_release_dquot,
842 .mark_dirty = ext4_mark_dquot_dirty,
843 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844};
845
Mingming Cao617ba132006-10-11 01:20:53 -0700846static struct quotactl_ops ext4_qctl_operations = {
847 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700848 .quota_off = vfs_quota_off,
849 .quota_sync = vfs_quota_sync,
850 .get_info = vfs_get_dqinfo,
851 .set_info = vfs_set_dqinfo,
852 .get_dqblk = vfs_get_dqblk,
853 .set_dqblk = vfs_set_dqblk
854};
855#endif
856
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800857static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700858 .alloc_inode = ext4_alloc_inode,
859 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700860 .write_inode = ext4_write_inode,
861 .dirty_inode = ext4_dirty_inode,
862 .delete_inode = ext4_delete_inode,
863 .put_super = ext4_put_super,
864 .write_super = ext4_write_super,
865 .sync_fs = ext4_sync_fs,
866 .write_super_lockfs = ext4_write_super_lockfs,
867 .unlockfs = ext4_unlockfs,
868 .statfs = ext4_statfs,
869 .remount_fs = ext4_remount,
870 .clear_inode = ext4_clear_inode,
871 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700873 .quota_read = ext4_quota_read,
874 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875#endif
876};
877
Christoph Hellwig39655162007-10-21 16:42:17 -0700878static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700879 .fh_to_dentry = ext4_fh_to_dentry,
880 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -0700881 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882};
883
884enum {
885 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
886 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
887 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
888 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
889 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
890 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -0500891 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700892 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
893 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
894 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
895 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500896 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
Alex Tomasc9de5602008-01-29 00:19:52 -0500897 Opt_mballoc, Opt_nomballoc, Opt_stripe,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700898};
899
900static match_table_t tokens = {
901 {Opt_bsd_df, "bsddf"},
902 {Opt_minix_df, "minixdf"},
903 {Opt_grpid, "grpid"},
904 {Opt_grpid, "bsdgroups"},
905 {Opt_nogrpid, "nogrpid"},
906 {Opt_nogrpid, "sysvgroups"},
907 {Opt_resgid, "resgid=%u"},
908 {Opt_resuid, "resuid=%u"},
909 {Opt_sb, "sb=%u"},
910 {Opt_err_cont, "errors=continue"},
911 {Opt_err_panic, "errors=panic"},
912 {Opt_err_ro, "errors=remount-ro"},
913 {Opt_nouid32, "nouid32"},
914 {Opt_nocheck, "nocheck"},
915 {Opt_nocheck, "check=none"},
916 {Opt_debug, "debug"},
917 {Opt_oldalloc, "oldalloc"},
918 {Opt_orlov, "orlov"},
919 {Opt_user_xattr, "user_xattr"},
920 {Opt_nouser_xattr, "nouser_xattr"},
921 {Opt_acl, "acl"},
922 {Opt_noacl, "noacl"},
923 {Opt_reservation, "reservation"},
924 {Opt_noreservation, "noreservation"},
925 {Opt_noload, "noload"},
926 {Opt_nobh, "nobh"},
927 {Opt_bh, "bh"},
928 {Opt_commit, "commit=%u"},
929 {Opt_journal_update, "journal=update"},
930 {Opt_journal_inum, "journal=%u"},
931 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -0500932 {Opt_journal_checksum, "journal_checksum"},
933 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 {Opt_abort, "abort"},
935 {Opt_data_journal, "data=journal"},
936 {Opt_data_ordered, "data=ordered"},
937 {Opt_data_writeback, "data=writeback"},
938 {Opt_offusrjquota, "usrjquota="},
939 {Opt_usrjquota, "usrjquota=%s"},
940 {Opt_offgrpjquota, "grpjquota="},
941 {Opt_grpjquota, "grpjquota=%s"},
942 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
943 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
944 {Opt_grpquota, "grpquota"},
945 {Opt_noquota, "noquota"},
946 {Opt_quota, "quota"},
947 {Opt_usrquota, "usrquota"},
948 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -0700949 {Opt_extents, "extents"},
Mingming Cao1e2462f2007-07-18 09:00:55 -0400950 {Opt_noextents, "noextents"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500951 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -0500952 {Opt_mballoc, "mballoc"},
953 {Opt_nomballoc, "nomballoc"},
954 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700955 {Opt_resize, "resize"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -0400956 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957};
958
Mingming Cao617ba132006-10-11 01:20:53 -0700959static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700960{
Mingming Cao617ba132006-10-11 01:20:53 -0700961 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962 char *options = (char *) *data;
963
964 if (!options || strncmp(options, "sb=", 3) != 0)
965 return 1; /* Default location */
966 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -0700967 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700968 sb_block = simple_strtoul(options, &options, 0);
969 if (*options && *options != ',') {
Mingming Cao617ba132006-10-11 01:20:53 -0700970 printk("EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971 (char *) *data);
972 return 1;
973 }
974 if (*options == ',')
975 options++;
976 *data = (void *) options;
977 return sb_block;
978}
979
980static int parse_options (char *options, struct super_block *sb,
981 unsigned int *inum, unsigned long *journal_devnum,
Mingming Cao617ba132006-10-11 01:20:53 -0700982 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983{
Mingming Cao617ba132006-10-11 01:20:53 -0700984 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985 char * p;
986 substring_t args[MAX_OPT_ARGS];
987 int data_opt = 0;
988 int option;
989#ifdef CONFIG_QUOTA
Jan Karadfc5d032008-05-13 19:11:51 -0400990 int qtype, qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 char *qname;
992#endif
993
994 if (!options)
995 return 1;
996
997 while ((p = strsep (&options, ",")) != NULL) {
998 int token;
999 if (!*p)
1000 continue;
1001
1002 token = match_token(p, tokens, args);
1003 switch (token) {
1004 case Opt_bsd_df:
1005 clear_opt (sbi->s_mount_opt, MINIX_DF);
1006 break;
1007 case Opt_minix_df:
1008 set_opt (sbi->s_mount_opt, MINIX_DF);
1009 break;
1010 case Opt_grpid:
1011 set_opt (sbi->s_mount_opt, GRPID);
1012 break;
1013 case Opt_nogrpid:
1014 clear_opt (sbi->s_mount_opt, GRPID);
1015 break;
1016 case Opt_resuid:
1017 if (match_int(&args[0], &option))
1018 return 0;
1019 sbi->s_resuid = option;
1020 break;
1021 case Opt_resgid:
1022 if (match_int(&args[0], &option))
1023 return 0;
1024 sbi->s_resgid = option;
1025 break;
1026 case Opt_sb:
1027 /* handled by get_sb_block() instead of here */
1028 /* *sb_block = match_int(&args[0]); */
1029 break;
1030 case Opt_err_panic:
1031 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1032 clear_opt (sbi->s_mount_opt, ERRORS_RO);
1033 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
1034 break;
1035 case Opt_err_ro:
1036 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1037 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1038 set_opt (sbi->s_mount_opt, ERRORS_RO);
1039 break;
1040 case Opt_err_cont:
1041 clear_opt (sbi->s_mount_opt, ERRORS_RO);
1042 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1043 set_opt (sbi->s_mount_opt, ERRORS_CONT);
1044 break;
1045 case Opt_nouid32:
1046 set_opt (sbi->s_mount_opt, NO_UID32);
1047 break;
1048 case Opt_nocheck:
1049 clear_opt (sbi->s_mount_opt, CHECK);
1050 break;
1051 case Opt_debug:
1052 set_opt (sbi->s_mount_opt, DEBUG);
1053 break;
1054 case Opt_oldalloc:
1055 set_opt (sbi->s_mount_opt, OLDALLOC);
1056 break;
1057 case Opt_orlov:
1058 clear_opt (sbi->s_mount_opt, OLDALLOC);
1059 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001060#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 case Opt_user_xattr:
1062 set_opt (sbi->s_mount_opt, XATTR_USER);
1063 break;
1064 case Opt_nouser_xattr:
1065 clear_opt (sbi->s_mount_opt, XATTR_USER);
1066 break;
1067#else
1068 case Opt_user_xattr:
1069 case Opt_nouser_xattr:
Mingming Cao617ba132006-10-11 01:20:53 -07001070 printk("EXT4 (no)user_xattr options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 break;
1072#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001073#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001074 case Opt_acl:
1075 set_opt(sbi->s_mount_opt, POSIX_ACL);
1076 break;
1077 case Opt_noacl:
1078 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1079 break;
1080#else
1081 case Opt_acl:
1082 case Opt_noacl:
Mingming Cao617ba132006-10-11 01:20:53 -07001083 printk("EXT4 (no)acl options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001084 break;
1085#endif
1086 case Opt_reservation:
1087 set_opt(sbi->s_mount_opt, RESERVATION);
1088 break;
1089 case Opt_noreservation:
1090 clear_opt(sbi->s_mount_opt, RESERVATION);
1091 break;
1092 case Opt_journal_update:
1093 /* @@@ FIXME */
1094 /* Eventually we will want to be able to create
1095 a journal file here. For now, only allow the
1096 user to specify an existing inode to be the
1097 journal file. */
1098 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001099 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001100 "journal on remount\n");
1101 return 0;
1102 }
1103 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
1104 break;
1105 case Opt_journal_inum:
1106 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001107 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 "journal on remount\n");
1109 return 0;
1110 }
1111 if (match_int(&args[0], &option))
1112 return 0;
1113 *inum = option;
1114 break;
1115 case Opt_journal_dev:
1116 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001117 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118 "journal on remount\n");
1119 return 0;
1120 }
1121 if (match_int(&args[0], &option))
1122 return 0;
1123 *journal_devnum = option;
1124 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001125 case Opt_journal_checksum:
1126 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1127 break;
1128 case Opt_journal_async_commit:
1129 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1130 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1131 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 case Opt_noload:
1133 set_opt (sbi->s_mount_opt, NOLOAD);
1134 break;
1135 case Opt_commit:
1136 if (match_int(&args[0], &option))
1137 return 0;
1138 if (option < 0)
1139 return 0;
1140 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001141 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001142 sbi->s_commit_interval = HZ * option;
1143 break;
1144 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001145 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 goto datacheck;
1147 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001148 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149 goto datacheck;
1150 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001151 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 datacheck:
1153 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001154 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001155 != data_opt) {
1156 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001157 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001158 "mode on remount\n");
1159 return 0;
1160 }
1161 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001162 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163 sbi->s_mount_opt |= data_opt;
1164 }
1165 break;
1166#ifdef CONFIG_QUOTA
1167 case Opt_usrjquota:
1168 qtype = USRQUOTA;
1169 goto set_qf_name;
1170 case Opt_grpjquota:
1171 qtype = GRPQUOTA;
1172set_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001173 if ((sb_any_quota_enabled(sb) ||
1174 sb_any_quota_suspended(sb)) &&
1175 !sbi->s_qf_names[qtype]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001176 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001177 "EXT4-fs: Cannot change journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 "quota options when quota turned on.\n");
1179 return 0;
1180 }
1181 qname = match_strdup(&args[0]);
1182 if (!qname) {
1183 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001184 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 "storing quotafile name.\n");
1186 return 0;
1187 }
1188 if (sbi->s_qf_names[qtype] &&
1189 strcmp(sbi->s_qf_names[qtype], qname)) {
1190 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001191 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192 "specified.\n", QTYPE2NAME(qtype));
1193 kfree(qname);
1194 return 0;
1195 }
1196 sbi->s_qf_names[qtype] = qname;
1197 if (strchr(sbi->s_qf_names[qtype], '/')) {
1198 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001199 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001200 "filesystem root.\n");
1201 kfree(sbi->s_qf_names[qtype]);
1202 sbi->s_qf_names[qtype] = NULL;
1203 return 0;
1204 }
1205 set_opt(sbi->s_mount_opt, QUOTA);
1206 break;
1207 case Opt_offusrjquota:
1208 qtype = USRQUOTA;
1209 goto clear_qf_name;
1210 case Opt_offgrpjquota:
1211 qtype = GRPQUOTA;
1212clear_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001213 if ((sb_any_quota_enabled(sb) ||
1214 sb_any_quota_suspended(sb)) &&
1215 sbi->s_qf_names[qtype]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001216 printk(KERN_ERR "EXT4-fs: Cannot change "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001217 "journaled quota options when "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001218 "quota turned on.\n");
1219 return 0;
1220 }
1221 /*
1222 * The space will be released later when all options
1223 * are confirmed to be correct
1224 */
1225 sbi->s_qf_names[qtype] = NULL;
1226 break;
1227 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001228 qfmt = QFMT_VFS_OLD;
1229 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001230 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001231 qfmt = QFMT_VFS_V0;
1232set_qf_format:
1233 if ((sb_any_quota_enabled(sb) ||
1234 sb_any_quota_suspended(sb)) &&
1235 sbi->s_jquota_fmt != qfmt) {
1236 printk(KERN_ERR "EXT4-fs: Cannot change "
1237 "journaled quota options when "
1238 "quota turned on.\n");
1239 return 0;
1240 }
1241 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 break;
1243 case Opt_quota:
1244 case Opt_usrquota:
1245 set_opt(sbi->s_mount_opt, QUOTA);
1246 set_opt(sbi->s_mount_opt, USRQUOTA);
1247 break;
1248 case Opt_grpquota:
1249 set_opt(sbi->s_mount_opt, QUOTA);
1250 set_opt(sbi->s_mount_opt, GRPQUOTA);
1251 break;
1252 case Opt_noquota:
1253 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001254 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 "options when quota turned on.\n");
1256 return 0;
1257 }
1258 clear_opt(sbi->s_mount_opt, QUOTA);
1259 clear_opt(sbi->s_mount_opt, USRQUOTA);
1260 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1261 break;
1262#else
1263 case Opt_quota:
1264 case Opt_usrquota:
1265 case Opt_grpquota:
Jan Karacd59e7b2008-05-13 19:11:51 -04001266 printk(KERN_ERR
1267 "EXT4-fs: quota options not supported.\n");
1268 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 case Opt_usrjquota:
1270 case Opt_grpjquota:
1271 case Opt_offusrjquota:
1272 case Opt_offgrpjquota:
1273 case Opt_jqfmt_vfsold:
1274 case Opt_jqfmt_vfsv0:
1275 printk(KERN_ERR
Jan Karacd59e7b2008-05-13 19:11:51 -04001276 "EXT4-fs: journaled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001277 "supported.\n");
1278 break;
1279 case Opt_noquota:
1280 break;
1281#endif
1282 case Opt_abort:
1283 set_opt(sbi->s_mount_opt, ABORT);
1284 break;
1285 case Opt_barrier:
1286 if (match_int(&args[0], &option))
1287 return 0;
1288 if (option)
1289 set_opt(sbi->s_mount_opt, BARRIER);
1290 else
1291 clear_opt(sbi->s_mount_opt, BARRIER);
1292 break;
1293 case Opt_ignore:
1294 break;
1295 case Opt_resize:
1296 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001297 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 "for remount\n");
1299 return 0;
1300 }
1301 if (match_int(&args[0], &option) != 0)
1302 return 0;
1303 *n_blocks_count = option;
1304 break;
1305 case Opt_nobh:
1306 set_opt(sbi->s_mount_opt, NOBH);
1307 break;
1308 case Opt_bh:
1309 clear_opt(sbi->s_mount_opt, NOBH);
1310 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001311 case Opt_extents:
1312 set_opt (sbi->s_mount_opt, EXTENTS);
1313 break;
Mingming Cao1e2462f2007-07-18 09:00:55 -04001314 case Opt_noextents:
1315 clear_opt (sbi->s_mount_opt, EXTENTS);
1316 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001317 case Opt_i_version:
1318 set_opt(sbi->s_mount_opt, I_VERSION);
1319 sb->s_flags |= MS_I_VERSION;
1320 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001321 case Opt_mballoc:
1322 set_opt(sbi->s_mount_opt, MBALLOC);
1323 break;
1324 case Opt_nomballoc:
1325 clear_opt(sbi->s_mount_opt, MBALLOC);
1326 break;
1327 case Opt_stripe:
1328 if (match_int(&args[0], &option))
1329 return 0;
1330 if (option < 0)
1331 return 0;
1332 sbi->s_stripe = option;
1333 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001334 default:
1335 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001336 "EXT4-fs: Unrecognized mount option \"%s\" "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001337 "or missing value\n", p);
1338 return 0;
1339 }
1340 }
1341#ifdef CONFIG_QUOTA
1342 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001343 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001344 sbi->s_qf_names[USRQUOTA])
1345 clear_opt(sbi->s_mount_opt, USRQUOTA);
1346
Mingming Cao617ba132006-10-11 01:20:53 -07001347 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001348 sbi->s_qf_names[GRPQUOTA])
1349 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1350
1351 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001352 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001353 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001354 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1355 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001356 "format mixing.\n");
1357 return 0;
1358 }
1359
1360 if (!sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001361 printk(KERN_ERR "EXT4-fs: journaled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001362 "not specified.\n");
1363 return 0;
1364 }
1365 } else {
1366 if (sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001367 printk(KERN_ERR "EXT4-fs: journaled quota format "
1368 "specified with no journaling "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001369 "enabled.\n");
1370 return 0;
1371 }
1372 }
1373#endif
1374 return 1;
1375}
1376
Mingming Cao617ba132006-10-11 01:20:53 -07001377static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001378 int read_only)
1379{
Mingming Cao617ba132006-10-11 01:20:53 -07001380 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381 int res = 0;
1382
Mingming Cao617ba132006-10-11 01:20:53 -07001383 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1384 printk (KERN_ERR "EXT4-fs warning: revision level too high, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001385 "forcing read-only mode\n");
1386 res = MS_RDONLY;
1387 }
1388 if (read_only)
1389 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001390 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1391 printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001392 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001393 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001394 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001395 "EXT4-fs warning: mounting fs with errors, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001396 "running e2fsck is recommended\n");
1397 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1398 le16_to_cpu(es->s_mnt_count) >=
1399 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1400 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001401 "EXT4-fs warning: maximal mount count reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001402 "running e2fsck is recommended\n");
1403 else if (le32_to_cpu(es->s_checkinterval) &&
1404 (le32_to_cpu(es->s_lastcheck) +
1405 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1406 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001407 "EXT4-fs warning: checktime reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001408 "running e2fsck is recommended\n");
1409#if 0
1410 /* @@@ We _will_ want to clear the valid bit if we find
Andrew Morton63f57932006-10-11 01:21:24 -07001411 * inconsistencies, to force a fsck at reboot. But for
1412 * a plain journaled filesystem we can keep it set as
1413 * valid forever! :)
1414 */
Marcin Slusarz216c34b2008-04-17 10:38:59 -04001415 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001416#endif
1417 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001418 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001419 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001420 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001421 ext4_update_dynamic_rev(sb);
1422 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001423
Mingming Cao617ba132006-10-11 01:20:53 -07001424 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001425 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -07001426 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001427 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1428 sb->s_blocksize,
1429 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001430 EXT4_BLOCKS_PER_GROUP(sb),
1431 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001432 sbi->s_mount_opt);
1433
Mingming Cao617ba132006-10-11 01:20:53 -07001434 printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1435 if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436 char b[BDEVNAME_SIZE];
1437
1438 printk("external journal on %s\n",
Mingming Cao617ba132006-10-11 01:20:53 -07001439 bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001440 } else {
1441 printk("internal journal\n");
1442 }
1443 return res;
1444}
1445
Andreas Dilger717d50e2007-10-16 18:38:25 -04001446__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1447 struct ext4_group_desc *gdp)
1448{
1449 __u16 crc = 0;
1450
1451 if (sbi->s_es->s_feature_ro_compat &
1452 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1453 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1454 __le32 le_group = cpu_to_le32(block_group);
1455
1456 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1457 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1458 crc = crc16(crc, (__u8 *)gdp, offset);
1459 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1460 /* for checksum of struct ext4_group_desc do the rest...*/
1461 if ((sbi->s_es->s_feature_incompat &
1462 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1463 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1464 crc = crc16(crc, (__u8 *)gdp + offset,
1465 le16_to_cpu(sbi->s_es->s_desc_size) -
1466 offset);
1467 }
1468
1469 return cpu_to_le16(crc);
1470}
1471
1472int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1473 struct ext4_group_desc *gdp)
1474{
1475 if ((sbi->s_es->s_feature_ro_compat &
1476 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1477 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1478 return 0;
1479
1480 return 1;
1481}
1482
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001483/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001484static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485{
Mingming Cao617ba132006-10-11 01:20:53 -07001486 struct ext4_sb_info *sbi = EXT4_SB(sb);
1487 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1488 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001489 ext4_fsblk_t block_bitmap;
1490 ext4_fsblk_t inode_bitmap;
1491 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001492 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001493 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001494
Jose R. Santosce421582007-10-16 18:38:25 -04001495 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1496 flexbg_flag = 1;
1497
Mingming Cao617ba132006-10-11 01:20:53 -07001498 ext4_debug ("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001499
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001500 for (i = 0; i < sbi->s_groups_count; i++) {
1501 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1502
Jose R. Santosce421582007-10-16 18:38:25 -04001503 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001504 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 else
1506 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001507 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001508
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001509 block_bitmap = ext4_block_bitmap(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001510 if (block_bitmap < first_block || block_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 {
Josef Bacikc19204b2008-04-29 22:00:28 -04001512 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1513 "Block bitmap for group %lu not in group "
1514 "(block %llu)!", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001515 return 0;
1516 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001517 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001518 if (inode_bitmap < first_block || inode_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001519 {
Josef Bacikc19204b2008-04-29 22:00:28 -04001520 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1521 "Inode bitmap for group %lu not in group "
1522 "(block %llu)!", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001523 return 0;
1524 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001525 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001526 if (inode_table < first_block ||
Eric Sandeen780dcdb2007-07-26 10:41:11 -07001527 inode_table + sbi->s_itb_per_group - 1 > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001528 {
Josef Bacikc19204b2008-04-29 22:00:28 -04001529 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1530 "Inode table for group %lu not in group "
1531 "(block %llu)!", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001532 return 0;
1533 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001534 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001535 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1536 "Checksum for group %lu failed (%u!=%u)\n",
1537 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1538 gdp)), le16_to_cpu(gdp->bg_checksum));
Andreas Dilger717d50e2007-10-16 18:38:25 -04001539 return 0;
1540 }
Jose R. Santosce421582007-10-16 18:38:25 -04001541 if (!flexbg_flag)
1542 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543 }
1544
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001545 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07001546 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001547 return 1;
1548}
1549
Mingming Cao617ba132006-10-11 01:20:53 -07001550/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551 * the superblock) which were deleted from all directories, but held open by
1552 * a process at the time of a crash. We walk the list and try to delete these
1553 * inodes at recovery time (only with a read-write filesystem).
1554 *
1555 * In order to keep the orphan inode chain consistent during traversal (in
1556 * case of crash during recovery), we link each inode into the superblock
1557 * orphan list_head and handle it the same way as an inode deletion during
1558 * normal operation (which journals the operations for us).
1559 *
1560 * We only do an iget() and an iput() on each inode, which is very safe if we
1561 * accidentally point at an in-use or already deleted inode. The worst that
1562 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001563 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001564 * e2fsck was run on this filesystem, and it must have already done the orphan
1565 * inode cleanup for us, so we can safely abort without any further action.
1566 */
Mingming Cao617ba132006-10-11 01:20:53 -07001567static void ext4_orphan_cleanup (struct super_block * sb,
1568 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001569{
1570 unsigned int s_flags = sb->s_flags;
1571 int nr_orphans = 0, nr_truncates = 0;
1572#ifdef CONFIG_QUOTA
1573 int i;
1574#endif
1575 if (!es->s_last_orphan) {
1576 jbd_debug(4, "no orphan inodes to clean up\n");
1577 return;
1578 }
1579
Eric Sandeena8f48a92006-12-06 20:40:13 -08001580 if (bdev_read_only(sb->s_bdev)) {
1581 printk(KERN_ERR "EXT4-fs: write access "
1582 "unavailable, skipping orphan cleanup.\n");
1583 return;
1584 }
1585
Mingming Cao617ba132006-10-11 01:20:53 -07001586 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001587 if (es->s_last_orphan)
1588 jbd_debug(1, "Errors on filesystem, "
1589 "clearing orphan list.\n");
1590 es->s_last_orphan = 0;
1591 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1592 return;
1593 }
1594
1595 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001596 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001597 sb->s_id);
1598 sb->s_flags &= ~MS_RDONLY;
1599 }
1600#ifdef CONFIG_QUOTA
1601 /* Needed for iput() to work correctly and not trash data */
1602 sb->s_flags |= MS_ACTIVE;
1603 /* Turn on quotas so that they are updated correctly */
1604 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001605 if (EXT4_SB(sb)->s_qf_names[i]) {
1606 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001607 if (ret < 0)
1608 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001609 "EXT4-fs: Cannot turn on journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 "quota: error %d\n", ret);
1611 }
1612 }
1613#endif
1614
1615 while (es->s_last_orphan) {
1616 struct inode *inode;
1617
Josef Bacik97bd42b2008-04-29 22:04:56 -04001618 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1619 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001620 es->s_last_orphan = 0;
1621 break;
1622 }
1623
Mingming Cao617ba132006-10-11 01:20:53 -07001624 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625 DQUOT_INIT(inode);
1626 if (inode->i_nlink) {
1627 printk(KERN_DEBUG
1628 "%s: truncating inode %lu to %Ld bytes\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001629 __func__, inode->i_ino, inode->i_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001630 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1631 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001632 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001633 nr_truncates++;
1634 } else {
1635 printk(KERN_DEBUG
1636 "%s: deleting unreferenced inode %lu\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001637 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001638 jbd_debug(2, "deleting unreferenced inode %lu\n",
1639 inode->i_ino);
1640 nr_orphans++;
1641 }
1642 iput(inode); /* The delete magic happens here! */
1643 }
1644
1645#define PLURAL(x) (x), ((x)==1) ? "" : "s"
1646
1647 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001648 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649 sb->s_id, PLURAL(nr_orphans));
1650 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001651 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652 sb->s_id, PLURAL(nr_truncates));
1653#ifdef CONFIG_QUOTA
1654 /* Turn quotas off */
1655 for (i = 0; i < MAXQUOTAS; i++) {
1656 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07001657 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001658 }
1659#endif
1660 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1661}
Eric Sandeencd2291a2008-01-28 23:58:27 -05001662/*
1663 * Maximal extent format file size.
1664 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1665 * extent format containers, within a sector_t, and within i_blocks
1666 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1667 * so that won't be a limiting factor.
1668 *
1669 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1670 */
1671static loff_t ext4_max_size(int blkbits)
1672{
1673 loff_t res;
1674 loff_t upper_limit = MAX_LFS_FILESIZE;
1675
1676 /* small i_blocks in vfs inode? */
1677 if (sizeof(blkcnt_t) < sizeof(u64)) {
1678 /*
1679 * CONFIG_LSF is not enabled implies the inode
1680 * i_block represent total blocks in 512 bytes
1681 * 32 == size of vfs inode i_blocks * 8
1682 */
1683 upper_limit = (1LL << 32) - 1;
1684
1685 /* total blocks in file system block size */
1686 upper_limit >>= (blkbits - 9);
1687 upper_limit <<= blkbits;
1688 }
1689
1690 /* 32-bit extent-start container, ee_block */
1691 res = 1LL << 32;
1692 res <<= blkbits;
1693 res -= 1;
1694
1695 /* Sanity check against vm- & vfs- imposed limits */
1696 if (res > upper_limit)
1697 res = upper_limit;
1698
1699 return res;
1700}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001701
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001702/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05001703 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001704 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1705 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001706 */
Eric Sandeencd2291a2008-01-28 23:58:27 -05001707static loff_t ext4_max_bitmap_size(int bits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001708{
Mingming Cao617ba132006-10-11 01:20:53 -07001709 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001710 int meta_blocks;
1711 loff_t upper_limit;
1712 /* This is calculated to be the largest file size for a
Eric Sandeencd2291a2008-01-28 23:58:27 -05001713 * dense, bitmapped file such that the total number of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001715 * does not exceed 2^48 -1
1716 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1717 * total number of 512 bytes blocks of the file
1718 */
1719
1720 if (sizeof(blkcnt_t) < sizeof(u64)) {
1721 /*
1722 * CONFIG_LSF is not enabled implies the inode
1723 * i_block represent total blocks in 512 bytes
1724 * 32 == size of vfs inode i_blocks * 8
1725 */
1726 upper_limit = (1LL << 32) - 1;
1727
1728 /* total blocks in file system block size */
1729 upper_limit >>= (bits - 9);
1730
1731 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05001732 /*
1733 * We use 48 bit ext4_inode i_blocks
1734 * With EXT4_HUGE_FILE_FL set the i_blocks
1735 * represent total number of blocks in
1736 * file system block size
1737 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001738 upper_limit = (1LL << 48) - 1;
1739
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001740 }
1741
1742 /* indirect blocks */
1743 meta_blocks = 1;
1744 /* double indirect blocks */
1745 meta_blocks += 1 + (1LL << (bits-2));
1746 /* tripple indirect blocks */
1747 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1748
1749 upper_limit -= meta_blocks;
1750 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001751
1752 res += 1LL << (bits-2);
1753 res += 1LL << (2*(bits-2));
1754 res += 1LL << (3*(bits-2));
1755 res <<= bits;
1756 if (res > upper_limit)
1757 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001758
1759 if (res > MAX_LFS_FILESIZE)
1760 res = MAX_LFS_FILESIZE;
1761
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001762 return res;
1763}
1764
Mingming Cao617ba132006-10-11 01:20:53 -07001765static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001766 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001767{
Mingming Cao617ba132006-10-11 01:20:53 -07001768 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001769 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001770 int has_super = 0;
1771
1772 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1773
Mingming Cao617ba132006-10-11 01:20:53 -07001774 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001775 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001776 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001777 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001778 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001780 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001781}
1782
Alex Tomasc9de5602008-01-29 00:19:52 -05001783/**
1784 * ext4_get_stripe_size: Get the stripe size.
1785 * @sbi: In memory super block info
1786 *
1787 * If we have specified it via mount option, then
1788 * use the mount option value. If the value specified at mount time is
1789 * greater than the blocks per group use the super block value.
1790 * If the super block value is greater than blocks per group return 0.
1791 * Allocator needs it be less than blocks per group.
1792 *
1793 */
1794static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1795{
1796 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1797 unsigned long stripe_width =
1798 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1799
1800 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1801 return sbi->s_stripe;
1802
1803 if (stripe_width <= sbi->s_blocks_per_group)
1804 return stripe_width;
1805
1806 if (stride <= sbi->s_blocks_per_group)
1807 return stride;
1808
1809 return 0;
1810}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001811
Mingming Cao617ba132006-10-11 01:20:53 -07001812static int ext4_fill_super (struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001813 __releases(kernel_sem)
1814 __acquires(kernel_sem)
1815
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001816{
1817 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001818 struct ext4_super_block *es = NULL;
1819 struct ext4_sb_info *sbi;
1820 ext4_fsblk_t block;
1821 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001822 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001823 unsigned long offset = 0;
1824 unsigned int journal_inum = 0;
1825 unsigned long journal_devnum = 0;
1826 unsigned long def_mount_opts;
1827 struct inode *root;
David Howells1d1fe1e2008-02-07 00:15:37 -08001828 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001829 int blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001830 int db_count;
1831 int i;
1832 int needs_recovery;
1833 __le32 features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001834 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07001835 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836
1837 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1838 if (!sbi)
1839 return -ENOMEM;
1840 sb->s_fs_info = sbi;
1841 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001842 sbi->s_resuid = EXT4_DEF_RESUID;
1843 sbi->s_resgid = EXT4_DEF_RESGID;
Miklos Szeredid9c9bef2007-10-16 23:26:27 -07001844 sbi->s_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001845
1846 unlock_kernel();
1847
Mingming Cao617ba132006-10-11 01:20:53 -07001848 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001849 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07001850 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001851 goto out_fail;
1852 }
1853
Takashi Satoafc7cbc2008-01-28 23:58:27 -05001854 if (!sb_set_blocksize(sb, blocksize)) {
1855 printk(KERN_ERR "EXT4-fs: bad blocksize %d.\n", blocksize);
1856 goto out_fail;
1857 }
1858
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001859 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001860 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001861 * block sizes. We need to calculate the offset from buffer start.
1862 */
Mingming Cao617ba132006-10-11 01:20:53 -07001863 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001864 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1865 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001866 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001867 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001868 }
1869
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001870 if (!(bh = sb_bread(sb, logical_sb_block))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001871 printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001872 goto out_fail;
1873 }
1874 /*
1875 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07001876 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001877 */
Mingming Cao617ba132006-10-11 01:20:53 -07001878 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001879 sbi->s_es = es;
1880 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07001881 if (sb->s_magic != EXT4_SUPER_MAGIC)
1882 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001883
1884 /* Set defaults before we parse the mount options */
1885 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07001886 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001887 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07001888 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001889 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07001890 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001891 set_opt(sbi->s_mount_opt, NO_UID32);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001892#ifdef CONFIG_EXT4DEV_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07001893 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001894 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001895#endif
1896#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07001897 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001898 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001899#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001900 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1901 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1902 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1903 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1904 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1905 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001906
Mingming Cao617ba132006-10-11 01:20:53 -07001907 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001908 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05001909 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07001910 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05001911 else
1912 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001913
1914 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1915 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1916
1917 set_opt(sbi->s_mount_opt, RESERVATION);
Eric Sandeen571640c2008-05-26 12:29:46 -04001918 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001919
Mingming Cao1e2462f2007-07-18 09:00:55 -04001920 /*
1921 * turn on extents feature by default in ext4 filesystem
1922 * User -o noextents to turn it off
1923 */
1924 set_opt(sbi->s_mount_opt, EXTENTS);
Aneesh Kumar K.V3dbd0ed2008-01-28 23:58:26 -05001925 /*
1926 * turn on mballoc feature by default in ext4 filesystem
1927 * User -o nomballoc to turn it off
1928 */
1929 set_opt(sbi->s_mount_opt, MBALLOC);
Mingming Cao1e2462f2007-07-18 09:00:55 -04001930
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001931 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1932 NULL, 0))
1933 goto failed_mount;
1934
1935 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07001936 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001937
Mingming Cao617ba132006-10-11 01:20:53 -07001938 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1939 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1940 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1941 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001942 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001943 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001944 "running e2fsck is recommended\n");
Theodore Tso469108f2008-02-10 01:11:44 -05001945
1946 /*
1947 * Since ext4 is still considered development code, we require
1948 * that the TEST_FILESYS flag in s->flags be set.
1949 */
1950 if (!(le32_to_cpu(es->s_flags) & EXT2_FLAGS_TEST_FILESYS)) {
1951 printk(KERN_WARNING "EXT4-fs: %s: not marked "
1952 "OK to use with test code.\n", sb->s_id);
1953 goto failed_mount;
1954 }
1955
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001956 /*
1957 * Check feature flags regardless of the revision level, since we
1958 * previously didn't change the revision level when setting the flags,
1959 * so there is a chance incompat flags are set on a rev 0 filesystem.
1960 */
Mingming Cao617ba132006-10-11 01:20:53 -07001961 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001962 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001963 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001964 "unsupported optional features (%x).\n",
1965 sb->s_id, le32_to_cpu(features));
1966 goto failed_mount;
1967 }
Mingming Cao617ba132006-10-11 01:20:53 -07001968 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001970 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001971 "unsupported optional features (%x).\n",
1972 sb->s_id, le32_to_cpu(features));
1973 goto failed_mount;
1974 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001975 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
1976 /*
1977 * Large file size enabled file system can only be
1978 * mount if kernel is build with CONFIG_LSF
1979 */
1980 if (sizeof(root->i_blocks) < sizeof(u64) &&
1981 !(sb->s_flags & MS_RDONLY)) {
1982 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
1983 "files cannot be mounted read-write "
1984 "without CONFIG_LSF.\n", sb->s_id);
1985 goto failed_mount;
1986 }
1987 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001988 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1989
Mingming Cao617ba132006-10-11 01:20:53 -07001990 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1991 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001992 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001993 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001994 blocksize, sb->s_id);
1995 goto failed_mount;
1996 }
1997
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001998 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05001999
2000 /* Validate the filesystem blocksize */
2001 if (!sb_set_blocksize(sb, blocksize)) {
2002 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2003 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002004 goto failed_mount;
2005 }
2006
2007 brelse (bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002008 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2009 offset = do_div(logical_sb_block, blocksize);
2010 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002011 if (!bh) {
2012 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002013 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002014 goto failed_mount;
2015 }
Mingming Cao617ba132006-10-11 01:20:53 -07002016 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002017 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002018 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002019 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002020 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002021 goto failed_mount;
2022 }
2023 }
2024
Eric Sandeene2b46572008-01-28 23:58:27 -05002025 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits);
Mingming Cao617ba132006-10-11 01:20:53 -07002026 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002027
Mingming Cao617ba132006-10-11 01:20:53 -07002028 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2029 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2030 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002031 } else {
2032 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2033 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002034 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002035 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002036 (sbi->s_inode_size > blocksize)) {
2037 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002038 "EXT4-fs: unsupported inode size: %d\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002039 sbi->s_inode_size);
2040 goto failed_mount;
2041 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002042 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2043 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002044 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002045 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2046 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002047 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002048 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002049 !is_power_of_2(sbi->s_desc_size)) {
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002050 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002051 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002052 sbi->s_desc_size);
2053 goto failed_mount;
2054 }
2055 } else
2056 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002058 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002059 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002060 goto cantfind_ext4;
2061 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002062 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002063 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002064 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2065 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002066 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002067 sbi->s_sbh = bh;
2068 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002069 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2070 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002071 for (i=0; i < 4; i++)
2072 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2073 sbi->s_def_hash_version = es->s_def_hash_version;
2074
2075 if (sbi->s_blocks_per_group > blocksize * 8) {
2076 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002077 "EXT4-fs: #blocks per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002078 sbi->s_blocks_per_group);
2079 goto failed_mount;
2080 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081 if (sbi->s_inodes_per_group > blocksize * 8) {
2082 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002083 "EXT4-fs: #inodes per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002084 sbi->s_inodes_per_group);
2085 goto failed_mount;
2086 }
2087
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002088 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002089 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002090 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002091 " too large to mount safely\n", sb->s_id);
2092 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07002093 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002094 "enabled\n");
2095 goto failed_mount;
2096 }
2097
Mingming Cao617ba132006-10-11 01:20:53 -07002098 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2099 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002100
2101 /* ensure blocks_count calculation below doesn't sign-extend */
2102 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2103 le32_to_cpu(es->s_first_data_block) + 1) {
2104 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2105 "first data block %u, blocks per group %lu\n",
2106 ext4_blocks_count(es),
2107 le32_to_cpu(es->s_first_data_block),
2108 EXT4_BLOCKS_PER_GROUP(sb));
2109 goto failed_mount;
2110 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002111 blocks_count = (ext4_blocks_count(es) -
2112 le32_to_cpu(es->s_first_data_block) +
2113 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2114 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2115 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07002116 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2117 EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002118 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
2119 GFP_KERNEL);
2120 if (sbi->s_group_desc == NULL) {
Mingming Cao617ba132006-10-11 01:20:53 -07002121 printk (KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002122 goto failed_mount;
2123 }
2124
2125 bgl_lock_init(&sbi->s_blockgroup_lock);
2126
2127 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002128 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129 sbi->s_group_desc[i] = sb_bread(sb, block);
2130 if (!sbi->s_group_desc[i]) {
Mingming Cao617ba132006-10-11 01:20:53 -07002131 printk (KERN_ERR "EXT4-fs: "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002132 "can't read group descriptor %d\n", i);
2133 db_count = i;
2134 goto failed_mount2;
2135 }
2136 }
Mingming Cao617ba132006-10-11 01:20:53 -07002137 if (!ext4_check_descriptors (sb)) {
2138 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002139 goto failed_mount2;
2140 }
2141 sbi->s_gdb_count = db_count;
2142 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2143 spin_lock_init(&sbi->s_next_gen_lock);
2144
Peter Zijlstra833f4072007-10-16 23:25:45 -07002145 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2146 ext4_count_free_blocks(sb));
2147 if (!err) {
2148 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2149 ext4_count_free_inodes(sb));
2150 }
2151 if (!err) {
2152 err = percpu_counter_init(&sbi->s_dirs_counter,
2153 ext4_count_dirs(sb));
2154 }
2155 if (err) {
2156 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2157 goto failed_mount3;
2158 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002159
2160 /* per fileystem reservation list head & lock */
2161 spin_lock_init(&sbi->s_rsv_window_lock);
2162 sbi->s_rsv_window_root = RB_ROOT;
2163 /* Add a single, static dummy reservation to the start of the
2164 * reservation window list --- it gives us a placeholder for
2165 * append-at-start-of-list which makes the allocation logic
2166 * _much_ simpler. */
Mingming Cao617ba132006-10-11 01:20:53 -07002167 sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
2168 sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
2170 sbi->s_rsv_window_head.rsv_goal_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002171 ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002172
Alex Tomasc9de5602008-01-29 00:19:52 -05002173 sbi->s_stripe = ext4_get_stripe_size(sbi);
2174
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002175 /*
2176 * set up enough so that it can read an inode
2177 */
Mingming Cao617ba132006-10-11 01:20:53 -07002178 sb->s_op = &ext4_sops;
2179 sb->s_export_op = &ext4_export_ops;
2180 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002181#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002182 sb->s_qcop = &ext4_qctl_operations;
2183 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002184#endif
2185 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2186
2187 sb->s_root = NULL;
2188
2189 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002190 EXT4_HAS_INCOMPAT_FEATURE(sb,
2191 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002192
2193 /*
2194 * The first inode we look at is the journal inode. Don't try
2195 * root first: it may be modified in the journal!
2196 */
2197 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002198 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2199 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002200 goto failed_mount3;
Theodore Ts'o624080e2008-06-06 17:50:40 -04002201 if (!(sb->s_flags & MS_RDONLY) &&
2202 EXT4_SB(sb)->s_journal->j_failed_commit) {
2203 printk(KERN_CRIT "EXT4-fs error (device %s): "
2204 "ext4_fill_super: Journal transaction "
2205 "%u is corrupt\n", sb->s_id,
2206 EXT4_SB(sb)->s_journal->j_failed_commit);
2207 if (test_opt (sb, ERRORS_RO)) {
2208 printk (KERN_CRIT
2209 "Mounting filesystem read-only\n");
2210 sb->s_flags |= MS_RDONLY;
2211 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2212 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2213 }
2214 if (test_opt(sb, ERRORS_PANIC)) {
2215 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2216 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2217 ext4_commit_super(sb, es, 1);
2218 printk(KERN_CRIT
2219 "EXT4-fs (device %s): mount failed\n",
2220 sb->s_id);
2221 goto failed_mount4;
2222 }
2223 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002225 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002226 goto failed_mount3;
2227 } else {
2228 if (!silent)
2229 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002230 "ext4: No journal on filesystem on %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002231 sb->s_id);
2232 goto failed_mount3;
2233 }
2234
Jose R. Santoseb40a092007-07-18 08:37:25 -04002235 if (ext4_blocks_count(es) > 0xffffffffULL &&
2236 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2237 JBD2_FEATURE_INCOMPAT_64BIT)) {
2238 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2239 goto failed_mount4;
2240 }
2241
Girish Shilamkar818d2762008-01-28 23:58:27 -05002242 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2243 jbd2_journal_set_features(sbi->s_journal,
2244 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2245 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2246 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2247 jbd2_journal_set_features(sbi->s_journal,
2248 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2249 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2250 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2251 } else {
2252 jbd2_journal_clear_features(sbi->s_journal,
2253 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2254 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2255 }
2256
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002257 /* We have now updated the journal if required, so we can
2258 * validate the data journaling mode. */
2259 switch (test_opt(sb, DATA_FLAGS)) {
2260 case 0:
2261 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002262 * capabilities: ORDERED_DATA if the journal can
2263 * cope, else JOURNAL_DATA
2264 */
Mingming Caodab291a2006-10-11 01:21:01 -07002265 if (jbd2_journal_check_available_features
2266 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002267 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2268 else
2269 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2270 break;
2271
Mingming Cao617ba132006-10-11 01:20:53 -07002272 case EXT4_MOUNT_ORDERED_DATA:
2273 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002274 if (!jbd2_journal_check_available_features
2275 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002276 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002277 "requested data journaling mode\n");
2278 goto failed_mount4;
2279 }
2280 default:
2281 break;
2282 }
2283
2284 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002285 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2286 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002287 "its supported only with writeback mode\n");
2288 clear_opt(sbi->s_mount_opt, NOBH);
2289 }
2290 }
2291 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002292 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293 * so we can safely mount the rest of the filesystem now.
2294 */
2295
David Howells1d1fe1e2008-02-07 00:15:37 -08002296 root = ext4_iget(sb, EXT4_ROOT_INO);
2297 if (IS_ERR(root)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002298 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
David Howells1d1fe1e2008-02-07 00:15:37 -08002299 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002300 goto failed_mount4;
2301 }
2302 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002303 iput(root);
Mingming Cao617ba132006-10-11 01:20:53 -07002304 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002305 goto failed_mount4;
2306 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002307 sb->s_root = d_alloc_root(root);
2308 if (!sb->s_root) {
2309 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2310 iput(root);
2311 ret = -ENOMEM;
2312 goto failed_mount4;
2313 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002314
Mingming Cao617ba132006-10-11 01:20:53 -07002315 ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002316
2317 /* determine the minimum size of new large inodes, if present */
2318 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2319 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2320 EXT4_GOOD_OLD_INODE_SIZE;
2321 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2322 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2323 if (sbi->s_want_extra_isize <
2324 le16_to_cpu(es->s_want_extra_isize))
2325 sbi->s_want_extra_isize =
2326 le16_to_cpu(es->s_want_extra_isize);
2327 if (sbi->s_want_extra_isize <
2328 le16_to_cpu(es->s_min_extra_isize))
2329 sbi->s_want_extra_isize =
2330 le16_to_cpu(es->s_min_extra_isize);
2331 }
2332 }
2333 /* Check if enough inode space is available */
2334 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2335 sbi->s_inode_size) {
2336 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2337 EXT4_GOOD_OLD_INODE_SIZE;
2338 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2339 "available.\n");
2340 }
2341
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002342 /*
2343 * akpm: core read_super() calls in here with the superblock locked.
2344 * That deadlocks, because orphan cleanup needs to lock the superblock
2345 * in numerous places. Here we just pop the lock - it's relatively
2346 * harmless, because we are now ready to accept write_super() requests,
2347 * and aviro says that's the only reason for hanging onto the
2348 * superblock lock.
2349 */
Mingming Cao617ba132006-10-11 01:20:53 -07002350 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2351 ext4_orphan_cleanup(sb, es);
2352 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002353 if (needs_recovery)
Mingming Cao617ba132006-10-11 01:20:53 -07002354 printk (KERN_INFO "EXT4-fs: recovery complete.\n");
2355 ext4_mark_recovery_complete(sb, es);
2356 printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
2357 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
2358 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002359 "writeback");
2360
Alex Tomasa86c6182006-10-11 01:21:03 -07002361 ext4_ext_init(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002362 ext4_mb_init(sb, needs_recovery);
Alex Tomasa86c6182006-10-11 01:21:03 -07002363
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002364 lock_kernel();
2365 return 0;
2366
Mingming Cao617ba132006-10-11 01:20:53 -07002367cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002368 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07002369 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002370 sb->s_id);
2371 goto failed_mount;
2372
2373failed_mount4:
Mingming Caodab291a2006-10-11 01:21:01 -07002374 jbd2_journal_destroy(sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002375failed_mount3:
2376 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2377 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2378 percpu_counter_destroy(&sbi->s_dirs_counter);
2379failed_mount2:
2380 for (i = 0; i < db_count; i++)
2381 brelse(sbi->s_group_desc[i]);
2382 kfree(sbi->s_group_desc);
2383failed_mount:
2384#ifdef CONFIG_QUOTA
2385 for (i = 0; i < MAXQUOTAS; i++)
2386 kfree(sbi->s_qf_names[i]);
2387#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002388 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002389 brelse(bh);
2390out_fail:
2391 sb->s_fs_info = NULL;
2392 kfree(sbi);
2393 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08002394 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002395}
2396
2397/*
2398 * Setup any per-fs journal parameters now. We'll do this both on
2399 * initial mount, once the journal has been initialised but before we've
2400 * done any recovery; and again on any subsequent remount.
2401 */
Mingming Cao617ba132006-10-11 01:20:53 -07002402static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002403{
Mingming Cao617ba132006-10-11 01:20:53 -07002404 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002405
2406 if (sbi->s_commit_interval)
2407 journal->j_commit_interval = sbi->s_commit_interval;
Mingming Cao617ba132006-10-11 01:20:53 -07002408 /* We could also set up an ext4-specific default for the commit
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002409 * interval here, but for now we'll just fall back to the jbd
2410 * default. */
2411
2412 spin_lock(&journal->j_state_lock);
2413 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07002414 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002415 else
Mingming Caodab291a2006-10-11 01:21:01 -07002416 journal->j_flags &= ~JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002417 spin_unlock(&journal->j_state_lock);
2418}
2419
Mingming Cao617ba132006-10-11 01:20:53 -07002420static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002421 unsigned int journal_inum)
2422{
2423 struct inode *journal_inode;
2424 journal_t *journal;
2425
2426 /* First, test for the existence of a valid inode on disk. Bad
2427 * things happen if we iget() an unused inode, as the subsequent
2428 * iput() will try to delete it. */
2429
David Howells1d1fe1e2008-02-07 00:15:37 -08002430 journal_inode = ext4_iget(sb, journal_inum);
2431 if (IS_ERR(journal_inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002432 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002433 return NULL;
2434 }
2435 if (!journal_inode->i_nlink) {
2436 make_bad_inode(journal_inode);
2437 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002438 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439 return NULL;
2440 }
2441
2442 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
2443 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08002444 if (!S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002445 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002446 iput(journal_inode);
2447 return NULL;
2448 }
2449
Mingming Caodab291a2006-10-11 01:21:01 -07002450 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002451 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002452 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002453 iput(journal_inode);
2454 return NULL;
2455 }
2456 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002457 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002458 return journal;
2459}
2460
Mingming Cao617ba132006-10-11 01:20:53 -07002461static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462 dev_t j_dev)
2463{
2464 struct buffer_head * bh;
2465 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07002466 ext4_fsblk_t start;
2467 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002468 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07002469 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002470 unsigned long offset;
Mingming Cao617ba132006-10-11 01:20:53 -07002471 struct ext4_super_block * es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002472 struct block_device *bdev;
2473
Mingming Cao617ba132006-10-11 01:20:53 -07002474 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002475 if (bdev == NULL)
2476 return NULL;
2477
2478 if (bd_claim(bdev, sb)) {
2479 printk(KERN_ERR
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002480 "EXT4: failed to claim external journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002481 blkdev_put(bdev);
2482 return NULL;
2483 }
2484
2485 blocksize = sb->s_blocksize;
2486 hblock = bdev_hardsect_size(bdev);
2487 if (blocksize < hblock) {
2488 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002489 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002490 goto out_bdev;
2491 }
2492
Mingming Cao617ba132006-10-11 01:20:53 -07002493 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2494 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002495 set_blocksize(bdev, blocksize);
2496 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002497 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002498 "external journal\n");
2499 goto out_bdev;
2500 }
2501
Mingming Cao617ba132006-10-11 01:20:53 -07002502 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2503 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002504 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002505 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2506 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002507 "bad superblock\n");
2508 brelse(bh);
2509 goto out_bdev;
2510 }
2511
Mingming Cao617ba132006-10-11 01:20:53 -07002512 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2513 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002514 brelse(bh);
2515 goto out_bdev;
2516 }
2517
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002518 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002519 start = sb_block + 1;
2520 brelse(bh); /* we're done with the superblock */
2521
Mingming Caodab291a2006-10-11 01:21:01 -07002522 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002523 start, len, blocksize);
2524 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002525 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002526 goto out_bdev;
2527 }
2528 journal->j_private = sb;
2529 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2530 wait_on_buffer(journal->j_sb_buffer);
2531 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002532 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002533 goto out_journal;
2534 }
2535 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002536 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002537 "user (unsupported) - %d\n",
2538 be32_to_cpu(journal->j_superblock->s_nr_users));
2539 goto out_journal;
2540 }
Mingming Cao617ba132006-10-11 01:20:53 -07002541 EXT4_SB(sb)->journal_bdev = bdev;
2542 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002543 return journal;
2544out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002545 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002546out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002547 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002548 return NULL;
2549}
2550
Mingming Cao617ba132006-10-11 01:20:53 -07002551static int ext4_load_journal(struct super_block *sb,
2552 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002553 unsigned long journal_devnum)
2554{
2555 journal_t *journal;
2556 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2557 dev_t journal_dev;
2558 int err = 0;
2559 int really_read_only;
2560
2561 if (journal_devnum &&
2562 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002563 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002564 "numbers have changed\n");
2565 journal_dev = new_decode_dev(journal_devnum);
2566 } else
2567 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2568
2569 really_read_only = bdev_read_only(sb->s_bdev);
2570
2571 /*
2572 * Are we loading a blank journal or performing recovery after a
2573 * crash? For recovery, we need to check in advance whether we
2574 * can get read-write access to the device.
2575 */
2576
Mingming Cao617ba132006-10-11 01:20:53 -07002577 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002578 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002579 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002580 "required on readonly filesystem.\n");
2581 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002582 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002583 "unavailable, cannot proceed.\n");
2584 return -EROFS;
2585 }
Mingming Cao617ba132006-10-11 01:20:53 -07002586 printk (KERN_INFO "EXT4-fs: write access will "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002587 "be enabled during recovery.\n");
2588 }
2589 }
2590
2591 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002592 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002593 "and inode journals!\n");
2594 return -EINVAL;
2595 }
2596
2597 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002598 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002599 return -EINVAL;
2600 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002601 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002602 return -EINVAL;
2603 }
2604
2605 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002606 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002607 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002608 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002609 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002610 return err;
2611 }
2612 }
2613
Mingming Cao617ba132006-10-11 01:20:53 -07002614 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002615 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002616 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002617 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002618
2619 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002620 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002621 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002622 return err;
2623 }
2624
Mingming Cao617ba132006-10-11 01:20:53 -07002625 EXT4_SB(sb)->s_journal = journal;
2626 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002627
2628 if (journal_devnum &&
2629 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2630 es->s_journal_dev = cpu_to_le32(journal_devnum);
2631 sb->s_dirt = 1;
2632
2633 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002634 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002635 }
2636
2637 return 0;
2638}
2639
Mingming Cao617ba132006-10-11 01:20:53 -07002640static int ext4_create_journal(struct super_block * sb,
2641 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002642 unsigned int journal_inum)
2643{
2644 journal_t *journal;
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002645 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002646
2647 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002648 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002649 "create journal.\n");
2650 return -EROFS;
2651 }
2652
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002653 journal = ext4_get_journal(sb, journal_inum);
2654 if (!journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002655 return -EINVAL;
2656
Mingming Cao617ba132006-10-11 01:20:53 -07002657 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002658 journal_inum);
2659
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002660 err = jbd2_journal_create(journal);
2661 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002662 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002663 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002664 return -EIO;
2665 }
2666
Mingming Cao617ba132006-10-11 01:20:53 -07002667 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002668
Mingming Cao617ba132006-10-11 01:20:53 -07002669 ext4_update_dynamic_rev(sb);
2670 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2671 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002672
2673 es->s_journal_inum = cpu_to_le32(journal_inum);
2674 sb->s_dirt = 1;
2675
2676 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002677 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002678
2679 return 0;
2680}
2681
Mingming Cao617ba132006-10-11 01:20:53 -07002682static void ext4_commit_super (struct super_block * sb,
2683 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002684 int sync)
2685{
Mingming Cao617ba132006-10-11 01:20:53 -07002686 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002687
2688 if (!sbh)
2689 return;
2690 es->s_wtime = cpu_to_le32(get_seconds());
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002691 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07002692 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002693 BUFFER_TRACE(sbh, "marking dirty");
2694 mark_buffer_dirty(sbh);
2695 if (sync)
2696 sync_dirty_buffer(sbh);
2697}
2698
2699
2700/*
2701 * Have we just finished recovery? If so, and if we are mounting (or
2702 * remounting) the filesystem readonly, then we will end up with a
2703 * consistent fs on disk. Record that fact.
2704 */
Mingming Cao617ba132006-10-11 01:20:53 -07002705static void ext4_mark_recovery_complete(struct super_block * sb,
2706 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002707{
Mingming Cao617ba132006-10-11 01:20:53 -07002708 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002709
Mingming Caodab291a2006-10-11 01:21:01 -07002710 jbd2_journal_lock_updates(journal);
2711 jbd2_journal_flush(journal);
Jan Kara32c37732007-07-15 23:41:09 -07002712 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002713 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002714 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002715 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002716 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002717 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002718 }
Jan Kara32c37732007-07-15 23:41:09 -07002719 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002720 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002721}
2722
2723/*
2724 * If we are mounting (or read-write remounting) a filesystem whose journal
2725 * has recorded an error from a previous lifetime, move that error to the
2726 * main filesystem now.
2727 */
Mingming Cao617ba132006-10-11 01:20:53 -07002728static void ext4_clear_journal_err(struct super_block * sb,
2729 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002730{
2731 journal_t *journal;
2732 int j_errno;
2733 const char *errstr;
2734
Mingming Cao617ba132006-10-11 01:20:53 -07002735 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002736
2737 /*
2738 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07002739 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002740 */
2741
Mingming Caodab291a2006-10-11 01:21:01 -07002742 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002743 if (j_errno) {
2744 char nbuf[16];
2745
Mingming Cao617ba132006-10-11 01:20:53 -07002746 errstr = ext4_decode_error(sb, j_errno, nbuf);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002747 ext4_warning(sb, __func__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002748 "from previous mount: %s", errstr);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002749 ext4_warning(sb, __func__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002750 "filesystem check.");
2751
Mingming Cao617ba132006-10-11 01:20:53 -07002752 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2753 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2754 ext4_commit_super (sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002755
Mingming Caodab291a2006-10-11 01:21:01 -07002756 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002757 }
2758}
2759
2760/*
2761 * Force the running and committing transactions to commit,
2762 * and wait on the commit.
2763 */
Mingming Cao617ba132006-10-11 01:20:53 -07002764int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002765{
2766 journal_t *journal;
2767 int ret;
2768
2769 if (sb->s_flags & MS_RDONLY)
2770 return 0;
2771
Mingming Cao617ba132006-10-11 01:20:53 -07002772 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002773 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002774 ret = ext4_journal_force_commit(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002775 return ret;
2776}
2777
2778/*
Mingming Cao617ba132006-10-11 01:20:53 -07002779 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002780 * have to propagate any other updates to the superblock on disk at this
2781 * point. Just start an async writeback to get the buffers on their way
2782 * to the disk.
2783 *
2784 * This implicitly triggers the writebehind on sync().
2785 */
2786
Mingming Cao617ba132006-10-11 01:20:53 -07002787static void ext4_write_super (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002788{
2789 if (mutex_trylock(&sb->s_lock) != 0)
2790 BUG();
2791 sb->s_dirt = 0;
2792}
2793
Mingming Cao617ba132006-10-11 01:20:53 -07002794static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002795{
2796 tid_t target;
2797
2798 sb->s_dirt = 0;
Mingming Caodab291a2006-10-11 01:21:01 -07002799 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002800 if (wait)
Mingming Caodab291a2006-10-11 01:21:01 -07002801 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002802 }
2803 return 0;
2804}
2805
2806/*
2807 * LVM calls this function before a (read-only) snapshot is created. This
2808 * gives us a chance to flush the journal completely and mark the fs clean.
2809 */
Mingming Cao617ba132006-10-11 01:20:53 -07002810static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002811{
2812 sb->s_dirt = 0;
2813
2814 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002815 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002816
2817 /* Now we set up the journal barrier. */
Mingming Caodab291a2006-10-11 01:21:01 -07002818 jbd2_journal_lock_updates(journal);
2819 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002820
2821 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07002822 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2823 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002824 }
2825}
2826
2827/*
2828 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2829 * flag here, even though the filesystem is not technically dirty yet.
2830 */
Mingming Cao617ba132006-10-11 01:20:53 -07002831static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002832{
2833 if (!(sb->s_flags & MS_RDONLY)) {
2834 lock_super(sb);
2835 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07002836 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2837 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002838 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002839 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002840 }
2841}
2842
Mingming Cao617ba132006-10-11 01:20:53 -07002843static int ext4_remount (struct super_block * sb, int * flags, char * data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002844{
Mingming Cao617ba132006-10-11 01:20:53 -07002845 struct ext4_super_block * es;
2846 struct ext4_sb_info *sbi = EXT4_SB(sb);
2847 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002848 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07002849 struct ext4_mount_options old_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002850 int err;
2851#ifdef CONFIG_QUOTA
2852 int i;
2853#endif
2854
2855 /* Store the original options */
2856 old_sb_flags = sb->s_flags;
2857 old_opts.s_mount_opt = sbi->s_mount_opt;
2858 old_opts.s_resuid = sbi->s_resuid;
2859 old_opts.s_resgid = sbi->s_resgid;
2860 old_opts.s_commit_interval = sbi->s_commit_interval;
2861#ifdef CONFIG_QUOTA
2862 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2863 for (i = 0; i < MAXQUOTAS; i++)
2864 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2865#endif
2866
2867 /*
2868 * Allow the "check" option to be passed as a remount option.
2869 */
2870 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2871 err = -EINVAL;
2872 goto restore_opts;
2873 }
2874
Mingming Cao617ba132006-10-11 01:20:53 -07002875 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
Harvey Harrison46e665e2008-04-17 10:38:59 -04002876 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002877
2878 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002879 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002880
2881 es = sbi->s_es;
2882
Mingming Cao617ba132006-10-11 01:20:53 -07002883 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002884
2885 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002886 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002887 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002888 err = -EROFS;
2889 goto restore_opts;
2890 }
2891
2892 if (*flags & MS_RDONLY) {
2893 /*
2894 * First of all, the unconditional stuff we have to do
2895 * to disable replay of the journal when we next remount
2896 */
2897 sb->s_flags |= MS_RDONLY;
2898
2899 /*
2900 * OK, test if we are remounting a valid rw partition
2901 * readonly, and if so set the rdonly flag and then
2902 * mark the partition as valid again.
2903 */
Mingming Cao617ba132006-10-11 01:20:53 -07002904 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2905 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002906 es->s_state = cpu_to_le16(sbi->s_mount_state);
2907
Jan Kara32c37732007-07-15 23:41:09 -07002908 /*
2909 * We have to unlock super so that we can wait for
2910 * transactions.
2911 */
2912 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002913 ext4_mark_recovery_complete(sb, es);
Jan Kara32c37732007-07-15 23:41:09 -07002914 lock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002915 } else {
2916 __le32 ret;
Mingming Cao617ba132006-10-11 01:20:53 -07002917 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2918 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2919 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002920 "remount RDWR because of unsupported "
2921 "optional features (%x).\n",
2922 sb->s_id, le32_to_cpu(ret));
2923 err = -EROFS;
2924 goto restore_opts;
2925 }
Eric Sandeenead65962007-02-10 01:46:08 -08002926
2927 /*
2928 * If we have an unprocessed orphan list hanging
2929 * around from a previously readonly bdev mount,
2930 * require a full umount/remount for now.
2931 */
2932 if (es->s_last_orphan) {
2933 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2934 "remount RDWR because of unprocessed "
2935 "orphan inode list. Please "
2936 "umount/remount instead.\n",
2937 sb->s_id);
2938 err = -EINVAL;
2939 goto restore_opts;
2940 }
2941
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002942 /*
2943 * Mounting a RDONLY partition read-write, so reread
2944 * and store the current valid flag. (It may have
2945 * been changed by e2fsck since we originally mounted
2946 * the partition.)
2947 */
Mingming Cao617ba132006-10-11 01:20:53 -07002948 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002949 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07002950 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002951 goto restore_opts;
Mingming Cao617ba132006-10-11 01:20:53 -07002952 if (!ext4_setup_super (sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002953 sb->s_flags &= ~MS_RDONLY;
2954 }
2955 }
2956#ifdef CONFIG_QUOTA
2957 /* Release old quota file names */
2958 for (i = 0; i < MAXQUOTAS; i++)
2959 if (old_opts.s_qf_names[i] &&
2960 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2961 kfree(old_opts.s_qf_names[i]);
2962#endif
2963 return 0;
2964restore_opts:
2965 sb->s_flags = old_sb_flags;
2966 sbi->s_mount_opt = old_opts.s_mount_opt;
2967 sbi->s_resuid = old_opts.s_resuid;
2968 sbi->s_resgid = old_opts.s_resgid;
2969 sbi->s_commit_interval = old_opts.s_commit_interval;
2970#ifdef CONFIG_QUOTA
2971 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2972 for (i = 0; i < MAXQUOTAS; i++) {
2973 if (sbi->s_qf_names[i] &&
2974 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2975 kfree(sbi->s_qf_names[i]);
2976 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2977 }
2978#endif
2979 return err;
2980}
2981
Mingming Cao617ba132006-10-11 01:20:53 -07002982static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002983{
2984 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002985 struct ext4_sb_info *sbi = EXT4_SB(sb);
2986 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08002987 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002988
Badari Pulavarty5e700302007-07-15 23:42:00 -07002989 if (test_opt(sb, MINIX_DF)) {
2990 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04002991 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Avantika Mathurfd2d4292008-01-28 23:58:27 -05002992 ext4_group_t ngroups = sbi->s_groups_count, i;
Badari Pulavarty5e700302007-07-15 23:42:00 -07002993 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002994 smp_rmb();
2995
2996 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07002997 * Compute the overhead (FS structures). This is constant
2998 * for a given filesystem unless the number of block groups
2999 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003000 */
3001
3002 /*
3003 * All of the blocks before first_data_block are
3004 * overhead
3005 */
3006 overhead = le32_to_cpu(es->s_first_data_block);
3007
3008 /*
3009 * Add the overhead attributed to the superblock and
3010 * block group descriptors. If the sparse superblocks
3011 * feature is turned on, then not all groups have this.
3012 */
3013 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003014 overhead += ext4_bg_has_super(sb, i) +
3015 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003016 cond_resched();
3017 }
3018
3019 /*
3020 * Every block group has an inode bitmap, a block
3021 * bitmap, and an inode table.
3022 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003023 overhead += ngroups * (2 + sbi->s_itb_per_group);
3024 sbi->s_overhead_last = overhead;
3025 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003026 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003027 }
3028
Mingming Cao617ba132006-10-11 01:20:53 -07003029 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003030 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003031 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003032 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
Aneesh Kumar K.V308ba3e2007-10-16 18:38:25 -04003033 ext4_free_blocks_count_set(es, buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003034 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3035 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003036 buf->f_bavail = 0;
3037 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003038 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003039 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07003040 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003041 fsid = le64_to_cpup((void *)es->s_uuid) ^
3042 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3043 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3044 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003045 return 0;
3046}
3047
3048/* Helper function for writing quotas on sync - we need to start transaction before quota file
3049 * is locked for write. Otherwise the are possible deadlocks:
3050 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003051 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07003052 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003053 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003054 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003055 *
3056 */
3057
3058#ifdef CONFIG_QUOTA
3059
3060static inline struct inode *dquot_to_inode(struct dquot *dquot)
3061{
3062 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3063}
3064
Mingming Cao617ba132006-10-11 01:20:53 -07003065static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003066{
3067 handle_t *handle;
3068 int ret, err;
3069
3070 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003071 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003072 if (IS_ERR(handle))
3073 return PTR_ERR(handle);
3074 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003075 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003076 if (!ret)
3077 ret = err;
3078 return ret;
3079}
3080
Mingming Cao617ba132006-10-11 01:20:53 -07003081static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003082{
3083 handle_t *handle;
3084 int ret, err;
3085
3086 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003087 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Jan Kara2887df12008-04-29 22:02:07 -04003088 if (IS_ERR(handle)) {
3089 /*
3090 * We call dquot_drop() anyway to at least release references
3091 * to quota structures so that umount does not hang.
3092 */
3093 dquot_drop(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003094 return PTR_ERR(handle);
Jan Kara2887df12008-04-29 22:02:07 -04003095 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003096 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003097 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003098 if (!ret)
3099 ret = err;
3100 return ret;
3101}
3102
Mingming Cao617ba132006-10-11 01:20:53 -07003103static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003104{
3105 int ret, err;
3106 handle_t *handle;
3107 struct inode *inode;
3108
3109 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003110 handle = ext4_journal_start(inode,
3111 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003112 if (IS_ERR(handle))
3113 return PTR_ERR(handle);
3114 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003115 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003116 if (!ret)
3117 ret = err;
3118 return ret;
3119}
3120
Mingming Cao617ba132006-10-11 01:20:53 -07003121static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003122{
3123 int ret, err;
3124 handle_t *handle;
3125
Mingming Cao617ba132006-10-11 01:20:53 -07003126 handle = ext4_journal_start(dquot_to_inode(dquot),
3127 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003128 if (IS_ERR(handle))
3129 return PTR_ERR(handle);
3130 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003131 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003132 if (!ret)
3133 ret = err;
3134 return ret;
3135}
3136
Mingming Cao617ba132006-10-11 01:20:53 -07003137static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003138{
3139 int ret, err;
3140 handle_t *handle;
3141
Mingming Cao617ba132006-10-11 01:20:53 -07003142 handle = ext4_journal_start(dquot_to_inode(dquot),
3143 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003144 if (IS_ERR(handle)) {
3145 /* Release dquot anyway to avoid endless cycle in dqput() */
3146 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003147 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003148 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003149 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003150 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003151 if (!ret)
3152 ret = err;
3153 return ret;
3154}
3155
Mingming Cao617ba132006-10-11 01:20:53 -07003156static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003157{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003158 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003159 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3160 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003161 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003162 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003163 } else {
3164 return dquot_mark_dquot_dirty(dquot);
3165 }
3166}
3167
Mingming Cao617ba132006-10-11 01:20:53 -07003168static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003169{
3170 int ret, err;
3171 handle_t *handle;
3172
3173 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003174 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003175 if (IS_ERR(handle))
3176 return PTR_ERR(handle);
3177 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003178 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179 if (!ret)
3180 ret = err;
3181 return ret;
3182}
3183
3184/*
3185 * Turn on quotas during mount time - we need to find
3186 * the quota file and such...
3187 */
Mingming Cao617ba132006-10-11 01:20:53 -07003188static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003189{
Mingming Cao617ba132006-10-11 01:20:53 -07003190 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3191 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003192}
3193
3194/*
3195 * Standard function to be called on quota_on
3196 */
Mingming Cao617ba132006-10-11 01:20:53 -07003197static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Jan Kara6f28e082008-04-28 02:14:34 -07003198 char *path, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003199{
3200 int err;
3201 struct nameidata nd;
3202
3203 if (!test_opt(sb, QUOTA))
3204 return -EINVAL;
Jan Kara06235432008-05-13 19:11:51 -04003205 /* When remounting, no checks are needed and in fact, path is NULL */
3206 if (remount)
Jan Kara6f28e082008-04-28 02:14:34 -07003207 return vfs_quota_on(sb, type, format_id, path, remount);
Jan Kara06235432008-05-13 19:11:51 -04003208
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003209 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
3210 if (err)
3211 return err;
Jan Kara06235432008-05-13 19:11:51 -04003212
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003213 /* Quotafile not on the same filesystem? */
Jan Blunck4ac91372008-02-14 19:34:32 -08003214 if (nd.path.mnt->mnt_sb != sb) {
Jan Blunck1d957f92008-02-14 19:34:35 -08003215 path_put(&nd.path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003216 return -EXDEV;
3217 }
Jan Kara06235432008-05-13 19:11:51 -04003218 /* Journaling quota? */
3219 if (EXT4_SB(sb)->s_qf_names[type]) {
3220 /* Quotafile not of fs root? */
3221 if (nd.path.dentry->d_parent->d_inode != sb->s_root->d_inode)
3222 printk(KERN_WARNING
3223 "EXT4-fs: Quota file not on filesystem root. "
3224 "Journaled quota will not work.\n");
3225 }
3226
3227 /*
3228 * When we journal data on quota file, we have to flush journal to see
3229 * all updates to the file when we bypass pagecache...
3230 */
3231 if (ext4_should_journal_data(nd.path.dentry->d_inode)) {
3232 /*
3233 * We don't need to lock updates but journal_flush() could
3234 * otherwise be livelocked...
3235 */
3236 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3237 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3238 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3239 }
3240
Jan Blunck1d957f92008-02-14 19:34:35 -08003241 path_put(&nd.path);
Jan Kara6f28e082008-04-28 02:14:34 -07003242 return vfs_quota_on(sb, type, format_id, path, remount);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003243}
3244
3245/* Read data from quotafile - avoid pagecache and such because we cannot afford
3246 * acquiring the locks... As quota files are never truncated and quota code
3247 * itself serializes the operations (and noone else should touch the files)
3248 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003249static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003250 size_t len, loff_t off)
3251{
3252 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003253 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003254 int err = 0;
3255 int offset = off & (sb->s_blocksize - 1);
3256 int tocopy;
3257 size_t toread;
3258 struct buffer_head *bh;
3259 loff_t i_size = i_size_read(inode);
3260
3261 if (off > i_size)
3262 return 0;
3263 if (off+len > i_size)
3264 len = i_size-off;
3265 toread = len;
3266 while (toread > 0) {
3267 tocopy = sb->s_blocksize - offset < toread ?
3268 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003269 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003270 if (err)
3271 return err;
3272 if (!bh) /* A hole? */
3273 memset(data, 0, tocopy);
3274 else
3275 memcpy(data, bh->b_data+offset, tocopy);
3276 brelse(bh);
3277 offset = 0;
3278 toread -= tocopy;
3279 data += tocopy;
3280 blk++;
3281 }
3282 return len;
3283}
3284
3285/* Write to quotafile (we know the transaction is already started and has
3286 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003287static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003288 const char *data, size_t len, loff_t off)
3289{
3290 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003291 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003292 int err = 0;
3293 int offset = off & (sb->s_blocksize - 1);
3294 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003295 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003296 size_t towrite = len;
3297 struct buffer_head *bh;
3298 handle_t *handle = journal_current_handle();
3299
Jan Kara9c3013e2007-09-11 15:23:29 -07003300 if (!handle) {
3301 printk(KERN_WARNING "EXT4-fs: Quota write (off=%Lu, len=%Lu)"
3302 " cancelled because transaction is not started.\n",
3303 (unsigned long long)off, (unsigned long long)len);
3304 return -EIO;
3305 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003306 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3307 while (towrite > 0) {
3308 tocopy = sb->s_blocksize - offset < towrite ?
3309 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003310 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003311 if (!bh)
3312 goto out;
3313 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003314 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003315 if (err) {
3316 brelse(bh);
3317 goto out;
3318 }
3319 }
3320 lock_buffer(bh);
3321 memcpy(bh->b_data+offset, data, tocopy);
3322 flush_dcache_page(bh->b_page);
3323 unlock_buffer(bh);
3324 if (journal_quota)
Mingming Cao617ba132006-10-11 01:20:53 -07003325 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003326 else {
3327 /* Always do at least ordered writes for quotas */
Mingming Cao617ba132006-10-11 01:20:53 -07003328 err = ext4_journal_dirty_data(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003329 mark_buffer_dirty(bh);
3330 }
3331 brelse(bh);
3332 if (err)
3333 goto out;
3334 offset = 0;
3335 towrite -= tocopy;
3336 data += tocopy;
3337 blk++;
3338 }
3339out:
3340 if (len == towrite)
3341 return err;
3342 if (inode->i_size < off+len-towrite) {
3343 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07003344 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003345 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003346 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003347 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003348 mutex_unlock(&inode->i_mutex);
3349 return len - towrite;
3350}
3351
3352#endif
3353
Mingming Cao617ba132006-10-11 01:20:53 -07003354static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003355 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3356{
Mingming Cao617ba132006-10-11 01:20:53 -07003357 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003358}
3359
Mingming Cao617ba132006-10-11 01:20:53 -07003360static struct file_system_type ext4dev_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003361 .owner = THIS_MODULE,
Mingming Cao617ba132006-10-11 01:20:53 -07003362 .name = "ext4dev",
3363 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003364 .kill_sb = kill_block_super,
3365 .fs_flags = FS_REQUIRES_DEV,
3366};
3367
Mingming Cao617ba132006-10-11 01:20:53 -07003368static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003369{
Alex Tomasc9de5602008-01-29 00:19:52 -05003370 int err;
3371
3372 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003373 if (err)
3374 return err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003375
3376 err = init_ext4_xattr();
3377 if (err)
3378 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003379 err = init_inodecache();
3380 if (err)
3381 goto out1;
Andrew Morton63f57932006-10-11 01:21:24 -07003382 err = register_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003383 if (err)
3384 goto out;
3385 return 0;
3386out:
3387 destroy_inodecache();
3388out1:
Mingming Cao617ba132006-10-11 01:20:53 -07003389 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003390out2:
3391 exit_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003392 return err;
3393}
3394
Mingming Cao617ba132006-10-11 01:20:53 -07003395static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003396{
Mingming Cao617ba132006-10-11 01:20:53 -07003397 unregister_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003398 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07003399 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003400 exit_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003401}
3402
3403MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07003404MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003405MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07003406module_init(init_ext4_fs)
3407module_exit(exit_ext4_fs)