blob: 2415e2b0970733f392ce4c9d1f7d5a48e70c013b [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>
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040037#include <linux/proc_fs.h>
Theodore Ts'oede86cc2008-10-05 20:50:06 -040038#include <linux/marker.h>
Vignesh Babu13305932007-07-18 09:11:02 -040039#include <linux/log2.h>
Andreas Dilger717d50e2007-10-16 18:38:25 -040040#include <linux/crc16.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070041#include <asm/uaccess.h>
42
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040043#include "ext4.h"
44#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045#include "xattr.h"
46#include "acl.h"
47#include "namei.h"
Andreas Dilger717d50e2007-10-16 18:38:25 -040048#include "group.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040050struct proc_dir_entry *ext4_proc_root;
51
Mingming Cao617ba132006-10-11 01:20:53 -070052static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053 unsigned long journal_devnum);
Mingming Cao617ba132006-10-11 01:20:53 -070054static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070055 unsigned int);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040056static void ext4_commit_super(struct super_block *sb,
57 struct ext4_super_block *es, int sync);
58static void ext4_mark_recovery_complete(struct super_block *sb,
59 struct ext4_super_block *es);
60static void ext4_clear_journal_err(struct super_block *sb,
61 struct ext4_super_block *es);
Mingming Cao617ba132006-10-11 01:20:53 -070062static int ext4_sync_fs(struct super_block *sb, int wait);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040063static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064 char nbuf[16]);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040065static int ext4_remount(struct super_block *sb, int *flags, char *data);
66static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
Mingming Cao617ba132006-10-11 01:20:53 -070067static void ext4_unlockfs(struct super_block *sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040068static void ext4_write_super(struct super_block *sb);
Mingming Cao617ba132006-10-11 01:20:53 -070069static void ext4_write_super_lockfs(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070070
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070071
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070072ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
73 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070074{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040075 return le32_to_cpu(bg->bg_block_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070076 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040077 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070078}
79
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070080ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
81 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070082{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040083 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070084 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040085 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070086}
87
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070088ext4_fsblk_t ext4_inode_table(struct super_block *sb,
89 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070090{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040091 return le32_to_cpu(bg->bg_inode_table_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070092 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040093 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070094}
95
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070096void ext4_block_bitmap_set(struct super_block *sb,
97 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070098{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040099 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700100 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
101 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700102}
103
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700104void ext4_inode_bitmap_set(struct super_block *sb,
105 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700106{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400107 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700108 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
109 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700110}
111
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700112void ext4_inode_table_set(struct super_block *sb,
113 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700114{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400115 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700116 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
117 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700118}
119
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700120/*
Mingming Caodab291a2006-10-11 01:21:01 -0700121 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700122 *
123 * The only special thing we need to do here is to make sure that all
124 * journal_end calls result in the superblock being marked dirty, so
125 * that sync() will call the filesystem's write_super callback if
126 * appropriate.
127 */
Mingming Cao617ba132006-10-11 01:20:53 -0700128handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700129{
130 journal_t *journal;
131
132 if (sb->s_flags & MS_RDONLY)
133 return ERR_PTR(-EROFS);
134
135 /* Special case here: if the journal has aborted behind our
136 * backs (eg. EIO in the commit thread), then we still need to
137 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700138 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -0500139 if (journal) {
140 if (is_journal_aborted(journal)) {
141 ext4_abort(sb, __func__,
142 "Detected aborted journal");
143 return ERR_PTR(-EROFS);
144 }
145 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146 }
Frank Mayhar03901312009-01-07 00:06:22 -0500147 /*
148 * We're not journaling, return the appropriate indication.
149 */
150 current->journal_info = EXT4_NOJOURNAL_HANDLE;
151 return current->journal_info;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700152}
153
154/*
155 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700156 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700157 * that sync() will call the filesystem's write_super callback if
158 * appropriate.
159 */
Mingming Cao617ba132006-10-11 01:20:53 -0700160int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161{
162 struct super_block *sb;
163 int err;
164 int rc;
165
Frank Mayhar03901312009-01-07 00:06:22 -0500166 if (!ext4_handle_valid(handle)) {
167 /*
168 * Do this here since we don't call jbd2_journal_stop() in
169 * no-journal mode.
170 */
171 current->journal_info = NULL;
172 return 0;
173 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 sb = handle->h_transaction->t_journal->j_private;
175 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700176 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177
178 if (!err)
179 err = rc;
180 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700181 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182 return err;
183}
184
Mingming Cao617ba132006-10-11 01:20:53 -0700185void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 struct buffer_head *bh, handle_t *handle, int err)
187{
188 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700189 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190
Frank Mayhar03901312009-01-07 00:06:22 -0500191 BUG_ON(!ext4_handle_valid(handle));
192
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193 if (bh)
194 BUFFER_TRACE(bh, "abort");
195
196 if (!handle->h_err)
197 handle->h_err = err;
198
199 if (is_handle_aborted(handle))
200 return;
201
202 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
203 caller, errstr, err_fn);
204
Mingming Caodab291a2006-10-11 01:21:01 -0700205 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700206}
207
208/* Deal with the reporting of failure conditions on a filesystem such as
209 * inconsistencies detected or read IO failures.
210 *
211 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700212 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213 * write ordering constraints on the superblock which prevent us from
214 * writing it out straight away; and given that the journal is about to
215 * be aborted, we can't rely on the current, or future, transactions to
216 * write out the superblock safely.
217 *
Mingming Caodab291a2006-10-11 01:21:01 -0700218 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219 * the journal instead. On recovery, the journal will compain about
220 * that error until we've noted it down and cleared it.
221 */
222
Mingming Cao617ba132006-10-11 01:20:53 -0700223static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224{
Mingming Cao617ba132006-10-11 01:20:53 -0700225 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226
Mingming Cao617ba132006-10-11 01:20:53 -0700227 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
228 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229
230 if (sb->s_flags & MS_RDONLY)
231 return;
232
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400233 if (!test_opt(sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700234 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235
Mingming Cao617ba132006-10-11 01:20:53 -0700236 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700238 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700239 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400240 if (test_opt(sb, ERRORS_RO)) {
241 printk(KERN_CRIT "Remounting filesystem read-only\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 sb->s_flags |= MS_RDONLY;
243 }
Mingming Cao617ba132006-10-11 01:20:53 -0700244 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700246 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700247 sb->s_id);
248}
249
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400250void ext4_error(struct super_block *sb, const char *function,
251 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252{
253 va_list args;
254
255 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400256 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257 vprintk(fmt, args);
258 printk("\n");
259 va_end(args);
260
Mingming Cao617ba132006-10-11 01:20:53 -0700261 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262}
263
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400264static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265 char nbuf[16])
266{
267 char *errstr = NULL;
268
269 switch (errno) {
270 case -EIO:
271 errstr = "IO failure";
272 break;
273 case -ENOMEM:
274 errstr = "Out of memory";
275 break;
276 case -EROFS:
Mingming Caodab291a2006-10-11 01:21:01 -0700277 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 errstr = "Journal has aborted";
279 else
280 errstr = "Readonly filesystem";
281 break;
282 default:
283 /* If the caller passed in an extra buffer for unknown
284 * errors, textualise them now. Else we just return
285 * NULL. */
286 if (nbuf) {
287 /* Check for truncated error codes... */
288 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
289 errstr = nbuf;
290 }
291 break;
292 }
293
294 return errstr;
295}
296
Mingming Cao617ba132006-10-11 01:20:53 -0700297/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298 * automatically and invokes the appropriate error response. */
299
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400300void __ext4_std_error(struct super_block *sb, const char *function, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301{
302 char nbuf[16];
303 const char *errstr;
304
305 /* Special case: if the error is EROFS, and we're not already
306 * inside a transaction, then there's really no point in logging
307 * an error. */
308 if (errno == -EROFS && journal_current_handle() == NULL &&
309 (sb->s_flags & MS_RDONLY))
310 return;
311
Mingming Cao617ba132006-10-11 01:20:53 -0700312 errstr = ext4_decode_error(sb, errno, nbuf);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400313 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
314 sb->s_id, function, errstr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315
Mingming Cao617ba132006-10-11 01:20:53 -0700316 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317}
318
319/*
Mingming Cao617ba132006-10-11 01:20:53 -0700320 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 * abort function may be used to deal with unrecoverable failures such
322 * as journal IO errors or ENOMEM at a critical moment in log management.
323 *
324 * We unconditionally force the filesystem into an ABORT|READONLY state,
325 * unless the error response on the fs has been set to panic in which
326 * case we take the easy way out and panic immediately.
327 */
328
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400329void ext4_abort(struct super_block *sb, const char *function,
330 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331{
332 va_list args;
333
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400334 printk(KERN_CRIT "ext4_abort called.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700335
336 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400337 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338 vprintk(fmt, args);
339 printk("\n");
340 va_end(args);
341
342 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700343 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700344
345 if (sb->s_flags & MS_RDONLY)
346 return;
347
348 printk(KERN_CRIT "Remounting filesystem read-only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700349 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700350 sb->s_flags |= MS_RDONLY;
Mingming Cao617ba132006-10-11 01:20:53 -0700351 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400352 if (EXT4_SB(sb)->s_journal)
353 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700354}
355
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400356void ext4_warning(struct super_block *sb, const char *function,
357 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358{
359 va_list args;
360
361 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700362 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363 sb->s_id, function);
364 vprintk(fmt, args);
365 printk("\n");
366 va_end(args);
367}
368
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500369void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
370 const char *function, const char *fmt, ...)
371__releases(bitlock)
372__acquires(bitlock)
373{
374 va_list args;
375 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
376
377 va_start(args, fmt);
378 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
379 vprintk(fmt, args);
380 printk("\n");
381 va_end(args);
382
383 if (test_opt(sb, ERRORS_CONT)) {
384 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
385 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
386 ext4_commit_super(sb, es, 0);
387 return;
388 }
389 ext4_unlock_group(sb, grp);
390 ext4_handle_error(sb);
391 /*
392 * We only get here in the ERRORS_RO case; relocking the group
393 * may be dangerous, but nothing bad will happen since the
394 * filesystem will have already been marked read/only and the
395 * journal has been aborted. We return 1 as a hint to callers
396 * who might what to use the return value from
397 * ext4_grp_locked_error() to distinguish beween the
398 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
399 * aggressively from the ext4 function in question, with a
400 * more appropriate error code.
401 */
402 ext4_lock_group(sb, grp);
403 return;
404}
405
406
Mingming Cao617ba132006-10-11 01:20:53 -0700407void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408{
Mingming Cao617ba132006-10-11 01:20:53 -0700409 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410
Mingming Cao617ba132006-10-11 01:20:53 -0700411 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700412 return;
413
Harvey Harrison46e665e2008-04-17 10:38:59 -0400414 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700415 "updating to rev %d because of new feature flag, "
416 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700417 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700418
Mingming Cao617ba132006-10-11 01:20:53 -0700419 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
420 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
421 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422 /* leave es->s_feature_*compat flags alone */
423 /* es->s_uuid will be set by e2fsck if empty */
424
425 /*
426 * The rest of the superblock fields should be zero, and if not it
427 * means they are likely already in use, so leave them alone. We
428 * can leave it up to e2fsck to clean up any inconsistencies there.
429 */
430}
431
432/*
433 * Open the external journal device
434 */
Mingming Cao617ba132006-10-11 01:20:53 -0700435static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436{
437 struct block_device *bdev;
438 char b[BDEVNAME_SIZE];
439
440 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
441 if (IS_ERR(bdev))
442 goto fail;
443 return bdev;
444
445fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700446 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 __bdevname(dev, b), PTR_ERR(bdev));
448 return NULL;
449}
450
451/*
452 * Release the journal device
453 */
Mingming Cao617ba132006-10-11 01:20:53 -0700454static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455{
456 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500457 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458}
459
Mingming Cao617ba132006-10-11 01:20:53 -0700460static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461{
462 struct block_device *bdev;
463 int ret = -ENODEV;
464
465 bdev = sbi->journal_bdev;
466 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700467 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 sbi->journal_bdev = NULL;
469 }
470 return ret;
471}
472
473static inline struct inode *orphan_list_entry(struct list_head *l)
474{
Mingming Cao617ba132006-10-11 01:20:53 -0700475 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476}
477
Mingming Cao617ba132006-10-11 01:20:53 -0700478static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479{
480 struct list_head *l;
481
482 printk(KERN_ERR "sb orphan head is %d\n",
483 le32_to_cpu(sbi->s_es->s_last_orphan));
484
485 printk(KERN_ERR "sb_info orphan list:\n");
486 list_for_each(l, &sbi->s_orphan) {
487 struct inode *inode = orphan_list_entry(l);
488 printk(KERN_ERR " "
489 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
490 inode->i_sb->s_id, inode->i_ino, inode,
491 inode->i_mode, inode->i_nlink,
492 NEXT_ORPHAN(inode));
493 }
494}
495
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400496static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497{
Mingming Cao617ba132006-10-11 01:20:53 -0700498 struct ext4_sb_info *sbi = EXT4_SB(sb);
499 struct ext4_super_block *es = sbi->s_es;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400500 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501
Alex Tomasc9de5602008-01-29 00:19:52 -0500502 ext4_mb_release(sb);
Alex Tomasa86c6182006-10-11 01:21:03 -0700503 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700504 ext4_xattr_put_super(sb);
Frank Mayhar03901312009-01-07 00:06:22 -0500505 if (sbi->s_journal) {
506 err = jbd2_journal_destroy(sbi->s_journal);
507 sbi->s_journal = NULL;
508 if (err < 0)
509 ext4_abort(sb, __func__,
510 "Couldn't clean up the journal");
511 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700513 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 es->s_state = cpu_to_le16(sbi->s_mount_state);
Mingming Cao617ba132006-10-11 01:20:53 -0700515 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400517 if (sbi->s_proc) {
518 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400519 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400520 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521
522 for (i = 0; i < sbi->s_gdb_count; i++)
523 brelse(sbi->s_group_desc[i]);
524 kfree(sbi->s_group_desc);
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400525 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526 percpu_counter_destroy(&sbi->s_freeblocks_counter);
527 percpu_counter_destroy(&sbi->s_freeinodes_counter);
528 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400529 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530 brelse(sbi->s_sbh);
531#ifdef CONFIG_QUOTA
532 for (i = 0; i < MAXQUOTAS; i++)
533 kfree(sbi->s_qf_names[i]);
534#endif
535
536 /* Debugging code just in case the in-memory inode orphan list
537 * isn't empty. The on-disk one can be non-empty if we've
538 * detected an error and taken the fs readonly, but the
539 * in-memory list had better be clean by this point. */
540 if (!list_empty(&sbi->s_orphan))
541 dump_orphan_list(sb, sbi);
542 J_ASSERT(list_empty(&sbi->s_orphan));
543
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700544 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
546 /*
547 * Invalidate the journal device's buffers. We don't want them
548 * floating about in memory - the physical journal device may
549 * hotswapped, and it breaks the `ro-after' testing code.
550 */
551 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700552 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700553 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 }
555 sb->s_fs_info = NULL;
556 kfree(sbi);
557 return;
558}
559
Christoph Lametere18b8902006-12-06 20:33:20 -0800560static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561
562/*
563 * Called inside transaction, so use GFP_NOFS
564 */
Mingming Cao617ba132006-10-11 01:20:53 -0700565static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566{
Mingming Cao617ba132006-10-11 01:20:53 -0700567 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800569 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570 if (!ei)
571 return NULL;
Theodore Ts'o03010a32008-10-10 20:02:48 -0400572#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700573 ei->i_acl = EXT4_ACL_NOT_CACHED;
574 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700575#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700576 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400577 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700578 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500579 INIT_LIST_HEAD(&ei->i_prealloc_list);
580 spin_lock_init(&ei->i_prealloc_lock);
Frank Mayhar03901312009-01-07 00:06:22 -0500581 /*
582 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
583 * therefore it can be null here. Don't check it, just initialize
584 * jinode.
585 */
Jan Kara678aaf42008-07-11 19:27:31 -0400586 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400587 ei->i_reserved_data_blocks = 0;
588 ei->i_reserved_meta_blocks = 0;
589 ei->i_allocated_meta_blocks = 0;
590 ei->i_delalloc_reserved_flag = 0;
591 spin_lock_init(&(ei->i_block_reservation_lock));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700592 return &ei->vfs_inode;
593}
594
Mingming Cao617ba132006-10-11 01:20:53 -0700595static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700597 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
598 printk("EXT4 Inode %p: orphan list check failed!\n",
599 EXT4_I(inode));
600 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
601 EXT4_I(inode), sizeof(struct ext4_inode_info),
602 true);
603 dump_stack();
604 }
Mingming Cao617ba132006-10-11 01:20:53 -0700605 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606}
607
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700608static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700609{
Mingming Cao617ba132006-10-11 01:20:53 -0700610 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611
Christoph Lametera35afb82007-05-16 22:10:57 -0700612 INIT_LIST_HEAD(&ei->i_orphan);
Theodore Ts'o03010a32008-10-10 20:02:48 -0400613#ifdef CONFIG_EXT4_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700614 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700615#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500616 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700617 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700618}
619
620static int init_inodecache(void)
621{
Mingming Cao617ba132006-10-11 01:20:53 -0700622 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
623 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624 0, (SLAB_RECLAIM_ACCOUNT|
625 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900626 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700627 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 return -ENOMEM;
629 return 0;
630}
631
632static void destroy_inodecache(void)
633{
Mingming Cao617ba132006-10-11 01:20:53 -0700634 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700635}
636
Mingming Cao617ba132006-10-11 01:20:53 -0700637static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638{
Theodore Ts'o03010a32008-10-10 20:02:48 -0400639#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700640 if (EXT4_I(inode)->i_acl &&
641 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
642 posix_acl_release(EXT4_I(inode)->i_acl);
643 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700644 }
Mingming Cao617ba132006-10-11 01:20:53 -0700645 if (EXT4_I(inode)->i_default_acl &&
646 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
647 posix_acl_release(EXT4_I(inode)->i_default_acl);
648 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649 }
650#endif
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400651 ext4_discard_preallocations(inode);
Frank Mayhar03901312009-01-07 00:06:22 -0500652 if (EXT4_JOURNAL(inode))
653 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
Jan Kara678aaf42008-07-11 19:27:31 -0400654 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655}
656
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400657static inline void ext4_show_quota_options(struct seq_file *seq,
658 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659{
660#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700661 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700662
663 if (sbi->s_jquota_fmt)
664 seq_printf(seq, ",jqfmt=%s",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400665 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700666
667 if (sbi->s_qf_names[USRQUOTA])
668 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
669
670 if (sbi->s_qf_names[GRPQUOTA])
671 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
672
Mingming Cao617ba132006-10-11 01:20:53 -0700673 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674 seq_puts(seq, ",usrquota");
675
Mingming Cao617ba132006-10-11 01:20:53 -0700676 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 seq_puts(seq, ",grpquota");
678#endif
679}
680
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700681/*
682 * Show an option if
683 * - it's set to a non-default value OR
684 * - if the per-sb default is different from the global default
685 */
Mingming Cao617ba132006-10-11 01:20:53 -0700686static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500688 int def_errors;
689 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700690 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700691 struct ext4_sb_info *sbi = EXT4_SB(sb);
692 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700693
694 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500695 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700696
697 if (sbi->s_sb_block != 1)
698 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
699 if (test_opt(sb, MINIX_DF))
700 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500701 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700702 seq_puts(seq, ",grpid");
703 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
704 seq_puts(seq, ",nogrpid");
705 if (sbi->s_resuid != EXT4_DEF_RESUID ||
706 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
707 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
708 }
709 if (sbi->s_resgid != EXT4_DEF_RESGID ||
710 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
711 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
712 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500713 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700714 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500715 def_errors == EXT4_ERRORS_CONTINUE) {
716 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700717 }
718 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500719 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500720 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500721 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700722 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500723 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700724 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500725 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700726 seq_puts(seq, ",debug");
727 if (test_opt(sb, OLDALLOC))
728 seq_puts(seq, ",oldalloc");
Theodore Ts'o03010a32008-10-10 20:02:48 -0400729#ifdef CONFIG_EXT4_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500730 if (test_opt(sb, XATTR_USER) &&
731 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700732 seq_puts(seq, ",user_xattr");
733 if (!test_opt(sb, XATTR_USER) &&
734 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
735 seq_puts(seq, ",nouser_xattr");
736 }
737#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400738#ifdef CONFIG_EXT4_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500739 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700740 seq_puts(seq, ",acl");
741 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
742 seq_puts(seq, ",noacl");
743#endif
744 if (!test_opt(sb, RESERVATION))
745 seq_puts(seq, ",noreservation");
Theodore Ts'o30773842009-01-03 20:27:38 -0500746 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700747 seq_printf(seq, ",commit=%u",
748 (unsigned) (sbi->s_commit_interval / HZ));
749 }
Theodore Ts'o30773842009-01-03 20:27:38 -0500750 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
751 seq_printf(seq, ",min_batch_time=%u",
752 (unsigned) sbi->s_min_batch_time);
753 }
754 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
755 seq_printf(seq, ",max_batch_time=%u",
756 (unsigned) sbi->s_min_batch_time);
757 }
758
Eric Sandeen571640c2008-05-26 12:29:46 -0400759 /*
760 * We're changing the default of barrier mount option, so
761 * let's always display its mount state so it's clear what its
762 * status is.
763 */
764 seq_puts(seq, ",barrier=");
765 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400766 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
767 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700768 if (test_opt(sb, NOBH))
769 seq_puts(seq, ",nobh");
770 if (!test_opt(sb, EXTENTS))
771 seq_puts(seq, ",noextents");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500772 if (test_opt(sb, I_VERSION))
773 seq_puts(seq, ",i_version");
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400774 if (!test_opt(sb, DELALLOC))
775 seq_puts(seq, ",nodelalloc");
776
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500778 if (sbi->s_stripe)
779 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500780 /*
781 * journal mode get enabled in different ways
782 * So just print the value even if we didn't specify it
783 */
Mingming Cao617ba132006-10-11 01:20:53 -0700784 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700786 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700788 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789 seq_puts(seq, ",data=writeback");
790
Theodore Ts'o240799c2008-10-09 23:53:47 -0400791 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
792 seq_printf(seq, ",inode_readahead_blks=%u",
793 sbi->s_inode_readahead_blks);
794
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400795 if (test_opt(sb, DATA_ERR_ABORT))
796 seq_puts(seq, ",data_err=abort");
797
Mingming Cao617ba132006-10-11 01:20:53 -0700798 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700799 return 0;
800}
801
802
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700803static struct inode *ext4_nfs_get_inode(struct super_block *sb,
804 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700805{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807
Mingming Cao617ba132006-10-11 01:20:53 -0700808 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700810 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 return ERR_PTR(-ESTALE);
812
813 /* iget isn't really right if the inode is currently unallocated!!
814 *
Mingming Cao617ba132006-10-11 01:20:53 -0700815 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816 * deleted, so we should be safe.
817 *
818 * Currently we don't know the generation for parent directory, so
819 * a generation of 0 means "accept any"
820 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800821 inode = ext4_iget(sb, ino);
822 if (IS_ERR(inode))
823 return ERR_CAST(inode);
824 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700825 iput(inode);
826 return ERR_PTR(-ESTALE);
827 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700828
829 return inode;
830}
831
832static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
833 int fh_len, int fh_type)
834{
835 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
836 ext4_nfs_get_inode);
837}
838
839static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
840 int fh_len, int fh_type)
841{
842 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
843 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844}
845
846#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400847#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400848#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700849
Mingming Cao617ba132006-10-11 01:20:53 -0700850static int ext4_dquot_initialize(struct inode *inode, int type);
851static int ext4_dquot_drop(struct inode *inode);
852static int ext4_write_dquot(struct dquot *dquot);
853static int ext4_acquire_dquot(struct dquot *dquot);
854static int ext4_release_dquot(struct dquot *dquot);
855static int ext4_mark_dquot_dirty(struct dquot *dquot);
856static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -0700857static int ext4_quota_on(struct super_block *sb, int type, int format_id,
858 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -0700859static int ext4_quota_on_mount(struct super_block *sb, int type);
860static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700861 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700862static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863 const char *data, size_t len, loff_t off);
864
Mingming Cao617ba132006-10-11 01:20:53 -0700865static struct dquot_operations ext4_quota_operations = {
866 .initialize = ext4_dquot_initialize,
867 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868 .alloc_space = dquot_alloc_space,
869 .alloc_inode = dquot_alloc_inode,
870 .free_space = dquot_free_space,
871 .free_inode = dquot_free_inode,
872 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700873 .write_dquot = ext4_write_dquot,
874 .acquire_dquot = ext4_acquire_dquot,
875 .release_dquot = ext4_release_dquot,
876 .mark_dirty = ext4_mark_dquot_dirty,
877 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878};
879
Mingming Cao617ba132006-10-11 01:20:53 -0700880static struct quotactl_ops ext4_qctl_operations = {
881 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 .quota_off = vfs_quota_off,
883 .quota_sync = vfs_quota_sync,
884 .get_info = vfs_get_dqinfo,
885 .set_info = vfs_set_dqinfo,
886 .get_dqblk = vfs_get_dqblk,
887 .set_dqblk = vfs_set_dqblk
888};
889#endif
890
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800891static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700892 .alloc_inode = ext4_alloc_inode,
893 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700894 .write_inode = ext4_write_inode,
895 .dirty_inode = ext4_dirty_inode,
896 .delete_inode = ext4_delete_inode,
897 .put_super = ext4_put_super,
898 .write_super = ext4_write_super,
899 .sync_fs = ext4_sync_fs,
900 .write_super_lockfs = ext4_write_super_lockfs,
901 .unlockfs = ext4_unlockfs,
902 .statfs = ext4_statfs,
903 .remount_fs = ext4_remount,
904 .clear_inode = ext4_clear_inode,
905 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700907 .quota_read = ext4_quota_read,
908 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909#endif
910};
911
Christoph Hellwig39655162007-10-21 16:42:17 -0700912static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700913 .fh_to_dentry = ext4_fh_to_dentry,
914 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -0700915 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916};
917
918enum {
919 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
920 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -0400921 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
923 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
Theodore Ts'o30773842009-01-03 20:27:38 -0500924 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
925 Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -0500926 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400928 Opt_data_err_abort, Opt_data_err_ignore,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700929 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
930 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
931 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500932 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -0400933 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o240799c2008-10-09 23:53:47 -0400934 Opt_inode_readahead_blks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935};
936
Steven Whitehousea447c092008-10-13 10:46:57 +0100937static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700938 {Opt_bsd_df, "bsddf"},
939 {Opt_minix_df, "minixdf"},
940 {Opt_grpid, "grpid"},
941 {Opt_grpid, "bsdgroups"},
942 {Opt_nogrpid, "nogrpid"},
943 {Opt_nogrpid, "sysvgroups"},
944 {Opt_resgid, "resgid=%u"},
945 {Opt_resuid, "resuid=%u"},
946 {Opt_sb, "sb=%u"},
947 {Opt_err_cont, "errors=continue"},
948 {Opt_err_panic, "errors=panic"},
949 {Opt_err_ro, "errors=remount-ro"},
950 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 {Opt_debug, "debug"},
952 {Opt_oldalloc, "oldalloc"},
953 {Opt_orlov, "orlov"},
954 {Opt_user_xattr, "user_xattr"},
955 {Opt_nouser_xattr, "nouser_xattr"},
956 {Opt_acl, "acl"},
957 {Opt_noacl, "noacl"},
958 {Opt_reservation, "reservation"},
959 {Opt_noreservation, "noreservation"},
960 {Opt_noload, "noload"},
961 {Opt_nobh, "nobh"},
962 {Opt_bh, "bh"},
963 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -0500964 {Opt_min_batch_time, "min_batch_time=%u"},
965 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 {Opt_journal_update, "journal=update"},
967 {Opt_journal_inum, "journal=%u"},
968 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -0500969 {Opt_journal_checksum, "journal_checksum"},
970 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971 {Opt_abort, "abort"},
972 {Opt_data_journal, "data=journal"},
973 {Opt_data_ordered, "data=ordered"},
974 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400975 {Opt_data_err_abort, "data_err=abort"},
976 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700977 {Opt_offusrjquota, "usrjquota="},
978 {Opt_usrjquota, "usrjquota=%s"},
979 {Opt_offgrpjquota, "grpjquota="},
980 {Opt_grpjquota, "grpjquota=%s"},
981 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
982 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
983 {Opt_grpquota, "grpquota"},
984 {Opt_noquota, "noquota"},
985 {Opt_quota, "quota"},
986 {Opt_usrquota, "usrquota"},
987 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -0700988 {Opt_extents, "extents"},
Mingming Cao1e2462f2007-07-18 09:00:55 -0400989 {Opt_noextents, "noextents"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500990 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -0500991 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -0400993 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400994 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o240799c2008-10-09 23:53:47 -0400995 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -0400996 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700997};
998
Mingming Cao617ba132006-10-11 01:20:53 -0700999static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000{
Mingming Cao617ba132006-10-11 01:20:53 -07001001 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002 char *options = (char *) *data;
1003
1004 if (!options || strncmp(options, "sb=", 3) != 0)
1005 return 1; /* Default location */
1006 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -07001007 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001008 sb_block = simple_strtoul(options, &options, 0);
1009 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001010 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001011 (char *) *data);
1012 return 1;
1013 }
1014 if (*options == ',')
1015 options++;
1016 *data = (void *) options;
1017 return sb_block;
1018}
1019
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001020static int parse_options(char *options, struct super_block *sb,
1021 unsigned int *inum, unsigned long *journal_devnum,
1022 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001023{
Mingming Cao617ba132006-10-11 01:20:53 -07001024 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001025 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001026 substring_t args[MAX_OPT_ARGS];
1027 int data_opt = 0;
1028 int option;
1029#ifdef CONFIG_QUOTA
Jan Karadfc5d032008-05-13 19:11:51 -04001030 int qtype, qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001031 char *qname;
1032#endif
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001033 ext4_fsblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034
1035 if (!options)
1036 return 1;
1037
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001038 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001039 int token;
1040 if (!*p)
1041 continue;
1042
1043 token = match_token(p, tokens, args);
1044 switch (token) {
1045 case Opt_bsd_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001046 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 break;
1048 case Opt_minix_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001049 set_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 break;
1051 case Opt_grpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001052 set_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 break;
1054 case Opt_nogrpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001055 clear_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 break;
1057 case Opt_resuid:
1058 if (match_int(&args[0], &option))
1059 return 0;
1060 sbi->s_resuid = option;
1061 break;
1062 case Opt_resgid:
1063 if (match_int(&args[0], &option))
1064 return 0;
1065 sbi->s_resgid = option;
1066 break;
1067 case Opt_sb:
1068 /* handled by get_sb_block() instead of here */
1069 /* *sb_block = match_int(&args[0]); */
1070 break;
1071 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001072 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1073 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1074 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075 break;
1076 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001077 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1078 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1079 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001080 break;
1081 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001082 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1083 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1084 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001085 break;
1086 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001087 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001088 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001090 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091 break;
1092 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001093 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001094 break;
1095 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001096 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001097 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001098#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001099 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001100 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001101 break;
1102 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001103 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104 break;
1105#else
1106 case Opt_user_xattr:
1107 case Opt_nouser_xattr:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001108 printk(KERN_ERR "EXT4 (no)user_xattr options "
1109 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001110 break;
1111#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001112#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001113 case Opt_acl:
1114 set_opt(sbi->s_mount_opt, POSIX_ACL);
1115 break;
1116 case Opt_noacl:
1117 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1118 break;
1119#else
1120 case Opt_acl:
1121 case Opt_noacl:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001122 printk(KERN_ERR "EXT4 (no)acl options "
1123 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 break;
1125#endif
1126 case Opt_reservation:
1127 set_opt(sbi->s_mount_opt, RESERVATION);
1128 break;
1129 case Opt_noreservation:
1130 clear_opt(sbi->s_mount_opt, RESERVATION);
1131 break;
1132 case Opt_journal_update:
1133 /* @@@ FIXME */
1134 /* Eventually we will want to be able to create
1135 a journal file here. For now, only allow the
1136 user to specify an existing inode to be the
1137 journal file. */
1138 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001139 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001140 "journal on remount\n");
1141 return 0;
1142 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001143 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001144 break;
1145 case Opt_journal_inum:
1146 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001147 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 "journal on remount\n");
1149 return 0;
1150 }
1151 if (match_int(&args[0], &option))
1152 return 0;
1153 *inum = option;
1154 break;
1155 case Opt_journal_dev:
1156 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001157 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001158 "journal on remount\n");
1159 return 0;
1160 }
1161 if (match_int(&args[0], &option))
1162 return 0;
1163 *journal_devnum = option;
1164 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001165 case Opt_journal_checksum:
1166 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1167 break;
1168 case Opt_journal_async_commit:
1169 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1170 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1171 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001173 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 break;
1175 case Opt_commit:
1176 if (match_int(&args[0], &option))
1177 return 0;
1178 if (option < 0)
1179 return 0;
1180 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001181 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001182 sbi->s_commit_interval = HZ * option;
1183 break;
Theodore Ts'o30773842009-01-03 20:27:38 -05001184 case Opt_max_batch_time:
1185 if (match_int(&args[0], &option))
1186 return 0;
1187 if (option < 0)
1188 return 0;
1189 if (option == 0)
1190 option = EXT4_DEF_MAX_BATCH_TIME;
1191 sbi->s_max_batch_time = option;
1192 break;
1193 case Opt_min_batch_time:
1194 if (match_int(&args[0], &option))
1195 return 0;
1196 if (option < 0)
1197 return 0;
1198 sbi->s_min_batch_time = option;
1199 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001200 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001201 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001202 goto datacheck;
1203 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001204 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001205 goto datacheck;
1206 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001207 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 datacheck:
1209 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001210 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001211 != data_opt) {
1212 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001213 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001214 "mode on remount\n");
1215 return 0;
1216 }
1217 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001218 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219 sbi->s_mount_opt |= data_opt;
1220 }
1221 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001222 case Opt_data_err_abort:
1223 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1224 break;
1225 case Opt_data_err_ignore:
1226 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1227 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001228#ifdef CONFIG_QUOTA
1229 case Opt_usrjquota:
1230 qtype = USRQUOTA;
1231 goto set_qf_name;
1232 case Opt_grpjquota:
1233 qtype = GRPQUOTA;
1234set_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001235 if ((sb_any_quota_enabled(sb) ||
1236 sb_any_quota_suspended(sb)) &&
1237 !sbi->s_qf_names[qtype]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238 printk(KERN_ERR
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001239 "EXT4-fs: Cannot change journaled "
1240 "quota options when quota turned on.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001241 return 0;
1242 }
1243 qname = match_strdup(&args[0]);
1244 if (!qname) {
1245 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001246 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001247 "storing quotafile name.\n");
1248 return 0;
1249 }
1250 if (sbi->s_qf_names[qtype] &&
1251 strcmp(sbi->s_qf_names[qtype], qname)) {
1252 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001253 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254 "specified.\n", QTYPE2NAME(qtype));
1255 kfree(qname);
1256 return 0;
1257 }
1258 sbi->s_qf_names[qtype] = qname;
1259 if (strchr(sbi->s_qf_names[qtype], '/')) {
1260 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001261 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001262 "filesystem root.\n");
1263 kfree(sbi->s_qf_names[qtype]);
1264 sbi->s_qf_names[qtype] = NULL;
1265 return 0;
1266 }
1267 set_opt(sbi->s_mount_opt, QUOTA);
1268 break;
1269 case Opt_offusrjquota:
1270 qtype = USRQUOTA;
1271 goto clear_qf_name;
1272 case Opt_offgrpjquota:
1273 qtype = GRPQUOTA;
1274clear_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001275 if ((sb_any_quota_enabled(sb) ||
1276 sb_any_quota_suspended(sb)) &&
1277 sbi->s_qf_names[qtype]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001278 printk(KERN_ERR "EXT4-fs: Cannot change "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001279 "journaled quota options when "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001280 "quota turned on.\n");
1281 return 0;
1282 }
1283 /*
1284 * The space will be released later when all options
1285 * are confirmed to be correct
1286 */
1287 sbi->s_qf_names[qtype] = NULL;
1288 break;
1289 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001290 qfmt = QFMT_VFS_OLD;
1291 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001293 qfmt = QFMT_VFS_V0;
1294set_qf_format:
1295 if ((sb_any_quota_enabled(sb) ||
1296 sb_any_quota_suspended(sb)) &&
1297 sbi->s_jquota_fmt != qfmt) {
1298 printk(KERN_ERR "EXT4-fs: Cannot change "
1299 "journaled quota options when "
1300 "quota turned on.\n");
1301 return 0;
1302 }
1303 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001304 break;
1305 case Opt_quota:
1306 case Opt_usrquota:
1307 set_opt(sbi->s_mount_opt, QUOTA);
1308 set_opt(sbi->s_mount_opt, USRQUOTA);
1309 break;
1310 case Opt_grpquota:
1311 set_opt(sbi->s_mount_opt, QUOTA);
1312 set_opt(sbi->s_mount_opt, GRPQUOTA);
1313 break;
1314 case Opt_noquota:
1315 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001316 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001317 "options when quota turned on.\n");
1318 return 0;
1319 }
1320 clear_opt(sbi->s_mount_opt, QUOTA);
1321 clear_opt(sbi->s_mount_opt, USRQUOTA);
1322 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1323 break;
1324#else
1325 case Opt_quota:
1326 case Opt_usrquota:
1327 case Opt_grpquota:
Jan Karacd59e7b2008-05-13 19:11:51 -04001328 printk(KERN_ERR
1329 "EXT4-fs: quota options not supported.\n");
1330 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001331 case Opt_usrjquota:
1332 case Opt_grpjquota:
1333 case Opt_offusrjquota:
1334 case Opt_offgrpjquota:
1335 case Opt_jqfmt_vfsold:
1336 case Opt_jqfmt_vfsv0:
1337 printk(KERN_ERR
Jan Karacd59e7b2008-05-13 19:11:51 -04001338 "EXT4-fs: journaled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001339 "supported.\n");
1340 break;
1341 case Opt_noquota:
1342 break;
1343#endif
1344 case Opt_abort:
1345 set_opt(sbi->s_mount_opt, ABORT);
1346 break;
1347 case Opt_barrier:
1348 if (match_int(&args[0], &option))
1349 return 0;
1350 if (option)
1351 set_opt(sbi->s_mount_opt, BARRIER);
1352 else
1353 clear_opt(sbi->s_mount_opt, BARRIER);
1354 break;
1355 case Opt_ignore:
1356 break;
1357 case Opt_resize:
1358 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001359 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001360 "for remount\n");
1361 return 0;
1362 }
1363 if (match_int(&args[0], &option) != 0)
1364 return 0;
1365 *n_blocks_count = option;
1366 break;
1367 case Opt_nobh:
1368 set_opt(sbi->s_mount_opt, NOBH);
1369 break;
1370 case Opt_bh:
1371 clear_opt(sbi->s_mount_opt, NOBH);
1372 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001373 case Opt_extents:
Eric Sandeene4079a12008-07-11 19:27:31 -04001374 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1375 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1376 ext4_warning(sb, __func__,
1377 "extents feature not enabled "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001378 "on this filesystem, use tune2fs");
Eric Sandeene4079a12008-07-11 19:27:31 -04001379 return 0;
1380 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001381 set_opt(sbi->s_mount_opt, EXTENTS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001382 break;
Mingming Cao1e2462f2007-07-18 09:00:55 -04001383 case Opt_noextents:
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001384 /*
1385 * When e2fsprogs support resizing an already existing
1386 * ext3 file system to greater than 2**32 we need to
1387 * add support to block allocator to handle growing
1388 * already existing block mapped inode so that blocks
1389 * allocated for them fall within 2**32
1390 */
1391 last_block = ext4_blocks_count(sbi->s_es) - 1;
1392 if (last_block > 0xffffffffULL) {
1393 printk(KERN_ERR "EXT4-fs: Filesystem too "
1394 "large to mount with "
1395 "-o noextents options\n");
1396 return 0;
1397 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001398 clear_opt(sbi->s_mount_opt, EXTENTS);
Mingming Cao1e2462f2007-07-18 09:00:55 -04001399 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001400 case Opt_i_version:
1401 set_opt(sbi->s_mount_opt, I_VERSION);
1402 sb->s_flags |= MS_I_VERSION;
1403 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001404 case Opt_nodelalloc:
1405 clear_opt(sbi->s_mount_opt, DELALLOC);
1406 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001407 case Opt_stripe:
1408 if (match_int(&args[0], &option))
1409 return 0;
1410 if (option < 0)
1411 return 0;
1412 sbi->s_stripe = option;
1413 break;
Alex Tomas64769242008-07-11 19:27:31 -04001414 case Opt_delalloc:
1415 set_opt(sbi->s_mount_opt, DELALLOC);
1416 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001417 case Opt_inode_readahead_blks:
1418 if (match_int(&args[0], &option))
1419 return 0;
1420 if (option < 0 || option > (1 << 30))
1421 return 0;
1422 sbi->s_inode_readahead_blks = option;
1423 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001424 default:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001425 printk(KERN_ERR
1426 "EXT4-fs: Unrecognized mount option \"%s\" "
1427 "or missing value\n", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001428 return 0;
1429 }
1430 }
1431#ifdef CONFIG_QUOTA
1432 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001433 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001434 sbi->s_qf_names[USRQUOTA])
1435 clear_opt(sbi->s_mount_opt, USRQUOTA);
1436
Mingming Cao617ba132006-10-11 01:20:53 -07001437 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438 sbi->s_qf_names[GRPQUOTA])
1439 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1440
1441 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001442 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001443 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001444 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1445 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001446 "format mixing.\n");
1447 return 0;
1448 }
1449
1450 if (!sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001451 printk(KERN_ERR "EXT4-fs: journaled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452 "not specified.\n");
1453 return 0;
1454 }
1455 } else {
1456 if (sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001457 printk(KERN_ERR "EXT4-fs: journaled quota format "
1458 "specified with no journaling "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 "enabled.\n");
1460 return 0;
1461 }
1462 }
1463#endif
1464 return 1;
1465}
1466
Mingming Cao617ba132006-10-11 01:20:53 -07001467static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468 int read_only)
1469{
Mingming Cao617ba132006-10-11 01:20:53 -07001470 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001471 int res = 0;
1472
Mingming Cao617ba132006-10-11 01:20:53 -07001473 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001474 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1475 "forcing read-only mode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476 res = MS_RDONLY;
1477 }
1478 if (read_only)
1479 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001480 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001481 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1482 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001483 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001484 printk(KERN_WARNING
1485 "EXT4-fs warning: mounting fs with errors, "
1486 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001487 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1488 le16_to_cpu(es->s_mnt_count) >=
1489 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001490 printk(KERN_WARNING
1491 "EXT4-fs warning: maximal mount count reached, "
1492 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001493 else if (le32_to_cpu(es->s_checkinterval) &&
1494 (le32_to_cpu(es->s_lastcheck) +
1495 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001496 printk(KERN_WARNING
1497 "EXT4-fs warning: checktime reached, "
1498 "running e2fsck is recommended\n");
Frank Mayhar03901312009-01-07 00:06:22 -05001499 if (!sbi->s_journal)
1500 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001502 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001503 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001505 ext4_update_dynamic_rev(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05001506 if (sbi->s_journal)
1507 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001508
Mingming Cao617ba132006-10-11 01:20:53 -07001509 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001511 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001512 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1513 sb->s_blocksize,
1514 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001515 EXT4_BLOCKS_PER_GROUP(sb),
1516 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001517 sbi->s_mount_opt);
1518
Frank Mayhar03901312009-01-07 00:06:22 -05001519 if (EXT4_SB(sb)->s_journal) {
1520 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1521 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1522 "external", EXT4_SB(sb)->s_journal->j_devname);
1523 } else {
1524 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1525 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001526 return res;
1527}
1528
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001529static int ext4_fill_flex_info(struct super_block *sb)
1530{
1531 struct ext4_sb_info *sbi = EXT4_SB(sb);
1532 struct ext4_group_desc *gdp = NULL;
1533 struct buffer_head *bh;
1534 ext4_group_t flex_group_count;
1535 ext4_group_t flex_group;
1536 int groups_per_flex = 0;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001537 int i;
1538
1539 if (!sbi->s_es->s_log_groups_per_flex) {
1540 sbi->s_log_groups_per_flex = 0;
1541 return 1;
1542 }
1543
1544 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1545 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1546
Frederic Bohec62a11f2008-09-08 10:20:24 -04001547 /* We allocate both existing and potentially added groups */
1548 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001549 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1550 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Li Zefanec05e862008-07-24 12:49:59 -04001551 sbi->s_flex_groups = kzalloc(flex_group_count *
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001552 sizeof(struct flex_groups), GFP_KERNEL);
1553 if (sbi->s_flex_groups == NULL) {
Li Zefanec05e862008-07-24 12:49:59 -04001554 printk(KERN_ERR "EXT4-fs: not enough memory for "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001555 "%u flex groups\n", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001556 goto failed;
1557 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001558
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001559 for (i = 0; i < sbi->s_groups_count; i++) {
1560 gdp = ext4_get_group_desc(sb, i, &bh);
1561
1562 flex_group = ext4_flex_group(sbi, i);
1563 sbi->s_flex_groups[flex_group].free_inodes +=
1564 le16_to_cpu(gdp->bg_free_inodes_count);
1565 sbi->s_flex_groups[flex_group].free_blocks +=
1566 le16_to_cpu(gdp->bg_free_blocks_count);
1567 }
1568
1569 return 1;
1570failed:
1571 return 0;
1572}
1573
Andreas Dilger717d50e2007-10-16 18:38:25 -04001574__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1575 struct ext4_group_desc *gdp)
1576{
1577 __u16 crc = 0;
1578
1579 if (sbi->s_es->s_feature_ro_compat &
1580 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1581 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1582 __le32 le_group = cpu_to_le32(block_group);
1583
1584 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1585 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1586 crc = crc16(crc, (__u8 *)gdp, offset);
1587 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1588 /* for checksum of struct ext4_group_desc do the rest...*/
1589 if ((sbi->s_es->s_feature_incompat &
1590 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1591 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1592 crc = crc16(crc, (__u8 *)gdp + offset,
1593 le16_to_cpu(sbi->s_es->s_desc_size) -
1594 offset);
1595 }
1596
1597 return cpu_to_le16(crc);
1598}
1599
1600int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1601 struct ext4_group_desc *gdp)
1602{
1603 if ((sbi->s_es->s_feature_ro_compat &
1604 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1605 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1606 return 0;
1607
1608 return 1;
1609}
1610
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001612static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613{
Mingming Cao617ba132006-10-11 01:20:53 -07001614 struct ext4_sb_info *sbi = EXT4_SB(sb);
1615 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1616 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001617 ext4_fsblk_t block_bitmap;
1618 ext4_fsblk_t inode_bitmap;
1619 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001620 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001621 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001622
Jose R. Santosce421582007-10-16 18:38:25 -04001623 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1624 flexbg_flag = 1;
1625
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001626 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001627
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001628 for (i = 0; i < sbi->s_groups_count; i++) {
1629 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1630
Jose R. Santosce421582007-10-16 18:38:25 -04001631 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001632 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001633 else
1634 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001635 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001636
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001637 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001638 if (block_bitmap < first_block || block_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001639 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001640 "Block bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001641 "(block %llu)!\n", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642 return 0;
1643 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001644 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001645 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001646 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001647 "Inode bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001648 "(block %llu)!\n", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649 return 0;
1650 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001651 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001652 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001653 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001654 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001655 "Inode table for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001656 "(block %llu)!\n", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657 return 0;
1658 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001659 spin_lock(sb_bgl_lock(sbi, i));
Andreas Dilger717d50e2007-10-16 18:38:25 -04001660 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001661 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001662 "Checksum for group %u failed (%u!=%u)\n",
Josef Bacikc19204b2008-04-29 22:00:28 -04001663 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1664 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001665 if (!(sb->s_flags & MS_RDONLY)) {
1666 spin_unlock(sb_bgl_lock(sbi, i));
Theodore Ts'o8a266462008-07-26 14:34:21 -04001667 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001668 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001669 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001670 spin_unlock(sb_bgl_lock(sbi, i));
Jose R. Santosce421582007-10-16 18:38:25 -04001671 if (!flexbg_flag)
1672 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001673 }
1674
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001675 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001676 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001677 return 1;
1678}
1679
Mingming Cao617ba132006-10-11 01:20:53 -07001680/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001681 * the superblock) which were deleted from all directories, but held open by
1682 * a process at the time of a crash. We walk the list and try to delete these
1683 * inodes at recovery time (only with a read-write filesystem).
1684 *
1685 * In order to keep the orphan inode chain consistent during traversal (in
1686 * case of crash during recovery), we link each inode into the superblock
1687 * orphan list_head and handle it the same way as an inode deletion during
1688 * normal operation (which journals the operations for us).
1689 *
1690 * We only do an iget() and an iput() on each inode, which is very safe if we
1691 * accidentally point at an in-use or already deleted inode. The worst that
1692 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001693 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694 * e2fsck was run on this filesystem, and it must have already done the orphan
1695 * inode cleanup for us, so we can safely abort without any further action.
1696 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001697static void ext4_orphan_cleanup(struct super_block *sb,
1698 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001699{
1700 unsigned int s_flags = sb->s_flags;
1701 int nr_orphans = 0, nr_truncates = 0;
1702#ifdef CONFIG_QUOTA
1703 int i;
1704#endif
1705 if (!es->s_last_orphan) {
1706 jbd_debug(4, "no orphan inodes to clean up\n");
1707 return;
1708 }
1709
Eric Sandeena8f48a92006-12-06 20:40:13 -08001710 if (bdev_read_only(sb->s_bdev)) {
1711 printk(KERN_ERR "EXT4-fs: write access "
1712 "unavailable, skipping orphan cleanup.\n");
1713 return;
1714 }
1715
Mingming Cao617ba132006-10-11 01:20:53 -07001716 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717 if (es->s_last_orphan)
1718 jbd_debug(1, "Errors on filesystem, "
1719 "clearing orphan list.\n");
1720 es->s_last_orphan = 0;
1721 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1722 return;
1723 }
1724
1725 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001726 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727 sb->s_id);
1728 sb->s_flags &= ~MS_RDONLY;
1729 }
1730#ifdef CONFIG_QUOTA
1731 /* Needed for iput() to work correctly and not trash data */
1732 sb->s_flags |= MS_ACTIVE;
1733 /* Turn on quotas so that they are updated correctly */
1734 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001735 if (EXT4_SB(sb)->s_qf_names[i]) {
1736 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737 if (ret < 0)
1738 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001739 "EXT4-fs: Cannot turn on journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001740 "quota: error %d\n", ret);
1741 }
1742 }
1743#endif
1744
1745 while (es->s_last_orphan) {
1746 struct inode *inode;
1747
Josef Bacik97bd42b2008-04-29 22:04:56 -04001748 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1749 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001750 es->s_last_orphan = 0;
1751 break;
1752 }
1753
Mingming Cao617ba132006-10-11 01:20:53 -07001754 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755 DQUOT_INIT(inode);
1756 if (inode->i_nlink) {
1757 printk(KERN_DEBUG
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001758 "%s: truncating inode %lu to %lld bytes\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001759 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001760 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001761 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001762 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001763 nr_truncates++;
1764 } else {
1765 printk(KERN_DEBUG
1766 "%s: deleting unreferenced inode %lu\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001767 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001768 jbd_debug(2, "deleting unreferenced inode %lu\n",
1769 inode->i_ino);
1770 nr_orphans++;
1771 }
1772 iput(inode); /* The delete magic happens here! */
1773 }
1774
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001775#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001776
1777 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001778 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779 sb->s_id, PLURAL(nr_orphans));
1780 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001781 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001782 sb->s_id, PLURAL(nr_truncates));
1783#ifdef CONFIG_QUOTA
1784 /* Turn quotas off */
1785 for (i = 0; i < MAXQUOTAS; i++) {
1786 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07001787 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001788 }
1789#endif
1790 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1791}
Eric Sandeencd2291a2008-01-28 23:58:27 -05001792/*
1793 * Maximal extent format file size.
1794 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1795 * extent format containers, within a sector_t, and within i_blocks
1796 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1797 * so that won't be a limiting factor.
1798 *
1799 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1800 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001801static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05001802{
1803 loff_t res;
1804 loff_t upper_limit = MAX_LFS_FILESIZE;
1805
1806 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001807 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05001808 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001809 * CONFIG_LBD is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05001810 * i_block represent total blocks in 512 bytes
1811 * 32 == size of vfs inode i_blocks * 8
1812 */
1813 upper_limit = (1LL << 32) - 1;
1814
1815 /* total blocks in file system block size */
1816 upper_limit >>= (blkbits - 9);
1817 upper_limit <<= blkbits;
1818 }
1819
1820 /* 32-bit extent-start container, ee_block */
1821 res = 1LL << 32;
1822 res <<= blkbits;
1823 res -= 1;
1824
1825 /* Sanity check against vm- & vfs- imposed limits */
1826 if (res > upper_limit)
1827 res = upper_limit;
1828
1829 return res;
1830}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001831
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001832/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05001833 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001834 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1835 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001837static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001838{
Mingming Cao617ba132006-10-11 01:20:53 -07001839 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001840 int meta_blocks;
1841 loff_t upper_limit;
1842 /* This is calculated to be the largest file size for a
Eric Sandeencd2291a2008-01-28 23:58:27 -05001843 * dense, bitmapped file such that the total number of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001844 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001845 * does not exceed 2^48 -1
1846 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1847 * total number of 512 bytes blocks of the file
1848 */
1849
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001850 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001851 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001852 * !has_huge_files or CONFIG_LBD is not enabled
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001853 * implies the inode i_block represent total blocks in
1854 * 512 bytes 32 == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001855 */
1856 upper_limit = (1LL << 32) - 1;
1857
1858 /* total blocks in file system block size */
1859 upper_limit >>= (bits - 9);
1860
1861 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05001862 /*
1863 * We use 48 bit ext4_inode i_blocks
1864 * With EXT4_HUGE_FILE_FL set the i_blocks
1865 * represent total number of blocks in
1866 * file system block size
1867 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001868 upper_limit = (1LL << 48) - 1;
1869
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001870 }
1871
1872 /* indirect blocks */
1873 meta_blocks = 1;
1874 /* double indirect blocks */
1875 meta_blocks += 1 + (1LL << (bits-2));
1876 /* tripple indirect blocks */
1877 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1878
1879 upper_limit -= meta_blocks;
1880 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001881
1882 res += 1LL << (bits-2);
1883 res += 1LL << (2*(bits-2));
1884 res += 1LL << (3*(bits-2));
1885 res <<= bits;
1886 if (res > upper_limit)
1887 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001888
1889 if (res > MAX_LFS_FILESIZE)
1890 res = MAX_LFS_FILESIZE;
1891
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001892 return res;
1893}
1894
Mingming Cao617ba132006-10-11 01:20:53 -07001895static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001896 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001897{
Mingming Cao617ba132006-10-11 01:20:53 -07001898 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001899 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001900 int has_super = 0;
1901
1902 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1903
Mingming Cao617ba132006-10-11 01:20:53 -07001904 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001905 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001906 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001907 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001908 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001909 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001910 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001911}
1912
Alex Tomasc9de5602008-01-29 00:19:52 -05001913/**
1914 * ext4_get_stripe_size: Get the stripe size.
1915 * @sbi: In memory super block info
1916 *
1917 * If we have specified it via mount option, then
1918 * use the mount option value. If the value specified at mount time is
1919 * greater than the blocks per group use the super block value.
1920 * If the super block value is greater than blocks per group return 0.
1921 * Allocator needs it be less than blocks per group.
1922 *
1923 */
1924static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1925{
1926 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1927 unsigned long stripe_width =
1928 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1929
1930 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1931 return sbi->s_stripe;
1932
1933 if (stripe_width <= sbi->s_blocks_per_group)
1934 return stripe_width;
1935
1936 if (stride <= sbi->s_blocks_per_group)
1937 return stride;
1938
1939 return 0;
1940}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001941
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001942static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04001943 __releases(kernel_lock)
1944 __acquires(kernel_lock)
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001945
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001946{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001947 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001948 struct ext4_super_block *es = NULL;
1949 struct ext4_sb_info *sbi;
1950 ext4_fsblk_t block;
1951 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001952 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001953 unsigned long offset = 0;
1954 unsigned int journal_inum = 0;
1955 unsigned long journal_devnum = 0;
1956 unsigned long def_mount_opts;
1957 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04001958 char *cp;
Frank Mayhar03901312009-01-07 00:06:22 -05001959 const char *descr;
David Howells1d1fe1e2008-02-07 00:15:37 -08001960 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001961 int blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001962 int db_count;
1963 int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001964 int needs_recovery, has_huge_files;
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05001965 int features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001966 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07001967 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001968
1969 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1970 if (!sbi)
1971 return -ENOMEM;
1972 sb->s_fs_info = sbi;
1973 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001974 sbi->s_resuid = EXT4_DEF_RESUID;
1975 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001976 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07001977 sbi->s_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001978
1979 unlock_kernel();
1980
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04001981 /* Cleanup superblock name */
1982 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
1983 *cp = '!';
1984
Mingming Cao617ba132006-10-11 01:20:53 -07001985 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001986 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07001987 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001988 goto out_fail;
1989 }
1990
1991 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001992 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001993 * block sizes. We need to calculate the offset from buffer start.
1994 */
Mingming Cao617ba132006-10-11 01:20:53 -07001995 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001996 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1997 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001998 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001999 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002000 }
2001
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002002 if (!(bh = sb_bread(sb, logical_sb_block))) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002003 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002004 goto out_fail;
2005 }
2006 /*
2007 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07002008 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002009 */
Mingming Cao617ba132006-10-11 01:20:53 -07002010 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002011 sbi->s_es = es;
2012 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07002013 if (sb->s_magic != EXT4_SUPER_MAGIC)
2014 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002015
2016 /* Set defaults before we parse the mount options */
2017 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07002018 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002019 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07002020 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002021 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07002022 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002023 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04002024#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07002025 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002026 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002027#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04002028#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07002029 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002030 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002031#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002032 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2033 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2034 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2035 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2036 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2037 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002038
Mingming Cao617ba132006-10-11 01:20:53 -07002039 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002040 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002041 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002042 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002043 else
2044 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002045
2046 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2047 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
Theodore Ts'o30773842009-01-03 20:27:38 -05002048 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2049 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2050 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002051
2052 set_opt(sbi->s_mount_opt, RESERVATION);
Eric Sandeen571640c2008-05-26 12:29:46 -04002053 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002054
Mingming Cao1e2462f2007-07-18 09:00:55 -04002055 /*
2056 * turn on extents feature by default in ext4 filesystem
Eric Sandeene4079a12008-07-11 19:27:31 -04002057 * only if feature flag already set by mkfs or tune2fs.
2058 * Use -o noextents to turn it off
Mingming Cao1e2462f2007-07-18 09:00:55 -04002059 */
Eric Sandeene4079a12008-07-11 19:27:31 -04002060 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2061 set_opt(sbi->s_mount_opt, EXTENTS);
2062 else
2063 ext4_warning(sb, __func__,
2064 "extents feature not enabled on this filesystem, "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05002065 "use tune2fs.");
Mingming Cao1e2462f2007-07-18 09:00:55 -04002066
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002067 /*
2068 * enable delayed allocation by default
2069 * Use -o nodelalloc to turn it off
2070 */
2071 set_opt(sbi->s_mount_opt, DELALLOC);
2072
2073
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002074 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2075 NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002076 goto failed_mount;
2077
2078 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002079 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002080
Mingming Cao617ba132006-10-11 01:20:53 -07002081 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2082 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2083 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2084 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002085 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002086 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002087 "running e2fsck is recommended\n");
Theodore Tso469108f2008-02-10 01:11:44 -05002088
2089 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002090 * Check feature flags regardless of the revision level, since we
2091 * previously didn't change the revision level when setting the flags,
2092 * so there is a chance incompat flags are set on a rev 0 filesystem.
2093 */
Mingming Cao617ba132006-10-11 01:20:53 -07002094 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002095 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002096 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002097 "unsupported optional features (%x).\n", sb->s_id,
2098 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2099 ~EXT4_FEATURE_INCOMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002100 goto failed_mount;
2101 }
Mingming Cao617ba132006-10-11 01:20:53 -07002102 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002103 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002104 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002105 "unsupported optional features (%x).\n", sb->s_id,
2106 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2107 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108 goto failed_mount;
2109 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002110 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2111 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2112 if (has_huge_files) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002113 /*
2114 * Large file size enabled file system can only be
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002115 * mount if kernel is build with CONFIG_LBD
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002116 */
2117 if (sizeof(root->i_blocks) < sizeof(u64) &&
2118 !(sb->s_flags & MS_RDONLY)) {
2119 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2120 "files cannot be mounted read-write "
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002121 "without CONFIG_LBD.\n", sb->s_id);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002122 goto failed_mount;
2123 }
2124 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002125 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2126
Mingming Cao617ba132006-10-11 01:20:53 -07002127 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2128 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002130 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002131 blocksize, sb->s_id);
2132 goto failed_mount;
2133 }
2134
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002136
2137 /* Validate the filesystem blocksize */
2138 if (!sb_set_blocksize(sb, blocksize)) {
2139 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2140 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002141 goto failed_mount;
2142 }
2143
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002144 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002145 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2146 offset = do_div(logical_sb_block, blocksize);
2147 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148 if (!bh) {
2149 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002150 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002151 goto failed_mount;
2152 }
Mingming Cao617ba132006-10-11 01:20:53 -07002153 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002154 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002155 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002156 printk(KERN_ERR
2157 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158 goto failed_mount;
2159 }
2160 }
2161
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002162 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2163 has_huge_files);
2164 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002165
Mingming Cao617ba132006-10-11 01:20:53 -07002166 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2167 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2168 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169 } else {
2170 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2171 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002172 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002173 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002174 (sbi->s_inode_size > blocksize)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002175 printk(KERN_ERR
2176 "EXT4-fs: unsupported inode size: %d\n",
2177 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002178 goto failed_mount;
2179 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002180 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2181 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002182 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002183 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2184 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002185 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002186 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002187 !is_power_of_2(sbi->s_desc_size)) {
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002188 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002189 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002190 sbi->s_desc_size);
2191 goto failed_mount;
2192 }
2193 } else
2194 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002195 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002196 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002197 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002198 goto cantfind_ext4;
2199 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002200 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002201 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002202 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2203 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002204 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002205 sbi->s_sbh = bh;
2206 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002207 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2208 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002209 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002210 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2211 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002212 i = le32_to_cpu(es->s_flags);
2213 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2214 sbi->s_hash_unsigned = 3;
2215 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2216#ifdef __CHAR_UNSIGNED__
2217 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2218 sbi->s_hash_unsigned = 3;
2219#else
2220 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2221#endif
2222 sb->s_dirt = 1;
2223 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224
2225 if (sbi->s_blocks_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002226 printk(KERN_ERR
2227 "EXT4-fs: #blocks per group too big: %lu\n",
2228 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002229 goto failed_mount;
2230 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002231 if (sbi->s_inodes_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002232 printk(KERN_ERR
2233 "EXT4-fs: #inodes per group too big: %lu\n",
2234 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002235 goto failed_mount;
2236 }
2237
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002238 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002239 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002240 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002241 " too large to mount safely\n", sb->s_id);
2242 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07002243 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002244 "enabled\n");
2245 goto failed_mount;
2246 }
2247
Mingming Cao617ba132006-10-11 01:20:53 -07002248 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2249 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002250
2251 /* ensure blocks_count calculation below doesn't sign-extend */
2252 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2253 le32_to_cpu(es->s_first_data_block) + 1) {
2254 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2255 "first data block %u, blocks per group %lu\n",
2256 ext4_blocks_count(es),
2257 le32_to_cpu(es->s_first_data_block),
2258 EXT4_BLOCKS_PER_GROUP(sb));
2259 goto failed_mount;
2260 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002261 blocks_count = (ext4_blocks_count(es) -
2262 le32_to_cpu(es->s_first_data_block) +
2263 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2264 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2265 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07002266 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2267 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002268 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002269 GFP_KERNEL);
2270 if (sbi->s_group_desc == NULL) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002271 printk(KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002272 goto failed_mount;
2273 }
2274
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002275#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002276 if (ext4_proc_root)
2277 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2278
Theodore Ts'o240799c2008-10-09 23:53:47 -04002279 if (sbi->s_proc)
2280 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2281 &ext4_ui_proc_fops,
2282 &sbi->s_inode_readahead_blks);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002283#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002284
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285 bgl_lock_init(&sbi->s_blockgroup_lock);
2286
2287 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002288 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002289 sbi->s_group_desc[i] = sb_bread(sb, block);
2290 if (!sbi->s_group_desc[i]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002291 printk(KERN_ERR "EXT4-fs: "
2292 "can't read group descriptor %d\n", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293 db_count = i;
2294 goto failed_mount2;
2295 }
2296 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002297 if (!ext4_check_descriptors(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002298 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002299 goto failed_mount2;
2300 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002301 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2302 if (!ext4_fill_flex_info(sb)) {
2303 printk(KERN_ERR
2304 "EXT4-fs: unable to initialize "
2305 "flex_bg meta info!\n");
2306 goto failed_mount2;
2307 }
2308
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002309 sbi->s_gdb_count = db_count;
2310 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2311 spin_lock_init(&sbi->s_next_gen_lock);
2312
Peter Zijlstra833f4072007-10-16 23:25:45 -07002313 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2314 ext4_count_free_blocks(sb));
2315 if (!err) {
2316 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2317 ext4_count_free_inodes(sb));
2318 }
2319 if (!err) {
2320 err = percpu_counter_init(&sbi->s_dirs_counter,
2321 ext4_count_dirs(sb));
2322 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002323 if (!err) {
2324 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2325 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002326 if (err) {
2327 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2328 goto failed_mount3;
2329 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002330
Alex Tomasc9de5602008-01-29 00:19:52 -05002331 sbi->s_stripe = ext4_get_stripe_size(sbi);
2332
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002333 /*
2334 * set up enough so that it can read an inode
2335 */
Mingming Cao617ba132006-10-11 01:20:53 -07002336 sb->s_op = &ext4_sops;
2337 sb->s_export_op = &ext4_export_ops;
2338 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002339#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002340 sb->s_qcop = &ext4_qctl_operations;
2341 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002342#endif
2343 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2344
2345 sb->s_root = NULL;
2346
2347 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002348 EXT4_HAS_INCOMPAT_FEATURE(sb,
2349 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002350
2351 /*
2352 * The first inode we look at is the journal inode. Don't try
2353 * root first: it may be modified in the journal!
2354 */
2355 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002356 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2357 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002358 goto failed_mount3;
Theodore Ts'o624080e2008-06-06 17:50:40 -04002359 if (!(sb->s_flags & MS_RDONLY) &&
2360 EXT4_SB(sb)->s_journal->j_failed_commit) {
2361 printk(KERN_CRIT "EXT4-fs error (device %s): "
2362 "ext4_fill_super: Journal transaction "
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002363 "%u is corrupt\n", sb->s_id,
Theodore Ts'o624080e2008-06-06 17:50:40 -04002364 EXT4_SB(sb)->s_journal->j_failed_commit);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002365 if (test_opt(sb, ERRORS_RO)) {
2366 printk(KERN_CRIT
2367 "Mounting filesystem read-only\n");
Theodore Ts'o624080e2008-06-06 17:50:40 -04002368 sb->s_flags |= MS_RDONLY;
2369 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2370 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2371 }
2372 if (test_opt(sb, ERRORS_PANIC)) {
2373 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2374 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2375 ext4_commit_super(sb, es, 1);
Theodore Ts'o624080e2008-06-06 17:50:40 -04002376 goto failed_mount4;
2377 }
2378 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002379 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002380 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002381 goto failed_mount3;
Frank Mayhar03901312009-01-07 00:06:22 -05002382 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2383 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2384 printk(KERN_ERR "EXT4-fs: required journal recovery "
2385 "suppressed and not mounted read-only\n");
2386 goto failed_mount4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002387 } else {
Frank Mayhar03901312009-01-07 00:06:22 -05002388 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2389 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2390 sbi->s_journal = NULL;
2391 needs_recovery = 0;
2392 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002393 }
2394
Jose R. Santoseb40a092007-07-18 08:37:25 -04002395 if (ext4_blocks_count(es) > 0xffffffffULL &&
2396 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2397 JBD2_FEATURE_INCOMPAT_64BIT)) {
2398 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2399 goto failed_mount4;
2400 }
2401
Girish Shilamkar818d2762008-01-28 23:58:27 -05002402 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2403 jbd2_journal_set_features(sbi->s_journal,
2404 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2405 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2406 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2407 jbd2_journal_set_features(sbi->s_journal,
2408 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2409 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2410 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2411 } else {
2412 jbd2_journal_clear_features(sbi->s_journal,
2413 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2414 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2415 }
2416
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002417 /* We have now updated the journal if required, so we can
2418 * validate the data journaling mode. */
2419 switch (test_opt(sb, DATA_FLAGS)) {
2420 case 0:
2421 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002422 * capabilities: ORDERED_DATA if the journal can
2423 * cope, else JOURNAL_DATA
2424 */
Mingming Caodab291a2006-10-11 01:21:01 -07002425 if (jbd2_journal_check_available_features
2426 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002427 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2428 else
2429 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2430 break;
2431
Mingming Cao617ba132006-10-11 01:20:53 -07002432 case EXT4_MOUNT_ORDERED_DATA:
2433 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002434 if (!jbd2_journal_check_available_features
2435 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002436 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002437 "requested data journaling mode\n");
2438 goto failed_mount4;
2439 }
2440 default:
2441 break;
2442 }
2443
Frank Mayhar03901312009-01-07 00:06:22 -05002444no_journal:
2445
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002446 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002447 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2448 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002449 "its supported only with writeback mode\n");
2450 clear_opt(sbi->s_mount_opt, NOBH);
2451 }
2452 }
2453 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002454 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002455 * so we can safely mount the rest of the filesystem now.
2456 */
2457
David Howells1d1fe1e2008-02-07 00:15:37 -08002458 root = ext4_iget(sb, EXT4_ROOT_INO);
2459 if (IS_ERR(root)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002460 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
David Howells1d1fe1e2008-02-07 00:15:37 -08002461 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462 goto failed_mount4;
2463 }
2464 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002465 iput(root);
Mingming Cao617ba132006-10-11 01:20:53 -07002466 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002467 goto failed_mount4;
2468 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002469 sb->s_root = d_alloc_root(root);
2470 if (!sb->s_root) {
2471 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2472 iput(root);
2473 ret = -ENOMEM;
2474 goto failed_mount4;
2475 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002476
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002477 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002478
2479 /* determine the minimum size of new large inodes, if present */
2480 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2481 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2482 EXT4_GOOD_OLD_INODE_SIZE;
2483 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2484 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2485 if (sbi->s_want_extra_isize <
2486 le16_to_cpu(es->s_want_extra_isize))
2487 sbi->s_want_extra_isize =
2488 le16_to_cpu(es->s_want_extra_isize);
2489 if (sbi->s_want_extra_isize <
2490 le16_to_cpu(es->s_min_extra_isize))
2491 sbi->s_want_extra_isize =
2492 le16_to_cpu(es->s_min_extra_isize);
2493 }
2494 }
2495 /* Check if enough inode space is available */
2496 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2497 sbi->s_inode_size) {
2498 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2499 EXT4_GOOD_OLD_INODE_SIZE;
2500 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2501 "available.\n");
2502 }
2503
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002504 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2505 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2506 "requested data journaling mode\n");
2507 clear_opt(sbi->s_mount_opt, DELALLOC);
2508 } else if (test_opt(sb, DELALLOC))
2509 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2510
2511 ext4_ext_init(sb);
2512 err = ext4_mb_init(sb, needs_recovery);
2513 if (err) {
2514 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2515 err);
2516 goto failed_mount4;
2517 }
2518
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002519 /*
2520 * akpm: core read_super() calls in here with the superblock locked.
2521 * That deadlocks, because orphan cleanup needs to lock the superblock
2522 * in numerous places. Here we just pop the lock - it's relatively
2523 * harmless, because we are now ready to accept write_super() requests,
2524 * and aviro says that's the only reason for hanging onto the
2525 * superblock lock.
2526 */
Mingming Cao617ba132006-10-11 01:20:53 -07002527 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2528 ext4_orphan_cleanup(sb, es);
2529 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05002530 if (needs_recovery) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002531 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
Frank Mayhar03901312009-01-07 00:06:22 -05002532 ext4_mark_recovery_complete(sb, es);
2533 }
2534 if (EXT4_SB(sb)->s_journal) {
2535 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2536 descr = " journalled data mode";
2537 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2538 descr = " ordered data mode";
2539 else
2540 descr = " writeback data mode";
2541 } else
2542 descr = "out journal";
2543
2544 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2545 sb->s_id, descr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002546
2547 lock_kernel();
2548 return 0;
2549
Mingming Cao617ba132006-10-11 01:20:53 -07002550cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002551 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07002552 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002553 sb->s_id);
2554 goto failed_mount;
2555
2556failed_mount4:
Frank Mayhar03901312009-01-07 00:06:22 -05002557 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2558 if (sbi->s_journal) {
2559 jbd2_journal_destroy(sbi->s_journal);
2560 sbi->s_journal = NULL;
2561 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002562failed_mount3:
2563 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2564 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2565 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002566 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002567failed_mount2:
2568 for (i = 0; i < db_count; i++)
2569 brelse(sbi->s_group_desc[i]);
2570 kfree(sbi->s_group_desc);
2571failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04002572 if (sbi->s_proc) {
2573 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002574 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04002575 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002576#ifdef CONFIG_QUOTA
2577 for (i = 0; i < MAXQUOTAS; i++)
2578 kfree(sbi->s_qf_names[i]);
2579#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002580 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002581 brelse(bh);
2582out_fail:
2583 sb->s_fs_info = NULL;
2584 kfree(sbi);
2585 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08002586 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002587}
2588
2589/*
2590 * Setup any per-fs journal parameters now. We'll do this both on
2591 * initial mount, once the journal has been initialised but before we've
2592 * done any recovery; and again on any subsequent remount.
2593 */
Mingming Cao617ba132006-10-11 01:20:53 -07002594static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002595{
Mingming Cao617ba132006-10-11 01:20:53 -07002596 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002597
Theodore Ts'o30773842009-01-03 20:27:38 -05002598 journal->j_commit_interval = sbi->s_commit_interval;
2599 journal->j_min_batch_time = sbi->s_min_batch_time;
2600 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002601
2602 spin_lock(&journal->j_state_lock);
2603 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07002604 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002605 else
Mingming Caodab291a2006-10-11 01:21:01 -07002606 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04002607 if (test_opt(sb, DATA_ERR_ABORT))
2608 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2609 else
2610 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002611 spin_unlock(&journal->j_state_lock);
2612}
2613
Mingming Cao617ba132006-10-11 01:20:53 -07002614static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002615 unsigned int journal_inum)
2616{
2617 struct inode *journal_inode;
2618 journal_t *journal;
2619
Frank Mayhar03901312009-01-07 00:06:22 -05002620 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2621
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002622 /* First, test for the existence of a valid inode on disk. Bad
2623 * things happen if we iget() an unused inode, as the subsequent
2624 * iput() will try to delete it. */
2625
David Howells1d1fe1e2008-02-07 00:15:37 -08002626 journal_inode = ext4_iget(sb, journal_inum);
2627 if (IS_ERR(journal_inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002628 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002629 return NULL;
2630 }
2631 if (!journal_inode->i_nlink) {
2632 make_bad_inode(journal_inode);
2633 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002634 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002635 return NULL;
2636 }
2637
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002638 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002639 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08002640 if (!S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002641 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002642 iput(journal_inode);
2643 return NULL;
2644 }
2645
Mingming Caodab291a2006-10-11 01:21:01 -07002646 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002647 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002648 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002649 iput(journal_inode);
2650 return NULL;
2651 }
2652 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002653 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002654 return journal;
2655}
2656
Mingming Cao617ba132006-10-11 01:20:53 -07002657static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002658 dev_t j_dev)
2659{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002660 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002661 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07002662 ext4_fsblk_t start;
2663 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002664 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07002665 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002666 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002667 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002668 struct block_device *bdev;
2669
Frank Mayhar03901312009-01-07 00:06:22 -05002670 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2671
Mingming Cao617ba132006-10-11 01:20:53 -07002672 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002673 if (bdev == NULL)
2674 return NULL;
2675
2676 if (bd_claim(bdev, sb)) {
2677 printk(KERN_ERR
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002678 "EXT4: failed to claim external journal device.\n");
Al Viro9a1c3542008-02-22 20:40:24 -05002679 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002680 return NULL;
2681 }
2682
2683 blocksize = sb->s_blocksize;
2684 hblock = bdev_hardsect_size(bdev);
2685 if (blocksize < hblock) {
2686 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002687 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002688 goto out_bdev;
2689 }
2690
Mingming Cao617ba132006-10-11 01:20:53 -07002691 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2692 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002693 set_blocksize(bdev, blocksize);
2694 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002695 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002696 "external journal\n");
2697 goto out_bdev;
2698 }
2699
Mingming Cao617ba132006-10-11 01:20:53 -07002700 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2701 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002702 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002703 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2704 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002705 "bad superblock\n");
2706 brelse(bh);
2707 goto out_bdev;
2708 }
2709
Mingming Cao617ba132006-10-11 01:20:53 -07002710 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2711 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002712 brelse(bh);
2713 goto out_bdev;
2714 }
2715
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002716 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002717 start = sb_block + 1;
2718 brelse(bh); /* we're done with the superblock */
2719
Mingming Caodab291a2006-10-11 01:21:01 -07002720 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002721 start, len, blocksize);
2722 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002723 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002724 goto out_bdev;
2725 }
2726 journal->j_private = sb;
2727 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2728 wait_on_buffer(journal->j_sb_buffer);
2729 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002730 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002731 goto out_journal;
2732 }
2733 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002734 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002735 "user (unsupported) - %d\n",
2736 be32_to_cpu(journal->j_superblock->s_nr_users));
2737 goto out_journal;
2738 }
Mingming Cao617ba132006-10-11 01:20:53 -07002739 EXT4_SB(sb)->journal_bdev = bdev;
2740 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002741 return journal;
2742out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002743 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002744out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002745 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002746 return NULL;
2747}
2748
Mingming Cao617ba132006-10-11 01:20:53 -07002749static int ext4_load_journal(struct super_block *sb,
2750 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002751 unsigned long journal_devnum)
2752{
2753 journal_t *journal;
2754 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2755 dev_t journal_dev;
2756 int err = 0;
2757 int really_read_only;
2758
Frank Mayhar03901312009-01-07 00:06:22 -05002759 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2760
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002761 if (journal_devnum &&
2762 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002763 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002764 "numbers have changed\n");
2765 journal_dev = new_decode_dev(journal_devnum);
2766 } else
2767 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2768
2769 really_read_only = bdev_read_only(sb->s_bdev);
2770
2771 /*
2772 * Are we loading a blank journal or performing recovery after a
2773 * crash? For recovery, we need to check in advance whether we
2774 * can get read-write access to the device.
2775 */
2776
Mingming Cao617ba132006-10-11 01:20:53 -07002777 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002778 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002779 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002780 "required on readonly filesystem.\n");
2781 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002782 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002783 "unavailable, cannot proceed.\n");
2784 return -EROFS;
2785 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002786 printk(KERN_INFO "EXT4-fs: write access will "
2787 "be enabled during recovery.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002788 }
2789 }
2790
2791 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002792 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002793 "and inode journals!\n");
2794 return -EINVAL;
2795 }
2796
2797 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002798 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002799 return -EINVAL;
2800 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002801 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002802 return -EINVAL;
2803 }
2804
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002805 if (journal->j_flags & JBD2_BARRIER)
2806 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2807 else
2808 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2809
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002810 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002811 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002812 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002813 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002814 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002815 return err;
2816 }
2817 }
2818
Mingming Cao617ba132006-10-11 01:20:53 -07002819 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002820 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002821 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002822 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002823
2824 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002825 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002826 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002827 return err;
2828 }
2829
Mingming Cao617ba132006-10-11 01:20:53 -07002830 EXT4_SB(sb)->s_journal = journal;
2831 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002832
2833 if (journal_devnum &&
2834 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2835 es->s_journal_dev = cpu_to_le32(journal_devnum);
2836 sb->s_dirt = 1;
2837
2838 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002839 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002840 }
2841
2842 return 0;
2843}
2844
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002845static int ext4_create_journal(struct super_block *sb,
2846 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002847 unsigned int journal_inum)
2848{
2849 journal_t *journal;
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002850 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002851
2852 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002853 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002854 "create journal.\n");
2855 return -EROFS;
2856 }
2857
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002858 journal = ext4_get_journal(sb, journal_inum);
2859 if (!journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002860 return -EINVAL;
2861
Mingming Cao617ba132006-10-11 01:20:53 -07002862 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002863 journal_inum);
2864
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002865 err = jbd2_journal_create(journal);
2866 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002867 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002868 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002869 return -EIO;
2870 }
2871
Mingming Cao617ba132006-10-11 01:20:53 -07002872 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002873
Mingming Cao617ba132006-10-11 01:20:53 -07002874 ext4_update_dynamic_rev(sb);
2875 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2876 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002877
2878 es->s_journal_inum = cpu_to_le32(journal_inum);
2879 sb->s_dirt = 1;
2880
2881 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002882 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002883
2884 return 0;
2885}
2886
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002887static void ext4_commit_super(struct super_block *sb,
2888 struct ext4_super_block *es, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002889{
Mingming Cao617ba132006-10-11 01:20:53 -07002890 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002891
2892 if (!sbh)
2893 return;
Theodore Ts'o914258b2008-10-06 21:35:40 -04002894 if (buffer_write_io_error(sbh)) {
2895 /*
2896 * Oh, dear. A previous attempt to write the
2897 * superblock failed. This could happen because the
2898 * USB device was yanked out. Or it could happen to
2899 * be a transient write error and maybe the block will
2900 * be remapped. Nothing we can do but to retry the
2901 * write and hope for the best.
2902 */
2903 printk(KERN_ERR "ext4: previous I/O error to "
2904 "superblock detected for %s.\n", sb->s_id);
2905 clear_buffer_write_io_error(sbh);
2906 set_buffer_uptodate(sbh);
2907 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002908 es->s_wtime = cpu_to_le32(get_seconds());
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05002909 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
2910 &EXT4_SB(sb)->s_freeblocks_counter));
2911 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
2912 &EXT4_SB(sb)->s_freeinodes_counter));
2913
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002914 BUFFER_TRACE(sbh, "marking dirty");
2915 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002916 if (sync) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002917 sync_dirty_buffer(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002918 if (buffer_write_io_error(sbh)) {
2919 printk(KERN_ERR "ext4: I/O error while writing "
2920 "superblock for %s.\n", sb->s_id);
2921 clear_buffer_write_io_error(sbh);
2922 set_buffer_uptodate(sbh);
2923 }
2924 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002925}
2926
2927
2928/*
2929 * Have we just finished recovery? If so, and if we are mounting (or
2930 * remounting) the filesystem readonly, then we will end up with a
2931 * consistent fs on disk. Record that fact.
2932 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002933static void ext4_mark_recovery_complete(struct super_block *sb,
2934 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002935{
Mingming Cao617ba132006-10-11 01:20:53 -07002936 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002937
Frank Mayhar03901312009-01-07 00:06:22 -05002938 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2939 BUG_ON(journal != NULL);
2940 return;
2941 }
Mingming Caodab291a2006-10-11 01:21:01 -07002942 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04002943 if (jbd2_journal_flush(journal) < 0)
2944 goto out;
2945
Jan Kara32c37732007-07-15 23:41:09 -07002946 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002947 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002948 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002949 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002950 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002951 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002952 }
Jan Kara32c37732007-07-15 23:41:09 -07002953 unlock_super(sb);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04002954
2955out:
Mingming Caodab291a2006-10-11 01:21:01 -07002956 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002957}
2958
2959/*
2960 * If we are mounting (or read-write remounting) a filesystem whose journal
2961 * has recorded an error from a previous lifetime, move that error to the
2962 * main filesystem now.
2963 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002964static void ext4_clear_journal_err(struct super_block *sb,
2965 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002966{
2967 journal_t *journal;
2968 int j_errno;
2969 const char *errstr;
2970
Frank Mayhar03901312009-01-07 00:06:22 -05002971 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2972
Mingming Cao617ba132006-10-11 01:20:53 -07002973 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974
2975 /*
2976 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07002977 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002978 */
2979
Mingming Caodab291a2006-10-11 01:21:01 -07002980 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002981 if (j_errno) {
2982 char nbuf[16];
2983
Mingming Cao617ba132006-10-11 01:20:53 -07002984 errstr = ext4_decode_error(sb, j_errno, nbuf);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002985 ext4_warning(sb, __func__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002986 "from previous mount: %s", errstr);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002987 ext4_warning(sb, __func__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002988 "filesystem check.");
2989
Mingming Cao617ba132006-10-11 01:20:53 -07002990 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2991 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002992 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002993
Mingming Caodab291a2006-10-11 01:21:01 -07002994 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002995 }
2996}
2997
2998/*
2999 * Force the running and committing transactions to commit,
3000 * and wait on the commit.
3001 */
Mingming Cao617ba132006-10-11 01:20:53 -07003002int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003003{
3004 journal_t *journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003005 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003006
3007 if (sb->s_flags & MS_RDONLY)
3008 return 0;
3009
Mingming Cao617ba132006-10-11 01:20:53 -07003010 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003011 if (journal) {
3012 sb->s_dirt = 0;
3013 ret = ext4_journal_force_commit(journal);
3014 }
3015
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003016 return ret;
3017}
3018
3019/*
Mingming Cao617ba132006-10-11 01:20:53 -07003020 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021 * have to propagate any other updates to the superblock on disk at this
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003022 * point. (We can probably nuke this function altogether, and remove
3023 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003024 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003025static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003026{
Frank Mayhar03901312009-01-07 00:06:22 -05003027 if (EXT4_SB(sb)->s_journal) {
3028 if (mutex_trylock(&sb->s_lock) != 0)
3029 BUG();
3030 sb->s_dirt = 0;
3031 } else {
3032 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3033 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003034}
3035
Mingming Cao617ba132006-10-11 01:20:53 -07003036static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003037{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003038 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003039
Theodore Ts'oede86cc2008-10-05 20:50:06 -04003040 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003041 sb->s_dirt = 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003042 if (EXT4_SB(sb)->s_journal) {
3043 if (wait)
3044 ret = ext4_force_commit(sb);
3045 else
3046 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
3047 } else {
3048 ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
3049 }
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003050 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003051}
3052
3053/*
3054 * LVM calls this function before a (read-only) snapshot is created. This
3055 * gives us a chance to flush the journal completely and mark the fs clean.
3056 */
Mingming Cao617ba132006-10-11 01:20:53 -07003057static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003058{
3059 sb->s_dirt = 0;
3060
3061 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003062 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003063
Frank Mayhar03901312009-01-07 00:06:22 -05003064 if (journal) {
3065 /* Now we set up the journal barrier. */
3066 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003067
Frank Mayhar03901312009-01-07 00:06:22 -05003068 /*
3069 * We don't want to clear needs_recovery flag when we
3070 * failed to flush the journal.
3071 */
3072 if (jbd2_journal_flush(journal) < 0)
3073 return;
3074 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075
3076 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07003077 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3078 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003079 }
3080}
3081
3082/*
3083 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3084 * flag here, even though the filesystem is not technically dirty yet.
3085 */
Mingming Cao617ba132006-10-11 01:20:53 -07003086static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003087{
Frank Mayhar03901312009-01-07 00:06:22 -05003088 if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003089 lock_super(sb);
3090 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07003091 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3092 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003093 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07003094 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003095 }
3096}
3097
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003098static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003099{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003100 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003101 struct ext4_sb_info *sbi = EXT4_SB(sb);
3102 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003103 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003104 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003105 ext4_group_t g;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003106 int err;
3107#ifdef CONFIG_QUOTA
3108 int i;
3109#endif
3110
3111 /* Store the original options */
3112 old_sb_flags = sb->s_flags;
3113 old_opts.s_mount_opt = sbi->s_mount_opt;
3114 old_opts.s_resuid = sbi->s_resuid;
3115 old_opts.s_resgid = sbi->s_resgid;
3116 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003117 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3118 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003119#ifdef CONFIG_QUOTA
3120 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3121 for (i = 0; i < MAXQUOTAS; i++)
3122 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3123#endif
3124
3125 /*
3126 * Allow the "check" option to be passed as a remount option.
3127 */
3128 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3129 err = -EINVAL;
3130 goto restore_opts;
3131 }
3132
Mingming Cao617ba132006-10-11 01:20:53 -07003133 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
Harvey Harrison46e665e2008-04-17 10:38:59 -04003134 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003135
3136 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07003137 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003138
3139 es = sbi->s_es;
3140
Frank Mayhar03901312009-01-07 00:06:22 -05003141 if (sbi->s_journal)
3142 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003143
3144 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003145 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003146 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003147 err = -EROFS;
3148 goto restore_opts;
3149 }
3150
3151 if (*flags & MS_RDONLY) {
3152 /*
3153 * First of all, the unconditional stuff we have to do
3154 * to disable replay of the journal when we next remount
3155 */
3156 sb->s_flags |= MS_RDONLY;
3157
3158 /*
3159 * OK, test if we are remounting a valid rw partition
3160 * readonly, and if so set the rdonly flag and then
3161 * mark the partition as valid again.
3162 */
Mingming Cao617ba132006-10-11 01:20:53 -07003163 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3164 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003165 es->s_state = cpu_to_le16(sbi->s_mount_state);
3166
Jan Kara32c37732007-07-15 23:41:09 -07003167 /*
3168 * We have to unlock super so that we can wait for
3169 * transactions.
3170 */
Frank Mayhar03901312009-01-07 00:06:22 -05003171 if (sbi->s_journal) {
3172 unlock_super(sb);
3173 ext4_mark_recovery_complete(sb, es);
3174 lock_super(sb);
3175 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003176 } else {
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003177 int ret;
Mingming Cao617ba132006-10-11 01:20:53 -07003178 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3179 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3180 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003181 "remount RDWR because of unsupported "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003182 "optional features (%x).\n", sb->s_id,
3183 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3184 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003185 err = -EROFS;
3186 goto restore_opts;
3187 }
Eric Sandeenead65962007-02-10 01:46:08 -08003188
3189 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003190 * Make sure the group descriptor checksums
3191 * are sane. If they aren't, refuse to
3192 * remount r/w.
3193 */
3194 for (g = 0; g < sbi->s_groups_count; g++) {
3195 struct ext4_group_desc *gdp =
3196 ext4_get_group_desc(sb, g, NULL);
3197
3198 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3199 printk(KERN_ERR
3200 "EXT4-fs: ext4_remount: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003201 "Checksum for group %u failed (%u!=%u)\n",
Theodore Ts'o8a266462008-07-26 14:34:21 -04003202 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3203 le16_to_cpu(gdp->bg_checksum));
3204 err = -EINVAL;
3205 goto restore_opts;
3206 }
3207 }
3208
3209 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003210 * If we have an unprocessed orphan list hanging
3211 * around from a previously readonly bdev mount,
3212 * require a full umount/remount for now.
3213 */
3214 if (es->s_last_orphan) {
3215 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3216 "remount RDWR because of unprocessed "
3217 "orphan inode list. Please "
3218 "umount/remount instead.\n",
3219 sb->s_id);
3220 err = -EINVAL;
3221 goto restore_opts;
3222 }
3223
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003224 /*
3225 * Mounting a RDONLY partition read-write, so reread
3226 * and store the current valid flag. (It may have
3227 * been changed by e2fsck since we originally mounted
3228 * the partition.)
3229 */
Frank Mayhar03901312009-01-07 00:06:22 -05003230 if (sbi->s_journal)
3231 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003232 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003233 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003234 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003235 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003236 sb->s_flags &= ~MS_RDONLY;
3237 }
3238 }
Frank Mayhar03901312009-01-07 00:06:22 -05003239 if (sbi->s_journal == NULL)
3240 ext4_commit_super(sb, es, 1);
3241
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003242#ifdef CONFIG_QUOTA
3243 /* Release old quota file names */
3244 for (i = 0; i < MAXQUOTAS; i++)
3245 if (old_opts.s_qf_names[i] &&
3246 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3247 kfree(old_opts.s_qf_names[i]);
3248#endif
3249 return 0;
3250restore_opts:
3251 sb->s_flags = old_sb_flags;
3252 sbi->s_mount_opt = old_opts.s_mount_opt;
3253 sbi->s_resuid = old_opts.s_resuid;
3254 sbi->s_resgid = old_opts.s_resgid;
3255 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003256 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3257 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003258#ifdef CONFIG_QUOTA
3259 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3260 for (i = 0; i < MAXQUOTAS; i++) {
3261 if (sbi->s_qf_names[i] &&
3262 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3263 kfree(sbi->s_qf_names[i]);
3264 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3265 }
3266#endif
3267 return err;
3268}
3269
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003270static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003271{
3272 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003273 struct ext4_sb_info *sbi = EXT4_SB(sb);
3274 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003275 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003276
Badari Pulavarty5e700302007-07-15 23:42:00 -07003277 if (test_opt(sb, MINIX_DF)) {
3278 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003279 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Avantika Mathurfd2d4292008-01-28 23:58:27 -05003280 ext4_group_t ngroups = sbi->s_groups_count, i;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003281 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003282 smp_rmb();
3283
3284 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003285 * Compute the overhead (FS structures). This is constant
3286 * for a given filesystem unless the number of block groups
3287 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003288 */
3289
3290 /*
3291 * All of the blocks before first_data_block are
3292 * overhead
3293 */
3294 overhead = le32_to_cpu(es->s_first_data_block);
3295
3296 /*
3297 * Add the overhead attributed to the superblock and
3298 * block group descriptors. If the sparse superblocks
3299 * feature is turned on, then not all groups have this.
3300 */
3301 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003302 overhead += ext4_bg_has_super(sb, i) +
3303 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003304 cond_resched();
3305 }
3306
3307 /*
3308 * Every block group has an inode bitmap, a block
3309 * bitmap, and an inode table.
3310 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003311 overhead += ngroups * (2 + sbi->s_itb_per_group);
3312 sbi->s_overhead_last = overhead;
3313 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003314 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003315 }
3316
Mingming Cao617ba132006-10-11 01:20:53 -07003317 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003318 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003319 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003320 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3321 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Aneesh Kumar K.V308ba3e2007-10-16 18:38:25 -04003322 ext4_free_blocks_count_set(es, buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003323 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3324 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003325 buf->f_bavail = 0;
3326 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003327 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003328 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07003329 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003330 fsid = le64_to_cpup((void *)es->s_uuid) ^
3331 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3332 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3333 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003334 return 0;
3335}
3336
3337/* Helper function for writing quotas on sync - we need to start transaction before quota file
3338 * is locked for write. Otherwise the are possible deadlocks:
3339 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003340 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07003341 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003342 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003343 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003344 *
3345 */
3346
3347#ifdef CONFIG_QUOTA
3348
3349static inline struct inode *dquot_to_inode(struct dquot *dquot)
3350{
3351 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3352}
3353
Mingming Cao617ba132006-10-11 01:20:53 -07003354static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003355{
3356 handle_t *handle;
3357 int ret, err;
3358
3359 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003360 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003361 if (IS_ERR(handle))
3362 return PTR_ERR(handle);
3363 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003364 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003365 if (!ret)
3366 ret = err;
3367 return ret;
3368}
3369
Mingming Cao617ba132006-10-11 01:20:53 -07003370static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003371{
3372 handle_t *handle;
3373 int ret, err;
3374
3375 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003376 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Jan Kara2887df12008-04-29 22:02:07 -04003377 if (IS_ERR(handle)) {
3378 /*
3379 * We call dquot_drop() anyway to at least release references
3380 * to quota structures so that umount does not hang.
3381 */
3382 dquot_drop(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003383 return PTR_ERR(handle);
Jan Kara2887df12008-04-29 22:02:07 -04003384 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003385 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003386 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003387 if (!ret)
3388 ret = err;
3389 return ret;
3390}
3391
Mingming Cao617ba132006-10-11 01:20:53 -07003392static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003393{
3394 int ret, err;
3395 handle_t *handle;
3396 struct inode *inode;
3397
3398 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003399 handle = ext4_journal_start(inode,
3400 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003401 if (IS_ERR(handle))
3402 return PTR_ERR(handle);
3403 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003404 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003405 if (!ret)
3406 ret = err;
3407 return ret;
3408}
3409
Mingming Cao617ba132006-10-11 01:20:53 -07003410static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003411{
3412 int ret, err;
3413 handle_t *handle;
3414
Mingming Cao617ba132006-10-11 01:20:53 -07003415 handle = ext4_journal_start(dquot_to_inode(dquot),
3416 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003417 if (IS_ERR(handle))
3418 return PTR_ERR(handle);
3419 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003420 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003421 if (!ret)
3422 ret = err;
3423 return ret;
3424}
3425
Mingming Cao617ba132006-10-11 01:20:53 -07003426static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003427{
3428 int ret, err;
3429 handle_t *handle;
3430
Mingming Cao617ba132006-10-11 01:20:53 -07003431 handle = ext4_journal_start(dquot_to_inode(dquot),
3432 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003433 if (IS_ERR(handle)) {
3434 /* Release dquot anyway to avoid endless cycle in dqput() */
3435 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003436 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003437 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003438 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003439 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003440 if (!ret)
3441 ret = err;
3442 return ret;
3443}
3444
Mingming Cao617ba132006-10-11 01:20:53 -07003445static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003446{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003447 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003448 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3449 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003450 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003451 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003452 } else {
3453 return dquot_mark_dquot_dirty(dquot);
3454 }
3455}
3456
Mingming Cao617ba132006-10-11 01:20:53 -07003457static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003458{
3459 int ret, err;
3460 handle_t *handle;
3461
3462 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003463 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003464 if (IS_ERR(handle))
3465 return PTR_ERR(handle);
3466 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003467 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003468 if (!ret)
3469 ret = err;
3470 return ret;
3471}
3472
3473/*
3474 * Turn on quotas during mount time - we need to find
3475 * the quota file and such...
3476 */
Mingming Cao617ba132006-10-11 01:20:53 -07003477static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003478{
Mingming Cao617ba132006-10-11 01:20:53 -07003479 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3480 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003481}
3482
3483/*
3484 * Standard function to be called on quota_on
3485 */
Mingming Cao617ba132006-10-11 01:20:53 -07003486static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro82646132008-08-02 00:57:06 -04003487 char *name, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003488{
3489 int err;
Al Viro82646132008-08-02 00:57:06 -04003490 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003491
3492 if (!test_opt(sb, QUOTA))
3493 return -EINVAL;
Al Viro82646132008-08-02 00:57:06 -04003494 /* When remounting, no checks are needed and in fact, name is NULL */
Jan Kara06235432008-05-13 19:11:51 -04003495 if (remount)
Al Viro82646132008-08-02 00:57:06 -04003496 return vfs_quota_on(sb, type, format_id, name, remount);
Jan Kara06235432008-05-13 19:11:51 -04003497
Al Viro82646132008-08-02 00:57:06 -04003498 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003499 if (err)
3500 return err;
Jan Kara06235432008-05-13 19:11:51 -04003501
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003502 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04003503 if (path.mnt->mnt_sb != sb) {
3504 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003505 return -EXDEV;
3506 }
Jan Kara06235432008-05-13 19:11:51 -04003507 /* Journaling quota? */
3508 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003509 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04003510 if (path.dentry->d_parent != sb->s_root)
Jan Kara06235432008-05-13 19:11:51 -04003511 printk(KERN_WARNING
3512 "EXT4-fs: Quota file not on filesystem root. "
3513 "Journaled quota will not work.\n");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003514 }
Jan Kara06235432008-05-13 19:11:51 -04003515
3516 /*
3517 * When we journal data on quota file, we have to flush journal to see
3518 * all updates to the file when we bypass pagecache...
3519 */
Frank Mayhar03901312009-01-07 00:06:22 -05003520 if (EXT4_SB(sb)->s_journal &&
3521 ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04003522 /*
3523 * We don't need to lock updates but journal_flush() could
3524 * otherwise be livelocked...
3525 */
3526 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003527 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04003528 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003529 if (err) {
Al Viro82646132008-08-02 00:57:06 -04003530 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003531 return err;
3532 }
Jan Kara06235432008-05-13 19:11:51 -04003533 }
3534
Al Viro82646132008-08-02 00:57:06 -04003535 err = vfs_quota_on_path(sb, type, format_id, &path);
3536 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04003537 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003538}
3539
3540/* Read data from quotafile - avoid pagecache and such because we cannot afford
3541 * acquiring the locks... As quota files are never truncated and quota code
3542 * itself serializes the operations (and noone else should touch the files)
3543 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003544static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003545 size_t len, loff_t off)
3546{
3547 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003548 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003549 int err = 0;
3550 int offset = off & (sb->s_blocksize - 1);
3551 int tocopy;
3552 size_t toread;
3553 struct buffer_head *bh;
3554 loff_t i_size = i_size_read(inode);
3555
3556 if (off > i_size)
3557 return 0;
3558 if (off+len > i_size)
3559 len = i_size-off;
3560 toread = len;
3561 while (toread > 0) {
3562 tocopy = sb->s_blocksize - offset < toread ?
3563 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003564 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003565 if (err)
3566 return err;
3567 if (!bh) /* A hole? */
3568 memset(data, 0, tocopy);
3569 else
3570 memcpy(data, bh->b_data+offset, tocopy);
3571 brelse(bh);
3572 offset = 0;
3573 toread -= tocopy;
3574 data += tocopy;
3575 blk++;
3576 }
3577 return len;
3578}
3579
3580/* Write to quotafile (we know the transaction is already started and has
3581 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003582static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003583 const char *data, size_t len, loff_t off)
3584{
3585 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003586 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003587 int err = 0;
3588 int offset = off & (sb->s_blocksize - 1);
3589 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003590 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003591 size_t towrite = len;
3592 struct buffer_head *bh;
3593 handle_t *handle = journal_current_handle();
3594
Frank Mayhar03901312009-01-07 00:06:22 -05003595 if (EXT4_SB(sb)->s_journal && !handle) {
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003596 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
Jan Kara9c3013e2007-09-11 15:23:29 -07003597 " cancelled because transaction is not started.\n",
3598 (unsigned long long)off, (unsigned long long)len);
3599 return -EIO;
3600 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003601 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3602 while (towrite > 0) {
3603 tocopy = sb->s_blocksize - offset < towrite ?
3604 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003605 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003606 if (!bh)
3607 goto out;
3608 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003609 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003610 if (err) {
3611 brelse(bh);
3612 goto out;
3613 }
3614 }
3615 lock_buffer(bh);
3616 memcpy(bh->b_data+offset, data, tocopy);
3617 flush_dcache_page(bh->b_page);
3618 unlock_buffer(bh);
3619 if (journal_quota)
Frank Mayhar03901312009-01-07 00:06:22 -05003620 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003621 else {
3622 /* Always do at least ordered writes for quotas */
Jan Kara678aaf42008-07-11 19:27:31 -04003623 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003624 mark_buffer_dirty(bh);
3625 }
3626 brelse(bh);
3627 if (err)
3628 goto out;
3629 offset = 0;
3630 towrite -= tocopy;
3631 data += tocopy;
3632 blk++;
3633 }
3634out:
Jan Kara4d04e4f2008-07-04 09:59:34 -07003635 if (len == towrite) {
3636 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003637 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07003638 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003639 if (inode->i_size < off+len-towrite) {
3640 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07003641 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003642 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003643 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003644 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003645 mutex_unlock(&inode->i_mutex);
3646 return len - towrite;
3647}
3648
3649#endif
3650
Mingming Cao617ba132006-10-11 01:20:53 -07003651static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003652 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3653{
Mingming Cao617ba132006-10-11 01:20:53 -07003654 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003655}
3656
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003657#ifdef CONFIG_PROC_FS
3658static int ext4_ui_proc_show(struct seq_file *m, void *v)
3659{
3660 unsigned int *p = m->private;
3661
3662 seq_printf(m, "%u\n", *p);
3663 return 0;
3664}
3665
3666static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3667{
3668 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3669}
3670
3671static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3672 size_t cnt, loff_t *ppos)
3673{
Roel Kluin23475e22008-11-26 02:23:19 -05003674 unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003675 char str[32];
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003676
3677 if (cnt >= sizeof(str))
3678 return -EINVAL;
3679 if (copy_from_user(str, buf, cnt))
3680 return -EFAULT;
Roel Kluin23475e22008-11-26 02:23:19 -05003681
3682 *p = simple_strtoul(str, NULL, 0);
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003683 return cnt;
3684}
3685
3686const struct file_operations ext4_ui_proc_fops = {
3687 .owner = THIS_MODULE,
3688 .open = ext4_ui_proc_open,
3689 .read = seq_read,
3690 .llseek = seq_lseek,
3691 .release = single_release,
3692 .write = ext4_ui_proc_write,
3693};
3694#endif
3695
Theodore Ts'o03010a32008-10-10 20:02:48 -04003696static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003697 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04003698 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07003699 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003700 .kill_sb = kill_block_super,
3701 .fs_flags = FS_REQUIRES_DEV,
3702};
3703
Theodore Ts'o03010a32008-10-10 20:02:48 -04003704#ifdef CONFIG_EXT4DEV_COMPAT
3705static int ext4dev_get_sb(struct file_system_type *fs_type,
3706 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3707{
3708 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3709 "to mount using ext4\n");
3710 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3711 "will go away by 2.6.31\n");
3712 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3713}
3714
3715static struct file_system_type ext4dev_fs_type = {
3716 .owner = THIS_MODULE,
3717 .name = "ext4dev",
3718 .get_sb = ext4dev_get_sb,
3719 .kill_sb = kill_block_super,
3720 .fs_flags = FS_REQUIRES_DEV,
3721};
3722MODULE_ALIAS("ext4dev");
3723#endif
3724
Mingming Cao617ba132006-10-11 01:20:53 -07003725static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003726{
Alex Tomasc9de5602008-01-29 00:19:52 -05003727 int err;
3728
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003729 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003730 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003731 if (err)
3732 return err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003733
3734 err = init_ext4_xattr();
3735 if (err)
3736 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003737 err = init_inodecache();
3738 if (err)
3739 goto out1;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003740 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003741 if (err)
3742 goto out;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003743#ifdef CONFIG_EXT4DEV_COMPAT
3744 err = register_filesystem(&ext4dev_fs_type);
3745 if (err) {
3746 unregister_filesystem(&ext4_fs_type);
3747 goto out;
3748 }
3749#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003750 return 0;
3751out:
3752 destroy_inodecache();
3753out1:
Mingming Cao617ba132006-10-11 01:20:53 -07003754 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003755out2:
3756 exit_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003757 return err;
3758}
3759
Mingming Cao617ba132006-10-11 01:20:53 -07003760static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003761{
Theodore Ts'o03010a32008-10-10 20:02:48 -04003762 unregister_filesystem(&ext4_fs_type);
3763#ifdef CONFIG_EXT4DEV_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07003764 unregister_filesystem(&ext4dev_fs_type);
Theodore Ts'o03010a32008-10-10 20:02:48 -04003765#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003766 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07003767 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003768 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003769 remove_proc_entry("fs/ext4", NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003770}
3771
3772MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07003773MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003774MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07003775module_init(init_ext4_fs)
3776module_exit(exit_ext4_fs)