blob: a9dd1170bfea10bad688a690d1aac8ffa650f9b0 [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
Mingming Cao617ba132006-10-11 01:20:53 -0700369void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370{
Mingming Cao617ba132006-10-11 01:20:53 -0700371 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700372
Mingming Cao617ba132006-10-11 01:20:53 -0700373 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700374 return;
375
Harvey Harrison46e665e2008-04-17 10:38:59 -0400376 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700377 "updating to rev %d because of new feature flag, "
378 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700379 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380
Mingming Cao617ba132006-10-11 01:20:53 -0700381 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
382 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
383 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700384 /* leave es->s_feature_*compat flags alone */
385 /* es->s_uuid will be set by e2fsck if empty */
386
387 /*
388 * The rest of the superblock fields should be zero, and if not it
389 * means they are likely already in use, so leave them alone. We
390 * can leave it up to e2fsck to clean up any inconsistencies there.
391 */
392}
393
394/*
395 * Open the external journal device
396 */
Mingming Cao617ba132006-10-11 01:20:53 -0700397static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700398{
399 struct block_device *bdev;
400 char b[BDEVNAME_SIZE];
401
402 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
403 if (IS_ERR(bdev))
404 goto fail;
405 return bdev;
406
407fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700408 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 __bdevname(dev, b), PTR_ERR(bdev));
410 return NULL;
411}
412
413/*
414 * Release the journal device
415 */
Mingming Cao617ba132006-10-11 01:20:53 -0700416static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417{
418 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500419 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700420}
421
Mingming Cao617ba132006-10-11 01:20:53 -0700422static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700423{
424 struct block_device *bdev;
425 int ret = -ENODEV;
426
427 bdev = sbi->journal_bdev;
428 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700429 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700430 sbi->journal_bdev = NULL;
431 }
432 return ret;
433}
434
435static inline struct inode *orphan_list_entry(struct list_head *l)
436{
Mingming Cao617ba132006-10-11 01:20:53 -0700437 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700438}
439
Mingming Cao617ba132006-10-11 01:20:53 -0700440static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441{
442 struct list_head *l;
443
444 printk(KERN_ERR "sb orphan head is %d\n",
445 le32_to_cpu(sbi->s_es->s_last_orphan));
446
447 printk(KERN_ERR "sb_info orphan list:\n");
448 list_for_each(l, &sbi->s_orphan) {
449 struct inode *inode = orphan_list_entry(l);
450 printk(KERN_ERR " "
451 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
452 inode->i_sb->s_id, inode->i_ino, inode,
453 inode->i_mode, inode->i_nlink,
454 NEXT_ORPHAN(inode));
455 }
456}
457
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400458static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459{
Mingming Cao617ba132006-10-11 01:20:53 -0700460 struct ext4_sb_info *sbi = EXT4_SB(sb);
461 struct ext4_super_block *es = sbi->s_es;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400462 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700463
Alex Tomasc9de5602008-01-29 00:19:52 -0500464 ext4_mb_release(sb);
Alex Tomasa86c6182006-10-11 01:21:03 -0700465 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700466 ext4_xattr_put_super(sb);
Frank Mayhar03901312009-01-07 00:06:22 -0500467 if (sbi->s_journal) {
468 err = jbd2_journal_destroy(sbi->s_journal);
469 sbi->s_journal = NULL;
470 if (err < 0)
471 ext4_abort(sb, __func__,
472 "Couldn't clean up the journal");
473 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700475 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476 es->s_state = cpu_to_le16(sbi->s_mount_state);
Mingming Cao617ba132006-10-11 01:20:53 -0700477 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400479 if (sbi->s_proc) {
480 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400481 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400482 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483
484 for (i = 0; i < sbi->s_gdb_count; i++)
485 brelse(sbi->s_group_desc[i]);
486 kfree(sbi->s_group_desc);
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400487 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488 percpu_counter_destroy(&sbi->s_freeblocks_counter);
489 percpu_counter_destroy(&sbi->s_freeinodes_counter);
490 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400491 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492 brelse(sbi->s_sbh);
493#ifdef CONFIG_QUOTA
494 for (i = 0; i < MAXQUOTAS; i++)
495 kfree(sbi->s_qf_names[i]);
496#endif
497
498 /* Debugging code just in case the in-memory inode orphan list
499 * isn't empty. The on-disk one can be non-empty if we've
500 * detected an error and taken the fs readonly, but the
501 * in-memory list had better be clean by this point. */
502 if (!list_empty(&sbi->s_orphan))
503 dump_orphan_list(sb, sbi);
504 J_ASSERT(list_empty(&sbi->s_orphan));
505
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700506 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
508 /*
509 * Invalidate the journal device's buffers. We don't want them
510 * floating about in memory - the physical journal device may
511 * hotswapped, and it breaks the `ro-after' testing code.
512 */
513 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700514 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700515 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516 }
517 sb->s_fs_info = NULL;
518 kfree(sbi);
519 return;
520}
521
Christoph Lametere18b8902006-12-06 20:33:20 -0800522static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523
524/*
525 * Called inside transaction, so use GFP_NOFS
526 */
Mingming Cao617ba132006-10-11 01:20:53 -0700527static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528{
Mingming Cao617ba132006-10-11 01:20:53 -0700529 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800531 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 if (!ei)
533 return NULL;
Theodore Ts'o03010a32008-10-10 20:02:48 -0400534#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700535 ei->i_acl = EXT4_ACL_NOT_CACHED;
536 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400539 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700540 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500541 INIT_LIST_HEAD(&ei->i_prealloc_list);
542 spin_lock_init(&ei->i_prealloc_lock);
Frank Mayhar03901312009-01-07 00:06:22 -0500543 /*
544 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
545 * therefore it can be null here. Don't check it, just initialize
546 * jinode.
547 */
Jan Kara678aaf42008-07-11 19:27:31 -0400548 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400549 ei->i_reserved_data_blocks = 0;
550 ei->i_reserved_meta_blocks = 0;
551 ei->i_allocated_meta_blocks = 0;
552 ei->i_delalloc_reserved_flag = 0;
553 spin_lock_init(&(ei->i_block_reservation_lock));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 return &ei->vfs_inode;
555}
556
Mingming Cao617ba132006-10-11 01:20:53 -0700557static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700559 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
560 printk("EXT4 Inode %p: orphan list check failed!\n",
561 EXT4_I(inode));
562 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
563 EXT4_I(inode), sizeof(struct ext4_inode_info),
564 true);
565 dump_stack();
566 }
Mingming Cao617ba132006-10-11 01:20:53 -0700567 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568}
569
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700570static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700571{
Mingming Cao617ba132006-10-11 01:20:53 -0700572 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573
Christoph Lametera35afb82007-05-16 22:10:57 -0700574 INIT_LIST_HEAD(&ei->i_orphan);
Theodore Ts'o03010a32008-10-10 20:02:48 -0400575#ifdef CONFIG_EXT4_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700576 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500578 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700579 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580}
581
582static int init_inodecache(void)
583{
Mingming Cao617ba132006-10-11 01:20:53 -0700584 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
585 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 0, (SLAB_RECLAIM_ACCOUNT|
587 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900588 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700589 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590 return -ENOMEM;
591 return 0;
592}
593
594static void destroy_inodecache(void)
595{
Mingming Cao617ba132006-10-11 01:20:53 -0700596 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597}
598
Mingming Cao617ba132006-10-11 01:20:53 -0700599static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600{
Theodore Ts'o03010a32008-10-10 20:02:48 -0400601#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700602 if (EXT4_I(inode)->i_acl &&
603 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
604 posix_acl_release(EXT4_I(inode)->i_acl);
605 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 }
Mingming Cao617ba132006-10-11 01:20:53 -0700607 if (EXT4_I(inode)->i_default_acl &&
608 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
609 posix_acl_release(EXT4_I(inode)->i_default_acl);
610 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611 }
612#endif
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400613 ext4_discard_preallocations(inode);
Frank Mayhar03901312009-01-07 00:06:22 -0500614 if (EXT4_JOURNAL(inode))
615 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
Jan Kara678aaf42008-07-11 19:27:31 -0400616 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617}
618
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400619static inline void ext4_show_quota_options(struct seq_file *seq,
620 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621{
622#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700623 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624
625 if (sbi->s_jquota_fmt)
626 seq_printf(seq, ",jqfmt=%s",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400627 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628
629 if (sbi->s_qf_names[USRQUOTA])
630 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
631
632 if (sbi->s_qf_names[GRPQUOTA])
633 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
634
Mingming Cao617ba132006-10-11 01:20:53 -0700635 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636 seq_puts(seq, ",usrquota");
637
Mingming Cao617ba132006-10-11 01:20:53 -0700638 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700639 seq_puts(seq, ",grpquota");
640#endif
641}
642
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700643/*
644 * Show an option if
645 * - it's set to a non-default value OR
646 * - if the per-sb default is different from the global default
647 */
Mingming Cao617ba132006-10-11 01:20:53 -0700648static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500650 int def_errors;
651 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700652 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700653 struct ext4_sb_info *sbi = EXT4_SB(sb);
654 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700655
656 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500657 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700658
659 if (sbi->s_sb_block != 1)
660 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
661 if (test_opt(sb, MINIX_DF))
662 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500663 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700664 seq_puts(seq, ",grpid");
665 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
666 seq_puts(seq, ",nogrpid");
667 if (sbi->s_resuid != EXT4_DEF_RESUID ||
668 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
669 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
670 }
671 if (sbi->s_resgid != EXT4_DEF_RESGID ||
672 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
673 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
674 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500675 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700676 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500677 def_errors == EXT4_ERRORS_CONTINUE) {
678 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700679 }
680 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500681 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500682 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500683 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700684 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500685 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700686 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500687 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700688 seq_puts(seq, ",debug");
689 if (test_opt(sb, OLDALLOC))
690 seq_puts(seq, ",oldalloc");
Theodore Ts'o03010a32008-10-10 20:02:48 -0400691#ifdef CONFIG_EXT4_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500692 if (test_opt(sb, XATTR_USER) &&
693 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700694 seq_puts(seq, ",user_xattr");
695 if (!test_opt(sb, XATTR_USER) &&
696 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
697 seq_puts(seq, ",nouser_xattr");
698 }
699#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400700#ifdef CONFIG_EXT4_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500701 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700702 seq_puts(seq, ",acl");
703 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
704 seq_puts(seq, ",noacl");
705#endif
706 if (!test_opt(sb, RESERVATION))
707 seq_puts(seq, ",noreservation");
Theodore Ts'o30773842009-01-03 20:27:38 -0500708 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700709 seq_printf(seq, ",commit=%u",
710 (unsigned) (sbi->s_commit_interval / HZ));
711 }
Theodore Ts'o30773842009-01-03 20:27:38 -0500712 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
713 seq_printf(seq, ",min_batch_time=%u",
714 (unsigned) sbi->s_min_batch_time);
715 }
716 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
717 seq_printf(seq, ",max_batch_time=%u",
718 (unsigned) sbi->s_min_batch_time);
719 }
720
Eric Sandeen571640c2008-05-26 12:29:46 -0400721 /*
722 * We're changing the default of barrier mount option, so
723 * let's always display its mount state so it's clear what its
724 * status is.
725 */
726 seq_puts(seq, ",barrier=");
727 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400728 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
729 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700730 if (test_opt(sb, NOBH))
731 seq_puts(seq, ",nobh");
732 if (!test_opt(sb, EXTENTS))
733 seq_puts(seq, ",noextents");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500734 if (test_opt(sb, I_VERSION))
735 seq_puts(seq, ",i_version");
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400736 if (!test_opt(sb, DELALLOC))
737 seq_puts(seq, ",nodelalloc");
738
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500740 if (sbi->s_stripe)
741 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500742 /*
743 * journal mode get enabled in different ways
744 * So just print the value even if we didn't specify it
745 */
Mingming Cao617ba132006-10-11 01:20:53 -0700746 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700748 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700750 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751 seq_puts(seq, ",data=writeback");
752
Theodore Ts'o240799c2008-10-09 23:53:47 -0400753 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
754 seq_printf(seq, ",inode_readahead_blks=%u",
755 sbi->s_inode_readahead_blks);
756
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400757 if (test_opt(sb, DATA_ERR_ABORT))
758 seq_puts(seq, ",data_err=abort");
759
Mingming Cao617ba132006-10-11 01:20:53 -0700760 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 return 0;
762}
763
764
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700765static struct inode *ext4_nfs_get_inode(struct super_block *sb,
766 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769
Mingming Cao617ba132006-10-11 01:20:53 -0700770 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700772 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773 return ERR_PTR(-ESTALE);
774
775 /* iget isn't really right if the inode is currently unallocated!!
776 *
Mingming Cao617ba132006-10-11 01:20:53 -0700777 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700778 * deleted, so we should be safe.
779 *
780 * Currently we don't know the generation for parent directory, so
781 * a generation of 0 means "accept any"
782 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800783 inode = ext4_iget(sb, ino);
784 if (IS_ERR(inode))
785 return ERR_CAST(inode);
786 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787 iput(inode);
788 return ERR_PTR(-ESTALE);
789 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700790
791 return inode;
792}
793
794static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
795 int fh_len, int fh_type)
796{
797 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
798 ext4_nfs_get_inode);
799}
800
801static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
802 int fh_len, int fh_type)
803{
804 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
805 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806}
807
808#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400809#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400810#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811
Mingming Cao617ba132006-10-11 01:20:53 -0700812static int ext4_dquot_initialize(struct inode *inode, int type);
813static int ext4_dquot_drop(struct inode *inode);
814static int ext4_write_dquot(struct dquot *dquot);
815static int ext4_acquire_dquot(struct dquot *dquot);
816static int ext4_release_dquot(struct dquot *dquot);
817static int ext4_mark_dquot_dirty(struct dquot *dquot);
818static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -0700819static int ext4_quota_on(struct super_block *sb, int type, int format_id,
820 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -0700821static int ext4_quota_on_mount(struct super_block *sb, int type);
822static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700824static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700825 const char *data, size_t len, loff_t off);
826
Mingming Cao617ba132006-10-11 01:20:53 -0700827static struct dquot_operations ext4_quota_operations = {
828 .initialize = ext4_dquot_initialize,
829 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830 .alloc_space = dquot_alloc_space,
831 .alloc_inode = dquot_alloc_inode,
832 .free_space = dquot_free_space,
833 .free_inode = dquot_free_inode,
834 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700835 .write_dquot = ext4_write_dquot,
836 .acquire_dquot = ext4_acquire_dquot,
837 .release_dquot = ext4_release_dquot,
838 .mark_dirty = ext4_mark_dquot_dirty,
839 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700840};
841
Mingming Cao617ba132006-10-11 01:20:53 -0700842static struct quotactl_ops ext4_qctl_operations = {
843 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 .quota_off = vfs_quota_off,
845 .quota_sync = vfs_quota_sync,
846 .get_info = vfs_get_dqinfo,
847 .set_info = vfs_set_dqinfo,
848 .get_dqblk = vfs_get_dqblk,
849 .set_dqblk = vfs_set_dqblk
850};
851#endif
852
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800853static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700854 .alloc_inode = ext4_alloc_inode,
855 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700856 .write_inode = ext4_write_inode,
857 .dirty_inode = ext4_dirty_inode,
858 .delete_inode = ext4_delete_inode,
859 .put_super = ext4_put_super,
860 .write_super = ext4_write_super,
861 .sync_fs = ext4_sync_fs,
862 .write_super_lockfs = ext4_write_super_lockfs,
863 .unlockfs = ext4_unlockfs,
864 .statfs = ext4_statfs,
865 .remount_fs = ext4_remount,
866 .clear_inode = ext4_clear_inode,
867 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700869 .quota_read = ext4_quota_read,
870 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871#endif
872};
873
Christoph Hellwig39655162007-10-21 16:42:17 -0700874static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700875 .fh_to_dentry = ext4_fh_to_dentry,
876 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -0700877 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878};
879
880enum {
881 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
882 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -0400883 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700884 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
885 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
Theodore Ts'o30773842009-01-03 20:27:38 -0500886 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
887 Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -0500888 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400890 Opt_data_err_abort, Opt_data_err_ignore,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
892 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
893 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500894 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -0400895 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o240799c2008-10-09 23:53:47 -0400896 Opt_inode_readahead_blks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897};
898
Steven Whitehousea447c092008-10-13 10:46:57 +0100899static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900 {Opt_bsd_df, "bsddf"},
901 {Opt_minix_df, "minixdf"},
902 {Opt_grpid, "grpid"},
903 {Opt_grpid, "bsdgroups"},
904 {Opt_nogrpid, "nogrpid"},
905 {Opt_nogrpid, "sysvgroups"},
906 {Opt_resgid, "resgid=%u"},
907 {Opt_resuid, "resuid=%u"},
908 {Opt_sb, "sb=%u"},
909 {Opt_err_cont, "errors=continue"},
910 {Opt_err_panic, "errors=panic"},
911 {Opt_err_ro, "errors=remount-ro"},
912 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700913 {Opt_debug, "debug"},
914 {Opt_oldalloc, "oldalloc"},
915 {Opt_orlov, "orlov"},
916 {Opt_user_xattr, "user_xattr"},
917 {Opt_nouser_xattr, "nouser_xattr"},
918 {Opt_acl, "acl"},
919 {Opt_noacl, "noacl"},
920 {Opt_reservation, "reservation"},
921 {Opt_noreservation, "noreservation"},
922 {Opt_noload, "noload"},
923 {Opt_nobh, "nobh"},
924 {Opt_bh, "bh"},
925 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -0500926 {Opt_min_batch_time, "min_batch_time=%u"},
927 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700928 {Opt_journal_update, "journal=update"},
929 {Opt_journal_inum, "journal=%u"},
930 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -0500931 {Opt_journal_checksum, "journal_checksum"},
932 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 {Opt_abort, "abort"},
934 {Opt_data_journal, "data=journal"},
935 {Opt_data_ordered, "data=ordered"},
936 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400937 {Opt_data_err_abort, "data_err=abort"},
938 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700939 {Opt_offusrjquota, "usrjquota="},
940 {Opt_usrjquota, "usrjquota=%s"},
941 {Opt_offgrpjquota, "grpjquota="},
942 {Opt_grpjquota, "grpjquota=%s"},
943 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
944 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
945 {Opt_grpquota, "grpquota"},
946 {Opt_noquota, "noquota"},
947 {Opt_quota, "quota"},
948 {Opt_usrquota, "usrquota"},
949 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -0700950 {Opt_extents, "extents"},
Mingming Cao1e2462f2007-07-18 09:00:55 -0400951 {Opt_noextents, "noextents"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500952 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -0500953 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -0400955 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400956 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o240799c2008-10-09 23:53:47 -0400957 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -0400958 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959};
960
Mingming Cao617ba132006-10-11 01:20:53 -0700961static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962{
Mingming Cao617ba132006-10-11 01:20:53 -0700963 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964 char *options = (char *) *data;
965
966 if (!options || strncmp(options, "sb=", 3) != 0)
967 return 1; /* Default location */
968 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -0700969 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970 sb_block = simple_strtoul(options, &options, 0);
971 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400972 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 (char *) *data);
974 return 1;
975 }
976 if (*options == ',')
977 options++;
978 *data = (void *) options;
979 return sb_block;
980}
981
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400982static int parse_options(char *options, struct super_block *sb,
983 unsigned int *inum, unsigned long *journal_devnum,
984 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985{
Mingming Cao617ba132006-10-11 01:20:53 -0700986 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400987 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700988 substring_t args[MAX_OPT_ARGS];
989 int data_opt = 0;
990 int option;
991#ifdef CONFIG_QUOTA
Jan Karadfc5d032008-05-13 19:11:51 -0400992 int qtype, qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 char *qname;
994#endif
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -0400995 ext4_fsblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996
997 if (!options)
998 return 1;
999
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001000 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001 int token;
1002 if (!*p)
1003 continue;
1004
1005 token = match_token(p, tokens, args);
1006 switch (token) {
1007 case Opt_bsd_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001008 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001009 break;
1010 case Opt_minix_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001011 set_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001012 break;
1013 case Opt_grpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001014 set_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015 break;
1016 case Opt_nogrpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001017 clear_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018 break;
1019 case Opt_resuid:
1020 if (match_int(&args[0], &option))
1021 return 0;
1022 sbi->s_resuid = option;
1023 break;
1024 case Opt_resgid:
1025 if (match_int(&args[0], &option))
1026 return 0;
1027 sbi->s_resgid = option;
1028 break;
1029 case Opt_sb:
1030 /* handled by get_sb_block() instead of here */
1031 /* *sb_block = match_int(&args[0]); */
1032 break;
1033 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001034 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1035 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1036 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001037 break;
1038 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001039 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1040 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1041 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001042 break;
1043 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001044 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1045 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1046 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 break;
1048 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001049 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001052 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 break;
1054 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001055 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 break;
1057 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001058 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001059 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001060#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001062 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 break;
1064 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001065 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001066 break;
1067#else
1068 case Opt_user_xattr:
1069 case Opt_nouser_xattr:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001070 printk(KERN_ERR "EXT4 (no)user_xattr options "
1071 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001072 break;
1073#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001074#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075 case Opt_acl:
1076 set_opt(sbi->s_mount_opt, POSIX_ACL);
1077 break;
1078 case Opt_noacl:
1079 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1080 break;
1081#else
1082 case Opt_acl:
1083 case Opt_noacl:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001084 printk(KERN_ERR "EXT4 (no)acl options "
1085 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001086 break;
1087#endif
1088 case Opt_reservation:
1089 set_opt(sbi->s_mount_opt, RESERVATION);
1090 break;
1091 case Opt_noreservation:
1092 clear_opt(sbi->s_mount_opt, RESERVATION);
1093 break;
1094 case Opt_journal_update:
1095 /* @@@ FIXME */
1096 /* Eventually we will want to be able to create
1097 a journal file here. For now, only allow the
1098 user to specify an existing inode to be the
1099 journal file. */
1100 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001101 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 "journal on remount\n");
1103 return 0;
1104 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001105 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106 break;
1107 case Opt_journal_inum:
1108 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001109 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001110 "journal on remount\n");
1111 return 0;
1112 }
1113 if (match_int(&args[0], &option))
1114 return 0;
1115 *inum = option;
1116 break;
1117 case Opt_journal_dev:
1118 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001119 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001120 "journal on remount\n");
1121 return 0;
1122 }
1123 if (match_int(&args[0], &option))
1124 return 0;
1125 *journal_devnum = option;
1126 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001127 case Opt_journal_checksum:
1128 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1129 break;
1130 case Opt_journal_async_commit:
1131 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1132 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1133 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001135 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 break;
1137 case Opt_commit:
1138 if (match_int(&args[0], &option))
1139 return 0;
1140 if (option < 0)
1141 return 0;
1142 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001143 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001144 sbi->s_commit_interval = HZ * option;
1145 break;
Theodore Ts'o30773842009-01-03 20:27:38 -05001146 case Opt_max_batch_time:
1147 if (match_int(&args[0], &option))
1148 return 0;
1149 if (option < 0)
1150 return 0;
1151 if (option == 0)
1152 option = EXT4_DEF_MAX_BATCH_TIME;
1153 sbi->s_max_batch_time = option;
1154 break;
1155 case Opt_min_batch_time:
1156 if (match_int(&args[0], &option))
1157 return 0;
1158 if (option < 0)
1159 return 0;
1160 sbi->s_min_batch_time = option;
1161 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001162 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001163 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 goto datacheck;
1165 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001166 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001167 goto datacheck;
1168 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001169 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170 datacheck:
1171 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001172 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001173 != data_opt) {
1174 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001175 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001176 "mode on remount\n");
1177 return 0;
1178 }
1179 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001180 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181 sbi->s_mount_opt |= data_opt;
1182 }
1183 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001184 case Opt_data_err_abort:
1185 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1186 break;
1187 case Opt_data_err_ignore:
1188 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1189 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001190#ifdef CONFIG_QUOTA
1191 case Opt_usrjquota:
1192 qtype = USRQUOTA;
1193 goto set_qf_name;
1194 case Opt_grpjquota:
1195 qtype = GRPQUOTA;
1196set_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001197 if ((sb_any_quota_enabled(sb) ||
1198 sb_any_quota_suspended(sb)) &&
1199 !sbi->s_qf_names[qtype]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001200 printk(KERN_ERR
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001201 "EXT4-fs: Cannot change journaled "
1202 "quota options when quota turned on.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203 return 0;
1204 }
1205 qname = match_strdup(&args[0]);
1206 if (!qname) {
1207 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001208 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209 "storing quotafile name.\n");
1210 return 0;
1211 }
1212 if (sbi->s_qf_names[qtype] &&
1213 strcmp(sbi->s_qf_names[qtype], qname)) {
1214 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001215 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216 "specified.\n", QTYPE2NAME(qtype));
1217 kfree(qname);
1218 return 0;
1219 }
1220 sbi->s_qf_names[qtype] = qname;
1221 if (strchr(sbi->s_qf_names[qtype], '/')) {
1222 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001223 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001224 "filesystem root.\n");
1225 kfree(sbi->s_qf_names[qtype]);
1226 sbi->s_qf_names[qtype] = NULL;
1227 return 0;
1228 }
1229 set_opt(sbi->s_mount_opt, QUOTA);
1230 break;
1231 case Opt_offusrjquota:
1232 qtype = USRQUOTA;
1233 goto clear_qf_name;
1234 case Opt_offgrpjquota:
1235 qtype = GRPQUOTA;
1236clear_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001237 if ((sb_any_quota_enabled(sb) ||
1238 sb_any_quota_suspended(sb)) &&
1239 sbi->s_qf_names[qtype]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001240 printk(KERN_ERR "EXT4-fs: Cannot change "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001241 "journaled quota options when "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 "quota turned on.\n");
1243 return 0;
1244 }
1245 /*
1246 * The space will be released later when all options
1247 * are confirmed to be correct
1248 */
1249 sbi->s_qf_names[qtype] = NULL;
1250 break;
1251 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001252 qfmt = QFMT_VFS_OLD;
1253 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001255 qfmt = QFMT_VFS_V0;
1256set_qf_format:
1257 if ((sb_any_quota_enabled(sb) ||
1258 sb_any_quota_suspended(sb)) &&
1259 sbi->s_jquota_fmt != qfmt) {
1260 printk(KERN_ERR "EXT4-fs: Cannot change "
1261 "journaled quota options when "
1262 "quota turned on.\n");
1263 return 0;
1264 }
1265 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001266 break;
1267 case Opt_quota:
1268 case Opt_usrquota:
1269 set_opt(sbi->s_mount_opt, QUOTA);
1270 set_opt(sbi->s_mount_opt, USRQUOTA);
1271 break;
1272 case Opt_grpquota:
1273 set_opt(sbi->s_mount_opt, QUOTA);
1274 set_opt(sbi->s_mount_opt, GRPQUOTA);
1275 break;
1276 case Opt_noquota:
1277 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001278 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001279 "options when quota turned on.\n");
1280 return 0;
1281 }
1282 clear_opt(sbi->s_mount_opt, QUOTA);
1283 clear_opt(sbi->s_mount_opt, USRQUOTA);
1284 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1285 break;
1286#else
1287 case Opt_quota:
1288 case Opt_usrquota:
1289 case Opt_grpquota:
Jan Karacd59e7b2008-05-13 19:11:51 -04001290 printk(KERN_ERR
1291 "EXT4-fs: quota options not supported.\n");
1292 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 case Opt_usrjquota:
1294 case Opt_grpjquota:
1295 case Opt_offusrjquota:
1296 case Opt_offgrpjquota:
1297 case Opt_jqfmt_vfsold:
1298 case Opt_jqfmt_vfsv0:
1299 printk(KERN_ERR
Jan Karacd59e7b2008-05-13 19:11:51 -04001300 "EXT4-fs: journaled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001301 "supported.\n");
1302 break;
1303 case Opt_noquota:
1304 break;
1305#endif
1306 case Opt_abort:
1307 set_opt(sbi->s_mount_opt, ABORT);
1308 break;
1309 case Opt_barrier:
1310 if (match_int(&args[0], &option))
1311 return 0;
1312 if (option)
1313 set_opt(sbi->s_mount_opt, BARRIER);
1314 else
1315 clear_opt(sbi->s_mount_opt, BARRIER);
1316 break;
1317 case Opt_ignore:
1318 break;
1319 case Opt_resize:
1320 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001321 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001322 "for remount\n");
1323 return 0;
1324 }
1325 if (match_int(&args[0], &option) != 0)
1326 return 0;
1327 *n_blocks_count = option;
1328 break;
1329 case Opt_nobh:
1330 set_opt(sbi->s_mount_opt, NOBH);
1331 break;
1332 case Opt_bh:
1333 clear_opt(sbi->s_mount_opt, NOBH);
1334 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001335 case Opt_extents:
Eric Sandeene4079a12008-07-11 19:27:31 -04001336 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1337 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1338 ext4_warning(sb, __func__,
1339 "extents feature not enabled "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001340 "on this filesystem, use tune2fs");
Eric Sandeene4079a12008-07-11 19:27:31 -04001341 return 0;
1342 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001343 set_opt(sbi->s_mount_opt, EXTENTS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001344 break;
Mingming Cao1e2462f2007-07-18 09:00:55 -04001345 case Opt_noextents:
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001346 /*
1347 * When e2fsprogs support resizing an already existing
1348 * ext3 file system to greater than 2**32 we need to
1349 * add support to block allocator to handle growing
1350 * already existing block mapped inode so that blocks
1351 * allocated for them fall within 2**32
1352 */
1353 last_block = ext4_blocks_count(sbi->s_es) - 1;
1354 if (last_block > 0xffffffffULL) {
1355 printk(KERN_ERR "EXT4-fs: Filesystem too "
1356 "large to mount with "
1357 "-o noextents options\n");
1358 return 0;
1359 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001360 clear_opt(sbi->s_mount_opt, EXTENTS);
Mingming Cao1e2462f2007-07-18 09:00:55 -04001361 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001362 case Opt_i_version:
1363 set_opt(sbi->s_mount_opt, I_VERSION);
1364 sb->s_flags |= MS_I_VERSION;
1365 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001366 case Opt_nodelalloc:
1367 clear_opt(sbi->s_mount_opt, DELALLOC);
1368 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001369 case Opt_stripe:
1370 if (match_int(&args[0], &option))
1371 return 0;
1372 if (option < 0)
1373 return 0;
1374 sbi->s_stripe = option;
1375 break;
Alex Tomas64769242008-07-11 19:27:31 -04001376 case Opt_delalloc:
1377 set_opt(sbi->s_mount_opt, DELALLOC);
1378 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001379 case Opt_inode_readahead_blks:
1380 if (match_int(&args[0], &option))
1381 return 0;
1382 if (option < 0 || option > (1 << 30))
1383 return 0;
1384 sbi->s_inode_readahead_blks = option;
1385 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001386 default:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001387 printk(KERN_ERR
1388 "EXT4-fs: Unrecognized mount option \"%s\" "
1389 "or missing value\n", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001390 return 0;
1391 }
1392 }
1393#ifdef CONFIG_QUOTA
1394 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001395 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001396 sbi->s_qf_names[USRQUOTA])
1397 clear_opt(sbi->s_mount_opt, USRQUOTA);
1398
Mingming Cao617ba132006-10-11 01:20:53 -07001399 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001400 sbi->s_qf_names[GRPQUOTA])
1401 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1402
1403 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001404 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001405 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001406 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1407 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001408 "format mixing.\n");
1409 return 0;
1410 }
1411
1412 if (!sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001413 printk(KERN_ERR "EXT4-fs: journaled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001414 "not specified.\n");
1415 return 0;
1416 }
1417 } else {
1418 if (sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001419 printk(KERN_ERR "EXT4-fs: journaled quota format "
1420 "specified with no journaling "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001421 "enabled.\n");
1422 return 0;
1423 }
1424 }
1425#endif
1426 return 1;
1427}
1428
Mingming Cao617ba132006-10-11 01:20:53 -07001429static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001430 int read_only)
1431{
Mingming Cao617ba132006-10-11 01:20:53 -07001432 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001433 int res = 0;
1434
Mingming Cao617ba132006-10-11 01:20:53 -07001435 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001436 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1437 "forcing read-only mode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438 res = MS_RDONLY;
1439 }
1440 if (read_only)
1441 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001442 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001443 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1444 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001445 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001446 printk(KERN_WARNING
1447 "EXT4-fs warning: mounting fs with errors, "
1448 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1450 le16_to_cpu(es->s_mnt_count) >=
1451 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001452 printk(KERN_WARNING
1453 "EXT4-fs warning: maximal mount count reached, "
1454 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001455 else if (le32_to_cpu(es->s_checkinterval) &&
1456 (le32_to_cpu(es->s_lastcheck) +
1457 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001458 printk(KERN_WARNING
1459 "EXT4-fs warning: checktime reached, "
1460 "running e2fsck is recommended\n");
Frank Mayhar03901312009-01-07 00:06:22 -05001461 if (!sbi->s_journal)
1462 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001463 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001464 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001465 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001466 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001467 ext4_update_dynamic_rev(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05001468 if (sbi->s_journal)
1469 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001470
Mingming Cao617ba132006-10-11 01:20:53 -07001471 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001472 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001473 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001474 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1475 sb->s_blocksize,
1476 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001477 EXT4_BLOCKS_PER_GROUP(sb),
1478 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001479 sbi->s_mount_opt);
1480
Frank Mayhar03901312009-01-07 00:06:22 -05001481 if (EXT4_SB(sb)->s_journal) {
1482 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1483 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1484 "external", EXT4_SB(sb)->s_journal->j_devname);
1485 } else {
1486 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1487 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001488 return res;
1489}
1490
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001491static int ext4_fill_flex_info(struct super_block *sb)
1492{
1493 struct ext4_sb_info *sbi = EXT4_SB(sb);
1494 struct ext4_group_desc *gdp = NULL;
1495 struct buffer_head *bh;
1496 ext4_group_t flex_group_count;
1497 ext4_group_t flex_group;
1498 int groups_per_flex = 0;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001499 int i;
1500
1501 if (!sbi->s_es->s_log_groups_per_flex) {
1502 sbi->s_log_groups_per_flex = 0;
1503 return 1;
1504 }
1505
1506 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1507 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1508
Frederic Bohec62a11f2008-09-08 10:20:24 -04001509 /* We allocate both existing and potentially added groups */
1510 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001511 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1512 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Li Zefanec05e862008-07-24 12:49:59 -04001513 sbi->s_flex_groups = kzalloc(flex_group_count *
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001514 sizeof(struct flex_groups), GFP_KERNEL);
1515 if (sbi->s_flex_groups == NULL) {
Li Zefanec05e862008-07-24 12:49:59 -04001516 printk(KERN_ERR "EXT4-fs: not enough memory for "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001517 "%u flex groups\n", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001518 goto failed;
1519 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001520
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001521 for (i = 0; i < sbi->s_groups_count; i++) {
1522 gdp = ext4_get_group_desc(sb, i, &bh);
1523
1524 flex_group = ext4_flex_group(sbi, i);
1525 sbi->s_flex_groups[flex_group].free_inodes +=
1526 le16_to_cpu(gdp->bg_free_inodes_count);
1527 sbi->s_flex_groups[flex_group].free_blocks +=
1528 le16_to_cpu(gdp->bg_free_blocks_count);
1529 }
1530
1531 return 1;
1532failed:
1533 return 0;
1534}
1535
Andreas Dilger717d50e2007-10-16 18:38:25 -04001536__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1537 struct ext4_group_desc *gdp)
1538{
1539 __u16 crc = 0;
1540
1541 if (sbi->s_es->s_feature_ro_compat &
1542 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1543 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1544 __le32 le_group = cpu_to_le32(block_group);
1545
1546 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1547 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1548 crc = crc16(crc, (__u8 *)gdp, offset);
1549 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1550 /* for checksum of struct ext4_group_desc do the rest...*/
1551 if ((sbi->s_es->s_feature_incompat &
1552 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1553 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1554 crc = crc16(crc, (__u8 *)gdp + offset,
1555 le16_to_cpu(sbi->s_es->s_desc_size) -
1556 offset);
1557 }
1558
1559 return cpu_to_le16(crc);
1560}
1561
1562int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1563 struct ext4_group_desc *gdp)
1564{
1565 if ((sbi->s_es->s_feature_ro_compat &
1566 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1567 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1568 return 0;
1569
1570 return 1;
1571}
1572
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001574static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001575{
Mingming Cao617ba132006-10-11 01:20:53 -07001576 struct ext4_sb_info *sbi = EXT4_SB(sb);
1577 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1578 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001579 ext4_fsblk_t block_bitmap;
1580 ext4_fsblk_t inode_bitmap;
1581 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001582 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001583 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584
Jose R. Santosce421582007-10-16 18:38:25 -04001585 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1586 flexbg_flag = 1;
1587
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001588 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001590 for (i = 0; i < sbi->s_groups_count; i++) {
1591 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1592
Jose R. Santosce421582007-10-16 18:38:25 -04001593 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001594 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001595 else
1596 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001597 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001598
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001599 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001600 if (block_bitmap < first_block || block_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001601 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001602 "Block bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001603 "(block %llu)!\n", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001604 return 0;
1605 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001606 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001607 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001608 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001609 "Inode bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001610 "(block %llu)!\n", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 return 0;
1612 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001613 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001614 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001615 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001616 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001617 "Inode table for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001618 "(block %llu)!\n", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001619 return 0;
1620 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001621 spin_lock(sb_bgl_lock(sbi, i));
Andreas Dilger717d50e2007-10-16 18:38:25 -04001622 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001623 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001624 "Checksum for group %u failed (%u!=%u)\n",
Josef Bacikc19204b2008-04-29 22:00:28 -04001625 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1626 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001627 if (!(sb->s_flags & MS_RDONLY)) {
1628 spin_unlock(sb_bgl_lock(sbi, i));
Theodore Ts'o8a266462008-07-26 14:34:21 -04001629 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001630 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001631 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001632 spin_unlock(sb_bgl_lock(sbi, i));
Jose R. Santosce421582007-10-16 18:38:25 -04001633 if (!flexbg_flag)
1634 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001635 }
1636
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001637 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001638 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639 return 1;
1640}
1641
Mingming Cao617ba132006-10-11 01:20:53 -07001642/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643 * the superblock) which were deleted from all directories, but held open by
1644 * a process at the time of a crash. We walk the list and try to delete these
1645 * inodes at recovery time (only with a read-write filesystem).
1646 *
1647 * In order to keep the orphan inode chain consistent during traversal (in
1648 * case of crash during recovery), we link each inode into the superblock
1649 * orphan list_head and handle it the same way as an inode deletion during
1650 * normal operation (which journals the operations for us).
1651 *
1652 * We only do an iget() and an iput() on each inode, which is very safe if we
1653 * accidentally point at an in-use or already deleted inode. The worst that
1654 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001655 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656 * e2fsck was run on this filesystem, and it must have already done the orphan
1657 * inode cleanup for us, so we can safely abort without any further action.
1658 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001659static void ext4_orphan_cleanup(struct super_block *sb,
1660 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001661{
1662 unsigned int s_flags = sb->s_flags;
1663 int nr_orphans = 0, nr_truncates = 0;
1664#ifdef CONFIG_QUOTA
1665 int i;
1666#endif
1667 if (!es->s_last_orphan) {
1668 jbd_debug(4, "no orphan inodes to clean up\n");
1669 return;
1670 }
1671
Eric Sandeena8f48a92006-12-06 20:40:13 -08001672 if (bdev_read_only(sb->s_bdev)) {
1673 printk(KERN_ERR "EXT4-fs: write access "
1674 "unavailable, skipping orphan cleanup.\n");
1675 return;
1676 }
1677
Mingming Cao617ba132006-10-11 01:20:53 -07001678 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001679 if (es->s_last_orphan)
1680 jbd_debug(1, "Errors on filesystem, "
1681 "clearing orphan list.\n");
1682 es->s_last_orphan = 0;
1683 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1684 return;
1685 }
1686
1687 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001688 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689 sb->s_id);
1690 sb->s_flags &= ~MS_RDONLY;
1691 }
1692#ifdef CONFIG_QUOTA
1693 /* Needed for iput() to work correctly and not trash data */
1694 sb->s_flags |= MS_ACTIVE;
1695 /* Turn on quotas so that they are updated correctly */
1696 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001697 if (EXT4_SB(sb)->s_qf_names[i]) {
1698 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001699 if (ret < 0)
1700 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001701 "EXT4-fs: Cannot turn on journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001702 "quota: error %d\n", ret);
1703 }
1704 }
1705#endif
1706
1707 while (es->s_last_orphan) {
1708 struct inode *inode;
1709
Josef Bacik97bd42b2008-04-29 22:04:56 -04001710 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1711 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001712 es->s_last_orphan = 0;
1713 break;
1714 }
1715
Mingming Cao617ba132006-10-11 01:20:53 -07001716 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717 DQUOT_INIT(inode);
1718 if (inode->i_nlink) {
1719 printk(KERN_DEBUG
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001720 "%s: truncating inode %lu to %lld bytes\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001721 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001722 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001723 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001724 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001725 nr_truncates++;
1726 } else {
1727 printk(KERN_DEBUG
1728 "%s: deleting unreferenced inode %lu\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001729 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730 jbd_debug(2, "deleting unreferenced inode %lu\n",
1731 inode->i_ino);
1732 nr_orphans++;
1733 }
1734 iput(inode); /* The delete magic happens here! */
1735 }
1736
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001737#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001738
1739 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001740 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741 sb->s_id, PLURAL(nr_orphans));
1742 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001743 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744 sb->s_id, PLURAL(nr_truncates));
1745#ifdef CONFIG_QUOTA
1746 /* Turn quotas off */
1747 for (i = 0; i < MAXQUOTAS; i++) {
1748 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07001749 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001750 }
1751#endif
1752 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1753}
Eric Sandeencd2291a2008-01-28 23:58:27 -05001754/*
1755 * Maximal extent format file size.
1756 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1757 * extent format containers, within a sector_t, and within i_blocks
1758 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1759 * so that won't be a limiting factor.
1760 *
1761 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1762 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001763static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05001764{
1765 loff_t res;
1766 loff_t upper_limit = MAX_LFS_FILESIZE;
1767
1768 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001769 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05001770 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001771 * CONFIG_LBD is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05001772 * i_block represent total blocks in 512 bytes
1773 * 32 == size of vfs inode i_blocks * 8
1774 */
1775 upper_limit = (1LL << 32) - 1;
1776
1777 /* total blocks in file system block size */
1778 upper_limit >>= (blkbits - 9);
1779 upper_limit <<= blkbits;
1780 }
1781
1782 /* 32-bit extent-start container, ee_block */
1783 res = 1LL << 32;
1784 res <<= blkbits;
1785 res -= 1;
1786
1787 /* Sanity check against vm- & vfs- imposed limits */
1788 if (res > upper_limit)
1789 res = upper_limit;
1790
1791 return res;
1792}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001794/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05001795 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001796 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1797 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001799static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001800{
Mingming Cao617ba132006-10-11 01:20:53 -07001801 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001802 int meta_blocks;
1803 loff_t upper_limit;
1804 /* This is calculated to be the largest file size for a
Eric Sandeencd2291a2008-01-28 23:58:27 -05001805 * dense, bitmapped file such that the total number of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001806 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001807 * does not exceed 2^48 -1
1808 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1809 * total number of 512 bytes blocks of the file
1810 */
1811
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001812 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001813 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001814 * !has_huge_files or CONFIG_LBD is not enabled
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001815 * implies the inode i_block represent total blocks in
1816 * 512 bytes 32 == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001817 */
1818 upper_limit = (1LL << 32) - 1;
1819
1820 /* total blocks in file system block size */
1821 upper_limit >>= (bits - 9);
1822
1823 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05001824 /*
1825 * We use 48 bit ext4_inode i_blocks
1826 * With EXT4_HUGE_FILE_FL set the i_blocks
1827 * represent total number of blocks in
1828 * file system block size
1829 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001830 upper_limit = (1LL << 48) - 1;
1831
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001832 }
1833
1834 /* indirect blocks */
1835 meta_blocks = 1;
1836 /* double indirect blocks */
1837 meta_blocks += 1 + (1LL << (bits-2));
1838 /* tripple indirect blocks */
1839 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1840
1841 upper_limit -= meta_blocks;
1842 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001843
1844 res += 1LL << (bits-2);
1845 res += 1LL << (2*(bits-2));
1846 res += 1LL << (3*(bits-2));
1847 res <<= bits;
1848 if (res > upper_limit)
1849 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001850
1851 if (res > MAX_LFS_FILESIZE)
1852 res = MAX_LFS_FILESIZE;
1853
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854 return res;
1855}
1856
Mingming Cao617ba132006-10-11 01:20:53 -07001857static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001858 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001859{
Mingming Cao617ba132006-10-11 01:20:53 -07001860 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001861 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862 int has_super = 0;
1863
1864 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1865
Mingming Cao617ba132006-10-11 01:20:53 -07001866 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001867 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001868 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001869 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001870 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001871 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001872 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001873}
1874
Alex Tomasc9de5602008-01-29 00:19:52 -05001875/**
1876 * ext4_get_stripe_size: Get the stripe size.
1877 * @sbi: In memory super block info
1878 *
1879 * If we have specified it via mount option, then
1880 * use the mount option value. If the value specified at mount time is
1881 * greater than the blocks per group use the super block value.
1882 * If the super block value is greater than blocks per group return 0.
1883 * Allocator needs it be less than blocks per group.
1884 *
1885 */
1886static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1887{
1888 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1889 unsigned long stripe_width =
1890 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1891
1892 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1893 return sbi->s_stripe;
1894
1895 if (stripe_width <= sbi->s_blocks_per_group)
1896 return stripe_width;
1897
1898 if (stride <= sbi->s_blocks_per_group)
1899 return stride;
1900
1901 return 0;
1902}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001903
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001904static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04001905 __releases(kernel_lock)
1906 __acquires(kernel_lock)
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001907
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001908{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001909 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001910 struct ext4_super_block *es = NULL;
1911 struct ext4_sb_info *sbi;
1912 ext4_fsblk_t block;
1913 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001914 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001915 unsigned long offset = 0;
1916 unsigned int journal_inum = 0;
1917 unsigned long journal_devnum = 0;
1918 unsigned long def_mount_opts;
1919 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04001920 char *cp;
Frank Mayhar03901312009-01-07 00:06:22 -05001921 const char *descr;
David Howells1d1fe1e2008-02-07 00:15:37 -08001922 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001923 int blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001924 int db_count;
1925 int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001926 int needs_recovery, has_huge_files;
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05001927 int features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001928 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07001929 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001930
1931 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1932 if (!sbi)
1933 return -ENOMEM;
1934 sb->s_fs_info = sbi;
1935 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001936 sbi->s_resuid = EXT4_DEF_RESUID;
1937 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001938 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07001939 sbi->s_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001940
1941 unlock_kernel();
1942
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04001943 /* Cleanup superblock name */
1944 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
1945 *cp = '!';
1946
Mingming Cao617ba132006-10-11 01:20:53 -07001947 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001948 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07001949 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001950 goto out_fail;
1951 }
1952
1953 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001954 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001955 * block sizes. We need to calculate the offset from buffer start.
1956 */
Mingming Cao617ba132006-10-11 01:20:53 -07001957 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001958 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1959 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001960 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001961 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001962 }
1963
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001964 if (!(bh = sb_bread(sb, logical_sb_block))) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001965 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001966 goto out_fail;
1967 }
1968 /*
1969 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07001970 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001971 */
Mingming Cao617ba132006-10-11 01:20:53 -07001972 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001973 sbi->s_es = es;
1974 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07001975 if (sb->s_magic != EXT4_SUPER_MAGIC)
1976 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001977
1978 /* Set defaults before we parse the mount options */
1979 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07001980 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001981 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07001982 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001983 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07001984 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001985 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04001986#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07001987 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001988 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001989#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001990#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07001991 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001992 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001993#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001994 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1995 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1996 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1997 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1998 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1999 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002000
Mingming Cao617ba132006-10-11 01:20:53 -07002001 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002002 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002003 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002004 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002005 else
2006 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002007
2008 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2009 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
Theodore Ts'o30773842009-01-03 20:27:38 -05002010 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2011 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2012 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013
2014 set_opt(sbi->s_mount_opt, RESERVATION);
Eric Sandeen571640c2008-05-26 12:29:46 -04002015 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002016
Mingming Cao1e2462f2007-07-18 09:00:55 -04002017 /*
2018 * turn on extents feature by default in ext4 filesystem
Eric Sandeene4079a12008-07-11 19:27:31 -04002019 * only if feature flag already set by mkfs or tune2fs.
2020 * Use -o noextents to turn it off
Mingming Cao1e2462f2007-07-18 09:00:55 -04002021 */
Eric Sandeene4079a12008-07-11 19:27:31 -04002022 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2023 set_opt(sbi->s_mount_opt, EXTENTS);
2024 else
2025 ext4_warning(sb, __func__,
2026 "extents feature not enabled on this filesystem, "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05002027 "use tune2fs.");
Mingming Cao1e2462f2007-07-18 09:00:55 -04002028
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002029 /*
2030 * enable delayed allocation by default
2031 * Use -o nodelalloc to turn it off
2032 */
2033 set_opt(sbi->s_mount_opt, DELALLOC);
2034
2035
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002036 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2037 NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002038 goto failed_mount;
2039
2040 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002041 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002042
Mingming Cao617ba132006-10-11 01:20:53 -07002043 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2044 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2045 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2046 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002047 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002048 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002049 "running e2fsck is recommended\n");
Theodore Tso469108f2008-02-10 01:11:44 -05002050
2051 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052 * Check feature flags regardless of the revision level, since we
2053 * previously didn't change the revision level when setting the flags,
2054 * so there is a chance incompat flags are set on a rev 0 filesystem.
2055 */
Mingming Cao617ba132006-10-11 01:20:53 -07002056 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002058 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002059 "unsupported optional features (%x).\n", sb->s_id,
2060 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2061 ~EXT4_FEATURE_INCOMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002062 goto failed_mount;
2063 }
Mingming Cao617ba132006-10-11 01:20:53 -07002064 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002065 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002066 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002067 "unsupported optional features (%x).\n", sb->s_id,
2068 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2069 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002070 goto failed_mount;
2071 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002072 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2073 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2074 if (has_huge_files) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002075 /*
2076 * Large file size enabled file system can only be
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002077 * mount if kernel is build with CONFIG_LBD
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002078 */
2079 if (sizeof(root->i_blocks) < sizeof(u64) &&
2080 !(sb->s_flags & MS_RDONLY)) {
2081 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2082 "files cannot be mounted read-write "
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002083 "without CONFIG_LBD.\n", sb->s_id);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002084 goto failed_mount;
2085 }
2086 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002087 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2088
Mingming Cao617ba132006-10-11 01:20:53 -07002089 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2090 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002091 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002092 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002093 blocksize, sb->s_id);
2094 goto failed_mount;
2095 }
2096
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002097 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002098
2099 /* Validate the filesystem blocksize */
2100 if (!sb_set_blocksize(sb, blocksize)) {
2101 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2102 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002103 goto failed_mount;
2104 }
2105
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002106 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002107 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2108 offset = do_div(logical_sb_block, blocksize);
2109 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 if (!bh) {
2111 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002112 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002113 goto failed_mount;
2114 }
Mingming Cao617ba132006-10-11 01:20:53 -07002115 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002116 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002117 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002118 printk(KERN_ERR
2119 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002120 goto failed_mount;
2121 }
2122 }
2123
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002124 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2125 has_huge_files);
2126 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002127
Mingming Cao617ba132006-10-11 01:20:53 -07002128 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2129 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2130 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002131 } else {
2132 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2133 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002134 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002135 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002136 (sbi->s_inode_size > blocksize)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002137 printk(KERN_ERR
2138 "EXT4-fs: unsupported inode size: %d\n",
2139 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002140 goto failed_mount;
2141 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002142 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2143 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002144 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002145 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2146 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002147 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002148 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002149 !is_power_of_2(sbi->s_desc_size)) {
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002150 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002151 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002152 sbi->s_desc_size);
2153 goto failed_mount;
2154 }
2155 } else
2156 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002157 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002159 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002160 goto cantfind_ext4;
2161 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002162 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002163 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002164 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2165 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002166 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002167 sbi->s_sbh = bh;
2168 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002169 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2170 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002171 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002172 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2173 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002174 i = le32_to_cpu(es->s_flags);
2175 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2176 sbi->s_hash_unsigned = 3;
2177 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2178#ifdef __CHAR_UNSIGNED__
2179 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2180 sbi->s_hash_unsigned = 3;
2181#else
2182 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2183#endif
2184 sb->s_dirt = 1;
2185 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002186
2187 if (sbi->s_blocks_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002188 printk(KERN_ERR
2189 "EXT4-fs: #blocks per group too big: %lu\n",
2190 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002191 goto failed_mount;
2192 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193 if (sbi->s_inodes_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002194 printk(KERN_ERR
2195 "EXT4-fs: #inodes per group too big: %lu\n",
2196 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002197 goto failed_mount;
2198 }
2199
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002200 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002201 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002202 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002203 " too large to mount safely\n", sb->s_id);
2204 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07002205 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002206 "enabled\n");
2207 goto failed_mount;
2208 }
2209
Mingming Cao617ba132006-10-11 01:20:53 -07002210 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2211 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002212
2213 /* ensure blocks_count calculation below doesn't sign-extend */
2214 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2215 le32_to_cpu(es->s_first_data_block) + 1) {
2216 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2217 "first data block %u, blocks per group %lu\n",
2218 ext4_blocks_count(es),
2219 le32_to_cpu(es->s_first_data_block),
2220 EXT4_BLOCKS_PER_GROUP(sb));
2221 goto failed_mount;
2222 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002223 blocks_count = (ext4_blocks_count(es) -
2224 le32_to_cpu(es->s_first_data_block) +
2225 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2226 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2227 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07002228 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2229 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002230 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002231 GFP_KERNEL);
2232 if (sbi->s_group_desc == NULL) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002233 printk(KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002234 goto failed_mount;
2235 }
2236
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002237#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002238 if (ext4_proc_root)
2239 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2240
Theodore Ts'o240799c2008-10-09 23:53:47 -04002241 if (sbi->s_proc)
2242 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2243 &ext4_ui_proc_fops,
2244 &sbi->s_inode_readahead_blks);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002245#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002246
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002247 bgl_lock_init(&sbi->s_blockgroup_lock);
2248
2249 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002250 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002251 sbi->s_group_desc[i] = sb_bread(sb, block);
2252 if (!sbi->s_group_desc[i]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002253 printk(KERN_ERR "EXT4-fs: "
2254 "can't read group descriptor %d\n", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002255 db_count = i;
2256 goto failed_mount2;
2257 }
2258 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002259 if (!ext4_check_descriptors(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002260 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002261 goto failed_mount2;
2262 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002263 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2264 if (!ext4_fill_flex_info(sb)) {
2265 printk(KERN_ERR
2266 "EXT4-fs: unable to initialize "
2267 "flex_bg meta info!\n");
2268 goto failed_mount2;
2269 }
2270
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271 sbi->s_gdb_count = db_count;
2272 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2273 spin_lock_init(&sbi->s_next_gen_lock);
2274
Peter Zijlstra833f4072007-10-16 23:25:45 -07002275 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2276 ext4_count_free_blocks(sb));
2277 if (!err) {
2278 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2279 ext4_count_free_inodes(sb));
2280 }
2281 if (!err) {
2282 err = percpu_counter_init(&sbi->s_dirs_counter,
2283 ext4_count_dirs(sb));
2284 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002285 if (!err) {
2286 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2287 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002288 if (err) {
2289 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2290 goto failed_mount3;
2291 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002292
Alex Tomasc9de5602008-01-29 00:19:52 -05002293 sbi->s_stripe = ext4_get_stripe_size(sbi);
2294
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002295 /*
2296 * set up enough so that it can read an inode
2297 */
Mingming Cao617ba132006-10-11 01:20:53 -07002298 sb->s_op = &ext4_sops;
2299 sb->s_export_op = &ext4_export_ops;
2300 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002301#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002302 sb->s_qcop = &ext4_qctl_operations;
2303 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002304#endif
2305 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2306
2307 sb->s_root = NULL;
2308
2309 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002310 EXT4_HAS_INCOMPAT_FEATURE(sb,
2311 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002312
2313 /*
2314 * The first inode we look at is the journal inode. Don't try
2315 * root first: it may be modified in the journal!
2316 */
2317 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002318 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2319 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002320 goto failed_mount3;
Theodore Ts'o624080e2008-06-06 17:50:40 -04002321 if (!(sb->s_flags & MS_RDONLY) &&
2322 EXT4_SB(sb)->s_journal->j_failed_commit) {
2323 printk(KERN_CRIT "EXT4-fs error (device %s): "
2324 "ext4_fill_super: Journal transaction "
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002325 "%u is corrupt\n", sb->s_id,
Theodore Ts'o624080e2008-06-06 17:50:40 -04002326 EXT4_SB(sb)->s_journal->j_failed_commit);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002327 if (test_opt(sb, ERRORS_RO)) {
2328 printk(KERN_CRIT
2329 "Mounting filesystem read-only\n");
Theodore Ts'o624080e2008-06-06 17:50:40 -04002330 sb->s_flags |= MS_RDONLY;
2331 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2332 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2333 }
2334 if (test_opt(sb, ERRORS_PANIC)) {
2335 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2336 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2337 ext4_commit_super(sb, es, 1);
Theodore Ts'o624080e2008-06-06 17:50:40 -04002338 goto failed_mount4;
2339 }
2340 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002341 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002342 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002343 goto failed_mount3;
Frank Mayhar03901312009-01-07 00:06:22 -05002344 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2345 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2346 printk(KERN_ERR "EXT4-fs: required journal recovery "
2347 "suppressed and not mounted read-only\n");
2348 goto failed_mount4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002349 } else {
Frank Mayhar03901312009-01-07 00:06:22 -05002350 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2351 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2352 sbi->s_journal = NULL;
2353 needs_recovery = 0;
2354 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002355 }
2356
Jose R. Santoseb40a092007-07-18 08:37:25 -04002357 if (ext4_blocks_count(es) > 0xffffffffULL &&
2358 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2359 JBD2_FEATURE_INCOMPAT_64BIT)) {
2360 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2361 goto failed_mount4;
2362 }
2363
Girish Shilamkar818d2762008-01-28 23:58:27 -05002364 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2365 jbd2_journal_set_features(sbi->s_journal,
2366 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2367 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2368 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2369 jbd2_journal_set_features(sbi->s_journal,
2370 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2371 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2372 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2373 } else {
2374 jbd2_journal_clear_features(sbi->s_journal,
2375 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2376 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2377 }
2378
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002379 /* We have now updated the journal if required, so we can
2380 * validate the data journaling mode. */
2381 switch (test_opt(sb, DATA_FLAGS)) {
2382 case 0:
2383 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002384 * capabilities: ORDERED_DATA if the journal can
2385 * cope, else JOURNAL_DATA
2386 */
Mingming Caodab291a2006-10-11 01:21:01 -07002387 if (jbd2_journal_check_available_features
2388 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002389 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2390 else
2391 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2392 break;
2393
Mingming Cao617ba132006-10-11 01:20:53 -07002394 case EXT4_MOUNT_ORDERED_DATA:
2395 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002396 if (!jbd2_journal_check_available_features
2397 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002398 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002399 "requested data journaling mode\n");
2400 goto failed_mount4;
2401 }
2402 default:
2403 break;
2404 }
2405
Frank Mayhar03901312009-01-07 00:06:22 -05002406no_journal:
2407
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002408 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002409 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2410 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002411 "its supported only with writeback mode\n");
2412 clear_opt(sbi->s_mount_opt, NOBH);
2413 }
2414 }
2415 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002416 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002417 * so we can safely mount the rest of the filesystem now.
2418 */
2419
David Howells1d1fe1e2008-02-07 00:15:37 -08002420 root = ext4_iget(sb, EXT4_ROOT_INO);
2421 if (IS_ERR(root)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002422 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
David Howells1d1fe1e2008-02-07 00:15:37 -08002423 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002424 goto failed_mount4;
2425 }
2426 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002427 iput(root);
Mingming Cao617ba132006-10-11 01:20:53 -07002428 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002429 goto failed_mount4;
2430 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002431 sb->s_root = d_alloc_root(root);
2432 if (!sb->s_root) {
2433 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2434 iput(root);
2435 ret = -ENOMEM;
2436 goto failed_mount4;
2437 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002438
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002439 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002440
2441 /* determine the minimum size of new large inodes, if present */
2442 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2443 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2444 EXT4_GOOD_OLD_INODE_SIZE;
2445 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2446 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2447 if (sbi->s_want_extra_isize <
2448 le16_to_cpu(es->s_want_extra_isize))
2449 sbi->s_want_extra_isize =
2450 le16_to_cpu(es->s_want_extra_isize);
2451 if (sbi->s_want_extra_isize <
2452 le16_to_cpu(es->s_min_extra_isize))
2453 sbi->s_want_extra_isize =
2454 le16_to_cpu(es->s_min_extra_isize);
2455 }
2456 }
2457 /* Check if enough inode space is available */
2458 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2459 sbi->s_inode_size) {
2460 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2461 EXT4_GOOD_OLD_INODE_SIZE;
2462 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2463 "available.\n");
2464 }
2465
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002466 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2467 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2468 "requested data journaling mode\n");
2469 clear_opt(sbi->s_mount_opt, DELALLOC);
2470 } else if (test_opt(sb, DELALLOC))
2471 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2472
2473 ext4_ext_init(sb);
2474 err = ext4_mb_init(sb, needs_recovery);
2475 if (err) {
2476 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2477 err);
2478 goto failed_mount4;
2479 }
2480
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002481 /*
2482 * akpm: core read_super() calls in here with the superblock locked.
2483 * That deadlocks, because orphan cleanup needs to lock the superblock
2484 * in numerous places. Here we just pop the lock - it's relatively
2485 * harmless, because we are now ready to accept write_super() requests,
2486 * and aviro says that's the only reason for hanging onto the
2487 * superblock lock.
2488 */
Mingming Cao617ba132006-10-11 01:20:53 -07002489 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2490 ext4_orphan_cleanup(sb, es);
2491 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05002492 if (needs_recovery) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002493 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
Frank Mayhar03901312009-01-07 00:06:22 -05002494 ext4_mark_recovery_complete(sb, es);
2495 }
2496 if (EXT4_SB(sb)->s_journal) {
2497 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2498 descr = " journalled data mode";
2499 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2500 descr = " ordered data mode";
2501 else
2502 descr = " writeback data mode";
2503 } else
2504 descr = "out journal";
2505
2506 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2507 sb->s_id, descr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002508
2509 lock_kernel();
2510 return 0;
2511
Mingming Cao617ba132006-10-11 01:20:53 -07002512cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002513 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07002514 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002515 sb->s_id);
2516 goto failed_mount;
2517
2518failed_mount4:
Frank Mayhar03901312009-01-07 00:06:22 -05002519 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2520 if (sbi->s_journal) {
2521 jbd2_journal_destroy(sbi->s_journal);
2522 sbi->s_journal = NULL;
2523 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002524failed_mount3:
2525 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2526 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2527 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002528 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002529failed_mount2:
2530 for (i = 0; i < db_count; i++)
2531 brelse(sbi->s_group_desc[i]);
2532 kfree(sbi->s_group_desc);
2533failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04002534 if (sbi->s_proc) {
2535 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002536 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04002537 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002538#ifdef CONFIG_QUOTA
2539 for (i = 0; i < MAXQUOTAS; i++)
2540 kfree(sbi->s_qf_names[i]);
2541#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002542 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002543 brelse(bh);
2544out_fail:
2545 sb->s_fs_info = NULL;
2546 kfree(sbi);
2547 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08002548 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002549}
2550
2551/*
2552 * Setup any per-fs journal parameters now. We'll do this both on
2553 * initial mount, once the journal has been initialised but before we've
2554 * done any recovery; and again on any subsequent remount.
2555 */
Mingming Cao617ba132006-10-11 01:20:53 -07002556static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002557{
Mingming Cao617ba132006-10-11 01:20:53 -07002558 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002559
Theodore Ts'o30773842009-01-03 20:27:38 -05002560 journal->j_commit_interval = sbi->s_commit_interval;
2561 journal->j_min_batch_time = sbi->s_min_batch_time;
2562 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002563
2564 spin_lock(&journal->j_state_lock);
2565 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07002566 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002567 else
Mingming Caodab291a2006-10-11 01:21:01 -07002568 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04002569 if (test_opt(sb, DATA_ERR_ABORT))
2570 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2571 else
2572 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002573 spin_unlock(&journal->j_state_lock);
2574}
2575
Mingming Cao617ba132006-10-11 01:20:53 -07002576static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002577 unsigned int journal_inum)
2578{
2579 struct inode *journal_inode;
2580 journal_t *journal;
2581
Frank Mayhar03901312009-01-07 00:06:22 -05002582 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2583
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002584 /* First, test for the existence of a valid inode on disk. Bad
2585 * things happen if we iget() an unused inode, as the subsequent
2586 * iput() will try to delete it. */
2587
David Howells1d1fe1e2008-02-07 00:15:37 -08002588 journal_inode = ext4_iget(sb, journal_inum);
2589 if (IS_ERR(journal_inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002590 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002591 return NULL;
2592 }
2593 if (!journal_inode->i_nlink) {
2594 make_bad_inode(journal_inode);
2595 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002596 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002597 return NULL;
2598 }
2599
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002600 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002601 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08002602 if (!S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002603 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002604 iput(journal_inode);
2605 return NULL;
2606 }
2607
Mingming Caodab291a2006-10-11 01:21:01 -07002608 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002609 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002610 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002611 iput(journal_inode);
2612 return NULL;
2613 }
2614 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002615 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002616 return journal;
2617}
2618
Mingming Cao617ba132006-10-11 01:20:53 -07002619static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002620 dev_t j_dev)
2621{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002622 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002623 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07002624 ext4_fsblk_t start;
2625 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002626 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07002627 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002628 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002629 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002630 struct block_device *bdev;
2631
Frank Mayhar03901312009-01-07 00:06:22 -05002632 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2633
Mingming Cao617ba132006-10-11 01:20:53 -07002634 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002635 if (bdev == NULL)
2636 return NULL;
2637
2638 if (bd_claim(bdev, sb)) {
2639 printk(KERN_ERR
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002640 "EXT4: failed to claim external journal device.\n");
Al Viro9a1c3542008-02-22 20:40:24 -05002641 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002642 return NULL;
2643 }
2644
2645 blocksize = sb->s_blocksize;
2646 hblock = bdev_hardsect_size(bdev);
2647 if (blocksize < hblock) {
2648 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002649 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002650 goto out_bdev;
2651 }
2652
Mingming Cao617ba132006-10-11 01:20:53 -07002653 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2654 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002655 set_blocksize(bdev, blocksize);
2656 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002657 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002658 "external journal\n");
2659 goto out_bdev;
2660 }
2661
Mingming Cao617ba132006-10-11 01:20:53 -07002662 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2663 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002664 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002665 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2666 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002667 "bad superblock\n");
2668 brelse(bh);
2669 goto out_bdev;
2670 }
2671
Mingming Cao617ba132006-10-11 01:20:53 -07002672 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2673 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002674 brelse(bh);
2675 goto out_bdev;
2676 }
2677
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002678 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002679 start = sb_block + 1;
2680 brelse(bh); /* we're done with the superblock */
2681
Mingming Caodab291a2006-10-11 01:21:01 -07002682 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002683 start, len, blocksize);
2684 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002685 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002686 goto out_bdev;
2687 }
2688 journal->j_private = sb;
2689 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2690 wait_on_buffer(journal->j_sb_buffer);
2691 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002692 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002693 goto out_journal;
2694 }
2695 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002696 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002697 "user (unsupported) - %d\n",
2698 be32_to_cpu(journal->j_superblock->s_nr_users));
2699 goto out_journal;
2700 }
Mingming Cao617ba132006-10-11 01:20:53 -07002701 EXT4_SB(sb)->journal_bdev = bdev;
2702 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002703 return journal;
2704out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002705 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002706out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002707 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002708 return NULL;
2709}
2710
Mingming Cao617ba132006-10-11 01:20:53 -07002711static int ext4_load_journal(struct super_block *sb,
2712 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002713 unsigned long journal_devnum)
2714{
2715 journal_t *journal;
2716 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2717 dev_t journal_dev;
2718 int err = 0;
2719 int really_read_only;
2720
Frank Mayhar03901312009-01-07 00:06:22 -05002721 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2722
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002723 if (journal_devnum &&
2724 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002725 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002726 "numbers have changed\n");
2727 journal_dev = new_decode_dev(journal_devnum);
2728 } else
2729 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2730
2731 really_read_only = bdev_read_only(sb->s_bdev);
2732
2733 /*
2734 * Are we loading a blank journal or performing recovery after a
2735 * crash? For recovery, we need to check in advance whether we
2736 * can get read-write access to the device.
2737 */
2738
Mingming Cao617ba132006-10-11 01:20:53 -07002739 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002740 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002741 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002742 "required on readonly filesystem.\n");
2743 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002744 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002745 "unavailable, cannot proceed.\n");
2746 return -EROFS;
2747 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002748 printk(KERN_INFO "EXT4-fs: write access will "
2749 "be enabled during recovery.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002750 }
2751 }
2752
2753 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002754 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002755 "and inode journals!\n");
2756 return -EINVAL;
2757 }
2758
2759 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002760 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002761 return -EINVAL;
2762 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002763 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002764 return -EINVAL;
2765 }
2766
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002767 if (journal->j_flags & JBD2_BARRIER)
2768 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2769 else
2770 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2771
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002772 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002773 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002774 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002775 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002776 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002777 return err;
2778 }
2779 }
2780
Mingming Cao617ba132006-10-11 01:20:53 -07002781 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002782 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002783 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002784 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002785
2786 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002787 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002788 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002789 return err;
2790 }
2791
Mingming Cao617ba132006-10-11 01:20:53 -07002792 EXT4_SB(sb)->s_journal = journal;
2793 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002794
2795 if (journal_devnum &&
2796 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2797 es->s_journal_dev = cpu_to_le32(journal_devnum);
2798 sb->s_dirt = 1;
2799
2800 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002801 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002802 }
2803
2804 return 0;
2805}
2806
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002807static int ext4_create_journal(struct super_block *sb,
2808 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002809 unsigned int journal_inum)
2810{
2811 journal_t *journal;
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002812 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002813
2814 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002815 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002816 "create journal.\n");
2817 return -EROFS;
2818 }
2819
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002820 journal = ext4_get_journal(sb, journal_inum);
2821 if (!journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002822 return -EINVAL;
2823
Mingming Cao617ba132006-10-11 01:20:53 -07002824 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002825 journal_inum);
2826
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002827 err = jbd2_journal_create(journal);
2828 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002829 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002830 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002831 return -EIO;
2832 }
2833
Mingming Cao617ba132006-10-11 01:20:53 -07002834 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002835
Mingming Cao617ba132006-10-11 01:20:53 -07002836 ext4_update_dynamic_rev(sb);
2837 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2838 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002839
2840 es->s_journal_inum = cpu_to_le32(journal_inum);
2841 sb->s_dirt = 1;
2842
2843 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002844 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002845
2846 return 0;
2847}
2848
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002849static void ext4_commit_super(struct super_block *sb,
2850 struct ext4_super_block *es, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002851{
Mingming Cao617ba132006-10-11 01:20:53 -07002852 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002853
2854 if (!sbh)
2855 return;
Theodore Ts'o914258b2008-10-06 21:35:40 -04002856 if (buffer_write_io_error(sbh)) {
2857 /*
2858 * Oh, dear. A previous attempt to write the
2859 * superblock failed. This could happen because the
2860 * USB device was yanked out. Or it could happen to
2861 * be a transient write error and maybe the block will
2862 * be remapped. Nothing we can do but to retry the
2863 * write and hope for the best.
2864 */
2865 printk(KERN_ERR "ext4: previous I/O error to "
2866 "superblock detected for %s.\n", sb->s_id);
2867 clear_buffer_write_io_error(sbh);
2868 set_buffer_uptodate(sbh);
2869 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002870 es->s_wtime = cpu_to_le32(get_seconds());
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002871 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07002872 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002873 BUFFER_TRACE(sbh, "marking dirty");
2874 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002875 if (sync) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002876 sync_dirty_buffer(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002877 if (buffer_write_io_error(sbh)) {
2878 printk(KERN_ERR "ext4: I/O error while writing "
2879 "superblock for %s.\n", sb->s_id);
2880 clear_buffer_write_io_error(sbh);
2881 set_buffer_uptodate(sbh);
2882 }
2883 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002884}
2885
2886
2887/*
2888 * Have we just finished recovery? If so, and if we are mounting (or
2889 * remounting) the filesystem readonly, then we will end up with a
2890 * consistent fs on disk. Record that fact.
2891 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002892static void ext4_mark_recovery_complete(struct super_block *sb,
2893 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002894{
Mingming Cao617ba132006-10-11 01:20:53 -07002895 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002896
Frank Mayhar03901312009-01-07 00:06:22 -05002897 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2898 BUG_ON(journal != NULL);
2899 return;
2900 }
Mingming Caodab291a2006-10-11 01:21:01 -07002901 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04002902 if (jbd2_journal_flush(journal) < 0)
2903 goto out;
2904
Jan Kara32c37732007-07-15 23:41:09 -07002905 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002906 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002907 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002908 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002909 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002910 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911 }
Jan Kara32c37732007-07-15 23:41:09 -07002912 unlock_super(sb);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04002913
2914out:
Mingming Caodab291a2006-10-11 01:21:01 -07002915 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002916}
2917
2918/*
2919 * If we are mounting (or read-write remounting) a filesystem whose journal
2920 * has recorded an error from a previous lifetime, move that error to the
2921 * main filesystem now.
2922 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002923static void ext4_clear_journal_err(struct super_block *sb,
2924 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002925{
2926 journal_t *journal;
2927 int j_errno;
2928 const char *errstr;
2929
Frank Mayhar03901312009-01-07 00:06:22 -05002930 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2931
Mingming Cao617ba132006-10-11 01:20:53 -07002932 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002933
2934 /*
2935 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07002936 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002937 */
2938
Mingming Caodab291a2006-10-11 01:21:01 -07002939 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002940 if (j_errno) {
2941 char nbuf[16];
2942
Mingming Cao617ba132006-10-11 01:20:53 -07002943 errstr = ext4_decode_error(sb, j_errno, nbuf);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002944 ext4_warning(sb, __func__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945 "from previous mount: %s", errstr);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002946 ext4_warning(sb, __func__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947 "filesystem check.");
2948
Mingming Cao617ba132006-10-11 01:20:53 -07002949 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2950 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002951 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002952
Mingming Caodab291a2006-10-11 01:21:01 -07002953 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002954 }
2955}
2956
2957/*
2958 * Force the running and committing transactions to commit,
2959 * and wait on the commit.
2960 */
Mingming Cao617ba132006-10-11 01:20:53 -07002961int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002962{
2963 journal_t *journal;
Frank Mayhar03901312009-01-07 00:06:22 -05002964 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002965
2966 if (sb->s_flags & MS_RDONLY)
2967 return 0;
2968
Mingming Cao617ba132006-10-11 01:20:53 -07002969 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -05002970 if (journal) {
2971 sb->s_dirt = 0;
2972 ret = ext4_journal_force_commit(journal);
2973 }
2974
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002975 return ret;
2976}
2977
2978/*
Mingming Cao617ba132006-10-11 01:20:53 -07002979 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002980 * have to propagate any other updates to the superblock on disk at this
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05002981 * point. (We can probably nuke this function altogether, and remove
2982 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002983 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002984static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002985{
Frank Mayhar03901312009-01-07 00:06:22 -05002986 if (EXT4_SB(sb)->s_journal) {
2987 if (mutex_trylock(&sb->s_lock) != 0)
2988 BUG();
2989 sb->s_dirt = 0;
2990 } else {
2991 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2992 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002993}
2994
Mingming Cao617ba132006-10-11 01:20:53 -07002995static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002996{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05002997 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998
Theodore Ts'oede86cc2008-10-05 20:50:06 -04002999 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003000 sb->s_dirt = 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003001 if (EXT4_SB(sb)->s_journal) {
3002 if (wait)
3003 ret = ext4_force_commit(sb);
3004 else
3005 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
3006 } else {
3007 ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
3008 }
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003009 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003010}
3011
3012/*
3013 * LVM calls this function before a (read-only) snapshot is created. This
3014 * gives us a chance to flush the journal completely and mark the fs clean.
3015 */
Mingming Cao617ba132006-10-11 01:20:53 -07003016static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003017{
3018 sb->s_dirt = 0;
3019
3020 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003021 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003022
Frank Mayhar03901312009-01-07 00:06:22 -05003023 if (journal) {
3024 /* Now we set up the journal barrier. */
3025 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003026
Frank Mayhar03901312009-01-07 00:06:22 -05003027 /*
3028 * We don't want to clear needs_recovery flag when we
3029 * failed to flush the journal.
3030 */
3031 if (jbd2_journal_flush(journal) < 0)
3032 return;
3033 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003034
3035 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07003036 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3037 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003038 }
3039}
3040
3041/*
3042 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3043 * flag here, even though the filesystem is not technically dirty yet.
3044 */
Mingming Cao617ba132006-10-11 01:20:53 -07003045static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003046{
Frank Mayhar03901312009-01-07 00:06:22 -05003047 if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003048 lock_super(sb);
3049 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07003050 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3051 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003052 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07003053 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003054 }
3055}
3056
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003057static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003058{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003059 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003060 struct ext4_sb_info *sbi = EXT4_SB(sb);
3061 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003062 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003063 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003064 ext4_group_t g;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065 int err;
3066#ifdef CONFIG_QUOTA
3067 int i;
3068#endif
3069
3070 /* Store the original options */
3071 old_sb_flags = sb->s_flags;
3072 old_opts.s_mount_opt = sbi->s_mount_opt;
3073 old_opts.s_resuid = sbi->s_resuid;
3074 old_opts.s_resgid = sbi->s_resgid;
3075 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003076 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3077 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003078#ifdef CONFIG_QUOTA
3079 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3080 for (i = 0; i < MAXQUOTAS; i++)
3081 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3082#endif
3083
3084 /*
3085 * Allow the "check" option to be passed as a remount option.
3086 */
3087 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3088 err = -EINVAL;
3089 goto restore_opts;
3090 }
3091
Mingming Cao617ba132006-10-11 01:20:53 -07003092 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
Harvey Harrison46e665e2008-04-17 10:38:59 -04003093 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003094
3095 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07003096 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003097
3098 es = sbi->s_es;
3099
Frank Mayhar03901312009-01-07 00:06:22 -05003100 if (sbi->s_journal)
3101 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003102
3103 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003104 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003105 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003106 err = -EROFS;
3107 goto restore_opts;
3108 }
3109
3110 if (*flags & MS_RDONLY) {
3111 /*
3112 * First of all, the unconditional stuff we have to do
3113 * to disable replay of the journal when we next remount
3114 */
3115 sb->s_flags |= MS_RDONLY;
3116
3117 /*
3118 * OK, test if we are remounting a valid rw partition
3119 * readonly, and if so set the rdonly flag and then
3120 * mark the partition as valid again.
3121 */
Mingming Cao617ba132006-10-11 01:20:53 -07003122 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3123 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003124 es->s_state = cpu_to_le16(sbi->s_mount_state);
3125
Jan Kara32c37732007-07-15 23:41:09 -07003126 /*
3127 * We have to unlock super so that we can wait for
3128 * transactions.
3129 */
Frank Mayhar03901312009-01-07 00:06:22 -05003130 if (sbi->s_journal) {
3131 unlock_super(sb);
3132 ext4_mark_recovery_complete(sb, es);
3133 lock_super(sb);
3134 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003135 } else {
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003136 int ret;
Mingming Cao617ba132006-10-11 01:20:53 -07003137 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3138 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3139 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003140 "remount RDWR because of unsupported "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003141 "optional features (%x).\n", sb->s_id,
3142 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3143 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003144 err = -EROFS;
3145 goto restore_opts;
3146 }
Eric Sandeenead65962007-02-10 01:46:08 -08003147
3148 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003149 * Make sure the group descriptor checksums
3150 * are sane. If they aren't, refuse to
3151 * remount r/w.
3152 */
3153 for (g = 0; g < sbi->s_groups_count; g++) {
3154 struct ext4_group_desc *gdp =
3155 ext4_get_group_desc(sb, g, NULL);
3156
3157 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3158 printk(KERN_ERR
3159 "EXT4-fs: ext4_remount: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003160 "Checksum for group %u failed (%u!=%u)\n",
Theodore Ts'o8a266462008-07-26 14:34:21 -04003161 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3162 le16_to_cpu(gdp->bg_checksum));
3163 err = -EINVAL;
3164 goto restore_opts;
3165 }
3166 }
3167
3168 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003169 * If we have an unprocessed orphan list hanging
3170 * around from a previously readonly bdev mount,
3171 * require a full umount/remount for now.
3172 */
3173 if (es->s_last_orphan) {
3174 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3175 "remount RDWR because of unprocessed "
3176 "orphan inode list. Please "
3177 "umount/remount instead.\n",
3178 sb->s_id);
3179 err = -EINVAL;
3180 goto restore_opts;
3181 }
3182
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003183 /*
3184 * Mounting a RDONLY partition read-write, so reread
3185 * and store the current valid flag. (It may have
3186 * been changed by e2fsck since we originally mounted
3187 * the partition.)
3188 */
Frank Mayhar03901312009-01-07 00:06:22 -05003189 if (sbi->s_journal)
3190 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003191 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003192 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003193 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003194 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003195 sb->s_flags &= ~MS_RDONLY;
3196 }
3197 }
Frank Mayhar03901312009-01-07 00:06:22 -05003198 if (sbi->s_journal == NULL)
3199 ext4_commit_super(sb, es, 1);
3200
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003201#ifdef CONFIG_QUOTA
3202 /* Release old quota file names */
3203 for (i = 0; i < MAXQUOTAS; i++)
3204 if (old_opts.s_qf_names[i] &&
3205 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3206 kfree(old_opts.s_qf_names[i]);
3207#endif
3208 return 0;
3209restore_opts:
3210 sb->s_flags = old_sb_flags;
3211 sbi->s_mount_opt = old_opts.s_mount_opt;
3212 sbi->s_resuid = old_opts.s_resuid;
3213 sbi->s_resgid = old_opts.s_resgid;
3214 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003215 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3216 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003217#ifdef CONFIG_QUOTA
3218 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3219 for (i = 0; i < MAXQUOTAS; i++) {
3220 if (sbi->s_qf_names[i] &&
3221 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3222 kfree(sbi->s_qf_names[i]);
3223 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3224 }
3225#endif
3226 return err;
3227}
3228
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003229static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003230{
3231 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003232 struct ext4_sb_info *sbi = EXT4_SB(sb);
3233 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003234 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003235
Badari Pulavarty5e700302007-07-15 23:42:00 -07003236 if (test_opt(sb, MINIX_DF)) {
3237 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003238 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Avantika Mathurfd2d4292008-01-28 23:58:27 -05003239 ext4_group_t ngroups = sbi->s_groups_count, i;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003240 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003241 smp_rmb();
3242
3243 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003244 * Compute the overhead (FS structures). This is constant
3245 * for a given filesystem unless the number of block groups
3246 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003247 */
3248
3249 /*
3250 * All of the blocks before first_data_block are
3251 * overhead
3252 */
3253 overhead = le32_to_cpu(es->s_first_data_block);
3254
3255 /*
3256 * Add the overhead attributed to the superblock and
3257 * block group descriptors. If the sparse superblocks
3258 * feature is turned on, then not all groups have this.
3259 */
3260 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003261 overhead += ext4_bg_has_super(sb, i) +
3262 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003263 cond_resched();
3264 }
3265
3266 /*
3267 * Every block group has an inode bitmap, a block
3268 * bitmap, and an inode table.
3269 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003270 overhead += ngroups * (2 + sbi->s_itb_per_group);
3271 sbi->s_overhead_last = overhead;
3272 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003273 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003274 }
3275
Mingming Cao617ba132006-10-11 01:20:53 -07003276 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003277 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003278 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003279 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3280 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Aneesh Kumar K.V308ba3e2007-10-16 18:38:25 -04003281 ext4_free_blocks_count_set(es, buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003282 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3283 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003284 buf->f_bavail = 0;
3285 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003286 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003287 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07003288 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003289 fsid = le64_to_cpup((void *)es->s_uuid) ^
3290 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3291 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3292 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003293 return 0;
3294}
3295
3296/* Helper function for writing quotas on sync - we need to start transaction before quota file
3297 * is locked for write. Otherwise the are possible deadlocks:
3298 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003299 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07003300 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003301 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003302 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003303 *
3304 */
3305
3306#ifdef CONFIG_QUOTA
3307
3308static inline struct inode *dquot_to_inode(struct dquot *dquot)
3309{
3310 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3311}
3312
Mingming Cao617ba132006-10-11 01:20:53 -07003313static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003314{
3315 handle_t *handle;
3316 int ret, err;
3317
3318 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003319 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003320 if (IS_ERR(handle))
3321 return PTR_ERR(handle);
3322 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003323 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003324 if (!ret)
3325 ret = err;
3326 return ret;
3327}
3328
Mingming Cao617ba132006-10-11 01:20:53 -07003329static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003330{
3331 handle_t *handle;
3332 int ret, err;
3333
3334 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003335 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Jan Kara2887df12008-04-29 22:02:07 -04003336 if (IS_ERR(handle)) {
3337 /*
3338 * We call dquot_drop() anyway to at least release references
3339 * to quota structures so that umount does not hang.
3340 */
3341 dquot_drop(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003342 return PTR_ERR(handle);
Jan Kara2887df12008-04-29 22:02:07 -04003343 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003344 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003345 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003346 if (!ret)
3347 ret = err;
3348 return ret;
3349}
3350
Mingming Cao617ba132006-10-11 01:20:53 -07003351static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003352{
3353 int ret, err;
3354 handle_t *handle;
3355 struct inode *inode;
3356
3357 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003358 handle = ext4_journal_start(inode,
3359 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003360 if (IS_ERR(handle))
3361 return PTR_ERR(handle);
3362 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003363 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003364 if (!ret)
3365 ret = err;
3366 return ret;
3367}
3368
Mingming Cao617ba132006-10-11 01:20:53 -07003369static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003370{
3371 int ret, err;
3372 handle_t *handle;
3373
Mingming Cao617ba132006-10-11 01:20:53 -07003374 handle = ext4_journal_start(dquot_to_inode(dquot),
3375 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003376 if (IS_ERR(handle))
3377 return PTR_ERR(handle);
3378 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003379 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003380 if (!ret)
3381 ret = err;
3382 return ret;
3383}
3384
Mingming Cao617ba132006-10-11 01:20:53 -07003385static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003386{
3387 int ret, err;
3388 handle_t *handle;
3389
Mingming Cao617ba132006-10-11 01:20:53 -07003390 handle = ext4_journal_start(dquot_to_inode(dquot),
3391 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003392 if (IS_ERR(handle)) {
3393 /* Release dquot anyway to avoid endless cycle in dqput() */
3394 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003395 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003396 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003397 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003398 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003399 if (!ret)
3400 ret = err;
3401 return ret;
3402}
3403
Mingming Cao617ba132006-10-11 01:20:53 -07003404static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003405{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003406 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003407 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3408 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003409 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003410 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003411 } else {
3412 return dquot_mark_dquot_dirty(dquot);
3413 }
3414}
3415
Mingming Cao617ba132006-10-11 01:20:53 -07003416static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003417{
3418 int ret, err;
3419 handle_t *handle;
3420
3421 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003422 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003423 if (IS_ERR(handle))
3424 return PTR_ERR(handle);
3425 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003426 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003427 if (!ret)
3428 ret = err;
3429 return ret;
3430}
3431
3432/*
3433 * Turn on quotas during mount time - we need to find
3434 * the quota file and such...
3435 */
Mingming Cao617ba132006-10-11 01:20:53 -07003436static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003437{
Mingming Cao617ba132006-10-11 01:20:53 -07003438 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3439 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003440}
3441
3442/*
3443 * Standard function to be called on quota_on
3444 */
Mingming Cao617ba132006-10-11 01:20:53 -07003445static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro82646132008-08-02 00:57:06 -04003446 char *name, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003447{
3448 int err;
Al Viro82646132008-08-02 00:57:06 -04003449 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003450
3451 if (!test_opt(sb, QUOTA))
3452 return -EINVAL;
Al Viro82646132008-08-02 00:57:06 -04003453 /* When remounting, no checks are needed and in fact, name is NULL */
Jan Kara06235432008-05-13 19:11:51 -04003454 if (remount)
Al Viro82646132008-08-02 00:57:06 -04003455 return vfs_quota_on(sb, type, format_id, name, remount);
Jan Kara06235432008-05-13 19:11:51 -04003456
Al Viro82646132008-08-02 00:57:06 -04003457 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003458 if (err)
3459 return err;
Jan Kara06235432008-05-13 19:11:51 -04003460
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003461 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04003462 if (path.mnt->mnt_sb != sb) {
3463 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003464 return -EXDEV;
3465 }
Jan Kara06235432008-05-13 19:11:51 -04003466 /* Journaling quota? */
3467 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003468 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04003469 if (path.dentry->d_parent != sb->s_root)
Jan Kara06235432008-05-13 19:11:51 -04003470 printk(KERN_WARNING
3471 "EXT4-fs: Quota file not on filesystem root. "
3472 "Journaled quota will not work.\n");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003473 }
Jan Kara06235432008-05-13 19:11:51 -04003474
3475 /*
3476 * When we journal data on quota file, we have to flush journal to see
3477 * all updates to the file when we bypass pagecache...
3478 */
Frank Mayhar03901312009-01-07 00:06:22 -05003479 if (EXT4_SB(sb)->s_journal &&
3480 ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04003481 /*
3482 * We don't need to lock updates but journal_flush() could
3483 * otherwise be livelocked...
3484 */
3485 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003486 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04003487 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003488 if (err) {
Al Viro82646132008-08-02 00:57:06 -04003489 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003490 return err;
3491 }
Jan Kara06235432008-05-13 19:11:51 -04003492 }
3493
Al Viro82646132008-08-02 00:57:06 -04003494 err = vfs_quota_on_path(sb, type, format_id, &path);
3495 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04003496 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003497}
3498
3499/* Read data from quotafile - avoid pagecache and such because we cannot afford
3500 * acquiring the locks... As quota files are never truncated and quota code
3501 * itself serializes the operations (and noone else should touch the files)
3502 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003503static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003504 size_t len, loff_t off)
3505{
3506 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003507 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003508 int err = 0;
3509 int offset = off & (sb->s_blocksize - 1);
3510 int tocopy;
3511 size_t toread;
3512 struct buffer_head *bh;
3513 loff_t i_size = i_size_read(inode);
3514
3515 if (off > i_size)
3516 return 0;
3517 if (off+len > i_size)
3518 len = i_size-off;
3519 toread = len;
3520 while (toread > 0) {
3521 tocopy = sb->s_blocksize - offset < toread ?
3522 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003523 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003524 if (err)
3525 return err;
3526 if (!bh) /* A hole? */
3527 memset(data, 0, tocopy);
3528 else
3529 memcpy(data, bh->b_data+offset, tocopy);
3530 brelse(bh);
3531 offset = 0;
3532 toread -= tocopy;
3533 data += tocopy;
3534 blk++;
3535 }
3536 return len;
3537}
3538
3539/* Write to quotafile (we know the transaction is already started and has
3540 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003541static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003542 const char *data, size_t len, loff_t off)
3543{
3544 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003545 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003546 int err = 0;
3547 int offset = off & (sb->s_blocksize - 1);
3548 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003549 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003550 size_t towrite = len;
3551 struct buffer_head *bh;
3552 handle_t *handle = journal_current_handle();
3553
Frank Mayhar03901312009-01-07 00:06:22 -05003554 if (EXT4_SB(sb)->s_journal && !handle) {
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003555 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
Jan Kara9c3013e2007-09-11 15:23:29 -07003556 " cancelled because transaction is not started.\n",
3557 (unsigned long long)off, (unsigned long long)len);
3558 return -EIO;
3559 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003560 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3561 while (towrite > 0) {
3562 tocopy = sb->s_blocksize - offset < towrite ?
3563 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003564 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003565 if (!bh)
3566 goto out;
3567 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003568 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003569 if (err) {
3570 brelse(bh);
3571 goto out;
3572 }
3573 }
3574 lock_buffer(bh);
3575 memcpy(bh->b_data+offset, data, tocopy);
3576 flush_dcache_page(bh->b_page);
3577 unlock_buffer(bh);
3578 if (journal_quota)
Frank Mayhar03901312009-01-07 00:06:22 -05003579 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003580 else {
3581 /* Always do at least ordered writes for quotas */
Jan Kara678aaf42008-07-11 19:27:31 -04003582 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003583 mark_buffer_dirty(bh);
3584 }
3585 brelse(bh);
3586 if (err)
3587 goto out;
3588 offset = 0;
3589 towrite -= tocopy;
3590 data += tocopy;
3591 blk++;
3592 }
3593out:
Jan Kara4d04e4f2008-07-04 09:59:34 -07003594 if (len == towrite) {
3595 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003596 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07003597 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003598 if (inode->i_size < off+len-towrite) {
3599 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07003600 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003601 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003602 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003603 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003604 mutex_unlock(&inode->i_mutex);
3605 return len - towrite;
3606}
3607
3608#endif
3609
Mingming Cao617ba132006-10-11 01:20:53 -07003610static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003611 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3612{
Mingming Cao617ba132006-10-11 01:20:53 -07003613 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003614}
3615
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003616#ifdef CONFIG_PROC_FS
3617static int ext4_ui_proc_show(struct seq_file *m, void *v)
3618{
3619 unsigned int *p = m->private;
3620
3621 seq_printf(m, "%u\n", *p);
3622 return 0;
3623}
3624
3625static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3626{
3627 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3628}
3629
3630static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3631 size_t cnt, loff_t *ppos)
3632{
Roel Kluin23475e22008-11-26 02:23:19 -05003633 unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003634 char str[32];
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003635
3636 if (cnt >= sizeof(str))
3637 return -EINVAL;
3638 if (copy_from_user(str, buf, cnt))
3639 return -EFAULT;
Roel Kluin23475e22008-11-26 02:23:19 -05003640
3641 *p = simple_strtoul(str, NULL, 0);
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003642 return cnt;
3643}
3644
3645const struct file_operations ext4_ui_proc_fops = {
3646 .owner = THIS_MODULE,
3647 .open = ext4_ui_proc_open,
3648 .read = seq_read,
3649 .llseek = seq_lseek,
3650 .release = single_release,
3651 .write = ext4_ui_proc_write,
3652};
3653#endif
3654
Theodore Ts'o03010a32008-10-10 20:02:48 -04003655static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003656 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04003657 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07003658 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003659 .kill_sb = kill_block_super,
3660 .fs_flags = FS_REQUIRES_DEV,
3661};
3662
Theodore Ts'o03010a32008-10-10 20:02:48 -04003663#ifdef CONFIG_EXT4DEV_COMPAT
3664static int ext4dev_get_sb(struct file_system_type *fs_type,
3665 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3666{
3667 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3668 "to mount using ext4\n");
3669 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3670 "will go away by 2.6.31\n");
3671 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3672}
3673
3674static struct file_system_type ext4dev_fs_type = {
3675 .owner = THIS_MODULE,
3676 .name = "ext4dev",
3677 .get_sb = ext4dev_get_sb,
3678 .kill_sb = kill_block_super,
3679 .fs_flags = FS_REQUIRES_DEV,
3680};
3681MODULE_ALIAS("ext4dev");
3682#endif
3683
Mingming Cao617ba132006-10-11 01:20:53 -07003684static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003685{
Alex Tomasc9de5602008-01-29 00:19:52 -05003686 int err;
3687
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003688 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003689 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003690 if (err)
3691 return err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003692
3693 err = init_ext4_xattr();
3694 if (err)
3695 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003696 err = init_inodecache();
3697 if (err)
3698 goto out1;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003699 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003700 if (err)
3701 goto out;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003702#ifdef CONFIG_EXT4DEV_COMPAT
3703 err = register_filesystem(&ext4dev_fs_type);
3704 if (err) {
3705 unregister_filesystem(&ext4_fs_type);
3706 goto out;
3707 }
3708#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003709 return 0;
3710out:
3711 destroy_inodecache();
3712out1:
Mingming Cao617ba132006-10-11 01:20:53 -07003713 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003714out2:
3715 exit_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003716 return err;
3717}
3718
Mingming Cao617ba132006-10-11 01:20:53 -07003719static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003720{
Theodore Ts'o03010a32008-10-10 20:02:48 -04003721 unregister_filesystem(&ext4_fs_type);
3722#ifdef CONFIG_EXT4DEV_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07003723 unregister_filesystem(&ext4dev_fs_type);
Theodore Ts'o03010a32008-10-10 20:02:48 -04003724#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003725 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07003726 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003727 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003728 remove_proc_entry("fs/ext4", NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003729}
3730
3731MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07003732MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003733MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07003734module_init(init_ext4_fs)
3735module_exit(exit_ext4_fs)