blob: eca6c057b11977cbeb77ea177b60c8a67a447b6c [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>
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -040023#include <linux/vmalloc.h>
Mingming Caodab291a2006-10-11 01:21:01 -070024#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070025#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/blkdev.h>
28#include <linux/parser.h>
29#include <linux/smp_lock.h>
30#include <linux/buffer_head.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070031#include <linux/exportfs.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032#include <linux/vfs.h>
33#include <linux/random.h>
34#include <linux/mount.h>
35#include <linux/namei.h>
36#include <linux/quotaops.h>
37#include <linux/seq_file.h>
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040038#include <linux/proc_fs.h>
Theodore Ts'o3197ebd2009-03-31 09:10:09 -040039#include <linux/ctype.h>
Theodore Ts'oede86cc2008-10-05 20:50:06 -040040#include <linux/marker.h>
Vignesh Babu13305932007-07-18 09:11:02 -040041#include <linux/log2.h>
Andreas Dilger717d50e2007-10-16 18:38:25 -040042#include <linux/crc16.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070043#include <asm/uaccess.h>
44
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040045#include "ext4.h"
46#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047#include "xattr.h"
48#include "acl.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049
Curt Wohlgemuthf4033902009-05-01 20:27:20 -040050static int default_mb_history_length = 1000;
51
52module_param_named(default_mb_history_length, default_mb_history_length,
53 int, 0644);
54MODULE_PARM_DESC(default_mb_history_length,
55 "Default number of entries saved for mb_history");
56
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040057struct proc_dir_entry *ext4_proc_root;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -040058static struct kset *ext4_kset;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040059
Mingming Cao617ba132006-10-11 01:20:53 -070060static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070061 unsigned long journal_devnum);
Theodore Ts'oe2d67052009-05-01 00:33:44 -040062static int ext4_commit_super(struct super_block *sb, int sync);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040063static void ext4_mark_recovery_complete(struct super_block *sb,
64 struct ext4_super_block *es);
65static void ext4_clear_journal_err(struct super_block *sb,
66 struct ext4_super_block *es);
Mingming Cao617ba132006-10-11 01:20:53 -070067static int ext4_sync_fs(struct super_block *sb, int wait);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040068static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070069 char nbuf[16]);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040070static int ext4_remount(struct super_block *sb, int *flags, char *data);
71static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
Takashi Satoc4be0c12009-01-09 16:40:58 -080072static int ext4_unfreeze(struct super_block *sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040073static void ext4_write_super(struct super_block *sb);
Takashi Satoc4be0c12009-01-09 16:40:58 -080074static int ext4_freeze(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070075
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070076
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070077ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
78 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070079{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040080 return le32_to_cpu(bg->bg_block_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070081 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040082 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070083}
84
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070085ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
86 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070087{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040088 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070089 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040090 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070091}
92
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070093ext4_fsblk_t ext4_inode_table(struct super_block *sb,
94 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070095{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040096 return le32_to_cpu(bg->bg_inode_table_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070097 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040098 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070099}
100
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500101__u32 ext4_free_blks_count(struct super_block *sb,
102 struct ext4_group_desc *bg)
103{
104 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
105 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
106 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
107}
108
109__u32 ext4_free_inodes_count(struct super_block *sb,
110 struct ext4_group_desc *bg)
111{
112 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
113 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
114 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
115}
116
117__u32 ext4_used_dirs_count(struct super_block *sb,
118 struct ext4_group_desc *bg)
119{
120 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
121 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
122 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
123}
124
125__u32 ext4_itable_unused_count(struct super_block *sb,
126 struct ext4_group_desc *bg)
127{
128 return le16_to_cpu(bg->bg_itable_unused_lo) |
129 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
130 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
131}
132
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700133void ext4_block_bitmap_set(struct super_block *sb,
134 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700135{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -0400136 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700137 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
138 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700139}
140
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700141void ext4_inode_bitmap_set(struct super_block *sb,
142 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700143{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400144 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700145 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
146 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700147}
148
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700149void ext4_inode_table_set(struct super_block *sb,
150 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700151{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400152 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700153 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
154 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700155}
156
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500157void ext4_free_blks_set(struct super_block *sb,
158 struct ext4_group_desc *bg, __u32 count)
159{
160 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
161 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
162 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
163}
164
165void ext4_free_inodes_set(struct super_block *sb,
166 struct ext4_group_desc *bg, __u32 count)
167{
168 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
169 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
170 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
171}
172
173void ext4_used_dirs_set(struct super_block *sb,
174 struct ext4_group_desc *bg, __u32 count)
175{
176 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
177 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
178 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
179}
180
181void ext4_itable_unused_set(struct super_block *sb,
182 struct ext4_group_desc *bg, __u32 count)
183{
184 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
185 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
186 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
187}
188
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700189/*
Mingming Caodab291a2006-10-11 01:21:01 -0700190 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191 *
192 * The only special thing we need to do here is to make sure that all
193 * journal_end calls result in the superblock being marked dirty, so
194 * that sync() will call the filesystem's write_super callback if
195 * appropriate.
196 */
Mingming Cao617ba132006-10-11 01:20:53 -0700197handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198{
199 journal_t *journal;
200
201 if (sb->s_flags & MS_RDONLY)
202 return ERR_PTR(-EROFS);
203
204 /* Special case here: if the journal has aborted behind our
205 * backs (eg. EIO in the commit thread), then we still need to
206 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700207 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -0500208 if (journal) {
209 if (is_journal_aborted(journal)) {
210 ext4_abort(sb, __func__,
211 "Detected aborted journal");
212 return ERR_PTR(-EROFS);
213 }
214 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215 }
Frank Mayhar03901312009-01-07 00:06:22 -0500216 /*
217 * We're not journaling, return the appropriate indication.
218 */
219 current->journal_info = EXT4_NOJOURNAL_HANDLE;
220 return current->journal_info;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700221}
222
223/*
224 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700225 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 * that sync() will call the filesystem's write_super callback if
227 * appropriate.
228 */
Mingming Cao617ba132006-10-11 01:20:53 -0700229int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700230{
231 struct super_block *sb;
232 int err;
233 int rc;
234
Frank Mayhar03901312009-01-07 00:06:22 -0500235 if (!ext4_handle_valid(handle)) {
236 /*
237 * Do this here since we don't call jbd2_journal_stop() in
238 * no-journal mode.
239 */
240 current->journal_info = NULL;
241 return 0;
242 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243 sb = handle->h_transaction->t_journal->j_private;
244 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700245 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246
247 if (!err)
248 err = rc;
249 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700250 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251 return err;
252}
253
Mingming Cao617ba132006-10-11 01:20:53 -0700254void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700255 struct buffer_head *bh, handle_t *handle, int err)
256{
257 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700258 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259
Frank Mayhar03901312009-01-07 00:06:22 -0500260 BUG_ON(!ext4_handle_valid(handle));
261
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 if (bh)
263 BUFFER_TRACE(bh, "abort");
264
265 if (!handle->h_err)
266 handle->h_err = err;
267
268 if (is_handle_aborted(handle))
269 return;
270
271 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
272 caller, errstr, err_fn);
273
Mingming Caodab291a2006-10-11 01:21:01 -0700274 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275}
276
277/* Deal with the reporting of failure conditions on a filesystem such as
278 * inconsistencies detected or read IO failures.
279 *
280 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700281 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 * write ordering constraints on the superblock which prevent us from
283 * writing it out straight away; and given that the journal is about to
284 * be aborted, we can't rely on the current, or future, transactions to
285 * write out the superblock safely.
286 *
Mingming Caodab291a2006-10-11 01:21:01 -0700287 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 * the journal instead. On recovery, the journal will compain about
289 * that error until we've noted it down and cleared it.
290 */
291
Mingming Cao617ba132006-10-11 01:20:53 -0700292static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293{
Mingming Cao617ba132006-10-11 01:20:53 -0700294 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295
Mingming Cao617ba132006-10-11 01:20:53 -0700296 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
297 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298
299 if (sb->s_flags & MS_RDONLY)
300 return;
301
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400302 if (!test_opt(sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700303 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304
Mingming Cao617ba132006-10-11 01:20:53 -0700305 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700307 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400309 if (test_opt(sb, ERRORS_RO)) {
310 printk(KERN_CRIT "Remounting filesystem read-only\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700311 sb->s_flags |= MS_RDONLY;
312 }
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400313 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700315 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316 sb->s_id);
317}
318
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400319void ext4_error(struct super_block *sb, const char *function,
320 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321{
322 va_list args;
323
324 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400325 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700326 vprintk(fmt, args);
327 printk("\n");
328 va_end(args);
329
Mingming Cao617ba132006-10-11 01:20:53 -0700330 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331}
332
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400333static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700334 char nbuf[16])
335{
336 char *errstr = NULL;
337
338 switch (errno) {
339 case -EIO:
340 errstr = "IO failure";
341 break;
342 case -ENOMEM:
343 errstr = "Out of memory";
344 break;
345 case -EROFS:
Mingming Caodab291a2006-10-11 01:21:01 -0700346 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700347 errstr = "Journal has aborted";
348 else
349 errstr = "Readonly filesystem";
350 break;
351 default:
352 /* If the caller passed in an extra buffer for unknown
353 * errors, textualise them now. Else we just return
354 * NULL. */
355 if (nbuf) {
356 /* Check for truncated error codes... */
357 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
358 errstr = nbuf;
359 }
360 break;
361 }
362
363 return errstr;
364}
365
Mingming Cao617ba132006-10-11 01:20:53 -0700366/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700367 * automatically and invokes the appropriate error response. */
368
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400369void __ext4_std_error(struct super_block *sb, const char *function, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370{
371 char nbuf[16];
372 const char *errstr;
373
374 /* Special case: if the error is EROFS, and we're not already
375 * inside a transaction, then there's really no point in logging
376 * an error. */
377 if (errno == -EROFS && journal_current_handle() == NULL &&
378 (sb->s_flags & MS_RDONLY))
379 return;
380
Mingming Cao617ba132006-10-11 01:20:53 -0700381 errstr = ext4_decode_error(sb, errno, nbuf);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400382 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
383 sb->s_id, function, errstr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700384
Mingming Cao617ba132006-10-11 01:20:53 -0700385 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700386}
387
388/*
Mingming Cao617ba132006-10-11 01:20:53 -0700389 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700390 * abort function may be used to deal with unrecoverable failures such
391 * as journal IO errors or ENOMEM at a critical moment in log management.
392 *
393 * We unconditionally force the filesystem into an ABORT|READONLY state,
394 * unless the error response on the fs has been set to panic in which
395 * case we take the easy way out and panic immediately.
396 */
397
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400398void ext4_abort(struct super_block *sb, const char *function,
399 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400{
401 va_list args;
402
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400403 printk(KERN_CRIT "ext4_abort called.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404
405 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400406 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407 vprintk(fmt, args);
408 printk("\n");
409 va_end(args);
410
411 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700412 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700413
414 if (sb->s_flags & MS_RDONLY)
415 return;
416
417 printk(KERN_CRIT "Remounting filesystem read-only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700418 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700419 sb->s_flags |= MS_RDONLY;
Mingming Cao617ba132006-10-11 01:20:53 -0700420 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400421 if (EXT4_SB(sb)->s_journal)
422 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700423}
424
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400425void ext4_warning(struct super_block *sb, const char *function,
426 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700427{
428 va_list args;
429
430 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700431 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700432 sb->s_id, function);
433 vprintk(fmt, args);
434 printk("\n");
435 va_end(args);
436}
437
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500438void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
439 const char *function, const char *fmt, ...)
440__releases(bitlock)
441__acquires(bitlock)
442{
443 va_list args;
444 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
445
446 va_start(args, fmt);
447 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
448 vprintk(fmt, args);
449 printk("\n");
450 va_end(args);
451
452 if (test_opt(sb, ERRORS_CONT)) {
453 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
454 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400455 ext4_commit_super(sb, 0);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500456 return;
457 }
458 ext4_unlock_group(sb, grp);
459 ext4_handle_error(sb);
460 /*
461 * We only get here in the ERRORS_RO case; relocking the group
462 * may be dangerous, but nothing bad will happen since the
463 * filesystem will have already been marked read/only and the
464 * journal has been aborted. We return 1 as a hint to callers
465 * who might what to use the return value from
466 * ext4_grp_locked_error() to distinguish beween the
467 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
468 * aggressively from the ext4 function in question, with a
469 * more appropriate error code.
470 */
471 ext4_lock_group(sb, grp);
472 return;
473}
474
475
Mingming Cao617ba132006-10-11 01:20:53 -0700476void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700477{
Mingming Cao617ba132006-10-11 01:20:53 -0700478 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479
Mingming Cao617ba132006-10-11 01:20:53 -0700480 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481 return;
482
Harvey Harrison46e665e2008-04-17 10:38:59 -0400483 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700484 "updating to rev %d because of new feature flag, "
485 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700486 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700487
Mingming Cao617ba132006-10-11 01:20:53 -0700488 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
489 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
490 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491 /* leave es->s_feature_*compat flags alone */
492 /* es->s_uuid will be set by e2fsck if empty */
493
494 /*
495 * The rest of the superblock fields should be zero, and if not it
496 * means they are likely already in use, so leave them alone. We
497 * can leave it up to e2fsck to clean up any inconsistencies there.
498 */
499}
500
501/*
502 * Open the external journal device
503 */
Mingming Cao617ba132006-10-11 01:20:53 -0700504static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505{
506 struct block_device *bdev;
507 char b[BDEVNAME_SIZE];
508
509 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
510 if (IS_ERR(bdev))
511 goto fail;
512 return bdev;
513
514fail:
Theodore Ts'oabda1412009-01-06 00:20:32 -0500515 printk(KERN_ERR "EXT4-fs: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516 __bdevname(dev, b), PTR_ERR(bdev));
517 return NULL;
518}
519
520/*
521 * Release the journal device
522 */
Mingming Cao617ba132006-10-11 01:20:53 -0700523static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524{
525 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500526 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700527}
528
Mingming Cao617ba132006-10-11 01:20:53 -0700529static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530{
531 struct block_device *bdev;
532 int ret = -ENODEV;
533
534 bdev = sbi->journal_bdev;
535 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700536 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537 sbi->journal_bdev = NULL;
538 }
539 return ret;
540}
541
542static inline struct inode *orphan_list_entry(struct list_head *l)
543{
Mingming Cao617ba132006-10-11 01:20:53 -0700544 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545}
546
Mingming Cao617ba132006-10-11 01:20:53 -0700547static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548{
549 struct list_head *l;
550
551 printk(KERN_ERR "sb orphan head is %d\n",
552 le32_to_cpu(sbi->s_es->s_last_orphan));
553
554 printk(KERN_ERR "sb_info orphan list:\n");
555 list_for_each(l, &sbi->s_orphan) {
556 struct inode *inode = orphan_list_entry(l);
557 printk(KERN_ERR " "
558 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
559 inode->i_sb->s_id, inode->i_ino, inode,
560 inode->i_mode, inode->i_nlink,
561 NEXT_ORPHAN(inode));
562 }
563}
564
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400565static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566{
Mingming Cao617ba132006-10-11 01:20:53 -0700567 struct ext4_sb_info *sbi = EXT4_SB(sb);
568 struct ext4_super_block *es = sbi->s_es;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400569 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400571 ext4_release_system_zone(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500572 ext4_mb_release(sb);
Alex Tomasa86c6182006-10-11 01:21:03 -0700573 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700574 ext4_xattr_put_super(sb);
Frank Mayhar03901312009-01-07 00:06:22 -0500575 if (sbi->s_journal) {
576 err = jbd2_journal_destroy(sbi->s_journal);
577 sbi->s_journal = NULL;
578 if (err < 0)
579 ext4_abort(sb, __func__,
580 "Couldn't clean up the journal");
581 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700583 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700584 es->s_state = cpu_to_le16(sbi->s_mount_state);
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400585 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400587 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400588 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400589 }
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400590 kobject_del(&sbi->s_kobj);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591
592 for (i = 0; i < sbi->s_gdb_count; i++)
593 brelse(sbi->s_group_desc[i]);
594 kfree(sbi->s_group_desc);
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -0400595 if (is_vmalloc_addr(sbi->s_flex_groups))
596 vfree(sbi->s_flex_groups);
597 else
598 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 percpu_counter_destroy(&sbi->s_freeblocks_counter);
600 percpu_counter_destroy(&sbi->s_freeinodes_counter);
601 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400602 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603 brelse(sbi->s_sbh);
604#ifdef CONFIG_QUOTA
605 for (i = 0; i < MAXQUOTAS; i++)
606 kfree(sbi->s_qf_names[i]);
607#endif
608
609 /* Debugging code just in case the in-memory inode orphan list
610 * isn't empty. The on-disk one can be non-empty if we've
611 * detected an error and taken the fs readonly, but the
612 * in-memory list had better be clean by this point. */
613 if (!list_empty(&sbi->s_orphan))
614 dump_orphan_list(sb, sbi);
615 J_ASSERT(list_empty(&sbi->s_orphan));
616
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700617 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700618 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
619 /*
620 * Invalidate the journal device's buffers. We don't want them
621 * floating about in memory - the physical journal device may
622 * hotswapped, and it breaks the `ro-after' testing code.
623 */
624 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700625 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700626 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700627 }
628 sb->s_fs_info = NULL;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400629 /*
630 * Now that we are completely done shutting down the
631 * superblock, we need to actually destroy the kobject.
632 */
633 unlock_kernel();
634 unlock_super(sb);
635 kobject_put(&sbi->s_kobj);
636 wait_for_completion(&sbi->s_kobj_unregister);
637 lock_super(sb);
638 lock_kernel();
Pekka Enberg705895b2009-02-15 18:07:52 -0500639 kfree(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 kfree(sbi);
641 return;
642}
643
Christoph Lametere18b8902006-12-06 20:33:20 -0800644static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700645
646/*
647 * Called inside transaction, so use GFP_NOFS
648 */
Mingming Cao617ba132006-10-11 01:20:53 -0700649static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650{
Mingming Cao617ba132006-10-11 01:20:53 -0700651 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700652
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800653 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700654 if (!ei)
655 return NULL;
Theodore Ts'o03010a32008-10-10 20:02:48 -0400656#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700657 ei->i_acl = EXT4_ACL_NOT_CACHED;
658 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400661 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700662 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500663 INIT_LIST_HEAD(&ei->i_prealloc_list);
664 spin_lock_init(&ei->i_prealloc_lock);
Frank Mayhar03901312009-01-07 00:06:22 -0500665 /*
666 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
667 * therefore it can be null here. Don't check it, just initialize
668 * jinode.
669 */
Jan Kara678aaf42008-07-11 19:27:31 -0400670 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400671 ei->i_reserved_data_blocks = 0;
672 ei->i_reserved_meta_blocks = 0;
673 ei->i_allocated_meta_blocks = 0;
674 ei->i_delalloc_reserved_flag = 0;
675 spin_lock_init(&(ei->i_block_reservation_lock));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 return &ei->vfs_inode;
677}
678
Mingming Cao617ba132006-10-11 01:20:53 -0700679static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700680{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700681 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
682 printk("EXT4 Inode %p: orphan list check failed!\n",
683 EXT4_I(inode));
684 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
685 EXT4_I(inode), sizeof(struct ext4_inode_info),
686 true);
687 dump_stack();
688 }
Mingming Cao617ba132006-10-11 01:20:53 -0700689 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700690}
691
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700692static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700693{
Mingming Cao617ba132006-10-11 01:20:53 -0700694 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700695
Christoph Lametera35afb82007-05-16 22:10:57 -0700696 INIT_LIST_HEAD(&ei->i_orphan);
Theodore Ts'o03010a32008-10-10 20:02:48 -0400697#ifdef CONFIG_EXT4_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700698 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500700 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700701 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700702}
703
704static int init_inodecache(void)
705{
Mingming Cao617ba132006-10-11 01:20:53 -0700706 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
707 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 0, (SLAB_RECLAIM_ACCOUNT|
709 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900710 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700711 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700712 return -ENOMEM;
713 return 0;
714}
715
716static void destroy_inodecache(void)
717{
Mingming Cao617ba132006-10-11 01:20:53 -0700718 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719}
720
Mingming Cao617ba132006-10-11 01:20:53 -0700721static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722{
Theodore Ts'o03010a32008-10-10 20:02:48 -0400723#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700724 if (EXT4_I(inode)->i_acl &&
725 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
726 posix_acl_release(EXT4_I(inode)->i_acl);
727 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 }
Mingming Cao617ba132006-10-11 01:20:53 -0700729 if (EXT4_I(inode)->i_default_acl &&
730 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
731 posix_acl_release(EXT4_I(inode)->i_default_acl);
732 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 }
734#endif
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400735 ext4_discard_preallocations(inode);
Frank Mayhar03901312009-01-07 00:06:22 -0500736 if (EXT4_JOURNAL(inode))
737 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
Jan Kara678aaf42008-07-11 19:27:31 -0400738 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739}
740
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400741static inline void ext4_show_quota_options(struct seq_file *seq,
742 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700743{
744#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700745 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700746
747 if (sbi->s_jquota_fmt)
748 seq_printf(seq, ",jqfmt=%s",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400749 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750
751 if (sbi->s_qf_names[USRQUOTA])
752 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
753
754 if (sbi->s_qf_names[GRPQUOTA])
755 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
756
Mingming Cao617ba132006-10-11 01:20:53 -0700757 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758 seq_puts(seq, ",usrquota");
759
Mingming Cao617ba132006-10-11 01:20:53 -0700760 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 seq_puts(seq, ",grpquota");
762#endif
763}
764
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700765/*
766 * Show an option if
767 * - it's set to a non-default value OR
768 * - if the per-sb default is different from the global default
769 */
Mingming Cao617ba132006-10-11 01:20:53 -0700770static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500772 int def_errors;
773 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700774 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700775 struct ext4_sb_info *sbi = EXT4_SB(sb);
776 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700777
778 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500779 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700780
781 if (sbi->s_sb_block != 1)
782 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
783 if (test_opt(sb, MINIX_DF))
784 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500785 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700786 seq_puts(seq, ",grpid");
787 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
788 seq_puts(seq, ",nogrpid");
789 if (sbi->s_resuid != EXT4_DEF_RESUID ||
790 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
791 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
792 }
793 if (sbi->s_resgid != EXT4_DEF_RESGID ||
794 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
795 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
796 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500797 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700798 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500799 def_errors == EXT4_ERRORS_CONTINUE) {
800 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700801 }
802 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500803 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500804 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500805 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700806 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500807 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700808 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500809 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700810 seq_puts(seq, ",debug");
811 if (test_opt(sb, OLDALLOC))
812 seq_puts(seq, ",oldalloc");
Theodore Ts'o03010a32008-10-10 20:02:48 -0400813#ifdef CONFIG_EXT4_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500814 if (test_opt(sb, XATTR_USER) &&
815 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700816 seq_puts(seq, ",user_xattr");
817 if (!test_opt(sb, XATTR_USER) &&
818 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
819 seq_puts(seq, ",nouser_xattr");
820 }
821#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400822#ifdef CONFIG_EXT4_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500823 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700824 seq_puts(seq, ",acl");
825 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
826 seq_puts(seq, ",noacl");
827#endif
Theodore Ts'o30773842009-01-03 20:27:38 -0500828 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700829 seq_printf(seq, ",commit=%u",
830 (unsigned) (sbi->s_commit_interval / HZ));
831 }
Theodore Ts'o30773842009-01-03 20:27:38 -0500832 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
833 seq_printf(seq, ",min_batch_time=%u",
834 (unsigned) sbi->s_min_batch_time);
835 }
836 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
837 seq_printf(seq, ",max_batch_time=%u",
838 (unsigned) sbi->s_min_batch_time);
839 }
840
Eric Sandeen571640c2008-05-26 12:29:46 -0400841 /*
842 * We're changing the default of barrier mount option, so
843 * let's always display its mount state so it's clear what its
844 * status is.
845 */
846 seq_puts(seq, ",barrier=");
847 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400848 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
849 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700850 if (test_opt(sb, NOBH))
851 seq_puts(seq, ",nobh");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500852 if (test_opt(sb, I_VERSION))
853 seq_puts(seq, ",i_version");
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400854 if (!test_opt(sb, DELALLOC))
855 seq_puts(seq, ",nodelalloc");
856
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500858 if (sbi->s_stripe)
859 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500860 /*
861 * journal mode get enabled in different ways
862 * So just print the value even if we didn't specify it
863 */
Mingming Cao617ba132006-10-11 01:20:53 -0700864 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700866 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700868 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869 seq_puts(seq, ",data=writeback");
870
Theodore Ts'o240799c2008-10-09 23:53:47 -0400871 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
872 seq_printf(seq, ",inode_readahead_blks=%u",
873 sbi->s_inode_readahead_blks);
874
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400875 if (test_opt(sb, DATA_ERR_ABORT))
876 seq_puts(seq, ",data_err=abort");
877
Theodore Ts'oafd46722009-03-16 23:12:23 -0400878 if (test_opt(sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o06705bf2009-03-28 10:59:57 -0400879 seq_puts(seq, ",noauto_da_alloc");
Theodore Ts'oafd46722009-03-16 23:12:23 -0400880
Mingming Cao617ba132006-10-11 01:20:53 -0700881 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 return 0;
883}
884
885
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700886static struct inode *ext4_nfs_get_inode(struct super_block *sb,
887 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700890
Mingming Cao617ba132006-10-11 01:20:53 -0700891 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700892 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700893 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700894 return ERR_PTR(-ESTALE);
895
896 /* iget isn't really right if the inode is currently unallocated!!
897 *
Mingming Cao617ba132006-10-11 01:20:53 -0700898 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700899 * deleted, so we should be safe.
900 *
901 * Currently we don't know the generation for parent directory, so
902 * a generation of 0 means "accept any"
903 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800904 inode = ext4_iget(sb, ino);
905 if (IS_ERR(inode))
906 return ERR_CAST(inode);
907 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 iput(inode);
909 return ERR_PTR(-ESTALE);
910 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700911
912 return inode;
913}
914
915static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
916 int fh_len, int fh_type)
917{
918 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
919 ext4_nfs_get_inode);
920}
921
922static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
923 int fh_len, int fh_type)
924{
925 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
926 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927}
928
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -0500929/*
930 * Try to release metadata pages (indirect blocks, directories) which are
931 * mapped via the block device. Since these pages could have journal heads
932 * which would prevent try_to_free_buffers() from freeing them, we must use
933 * jbd2 layer's try_to_free_buffers() function to release them.
934 */
935static int bdev_try_to_free_page(struct super_block *sb, struct page *page, gfp_t wait)
936{
937 journal_t *journal = EXT4_SB(sb)->s_journal;
938
939 WARN_ON(PageChecked(page));
940 if (!page_has_buffers(page))
941 return 0;
942 if (journal)
943 return jbd2_journal_try_to_free_buffers(journal, page,
944 wait & ~__GFP_WAIT);
945 return try_to_free_buffers(page);
946}
947
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400949#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400950#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951
Mingming Cao617ba132006-10-11 01:20:53 -0700952static int ext4_write_dquot(struct dquot *dquot);
953static int ext4_acquire_dquot(struct dquot *dquot);
954static int ext4_release_dquot(struct dquot *dquot);
955static int ext4_mark_dquot_dirty(struct dquot *dquot);
956static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -0700957static int ext4_quota_on(struct super_block *sb, int type, int format_id,
958 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -0700959static int ext4_quota_on_mount(struct super_block *sb, int type);
960static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700962static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 const char *data, size_t len, loff_t off);
964
Mingming Cao617ba132006-10-11 01:20:53 -0700965static struct dquot_operations ext4_quota_operations = {
Jan Karaedf72452009-01-12 19:05:26 +0100966 .initialize = dquot_initialize,
967 .drop = dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700968 .alloc_space = dquot_alloc_space,
Mingming Cao60e58e02009-01-22 18:13:05 +0100969 .reserve_space = dquot_reserve_space,
970 .claim_space = dquot_claim_space,
971 .release_rsv = dquot_release_reserved_space,
972 .get_reserved_space = ext4_get_reserved_space,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 .alloc_inode = dquot_alloc_inode,
974 .free_space = dquot_free_space,
975 .free_inode = dquot_free_inode,
976 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700977 .write_dquot = ext4_write_dquot,
978 .acquire_dquot = ext4_acquire_dquot,
979 .release_dquot = ext4_release_dquot,
980 .mark_dirty = ext4_mark_dquot_dirty,
Jan Karaa5b5ee32008-11-25 15:31:35 +0100981 .write_info = ext4_write_info,
982 .alloc_dquot = dquot_alloc,
983 .destroy_dquot = dquot_destroy,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984};
985
Mingming Cao617ba132006-10-11 01:20:53 -0700986static struct quotactl_ops ext4_qctl_operations = {
987 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700988 .quota_off = vfs_quota_off,
989 .quota_sync = vfs_quota_sync,
990 .get_info = vfs_get_dqinfo,
991 .set_info = vfs_set_dqinfo,
992 .get_dqblk = vfs_get_dqblk,
993 .set_dqblk = vfs_set_dqblk
994};
995#endif
996
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800997static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700998 .alloc_inode = ext4_alloc_inode,
999 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001000 .write_inode = ext4_write_inode,
1001 .dirty_inode = ext4_dirty_inode,
1002 .delete_inode = ext4_delete_inode,
1003 .put_super = ext4_put_super,
Mingming Cao617ba132006-10-11 01:20:53 -07001004 .sync_fs = ext4_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -08001005 .freeze_fs = ext4_freeze,
1006 .unfreeze_fs = ext4_unfreeze,
Mingming Cao617ba132006-10-11 01:20:53 -07001007 .statfs = ext4_statfs,
1008 .remount_fs = ext4_remount,
1009 .clear_inode = ext4_clear_inode,
1010 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001011#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001012 .quota_read = ext4_quota_read,
1013 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014#endif
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001015 .bdev_try_to_free_page = bdev_try_to_free_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016};
1017
Theodore Ts'o9ca92382009-05-01 12:52:25 -04001018static const struct super_operations ext4_nojournal_sops = {
1019 .alloc_inode = ext4_alloc_inode,
1020 .destroy_inode = ext4_destroy_inode,
1021 .write_inode = ext4_write_inode,
1022 .dirty_inode = ext4_dirty_inode,
1023 .delete_inode = ext4_delete_inode,
1024 .write_super = ext4_write_super,
1025 .put_super = ext4_put_super,
1026 .statfs = ext4_statfs,
1027 .remount_fs = ext4_remount,
1028 .clear_inode = ext4_clear_inode,
1029 .show_options = ext4_show_options,
1030#ifdef CONFIG_QUOTA
1031 .quota_read = ext4_quota_read,
1032 .quota_write = ext4_quota_write,
1033#endif
1034 .bdev_try_to_free_page = bdev_try_to_free_page,
1035};
1036
Christoph Hellwig39655162007-10-21 16:42:17 -07001037static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001038 .fh_to_dentry = ext4_fh_to_dentry,
1039 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -07001040 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001041};
1042
1043enum {
1044 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1045 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001046 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001048 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
Theodore Ts'o30773842009-01-03 20:27:38 -05001049 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001050 Opt_journal_update, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -05001051 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Curt Wohlgemuthf4033902009-05-01 20:27:20 -04001053 Opt_data_err_abort, Opt_data_err_ignore, Opt_mb_history_length,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1055 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001056 Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err, Opt_resize,
1057 Opt_usrquota, Opt_grpquota, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001058 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001059 Opt_block_validity, Opt_noblock_validity,
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001060 Opt_inode_readahead_blks, Opt_journal_ioprio
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061};
1062
Steven Whitehousea447c092008-10-13 10:46:57 +01001063static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 {Opt_bsd_df, "bsddf"},
1065 {Opt_minix_df, "minixdf"},
1066 {Opt_grpid, "grpid"},
1067 {Opt_grpid, "bsdgroups"},
1068 {Opt_nogrpid, "nogrpid"},
1069 {Opt_nogrpid, "sysvgroups"},
1070 {Opt_resgid, "resgid=%u"},
1071 {Opt_resuid, "resuid=%u"},
1072 {Opt_sb, "sb=%u"},
1073 {Opt_err_cont, "errors=continue"},
1074 {Opt_err_panic, "errors=panic"},
1075 {Opt_err_ro, "errors=remount-ro"},
1076 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001077 {Opt_debug, "debug"},
1078 {Opt_oldalloc, "oldalloc"},
1079 {Opt_orlov, "orlov"},
1080 {Opt_user_xattr, "user_xattr"},
1081 {Opt_nouser_xattr, "nouser_xattr"},
1082 {Opt_acl, "acl"},
1083 {Opt_noacl, "noacl"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001084 {Opt_noload, "noload"},
1085 {Opt_nobh, "nobh"},
1086 {Opt_bh, "bh"},
1087 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -05001088 {Opt_min_batch_time, "min_batch_time=%u"},
1089 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001090 {Opt_journal_update, "journal=update"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001092 {Opt_journal_checksum, "journal_checksum"},
1093 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001094 {Opt_abort, "abort"},
1095 {Opt_data_journal, "data=journal"},
1096 {Opt_data_ordered, "data=ordered"},
1097 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001098 {Opt_data_err_abort, "data_err=abort"},
1099 {Opt_data_err_ignore, "data_err=ignore"},
Curt Wohlgemuthf4033902009-05-01 20:27:20 -04001100 {Opt_mb_history_length, "mb_history_length=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001101 {Opt_offusrjquota, "usrjquota="},
1102 {Opt_usrjquota, "usrjquota=%s"},
1103 {Opt_offgrpjquota, "grpjquota="},
1104 {Opt_grpjquota, "grpjquota=%s"},
1105 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1106 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1107 {Opt_grpquota, "grpquota"},
1108 {Opt_noquota, "noquota"},
1109 {Opt_quota, "quota"},
1110 {Opt_usrquota, "usrquota"},
1111 {Opt_barrier, "barrier=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001112 {Opt_barrier, "barrier"},
1113 {Opt_nobarrier, "nobarrier"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001114 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -05001115 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001116 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -04001117 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001118 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001119 {Opt_block_validity, "block_validity"},
1120 {Opt_noblock_validity, "noblock_validity"},
Theodore Ts'o240799c2008-10-09 23:53:47 -04001121 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001122 {Opt_journal_ioprio, "journal_ioprio=%u"},
Theodore Ts'oafd46722009-03-16 23:12:23 -04001123 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001124 {Opt_auto_da_alloc, "auto_da_alloc"},
1125 {Opt_noauto_da_alloc, "noauto_da_alloc"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -04001126 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127};
1128
Mingming Cao617ba132006-10-11 01:20:53 -07001129static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001130{
Mingming Cao617ba132006-10-11 01:20:53 -07001131 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 char *options = (char *) *data;
1133
1134 if (!options || strncmp(options, "sb=", 3) != 0)
1135 return 1; /* Default location */
1136 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -07001137 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138 sb_block = simple_strtoul(options, &options, 0);
1139 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001140 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001141 (char *) *data);
1142 return 1;
1143 }
1144 if (*options == ',')
1145 options++;
1146 *data = (void *) options;
1147 return sb_block;
1148}
1149
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001150#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1151
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001152static int parse_options(char *options, struct super_block *sb,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001153 unsigned long *journal_devnum,
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001154 unsigned int *journal_ioprio,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001155 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001156{
Mingming Cao617ba132006-10-11 01:20:53 -07001157 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001158 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159 substring_t args[MAX_OPT_ARGS];
1160 int data_opt = 0;
1161 int option;
1162#ifdef CONFIG_QUOTA
Jan Karadfc5d032008-05-13 19:11:51 -04001163 int qtype, qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 char *qname;
1165#endif
1166
1167 if (!options)
1168 return 1;
1169
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001170 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171 int token;
1172 if (!*p)
1173 continue;
1174
1175 token = match_token(p, tokens, args);
1176 switch (token) {
1177 case Opt_bsd_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001178 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179 break;
1180 case Opt_minix_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001181 set_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001182 break;
1183 case Opt_grpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001184 set_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 break;
1186 case Opt_nogrpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001187 clear_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188 break;
1189 case Opt_resuid:
1190 if (match_int(&args[0], &option))
1191 return 0;
1192 sbi->s_resuid = option;
1193 break;
1194 case Opt_resgid:
1195 if (match_int(&args[0], &option))
1196 return 0;
1197 sbi->s_resgid = option;
1198 break;
1199 case Opt_sb:
1200 /* handled by get_sb_block() instead of here */
1201 /* *sb_block = match_int(&args[0]); */
1202 break;
1203 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001204 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1205 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1206 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001207 break;
1208 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001209 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1210 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1211 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001212 break;
1213 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001214 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1215 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1216 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001217 break;
1218 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001219 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001220 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001221 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001222 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001223 break;
1224 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001225 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226 break;
1227 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001228 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001230#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001231 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001232 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001233 break;
1234 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001235 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001236 break;
1237#else
1238 case Opt_user_xattr:
1239 case Opt_nouser_xattr:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001240 printk(KERN_ERR "EXT4 (no)user_xattr options "
1241 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 break;
1243#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001244#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001245 case Opt_acl:
1246 set_opt(sbi->s_mount_opt, POSIX_ACL);
1247 break;
1248 case Opt_noacl:
1249 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1250 break;
1251#else
1252 case Opt_acl:
1253 case Opt_noacl:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001254 printk(KERN_ERR "EXT4 (no)acl options "
1255 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256 break;
1257#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258 case Opt_journal_update:
1259 /* @@@ FIXME */
1260 /* Eventually we will want to be able to create
1261 a journal file here. For now, only allow the
1262 user to specify an existing inode to be the
1263 journal file. */
1264 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001265 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001266 "journal on remount\n");
1267 return 0;
1268 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001269 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001270 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271 case Opt_journal_dev:
1272 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001273 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274 "journal on remount\n");
1275 return 0;
1276 }
1277 if (match_int(&args[0], &option))
1278 return 0;
1279 *journal_devnum = option;
1280 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001281 case Opt_journal_checksum:
1282 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1283 break;
1284 case Opt_journal_async_commit:
1285 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1286 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1287 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001288 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001289 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001290 break;
1291 case Opt_commit:
1292 if (match_int(&args[0], &option))
1293 return 0;
1294 if (option < 0)
1295 return 0;
1296 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001297 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 sbi->s_commit_interval = HZ * option;
1299 break;
Theodore Ts'o30773842009-01-03 20:27:38 -05001300 case Opt_max_batch_time:
1301 if (match_int(&args[0], &option))
1302 return 0;
1303 if (option < 0)
1304 return 0;
1305 if (option == 0)
1306 option = EXT4_DEF_MAX_BATCH_TIME;
1307 sbi->s_max_batch_time = option;
1308 break;
1309 case Opt_min_batch_time:
1310 if (match_int(&args[0], &option))
1311 return 0;
1312 if (option < 0)
1313 return 0;
1314 sbi->s_min_batch_time = option;
1315 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001316 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001317 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001318 goto datacheck;
1319 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001320 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001321 goto datacheck;
1322 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001323 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001324 datacheck:
1325 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001326 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001327 != data_opt) {
1328 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001329 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001330 "mode on remount\n");
1331 return 0;
1332 }
1333 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001334 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001335 sbi->s_mount_opt |= data_opt;
1336 }
1337 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001338 case Opt_data_err_abort:
1339 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1340 break;
1341 case Opt_data_err_ignore:
1342 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1343 break;
Curt Wohlgemuthf4033902009-05-01 20:27:20 -04001344 case Opt_mb_history_length:
1345 if (match_int(&args[0], &option))
1346 return 0;
1347 if (option < 0)
1348 return 0;
1349 sbi->s_mb_history_max = option;
1350 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351#ifdef CONFIG_QUOTA
1352 case Opt_usrjquota:
1353 qtype = USRQUOTA;
1354 goto set_qf_name;
1355 case Opt_grpjquota:
1356 qtype = GRPQUOTA;
1357set_qf_name:
Jan Kara17bd13b2008-08-20 18:14:35 +02001358 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001359 !sbi->s_qf_names[qtype]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001360 printk(KERN_ERR
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001361 "EXT4-fs: Cannot change journaled "
1362 "quota options when quota turned on.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001363 return 0;
1364 }
1365 qname = match_strdup(&args[0]);
1366 if (!qname) {
1367 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001368 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001369 "storing quotafile name.\n");
1370 return 0;
1371 }
1372 if (sbi->s_qf_names[qtype] &&
1373 strcmp(sbi->s_qf_names[qtype], qname)) {
1374 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001375 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001376 "specified.\n", QTYPE2NAME(qtype));
1377 kfree(qname);
1378 return 0;
1379 }
1380 sbi->s_qf_names[qtype] = qname;
1381 if (strchr(sbi->s_qf_names[qtype], '/')) {
1382 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001383 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001384 "filesystem root.\n");
1385 kfree(sbi->s_qf_names[qtype]);
1386 sbi->s_qf_names[qtype] = NULL;
1387 return 0;
1388 }
1389 set_opt(sbi->s_mount_opt, QUOTA);
1390 break;
1391 case Opt_offusrjquota:
1392 qtype = USRQUOTA;
1393 goto clear_qf_name;
1394 case Opt_offgrpjquota:
1395 qtype = GRPQUOTA;
1396clear_qf_name:
Jan Kara17bd13b2008-08-20 18:14:35 +02001397 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001398 sbi->s_qf_names[qtype]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001399 printk(KERN_ERR "EXT4-fs: Cannot change "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001400 "journaled quota options when "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001401 "quota turned on.\n");
1402 return 0;
1403 }
1404 /*
1405 * The space will be released later when all options
1406 * are confirmed to be correct
1407 */
1408 sbi->s_qf_names[qtype] = NULL;
1409 break;
1410 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001411 qfmt = QFMT_VFS_OLD;
1412 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001414 qfmt = QFMT_VFS_V0;
1415set_qf_format:
Jan Kara17bd13b2008-08-20 18:14:35 +02001416 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001417 sbi->s_jquota_fmt != qfmt) {
1418 printk(KERN_ERR "EXT4-fs: Cannot change "
1419 "journaled quota options when "
1420 "quota turned on.\n");
1421 return 0;
1422 }
1423 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001424 break;
1425 case Opt_quota:
1426 case Opt_usrquota:
1427 set_opt(sbi->s_mount_opt, QUOTA);
1428 set_opt(sbi->s_mount_opt, USRQUOTA);
1429 break;
1430 case Opt_grpquota:
1431 set_opt(sbi->s_mount_opt, QUOTA);
1432 set_opt(sbi->s_mount_opt, GRPQUOTA);
1433 break;
1434 case Opt_noquota:
Jan Kara17bd13b2008-08-20 18:14:35 +02001435 if (sb_any_quota_loaded(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001436 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001437 "options when quota turned on.\n");
1438 return 0;
1439 }
1440 clear_opt(sbi->s_mount_opt, QUOTA);
1441 clear_opt(sbi->s_mount_opt, USRQUOTA);
1442 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1443 break;
1444#else
1445 case Opt_quota:
1446 case Opt_usrquota:
1447 case Opt_grpquota:
Jan Karacd59e7b2008-05-13 19:11:51 -04001448 printk(KERN_ERR
1449 "EXT4-fs: quota options not supported.\n");
1450 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001451 case Opt_usrjquota:
1452 case Opt_grpjquota:
1453 case Opt_offusrjquota:
1454 case Opt_offgrpjquota:
1455 case Opt_jqfmt_vfsold:
1456 case Opt_jqfmt_vfsv0:
1457 printk(KERN_ERR
Jan Karacd59e7b2008-05-13 19:11:51 -04001458 "EXT4-fs: journaled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 "supported.\n");
1460 break;
1461 case Opt_noquota:
1462 break;
1463#endif
1464 case Opt_abort:
1465 set_opt(sbi->s_mount_opt, ABORT);
1466 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001467 case Opt_nobarrier:
1468 clear_opt(sbi->s_mount_opt, BARRIER);
1469 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001470 case Opt_barrier:
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001471 if (match_int(&args[0], &option)) {
1472 set_opt(sbi->s_mount_opt, BARRIER);
1473 break;
1474 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001475 if (option)
1476 set_opt(sbi->s_mount_opt, BARRIER);
1477 else
1478 clear_opt(sbi->s_mount_opt, BARRIER);
1479 break;
1480 case Opt_ignore:
1481 break;
1482 case Opt_resize:
1483 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001484 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485 "for remount\n");
1486 return 0;
1487 }
1488 if (match_int(&args[0], &option) != 0)
1489 return 0;
1490 *n_blocks_count = option;
1491 break;
1492 case Opt_nobh:
1493 set_opt(sbi->s_mount_opt, NOBH);
1494 break;
1495 case Opt_bh:
1496 clear_opt(sbi->s_mount_opt, NOBH);
1497 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001498 case Opt_i_version:
1499 set_opt(sbi->s_mount_opt, I_VERSION);
1500 sb->s_flags |= MS_I_VERSION;
1501 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001502 case Opt_nodelalloc:
1503 clear_opt(sbi->s_mount_opt, DELALLOC);
1504 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001505 case Opt_stripe:
1506 if (match_int(&args[0], &option))
1507 return 0;
1508 if (option < 0)
1509 return 0;
1510 sbi->s_stripe = option;
1511 break;
Alex Tomas64769242008-07-11 19:27:31 -04001512 case Opt_delalloc:
1513 set_opt(sbi->s_mount_opt, DELALLOC);
1514 break;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001515 case Opt_block_validity:
1516 set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1517 break;
1518 case Opt_noblock_validity:
1519 clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1520 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001521 case Opt_inode_readahead_blks:
1522 if (match_int(&args[0], &option))
1523 return 0;
1524 if (option < 0 || option > (1 << 30))
1525 return 0;
Theodore Ts'of7c43952009-04-24 23:31:59 -04001526 if (!is_power_of_2(option)) {
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04001527 printk(KERN_ERR "EXT4-fs: inode_readahead_blks"
1528 " must be a power of 2\n");
1529 return 0;
1530 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04001531 sbi->s_inode_readahead_blks = option;
1532 break;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001533 case Opt_journal_ioprio:
1534 if (match_int(&args[0], &option))
1535 return 0;
1536 if (option < 0 || option > 7)
1537 break;
1538 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1539 option);
1540 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001541 case Opt_noauto_da_alloc:
1542 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1543 break;
Theodore Ts'oafd46722009-03-16 23:12:23 -04001544 case Opt_auto_da_alloc:
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001545 if (match_int(&args[0], &option)) {
1546 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1547 break;
1548 }
Theodore Ts'oafd46722009-03-16 23:12:23 -04001549 if (option)
1550 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1551 else
1552 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1553 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001554 default:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001555 printk(KERN_ERR
1556 "EXT4-fs: Unrecognized mount option \"%s\" "
1557 "or missing value\n", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001558 return 0;
1559 }
1560 }
1561#ifdef CONFIG_QUOTA
1562 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001563 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001564 sbi->s_qf_names[USRQUOTA])
1565 clear_opt(sbi->s_mount_opt, USRQUOTA);
1566
Mingming Cao617ba132006-10-11 01:20:53 -07001567 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001568 sbi->s_qf_names[GRPQUOTA])
1569 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1570
1571 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001572 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001574 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1575 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001576 "format mixing.\n");
1577 return 0;
1578 }
1579
1580 if (!sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001581 printk(KERN_ERR "EXT4-fs: journaled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001582 "not specified.\n");
1583 return 0;
1584 }
1585 } else {
1586 if (sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001587 printk(KERN_ERR "EXT4-fs: journaled quota format "
1588 "specified with no journaling "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589 "enabled.\n");
1590 return 0;
1591 }
1592 }
1593#endif
1594 return 1;
1595}
1596
Mingming Cao617ba132006-10-11 01:20:53 -07001597static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001598 int read_only)
1599{
Mingming Cao617ba132006-10-11 01:20:53 -07001600 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001601 int res = 0;
1602
Mingming Cao617ba132006-10-11 01:20:53 -07001603 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001604 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1605 "forcing read-only mode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606 res = MS_RDONLY;
1607 }
1608 if (read_only)
1609 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001610 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001611 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1612 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001613 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001614 printk(KERN_WARNING
1615 "EXT4-fs warning: mounting fs with errors, "
1616 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001617 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1618 le16_to_cpu(es->s_mnt_count) >=
1619 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001620 printk(KERN_WARNING
1621 "EXT4-fs warning: maximal mount count reached, "
1622 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001623 else if (le32_to_cpu(es->s_checkinterval) &&
1624 (le32_to_cpu(es->s_lastcheck) +
1625 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001626 printk(KERN_WARNING
1627 "EXT4-fs warning: checktime reached, "
1628 "running e2fsck is recommended\n");
Frank Mayhar03901312009-01-07 00:06:22 -05001629 if (!sbi->s_journal)
1630 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001631 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001632 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001633 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001634 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001635 ext4_update_dynamic_rev(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05001636 if (sbi->s_journal)
1637 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001638
Theodore Ts'oe2d67052009-05-01 00:33:44 -04001639 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001640 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001641 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1643 sb->s_blocksize,
1644 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001645 EXT4_BLOCKS_PER_GROUP(sb),
1646 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647 sbi->s_mount_opt);
1648
Frank Mayhar03901312009-01-07 00:06:22 -05001649 if (EXT4_SB(sb)->s_journal) {
1650 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1651 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1652 "external", EXT4_SB(sb)->s_journal->j_devname);
1653 } else {
1654 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1655 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656 return res;
1657}
1658
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001659static int ext4_fill_flex_info(struct super_block *sb)
1660{
1661 struct ext4_sb_info *sbi = EXT4_SB(sb);
1662 struct ext4_group_desc *gdp = NULL;
1663 struct buffer_head *bh;
1664 ext4_group_t flex_group_count;
1665 ext4_group_t flex_group;
1666 int groups_per_flex = 0;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001667 size_t size;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001668 int i;
1669
1670 if (!sbi->s_es->s_log_groups_per_flex) {
1671 sbi->s_log_groups_per_flex = 0;
1672 return 1;
1673 }
1674
1675 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1676 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1677
Frederic Bohec62a11f2008-09-08 10:20:24 -04001678 /* We allocate both existing and potentially added groups */
1679 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001680 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1681 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001682 size = flex_group_count * sizeof(struct flex_groups);
1683 sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1684 if (sbi->s_flex_groups == NULL) {
1685 sbi->s_flex_groups = vmalloc(size);
1686 if (sbi->s_flex_groups)
1687 memset(sbi->s_flex_groups, 0, size);
1688 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001689 if (sbi->s_flex_groups == NULL) {
Li Zefanec05e862008-07-24 12:49:59 -04001690 printk(KERN_ERR "EXT4-fs: not enough memory for "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001691 "%u flex groups\n", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001692 goto failed;
1693 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001694
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001695 for (i = 0; i < sbi->s_groups_count; i++) {
1696 gdp = ext4_get_group_desc(sb, i, &bh);
1697
1698 flex_group = ext4_flex_group(sbi, i);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05001699 atomic_set(&sbi->s_flex_groups[flex_group].free_inodes,
1700 ext4_free_inodes_count(sb, gdp));
1701 atomic_set(&sbi->s_flex_groups[flex_group].free_blocks,
1702 ext4_free_blks_count(sb, gdp));
Theodore Ts'o7d39db12009-03-04 19:31:53 -05001703 atomic_set(&sbi->s_flex_groups[flex_group].used_dirs,
1704 ext4_used_dirs_count(sb, gdp));
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001705 }
1706
1707 return 1;
1708failed:
1709 return 0;
1710}
1711
Andreas Dilger717d50e2007-10-16 18:38:25 -04001712__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1713 struct ext4_group_desc *gdp)
1714{
1715 __u16 crc = 0;
1716
1717 if (sbi->s_es->s_feature_ro_compat &
1718 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1719 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1720 __le32 le_group = cpu_to_le32(block_group);
1721
1722 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1723 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1724 crc = crc16(crc, (__u8 *)gdp, offset);
1725 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1726 /* for checksum of struct ext4_group_desc do the rest...*/
1727 if ((sbi->s_es->s_feature_incompat &
1728 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1729 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1730 crc = crc16(crc, (__u8 *)gdp + offset,
1731 le16_to_cpu(sbi->s_es->s_desc_size) -
1732 offset);
1733 }
1734
1735 return cpu_to_le16(crc);
1736}
1737
1738int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1739 struct ext4_group_desc *gdp)
1740{
1741 if ((sbi->s_es->s_feature_ro_compat &
1742 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1743 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1744 return 0;
1745
1746 return 1;
1747}
1748
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001749/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001750static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001751{
Mingming Cao617ba132006-10-11 01:20:53 -07001752 struct ext4_sb_info *sbi = EXT4_SB(sb);
1753 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1754 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001755 ext4_fsblk_t block_bitmap;
1756 ext4_fsblk_t inode_bitmap;
1757 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001758 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001759 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001760
Jose R. Santosce421582007-10-16 18:38:25 -04001761 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1762 flexbg_flag = 1;
1763
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001764 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001765
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001766 for (i = 0; i < sbi->s_groups_count; i++) {
1767 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1768
Jose R. Santosce421582007-10-16 18:38:25 -04001769 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001770 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001771 else
1772 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001773 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001775 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001776 if (block_bitmap < first_block || block_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001777 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001778 "Block bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001779 "(block %llu)!\n", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001780 return 0;
1781 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001782 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001783 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001784 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001785 "Inode bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001786 "(block %llu)!\n", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787 return 0;
1788 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001789 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001790 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001791 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001792 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001793 "Inode table for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001794 "(block %llu)!\n", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001795 return 0;
1796 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001797 ext4_lock_group(sb, i);
Andreas Dilger717d50e2007-10-16 18:38:25 -04001798 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001799 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001800 "Checksum for group %u failed (%u!=%u)\n",
Josef Bacikc19204b2008-04-29 22:00:28 -04001801 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1802 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001803 if (!(sb->s_flags & MS_RDONLY)) {
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001804 ext4_unlock_group(sb, i);
Theodore Ts'o8a266462008-07-26 14:34:21 -04001805 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001806 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001807 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001808 ext4_unlock_group(sb, i);
Jose R. Santosce421582007-10-16 18:38:25 -04001809 if (!flexbg_flag)
1810 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001811 }
1812
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001813 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001814 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001815 return 1;
1816}
1817
Mingming Cao617ba132006-10-11 01:20:53 -07001818/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819 * the superblock) which were deleted from all directories, but held open by
1820 * a process at the time of a crash. We walk the list and try to delete these
1821 * inodes at recovery time (only with a read-write filesystem).
1822 *
1823 * In order to keep the orphan inode chain consistent during traversal (in
1824 * case of crash during recovery), we link each inode into the superblock
1825 * orphan list_head and handle it the same way as an inode deletion during
1826 * normal operation (which journals the operations for us).
1827 *
1828 * We only do an iget() and an iput() on each inode, which is very safe if we
1829 * accidentally point at an in-use or already deleted inode. The worst that
1830 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001831 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001832 * e2fsck was run on this filesystem, and it must have already done the orphan
1833 * inode cleanup for us, so we can safely abort without any further action.
1834 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001835static void ext4_orphan_cleanup(struct super_block *sb,
1836 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001837{
1838 unsigned int s_flags = sb->s_flags;
1839 int nr_orphans = 0, nr_truncates = 0;
1840#ifdef CONFIG_QUOTA
1841 int i;
1842#endif
1843 if (!es->s_last_orphan) {
1844 jbd_debug(4, "no orphan inodes to clean up\n");
1845 return;
1846 }
1847
Eric Sandeena8f48a92006-12-06 20:40:13 -08001848 if (bdev_read_only(sb->s_bdev)) {
1849 printk(KERN_ERR "EXT4-fs: write access "
1850 "unavailable, skipping orphan cleanup.\n");
1851 return;
1852 }
1853
Mingming Cao617ba132006-10-11 01:20:53 -07001854 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001855 if (es->s_last_orphan)
1856 jbd_debug(1, "Errors on filesystem, "
1857 "clearing orphan list.\n");
1858 es->s_last_orphan = 0;
1859 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1860 return;
1861 }
1862
1863 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001864 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001865 sb->s_id);
1866 sb->s_flags &= ~MS_RDONLY;
1867 }
1868#ifdef CONFIG_QUOTA
1869 /* Needed for iput() to work correctly and not trash data */
1870 sb->s_flags |= MS_ACTIVE;
1871 /* Turn on quotas so that they are updated correctly */
1872 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001873 if (EXT4_SB(sb)->s_qf_names[i]) {
1874 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001875 if (ret < 0)
1876 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001877 "EXT4-fs: Cannot turn on journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001878 "quota: error %d\n", ret);
1879 }
1880 }
1881#endif
1882
1883 while (es->s_last_orphan) {
1884 struct inode *inode;
1885
Josef Bacik97bd42b2008-04-29 22:04:56 -04001886 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1887 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001888 es->s_last_orphan = 0;
1889 break;
1890 }
1891
Mingming Cao617ba132006-10-11 01:20:53 -07001892 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Jan Karaa269eb12009-01-26 17:04:39 +01001893 vfs_dq_init(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001894 if (inode->i_nlink) {
1895 printk(KERN_DEBUG
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001896 "%s: truncating inode %lu to %lld bytes\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001897 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001898 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001899 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001900 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001901 nr_truncates++;
1902 } else {
1903 printk(KERN_DEBUG
1904 "%s: deleting unreferenced inode %lu\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001905 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001906 jbd_debug(2, "deleting unreferenced inode %lu\n",
1907 inode->i_ino);
1908 nr_orphans++;
1909 }
1910 iput(inode); /* The delete magic happens here! */
1911 }
1912
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001913#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001914
1915 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001916 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001917 sb->s_id, PLURAL(nr_orphans));
1918 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001919 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001920 sb->s_id, PLURAL(nr_truncates));
1921#ifdef CONFIG_QUOTA
1922 /* Turn quotas off */
1923 for (i = 0; i < MAXQUOTAS; i++) {
1924 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07001925 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001926 }
1927#endif
1928 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1929}
Eric Sandeencd2291a2008-01-28 23:58:27 -05001930/*
1931 * Maximal extent format file size.
1932 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1933 * extent format containers, within a sector_t, and within i_blocks
1934 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1935 * so that won't be a limiting factor.
1936 *
1937 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1938 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001939static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05001940{
1941 loff_t res;
1942 loff_t upper_limit = MAX_LFS_FILESIZE;
1943
1944 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001945 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05001946 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001947 * CONFIG_LBD is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05001948 * i_block represent total blocks in 512 bytes
1949 * 32 == size of vfs inode i_blocks * 8
1950 */
1951 upper_limit = (1LL << 32) - 1;
1952
1953 /* total blocks in file system block size */
1954 upper_limit >>= (blkbits - 9);
1955 upper_limit <<= blkbits;
1956 }
1957
1958 /* 32-bit extent-start container, ee_block */
1959 res = 1LL << 32;
1960 res <<= blkbits;
1961 res -= 1;
1962
1963 /* Sanity check against vm- & vfs- imposed limits */
1964 if (res > upper_limit)
1965 res = upper_limit;
1966
1967 return res;
1968}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001970/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05001971 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001972 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1973 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001974 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001975static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001976{
Mingming Cao617ba132006-10-11 01:20:53 -07001977 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001978 int meta_blocks;
1979 loff_t upper_limit;
1980 /* This is calculated to be the largest file size for a
Eric Sandeencd2291a2008-01-28 23:58:27 -05001981 * dense, bitmapped file such that the total number of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001982 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001983 * does not exceed 2^48 -1
1984 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1985 * total number of 512 bytes blocks of the file
1986 */
1987
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001988 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001989 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001990 * !has_huge_files or CONFIG_LBD is not enabled
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001991 * implies the inode i_block represent total blocks in
1992 * 512 bytes 32 == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001993 */
1994 upper_limit = (1LL << 32) - 1;
1995
1996 /* total blocks in file system block size */
1997 upper_limit >>= (bits - 9);
1998
1999 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05002000 /*
2001 * We use 48 bit ext4_inode i_blocks
2002 * With EXT4_HUGE_FILE_FL set the i_blocks
2003 * represent total number of blocks in
2004 * file system block size
2005 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002006 upper_limit = (1LL << 48) - 1;
2007
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002008 }
2009
2010 /* indirect blocks */
2011 meta_blocks = 1;
2012 /* double indirect blocks */
2013 meta_blocks += 1 + (1LL << (bits-2));
2014 /* tripple indirect blocks */
2015 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2016
2017 upper_limit -= meta_blocks;
2018 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002019
2020 res += 1LL << (bits-2);
2021 res += 1LL << (2*(bits-2));
2022 res += 1LL << (3*(bits-2));
2023 res <<= bits;
2024 if (res > upper_limit)
2025 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002026
2027 if (res > MAX_LFS_FILESIZE)
2028 res = MAX_LFS_FILESIZE;
2029
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002030 return res;
2031}
2032
Mingming Cao617ba132006-10-11 01:20:53 -07002033static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002034 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002035{
Mingming Cao617ba132006-10-11 01:20:53 -07002036 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05002037 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002038 int has_super = 0;
2039
2040 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2041
Mingming Cao617ba132006-10-11 01:20:53 -07002042 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002043 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002044 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002045 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07002046 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002047 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07002048 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002049}
2050
Alex Tomasc9de5602008-01-29 00:19:52 -05002051/**
2052 * ext4_get_stripe_size: Get the stripe size.
2053 * @sbi: In memory super block info
2054 *
2055 * If we have specified it via mount option, then
2056 * use the mount option value. If the value specified at mount time is
2057 * greater than the blocks per group use the super block value.
2058 * If the super block value is greater than blocks per group return 0.
2059 * Allocator needs it be less than blocks per group.
2060 *
2061 */
2062static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2063{
2064 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2065 unsigned long stripe_width =
2066 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2067
2068 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2069 return sbi->s_stripe;
2070
2071 if (stripe_width <= sbi->s_blocks_per_group)
2072 return stripe_width;
2073
2074 if (stride <= sbi->s_blocks_per_group)
2075 return stride;
2076
2077 return 0;
2078}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002079
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002080/* sysfs supprt */
2081
2082struct ext4_attr {
2083 struct attribute attr;
2084 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
2085 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
2086 const char *, size_t);
2087 int offset;
2088};
2089
2090static int parse_strtoul(const char *buf,
2091 unsigned long max, unsigned long *value)
2092{
2093 char *endp;
2094
2095 while (*buf && isspace(*buf))
2096 buf++;
2097 *value = simple_strtoul(buf, &endp, 0);
2098 while (*endp && isspace(*endp))
2099 endp++;
2100 if (*endp || *value > max)
2101 return -EINVAL;
2102
2103 return 0;
2104}
2105
2106static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2107 struct ext4_sb_info *sbi,
2108 char *buf)
2109{
2110 return snprintf(buf, PAGE_SIZE, "%llu\n",
2111 (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2112}
2113
2114static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2115 struct ext4_sb_info *sbi, char *buf)
2116{
2117 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2118
2119 return snprintf(buf, PAGE_SIZE, "%lu\n",
2120 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2121 sbi->s_sectors_written_start) >> 1);
2122}
2123
2124static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2125 struct ext4_sb_info *sbi, char *buf)
2126{
2127 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2128
2129 return snprintf(buf, PAGE_SIZE, "%llu\n",
2130 sbi->s_kbytes_written +
2131 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2132 EXT4_SB(sb)->s_sectors_written_start) >> 1));
2133}
2134
2135static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2136 struct ext4_sb_info *sbi,
2137 const char *buf, size_t count)
2138{
2139 unsigned long t;
2140
2141 if (parse_strtoul(buf, 0x40000000, &t))
2142 return -EINVAL;
2143
Theodore Ts'of7c43952009-04-24 23:31:59 -04002144 if (!is_power_of_2(t))
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002145 return -EINVAL;
2146
2147 sbi->s_inode_readahead_blks = t;
2148 return count;
2149}
2150
2151static ssize_t sbi_ui_show(struct ext4_attr *a,
2152 struct ext4_sb_info *sbi, char *buf)
2153{
2154 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2155
2156 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2157}
2158
2159static ssize_t sbi_ui_store(struct ext4_attr *a,
2160 struct ext4_sb_info *sbi,
2161 const char *buf, size_t count)
2162{
2163 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2164 unsigned long t;
2165
2166 if (parse_strtoul(buf, 0xffffffff, &t))
2167 return -EINVAL;
2168 *ui = t;
2169 return count;
2170}
2171
2172#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2173static struct ext4_attr ext4_attr_##_name = { \
2174 .attr = {.name = __stringify(_name), .mode = _mode }, \
2175 .show = _show, \
2176 .store = _store, \
2177 .offset = offsetof(struct ext4_sb_info, _elname), \
2178}
2179#define EXT4_ATTR(name, mode, show, store) \
2180static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2181
2182#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2183#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2184#define EXT4_RW_ATTR_SBI_UI(name, elname) \
2185 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2186#define ATTR_LIST(name) &ext4_attr_##name.attr
2187
2188EXT4_RO_ATTR(delayed_allocation_blocks);
2189EXT4_RO_ATTR(session_write_kbytes);
2190EXT4_RO_ATTR(lifetime_write_kbytes);
2191EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2192 inode_readahead_blks_store, s_inode_readahead_blks);
2193EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2194EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2195EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2196EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2197EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2198EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2199
2200static struct attribute *ext4_attrs[] = {
2201 ATTR_LIST(delayed_allocation_blocks),
2202 ATTR_LIST(session_write_kbytes),
2203 ATTR_LIST(lifetime_write_kbytes),
2204 ATTR_LIST(inode_readahead_blks),
2205 ATTR_LIST(mb_stats),
2206 ATTR_LIST(mb_max_to_scan),
2207 ATTR_LIST(mb_min_to_scan),
2208 ATTR_LIST(mb_order2_req),
2209 ATTR_LIST(mb_stream_req),
2210 ATTR_LIST(mb_group_prealloc),
2211 NULL,
2212};
2213
2214static ssize_t ext4_attr_show(struct kobject *kobj,
2215 struct attribute *attr, char *buf)
2216{
2217 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2218 s_kobj);
2219 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2220
2221 return a->show ? a->show(a, sbi, buf) : 0;
2222}
2223
2224static ssize_t ext4_attr_store(struct kobject *kobj,
2225 struct attribute *attr,
2226 const char *buf, size_t len)
2227{
2228 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2229 s_kobj);
2230 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2231
2232 return a->store ? a->store(a, sbi, buf, len) : 0;
2233}
2234
2235static void ext4_sb_release(struct kobject *kobj)
2236{
2237 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2238 s_kobj);
2239 complete(&sbi->s_kobj_unregister);
2240}
2241
2242
2243static struct sysfs_ops ext4_attr_ops = {
2244 .show = ext4_attr_show,
2245 .store = ext4_attr_store,
2246};
2247
2248static struct kobj_type ext4_ktype = {
2249 .default_attrs = ext4_attrs,
2250 .sysfs_ops = &ext4_attr_ops,
2251 .release = ext4_sb_release,
2252};
2253
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002254static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04002255 __releases(kernel_lock)
2256 __acquires(kernel_lock)
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002257
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002258{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002259 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002260 struct ext4_super_block *es = NULL;
2261 struct ext4_sb_info *sbi;
2262 ext4_fsblk_t block;
2263 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002264 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002265 unsigned long offset = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002266 unsigned long journal_devnum = 0;
2267 unsigned long def_mount_opts;
2268 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002269 char *cp;
Frank Mayhar03901312009-01-07 00:06:22 -05002270 const char *descr;
David Howells1d1fe1e2008-02-07 00:15:37 -08002271 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002272 int blocksize;
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002273 unsigned int db_count;
2274 unsigned int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002275 int needs_recovery, has_huge_files;
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002276 int features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002277 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07002278 int err;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002279 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002280
2281 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2282 if (!sbi)
2283 return -ENOMEM;
Pekka Enberg705895b2009-02-15 18:07:52 -05002284
2285 sbi->s_blockgroup_lock =
2286 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2287 if (!sbi->s_blockgroup_lock) {
2288 kfree(sbi);
2289 return -ENOMEM;
2290 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002291 sb->s_fs_info = sbi;
2292 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002293 sbi->s_resuid = EXT4_DEF_RESUID;
2294 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04002295 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07002296 sbi->s_sb_block = sb_block;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05002297 sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2298 sectors[1]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002299
2300 unlock_kernel();
2301
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002302 /* Cleanup superblock name */
2303 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2304 *cp = '!';
2305
Mingming Cao617ba132006-10-11 01:20:53 -07002306 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002307 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07002308 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002309 goto out_fail;
2310 }
2311
2312 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002313 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002314 * block sizes. We need to calculate the offset from buffer start.
2315 */
Mingming Cao617ba132006-10-11 01:20:53 -07002316 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002317 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2318 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002319 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002320 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002321 }
2322
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002323 if (!(bh = sb_bread(sb, logical_sb_block))) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002324 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002325 goto out_fail;
2326 }
2327 /*
2328 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07002329 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002330 */
Mingming Cao617ba132006-10-11 01:20:53 -07002331 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002332 sbi->s_es = es;
2333 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07002334 if (sb->s_magic != EXT4_SUPER_MAGIC)
2335 goto cantfind_ext4;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05002336 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002337
2338 /* Set defaults before we parse the mount options */
2339 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07002340 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002341 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07002342 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002343 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07002344 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002345 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04002346#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07002347 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002348 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002349#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04002350#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07002351 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002352 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002353#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002354 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2355 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2356 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2357 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2358 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2359 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002360
Mingming Cao617ba132006-10-11 01:20:53 -07002361 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002362 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002363 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002364 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002365 else
2366 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002367
2368 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2369 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
Theodore Ts'o30773842009-01-03 20:27:38 -05002370 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2371 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2372 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Curt Wohlgemuthf4033902009-05-01 20:27:20 -04002373 sbi->s_mb_history_max = default_mb_history_length;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002374
Eric Sandeen571640c2008-05-26 12:29:46 -04002375 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002376
Mingming Cao1e2462f2007-07-18 09:00:55 -04002377 /*
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002378 * enable delayed allocation by default
2379 * Use -o nodelalloc to turn it off
2380 */
2381 set_opt(sbi->s_mount_opt, DELALLOC);
2382
2383
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002384 if (!parse_options((char *) data, sb, &journal_devnum,
2385 &journal_ioprio, NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002386 goto failed_mount;
2387
2388 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002389 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002390
Mingming Cao617ba132006-10-11 01:20:53 -07002391 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2392 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2393 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2394 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002395 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002396 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002397 "running e2fsck is recommended\n");
Theodore Tso469108f2008-02-10 01:11:44 -05002398
2399 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002400 * Check feature flags regardless of the revision level, since we
2401 * previously didn't change the revision level when setting the flags,
2402 * so there is a chance incompat flags are set on a rev 0 filesystem.
2403 */
Mingming Cao617ba132006-10-11 01:20:53 -07002404 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002405 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002406 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002407 "unsupported optional features (%x).\n", sb->s_id,
2408 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2409 ~EXT4_FEATURE_INCOMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002410 goto failed_mount;
2411 }
Mingming Cao617ba132006-10-11 01:20:53 -07002412 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002413 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002414 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002415 "unsupported optional features (%x).\n", sb->s_id,
2416 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2417 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002418 goto failed_mount;
2419 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002420 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2421 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2422 if (has_huge_files) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002423 /*
2424 * Large file size enabled file system can only be
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002425 * mount if kernel is build with CONFIG_LBD
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002426 */
2427 if (sizeof(root->i_blocks) < sizeof(u64) &&
2428 !(sb->s_flags & MS_RDONLY)) {
2429 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2430 "files cannot be mounted read-write "
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002431 "without CONFIG_LBD.\n", sb->s_id);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002432 goto failed_mount;
2433 }
2434 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002435 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2436
Mingming Cao617ba132006-10-11 01:20:53 -07002437 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2438 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002440 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002441 blocksize, sb->s_id);
2442 goto failed_mount;
2443 }
2444
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002445 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002446
2447 /* Validate the filesystem blocksize */
2448 if (!sb_set_blocksize(sb, blocksize)) {
2449 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2450 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002451 goto failed_mount;
2452 }
2453
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002454 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002455 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2456 offset = do_div(logical_sb_block, blocksize);
2457 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002458 if (!bh) {
2459 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002460 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002461 goto failed_mount;
2462 }
Mingming Cao617ba132006-10-11 01:20:53 -07002463 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002464 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002465 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002466 printk(KERN_ERR
2467 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002468 goto failed_mount;
2469 }
2470 }
2471
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002472 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2473 has_huge_files);
2474 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002475
Mingming Cao617ba132006-10-11 01:20:53 -07002476 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2477 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2478 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002479 } else {
2480 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2481 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002482 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002483 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002484 (sbi->s_inode_size > blocksize)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002485 printk(KERN_ERR
2486 "EXT4-fs: unsupported inode size: %d\n",
2487 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002488 goto failed_mount;
2489 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002490 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2491 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002492 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002493 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2494 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002495 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002496 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002497 !is_power_of_2(sbi->s_desc_size)) {
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002498 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002499 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002500 sbi->s_desc_size);
2501 goto failed_mount;
2502 }
2503 } else
2504 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002505 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002506 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002507 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002508 goto cantfind_ext4;
2509 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002510 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002511 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002512 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2513 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002514 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002515 sbi->s_sbh = bh;
2516 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002517 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2518 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002519 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002520 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2521 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002522 i = le32_to_cpu(es->s_flags);
2523 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2524 sbi->s_hash_unsigned = 3;
2525 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2526#ifdef __CHAR_UNSIGNED__
2527 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2528 sbi->s_hash_unsigned = 3;
2529#else
2530 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2531#endif
2532 sb->s_dirt = 1;
2533 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002534
2535 if (sbi->s_blocks_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002536 printk(KERN_ERR
2537 "EXT4-fs: #blocks per group too big: %lu\n",
2538 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002539 goto failed_mount;
2540 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002541 if (sbi->s_inodes_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002542 printk(KERN_ERR
2543 "EXT4-fs: #inodes per group too big: %lu\n",
2544 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002545 goto failed_mount;
2546 }
2547
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002548 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002549 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002550 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002551 " too large to mount safely\n", sb->s_id);
2552 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07002553 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002554 "enabled\n");
2555 goto failed_mount;
2556 }
2557
Mingming Cao617ba132006-10-11 01:20:53 -07002558 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2559 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002560
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04002561 /* check blocks count against device size */
2562 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2563 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
2564 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu "
2565 "exceeds size of device (%llu blocks)\n",
2566 ext4_blocks_count(es), blocks_count);
2567 goto failed_mount;
2568 }
2569
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002570 /*
2571 * It makes no sense for the first data block to be beyond the end
2572 * of the filesystem.
2573 */
2574 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
2575 printk(KERN_WARNING "EXT4-fs: bad geometry: first data"
2576 "block %u is beyond end of filesystem (%llu)\n",
2577 le32_to_cpu(es->s_first_data_block),
2578 ext4_blocks_count(es));
Eric Sandeene7c95592008-01-28 23:58:27 -05002579 goto failed_mount;
2580 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002581 blocks_count = (ext4_blocks_count(es) -
2582 le32_to_cpu(es->s_first_data_block) +
2583 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2584 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002585 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
2586 printk(KERN_WARNING "EXT4-fs: groups count too large: %u "
2587 "(block count %llu, first data block %u, "
2588 "blocks per group %lu)\n", sbi->s_groups_count,
2589 ext4_blocks_count(es),
2590 le32_to_cpu(es->s_first_data_block),
2591 EXT4_BLOCKS_PER_GROUP(sb));
2592 goto failed_mount;
2593 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002594 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07002595 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2596 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002597 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002598 GFP_KERNEL);
2599 if (sbi->s_group_desc == NULL) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002600 printk(KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002601 goto failed_mount;
2602 }
2603
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002604#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002605 if (ext4_proc_root)
2606 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002607#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002608
Pekka Enberg705895b2009-02-15 18:07:52 -05002609 bgl_lock_init(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002610
2611 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002612 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002613 sbi->s_group_desc[i] = sb_bread(sb, block);
2614 if (!sbi->s_group_desc[i]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002615 printk(KERN_ERR "EXT4-fs: "
2616 "can't read group descriptor %d\n", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002617 db_count = i;
2618 goto failed_mount2;
2619 }
2620 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002621 if (!ext4_check_descriptors(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002622 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002623 goto failed_mount2;
2624 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002625 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2626 if (!ext4_fill_flex_info(sb)) {
2627 printk(KERN_ERR
2628 "EXT4-fs: unable to initialize "
2629 "flex_bg meta info!\n");
2630 goto failed_mount2;
2631 }
2632
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002633 sbi->s_gdb_count = db_count;
2634 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2635 spin_lock_init(&sbi->s_next_gen_lock);
2636
Peter Zijlstra833f4072007-10-16 23:25:45 -07002637 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2638 ext4_count_free_blocks(sb));
2639 if (!err) {
2640 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2641 ext4_count_free_inodes(sb));
2642 }
2643 if (!err) {
2644 err = percpu_counter_init(&sbi->s_dirs_counter,
2645 ext4_count_dirs(sb));
2646 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002647 if (!err) {
2648 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2649 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002650 if (err) {
2651 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2652 goto failed_mount3;
2653 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002654
Alex Tomasc9de5602008-01-29 00:19:52 -05002655 sbi->s_stripe = ext4_get_stripe_size(sbi);
2656
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002657 /*
2658 * set up enough so that it can read an inode
2659 */
Theodore Ts'o9ca92382009-05-01 12:52:25 -04002660 if (!test_opt(sb, NOLOAD) &&
2661 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2662 sb->s_op = &ext4_sops;
2663 else
2664 sb->s_op = &ext4_nojournal_sops;
Mingming Cao617ba132006-10-11 01:20:53 -07002665 sb->s_export_op = &ext4_export_ops;
2666 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002667#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002668 sb->s_qcop = &ext4_qctl_operations;
2669 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002670#endif
2671 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002672 mutex_init(&sbi->s_orphan_lock);
Theodore Ts'o32ed5052009-04-25 22:53:39 -04002673 mutex_init(&sbi->s_resize_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002674
2675 sb->s_root = NULL;
2676
2677 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002678 EXT4_HAS_INCOMPAT_FEATURE(sb,
2679 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002680
2681 /*
2682 * The first inode we look at is the journal inode. Don't try
2683 * root first: it may be modified in the journal!
2684 */
2685 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002686 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2687 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002688 goto failed_mount3;
Theodore Ts'o624080e2008-06-06 17:50:40 -04002689 if (!(sb->s_flags & MS_RDONLY) &&
2690 EXT4_SB(sb)->s_journal->j_failed_commit) {
2691 printk(KERN_CRIT "EXT4-fs error (device %s): "
2692 "ext4_fill_super: Journal transaction "
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002693 "%u is corrupt\n", sb->s_id,
Theodore Ts'o624080e2008-06-06 17:50:40 -04002694 EXT4_SB(sb)->s_journal->j_failed_commit);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002695 if (test_opt(sb, ERRORS_RO)) {
2696 printk(KERN_CRIT
2697 "Mounting filesystem read-only\n");
Theodore Ts'o624080e2008-06-06 17:50:40 -04002698 sb->s_flags |= MS_RDONLY;
2699 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2700 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2701 }
2702 if (test_opt(sb, ERRORS_PANIC)) {
2703 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2704 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04002705 ext4_commit_super(sb, 1);
Theodore Ts'o624080e2008-06-06 17:50:40 -04002706 goto failed_mount4;
2707 }
2708 }
Frank Mayhar03901312009-01-07 00:06:22 -05002709 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2710 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2711 printk(KERN_ERR "EXT4-fs: required journal recovery "
2712 "suppressed and not mounted read-only\n");
2713 goto failed_mount4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002714 } else {
Frank Mayhar03901312009-01-07 00:06:22 -05002715 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2716 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2717 sbi->s_journal = NULL;
2718 needs_recovery = 0;
2719 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002720 }
2721
Jose R. Santoseb40a092007-07-18 08:37:25 -04002722 if (ext4_blocks_count(es) > 0xffffffffULL &&
2723 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2724 JBD2_FEATURE_INCOMPAT_64BIT)) {
Theodore Ts'oabda1412009-01-06 00:20:32 -05002725 printk(KERN_ERR "EXT4-fs: Failed to set 64-bit journal feature\n");
Jose R. Santoseb40a092007-07-18 08:37:25 -04002726 goto failed_mount4;
2727 }
2728
Girish Shilamkar818d2762008-01-28 23:58:27 -05002729 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2730 jbd2_journal_set_features(sbi->s_journal,
2731 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2732 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2733 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2734 jbd2_journal_set_features(sbi->s_journal,
2735 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2736 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2737 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2738 } else {
2739 jbd2_journal_clear_features(sbi->s_journal,
2740 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2741 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2742 }
2743
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002744 /* We have now updated the journal if required, so we can
2745 * validate the data journaling mode. */
2746 switch (test_opt(sb, DATA_FLAGS)) {
2747 case 0:
2748 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002749 * capabilities: ORDERED_DATA if the journal can
2750 * cope, else JOURNAL_DATA
2751 */
Mingming Caodab291a2006-10-11 01:21:01 -07002752 if (jbd2_journal_check_available_features
2753 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002754 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2755 else
2756 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2757 break;
2758
Mingming Cao617ba132006-10-11 01:20:53 -07002759 case EXT4_MOUNT_ORDERED_DATA:
2760 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002761 if (!jbd2_journal_check_available_features
2762 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002763 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002764 "requested data journaling mode\n");
2765 goto failed_mount4;
2766 }
2767 default:
2768 break;
2769 }
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002770 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002771
Frank Mayhar03901312009-01-07 00:06:22 -05002772no_journal:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002773
2774 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002775 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2776 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002777 "its supported only with writeback mode\n");
2778 clear_opt(sbi->s_mount_opt, NOBH);
2779 }
2780 }
2781 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002782 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002783 * so we can safely mount the rest of the filesystem now.
2784 */
2785
David Howells1d1fe1e2008-02-07 00:15:37 -08002786 root = ext4_iget(sb, EXT4_ROOT_INO);
2787 if (IS_ERR(root)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002788 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
David Howells1d1fe1e2008-02-07 00:15:37 -08002789 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002790 goto failed_mount4;
2791 }
2792 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002793 iput(root);
Mingming Cao617ba132006-10-11 01:20:53 -07002794 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002795 goto failed_mount4;
2796 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002797 sb->s_root = d_alloc_root(root);
2798 if (!sb->s_root) {
2799 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2800 iput(root);
2801 ret = -ENOMEM;
2802 goto failed_mount4;
2803 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002804
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002805 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002806
2807 /* determine the minimum size of new large inodes, if present */
2808 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2809 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2810 EXT4_GOOD_OLD_INODE_SIZE;
2811 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2812 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2813 if (sbi->s_want_extra_isize <
2814 le16_to_cpu(es->s_want_extra_isize))
2815 sbi->s_want_extra_isize =
2816 le16_to_cpu(es->s_want_extra_isize);
2817 if (sbi->s_want_extra_isize <
2818 le16_to_cpu(es->s_min_extra_isize))
2819 sbi->s_want_extra_isize =
2820 le16_to_cpu(es->s_min_extra_isize);
2821 }
2822 }
2823 /* Check if enough inode space is available */
2824 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2825 sbi->s_inode_size) {
2826 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2827 EXT4_GOOD_OLD_INODE_SIZE;
2828 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2829 "available.\n");
2830 }
2831
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002832 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2833 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2834 "requested data journaling mode\n");
2835 clear_opt(sbi->s_mount_opt, DELALLOC);
2836 } else if (test_opt(sb, DELALLOC))
2837 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2838
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002839 err = ext4_setup_system_zone(sb);
2840 if (err) {
2841 printk(KERN_ERR "EXT4-fs: failed to initialize system "
2842 "zone (%d)\n", err);
2843 goto failed_mount4;
2844 }
2845
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002846 ext4_ext_init(sb);
2847 err = ext4_mb_init(sb, needs_recovery);
2848 if (err) {
2849 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2850 err);
2851 goto failed_mount4;
2852 }
2853
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002854 sbi->s_kobj.kset = ext4_kset;
2855 init_completion(&sbi->s_kobj_unregister);
2856 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
2857 "%s", sb->s_id);
2858 if (err) {
2859 ext4_mb_release(sb);
2860 ext4_ext_release(sb);
2861 goto failed_mount4;
2862 };
2863
Mingming Cao617ba132006-10-11 01:20:53 -07002864 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2865 ext4_orphan_cleanup(sb, es);
2866 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05002867 if (needs_recovery) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002868 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
Frank Mayhar03901312009-01-07 00:06:22 -05002869 ext4_mark_recovery_complete(sb, es);
2870 }
2871 if (EXT4_SB(sb)->s_journal) {
2872 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2873 descr = " journalled data mode";
2874 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2875 descr = " ordered data mode";
2876 else
2877 descr = " writeback data mode";
2878 } else
2879 descr = "out journal";
2880
2881 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2882 sb->s_id, descr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002883
2884 lock_kernel();
2885 return 0;
2886
Mingming Cao617ba132006-10-11 01:20:53 -07002887cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002888 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07002889 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002890 sb->s_id);
2891 goto failed_mount;
2892
2893failed_mount4:
Frank Mayhar03901312009-01-07 00:06:22 -05002894 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002895 ext4_release_system_zone(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05002896 if (sbi->s_journal) {
2897 jbd2_journal_destroy(sbi->s_journal);
2898 sbi->s_journal = NULL;
2899 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002900failed_mount3:
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04002901 if (sbi->s_flex_groups) {
2902 if (is_vmalloc_addr(sbi->s_flex_groups))
2903 vfree(sbi->s_flex_groups);
2904 else
2905 kfree(sbi->s_flex_groups);
2906 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002907 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2908 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2909 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002910 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911failed_mount2:
2912 for (i = 0; i < db_count; i++)
2913 brelse(sbi->s_group_desc[i]);
2914 kfree(sbi->s_group_desc);
2915failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04002916 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002917 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04002918 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002919#ifdef CONFIG_QUOTA
2920 for (i = 0; i < MAXQUOTAS; i++)
2921 kfree(sbi->s_qf_names[i]);
2922#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002923 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002924 brelse(bh);
2925out_fail:
2926 sb->s_fs_info = NULL;
Manish Katiyarf6830162009-05-17 23:52:44 -04002927 kfree(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002928 kfree(sbi);
2929 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08002930 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002931}
2932
2933/*
2934 * Setup any per-fs journal parameters now. We'll do this both on
2935 * initial mount, once the journal has been initialised but before we've
2936 * done any recovery; and again on any subsequent remount.
2937 */
Mingming Cao617ba132006-10-11 01:20:53 -07002938static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002939{
Mingming Cao617ba132006-10-11 01:20:53 -07002940 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002941
Theodore Ts'o30773842009-01-03 20:27:38 -05002942 journal->j_commit_interval = sbi->s_commit_interval;
2943 journal->j_min_batch_time = sbi->s_min_batch_time;
2944 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945
2946 spin_lock(&journal->j_state_lock);
2947 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07002948 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002949 else
Mingming Caodab291a2006-10-11 01:21:01 -07002950 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04002951 if (test_opt(sb, DATA_ERR_ABORT))
2952 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2953 else
2954 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002955 spin_unlock(&journal->j_state_lock);
2956}
2957
Mingming Cao617ba132006-10-11 01:20:53 -07002958static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002959 unsigned int journal_inum)
2960{
2961 struct inode *journal_inode;
2962 journal_t *journal;
2963
Frank Mayhar03901312009-01-07 00:06:22 -05002964 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2965
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002966 /* First, test for the existence of a valid inode on disk. Bad
2967 * things happen if we iget() an unused inode, as the subsequent
2968 * iput() will try to delete it. */
2969
David Howells1d1fe1e2008-02-07 00:15:37 -08002970 journal_inode = ext4_iget(sb, journal_inum);
2971 if (IS_ERR(journal_inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002972 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002973 return NULL;
2974 }
2975 if (!journal_inode->i_nlink) {
2976 make_bad_inode(journal_inode);
2977 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002978 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002979 return NULL;
2980 }
2981
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002982 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002983 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08002984 if (!S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002985 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002986 iput(journal_inode);
2987 return NULL;
2988 }
2989
Mingming Caodab291a2006-10-11 01:21:01 -07002990 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002991 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002992 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002993 iput(journal_inode);
2994 return NULL;
2995 }
2996 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002997 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998 return journal;
2999}
3000
Mingming Cao617ba132006-10-11 01:20:53 -07003001static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003002 dev_t j_dev)
3003{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003004 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003005 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07003006 ext4_fsblk_t start;
3007 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003008 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07003009 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003010 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003011 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012 struct block_device *bdev;
3013
Frank Mayhar03901312009-01-07 00:06:22 -05003014 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3015
Mingming Cao617ba132006-10-11 01:20:53 -07003016 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003017 if (bdev == NULL)
3018 return NULL;
3019
3020 if (bd_claim(bdev, sb)) {
3021 printk(KERN_ERR
Theodore Ts'oabda1412009-01-06 00:20:32 -05003022 "EXT4-fs: failed to claim external journal device.\n");
Al Viro9a1c3542008-02-22 20:40:24 -05003023 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003024 return NULL;
3025 }
3026
3027 blocksize = sb->s_blocksize;
3028 hblock = bdev_hardsect_size(bdev);
3029 if (blocksize < hblock) {
3030 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07003031 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003032 goto out_bdev;
3033 }
3034
Mingming Cao617ba132006-10-11 01:20:53 -07003035 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3036 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003037 set_blocksize(bdev, blocksize);
3038 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07003039 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003040 "external journal\n");
3041 goto out_bdev;
3042 }
3043
Mingming Cao617ba132006-10-11 01:20:53 -07003044 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3045 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003046 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07003047 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3048 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003049 "bad superblock\n");
3050 brelse(bh);
3051 goto out_bdev;
3052 }
3053
Mingming Cao617ba132006-10-11 01:20:53 -07003054 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3055 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003056 brelse(bh);
3057 goto out_bdev;
3058 }
3059
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003060 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003061 start = sb_block + 1;
3062 brelse(bh); /* we're done with the superblock */
3063
Mingming Caodab291a2006-10-11 01:21:01 -07003064 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065 start, len, blocksize);
3066 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07003067 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003068 goto out_bdev;
3069 }
3070 journal->j_private = sb;
3071 ll_rw_block(READ, 1, &journal->j_sb_buffer);
3072 wait_on_buffer(journal->j_sb_buffer);
3073 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003074 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075 goto out_journal;
3076 }
3077 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07003078 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003079 "user (unsupported) - %d\n",
3080 be32_to_cpu(journal->j_superblock->s_nr_users));
3081 goto out_journal;
3082 }
Mingming Cao617ba132006-10-11 01:20:53 -07003083 EXT4_SB(sb)->journal_bdev = bdev;
3084 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003085 return journal;
3086out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07003087 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003088out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07003089 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003090 return NULL;
3091}
3092
Mingming Cao617ba132006-10-11 01:20:53 -07003093static int ext4_load_journal(struct super_block *sb,
3094 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003095 unsigned long journal_devnum)
3096{
3097 journal_t *journal;
3098 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3099 dev_t journal_dev;
3100 int err = 0;
3101 int really_read_only;
3102
Frank Mayhar03901312009-01-07 00:06:22 -05003103 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3104
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003105 if (journal_devnum &&
3106 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003107 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003108 "numbers have changed\n");
3109 journal_dev = new_decode_dev(journal_devnum);
3110 } else
3111 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3112
3113 really_read_only = bdev_read_only(sb->s_bdev);
3114
3115 /*
3116 * Are we loading a blank journal or performing recovery after a
3117 * crash? For recovery, we need to check in advance whether we
3118 * can get read-write access to the device.
3119 */
3120
Mingming Cao617ba132006-10-11 01:20:53 -07003121 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003122 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07003123 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003124 "required on readonly filesystem.\n");
3125 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07003126 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003127 "unavailable, cannot proceed.\n");
3128 return -EROFS;
3129 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003130 printk(KERN_INFO "EXT4-fs: write access will "
3131 "be enabled during recovery.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003132 }
3133 }
3134
3135 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07003136 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003137 "and inode journals!\n");
3138 return -EINVAL;
3139 }
3140
3141 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07003142 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003143 return -EINVAL;
3144 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003145 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003146 return -EINVAL;
3147 }
3148
Theodore Ts'o4776004f2008-09-08 23:00:52 -04003149 if (journal->j_flags & JBD2_BARRIER)
3150 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
3151 else
3152 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
3153
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003154 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07003155 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003156 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07003157 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07003158 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003159 return err;
3160 }
3161 }
3162
Mingming Cao617ba132006-10-11 01:20:53 -07003163 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07003164 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003165 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07003166 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003167
3168 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07003169 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07003170 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003171 return err;
3172 }
3173
Mingming Cao617ba132006-10-11 01:20:53 -07003174 EXT4_SB(sb)->s_journal = journal;
3175 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003176
3177 if (journal_devnum &&
3178 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3179 es->s_journal_dev = cpu_to_le32(journal_devnum);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003180
3181 /* Make sure we flush the recovery flag to disk. */
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003182 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003183 }
3184
3185 return 0;
3186}
3187
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003188static int ext4_commit_super(struct super_block *sb, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003189{
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003190 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Mingming Cao617ba132006-10-11 01:20:53 -07003191 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Takashi Satoc4be0c12009-01-09 16:40:58 -08003192 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003193
3194 if (!sbh)
Takashi Satoc4be0c12009-01-09 16:40:58 -08003195 return error;
Theodore Ts'o914258b2008-10-06 21:35:40 -04003196 if (buffer_write_io_error(sbh)) {
3197 /*
3198 * Oh, dear. A previous attempt to write the
3199 * superblock failed. This could happen because the
3200 * USB device was yanked out. Or it could happen to
3201 * be a transient write error and maybe the block will
3202 * be remapped. Nothing we can do but to retry the
3203 * write and hope for the best.
3204 */
Theodore Ts'oabda1412009-01-06 00:20:32 -05003205 printk(KERN_ERR "EXT4-fs: previous I/O error to "
Theodore Ts'o914258b2008-10-06 21:35:40 -04003206 "superblock detected for %s.\n", sb->s_id);
3207 clear_buffer_write_io_error(sbh);
3208 set_buffer_uptodate(sbh);
3209 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003210 es->s_wtime = cpu_to_le32(get_seconds());
Theodore Ts'oafc32f72009-02-28 19:39:58 -05003211 es->s_kbytes_written =
3212 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3213 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3214 EXT4_SB(sb)->s_sectors_written_start) >> 1));
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003215 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
3216 &EXT4_SB(sb)->s_freeblocks_counter));
3217 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
3218 &EXT4_SB(sb)->s_freeinodes_counter));
Theodore Ts'o7234ab22009-04-30 21:24:04 -04003219 sb->s_dirt = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003220 BUFFER_TRACE(sbh, "marking dirty");
3221 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04003222 if (sync) {
Takashi Satoc4be0c12009-01-09 16:40:58 -08003223 error = sync_dirty_buffer(sbh);
3224 if (error)
3225 return error;
3226
3227 error = buffer_write_io_error(sbh);
3228 if (error) {
Theodore Ts'oabda1412009-01-06 00:20:32 -05003229 printk(KERN_ERR "EXT4-fs: I/O error while writing "
Theodore Ts'o914258b2008-10-06 21:35:40 -04003230 "superblock for %s.\n", sb->s_id);
3231 clear_buffer_write_io_error(sbh);
3232 set_buffer_uptodate(sbh);
3233 }
3234 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08003235 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003236}
3237
3238
3239/*
3240 * Have we just finished recovery? If so, and if we are mounting (or
3241 * remounting) the filesystem readonly, then we will end up with a
3242 * consistent fs on disk. Record that fact.
3243 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003244static void ext4_mark_recovery_complete(struct super_block *sb,
3245 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003246{
Mingming Cao617ba132006-10-11 01:20:53 -07003247 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003248
Frank Mayhar03901312009-01-07 00:06:22 -05003249 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3250 BUG_ON(journal != NULL);
3251 return;
3252 }
Mingming Caodab291a2006-10-11 01:21:01 -07003253 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003254 if (jbd2_journal_flush(journal) < 0)
3255 goto out;
3256
Mingming Cao617ba132006-10-11 01:20:53 -07003257 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003258 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07003259 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003260 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003261 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003262
3263out:
Mingming Caodab291a2006-10-11 01:21:01 -07003264 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003265}
3266
3267/*
3268 * If we are mounting (or read-write remounting) a filesystem whose journal
3269 * has recorded an error from a previous lifetime, move that error to the
3270 * main filesystem now.
3271 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003272static void ext4_clear_journal_err(struct super_block *sb,
3273 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003274{
3275 journal_t *journal;
3276 int j_errno;
3277 const char *errstr;
3278
Frank Mayhar03901312009-01-07 00:06:22 -05003279 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3280
Mingming Cao617ba132006-10-11 01:20:53 -07003281 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003282
3283 /*
3284 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07003285 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003286 */
3287
Mingming Caodab291a2006-10-11 01:21:01 -07003288 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003289 if (j_errno) {
3290 char nbuf[16];
3291
Mingming Cao617ba132006-10-11 01:20:53 -07003292 errstr = ext4_decode_error(sb, j_errno, nbuf);
Harvey Harrison46e665e2008-04-17 10:38:59 -04003293 ext4_warning(sb, __func__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003294 "from previous mount: %s", errstr);
Harvey Harrison46e665e2008-04-17 10:38:59 -04003295 ext4_warning(sb, __func__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003296 "filesystem check.");
3297
Mingming Cao617ba132006-10-11 01:20:53 -07003298 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3299 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003300 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003301
Mingming Caodab291a2006-10-11 01:21:01 -07003302 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003303 }
3304}
3305
3306/*
3307 * Force the running and committing transactions to commit,
3308 * and wait on the commit.
3309 */
Mingming Cao617ba132006-10-11 01:20:53 -07003310int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003311{
3312 journal_t *journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003313 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003314
3315 if (sb->s_flags & MS_RDONLY)
3316 return 0;
3317
Mingming Cao617ba132006-10-11 01:20:53 -07003318 journal = EXT4_SB(sb)->s_journal;
Theodore Ts'o7234ab22009-04-30 21:24:04 -04003319 if (journal)
Frank Mayhar03901312009-01-07 00:06:22 -05003320 ret = ext4_journal_force_commit(journal);
Frank Mayhar03901312009-01-07 00:06:22 -05003321
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003322 return ret;
3323}
3324
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003325static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003326{
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003327 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003328}
3329
Mingming Cao617ba132006-10-11 01:20:53 -07003330static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003331{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003332 int ret = 0;
Jan Kara9eddacf2009-02-10 06:46:05 -05003333 tid_t target;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003334
Theodore Ts'oede86cc2008-10-05 20:50:06 -04003335 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003336 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
3337 if (wait)
3338 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
Frank Mayhar03901312009-01-07 00:06:22 -05003339 }
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003340 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003341}
3342
3343/*
3344 * LVM calls this function before a (read-only) snapshot is created. This
3345 * gives us a chance to flush the journal completely and mark the fs clean.
3346 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003347static int ext4_freeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003348{
Takashi Satoc4be0c12009-01-09 16:40:58 -08003349 int error = 0;
3350 journal_t *journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003351
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003352 if (sb->s_flags & MS_RDONLY)
3353 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003354
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003355 journal = EXT4_SB(sb)->s_journal;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003356
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003357 /* Now we set up the journal barrier. */
3358 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003359
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003360 /*
3361 * Don't clear the needs_recovery flag if we failed to flush
3362 * the journal.
3363 */
3364 error = jbd2_journal_flush(journal);
3365 if (error < 0) {
3366 out:
3367 jbd2_journal_unlock_updates(journal);
3368 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003369 }
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003370
3371 /* Journal blocked and flushed, clear needs_recovery flag. */
3372 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3373 error = ext4_commit_super(sb, 1);
3374 if (error)
3375 goto out;
Takashi Satoc4be0c12009-01-09 16:40:58 -08003376 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003377}
3378
3379/*
3380 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3381 * flag here, even though the filesystem is not technically dirty yet.
3382 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003383static int ext4_unfreeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003384{
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003385 if (sb->s_flags & MS_RDONLY)
3386 return 0;
3387
3388 lock_super(sb);
3389 /* Reset the needs_recovery flag before the fs is unlocked. */
3390 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3391 ext4_commit_super(sb, 1);
3392 unlock_super(sb);
3393 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Takashi Satoc4be0c12009-01-09 16:40:58 -08003394 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003395}
3396
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003397static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003398{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003399 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003400 struct ext4_sb_info *sbi = EXT4_SB(sb);
3401 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003402 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003403 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003404 ext4_group_t g;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003405 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003406 int err;
3407#ifdef CONFIG_QUOTA
3408 int i;
3409#endif
3410
3411 /* Store the original options */
3412 old_sb_flags = sb->s_flags;
3413 old_opts.s_mount_opt = sbi->s_mount_opt;
3414 old_opts.s_resuid = sbi->s_resuid;
3415 old_opts.s_resgid = sbi->s_resgid;
3416 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003417 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3418 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003419#ifdef CONFIG_QUOTA
3420 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3421 for (i = 0; i < MAXQUOTAS; i++)
3422 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3423#endif
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003424 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3425 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003426
3427 /*
3428 * Allow the "check" option to be passed as a remount option.
3429 */
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003430 if (!parse_options(data, sb, NULL, &journal_ioprio,
3431 &n_blocks_count, 1)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003432 err = -EINVAL;
3433 goto restore_opts;
3434 }
3435
Mingming Cao617ba132006-10-11 01:20:53 -07003436 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
Harvey Harrison46e665e2008-04-17 10:38:59 -04003437 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003438
3439 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07003440 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003441
3442 es = sbi->s_es;
3443
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003444 if (sbi->s_journal) {
Frank Mayhar03901312009-01-07 00:06:22 -05003445 ext4_init_journal_params(sb, sbi->s_journal);
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003446 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3447 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003448
3449 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003450 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003451 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003452 err = -EROFS;
3453 goto restore_opts;
3454 }
3455
3456 if (*flags & MS_RDONLY) {
3457 /*
3458 * First of all, the unconditional stuff we have to do
3459 * to disable replay of the journal when we next remount
3460 */
3461 sb->s_flags |= MS_RDONLY;
3462
3463 /*
3464 * OK, test if we are remounting a valid rw partition
3465 * readonly, and if so set the rdonly flag and then
3466 * mark the partition as valid again.
3467 */
Mingming Cao617ba132006-10-11 01:20:53 -07003468 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3469 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003470 es->s_state = cpu_to_le16(sbi->s_mount_state);
3471
Theodore Ts'oa63c9eb2009-05-01 01:59:42 -04003472 if (sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05003473 ext4_mark_recovery_complete(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003474 } else {
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003475 int ret;
Mingming Cao617ba132006-10-11 01:20:53 -07003476 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3477 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3478 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003479 "remount RDWR because of unsupported "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003480 "optional features (%x).\n", sb->s_id,
3481 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3482 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003483 err = -EROFS;
3484 goto restore_opts;
3485 }
Eric Sandeenead65962007-02-10 01:46:08 -08003486
3487 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003488 * Make sure the group descriptor checksums
3489 * are sane. If they aren't, refuse to
3490 * remount r/w.
3491 */
3492 for (g = 0; g < sbi->s_groups_count; g++) {
3493 struct ext4_group_desc *gdp =
3494 ext4_get_group_desc(sb, g, NULL);
3495
3496 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3497 printk(KERN_ERR
3498 "EXT4-fs: ext4_remount: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003499 "Checksum for group %u failed (%u!=%u)\n",
Theodore Ts'o8a266462008-07-26 14:34:21 -04003500 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3501 le16_to_cpu(gdp->bg_checksum));
3502 err = -EINVAL;
3503 goto restore_opts;
3504 }
3505 }
3506
3507 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003508 * If we have an unprocessed orphan list hanging
3509 * around from a previously readonly bdev mount,
3510 * require a full umount/remount for now.
3511 */
3512 if (es->s_last_orphan) {
3513 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3514 "remount RDWR because of unprocessed "
3515 "orphan inode list. Please "
3516 "umount/remount instead.\n",
3517 sb->s_id);
3518 err = -EINVAL;
3519 goto restore_opts;
3520 }
3521
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003522 /*
3523 * Mounting a RDONLY partition read-write, so reread
3524 * and store the current valid flag. (It may have
3525 * been changed by e2fsck since we originally mounted
3526 * the partition.)
3527 */
Frank Mayhar03901312009-01-07 00:06:22 -05003528 if (sbi->s_journal)
3529 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003530 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003531 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003532 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003533 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003534 sb->s_flags &= ~MS_RDONLY;
3535 }
3536 }
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003537 ext4_setup_system_zone(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05003538 if (sbi->s_journal == NULL)
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003539 ext4_commit_super(sb, 1);
Frank Mayhar03901312009-01-07 00:06:22 -05003540
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003541#ifdef CONFIG_QUOTA
3542 /* Release old quota file names */
3543 for (i = 0; i < MAXQUOTAS; i++)
3544 if (old_opts.s_qf_names[i] &&
3545 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3546 kfree(old_opts.s_qf_names[i]);
3547#endif
3548 return 0;
3549restore_opts:
3550 sb->s_flags = old_sb_flags;
3551 sbi->s_mount_opt = old_opts.s_mount_opt;
3552 sbi->s_resuid = old_opts.s_resuid;
3553 sbi->s_resgid = old_opts.s_resgid;
3554 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003555 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3556 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003557#ifdef CONFIG_QUOTA
3558 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3559 for (i = 0; i < MAXQUOTAS; i++) {
3560 if (sbi->s_qf_names[i] &&
3561 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3562 kfree(sbi->s_qf_names[i]);
3563 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3564 }
3565#endif
3566 return err;
3567}
3568
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003569static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003570{
3571 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003572 struct ext4_sb_info *sbi = EXT4_SB(sb);
3573 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003574 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003575
Badari Pulavarty5e700302007-07-15 23:42:00 -07003576 if (test_opt(sb, MINIX_DF)) {
3577 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003578 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04003579 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003580 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003581
3582 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003583 * Compute the overhead (FS structures). This is constant
3584 * for a given filesystem unless the number of block groups
3585 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003586 */
3587
3588 /*
3589 * All of the blocks before first_data_block are
3590 * overhead
3591 */
3592 overhead = le32_to_cpu(es->s_first_data_block);
3593
3594 /*
3595 * Add the overhead attributed to the superblock and
3596 * block group descriptors. If the sparse superblocks
3597 * feature is turned on, then not all groups have this.
3598 */
3599 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003600 overhead += ext4_bg_has_super(sb, i) +
3601 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003602 cond_resched();
3603 }
3604
3605 /*
3606 * Every block group has an inode bitmap, a block
3607 * bitmap, and an inode table.
3608 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003609 overhead += ngroups * (2 + sbi->s_itb_per_group);
3610 sbi->s_overhead_last = overhead;
3611 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003612 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003613 }
3614
Mingming Cao617ba132006-10-11 01:20:53 -07003615 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003616 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003617 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003618 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3619 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Aneesh Kumar K.V308ba3e2007-10-16 18:38:25 -04003620 ext4_free_blocks_count_set(es, buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003621 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3622 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003623 buf->f_bavail = 0;
3624 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003625 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003626 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07003627 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003628 fsid = le64_to_cpup((void *)es->s_uuid) ^
3629 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3630 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3631 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003632 return 0;
3633}
3634
3635/* Helper function for writing quotas on sync - we need to start transaction before quota file
3636 * is locked for write. Otherwise the are possible deadlocks:
3637 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003638 * ext4_create() quota_sync()
Jan Karaa269eb12009-01-26 17:04:39 +01003639 * jbd2_journal_start() write_dquot()
3640 * vfs_dq_init() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003641 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003642 *
3643 */
3644
3645#ifdef CONFIG_QUOTA
3646
3647static inline struct inode *dquot_to_inode(struct dquot *dquot)
3648{
3649 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3650}
3651
Mingming Cao617ba132006-10-11 01:20:53 -07003652static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003653{
3654 int ret, err;
3655 handle_t *handle;
3656 struct inode *inode;
3657
3658 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003659 handle = ext4_journal_start(inode,
3660 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003661 if (IS_ERR(handle))
3662 return PTR_ERR(handle);
3663 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003664 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003665 if (!ret)
3666 ret = err;
3667 return ret;
3668}
3669
Mingming Cao617ba132006-10-11 01:20:53 -07003670static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003671{
3672 int ret, err;
3673 handle_t *handle;
3674
Mingming Cao617ba132006-10-11 01:20:53 -07003675 handle = ext4_journal_start(dquot_to_inode(dquot),
3676 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003677 if (IS_ERR(handle))
3678 return PTR_ERR(handle);
3679 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003680 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003681 if (!ret)
3682 ret = err;
3683 return ret;
3684}
3685
Mingming Cao617ba132006-10-11 01:20:53 -07003686static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003687{
3688 int ret, err;
3689 handle_t *handle;
3690
Mingming Cao617ba132006-10-11 01:20:53 -07003691 handle = ext4_journal_start(dquot_to_inode(dquot),
3692 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003693 if (IS_ERR(handle)) {
3694 /* Release dquot anyway to avoid endless cycle in dqput() */
3695 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003696 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003697 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003698 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003699 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003700 if (!ret)
3701 ret = err;
3702 return ret;
3703}
3704
Mingming Cao617ba132006-10-11 01:20:53 -07003705static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003706{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003707 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003708 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3709 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003710 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003711 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003712 } else {
3713 return dquot_mark_dquot_dirty(dquot);
3714 }
3715}
3716
Mingming Cao617ba132006-10-11 01:20:53 -07003717static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003718{
3719 int ret, err;
3720 handle_t *handle;
3721
3722 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003723 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003724 if (IS_ERR(handle))
3725 return PTR_ERR(handle);
3726 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003727 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003728 if (!ret)
3729 ret = err;
3730 return ret;
3731}
3732
3733/*
3734 * Turn on quotas during mount time - we need to find
3735 * the quota file and such...
3736 */
Mingming Cao617ba132006-10-11 01:20:53 -07003737static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003738{
Mingming Cao617ba132006-10-11 01:20:53 -07003739 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3740 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003741}
3742
3743/*
3744 * Standard function to be called on quota_on
3745 */
Mingming Cao617ba132006-10-11 01:20:53 -07003746static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro82646132008-08-02 00:57:06 -04003747 char *name, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003748{
3749 int err;
Al Viro82646132008-08-02 00:57:06 -04003750 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003751
3752 if (!test_opt(sb, QUOTA))
3753 return -EINVAL;
Al Viro82646132008-08-02 00:57:06 -04003754 /* When remounting, no checks are needed and in fact, name is NULL */
Jan Kara06235432008-05-13 19:11:51 -04003755 if (remount)
Al Viro82646132008-08-02 00:57:06 -04003756 return vfs_quota_on(sb, type, format_id, name, remount);
Jan Kara06235432008-05-13 19:11:51 -04003757
Al Viro82646132008-08-02 00:57:06 -04003758 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003759 if (err)
3760 return err;
Jan Kara06235432008-05-13 19:11:51 -04003761
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003762 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04003763 if (path.mnt->mnt_sb != sb) {
3764 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003765 return -EXDEV;
3766 }
Jan Kara06235432008-05-13 19:11:51 -04003767 /* Journaling quota? */
3768 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003769 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04003770 if (path.dentry->d_parent != sb->s_root)
Jan Kara06235432008-05-13 19:11:51 -04003771 printk(KERN_WARNING
3772 "EXT4-fs: Quota file not on filesystem root. "
3773 "Journaled quota will not work.\n");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003774 }
Jan Kara06235432008-05-13 19:11:51 -04003775
3776 /*
3777 * When we journal data on quota file, we have to flush journal to see
3778 * all updates to the file when we bypass pagecache...
3779 */
Frank Mayhar03901312009-01-07 00:06:22 -05003780 if (EXT4_SB(sb)->s_journal &&
3781 ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04003782 /*
3783 * We don't need to lock updates but journal_flush() could
3784 * otherwise be livelocked...
3785 */
3786 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003787 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04003788 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003789 if (err) {
Al Viro82646132008-08-02 00:57:06 -04003790 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003791 return err;
3792 }
Jan Kara06235432008-05-13 19:11:51 -04003793 }
3794
Al Viro82646132008-08-02 00:57:06 -04003795 err = vfs_quota_on_path(sb, type, format_id, &path);
3796 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04003797 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003798}
3799
3800/* Read data from quotafile - avoid pagecache and such because we cannot afford
3801 * acquiring the locks... As quota files are never truncated and quota code
3802 * itself serializes the operations (and noone else should touch the files)
3803 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003804static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003805 size_t len, loff_t off)
3806{
3807 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003808 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003809 int err = 0;
3810 int offset = off & (sb->s_blocksize - 1);
3811 int tocopy;
3812 size_t toread;
3813 struct buffer_head *bh;
3814 loff_t i_size = i_size_read(inode);
3815
3816 if (off > i_size)
3817 return 0;
3818 if (off+len > i_size)
3819 len = i_size-off;
3820 toread = len;
3821 while (toread > 0) {
3822 tocopy = sb->s_blocksize - offset < toread ?
3823 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003824 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003825 if (err)
3826 return err;
3827 if (!bh) /* A hole? */
3828 memset(data, 0, tocopy);
3829 else
3830 memcpy(data, bh->b_data+offset, tocopy);
3831 brelse(bh);
3832 offset = 0;
3833 toread -= tocopy;
3834 data += tocopy;
3835 blk++;
3836 }
3837 return len;
3838}
3839
3840/* Write to quotafile (we know the transaction is already started and has
3841 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003842static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003843 const char *data, size_t len, loff_t off)
3844{
3845 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003846 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003847 int err = 0;
3848 int offset = off & (sb->s_blocksize - 1);
3849 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003850 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003851 size_t towrite = len;
3852 struct buffer_head *bh;
3853 handle_t *handle = journal_current_handle();
3854
Frank Mayhar03901312009-01-07 00:06:22 -05003855 if (EXT4_SB(sb)->s_journal && !handle) {
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003856 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
Jan Kara9c3013e2007-09-11 15:23:29 -07003857 " cancelled because transaction is not started.\n",
3858 (unsigned long long)off, (unsigned long long)len);
3859 return -EIO;
3860 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003861 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3862 while (towrite > 0) {
3863 tocopy = sb->s_blocksize - offset < towrite ?
3864 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003865 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003866 if (!bh)
3867 goto out;
3868 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003869 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003870 if (err) {
3871 brelse(bh);
3872 goto out;
3873 }
3874 }
3875 lock_buffer(bh);
3876 memcpy(bh->b_data+offset, data, tocopy);
3877 flush_dcache_page(bh->b_page);
3878 unlock_buffer(bh);
3879 if (journal_quota)
Frank Mayhar03901312009-01-07 00:06:22 -05003880 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003881 else {
3882 /* Always do at least ordered writes for quotas */
Jan Kara678aaf42008-07-11 19:27:31 -04003883 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003884 mark_buffer_dirty(bh);
3885 }
3886 brelse(bh);
3887 if (err)
3888 goto out;
3889 offset = 0;
3890 towrite -= tocopy;
3891 data += tocopy;
3892 blk++;
3893 }
3894out:
Jan Kara4d04e4f2008-07-04 09:59:34 -07003895 if (len == towrite) {
3896 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003897 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07003898 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003899 if (inode->i_size < off+len-towrite) {
3900 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07003901 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003902 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003903 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003904 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003905 mutex_unlock(&inode->i_mutex);
3906 return len - towrite;
3907}
3908
3909#endif
3910
Mingming Cao617ba132006-10-11 01:20:53 -07003911static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003912 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3913{
Mingming Cao617ba132006-10-11 01:20:53 -07003914 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003915}
3916
Theodore Ts'o03010a32008-10-10 20:02:48 -04003917static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003918 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04003919 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07003920 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003921 .kill_sb = kill_block_super,
3922 .fs_flags = FS_REQUIRES_DEV,
3923};
3924
Theodore Ts'o03010a32008-10-10 20:02:48 -04003925#ifdef CONFIG_EXT4DEV_COMPAT
3926static int ext4dev_get_sb(struct file_system_type *fs_type,
3927 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3928{
3929 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3930 "to mount using ext4\n");
3931 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3932 "will go away by 2.6.31\n");
3933 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3934}
3935
3936static struct file_system_type ext4dev_fs_type = {
3937 .owner = THIS_MODULE,
3938 .name = "ext4dev",
3939 .get_sb = ext4dev_get_sb,
3940 .kill_sb = kill_block_super,
3941 .fs_flags = FS_REQUIRES_DEV,
3942};
3943MODULE_ALIAS("ext4dev");
3944#endif
3945
Mingming Cao617ba132006-10-11 01:20:53 -07003946static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003947{
Alex Tomasc9de5602008-01-29 00:19:52 -05003948 int err;
3949
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003950 err = init_ext4_system_zone();
3951 if (err)
3952 return err;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04003953 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
3954 if (!ext4_kset)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003955 goto out4;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003956 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003957 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003958 if (err)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003959 goto out3;
Alex Tomasc9de5602008-01-29 00:19:52 -05003960
3961 err = init_ext4_xattr();
3962 if (err)
3963 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003964 err = init_inodecache();
3965 if (err)
3966 goto out1;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003967 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003968 if (err)
3969 goto out;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003970#ifdef CONFIG_EXT4DEV_COMPAT
3971 err = register_filesystem(&ext4dev_fs_type);
3972 if (err) {
3973 unregister_filesystem(&ext4_fs_type);
3974 goto out;
3975 }
3976#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003977 return 0;
3978out:
3979 destroy_inodecache();
3980out1:
Mingming Cao617ba132006-10-11 01:20:53 -07003981 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003982out2:
3983 exit_ext4_mballoc();
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003984out3:
3985 remove_proc_entry("fs/ext4", NULL);
3986 kset_unregister(ext4_kset);
3987out4:
3988 exit_ext4_system_zone();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003989 return err;
3990}
3991
Mingming Cao617ba132006-10-11 01:20:53 -07003992static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003993{
Theodore Ts'o03010a32008-10-10 20:02:48 -04003994 unregister_filesystem(&ext4_fs_type);
3995#ifdef CONFIG_EXT4DEV_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07003996 unregister_filesystem(&ext4dev_fs_type);
Theodore Ts'o03010a32008-10-10 20:02:48 -04003997#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003998 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07003999 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05004000 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04004001 remove_proc_entry("fs/ext4", NULL);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04004002 kset_unregister(ext4_kset);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004003 exit_ext4_system_zone();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004004}
4005
4006MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Theodore Ts'o83982b62009-01-06 14:53:16 -05004007MODULE_DESCRIPTION("Fourth Extended Filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004008MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07004009module_init(init_ext4_fs)
4010module_exit(exit_ext4_fs)